forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
okp.go
180 lines (154 loc) · 4.18 KB
/
okp.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
package jwk
import (
"bytes"
"crypto"
"crypto/ed25519"
"fmt"
"github.com/lestrrat-go/blackmagic"
"github.com/lestrrat-go/jwx/internal/base64"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/x25519"
"github.com/pkg/errors"
)
func (k *okpPublicKey) FromRaw(rawKeyIf interface{}) error {
k.mu.Lock()
defer k.mu.Unlock()
var crv jwa.EllipticCurveAlgorithm
switch rawKey := rawKeyIf.(type) {
case ed25519.PublicKey:
k.x = rawKey
crv = jwa.Ed25519
k.crv = &crv
case x25519.PublicKey:
k.x = rawKey
crv = jwa.X25519
k.crv = &crv
default:
return errors.Errorf(`unknown key type %T`, rawKeyIf)
}
return nil
}
func (k *okpPrivateKey) FromRaw(rawKeyIf interface{}) error {
k.mu.Lock()
defer k.mu.Unlock()
var crv jwa.EllipticCurveAlgorithm
switch rawKey := rawKeyIf.(type) {
case ed25519.PrivateKey:
k.d = rawKey.Seed()
k.x = rawKey.Public().(ed25519.PublicKey) //nolint:forcetypeassert
crv = jwa.Ed25519
k.crv = &crv
case x25519.PrivateKey:
k.d = rawKey.Seed()
k.x = rawKey.Public().(x25519.PublicKey) //nolint:forcetypeassert
crv = jwa.X25519
k.crv = &crv
default:
return errors.Errorf(`unknown key type %T`, rawKeyIf)
}
return nil
}
func buildOKPPublicKey(alg jwa.EllipticCurveAlgorithm, xbuf []byte) (interface{}, error) {
switch alg {
case jwa.Ed25519:
return ed25519.PublicKey(xbuf), nil
case jwa.X25519:
return x25519.PublicKey(xbuf), nil
default:
return nil, errors.Errorf(`invalid curve algorithm %s`, alg)
}
}
// Raw returns the EC-DSA public key represented by this JWK
func (k *okpPublicKey) Raw(v interface{}) error {
k.mu.RLock()
defer k.mu.RUnlock()
pubk, err := buildOKPPublicKey(k.Crv(), k.x)
if err != nil {
return errors.Wrap(err, `failed to build public key`)
}
return blackmagic.AssignIfCompatible(v, pubk)
}
func buildOKPPrivateKey(alg jwa.EllipticCurveAlgorithm, xbuf []byte, dbuf []byte) (interface{}, error) {
switch alg {
case jwa.Ed25519:
ret := ed25519.NewKeyFromSeed(dbuf)
if !bytes.Equal(xbuf, ret.Public().(ed25519.PublicKey)) {
return nil, errors.Errorf(`invalid x value given d value`)
}
return ret, nil
case jwa.X25519:
ret, err := x25519.NewKeyFromSeed(dbuf)
if err != nil {
return nil, errors.Wrap(err, `unable to construct x25519 private key from seed`)
}
if !bytes.Equal(xbuf, ret.Public().(x25519.PublicKey)) {
return nil, errors.Errorf(`invalid x value given d value`)
}
return ret, nil
default:
return nil, errors.Errorf(`invalid curve algorithm %s`, alg)
}
}
func (k *okpPrivateKey) Raw(v interface{}) error {
k.mu.RLock()
defer k.mu.RUnlock()
privk, err := buildOKPPrivateKey(k.Crv(), k.x, k.d)
if err != nil {
return errors.Wrap(err, `failed to build public key`)
}
return blackmagic.AssignIfCompatible(v, privk)
}
func makeOKPPublicKey(v interface {
makePairs() []*HeaderPair
}) (Key, error) {
newKey := NewOKPPublicKey()
// Iterate and copy everything except for the bits that should not be in the public key
for _, pair := range v.makePairs() {
switch pair.Key {
case OKPDKey:
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 *okpPrivateKey) PublicKey() (Key, error) {
return makeOKPPublicKey(k)
}
func (k *okpPublicKey) PublicKey() (Key, error) {
return makeOKPPublicKey(k)
}
func okpThumbprint(hash crypto.Hash, crv, x string) []byte {
h := hash.New()
fmt.Fprint(h, `{"crv":"`)
fmt.Fprint(h, crv)
fmt.Fprint(h, `","kty":"OKP","x":"`)
fmt.Fprint(h, x)
fmt.Fprint(h, `"}`)
return h.Sum(nil)
}
// Thumbprint returns the JWK thumbprint using the indicated
// hashing algorithm, according to RFC 7638 / 8037
func (k okpPublicKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
k.mu.RLock()
defer k.mu.RUnlock()
return okpThumbprint(
hash,
k.Crv().String(),
base64.EncodeToString(k.x),
), nil
}
// Thumbprint returns the JWK thumbprint using the indicated
// hashing algorithm, according to RFC 7638 / 8037
func (k okpPrivateKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
k.mu.RLock()
defer k.mu.RUnlock()
return okpThumbprint(
hash,
k.Crv().String(),
base64.EncodeToString(k.x),
), nil
}