-
Notifications
You must be signed in to change notification settings - Fork 0
/
keep.go
417 lines (368 loc) · 10.6 KB
/
keep.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package keep
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/jcmdev0/gpgagent"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/ssh/terminal"
)
const (
validChars = "abcdefijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789~-_=+(){}@#&$€"
)
// NewPassword return a randomly generated password of the requested length
func NewPassword(length int) ([]byte, error) {
password := make([]byte, length)
l := int64(len(validChars) - 1)
for i := 0; i < length; i++ {
randN, err := rand.Int(rand.Reader, big.NewInt(l))
if err != nil {
return nil, err
}
password[i] = validChars[randN.Int64()]
}
return password, nil
}
func getKeyRing(keyringPath string) (el openpgp.EntityList, err error) {
// Read in public key
keyringFileBuffer, err := os.Open(keyringPath)
if err != nil {
return nil, err
}
defer keyringFileBuffer.Close()
el, err = openpgp.ReadKeyRing(keyringFileBuffer)
if err != nil {
return nil, err
}
return el, nil
}
func filterEntityList(el openpgp.EntityList, recipients string) openpgp.EntityList {
rs := strings.Split(recipients, " ")
fel := make([]*openpgp.Entity, 0, len(rs))
for _, r := range rs {
for _, e := range el {
if r == e.PrimaryKey.KeyIdShortString() {
fel = append(fel, e)
}
}
}
return fel
}
func decodeFile(el openpgp.EntityList, pf openpgp.PromptFunction, fpath string) (*openpgp.MessageDetails, error) {
// Get the encrypted file content as a []byte
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
result, err := armor.Decode(f)
if err != nil {
return nil, err
}
// Decrypt it with the contents of the private key
return openpgp.ReadMessage(result.Body, el, pf, nil)
}
func promptFromString(passphrase string) openpgp.PromptFunction {
return func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
for _, k := range keys {
ID := k.PrivateKey.KeyIdShortString()
fmt.Printf("Passphrase to unlock your key (%s) : ", ID)
err := k.PrivateKey.Decrypt([]byte(passphrase))
if err != nil {
fmt.Println("\nAn error occurred while decrypting the key", err)
return nil, err
}
return []byte(passphrase), nil
}
return nil, fmt.Errorf("Unable to find key")
}
}
func promptFunctionGpgAgent() openpgp.PromptFunction {
return func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
conn, err := gpgagent.NewGpgAgentConn()
if err != nil {
return nil, err
}
defer conn.Close()
for _, key := range keys {
cacheID := strings.ToUpper(hex.EncodeToString(key.PublicKey.Fingerprint[:]))
request := gpgagent.PassphraseRequest{CacheKey: cacheID}
passphrase, err := conn.GetPassphrase(&request)
if err != nil {
return nil, err
}
err = key.PrivateKey.Decrypt([]byte(passphrase))
if err != nil {
err := conn.RemoveFromCache(cacheID)
if err != nil {
err = fmt.Errorf("cannot remove the key from cache: %s", err)
}
err = fmt.Errorf("can t decrypt: %s", err)
return nil, err
}
return []byte(passphrase), nil
}
return nil, fmt.Errorf("Unable to find key")
}
}
func promptTerminal(keys []openpgp.Key, symmetric bool) ([]byte, error) {
for _, k := range keys {
ID := k.PrivateKey.KeyIdShortString()
fmt.Printf("Passphrase to unlock your key (%s) : ", ID)
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return nil, err
}
err = k.PrivateKey.Decrypt(pw)
if err != nil {
fmt.Println("\nAn error occurred while decrypting the key", err)
return nil, err
}
return pw, nil
}
return nil, fmt.Errorf("Unable to find key")
}
// GuessPromptFunction is a function that returns an openpgp.PromptFunction well suited for the context.
func GuessPromptFunction() openpgp.PromptFunction {
pf := promptTerminal
conn, err := gpgagent.NewGpgAgentConn()
if err == nil {
pf = promptFunctionGpgAgent()
}
defer conn.Close()
// if GPGPASSPHRASE in Environ use it else request it when needed
envs := os.Environ()
for _, val := range envs {
env := strings.Split(val, "=")
if len(env) == 2 && env[0] == "GPGPASSPHRASE" {
fmt.Println("Overriding PromptFunction to use Environ")
pf = promptFromString(env[1])
break
}
}
return pf
}
// Config represents the configuration required to work with GPG.
type Config struct {
SecringDir string
PubringDir string
AccountDir string
RecipientKeyIds string
SignerKeyID string
PromptFunction openpgp.PromptFunction
}
// NewConfig returns an initialized Config with the information copied from a Profile. If nil Profile is passed we build one from DefaultProfile.
func NewConfig(p *Profile) *Config {
if p == nil {
p = DefaultProfile()
}
return &Config{
SecringDir: p.SecringDir,
PubringDir: p.PubringDir,
AccountDir: p.AccountDir,
RecipientKeyIds: p.RecipientKeyIds,
SignerKeyID: p.SignerKeyID,
PromptFunction: GuessPromptFunction(),
}
}
// EntityListRecipients returns the openpgp.EntityList corresponding to the RecipientKeyIds from the Config.
func (c *Config) EntityListRecipients() (openpgp.EntityList, error) {
el, err := getKeyRing(c.PubringDir)
if err != nil {
return nil, err
}
el = filterEntityList(el, c.RecipientKeyIds)
return el, nil
}
// EntityListWithSecretKey returns the openpgp.EntityList contains in Secring.
func (c *Config) EntityListWithSecretKey() (openpgp.EntityList, error) {
el, err := getKeyRing(c.SecringDir)
if err != nil {
return nil, err
}
return el, nil
}
// EntitySigner returns an Entity with a decrypted Private Key.
func (c *Config) EntitySigner() (*openpgp.Entity, error) {
el, err := getKeyRing(c.SecringDir)
if err != nil {
return nil, err
}
el = filterEntityList(el, c.SignerKeyID)
if len(el) != 1 {
return nil, fmt.Errorf("Exactly one SignerKeyID must be given, received : %d", len(el))
}
// Decrypt the private key
passphrase, err := c.PromptFunction(el.DecryptionKeys(), false)
if err != nil {
return nil, err
}
signer := el[0]
err = signer.PrivateKey.Decrypt(passphrase)
if err != nil {
return nil, err
}
return signer, nil
}
// decodeFile returns an io.Reader from which the content of the message can be read in clear text.
func (c *Config) decodeAccountFile(fpath string) (*openpgp.MessageDetails, error) {
el, err := c.EntityListWithSecretKey()
if err != nil {
return nil, err
}
return decodeFile(el, c.PromptFunction, fpath)
}
// ListAccountFiles returns the list of Files stored in the AccountDir.
// The list is filtered in a case in sensitive way.
func (c *Config) ListAccountFiles(fileSubStr string) ([]os.FileInfo, error) {
var filteredFiles []os.FileInfo
files, err := ioutil.ReadDir(c.AccountDir)
if err != nil {
return nil, err
}
for _, f := range files {
if strings.Contains(strings.ToLower(f.Name()), strings.ToLower(fileSubStr)) {
filteredFiles = append(filteredFiles, f)
}
}
return filteredFiles, nil
}
// Account represents an Account
type Account struct {
config *Config
Name string
Username string
Password string
Notes string
// The following fields are valued when the account is read.
IsSigned bool
SignedBy *openpgp.Key // the key of the signer, if available
}
// Path returns the theoretical path where the account file is.
func (a *Account) Path() string {
return filepath.Join(a.config.AccountDir, a.Name)
}
// NewAccountFromConsole returns an Account built with the elements collected by interacting with the user.
func NewAccountFromConsole(conf *Config) (*Account, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Account Name: ")
name, _ := reader.ReadString('\n')
fmt.Print("Enter Username: ")
username, _ := reader.ReadString('\n')
fmt.Print("Enter Notes: ")
notes, _ := reader.ReadString('\n')
fmt.Print("Enter Password (`gen` to generate a random one): ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
// Making sure that we jump a line in the console after reading the Password
fmt.Printf("\n")
if bytes.Equal(bytePassword, []byte("gen")) {
bytePassword, err = NewPassword(10)
if err != nil {
return nil, err
}
}
password := string(bytePassword)
account := Account{
config: conf,
Name: strings.TrimSpace(name),
Username: strings.TrimSpace(username),
Password: strings.TrimSpace(password),
Notes: strings.TrimSpace(notes),
}
return &account, nil
}
// NewAccountFromFile returns an Account as described by a file in the accountDir.
func NewAccountFromFile(conf *Config, fname string) (*Account, error) {
fpath := filepath.Join(conf.AccountDir, fname)
md, err := conf.decodeAccountFile(fpath)
if err != nil {
return nil, err
} else if md.IsSigned && md.SignatureError != nil {
return nil, fmt.Errorf("A signature error has been detected in this accoun : %v", err)
}
clearTextReader := md.UnverifiedBody
account, err := NewAccountFromReader(conf, filepath.Base(fpath), clearTextReader)
if md.IsSigned {
account.IsSigned = true
account.SignedBy = md.SignedBy
}
return account, err
}
func newAccountFromFileContent(conf *Config, name, str string) (*Account, error) {
a := Account{
config: conf,
Name: name,
}
chunks := strings.Split(str, "\n")
a.Password = chunks[0]
a.Username = chunks[1]
a.Notes = chunks[2]
return &a, nil
}
// NewAccountFromReader returns an account with the provided element.
// The reader is expected to returns bytes int the appropriate format :
// * []byte(password\nusername\nnotes)
func NewAccountFromReader(conf *Config, name string, r io.Reader) (*Account, error) {
content, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
account, err := newAccountFromFileContent(conf, name, string(content))
if err != nil {
return nil, err
}
return account, nil
}
// Bytes returns a slice of byte representing the account.
func (a Account) Bytes() []byte {
return []byte(fmt.Sprintf("%s\n%s\n%s", a.Password, a.Username, a.Notes))
}
// Encrypt returns the encrypted byte slice for an account.
func (a *Account) Encrypt() ([]byte, error) {
el, err := a.config.EntityListRecipients()
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
aw, err := armor.Encode(
buf,
"PGP MESSAGE",
map[string]string{"Version": "OpenPGP"},
)
var signer *openpgp.Entity
if a.config.SignerKeyID != "" {
signer, err = a.config.EntitySigner()
if err != nil {
return nil, err
}
}
w, err := openpgp.Encrypt(aw, el, signer, nil, nil)
if err != nil {
return nil, err
}
_, err = w.Write(a.Bytes())
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
err = aw.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}