/
rsa_key.go
296 lines (229 loc) · 6.86 KB
/
rsa_key.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package cmk
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)
type RsaPrivateKey rsa.PrivateKey
type RsaKey struct {
BaseKey
PrivateKey RsaPrivateKey
}
func NewRsaKey(spec KeySpec, usage KeyUsage, metadata KeyMetadata, policy string) (*RsaKey, error) {
var bits int
switch spec {
case SpecRsa2048:
bits = 2048
case SpecRsa3072:
bits = 3072
case SpecRsa4096:
bits = 4096
default:
return nil, errors.New("key spec error")
}
//---
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
k := &RsaKey{
PrivateKey: RsaPrivateKey(*privateKey),
}
k.Type = TypeRsa
k.Metadata = metadata
k.Policy = policy
k.Metadata.KeyUsage = usage
k.Metadata.KeySpec = spec
k.Metadata.CustomerMasterKeySpec = spec
switch usage {
case UsageSignVerify:
k.Metadata.SigningAlgorithms = []SigningAlgorithm{
SigningAlgorithmRsaPssSha256,
SigningAlgorithmRsaPssSha384,
SigningAlgorithmRsaPssSha512,
SigningAlgorithmRsaPkcsSha256,
SigningAlgorithmRsaPkcsSha384,
SigningAlgorithmRsaPkcsSha512,
}
case UsageEncryptDecrypt:
k.Metadata.EncryptionAlgorithms = []EncryptionAlgorithm{
EncryptionAlgorithmRsaOaepSha1,
EncryptionAlgorithmRsaOaepSha256,
}
default:
return nil, errors.New("key usage error")
}
return k, nil
}
//----------------------------------------------------
func (k *RsaKey) GetArn() string {
return k.GetMetadata().Arn
}
func (k *RsaKey) GetPolicy() string {
return k.Policy
}
func (k *RsaKey) GetKeyType() KeyType {
return k.Type
}
func (k *RsaKey) GetMetadata() *KeyMetadata {
return &k.Metadata
}
//----------------------------------------------------
func (k *RsaKey) Sign(digest []byte, algorithm SigningAlgorithm) ([]byte, error) {
//--------------------------
// Check the requested Signing Algorithm is supported by this key
validSigningAlgorithm := false
for _, a := range k.Metadata.SigningAlgorithms {
if a == algorithm {
validSigningAlgorithm = true
break
}
}
if !validSigningAlgorithm {
return []byte{}, &InvalidSigningAlgorithm{}
}
//--------------------------
var hash crypto.Hash
switch algorithm {
case SigningAlgorithmRsaPssSha256, SigningAlgorithmRsaPkcsSha256:
if len(digest) != (256 / 8) {
return []byte{}, &InvalidDigestLength{}
}
hash = crypto.SHA256
case SigningAlgorithmRsaPssSha384, SigningAlgorithmRsaPkcsSha384:
if len(digest) != (384 / 8) {
return []byte{}, &InvalidDigestLength{}
}
hash = crypto.SHA384
case SigningAlgorithmRsaPssSha512, SigningAlgorithmRsaPkcsSha512:
if len(digest) != (512 / 8) {
return []byte{}, &InvalidDigestLength{}
}
hash = crypto.SHA512
default:
return []byte{}, errors.New("unknown signing algorithm")
}
//---
key := rsa.PrivateKey(k.PrivateKey)
switch algorithm {
case SigningAlgorithmRsaPssSha256, SigningAlgorithmRsaPssSha384, SigningAlgorithmRsaPssSha512:
return rsa.SignPSS(rand.Reader, &key, hash, digest, nil)
case SigningAlgorithmRsaPkcsSha256, SigningAlgorithmRsaPkcsSha384, SigningAlgorithmRsaPkcsSha512:
return rsa.SignPKCS1v15(rand.Reader, &key, hash, digest)
default:
return []byte{}, errors.New("unknown signing algorithm")
}
}
func (k *RsaKey) HashAndSign(message []byte, algorithm SigningAlgorithm) ([]byte, error) {
digest, err := hashMessage(message, algorithm)
if err != nil {
return []byte{}, err
}
return k.Sign(digest, algorithm)
}
//----------------------------------------------------
func (k *RsaKey) Verify(signature []byte, digest []byte, algorithm SigningAlgorithm) (bool, error) {
var hash crypto.Hash
switch algorithm {
case SigningAlgorithmRsaPssSha256, SigningAlgorithmRsaPkcsSha256:
hash = crypto.SHA256
case SigningAlgorithmRsaPssSha384, SigningAlgorithmRsaPkcsSha384:
hash = crypto.SHA384
case SigningAlgorithmRsaPssSha512, SigningAlgorithmRsaPkcsSha512:
hash = crypto.SHA512
default:
return false, errors.New("unknown signing algorithm")
}
//---
key := rsa.PrivateKey(k.PrivateKey)
switch algorithm {
case SigningAlgorithmRsaPssSha256, SigningAlgorithmRsaPssSha384, SigningAlgorithmRsaPssSha512:
if err := rsa.VerifyPSS(&key.PublicKey, hash, digest, signature, nil); err != nil {
return false, nil
}
case SigningAlgorithmRsaPkcsSha256, SigningAlgorithmRsaPkcsSha384, SigningAlgorithmRsaPkcsSha512:
if err := rsa.VerifyPKCS1v15(&key.PublicKey, hash, digest, signature); err != nil {
return false, nil
}
default:
return false, errors.New("unknown signing algorithm")
}
return true, nil
}
func (k *RsaKey) HashAndVerify(signature []byte, message []byte, algorithm SigningAlgorithm) (bool, error) {
digest, err := hashMessage(message, algorithm)
if err != nil {
return false, err
}
return k.Verify(signature, digest, algorithm)
}
//----------------------------------------------------
// Construct key from YAML (seeding)
//---
func (k *RsaKey) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Cannot use embedded 'Key' struct
// https://github.com/go-yaml/yaml/issues/263
type YamlKey struct {
Metadata KeyMetadata `yaml:"Metadata"`
PrivateKeyPem string `yaml:"PrivateKeyPem"`
}
yk := YamlKey{}
if err := unmarshal(&yk); err != nil {
return &UnmarshalYAMLError{err.Error()}
}
k.Type = TypeRsa
k.Metadata = yk.Metadata
defaultSeededKeyMetadata(&k.Metadata)
pemDecoded, _ := pem.Decode([]byte(yk.PrivateKeyPem))
if pemDecoded == nil {
return &UnmarshalYAMLError{fmt.Sprintf("Unable to decode pem of key %s check the YAML.\n", k.Metadata.KeyId)}
}
parseResult, pkcsParseError := x509.ParsePKCS8PrivateKey(pemDecoded.Bytes)
if pkcsParseError != nil {
return &UnmarshalYAMLError{fmt.Sprintf("Unable to decode pem of key %s, Ensure it is in PKCS8 format with no password: %s.\n", k.Metadata.KeyId, pkcsParseError)}
}
key := parseResult.(*rsa.PrivateKey)
k.PrivateKey = RsaPrivateKey(*key)
bitLen := key.N.BitLen()
switch bitLen {
case 2048:
k.Metadata.KeySpec = SpecRsa2048
case 3072:
k.Metadata.KeySpec = SpecRsa3072
case 4096:
k.Metadata.KeySpec = SpecRsa4096
default:
return &UnmarshalYAMLError{
fmt.Sprintf(
"RSA Keysize must be one of (2048,3072,4096) bits. %d bits found for key %s.\n",
bitLen, k.Metadata.KeyId),
}
}
k.Metadata.CustomerMasterKeySpec = k.Metadata.KeySpec
switch k.Metadata.KeyUsage {
case UsageSignVerify:
k.Metadata.SigningAlgorithms = []SigningAlgorithm{
SigningAlgorithmRsaPssSha256,
SigningAlgorithmRsaPssSha384,
SigningAlgorithmRsaPssSha512,
SigningAlgorithmRsaPkcsSha256,
SigningAlgorithmRsaPkcsSha384,
SigningAlgorithmRsaPkcsSha512,
}
case UsageEncryptDecrypt:
k.Metadata.EncryptionAlgorithms = []EncryptionAlgorithm{
EncryptionAlgorithmRsaOaepSha1,
EncryptionAlgorithmRsaOaepSha256,
}
default:
return &UnmarshalYAMLError{
fmt.Sprintf(
"KeyUsage must be one of (%s,%s). It is mandatory for RSA keys.\n", UsageEncryptDecrypt, UsageSignVerify),
}
}
return nil
}