-
Notifications
You must be signed in to change notification settings - Fork 0
/
CA_map.go
349 lines (308 loc) · 9.31 KB
/
CA_map.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package kakaxi
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"sync"
"time"
)
// CAMap 存储签名算法对应的证书密钥对
var CAMap = map[x509.SignatureAlgorithm]CertKeyPair{}
// DomainCertCache 存储域名证书缓存
var DomainCertCache struct {
sync.RWMutex
certs map[string]DomainCert
}
// DomainCert 域名证书结构
type DomainCert struct {
Cert *x509.Certificate
CertPEM []byte
ExpiresAt time.Time
}
func init() {
CAMap = generateAllCertKeyPairs()
DomainCertCache.certs = make(map[string]DomainCert)
}
type CertKeyPair struct {
Certificate *x509.Certificate
PrivateKey interface{}
PublicKey interface{} // 添加公钥字段
CertPEM []byte
KeyPEM []byte
PubKeyPEM []byte // 可选:PEM格式的公钥
}
func generateAllCertKeyPairs() map[x509.SignatureAlgorithm]CertKeyPair {
result := make(map[x509.SignatureAlgorithm]CertKeyPair)
algorithms := []struct {
sigAlg x509.SignatureAlgorithm
genKeyFunc func() (interface{}, interface{}, error)
desc string
}{
// 与之前相同的算法列表...
{x509.SHA1WithRSA, rsaKeyGen(2048), "SHA1WithRSA (legacy)"},
{x509.SHA256WithRSA, rsaKeyGen(2048), "SHA256WithRSA"},
{x509.SHA384WithRSA, rsaKeyGen(3072), "SHA384WithRSA"},
{x509.SHA512WithRSA, rsaKeyGen(4096), "SHA512WithRSA"},
{x509.ECDSAWithSHA1, ecdsaKeyGen(elliptic.P256()), "ECDSAWithSHA1 (legacy)"},
{x509.ECDSAWithSHA256, ecdsaKeyGen(elliptic.P256()), "ECDSAWithSHA256"},
{x509.ECDSAWithSHA384, ecdsaKeyGen(elliptic.P384()), "ECDSAWithSHA384"},
{x509.ECDSAWithSHA512, ecdsaKeyGen(elliptic.P521()), "ECDSAWithSHA512"},
{x509.SHA256WithRSAPSS, rsaKeyGen(2048), "SHA256WithRSAPSS"},
{x509.SHA384WithRSAPSS, rsaKeyGen(3072), "SHA384WithRSAPSS"},
{x509.SHA512WithRSAPSS, rsaKeyGen(4096), "SHA512WithRSAPSS"},
{x509.PureEd25519, ed25519KeyGen(), "PureEd25519"},
}
for _, alg := range algorithms {
privateKey, publicKey, err := alg.genKeyFunc()
if err != nil {
log.Printf("生成密钥对失败 %v: %v", alg.desc, err)
continue
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Test Org"},
CommonName: fmt.Sprintf("Test %v Cert", alg.desc),
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(1, 0, 0),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
SignatureAlgorithm: alg.sigAlg,
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, publicKey, privateKey)
if err != nil {
log.Printf("创建证书失败 %v: %v", alg.desc, err)
continue
}
cert, err := x509.ParseCertificate(derBytes)
if err != nil {
log.Printf("解析证书失败 %v: %v", alg.desc, err)
continue
}
certPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: derBytes,
})
// 生成密钥的PEM编码
var keyPEM, pubKeyPEM []byte
switch k := privateKey.(type) {
case *rsa.PrivateKey:
keyPEM = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k),
})
pubKeyPEM = pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(&k.PublicKey),
})
case *ecdsa.PrivateKey:
keyBytes, err := x509.MarshalECPrivateKey(k)
if err != nil {
log.Printf("编码ECDSA私钥失败 %v: %v", alg.desc, err)
continue
}
keyPEM = pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: keyBytes,
})
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&k.PublicKey)
if err != nil {
log.Printf("编码ECDSA公钥失败 %v: %v", alg.desc, err)
continue
}
pubKeyPEM = pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubKeyBytes,
})
case ed25519.PrivateKey:
keyBytes, err := x509.MarshalPKCS8PrivateKey(k)
if err != nil {
log.Printf("编码Ed25519私钥失败 %v: %v", alg.desc, err)
continue
}
keyPEM = pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: keyBytes,
})
pubKeyBytes, err := x509.MarshalPKIXPublicKey(k.Public())
if err != nil {
log.Printf("编码Ed25519公钥失败 %v: %v", alg.desc, err)
continue
}
pubKeyPEM = pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubKeyBytes,
})
}
result[alg.sigAlg] = CertKeyPair{
Certificate: cert,
PrivateKey: privateKey,
PublicKey: publicKey,
CertPEM: certPEM,
KeyPEM: keyPEM,
PubKeyPEM: pubKeyPEM,
}
}
return result
}
// 辅助函数:生成RSA密钥对
func rsaKeyGen(bits int) func() (interface{}, interface{}, error) {
return func() (interface{}, interface{}, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
return key, &key.PublicKey, nil
}
}
// 辅助函数:生成ECDSA密钥对
func ecdsaKeyGen(curve elliptic.Curve) func() (interface{}, interface{}, error) {
return func() (interface{}, interface{}, error) {
key, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
return nil, nil, err
}
return key, &key.PublicKey, nil
}
}
// 辅助函数:生成Ed25519密钥对
func ed25519KeyGen() func() (interface{}, interface{}, error) {
return func() (interface{}, interface{}, error) {
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}
return priv, pub, nil
}
}
// 辅助函数 - 根据签名算法获取对应的密钥对
func GetKeyPairForAlgorithm(sigAlg x509.SignatureAlgorithm) (*CertKeyPair, error) {
pair, exists := CAMap[sigAlg]
if !exists {
return nil, fmt.Errorf("不支持的签名算法: %v", sigAlg)
}
return &pair, nil
}
// 示例:如何使用该函数创建证书
func CreateCertificateWithAlgorithm(template *x509.Certificate, parent *x509.Certificate) (*x509.Certificate, error) {
// 获取对应算法的密钥对
keyPair, err := GetKeyPairForAlgorithm(template.SignatureAlgorithm)
if err != nil {
return nil, err
}
// 如果是自签名证书
if parent == nil {
parent = template
}
// 创建证书
derBytes, err := x509.CreateCertificate(rand.Reader, template, parent, keyPair.PublicKey, keyPair.PrivateKey)
if err != nil {
return nil, fmt.Errorf("创建证书失败: %v", err)
}
// 解析并返回证书
return x509.ParseCertificate(derBytes)
}
// GetOrCreateDomainCert 获取或创建域名证书
func GetOrCreateDomainCert(domain string, sigAlg x509.SignatureAlgorithm) (*DomainCert, error) {
// 先尝试从缓存读取
DomainCertCache.RLock()
if cert, exists := DomainCertCache.certs[domain]; exists {
DomainCertCache.RUnlock()
// 检查证书是否过期
if time.Now().Before(cert.ExpiresAt) {
return &cert, nil
}
} else {
DomainCertCache.RUnlock()
}
// 没有找到或已过期,需要创建新证书
DomainCertCache.Lock()
defer DomainCertCache.Unlock()
// 双重检查,避免并发创建
if cert, exists := DomainCertCache.certs[domain]; exists {
if time.Now().Before(cert.ExpiresAt) {
return &cert, nil
}
}
// 获取CA证书和密钥
keyPair, err := GetKeyPairForAlgorithm(sigAlg)
if err != nil {
return nil, err
}
// 创建域名证书模板
template := &x509.Certificate{
SerialNumber: big.NewInt(time.Now().UnixNano()),
Subject: pkix.Name{
CommonName: domain,
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(1, 0, 0), // 1年有效期
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
SignatureAlgorithm: sigAlg,
DNSNames: []string{domain},
}
// 使用CA证书签发域名证书
derBytes, err := x509.CreateCertificate(rand.Reader, template, keyPair.Certificate, keyPair.PublicKey, keyPair.PrivateKey)
if err != nil {
return nil, fmt.Errorf("创建域名证书失败: %v", err)
}
// 解析证书
cert, err := x509.ParseCertificate(derBytes)
if err != nil {
return nil, fmt.Errorf("解析域名证书失败: %v", err)
}
// 生成PEM格式证书
certPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: derBytes,
})
// 创建新的域名证书记录
domainCert := DomainCert{
Cert: cert,
CertPEM: certPEM,
ExpiresAt: template.NotAfter,
}
// 更新缓存
DomainCertCache.certs[domain] = domainCert
return &domainCert, nil
}
// CleanExpiredCerts 清理过期的域名证书
func CleanExpiredCerts() {
DomainCertCache.Lock()
defer DomainCertCache.Unlock()
now := time.Now()
for domain, cert := range DomainCertCache.certs {
if now.After(cert.ExpiresAt) {
delete(DomainCertCache.certs, domain)
}
}
}
// GetCachedDomainsCount 获取缓存的域名证书数量
func GetCachedDomainsCount() int {
DomainCertCache.RLock()
defer DomainCertCache.RUnlock()
return len(DomainCertCache.certs)
}
// GetCachedDomains 获取所有缓存的域名列表
func GetCachedDomains() []string {
DomainCertCache.RLock()
defer DomainCertCache.RUnlock()
domains := make([]string, 0, len(DomainCertCache.certs))
for domain := range DomainCertCache.certs {
domains = append(domains, domain)
}
return domains
}
// 其他原有代码保持不变...