-
Notifications
You must be signed in to change notification settings - Fork 22
/
from.go
200 lines (157 loc) · 4.44 KB
/
from.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
package ca
import (
"errors"
"crypto/rsa"
"crypto/rand"
"crypto/x509"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"github.com/deatil/go-cryptobin/gm/sm2"
cryptobin_pkcs12 "github.com/deatil/go-cryptobin/pkcs12"
)
// 证书
func (this CA) FromCert(cert *x509.Certificate) CA {
this.cert = cert
return this
}
// 解析证书导入
func (this CA) FromCertificateDer(der []byte) CA {
cert, err := x509.ParseCertificate(der)
if err != nil {
return this.AppendError(err)
}
this.cert = cert
return this
}
// 证书请求
func (this CA) FromCertRequest(cert *x509.CertificateRequest) CA {
this.certRequest = cert
return this
}
// 解析证书导入
func (this CA) FromCertificateRequestDer(asn1Data []byte) CA {
certRequest, err := x509.ParseCertificateRequest(asn1Data)
if err != nil {
return this.AppendError(err)
}
this.certRequest = certRequest
return this
}
// 私钥
// 可用 [*rsa.PrivateKey | *ecdsa.PrivateKey | ed25519.PrivateKey | *sm2.PrivateKey]
func (this CA) FromPrivateKey(key any) CA {
this.privateKey = key
return this
}
// 公钥
// 可用 [*rsa.PublicKey | *ecdsa.PublicKey | ed25519.PublicKey | *sm2.PublicKey]
func (this CA) FromPublicKey(key any) CA {
this.publicKey = key
return this
}
// =======================
// pkcs12
func (this CA) FromPKCS12Cert(pfxData []byte, password string) CA {
privateKey, cert, err := cryptobin_pkcs12.Decode(pfxData, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.cert = cert
return this
}
// pkcs12
func (this CA) FromSM2PKCS12Cert(pfxData []byte, password string) CA {
pv, cert, err := cryptobin_pkcs12.Decode(pfxData, password)
if err != nil {
return this.AppendError(err)
}
switch k := pv.(type) {
case *ecdsa.PrivateKey:
switch k.Curve {
case sm2.P256():
sm2pub := &sm2.PublicKey{
Curve: k.Curve,
X: k.X,
Y: k.Y,
}
sm2Pri := &sm2.PrivateKey{
PublicKey: *sm2pub,
D: k.D,
}
if !k.IsOnCurve(k.X, k.Y) {
err := errors.New("error while validating SM2 private key: %v")
return this.AppendError(err)
}
this.privateKey = sm2Pri
this.cert = cert
return this
default:
// other
}
default:
// other
}
err = errors.New("unexpected type for p12 private key")
return this.AppendError(err)
}
// =======================
// 生成密钥 RSA
// 可选 [512 | 1024 | 2048 | 4096]
func (this CA) GenerateRSAKey(bits int) CA {
// 生成私钥
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = &privateKey.PublicKey
return this
}
// 生成密钥 Ecdsa
// 可选 [P521 | P384 | P256 | P224]
func (this CA) GenerateECDSAKey(curve string) CA {
var useCurve elliptic.Curve
switch {
case curve == "P521":
useCurve = elliptic.P521()
case curve == "P384":
useCurve = elliptic.P384()
case curve == "P256":
useCurve = elliptic.P256()
case curve == "P224":
useCurve = elliptic.P224()
default:
useCurve = elliptic.P256()
}
// 生成私钥
privateKey, err := ecdsa.GenerateKey(useCurve, rand.Reader)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = &privateKey.PublicKey
return this
}
// 生成密钥 EdDSA
func (this CA) GenerateEdDSAKey() CA {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = publicKey
return this
}
// 生成密钥 SM2
func (this CA) GenerateSM2Key() CA {
// 生成私钥
privateKey, err := sm2.GenerateKey(rand.Reader)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = &privateKey.PublicKey
return this
}