forked from aptly-dev/aptly
/
openpgp.go
243 lines (210 loc) · 6.13 KB
/
openpgp.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
package pgp
import (
"bytes"
"crypto"
"crypto/dsa"
"crypto/ecdsa"
"crypto/rsa"
"hash"
"io"
"strconv"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/errors"
"golang.org/x/crypto/openpgp/packet"
)
// hashForSignature returns a pair of hashes that can be used to verify a
// signature. The signature may specify that the contents of the signed message
// should be preprocessed (i.e. to normalize line endings). Thus this function
// returns two hashes. The second should be used to hash the message itself and
// performs any needed preprocessing.
func hashForSignature(hashID crypto.Hash, sigType packet.SignatureType) (hash.Hash, hash.Hash, error) {
if !hashID.Available() {
return nil, nil, errors.UnsupportedError("hash not available: " + strconv.Itoa(int(hashID)))
}
h := hashID.New()
switch sigType {
case packet.SigTypeBinary:
return h, h, nil
case packet.SigTypeText:
return h, openpgp.NewCanonicalTextHash(h), nil
}
return nil, nil, errors.UnsupportedError("unsupported signature type: " + strconv.Itoa(int(sigType)))
}
type signatureResult struct {
CreationTime time.Time
IssuerKeyID uint64
PubKeyAlgo packet.PublicKeyAlgorithm
Entity *openpgp.Entity
}
// checkDetachedSignature takes a signed file and a detached signature and
// returns the signer if the signature is valid. If the signer isn't known,
// ErrUnknownIssuer is returned.
//
// This is extended version of golang.org/x/crypto/openpgp to support multiple signers and returns multiple
// signers, plus keeps track of "missing" keys
func checkDetachedSignature(keyring openpgp.KeyRing, signed, signature io.Reader) (signers []signatureResult, missingKeys int, err error) {
var p packet.Packet
signedBuf := &bytes.Buffer{}
if _, e := io.Copy(signedBuf, signed); e != nil && e != io.EOF {
return nil, 0, e
}
packets := packet.NewReader(signature)
for {
p, err = packets.Next()
if err == io.EOF {
if len(signers) == 0 || missingKeys > 0 {
err = errors.ErrUnknownIssuer
} else {
err = nil
}
return
}
if err != nil {
return nil, 0, err
}
var issuerKeyID uint64
var hashFunc crypto.Hash
var sigType packet.SignatureType
var creationTime time.Time
var pubKeyAlgo packet.PublicKeyAlgorithm
var keys []openpgp.Key
switch sig := p.(type) {
case *packet.Signature:
if sig.IssuerKeyId == nil {
return nil, 0, errors.StructuralError("signature doesn't have an issuer")
}
issuerKeyID = *sig.IssuerKeyId
hashFunc = sig.Hash
sigType = sig.SigType
creationTime = sig.CreationTime
pubKeyAlgo = sig.PubKeyAlgo
case *packet.SignatureV3:
issuerKeyID = sig.IssuerKeyId
hashFunc = sig.Hash
sigType = sig.SigType
creationTime = sig.CreationTime
pubKeyAlgo = sig.PubKeyAlgo
default:
return nil, 0, errors.StructuralError("non signature packet found")
}
keys = keyring.KeysByIdUsage(issuerKeyID, packet.KeyFlagSign)
if len(keys) == 0 {
signers = append(signers, signatureResult{
CreationTime: creationTime,
IssuerKeyID: issuerKeyID,
PubKeyAlgo: pubKeyAlgo,
})
missingKeys++
continue
}
h, wrappedHash, err := hashForSignature(hashFunc, sigType)
if err != nil {
return nil, 0, err
}
if _, e := io.Copy(wrappedHash, bytes.NewReader(signedBuf.Bytes())); e != nil && e != io.EOF {
return nil, 0, e
}
allFailed := true
for _, key := range keys {
switch sig := p.(type) {
case *packet.Signature:
err = key.PublicKey.VerifySignature(h, sig)
case *packet.SignatureV3:
err = key.PublicKey.VerifySignatureV3(h, sig)
default:
panic("unreachable")
}
if err == nil {
signers = append(signers, signatureResult{
CreationTime: creationTime,
IssuerKeyID: issuerKeyID,
PubKeyAlgo: pubKeyAlgo,
Entity: key.Entity,
})
allFailed = false
}
}
if allFailed {
return nil, 0, err
}
}
}
// readArmored reads an armored block with the given type.
func readArmored(r io.Reader, expectedType string) (body io.Reader, err error) {
block, err := armor.Decode(r)
if err != nil {
return
}
if block.Type != expectedType {
return nil, errors.InvalidArgumentError("expected '" + expectedType + "', got: " + block.Type)
}
return block.Body, nil
}
// checkArmoredDetachedSignature performs the same actions as
// CheckDetachedSignature but expects the signature to be armored.
//
// This is extended version of golang.org/x/crypto/openpgp to support multiple signers and returns multiple
// signers, plus keeps track of "missing" keys
func checkArmoredDetachedSignature(keyring openpgp.KeyRing, signed, signature io.Reader) (signers []signatureResult, missingKeys int, err error) {
body, err := readArmored(signature, openpgp.SignatureType)
if err != nil {
return
}
return checkDetachedSignature(keyring, signed, body)
}
func pubkeyAlgorithmName(algorithm packet.PublicKeyAlgorithm) string {
switch algorithm {
case packet.PubKeyAlgoRSA, packet.PubKeyAlgoRSAEncryptOnly, packet.PubKeyAlgoRSASignOnly:
return "RSA"
case packet.PubKeyAlgoElGamal:
return "ElGamal"
case packet.PubKeyAlgoDSA:
return "DSA"
case packet.PubKeyAlgoECDH:
return "EDCH"
case packet.PubKeyAlgoECDSA:
return "ECDSA"
}
return "unknown"
}
func keyBits(key interface{}) string {
switch k := key.(type) {
case *rsa.PublicKey:
return strconv.Itoa(k.N.BitLen())
case *dsa.PublicKey:
return strconv.Itoa(k.P.BitLen())
case *ecdsa.PublicKey:
return strconv.Itoa(k.Curve.Params().BitSize)
default:
return "?"
}
}
func validEntity(entity *openpgp.Entity) bool {
var selfSig *packet.Signature
for _, ident := range entity.Identities {
if selfSig == nil {
selfSig = ident.SelfSignature
} else if ident.SelfSignature.IsPrimaryId != nil && *ident.SelfSignature.IsPrimaryId {
selfSig = ident.SelfSignature
break
}
}
if selfSig == nil {
return false
}
if len(entity.Revocations) > 0 {
return false
}
if selfSig.RevocationReason != nil {
return false
}
if !selfSig.FlagsValid {
return false
}
if selfSig.KeyLifetimeSecs != nil && selfSig.CreationTime.Add(time.Duration(*selfSig.KeyLifetimeSecs)*time.Second).Before(time.Now()) {
return false
}
return true
}