forked from jbowes/httpsig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
308 lines (245 loc) · 7 KB
/
verify.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
// Copyright (c) 2021 James Bowes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpsig
import (
"crypto"
"crypto/ecdsa"
"crypto/hmac"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"errors"
"fmt"
"io"
"strings"
"time"
)
// Parses the signature id and signature value
func parseSignature(in string) (string, string, error) {
var signature string
// ["sig1", "":asjfgnaslkdbnvakjsfb.ksjdbnv as;kdvbj=:"]
sParts := strings.SplitN(in, "=", 2)
if len(sParts) != 2 {
return "", signature, errMalformedSignature
}
sig := sParts[1]
n := len(sig)
// Prevent index out of bounds
if n < 2 {
return "", signature, errMalformedSignature
}
// Signature must be wrapped by colons, EX: ":<sig val>:"
if sig[0] != ':' && sig[n - 1] != ':' {
return "", signature, errMalformedSignature
}
signature = strings.Trim(sig, ":")
if signature == "" {
return "", signature, errMalformedSignature
}
return sParts[0], signature, nil
}
type verImpl struct {
w io.Writer
verify func([]byte) error
}
type verHolder struct {
alg string
verifier func() verImpl
}
type verifier struct {
configs HttpSigningConfigs
key verHolder
// For testing
nowFunc func() time.Time
}
// XXX: note about fail fast.
func (v *verifier) Verify(msg *message) error {
// Check for existence of "Signature" header
// This indicates that the message has been signed
sigHdr := msg.Header.Get(v.configs.SignatureHeaderLabel)
if sigHdr == "" {
return errNotSigned
}
// Check for existence of "Signature-Input" header
// This will allow us to replicate signature & validate authenticity
paramHdr := msg.Header.Get(v.configs.InputHeaderLabel)
if paramHdr == "" {
return errNotSigned
}
// Should look like "sig1=:asjfgnaslkdbnvakjsfb.ksjdbnv as;kdvbj=:"
sigParts := strings.Split(sigHdr, ", ")
// Should look like:
// 'sig1=("@method" "@path" "@query" "authorization" "content-type" "content-digest");created=1657133676'
paramParts := strings.Split(paramHdr, ", ")
// Verify num sigs matches num inputs
// Check only pertains to multisig
if len(sigParts) != len(paramParts) {
return errMalformedSignature
}
var sigID string
var params signatureParams
var err error
for _, p := range paramParts {
pParts := strings.SplitN(p, "=", 2)
if len(pParts) != 2 {
return errMalformedSignature
}
params, err = parseSignatureParams(pParts[1])
if err != nil {
return errMalformedSignature
}
sigID = pParts[0]
}
id, signature, err := parseSignature(sigHdr)
if err != nil {
return err
}
if id != sigID {
return errMalformedSignature
}
// expectedAlgorithm := v.key.alg
// if expectedAlgorithm != "" && *params.alg != "" &&
// expectedAlgorithm != *params.alg {
// return errAlgMismatch
// }
// Signatures are base64 encoded before transit, decode to verify
sig, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
return errMalformedSignature
}
verifier := v.key.verifier()
// canonicalize headers
for _, component := range params.coveredComponents {
var value []byte
if len(component) > 1 && component[0] == '@' {
value, err = canonicalizeDerivedComponent(component, *msg)
if err != nil {
return err
}
} else {
value, _, err = canonicalizeHeader(component, msg.Header)
}
_, err = verifier.w.Write(value)
if err != nil {
return fmt.Errorf("faild to write component's %s value %s to buffer. %w", component, value, err)
}
}
if _, err := verifier.w.Write(canonicalizeSignatureParams(¶ms)); err != nil {
return err
}
err = verifier.verify(sig)
if err != nil {
return errInvalidSignature
}
// Check signatures are not expired
if params.expires != nil && params.expires.After(time.Now()) {
return errSignatureExpired
}
return nil
}
func parseSignatureInputHeader(headerValue string) (string, signatureParams, error) {
var params signatureParams
var signatureID string
// Should look like:
// 'sig1=("@method" "@path" "@query" "authorization" "content-type" "content-digest");created=1657133676'
// Split into two parts the signature and the corresponding label
// ["sig1", "("@method" "@path" "@query" "authorization" "content-type" "content-digest");created=1657133676"]
pParts := strings.SplitN(headerValue, "=", 2)
if len(pParts) != 2 {
return signatureID, params, errMalformedSignature
}
signatureID = pParts[0]
// Parse signature input into signature params
params, err := parseSignatureParams(pParts[1])
if err != nil {
return signatureID, params, errMalformedSignature
}
return signatureID, params, nil
}
// XXX use vice here too.
var (
errNotSigned = errors.New("signature headers not found")
errMalformedSignature = errors.New("unable to parse signature headers")
errUnknownKey = errors.New("unknown key id")
errAlgMismatch = errors.New("algorithm mismatch for key id")
errSignatureExpired = errors.New("signature expired")
errInvalidSignature = errors.New("invalid signature")
)
// These error checking funcs aren't needed yet, so don't export them
/*
func IsNotSignedError(err error) bool { return errors.Is(err, notSignedError) }
func IsMalformedSignatureError(err error) bool { return errors.Is(err, malformedSignatureError) }
func IsUnknownKeyError(err error) bool { return errors.Is(err, unknownKeyError) }
func IsAlgMismatchError(err error) bool { return errors.Is(err, algMismatchError) }
func IsSignatureExpiredError(err error) bool { return errors.Is(err, signatureExpiredError) }
func IsInvalidSignatureError(err error) bool { return errors.Is(err, invalidSignatureError) }
*/
func verifyRsaPssSha512(pk *rsa.PublicKey) verHolder {
return verHolder{
alg: "rsa-pss-sha512",
verifier: func() verImpl {
h := sha512.New()
return verImpl{
w: h,
verify: func(s []byte) error {
b := h.Sum(nil)
return rsa.VerifyPSS(pk, crypto.SHA512, b, s, nil)
},
}
},
}
}
func verifyEccP256(pk *ecdsa.PublicKey) verHolder {
return verHolder{
alg: "ecdsa-p256-sha256",
verifier: func() verImpl {
h := sha256.New()
return verImpl{
w: h,
verify: func(s []byte) error {
b := h.Sum(nil)
if !ecdsa.VerifyASN1(pk, b, s) {
return errInvalidSignature
}
return nil
},
}
},
}
}
func verifyHmacSha256(secret []byte) verHolder {
// TODO: add alg
return verHolder{
alg: "hmac-sha256",
verifier: func() verImpl {
h := hmac.New(sha256.New, secret)
return verImpl{
w: h,
verify: func(in []byte) error {
if !hmac.Equal(in, h.Sum(nil)) {
return errInvalidSignature
}
return nil
},
}
},
}
}
func verifyRsa256(pub *rsa.PublicKey) verHolder {
// TODO: add alg
return verHolder{
alg: "SASSA-PKCS1-v1_5 using SHA-256",
verifier: func() verImpl {
h := sha256.New()
return verImpl{
w: h,
verify: func(signature []byte) error {
hashed := h.Sum(nil)
return rsa.VerifyPKCS1v15(pub, crypto.SHA256, hashed[:], signature)
},
}
},
}
}