-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrsa.go
More file actions
259 lines (228 loc) · 7.28 KB
/
rsa.go
File metadata and controls
259 lines (228 loc) · 7.28 KB
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
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
)
// algRSA implements the Alg interface for RSA signature algorithms.
// It supports RS256, RS384, and RS512 variants using PKCS#1 v1.5 padding
// with SHA-256, SHA-384, and SHA-512 respectively.
type algRSA struct {
name string // Algorithm name (e.g., "RS256", "RS384", "RS512")
hasher crypto.Hash // Hash function to use (SHA256, SHA384, or SHA512)
}
// Parse implements the AlgParser interface for RSA algorithms.
// It parses PEM-encoded private and public keys and returns the corresponding
// *rsa.PrivateKey and *rsa.PublicKey instances.
//
// Either private or public can be empty, but at least one should be provided.
// Returns an error if the key parsing fails or the key format is invalid.
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
}
// Name returns the algorithm name (e.g., "RS256", "RS384", "RS512").
func (a *algRSA) Name() string {
return a.name
}
// Sign implements the Alg interface for RSA signature generation.
// It creates an RSA signature using PKCS#1 v1.5 padding with the provided private key.
//
// The key must be an *rsa.PrivateKey. For security, RSA keys should be at least
// 2048 bits in length (3072+ bits recommended for new applications).
//
// Returns an error if the key is invalid or signing fails.
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)
}
// Verify implements the Alg interface for RSA signature verification.
// It verifies an RSA signature using PKCS#1 v1.5 padding against the provided public key.
//
// The method accepts either an *rsa.PublicKey or an *rsa.PrivateKey
// (from which it extracts the public key).
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 Sign functions and
// the public key to Verify functions.
//
// This function panics if either key file cannot be read or parsed.
// Use LoadPrivateKeyRSA and LoadPublicKeyRSA for error handling.
//
// Example:
//
// privateKey, publicKey := jwt.MustLoadRSA("rsa_private.pem", "rsa_public.pem")
// token, err := jwt.Sign(jwt.RS256, privateKey, claims)
// verifiedToken, err := jwt.Verify(jwt.RS256, publicKey, token)
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 loads and parses a PEM-encoded RSA private key from a file.
//
// The file should contain a PEM-encoded RSA private key in PKCS#1 or PKCS#8 format.
// Pass the returned value to Sign functions for token creation.
//
// Returns an error if the file cannot be read or the key cannot be parsed.
//
// Example:
//
// privateKey, err := jwt.LoadPrivateKeyRSA("rsa_private_key.pem")
// if err != nil {
// log.Fatal(err)
// }
// token, err := jwt.Sign(jwt.RS256, privateKey, claims)
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 loads and parses a PEM-encoded RSA public key from a file.
//
// The file should contain a PEM-encoded RSA public key in PKIX format,
// or a certificate containing an RSA public key.
// Pass the returned value to Verify functions for token validation.
//
// Returns an error if the file cannot be read or the key cannot be parsed.
//
// Example:
//
// publicKey, err := jwt.LoadPublicKeyRSA("rsa_public_key.pem")
// if err != nil {
// log.Fatal(err)
// }
// verifiedToken, err := jwt.Verify(jwt.RS256, publicKey, token)
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 PEM-encoded RSA private key bytes.
//
// The input should be PEM-encoded RSA private key data in PKCS#1 or PKCS#8 format.
// This function handles both formats automatically.
//
// Returns an error if the PEM block is missing or the key cannot be parsed.
// Use LoadPrivateKeyRSA for file-based loading.
//
// Example:
//
// keyData := []byte("-----BEGIN RSA PRIVATE KEY-----\n...")
// privateKey, err := jwt.ParsePrivateKeyRSA(keyData)
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 PEM-encoded RSA public key bytes.
//
// The input should be PEM-encoded RSA public key data in PKIX format,
// or a certificate containing an RSA public key.
// This function handles both formats automatically.
//
// Returns an error if the PEM block is missing or the key cannot be parsed.
// Use LoadPublicKeyRSA for file-based loading.
//
// Example:
//
// keyData := []byte("-----BEGIN PUBLIC KEY-----\n...")
// publicKey, err := jwt.ParsePublicKeyRSA(keyData)
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
}