forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsa.go
230 lines (196 loc) · 5.87 KB
/
ecdsa.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
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
package jwk
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"fmt"
"math/big"
"github.com/lestrrat-go/jwx/internal/base64"
"github.com/lestrrat-go/jwx/internal/blackmagic"
"github.com/lestrrat-go/jwx/internal/ecutil"
"github.com/lestrrat-go/jwx/jwa"
"github.com/pkg/errors"
)
func NewECDSAPublicKey() ECDSAPublicKey {
return newECDSAPublicKey()
}
func newECDSAPublicKey() *ecdsaPublicKey {
return &ecdsaPublicKey{
privateParams: make(map[string]interface{}),
}
}
func NewECDSAPrivateKey() ECDSAPrivateKey {
return newECDSAPrivateKey()
}
func newECDSAPrivateKey() *ecdsaPrivateKey {
return &ecdsaPrivateKey{
privateParams: make(map[string]interface{}),
}
}
func (k *ecdsaPublicKey) FromRaw(rawKey *ecdsa.PublicKey) error {
xbuf := ecutil.AllocECPointBuffer(rawKey.X, rawKey.Curve)
ybuf := ecutil.AllocECPointBuffer(rawKey.Y, rawKey.Curve)
defer ecutil.ReleaseECPointBuffer(xbuf)
defer ecutil.ReleaseECPointBuffer(ybuf)
k.x = make([]byte, len(xbuf))
copy(k.x, xbuf)
k.y = make([]byte, len(ybuf))
copy(k.y, ybuf)
switch rawKey.Curve {
case elliptic.P256():
if err := k.Set(ECDSACrvKey, jwa.P256); err != nil {
return errors.Wrap(err, `failed to set header`)
}
case elliptic.P384():
if err := k.Set(ECDSACrvKey, jwa.P384); err != nil {
return errors.Wrap(err, `failed to set header`)
}
case elliptic.P521():
if err := k.Set(ECDSACrvKey, jwa.P521); err != nil {
return errors.Wrap(err, `failed to set header`)
}
default:
return errors.Errorf(`invalid elliptic curve %s`, rawKey.Curve)
}
return nil
}
func (k *ecdsaPrivateKey) FromRaw(rawKey *ecdsa.PrivateKey) error {
xbuf := ecutil.AllocECPointBuffer(rawKey.X, rawKey.Curve)
ybuf := ecutil.AllocECPointBuffer(rawKey.Y, rawKey.Curve)
dbuf := ecutil.AllocECPointBuffer(rawKey.D, rawKey.Curve)
defer ecutil.ReleaseECPointBuffer(xbuf)
defer ecutil.ReleaseECPointBuffer(ybuf)
defer ecutil.ReleaseECPointBuffer(dbuf)
k.x = make([]byte, len(xbuf))
copy(k.x, xbuf)
k.y = make([]byte, len(ybuf))
copy(k.y, ybuf)
k.d = make([]byte, len(dbuf))
copy(k.d, dbuf)
switch rawKey.Curve {
case elliptic.P256():
if err := k.Set(ECDSACrvKey, jwa.P256); err != nil {
return errors.Wrap(err, "failed to write header")
}
case elliptic.P384():
if err := k.Set(ECDSACrvKey, jwa.P384); err != nil {
return errors.Wrap(err, "failed to write header")
}
case elliptic.P521():
if err := k.Set(ECDSACrvKey, jwa.P521); err != nil {
return errors.Wrap(err, "failed to write header")
}
default:
return errors.Errorf(`invalid elliptic curve %s`, rawKey.Curve)
}
return nil
}
func buildECDSAPublicKey(alg jwa.EllipticCurveAlgorithm, xbuf, ybuf []byte) (*ecdsa.PublicKey, error) {
var curve elliptic.Curve
switch alg {
case jwa.P256:
curve = elliptic.P256()
case jwa.P384:
curve = elliptic.P384()
case jwa.P521:
curve = elliptic.P521()
default:
return nil, errors.Errorf(`invalid curve algorithm %s`, alg)
}
var x, y big.Int
x.SetBytes(xbuf)
y.SetBytes(ybuf)
return &ecdsa.PublicKey{Curve: curve, X: &x, Y: &y}, nil
}
// Raw returns the EC-DSA public key represented by this JWK
func (k *ecdsaPublicKey) Raw(v interface{}) error {
pubk, err := buildECDSAPublicKey(k.Crv(), k.x, k.y)
if err != nil {
return errors.Wrap(err, `failed to build public key`)
}
return blackmagic.AssignIfCompatible(v, pubk)
}
func (k *ecdsaPrivateKey) Raw(v interface{}) error {
pubk, err := buildECDSAPublicKey(k.Crv(), k.x, k.y)
if err != nil {
return errors.Wrap(err, `failed to build public key`)
}
var key ecdsa.PrivateKey
var d big.Int
d.SetBytes(k.d)
key.D = &d
key.PublicKey = *pubk
return blackmagic.AssignIfCompatible(v, &key)
}
func makeECDSAPublicKey(v interface {
Iterate(context.Context) HeaderIterator
}) (Key, error) {
newKey := NewECDSAPublicKey()
// Iterate and copy everything except for the bits that should not be in the public key
for iter := v.Iterate(context.TODO()); iter.Next(context.TODO()); {
pair := iter.Pair()
switch pair.Key {
case ECDSADKey:
continue
default:
if err := newKey.Set(pair.Key.(string), pair.Value); err != nil {
return nil, errors.Wrapf(err, `failed to set field %s`, pair.Key)
}
}
}
return newKey, nil
}
func (k *ecdsaPrivateKey) PublicKey() (Key, error) {
return makeECDSAPublicKey(k)
}
func (k *ecdsaPublicKey) PublicKey() (Key, error) {
return makeECDSAPublicKey(k)
}
func ecdsaThumbprint(hash crypto.Hash, crv, x, y string) []byte {
h := hash.New()
fmt.Fprint(h, `{"crv":"`)
fmt.Fprint(h, crv)
fmt.Fprint(h, `","kty":"EC","x":"`)
fmt.Fprint(h, x)
fmt.Fprint(h, `","y":"`)
fmt.Fprint(h, y)
fmt.Fprint(h, `"}`)
return h.Sum(nil)
}
// Thumbprint returns the JWK thumbprint using the indicated
// hashing algorithm, according to RFC 7638
func (k ecdsaPublicKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
var key ecdsa.PublicKey
if err := k.Raw(&key); err != nil {
return nil, errors.Wrap(err, `failed to materialize ecdsa.PublicKey for thumbprint generation`)
}
xbuf := ecutil.AllocECPointBuffer(key.X, key.Curve)
ybuf := ecutil.AllocECPointBuffer(key.Y, key.Curve)
defer ecutil.ReleaseECPointBuffer(xbuf)
defer ecutil.ReleaseECPointBuffer(ybuf)
return ecdsaThumbprint(
hash,
key.Curve.Params().Name,
base64.EncodeToString(xbuf),
base64.EncodeToString(ybuf),
), nil
}
// Thumbprint returns the JWK thumbprint using the indicated
// hashing algorithm, according to RFC 7638
func (k ecdsaPrivateKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
var key ecdsa.PrivateKey
if err := k.Raw(&key); err != nil {
return nil, errors.Wrap(err, `failed to materialize ecdsa.PrivateKey for thumbprint generation`)
}
xbuf := ecutil.AllocECPointBuffer(key.X, key.Curve)
ybuf := ecutil.AllocECPointBuffer(key.Y, key.Curve)
defer ecutil.ReleaseECPointBuffer(xbuf)
defer ecutil.ReleaseECPointBuffer(ybuf)
return ecdsaThumbprint(
hash,
key.Curve.Params().Name,
base64.EncodeToString(xbuf),
base64.EncodeToString(ybuf),
), nil
}