This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpubkey.go
337 lines (296 loc) · 7.82 KB
/
pubkey.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
/*
Hockeypuck - OpenPGP key server
Copyright (C) 2012-2014 Casey Marshall
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package openpgp
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"fmt"
"strings"
"time"
"golang.org/x/crypto/openpgp/packet"
"gopkg.in/errgo.v1"
)
type PublicKey struct {
Packet
RFingerprint string
RKeyID string
RShortID string
// Creation stores the timestamp when the public key was created.
Creation time.Time
// Expiration stores the timestamp when the public key expires.
Expiration time.Time
// Algorithm stores the algorithm type of the public key.
Algorithm int
// BitLen stores the bit length of the public key.
BitLen int
Signatures []*Signature
Others []*Packet
}
func AlgorithmName(code int) string {
switch code {
case 1, 2, 3:
return "rsa"
case 16:
return "elg"
case 17:
return "dsa"
case 18:
return "ecdh"
case 19:
return "ecdsa"
case 20:
return "elg"
case 22:
return "eddsa"
default:
return fmt.Sprintf("unk(#%d)", code)
}
}
func (pk *PublicKey) QualifiedFingerprint() string {
return fmt.Sprintf("%s%d/%s", AlgorithmName(pk.Algorithm), pk.BitLen, Reverse(pk.RFingerprint))
}
func (pk *PublicKey) ShortID() string {
return Reverse(pk.RShortID)
}
func (pk *PublicKey) KeyID() string {
return Reverse(pk.RKeyID)
}
func (pk *PublicKey) Fingerprint() string {
return Reverse(pk.RFingerprint)
}
// appendSignature implements signable.
func (pk *PublicKey) appendSignature(sig *Signature) {
pk.Signatures = append(pk.Signatures, sig)
}
func (pkp *PublicKey) publicKeyPacket() (*packet.PublicKey, error) {
op, err := pkp.opaquePacket()
if err != nil {
return nil, errgo.Mask(err)
}
p, err := op.Parse()
if err != nil {
return nil, errgo.Mask(err)
}
pk, ok := p.(*packet.PublicKey)
if !ok {
return nil, errgo.Newf("expected public key packet, got %T", p)
}
return pk, nil
}
func (pkp *PublicKey) publicKeyV3Packet() (*packet.PublicKeyV3, error) {
op, err := pkp.opaquePacket()
if err != nil {
return nil, errgo.Mask(err)
}
p, err := op.Parse()
if err != nil {
return nil, errgo.Mask(err)
}
pk, ok := p.(*packet.PublicKeyV3)
if !ok {
return nil, errgo.Newf("expected public key V3 packet, got %T", p)
}
return pk, nil
}
func (pkp *PublicKey) parse(op *packet.OpaquePacket, subkey bool) error {
p, err := op.Parse()
if err != nil {
return errgo.Mask(err)
}
switch pk := p.(type) {
case *packet.PublicKey:
if pk.IsSubkey != subkey {
return ErrInvalidPacketType
}
return pkp.setPublicKey(pk)
case *packet.PublicKeyV3:
if pk.IsSubkey != subkey {
return ErrInvalidPacketType
}
return pkp.setPublicKeyV3(pk)
default:
}
return errgo.Mask(ErrInvalidPacketType)
}
func (pkp *PublicKey) setUnsupported(op *packet.OpaquePacket) error {
// Calculate opaque fingerprint on unsupported public key packet
h := sha1.New()
h.Write([]byte{0x99, byte(len(op.Contents) >> 8), byte(len(op.Contents))})
h.Write(op.Contents)
fpr := hex.EncodeToString(h.Sum(nil))
pkp.RFingerprint = Reverse(fpr)
pkp.UUID = pkp.RFingerprint
return pkp.setV4IDs(pkp.UUID)
}
func (pkp *PublicKey) setPublicKey(pk *packet.PublicKey) error {
buf := bytes.NewBuffer(nil)
err := pk.Serialize(buf)
if err != nil {
return errgo.Mask(err)
}
fingerprint := hex.EncodeToString(pk.Fingerprint[:])
bitLen, err := pk.BitLength()
if err != nil {
return errgo.Mask(err)
}
pkp.RFingerprint = Reverse(fingerprint)
pkp.UUID = pkp.RFingerprint
err = pkp.setV4IDs(pkp.UUID)
if err != nil {
return err
}
pkp.Creation = pk.CreationTime
pkp.Algorithm = int(pk.PubKeyAlgo)
pkp.BitLen = int(bitLen)
pkp.Parsed = true
return nil
}
func (pkp *PublicKey) setV4IDs(rfp string) error {
if len(rfp) < 8 {
return errgo.Newf("invalid fingerprint %q", rfp)
}
pkp.RShortID = rfp[:8]
if len(rfp) < 16 {
return errgo.Newf("invalid fingerprint %q", rfp)
}
pkp.RKeyID = rfp[:16]
return nil
}
func (pkp *PublicKey) setPublicKeyV3(pk *packet.PublicKeyV3) error {
var buf bytes.Buffer
err := pk.Serialize(&buf)
if err != nil {
return errgo.Mask(err)
}
fingerprint := hex.EncodeToString(pk.Fingerprint[:])
bitLen, err := pk.BitLength()
if err != nil {
return errgo.Mask(err)
}
pkp.RFingerprint = Reverse(fingerprint)
pkp.UUID = pkp.RFingerprint
pkp.RShortID = Reverse(fmt.Sprintf("%08x", uint32(pk.KeyId)))
pkp.RKeyID = Reverse(fmt.Sprintf("%016x", pk.KeyId))
pkp.Creation = pk.CreationTime
if pk.DaysToExpire > 0 {
pkp.Expiration = pkp.Creation.Add(time.Duration(pk.DaysToExpire) * time.Hour * 24)
}
pkp.Algorithm = int(pk.PubKeyAlgo)
pkp.BitLen = int(bitLen)
pkp.Parsed = true
return nil
}
type PrimaryKey struct {
PublicKey
MD5 string
SHA256 string
SubKeys []*SubKey
UserIDs []*UserID
UserAttributes []*UserAttribute
}
// contents implements the packetNode interface for top-level public keys.
func (pubkey *PrimaryKey) contents() []packetNode {
result := []packetNode{pubkey}
for _, sig := range pubkey.Signatures {
result = append(result, sig.contents()...)
}
for _, uid := range pubkey.UserIDs {
result = append(result, uid.contents()...)
}
for _, uat := range pubkey.UserAttributes {
result = append(result, uat.contents()...)
}
for _, subkey := range pubkey.SubKeys {
result = append(result, subkey.contents()...)
}
for _, other := range pubkey.Others {
result = append(result, other.contents()...)
}
return result
}
func (*PrimaryKey) removeDuplicate(parent packetNode, dup packetNode) error {
return errgo.New("cannot remove a duplicate primary pubkey")
}
func ParsePrimaryKey(op *packet.OpaquePacket) (*PrimaryKey, error) {
var buf bytes.Buffer
var err error
if err = op.Serialize(&buf); err != nil {
return nil, errgo.Mask(err)
}
pubkey := &PrimaryKey{
PublicKey: PublicKey{
Packet: Packet{
Tag: op.Tag,
Packet: buf.Bytes(),
},
},
}
// Attempt to parse the opaque packet into a public key type.
parseErr := pubkey.parse(op, false)
if parseErr != nil {
err = pubkey.setUnsupported(op)
if err != nil {
return nil, errgo.Mask(err)
}
} else {
pubkey.Parsed = true
}
return pubkey, nil
}
func (pubkey *PrimaryKey) setPublicKey(pk *packet.PublicKey) error {
if pk.IsSubkey {
return errgo.NoteMask(ErrInvalidPacketType, "expected primary public key packet, got sub-key")
}
return pubkey.PublicKey.setPublicKey(pk)
}
func (pubkey *PrimaryKey) setPublicKeyV3(pk *packet.PublicKeyV3) error {
if pk.IsSubkey {
return errgo.NoteMask(ErrInvalidPacketType, "expected primary public key packet, got sub-key")
}
return pubkey.PublicKey.setPublicKeyV3(pk)
}
func (pubkey *PrimaryKey) SelfSigs() *SelfSigs {
result := &SelfSigs{target: pubkey}
for _, sig := range pubkey.Signatures {
// Skip non-self-certifications.
if !strings.HasPrefix(pubkey.UUID, sig.RIssuerKeyID) {
continue
}
checkSig := &CheckSig{
PrimaryKey: pubkey,
Signature: sig,
Error: pubkey.verifyPublicKeySelfSig(&pubkey.PublicKey, sig),
}
if checkSig.Error != nil {
result.Errors = append(result.Errors, checkSig)
continue
}
switch sig.SigType {
case 0x20: // packet.SigTypeKeyRevocation
result.Revocations = append(result.Revocations, checkSig)
}
}
result.resolve()
return result
}
func (pubkey *PrimaryKey) updateMD5() error {
digest, err := SksDigest(pubkey, md5.New())
if err != nil {
return err
}
pubkey.MD5 = digest
return nil
}