forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_colons.go
118 lines (108 loc) · 2.69 KB
/
parse_colons.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
package gpg
import (
"bufio"
"io"
"strings"
"github.com/justwatchcom/gopass/backend/gpg"
)
// 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
// parseColons parses the `--with-colons` output format of GPG
func (g *GPG) parseColons(reader io.Reader) gpg.KeyList {
kl := make(gpg.KeyList, 0, 100)
scanner := bufio.NewScanner(reader)
var cur gpg.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 = gpg.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]gpg.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 := gpg.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]
}
}
ni.CreationDate = parseTS(fields[5])
ni.ExpirationDate = parseTS(fields[6])
cur.Identities[sn] = ni
}
}
if cur.Fingerprint != "" && cur.KeyLength > 0 {
kl = append(kl, cur)
}
return kl
}