-
Notifications
You must be signed in to change notification settings - Fork 19
/
rsa.go
187 lines (156 loc) · 4.27 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
185
186
187
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
)
type algRSA struct {
name string
hasher crypto.Hash
}
func (a *algRSA) Parse(private, public []byte) (privateKey PrivateKey, publicKey PublicKey, err error) {
if len(private) > 0 {
privateKey, err = ParsePrivateKeyRSA(private)
if err != nil {
return nil, nil, fmt.Errorf("RSA: private key: %v", err)
}
}
if len(public) > 0 {
publicKey, err = ParsePublicKeyRSA(public)
if err != nil {
return nil, nil, fmt.Errorf("RSA: public key: %v", err)
}
}
return
}
func (a *algRSA) Name() string {
return a.name
}
func (a *algRSA) Sign(key PrivateKey, headerAndPayload []byte) ([]byte, error) {
privateKey, ok := key.(*rsa.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)
return rsa.SignPKCS1v15(rand.Reader, privateKey, a.hasher, hashed)
}
func (a *algRSA) Verify(key PublicKey, headerAndPayload []byte, signature []byte) error {
publicKey, ok := key.(*rsa.PublicKey)
if !ok {
if privateKey, ok := key.(*rsa.PrivateKey); ok {
publicKey = &privateKey.PublicKey
} else {
return ErrInvalidKey
}
}
h := a.hasher.New()
// header.payload
_, err := h.Write(headerAndPayload)
if err != nil {
return err
}
hashed := h.Sum(nil)
if err = rsa.VerifyPKCS1v15(publicKey, a.hasher, hashed, signature); err != nil {
return fmt.Errorf("%w: %v", ErrTokenSignature, err)
}
return nil
}
// Key Helpers.
// MustLoadRSA accepts private and public PEM file paths
// and returns a pair of private and public RSA keys.
// Pass the returned private key to the `Token` (signing) function
// and the public key to the `Verify` function.
//
// It panics on errors.
func MustLoadRSA(privateKeyFilename, publicKeyFilename string) (*rsa.PrivateKey, *rsa.PublicKey) {
privateKey, err := LoadPrivateKeyRSA(privateKeyFilename)
if err != nil {
panicHandler(err)
}
publicKey, err := LoadPublicKeyRSA(publicKeyFilename)
if err != nil {
panicHandler(err)
}
return privateKey, publicKey
}
// LoadPrivateKeyRSA accepts a file path of a PEM-encoded RSA private key
// and returns the RSA private key Go value.
// Pass the returned value to the `Token` (signing) function.
func LoadPrivateKeyRSA(filename string) (*rsa.PrivateKey, error) {
b, err := ReadFile(filename)
if err != nil {
return nil, err
}
key, err := ParsePrivateKeyRSA(b)
if err != nil {
return nil, err
}
return key, nil
}
// LoadPublicKeyRSA accepts a file path of a PEM-encoded RSA public key
// and returns the RSA public key Go value.
// Pass the returned value to the `Verify` function.
func LoadPublicKeyRSA(filename string) (*rsa.PublicKey, error) {
b, err := ReadFile(filename)
if err != nil {
return nil, err
}
key, err := ParsePublicKeyRSA(b)
if err != nil {
return nil, err
}
return key, nil
}
// ParsePrivateKeyRSA decodes and parses the
// PEM-encoded RSA private key's raw contents.
// Pass the result to the `Token` (signing) function.
func ParsePrivateKeyRSA(key []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return nil, fmt.Errorf("private key: malformed or missing PEM format (RSA)")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
if key, err := x509.ParsePKCS8PrivateKey(block.Bytes); err == nil {
pKey, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("private key: expected a type of *rsa.PrivateKey")
}
privateKey = pKey
} else {
return nil, err
}
}
return privateKey, nil
}
// ParsePublicKeyRSA decodes and parses the
// PEM-encoded RSA public key's raw contents.
// Pass the result to the `Verify` function.
func ParsePublicKeyRSA(key []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return nil, fmt.Errorf("public key: malformed or missing PEM format (RSA)")
}
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.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("public key: expected a type of *rsa.PublicKey")
}
return publicKey, nil
}