-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa.go
184 lines (152 loc) · 4.13 KB
/
rsa.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
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)
// SigningAlgorithmRSA represents an RSA signing algorithm
type SigningAlgorithmRSA struct {
name string
hash crypto.Hash
}
// Instances of support hashing algs
var (
RSA = &SigningAlgorithmRSA{"RS256", crypto.SHA256}
RS256 = &SigningAlgorithmRSA{"RS256", crypto.SHA256}
RS384 = &SigningAlgorithmRSA{"RS256", crypto.SHA384}
RS512 = &SigningAlgorithmRSA{"RS512", crypto.SHA512}
)
// Name returns the name of the algorithm as specified in JSON Web Algorithms
func (alg *SigningAlgorithmRSA) Name() string {
return alg.name
}
func (alg *SigningAlgorithmRSA) sign(payload string, key interface{}) ([]byte, error) {
var (
rsaKey *rsa.PrivateKey
err error
)
switch k := key.(type) {
case string:
if rsaKey, err = ParseRSAPrivateKeyFromPEM([]byte(k)); err != nil {
return nil, err
}
case []byte:
if rsaKey, err = ParseRSAPrivateKeyFromPEM([]byte(k)); err != nil {
return nil, err
}
case *rsa.PrivateKey:
rsaKey = k
default:
return nil, ErrInvalidKey
}
hashFunc, err := newHashFunc(alg.hash)
if err != nil {
return nil, err
}
hasher := hashFunc()
hasher.Write([]byte(payload))
// Sign the string and return the encoded bytes
if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, alg.hash, hasher.Sum(nil)); err == nil {
return sigBytes, nil
}
return nil, err
}
// Sign takes a string payload and a key as either an rsa.PrivateKey or a string or
// byte array containing a PEM encoded key.
// Either returns the signature as a string or an error.
func (alg *SigningAlgorithmRSA) Sign(payload string, key interface{}) (string, error) {
var (
sigBytes []byte
err error
)
if sigBytes, err = alg.sign(payload, key); err == nil {
return encode(sigBytes), nil
}
return "", err
}
// Verify checks that the signature is valid
func (alg *SigningAlgorithmRSA) Verify(payload string, signature string, key interface{}) error {
var (
rsaKey *rsa.PublicKey
sigBytes []byte
err error
)
// decode the signature
if sigBytes, err = decode(signature); err != nil {
return err
}
switch k := key.(type) {
case string:
if rsaKey, err = ParseRSAPublicKeyFromPEM([]byte(k)); err != nil {
return err
}
case []byte:
if rsaKey, err = ParseRSAPublicKeyFromPEM([]byte(k)); err != nil {
return err
}
case *rsa.PublicKey:
rsaKey = k
default:
return ErrInvalidKey
}
hashFunc, err := newHashFunc(alg.hash)
if err != nil {
return err
}
hasher := hashFunc()
hasher.Write([]byte(payload))
return rsa.VerifyPKCS1v15(rsaKey, alg.hash, hasher.Sum(nil), sigBytes)
}
// Errors relating to parsing PEMs
var (
ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key")
)
// ParseRSAPrivateKeyFromPEM decodes a PEM encoded PKCS1 or PKCS8 private key
func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
var err error
// Parse PEM block
var block *pem.Block
if block, _ = pem.Decode(key); block == nil {
return nil, ErrKeyMustBePEMEncoded
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
return nil, err
}
}
var pkey *rsa.PrivateKey
var ok bool
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
return nil, ErrNotRSAPrivateKey
}
return pkey, nil
}
// ParseRSAPublicKeyFromPEM decodes a PEM encoded PKCS1 or PKCS8 public key
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
var err error
// Parse PEM block
var block *pem.Block
if block, _ = pem.Decode(key); block == nil {
return nil, ErrKeyMustBePEMEncoded
}
// Parse the key
var parsedKey interface{}
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
parsedKey = cert.PublicKey
} else {
return nil, err
}
}
var pkey *rsa.PublicKey
var ok bool
if pkey, ok = parsedKey.(*rsa.PublicKey); !ok {
return nil, ErrNotRSAPrivateKey
}
return pkey, nil
}