forked from gopasspw/gopass
/
parse_colons.go
145 lines (132 loc) · 3.28 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
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
package cli
import (
"bufio"
"io"
"regexp"
"strings"
"github.com/gopasspw/gopass/pkg/backend/crypto/gpg"
)
var (
// John Doe (user) <john.doe@example.com>
reUIDComment = regexp.MustCompile(`([^(<]+)\s+(\([^)]+\))\s+<([^>]+)>`)
// John Doe <john.doe@example.com>
reUID = regexp.MustCompile(`([^(<]+)\s+<([^>]+)>`)
// John Doe (user)
reUIDNoEmailComment = regexp.MustCompile(`([^(<]+)\s+(\([^)]+\))`)
)
// 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 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]
cur.Identities[sn] = parseColonIdentity(fields)
}
}
if cur.Fingerprint != "" && cur.KeyLength > 0 {
kl = append(kl, cur)
}
return kl
}
func parseColonIdentity(fields []string) gpg.Identity {
id := fields[9]
ni := gpg.Identity{
Name: id,
CreationDate: parseTS(fields[5]),
ExpirationDate: parseTS(fields[6]),
}
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]
return ni
}
}
if reUIDNoEmailComment.MatchString(id) {
if m := reUIDNoEmailComment.FindStringSubmatch(id); len(m) > 2 {
ni.Name = m[1]
ni.Comment = strings.Trim(m[2], "()")
}
return ni
}
if reUID.MatchString(id) {
if m := reUID.FindStringSubmatch(id); len(m) > 2 {
ni.Name = m[1]
ni.Email = m[2]
return ni
}
}
return ni
}