-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_provider.go
102 lines (95 loc) · 2.3 KB
/
key_provider.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
package asn
import (
"bytes"
"context"
"crypto/x509"
"encoding/base64"
"errors"
"github.com/lestrrat-go/jwx/v2/jws"
)
type verifiedKey struct {
name []byte
pubkey any
}
// KeyProvider implements jws.KeyProvider.
// Once pubkey verified, it's cached.
type KeyProvider struct {
trusted verifiedKey
fetcher RootCAFetcher
}
// NewKeyProvider returns a new KeyProvider.
func NewKeyProvider(fetcher RootCAFetcher) *KeyProvider {
return &KeyProvider{
trusted: verifiedKey{},
fetcher: fetcher,
}
}
// FetchKeys extracts the public key from the x5c field to verify the certificate according to https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.6.
// Treat the chain that matches the root certificate fetched by RootCAFetcher as the root certificate.
func (p *KeyProvider) FetchKeys(ctx context.Context, sink jws.KeySink, sig *jws.Signature, msg *jws.Message) error {
headers := sig.ProtectedHeaders()
certs := headers.X509CertChain()
verifyOpts := x509.VerifyOptions{
Roots: x509.NewCertPool(),
Intermediates: x509.NewCertPool(),
}
var isApple bool
var label []byte
var key *x509.Certificate
for i := 0; i < certs.Len(); i++ {
chain, _ := certs.Get(i)
if bytes.Equal(p.trusted.name, chain) {
sink.Key(headers.Algorithm(), p.trusted.pubkey)
return nil
}
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(chain)))
n, err := base64.StdEncoding.Decode(dbuf, chain)
if err != nil {
return err
}
cert, err := x509.ParseCertificate(dbuf[:n])
if err != nil {
return err
}
switch i {
case 0:
label = chain
key = cert
default:
hasRoot, err := p.match(ctx, chain)
if err != nil {
return err
}
if hasRoot {
isApple = true
verifyOpts.Roots.AddCert(cert)
} else {
verifyOpts.Intermediates.AddCert(cert)
}
}
}
if !isApple {
return errors.New("certificate is not from Apple")
}
if _, err := key.Verify(verifyOpts); err != nil {
return err
}
p.trusted = verifiedKey{
name: label,
pubkey: key.PublicKey,
}
sink.Key(headers.Algorithm(), key.PublicKey)
return nil
}
func (p *KeyProvider) match(ctx context.Context, chain []byte) (bool, error) {
roots, err := p.fetcher.Fetch(ctx)
if err != nil {
return false, err
}
for _, root := range roots {
if bytes.Equal(root, chain) {
return true, nil
}
}
return false, nil
}