1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
package crud
import (
"bytes"
"fmt"
"os"
"strings"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"gitlab.com/whiteboardguy/eno/internal/editor"
"gitlab.com/whiteboardguy/eno/internal/utils"
)
func PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, error) {
useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true)
if err != nil {
return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
}
if !useDefault {
fmt.Println("Warning: you will not be able to open or edit this note again.")
fmt.Println(" make sure you've used the '--edit' flag.")
}
addMore, err := utils.AskUserYesNo("Do you want to add more public keys?", false)
if err != nil {
return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
}
pubKeys := [][]byte{}
if useDefault {
pubKeys = append(pubKeys, defaultPubKeyBytes)
}
if addMore {
for {
fmt.Println("Enter public keys (\033[1mcomma-separated for multiple\033[0m):")
fmt.Println("Example: ~/keys/bob.pub, ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AA..., bob@github, james@gitlab")
fmt.Print("Public Keys: ")
utils.StdinScanner.Scan()
if err := utils.StdinScanner.Err(); err != nil {
return nil, false, fmt.Errorf("failed to read public key: %v", err)
}
input := strings.TrimSpace(utils.StdinScanner.Text())
if input == "" {
continue
}
for part := range strings.SplitSeq(input, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
if strings.Contains(part, "@") && !strings.HasPrefix(part, "ssh-") {
parts := strings.Split(part, "@")
if len(parts) != 2 {
return nil, false, fmt.Errorf("unsupported source or invalid key format: %s", part)
}
username, service := parts[0], parts[1]
body, err := utils.GetPubKeyFromService(username, service)
if err != nil {
return nil, false, fmt.Errorf("failed to get public key from %s: %v", part, err)
}
selectedKeys, err := selectServiceKeys(part, body)
if err != nil {
fmt.Printf("Warning: %s: %v\n", service, err)
continue
}
for _, k := range selectedKeys {
pubKeys = append(pubKeys, bytes.TrimSpace(k))
}
continue
}
var keyBytes []byte
if strings.HasPrefix(part, "ssh-") {
keyBytes = []byte(part)
} else {
var err error
keyBytes, err = os.ReadFile(part)
if err != nil {
return nil, false, fmt.Errorf("failed to read public key file %s: %v", part, err)
}
}
pubKeys = append(pubKeys, bytes.TrimSpace(keyBytes))
}
more, err := utils.AskUserYesNo("Add more public keys?", false)
if err != nil {
return nil, false, fmt.Errorf("failed to read confirmation: %v", err)
}
if !more {
break
}
}
}
return pubKeys, useDefault, nil
}
func NewNoteSSH(output string, defaultPubKeyBytes []byte) error {
pubKeys, _, err := PromptRecipientKeys(defaultPubKeyBytes)
if err != nil {
return err
}
if len(pubKeys) == 0 {
return fmt.Errorf("at least one recipient public key is required")
}
tmptxt := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...)
encrypted, _, err := crypt.EncryptBytesSSH(tmptxt, pubKeys)
if err != nil {
return err
}
fmt.Println(output)
file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(encrypted)
if err != nil {
return err
}
file.Sync()
// utils.ClearTerm()
fmt.Println("New note saved at:", file.Name())
return nil
}
func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath string, editorString string) error {
pubKeys, useDefault, err := PromptRecipientKeys(defaultPubKeyBytes)
if err != nil {
return err
}
if len(pubKeys) == 0 {
return fmt.Errorf("at least one recipient public key is required")
}
noteInfo, err := os.Stat(output)
preExisting := err == nil
initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...)
encrypted, noteKey, err := crypt.EncryptBytesSSH(initialContent, pubKeys)
if err != nil {
return err
}
crypt.TryMlock(noteKey)
defer clear(noteKey)
file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
_, err = file.Write(encrypted)
if err != nil {
return err
}
file.Sync()
file.Close()
ciphertext, err := os.ReadFile(output)
if err != nil {
return err
}
tmpFile, err := utils.SecureTempFile()
if err != nil {
return err
}
var plaintext []byte
if useDefault {
plaintext, err = crypt.DecryptBytesSSH(ciphertext, defaultPrivKeyPath)
} else {
plaintext, err = crypt.DecryptBytesSSHWithKey(ciphertext, noteKey)
}
if err != nil {
return err
}
crypt.TryMlock(plaintext)
defer clear(plaintext)
defer os.Remove(tmpFile.Name())
defer deferredCleanTmpFile(tmpFile, len(plaintext))
_, err = tmpFile.Write(plaintext)
if err != nil {
return err
}
err = editor.OpenWithEditor(tmpFile.Name(), editorString)
if err != nil {
return err
}
newContent, err := os.ReadFile(tmpFile.Name())
if err != nil {
return err
}
crypt.TryMlock(newContent)
defer clear(newContent)
if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err)
}
encrypted, _, err = crypt.EncryptBytesSSH(newContent, pubKeys)
if err != nil {
return err
}
perm := os.FileMode(0600)
if preExisting {
perm = noteInfo.Mode().Perm()
}
err = os.WriteFile(output, encrypted, perm)
fmt.Println("New note saved at:", output)
return err
}
|