/
gpg.go
433 lines (386 loc) · 10.3 KB
/
gpg.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package gpg
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
)
const (
fileMode = 0600
dirPerm = 0700
)
func init() {
// ensure created files don't have group or world perms set
// this setting should be inherited by sub-processes
syscall.Umask(077)
}
var (
reUIDComment = regexp.MustCompile(`([^(<]+)\s+(\([^)]+\))\s+<([^>]+)>`)
reUID = regexp.MustCompile(`([^(<]+)\s+<([^>]+)>`)
// GPGArgs contains the default GPG args for non-interactive use. Note: Do not use '--batch'
// as this will disable (necessary) passphrase questions!
GPGArgs = []string{"--quiet", "--yes", "--compress-algo=none", "--no-encrypt-to", "--no-auto-check-trustdb"}
// Debug prints all the commands executed
Debug = false
)
// KeyList is a searchable slice of Keys
type KeyList []Key
// UseableKeys returns the list of useable (valid keys)
func (kl KeyList) UseableKeys() KeyList {
nkl := make(KeyList, 0, len(kl))
for _, k := range kl {
if !k.IsUseable() {
continue
}
nkl = append(nkl, k)
}
return nkl
}
// FindKey will try to find the requested key
func (kl KeyList) FindKey(id string) (Key, error) {
id = strings.TrimPrefix(id, "0x")
for _, k := range kl {
if k.Fingerprint == id {
return k, nil
}
if strings.HasSuffix(k.Fingerprint, id) {
return k, nil
}
for _, ident := range k.Identities {
if ident.String() == id {
return k, nil
}
if ident.Email == id {
return k, nil
}
}
for sk := range k.SubKeys {
if strings.HasSuffix(sk, id) {
return k, nil
}
}
}
return Key{}, fmt.Errorf("No matching key found")
}
// ParseColons parses the `--with-colons` output format of GPG
func ParseColons(reader io.Reader) KeyList {
kl := make(KeyList, 0, 100)
scanner := bufio.NewScanner(reader)
// http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS
// Fields:
// 0 - Type of record
// Types:
// pub - Public Key
// crt - X.509 cert
// crs - X.509 cert and private key
// sub - Subkey (Secondary Key)
// sec - Secret / Private Key
// ssb - Secret Subkey
// uid - User ID
// uat - User attribute
// sig - Signature
// rev - Revocation Signature
// fpr - Fingerprint (field 9)
// pkd - Public Key Data
// grp - Keygrip
// rvk - Revocation KEy
// tfs - TOFU stats
// tru - Trust database info
// spk - Signature subpacket
// cfg - Configuration data
// 1 - Validity
// 2 - Key length
// 3 - Public Key Algo
// 4 - KeyID
// 5 - Creation Date (UTC)
// 6 - Expiration Date
// 7 - Cert S/N
// 8 - Ownertrust
// 9 - User-ID
// 10 - Sign. Class
// 11 - Key Caps.
// 12 - Issuer cert fp
// 13 - Flag
// 14 - S/N of a token
// 15 - Hash algo (2 - SHA-1, 8 - SHA-256)
// 16 - Curve Name
var cur Key
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
fields := strings.Split(line, ":")
switch fields[0] {
case "pub":
fallthrough
case "sec":
if cur.Fingerprint != "" && cur.KeyLength > 0 {
kl = append(kl, cur)
}
validity := fields[1]
if validity == "" && fields[0] == "sec" {
validity = "u"
}
cur = Key{
KeyType: fields[0],
Validity: validity,
KeyLength: parseInt(fields[2]),
CreationDate: parseTS(fields[5]),
ExpirationDate: parseTS(fields[6]),
Ownertrust: fields[8],
Identities: make(map[string]Identity, 1),
SubKeys: make(map[string]struct{}, 1),
}
case "sub":
fallthrough
case "ssb":
cur.SubKeys[fields[4]] = struct{}{}
case "fpr":
if cur.Fingerprint == "" {
cur.Fingerprint = fields[9]
}
case "uid":
sn := fields[7]
id := fields[9]
ni := Identity{}
if reUIDComment.MatchString(id) {
if m := reUIDComment.FindStringSubmatch(id); len(m) > 3 {
ni.Name = m[1]
ni.Comment = strings.Trim(m[2], "()")
ni.Email = m[3]
}
} else if reUID.MatchString(id) {
if m := reUID.FindStringSubmatch(id); len(m) > 2 {
ni.Name = m[1]
ni.Email = m[2]
}
}
cur.Identities[sn] = ni
}
}
if cur.Fingerprint != "" && cur.KeyLength > 0 {
kl = append(kl, cur)
}
return kl
}
// parseTS parses the passed string as an Epoch int and returns
// the time struct or the zero time struct
func parseTS(str string) time.Time {
t := time.Time{}
if sec, err := strconv.ParseInt(str, 10, 64); err == nil {
t = time.Unix(sec, 0)
}
return t
}
// parseInt parses the passed string as an int and returns it
// or 0 on errors
func parseInt(str string) int {
i := 0
if iv, err := strconv.ParseInt(str, 10, 32); err == nil {
i = int(iv)
}
return i
}
// Key is a GPG key (public or secret)
type Key struct {
KeyType string
KeyLength int
Validity string
CreationDate time.Time
ExpirationDate time.Time
Ownertrust string
Fingerprint string
Identities map[string]Identity
SubKeys map[string]struct{}
}
// IsUseable returns true if GPG would assume this key is useable for encryption
func (k Key) IsUseable() bool {
if k.ExpirationDate.Before(time.Now()) {
return false
}
switch k.Validity {
case "m":
return true
case "f":
return true
case "u":
return true
}
return false
}
// String implement fmt.Stringer. This method produces output that is close to, but
// not exactly the same, as the output form GPG itself
func (k Key) String() string {
fp := ""
if len(k.Fingerprint) > 24 {
fp = k.Fingerprint[24:]
}
out := fmt.Sprintf("%s %dD/0x%s %s", k.KeyType, k.KeyLength, fp, k.CreationDate.Format("2006-01-02"))
if !k.ExpirationDate.IsZero() {
out += fmt.Sprintf(" [expires: %s]", k.ExpirationDate.Format("2006-01-02"))
}
out += "\n Key fingerprint = " + k.Fingerprint
for _, id := range k.Identities {
out += fmt.Sprintf("\n" + id.String())
}
return out
}
// OneLine prints a terse representation of this key on one line (includes only
// the first identity!)
func (k Key) OneLine() string {
id := Identity{}
for _, i := range k.Identities {
id = i
break
}
return fmt.Sprintf("0x%s - %s", k.Fingerprint[24:], id.ID())
}
// Identity is a GPG identity, one key can have many IDs
type Identity struct {
Name string
Comment string
Email string
}
// ID returns the GPG ID format
func (i Identity) ID() string {
out := i.Name
if i.Comment != "" {
out += " (" + i.Comment + ")"
}
out += " <" + i.Email + ">"
return out
}
// String implement fmt.Stringer. This method resembels the output gpg uses
// for user-ids
func (i Identity) String() string {
return "uid " + i.ID()
}
// listKey lists all keys of the given type and matching the search strings
func listKeys(typ string, search ...string) (KeyList, error) {
args := []string{"--with-colons", "--with-fingerprint", "--fixed-list-mode", "--list-" + typ + "-keys"}
args = append(args, search...)
cmd := exec.Command("gpg", args...)
if Debug {
fmt.Printf("gpg.listKeys: %s %+v\n", cmd.Path, cmd.Args)
}
out, err := cmd.CombinedOutput()
if err != nil {
if bytes.Contains(out, []byte("secret key not available")) {
return KeyList{}, nil
}
return KeyList{}, err
}
return ParseColons(bytes.NewBuffer(out)), nil
}
// ListPublicKeys returns a parsed list of GPG public keys
func ListPublicKeys(search ...string) (KeyList, error) {
return listKeys("public", search...)
}
// ListPrivateKeys returns a parsed list of GPG secret keys
func ListPrivateKeys(search ...string) (KeyList, error) {
return listKeys("secret", search...)
}
// GetRecipients returns a list of recipient IDs for a given file
func GetRecipients(file string) ([]string, error) {
_ = os.Setenv("LANGUAGE", "C")
recp := make([]string, 0, 5)
args := []string{"--batch", "--list-only", "--no-default-keyring", "--secret-keyring", "/dev/null", file}
cmd := exec.Command("gpg", args...)
if Debug {
fmt.Printf("gpg.GetRecipients: %s %+v\n", cmd.Path, cmd.Args)
}
out, err := cmd.CombinedOutput()
if err != nil {
return []string{}, err
}
scanner := bufio.NewScanner(bytes.NewBuffer(out))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if !strings.HasPrefix(line, "gpg:") {
continue
}
p := strings.Split(line, ",")
if len(p) < 2 {
continue
}
p = strings.Split(strings.TrimSpace(p[1]), " ")
if len(p) < 2 {
continue
}
recp = append(recp, p[1])
}
return recp, nil
}
// Encrypt will encrypt the given content for the recipients. If alwaysTrust is true
// the trust-model will be set to always as to avoid (annoying) "unuseable public key"
// errors when encrypting.
func Encrypt(path string, content []byte, recipients []string, alwaysTrust bool) error {
if err := os.MkdirAll(filepath.Dir(path), dirPerm); err != nil {
return err
}
args := append(GPGArgs, "--encrypt", "--output", path)
if alwaysTrust {
// changing the trustmodel is possibly dangerous. A user should always
// explicitly opt-in to do this
args = append(args, "--trust-model=always")
}
for _, r := range recipients {
args = append(args, "--recipient", r)
}
cmd := exec.Command("gpg", args...)
if Debug {
fmt.Printf("gpg.Encrypt: %s %+v\n", cmd.Path, cmd.Args)
}
cmd.Stdin = bytes.NewReader(content)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// Decrypt will try to decrypt the given file
func Decrypt(path string) ([]byte, error) {
args := append(GPGArgs, "--decrypt", path)
cmd := exec.Command("gpg", args...)
if Debug {
fmt.Printf("gpg.Decrypt: %s %+v\n", cmd.Path, cmd.Args)
}
return cmd.Output()
}
// ExportPublicKey will export the named public key to the location given
func ExportPublicKey(id, filename string) error {
args := append(GPGArgs, "--armor", "--export", id)
cmd := exec.Command("gpg", args...)
if Debug {
fmt.Printf("gpg.ExportPublicKey: %s %+v\n", cmd.Path, cmd.Args)
}
out, err := cmd.Output()
if err != nil {
return err
}
return ioutil.WriteFile(filename, out, fileMode)
}
// ImportPublicKey will import a key from the given location
func ImportPublicKey(filename string) error {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
args := append(GPGArgs, "--import")
cmd := exec.Command("gpg", args...)
if Debug {
fmt.Printf("gpg.ImportPublicKey: %s %+v\n", cmd.Path, cmd.Args)
}
cmd.Stdin = bytes.NewReader(buf)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}