-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec.go
168 lines (142 loc) · 3.6 KB
/
ec.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
package jwk
import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/json"
"errors"
"fmt"
"github.com/ericyan/jwk/internal/base64url"
)
// ECDSAPublicKey represents an ECDSA public key, which contains
// algorithm-specific parameters defined in RFC7518, Section 6.2.1.
//
// ECDSAPublicKey implements the Key interface.
type ECDSAPublicKey struct {
*Params
CRV string `json:"crv"`
X *base64url.Value `json:"x"`
Y *base64url.Value `json:"y"`
pub *ecdsa.PublicKey
}
// NewECDSAPublicKey creates a new ECDSAPublicKey.
func NewECDSAPublicKey(pub *ecdsa.PublicKey, params *Params) (*ECDSAPublicKey, error) {
if params == nil {
params = &Params{KeyType: TypeEC}
}
if params.KeyType == "" {
params.KeyType = TypeEC
}
if params.KeyType != TypeEC {
return nil, errors.New("jwk: invalid params, wrong key type")
}
if pub == nil || pub.X == nil || pub.Y == nil {
return nil, errors.New("jwk: invalid crypto key")
}
var crv string
switch pub.Curve {
case elliptic.P256():
crv = "P-256"
case elliptic.P384():
crv = "P-384"
case elliptic.P521():
crv = "P-521"
default:
return nil, fmt.Errorf("jwk: unsupported elliptic curve")
}
return &ECDSAPublicKey{
params,
crv,
base64url.NewBigInt(pub.X),
base64url.NewBigInt(pub.Y),
pub,
}, nil
}
// ParseECDSAPublicKey parses the JSON Web Key as an ECDSA public key.
func ParseECDSAPublicKey(jwk []byte) (*ECDSAPublicKey, error) {
key := new(ECDSAPublicKey)
err := json.Unmarshal(jwk, key)
if err != nil {
return nil, err
}
if key.KeyType != TypeEC {
return nil, errors.New("jwk: invalid JWT, wrong type")
}
if key.X == nil {
return nil, errors.New("jwk: invalid JWT, missing N")
}
if key.Y == nil {
return nil, errors.New("jwk: invalid JWT, missing E")
}
var curve elliptic.Curve
switch key.CRV {
case "P-256":
curve = elliptic.P256()
case "P-384":
curve = elliptic.P384()
case "P-521":
curve = elliptic.P521()
default:
return nil, fmt.Errorf("jwk: unsupported elliptic curve")
}
key.pub = &ecdsa.PublicKey{
Curve: curve,
X: key.X.BigInt(),
Y: key.Y.BigInt(),
}
return key, nil
}
// CryptoKey returns the underlying cryptographic key.
func (key *ECDSAPublicKey) CryptoKey() CryptoKey {
return key.pub
}
// ECDSAPrivateKey represents an ECDSA private key, which contains
// algorithm-specific parameters defined in RFC 7518, Section 6.2.2.
//
// ECDSAPrivate implements the Key interface.
type ECDSAPrivateKey struct {
*ECDSAPublicKey
D *base64url.Value `json:"d"`
priv *ecdsa.PrivateKey
}
// NewECDSAPrivateKey creates a new ECDSAPrivate.
func NewECDSAPrivateKey(priv *ecdsa.PrivateKey, params *Params) (*ECDSAPrivateKey, error) {
if priv == nil {
return nil, errors.New("jwk: invalid crypto key")
}
pub, err := NewECDSAPublicKey(&priv.PublicKey, params)
if err != nil {
return nil, err
}
key := &ECDSAPrivateKey{
ECDSAPublicKey: pub,
D: base64url.NewBigInt(priv.D),
priv: priv,
}
return key, nil
}
// ParseECDSAPrivateKey parses the JSON Web Key as an ECDSA private key.
func ParseECDSAPrivateKey(jwk []byte) (*ECDSAPrivateKey, error) {
key := new(ECDSAPrivateKey)
err := json.Unmarshal(jwk, key)
if err != nil {
return nil, err
}
if key.D == nil {
return nil, errors.New("jwk: invalid JWT, missing D")
}
pub, err := ParseECDSAPublicKey(jwk)
if err != nil {
return nil, err
}
key.ECDSAPublicKey = pub
priv := &ecdsa.PrivateKey{
PublicKey: *key.ECDSAPublicKey.pub,
D: key.D.BigInt(),
}
key.priv = priv
return key, nil
}
// CryptoKey returns the underlying cryptographic key.
func (key *ECDSAPrivateKey) CryptoKey() CryptoKey {
return key.priv
}