/
rsa_pkcs1.go
167 lines (141 loc) · 3.81 KB
/
rsa_pkcs1.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
/*
* Copyright 2012 Dave Grijalva
* Copyright 2016 Fabrício Godoy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pkcs1
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"hash"
"github.com/raiqub/jose/jwa"
jwarsa "github.com/raiqub/jose/jwa/rsa"
)
const (
// MinimumRSAKeySize defines the minimum key size for RSA keys as
// recommended by security experts.
MinimumRSAKeySize = 2048
)
type rsaPKCS1Alg struct {
hashAlg crypto.Hash
hashFunc func() hash.Hash
}
func init() {
jwa.RegisterAlgorithm(jwa.RS256, New256)
jwa.RegisterAlgorithm(jwa.RS384, New384)
jwa.RegisterAlgorithm(jwa.RS512, New512)
}
// New256 returns a new RS256 cryptographic algorithm.
func New256() jwa.Algorithm {
return &rsaPKCS1Alg{
crypto.SHA256,
func() hash.Hash { return sha256.New() },
}
}
// New384 returns a new RS384 cryptographic algorithm.
func New384() jwa.Algorithm {
return &rsaPKCS1Alg{
crypto.SHA384,
func() hash.Hash { return sha512.New384() },
}
}
// New512 returns a new RS512 cryptographic algorithm.
func New512() jwa.Algorithm {
return &rsaPKCS1Alg{
crypto.SHA512,
func() hash.Hash { return sha512.New() },
}
}
// Implements the Verify method from SigningMethod
// For this signing method, must be either a PEM encoded PKCS1 or PKCS8 RSA public key as
// []byte, or an rsa.PublicKey structure.
func (m *rsaPKCS1Alg) Verify(input, signature string, key interface{}) error {
var err error
// Decode PEM-encoded key
if pem, ok := key.([]byte); ok {
out, err := jwarsa.ParseFromPEM(pem)
if err != nil {
return err
}
key = out
}
// Verify the key is the right type
var rsaKey *rsa.PublicKey
switch k := key.(type) {
case *rsa.PublicKey:
rsaKey = k
case *rsa.PrivateKey:
rsaKey = k.Public().(*rsa.PublicKey)
default:
return jwa.ErrInvalidKey{Value: key}
}
// Decode the signature
decSig, err := base64.RawURLEncoding.DecodeString(signature)
if err != nil {
return err
}
// Create hasher
hasher := m.hashFunc()
hasher.Write([]byte(input))
// Verify the signature
return rsa.VerifyPKCS1v15(rsaKey, m.hashAlg, hasher.Sum(nil), decSig)
}
// Implements the Sign method from SigningMethod
// For this signing method, must be either a PEM encoded PKCS1 or PKCS8 RSA private key as
// []byte, or an rsa.PrivateKey structure.
func (m *rsaPKCS1Alg) Sign(input string, key interface{}) (string, error) {
var err error
// Decode PEM-encoded key
if pem, ok := key.([]byte); ok {
out, err := jwarsa.ParseFromPEM(pem)
if err != nil {
return "", err
}
key = out
}
var rsaKey *rsa.PrivateKey
switch k := key.(type) {
case *rsa.PrivateKey:
rsaKey = k
default:
return "", jwa.ErrInvalidKey{Value: key}
}
// Create hasher
hasher := m.hashFunc()
hasher.Write([]byte(input))
// Sign the string and return the encoded bytes
sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey,
m.hashAlg, hasher.Sum(nil))
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(sigBytes), nil
}
func (m *rsaPKCS1Alg) GenerateKey(bits int) (interface{}, error) {
if bits < MinimumRSAKeySize {
return nil, jwa.ErrTooSmallKeySize{
Minimum: MinimumRSAKeySize,
Actual: bits,
}
}
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, jwa.ErrorGeneratingKey(err.Error())
}
return key, nil
}