/
jwk.go
120 lines (97 loc) · 2.08 KB
/
jwk.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
// +build go1.12
package jwk
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"net/http"
)
type key struct {
pub interface{}
hash crypto.Hash
}
type Keys map[string]*key
func (k Keys) Verify(kid string, input, sig []byte) error {
jwk, ok := k[kid]
if !ok {
return fmt.Errorf("no jwk for kid: %s", kid)
}
return jwk.Verify(input, sig)
}
// RFC7518 section 3.4
func (jwk *key) Verify(input, sig []byte) error {
key := jwk.pub
hp := jwk.hash
// for RSA we use THIS function..
switch k := key.(type) {
case *rsa.PublicKey:
// hash input first..
hasher := hp.New()
hasher.Write(input)
hashed := hasher.Sum(nil)
// then verify
// XXX security risk here, as error is NOT a constant
// so any new external package could defeat our verification
// by rsa.ErrVerification = nil
return rsa.VerifyPKCS1v15(k, hp, hashed, sig)
case *ecdsa.PublicKey:
var r, s big.Int
// as the rfc says.. the signature must be a 64 bytes sequece if
// it is p-256/sha256
if len(sig) != hp.Size()*2 {
return ErrParse
}
hmark := hp.Size()
rbuf := sig[:hmark]
sbuf := sig[hmark:]
//hashed := hp.New().Sum(input)
hasher := hp.New()
hasher.Write(input)
hashed := hasher.Sum(nil)
/*
r := &big.Int{}
s := &big.Int{}
*/
r.SetBytes(rbuf)
s.SetBytes(sbuf)
if ecdsa.Verify(k, hashed, &r, &s) {
return nil
}
}
return ErrUnsupported
}
func MapFromUrl(urls ...string) (Keys, error) {
var jwks jwkSet
jm := make(Keys)
for _, url := range urls {
//fmt.Printf("[%d] processing %s\n", iurl, url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(respbuf, &jwks)
if err != nil {
return nil, err
}
for _, jwk := range jwks.Keys {
//fmt.Printf("[%d] processing key...", ijwk)
key, err := jwk.parse()
if err != nil {
//fmt.Printf("invalid (%v)\n", err)
continue
}
//fmt.Printf("ok\n")
jm[jwk.Kid] = key
}
}
return jm, nil
}