-
Notifications
You must be signed in to change notification settings - Fork 19
/
ecdsa.go
209 lines (173 loc) · 4.8 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
package jwt
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
)
type algECDSA struct {
name string
hasher crypto.Hash
keySize int
curveBits int
}
func (a *algECDSA) Parse(private, public []byte) (privateKey PrivateKey, publicKey PublicKey, err error) {
if len(private) > 0 {
privateKey, err = ParsePrivateKeyECDSA(private)
if err != nil {
return nil, nil, fmt.Errorf("ECDSA: private key: %v", err)
}
}
if len(public) > 0 {
publicKey, err = ParsePublicKeyECDSA(public)
if err != nil {
return nil, nil, fmt.Errorf("ECDSA: public key: %v", err)
}
}
return
}
func (a *algECDSA) Name() string {
return a.name
}
// JWT handbook chapter 7.2.2.3.1 Algorithm
// The following code is a clone of the js code described in the book.
func (a *algECDSA) Sign(key PrivateKey, headerAndPayload []byte) ([]byte, error) {
privateKey, ok := key.(*ecdsa.PrivateKey)
if !ok {
return nil, ErrInvalidKey
}
h := a.hasher.New()
// header.payload
_, err := h.Write(headerAndPayload)
if err != nil {
return nil, err
}
hashed := h.Sum(nil)
r, s, err := ecdsa.Sign(rand.Reader, privateKey, hashed)
if err != nil {
return nil, err
}
curveBits := privateKey.Curve.Params().BitSize
if a.curveBits != curveBits {
return nil, ErrInvalidKey
}
keyBytes := curveBits / 8
if curveBits%8 > 0 {
keyBytes++
}
rBytes := r.Bytes()
rBytesPadded := make([]byte, keyBytes)
copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
sBytes := s.Bytes()
sBytesPadded := make([]byte, keyBytes)
copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
signature := append(rBytesPadded, sBytesPadded...)
return signature, nil
}
func (a *algECDSA) Verify(key PublicKey, headerAndPayload []byte, signature []byte) error {
publicKey, ok := key.(*ecdsa.PublicKey)
if !ok {
if privateKey, ok := key.(*ecdsa.PrivateKey); ok {
publicKey = &privateKey.PublicKey
} else {
return ErrInvalidKey
}
}
if len(signature) != 2*a.keySize {
return ErrTokenSignature
}
r := big.NewInt(0).SetBytes(signature[:a.keySize])
s := big.NewInt(0).SetBytes(signature[a.keySize:])
h := a.hasher.New()
// header.payload
_, err := h.Write(headerAndPayload)
if err != nil {
return err
}
hashed := h.Sum(nil)
if !ecdsa.Verify(publicKey, hashed, r, s) {
return ErrTokenSignature
}
return nil
}
// Key Helpers.
// MustLoadECDSA accepts private and public PEM filenames
// and returns a pair of private and public ECDSA keys.
// Pass the returned private key to the `Token` (signing) function
// and the public key to the `Verify` function.
//
// It panics on errors.
func MustLoadECDSA(privateKeyFilename, publicKeyFilename string) (*ecdsa.PrivateKey, *ecdsa.PublicKey) {
privateKey, err := LoadPrivateKeyECDSA(privateKeyFilename)
if err != nil {
panicHandler(err)
}
publicKey, err := LoadPublicKeyECDSA(publicKeyFilename)
if err != nil {
panicHandler(err)
}
return privateKey, publicKey
}
// LoadPrivateKeyECDSA accepts a file path of a PEM-encoded ECDSA private key
// and returns the ECDSA private key Go value.
// Pass the returned value to the `Token` (signing) function.
func LoadPrivateKeyECDSA(filename string) (*ecdsa.PrivateKey, error) {
b, err := ReadFile(filename)
if err != nil {
return nil, err
}
key, err := ParsePrivateKeyECDSA(b)
if err != nil {
return nil, err
}
return key, nil
}
// LoadPublicKeyECDSA accepts a file path of a PEM-encoded ECDSA public key
// and returns the ECDSA public key Go value.
// Pass the returned value to the `Verify` function.
func LoadPublicKeyECDSA(filename string) (*ecdsa.PublicKey, error) {
b, err := ReadFile(filename)
if err != nil {
return nil, err
}
key, err := ParsePublicKeyECDSA(b)
if err != nil {
return nil, err
}
return key, nil
}
// ParsePrivateKeyECDSA decodes and parses the
// PEM-encoded ECDSA private key's raw contents.
// Pass the result to the `Token` (signing) function.
func ParsePrivateKeyECDSA(key []byte) (*ecdsa.PrivateKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return nil, fmt.Errorf("private key: malformed or missing PEM format (ECDSA)")
}
return x509.ParseECPrivateKey(block.Bytes)
}
// ParsePublicKeyECDSA decodes and parses the
// PEM-encoded ECDSA public key's raw contents.
// Pass the result to the `Verify` function.
func ParsePublicKeyECDSA(key []byte) (*ecdsa.PublicKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return nil, fmt.Errorf("public key: malformed or missing PEM format (ECDSA)")
}
parsedKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
parsedKey = cert.PublicKey
} else {
return nil, err
}
}
publicKey, ok := parsedKey.(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("public key: malformed or missing PEM format (ECDSA)")
}
return publicKey, nil
}