Repository navigation
Expand file tree
/
Copy pathverifier.go
More file actions
401 lines (323 loc) · 10.7 KB
/
Copy pathverifier.go
File metadata and controls
401 lines (323 loc) · 10.7 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package httpsig
import (
"context"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"errors"
"fmt"
"net/http"
"time"
"github.com/dunglas/httpsfv"
)
// VerifiedSignature contains the attributes of a successfully verified HTTP
// message signature and the metadata returned by the KeyResolver.
type VerifiedSignature struct {
// Label is the received signature label. It is not cryptographically
// protected and must not be used as an identity or authorization claim.
Label string
// Components contains the canonicalized values of the covered HTTP
// message components, without the synthetic @signature-params component.
// Map keys are serialized Structured Fields String Items (e.g. `"@method"`
// or `"content-digest";req`).
Components map[string]string
// Parameters contains the actual transmitted signature parameters,
// including extensions, without synthetic defaults.
Parameters SignatureParameters
// Metadata is the application-defined metadata provided by the KeyResolver
// for this signature.
Metadata any
}
// VerificationResult contains the signatures selected and successfully
// verified during one call to Verifier.Verify.
// The order of the signatures is not guaranteed.
type VerificationResult []VerifiedSignature
// Verifier verifies HTTP message signatures according to its configured
// requirements. A call succeeds only if all selected signatures pass.
type Verifier interface {
// Verify returns information about the successfully verified signatures
// or an error. Per-call requirements do not change the Verifier.
Verify(msg *Message, opts ...VerifyOption) (VerificationResult, error)
}
type applicableSignature struct {
label string
input *signatureInput
}
// NewVerifier creates a Verifier instance.
// Without WithValidateAllSignatures, at least one WithRequiredTag is necessary.
// By default, created and expires are required and signatures may be at most
// 30 seconds old. Optional created or expires parameters are still checked
// when present. Global WithSignatureNegotiation cannot be combined with
// WithValidateAllSignatures. More specific requirements can be set through opts.
//
//nolint:cyclop
func NewVerifier(resolver KeyResolver, opts ...VerifierOption) (Verifier, error) {
if resolver == nil {
return nil, fmt.Errorf("%w: no key resolver provided", ErrVerifierCreation)
}
ver := &verifier{
keyResolver: resolver,
tagPolicies: make(map[string]*verificationPolicy),
}
globalPolicy := &verificationPolicy{
maxAge: new(30 * time.Second), //nolint:mnd
reqExpiresTS: new(true),
reqCreatedTS: new(true),
}
for _, opt := range opts {
if err := opt.applyVerifierOption(ver, globalPolicy, false); err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerifierCreation, err)
}
}
if !ver.validateAllSigs && len(ver.tagPolicies) == 0 {
return nil, fmt.Errorf("%w: validation of all signatures disabled, but no signature tags specified",
ErrVerifierCreation)
}
if globalPolicy.asb != nil && ver.validateAllSigs {
return nil, fmt.Errorf("%w: verification and negotiation of all posible signature is not possible",
ErrVerifierCreation)
}
for tag, policy := range ver.tagPolicies {
if err := policy.inherit(globalPolicy, tag); err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerifierCreation, err)
}
}
if ver.validateAllSigs {
if err := globalPolicy.prepare(""); err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerifierCreation, err)
}
ver.tagPolicies[""] = globalPolicy
}
return ver, nil
}
type payloadVerifier interface {
verify(ctx context.Context, data []byte, signature []byte) error
}
func newPayloadVerifier(verificationKey any, alg SignatureAlgorithm) (payloadVerifier, error) {
switch publicKey := verificationKey.(type) {
case ed25519.PublicKey:
return newEd25519Verifier(publicKey, alg)
case *rsa.PublicKey:
return newRSAVerifier(publicKey, alg)
case *ecdsa.PublicKey:
return newECDSAVerifier(publicKey, alg)
case []byte:
return newSymmetricSigner(publicKey, alg)
case MessageVerifier:
return &messageVerifierAdaptor{verifier: publicKey}, nil
default:
return nil, ErrUnsupportedKeyType
}
}
type verifier struct {
keyResolver KeyResolver
tagPolicies map[string]*verificationPolicy
validateAllSigs bool
}
func (v *verifier) Verify(msg *Message, opts ...VerifyOption) (VerificationResult, error) {
policiesByTag, err := v.policiesForCall(opts)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, err)
}
sigRefs, err := getSignatureReferences(msg.Header.Values(headerSignatureInput))
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, err)
}
signatureDict, err := httpsfv.UnmarshalDictionary(msg.Header.Values(headerSignature))
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, err)
}
// look if we miss any signatures for configured tags
if v.hasMissingSignatures(sigRefs) {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, v.negotiateSignatureParameters(msg, policiesByTag))
}
// collect those signatures, which we can verify
applicableSignatures, err := v.applicableSignatures(sigRefs)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, err)
}
// do the actual verification
results := make([]VerifiedSignature, 0, len(applicableSignatures))
for _, ref := range applicableSignatures {
name, params := ref.label, ref.input
tag, _ := params.tag()
policy, exists := policiesByTag[tag]
if !exists {
policy = policiesByTag[""]
}
signature, err := v.extractSignature(signatureDict, name)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, err)
}
result, err := v.verifySignature(msg, name, signature, params, policy)
if err != nil {
if errors.Is(err, ErrMissingParameter) {
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, v.negotiateSignatureParameters(msg, policiesByTag))
}
return nil, fmt.Errorf("%w: %w", ErrVerificationFailed, err)
}
results = append(results, result)
}
return results, nil
}
func (v *verifier) policiesForCall(opts []VerifyOption) (map[string]*verificationPolicy, error) {
if len(opts) == 0 {
return v.tagPolicies, nil
}
result := make(map[string]*verificationPolicy, len(v.tagPolicies))
for tag, original := range v.tagPolicies {
effective, err := original.withOptions(tag, opts)
if err != nil {
return nil, err
}
result[tag] = effective
}
return result, nil
}
func (v *verifier) applicableSignatures(refs *httpsfv.Dictionary) ([]applicableSignature, error) {
result := make([]applicableSignature, 0, len(refs.Names()))
for _, label := range refs.Names() {
member, _ := refs.Get(label)
list := member.(httpsfv.InnerList) //nolint:forcetypeassert
if !v.validateAllSigs {
tag, present := signatureTag(list)
if !present {
continue
}
if _, required := v.tagPolicies[tag]; !required {
continue
}
}
si, err := parseSignatureInput(list)
if err != nil {
return nil, fmt.Errorf("%w: label %q: %w", ErrMalformedData, label, err)
}
result = append(result, applicableSignature{label, si})
}
if len(result) == 0 {
return nil, &NoApplicableSignatureError{}
}
return result, nil
}
func (v *verifier) negotiateSignatureParameters(msg *Message, policiesByTag map[string]*verificationPolicy) error {
hdr := http.Header{}
for sigTag, policy := range policiesByTag {
if policy.asb == nil || len(sigTag) == 0 {
continue
}
buildOpts := make([]AcceptSignatureBuildOption, len(policy.expectedParameters))
for i, param := range policy.expectedParameters {
buildOpts[i] = param
}
err := policy.asb.Build(msg.Context, hdr, buildOpts...)
if err != nil {
return fmt.Errorf("%w: failed to negotiate signature for tag %s: %w", ErrSignatureNegotiationError, sigTag, err)
}
}
return &NoApplicableSignatureError{headerToAdd: hdr}
}
func (v *verifier) hasMissingSignatures(sigRefs *httpsfv.Dictionary) bool {
for sigTag := range v.tagPolicies {
if len(sigTag) == 0 {
// empty tag is used for a policy applying to all signatures
// ignoring it
continue
}
var present bool
for _, label := range sigRefs.Names() {
member, _ := sigRefs.Get(label)
tag, tagPresent := signatureTag(member.(httpsfv.InnerList)) //nolint:forcetypeassert
if tagPresent && tag == sigTag {
present = true
break
}
}
if !present {
return true
}
}
return false
}
func (v *verifier) verifySignature(
msg *Message,
label string,
signature []byte,
si *signatureInput,
policy *verificationPolicy,
) (VerifiedSignature, error) {
if err := policy.assertMetadata(si, msg); err != nil {
return VerifiedSignature{}, err
}
base, err := newSignatureBase(msg, si, nil)
if err != nil {
return VerifiedSignature{}, err
}
components := base.components()
params := si.parameters()
keyInfo, err := v.keyResolver.ResolveKey(msg.Context, SignatureInfo{
Label: label,
Message: msg,
Components: components,
Parameters: params,
})
if err != nil {
return VerifiedSignature{}, err
}
if err = policy.assertKeyAlgorithm(si, keyInfo.Key.Algorithm); err != nil {
return VerifiedSignature{}, err
}
verifier, err := newPayloadVerifier(keyInfo.Key.Material, keyInfo.Key.Algorithm)
if err != nil {
return VerifiedSignature{}, err
}
err = verifier.verify(msg.Context, base.marshal(), signature)
if err != nil {
return VerifiedSignature{}, err
}
return VerifiedSignature{
Label: label,
Components: components,
Parameters: params,
Metadata: keyInfo.Metadata,
}, nil
}
func (v *verifier) extractSignature(dict *httpsfv.Dictionary, name string) ([]byte, error) {
sigItem, ok := dict.Get(name)
if !ok {
return nil, fmt.Errorf("%w: no signature present for label %s", ErrMalformedData, name)
}
signature, ok := sigItem.(httpsfv.Item)
if !ok {
return nil, fmt.Errorf("%w: unexpected content type for label %s", ErrMalformedData, name)
}
signatureBytes, ok := signature.Value.([]byte)
if !ok {
return nil, fmt.Errorf("%w: unexpected value for label %s", ErrMalformedData, name)
}
return signatureBytes, nil
}
func signatureTag(list httpsfv.InnerList) (string, bool) {
if list.Params == nil {
return "", false
}
value, present := list.Params.Get(string(Tag))
if !present {
return "", false
}
tag, ok := value.(string)
return tag, ok
}
func getSignatureReferences(values []string) (*httpsfv.Dictionary, error) {
inputDict, err := httpsfv.UnmarshalDictionary(values)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrMalformedData, err)
}
// Check member types without converting values that require semantic validation.
for _, label := range inputDict.Names() {
m, _ := inputDict.Get(label)
if _, ok := m.(httpsfv.InnerList); !ok {
return nil, fmt.Errorf("%w: unexpected signature parameters format", ErrMalformedData)
}
}
return inputDict, nil
}