/
algorithm.go
244 lines (228 loc) · 6.29 KB
/
algorithm.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
// Tideland Go REST Server Library - JSON Web Token - Algorithm
//
// Copyright (C) 2016 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package jwt
//--------------------
// IMPORTS
//--------------------
import (
"crypto"
"crypto/ecdsa"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/asn1"
"math/big"
"github.com/tideland/golib/errors"
)
//--------------------
// 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, errors.New(ErrInvalidAlgorithm, errorMessages, 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 errors.New(ErrInvalidAlgorithm, errorMessages, 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) {
hashSum := func() []byte {
hasher := h.New()
hasher.Write(data)
return hasher.Sum(nil)
}
switch key := k.(type) {
case *ecdsa.PrivateKey:
// ECDSA algorithms.
if a[0] != 'E' {
return nil, errors.New(ErrInvalidCombination, errorMessages, a, "ECDSA")
}
r, s, err := ecdsa.Sign(rand.Reader, key, hashSum())
if err != nil {
return nil, errors.Annotate(err, ErrCannotSign, errorMessages)
}
sig, err := asn1.Marshal(ecPoint{r, s})
if err != nil {
return nil, errors.Annotate(err, ErrCannotSign, errorMessages)
}
return Signature(sig), nil
case []byte:
// HMAC algorithms.
if a[0] != 'H' {
return nil, errors.New(ErrInvalidCombination, errorMessages, a, "HMAC")
}
hasher := hmac.New(h.New, key)
hasher.Write(data)
sig := hasher.Sum(nil)
return Signature(sig), nil
case *rsa.PrivateKey:
// RSA and RSAPSS algorithms.
if a[0] != 'P' && a[0] != 'R' {
return nil, errors.New(ErrInvalidCombination, errorMessages, a, "RSA(PSS)")
}
if a.isRSAPSS() {
// RSAPSS.
options := &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthAuto,
Hash: h,
}
sig, err := rsa.SignPSS(rand.Reader, key, h, hashSum(), options)
if err != nil {
return nil, errors.Annotate(err, ErrCannotSign, errorMessages)
}
return Signature(sig), nil
} else {
// RSA.
sig, err := rsa.SignPKCS1v15(rand.Reader, key, h, hashSum())
if err != nil {
return nil, errors.Annotate(err, ErrCannotSign, errorMessages)
}
return Signature(sig), nil
}
case string:
// None algorithm.
if a != "none" {
return nil, errors.New(ErrInvalidCombination, errorMessages, a, "none")
}
return Signature(""), nil
default:
// No valid key type.
return nil, errors.New(ErrInvalidKeyType, errorMessages, k)
}
}
// 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 {
hashSum := func() []byte {
hasher := h.New()
hasher.Write(data)
return hasher.Sum(nil)
}
switch key := k.(type) {
case *ecdsa.PublicKey:
// ECDSA algorithms.
if a[0] != 'E' {
return errors.New(ErrInvalidCombination, errorMessages, a, "ECDSA")
}
var ecp ecPoint
if _, err := asn1.Unmarshal(sig, &ecp); err != nil {
return errors.Annotate(err, ErrCannotVerify, errorMessages)
}
if !ecdsa.Verify(key, hashSum(), ecp.R, ecp.S) {
return errors.New(ErrInvalidSignature, errorMessages)
}
return nil
case []byte:
// HMAC algorithms.
if a[0] != 'H' {
return errors.New(ErrInvalidCombination, errorMessages, a, "HMAC")
}
expectedSig, err := a.sign(data, k, h)
if err != nil {
return errors.Annotate(err, ErrCannotVerify, errorMessages)
}
if !hmac.Equal(sig, expectedSig) {
return errors.New(ErrInvalidSignature, errorMessages)
}
return nil
case *rsa.PublicKey:
// RSA and RSAPSS algorithms.
if a[0] != 'P' && a[0] != 'R' {
return errors.New(ErrInvalidCombination, errorMessages, a, "RSA(PSS)")
}
if a.isRSAPSS() {
// RSAPSS.
options := &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthAuto,
Hash: h,
}
if err := rsa.VerifyPSS(key, h, hashSum(), sig, options); err != nil {
return errors.Annotate(err, ErrInvalidSignature, errorMessages)
}
} else {
// RSA.
if err := rsa.VerifyPKCS1v15(key, h, hashSum(), sig); err != nil {
return errors.Annotate(err, ErrInvalidSignature, errorMessages)
}
}
return nil
case string:
// None algorithm.
if a != "none" {
return errors.New(ErrInvalidCombination, errorMessages, a, "none")
}
if len(sig) > 0 {
return errors.New(ErrInvalidSignature, errorMessages)
}
return nil
default:
// No valid key type.
return errors.New(ErrInvalidKeyType, errorMessages, k)
}
}
// EOF