-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
279 lines (255 loc) · 7.62 KB
/
crypto.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Tideland Go JSON Web Token
//
// Copyright (C) 2016-2021 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package jwt // import "tideland.dev/go/jwt"
//--------------------
// IMPORTS
//--------------------
import (
"crypto"
"crypto/ecdsa"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"encoding/asn1"
"fmt"
"math/big"
// Import hashing packages just to register them via init().
_ "crypto/sha256"
_ "crypto/sha512"
)
//--------------------
// SIGNATURE
//--------------------
// Signature is the resulting signature when signing
// a token.
type Signature []byte
//--------------------
// ALGORITHM
//--------------------
// Algorithm describes the algorithm used to sign a token.
type Algorithm string
// Definition of the supported algorithms.
const (
ES256 Algorithm = "ES256"
ES384 Algorithm = "ES384"
ES512 Algorithm = "ES512"
HS256 Algorithm = "HS256"
HS384 Algorithm = "HS384"
HS512 Algorithm = "HS512"
PS256 Algorithm = "PS256"
PS384 Algorithm = "PS384"
PS512 Algorithm = "PS512"
RS256 Algorithm = "RS256"
RS384 Algorithm = "RS384"
RS512 Algorithm = "RS512"
NONE Algorithm = "none"
)
// ecPoint is needed to marshal R and S of the ECDSA algorithms.
type ecPoint struct {
R *big.Int
S *big.Int
}
// Sign creates the signature for the data based on the
// algorithm and the key.
func (a Algorithm) Sign(data []byte, key Key) (Signature, error) {
switch a {
case ES256, HS256, PS256, RS256:
return a.sign(data, key, crypto.SHA256)
case ES384, HS384, PS384, RS384:
return a.sign(data, key, crypto.SHA384)
case ES512, HS512, PS512, RS512:
return a.sign(data, key, crypto.SHA512)
case NONE:
return a.sign(data, key, 0)
default:
return nil, fmt.Errorf("signing algorithm '%s' is invalid", a)
}
}
// Verify checks if the signature is correct for the data when using
// the passed key.
func (a Algorithm) Verify(data []byte, sig Signature, key Key) error {
switch a {
case ES256, HS256, PS256, RS256:
return a.verify(data, sig, key, crypto.SHA256)
case ES384, HS384, PS384, RS384:
return a.verify(data, sig, key, crypto.SHA384)
case ES512, HS512, PS512, RS512:
return a.verify(data, sig, key, crypto.SHA512)
case NONE:
return a.verify(data, sig, key, 0)
default:
return fmt.Errorf("verifying algorithm '%s' is invalid", a)
}
}
// isRSAPSS returns true when the algorithm is one of
// the RSAPSS algorithms.
func (a Algorithm) isRSAPSS() bool {
return a[0] == 'P'
}
// sign signs the passed data based on the key and the passed hash.
func (a Algorithm) sign(data []byte, k Key, h crypto.Hash) (Signature, error) {
switch key := k.(type) {
case *ecdsa.PrivateKey:
// ECDSA algorithms.
return a.signECDSA(data, key, h)
case []byte:
// HMAC algorithms.
return a.signHMAC(data, key, h)
case *rsa.PrivateKey:
// RSA and RSAPSS algorithms.
return a.signRSA(data, key, h)
case string:
// None algorithm.
if a != "none" {
return nil, fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "none")
}
return Signature(""), nil
default:
// No valid key type.
return nil, fmt.Errorf("key type %T is invalid", k)
}
}
// signECDSA signs the data using the ECDSA algorithm.
func (a Algorithm) signECDSA(data []byte, key *ecdsa.PrivateKey, h crypto.Hash) (Signature, error) {
if a[0] != 'E' {
return nil, fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "ECDSA")
}
r, s, err := ecdsa.Sign(rand.Reader, key, hashSum(data, h))
if err != nil {
return nil, fmt.Errorf("cannot sign the data: %v", err)
}
sig, err := asn1.Marshal(ecPoint{r, s})
if err != nil {
return nil, fmt.Errorf("cannot sign the data: %v", err)
}
return Signature(sig), nil
}
// signHMAC signs the data using the HMAC algorithm.
func (a Algorithm) signHMAC(data, key []byte, h crypto.Hash) (Signature, error) {
if a[0] != 'H' {
return nil, fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "HMAC")
}
hasher := hmac.New(h.New, key)
if _, err := hasher.Write(data); err != nil {
return nil, err
}
sig := hasher.Sum(nil)
return Signature(sig), nil
}
// signRSA signs the data using the RSAPSS or RSA algorithm.
func (a Algorithm) signRSA(data []byte, key *rsa.PrivateKey, h crypto.Hash) (Signature, error) {
if a[0] != 'P' && a[0] != 'R' {
return nil, fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "RSA(PSS)")
}
if a.isRSAPSS() {
// RSAPSS.
options := &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthAuto,
Hash: h,
}
sig, err := rsa.SignPSS(rand.Reader, key, h, hashSum(data, h), options)
if err != nil {
return nil, fmt.Errorf("cannot sign the data: %v", err)
}
return Signature(sig), nil
}
// RSA.
sig, err := rsa.SignPKCS1v15(rand.Reader, key, h, hashSum(data, h))
if err != nil {
return nil, fmt.Errorf("cannot sign the data: %v", err)
}
return Signature(sig), nil
}
// verify checks if the signature is correct for the passed data
// based on the key and the passed hash.
func (a Algorithm) verify(data []byte, sig Signature, k Key, h crypto.Hash) error {
switch key := k.(type) {
case *ecdsa.PublicKey:
// ECDSA algorithms.
return a.verifyECDSA(data, sig, key, h)
case []byte:
// HMAC algorithms.
return a.verifyHMAC(data, sig, key, h)
case *rsa.PublicKey:
// RSA and RSAPSS algorithms.
return a.verifyRSA(data, sig, key, h)
case string:
// None algorithm.
if a != "none" {
return fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "none")
}
if len(sig) > 0 {
return fmt.Errorf("data signature is invalid")
}
return nil
default:
// No valid key type.
return fmt.Errorf("key type %T is invalid", k)
}
}
// verifyECDSA verifies the data using the ECDSA algorithm.
func (a Algorithm) verifyECDSA(data []byte, sig Signature, key *ecdsa.PublicKey, h crypto.Hash) error {
if a[0] != 'E' {
return fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "ECDSA")
}
var ecp ecPoint
if _, err := asn1.Unmarshal(sig, &ecp); err != nil {
return fmt.Errorf("cannot verify the data: %v", err)
}
if !ecdsa.Verify(key, hashSum(data, h), ecp.R, ecp.S) {
return fmt.Errorf("data signature is invalid")
}
return nil
}
// verifyHMAC verifies the data using the HMAC algorithm.
func (a Algorithm) verifyHMAC(data []byte, sig Signature, key []byte, h crypto.Hash) error {
if a[0] != 'H' {
return fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "HMAC")
}
expectedSig, err := a.sign(data, key, h)
if err != nil {
return fmt.Errorf("cannot verify the data: %v", err)
}
if !hmac.Equal(sig, expectedSig) {
return fmt.Errorf("data signature is invalid")
}
return nil
}
// verifyRSA verifies the data using the RSAPSS or RSS algorithm.
func (a Algorithm) verifyRSA(data []byte, sig Signature, key *rsa.PublicKey, h crypto.Hash) error {
if a[0] != 'P' && a[0] != 'R' {
return fmt.Errorf("invalid combination of algorithm '%s' and key type '%s'", a, "RSA(PSS)")
}
if a.isRSAPSS() {
// RSAPSS.
options := &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthAuto,
Hash: h,
}
if err := rsa.VerifyPSS(key, h, hashSum(data, h), sig, options); err != nil {
return fmt.Errorf("data signature is invalid: %v", err)
}
} else {
// RSA.
if err := rsa.VerifyPKCS1v15(key, h, hashSum(data, h), sig); err != nil {
return fmt.Errorf("data signature is invalid: %v", err)
}
}
return nil
}
//--------------------
// HELPERS
//--------------------
// hashSum determines the hash sum of the passed data.
func hashSum(data []byte, h crypto.Hash) []byte {
hasher := h.New()
if _, err := hasher.Write(data); err != nil {
panic(err)
}
return hasher.Sum(nil)
}
// EOF