-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjwk.go
119 lines (102 loc) · 2.39 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
package jwk
import (
"crypto/ecdsa"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"github.com/ericyan/jwk/internal/base64url"
)
// JSON Web Key types defined in RFC7518, Section 6.
const (
TypeEC = "EC"
TypeRSA = "RSA"
TypeOCT = "oct"
)
// CryptoKey represents a cryptographic key using an unspecified algorithm.
type CryptoKey interface{}
// Params contains common JSON Web Key parameters.
type Params struct {
KeyType string `json:"kty"`
KeyUse string `json:"use,omitempty"`
KeyOps []string `json:"key_ops,omitempty"`
Algorithm string `json:"alg,omitempty"`
KeyID string `json:"kid,omitempty"`
}
// ID returns the key ID parameter.
func (p *Params) ID() string {
return p.KeyID
}
// Key represents a JSON Web Key.
type Key interface {
ID() string
CryptoKey() CryptoKey
}
// New creates a new Key.
func New(key CryptoKey, params *Params) (Key, error) {
switch k := key.(type) {
case *ecdsa.PublicKey:
return NewECDSAPublicKey(k, params)
case *ecdsa.PrivateKey:
return NewECDSAPrivateKey(k, params)
case *rsa.PublicKey:
return NewRSAPublicKey(k, params)
case *rsa.PrivateKey:
return NewRSAPrivateKey(k, params)
case []byte:
return NewOctetSequenceKey(k, params)
default:
return nil, errors.New("jwk: unsupported crypto key")
}
}
// Parse parses data as a JSON Web Key.
func Parse(data []byte) (Key, error) {
var hints struct {
KeyType string `json:"kty"`
D *base64url.Value `json:"d,omitempty"`
}
err := json.Unmarshal(data, &hints)
if err != nil {
return nil, err
}
switch hints.KeyType {
case TypeEC:
if hints.D != nil {
return ParseECDSAPrivateKey(data)
}
return ParseECDSAPublicKey(data)
case TypeRSA:
if hints.D != nil {
return ParseRSAPrivateKey(data)
}
return ParseRSAPublicKey(data)
case TypeOCT:
return ParseOctetSequenceKey(data)
default:
return nil, fmt.Errorf("jwk: unsupported key type '%s'", hints.KeyType)
}
}
// Set represents a JSON Web Key Set.
type Set struct {
Keys []Key `json:"keys"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *Set) UnmarshalJSON(data []byte) error {
var raw struct {
Keys []json.RawMessage `json:"keys"`
}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
keys := make([]Key, len(raw.Keys))
for i, jwk := range raw.Keys {
key, err := Parse(jwk)
if err != nil {
return err
}
keys[i] = key
}
s.Keys = keys
return nil
}