-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
228 lines (193 loc) · 4.13 KB
/
keys.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
package afssl
import (
"crypto/ecdh"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/aacfactory/afssl/gmsm/smx509"
)
type KeyName string
type KeyType interface {
Name() (name KeyName)
}
const unknownKeyTypeName = KeyName("UNKNOWN")
type unknownKeyType struct {
}
func (r *unknownKeyType) Name() (name KeyName) {
name = unknownKeyTypeName
return
}
func RSA() KeyType {
return RSAWithBits(4096)
}
func RSAWithBits(bits int) KeyType {
return &rsaKeyType{
keyBits: bits,
}
}
const rsaKeyTypeName = KeyName("RSA")
type rsaKeyType struct {
keyBits int
}
func (r *rsaKeyType) Name() (name KeyName) {
name = rsaKeyTypeName
return
}
func ECDSA() KeyType {
return ECDSAWithCurve(elliptic.P256())
}
func ECDSAWithCurve(curve elliptic.Curve) KeyType {
return &ecdsaKeyType{
curve: curve,
}
}
const ecdsaKeyTypeName = KeyName("ECDSA")
type ecdsaKeyType struct {
curve elliptic.Curve
}
func (r *ecdsaKeyType) Name() (name KeyName) {
name = ecdsaKeyTypeName
return
}
func ED25519() KeyType {
return ED25519WithSeed(nil)
}
func ED25519WithSeed(seed []byte) KeyType {
return &ed25519KeyType{
seed: seed,
}
}
const ed25519KeyTypeName = KeyName("ED25519")
type ed25519KeyType struct {
seed []byte
}
func (r *ed25519KeyType) Name() (name KeyName) {
name = ed25519KeyTypeName
return
}
func X25519() KeyType {
return &x25519KeyType{}
}
const x25519KeyTypeName = KeyName("X25519")
type x25519KeyType struct {
seed []byte
}
func (r *x25519KeyType) Name() (name KeyName) {
name = x25519KeyTypeName
return
}
func SM2() KeyType {
return &sm2KeyType{}
}
const sm2KeyTypeName = KeyName("SM2")
type sm2KeyType struct {
}
func (r *sm2KeyType) Name() (name KeyName) {
name = sm2KeyTypeName
return
}
func EncryptMasterSM9() KeyType {
return &sm9KeyType{
kind: "encrypt",
}
}
func SignMasterSM9() KeyType {
return &sm9KeyType{
kind: "sign",
}
}
const sm9KeyTypeName = KeyName("SM9")
type sm9KeyType struct {
kind string
}
func (r *sm9KeyType) Name() (name KeyName) {
name = sm9KeyTypeName
return
}
func ParsePrivateKey(keyPEM []byte) (key any, keyType KeyType, err error) {
keyBlock, _ := pem.Decode(keyPEM)
keyBlockType := keyBlock.Type
if keyBlockType == "" {
err = fmt.Errorf("afssl: invalid private key block type")
return
}
// PKCS1
if keyBlock.Type == "RSA PRIVATE KEY" {
rsaKey, parseKeyErr := x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
if parseKeyErr != nil {
err = fmt.Errorf("afssl: parse pkcs1 rsa private key failed, %v", parseKeyErr)
return
}
key = rsaKey
keyType = RSAWithBits(rsaKey.N.BitLen())
return
}
// GM
gmKeyType, _ := smx509.GetGMPrivateKeyType(keyBlock.Bytes)
// EC
if keyBlock.Type != "PRIVATE KEY" {
if gmKeyType == smx509.SM2Key {
sm2Key, parseSM2KeyErr := smx509.ParseSM2PrivateKey(keyBlock.Bytes)
if parseSM2KeyErr != nil {
err = fmt.Errorf("afssl: parse sm2 private key failed, %v", parseSM2KeyErr)
return
}
key = sm2Key
keyType = SM2()
return
} else {
ecKey, parseErr := x509.ParseECPrivateKey(keyBlock.Bytes)
if parseErr != nil {
err = fmt.Errorf("afssl: parse ec private key failed, %v", parseErr)
return
}
key = ecKey
keyType = ECDSAWithCurve(ecKey.Curve)
return
}
}
// PKCS8
// SM2
if gmKeyType == smx509.SM2Key {
sm2Key, parseSM2KeyErr := smx509.ParsePKCS8PrivateKey(keyBlock.Bytes)
if parseSM2KeyErr != nil {
err = fmt.Errorf("afssl: parse sm2 private key failed, %v", parseSM2KeyErr)
return
}
key = sm2Key
keyType = SM2()
return
}
// standards
k, parseErr := x509.ParsePKCS8PrivateKey(keyBlock.Bytes)
if parseErr != nil {
err = fmt.Errorf("afssl: parse PKCS8 private key failed, %v", parseErr)
return
}
switch kk := k.(type) {
case *rsa.PrivateKey:
key = kk
keyType = RSAWithBits(kk.N.BitLen())
break
case *ecdsa.PrivateKey:
key = kk
keyType = ECDSAWithCurve(kk.Curve)
break
case *ed25519.PrivateKey:
key = kk
keyType = ED25519WithSeed(kk.Seed())
break
case *ecdh.PrivateKey:
key = kk
keyType = X25519()
break
default:
err = fmt.Errorf("afssl: parse PKCS8 private key failed, unknown key type")
return
}
return
}