fix: 解决创建机构选择 RSA 加密失败的问题 (#3234)

This commit is contained in:
zhengkunwang 2023-12-08 17:58:09 +08:00 committed by GitHub
parent 3f8abf8ad8
commit 0fe9a87ea0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -92,36 +92,17 @@ func (w WebsiteCAService) Create(create request.WebsiteCACreate) (*request.Websi
MaxPathLenZero: false,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
}
var (
caPEM = new(bytes.Buffer)
)
privateKey, err := certcrypto.GeneratePrivateKey(ssl.KeyType(create.KeyType))
interPrivateKey, interPublicKey, privateBytes, err := createPrivateKey(create.KeyType)
if err != nil {
return nil, err
}
var (
publicKey any
caPEM = new(bytes.Buffer)
caPrivateKeyPEM = new(bytes.Buffer)
privateBlock = &pem.Block{}
)
if ssl.KeyType(create.KeyType) == certcrypto.EC256 || ssl.KeyType(create.KeyType) == certcrypto.EC384 {
publicKey = &privateKey.(*ecdsa.PrivateKey).PublicKey
publicKey = publicKey.(*ecdsa.PublicKey)
privateBlock.Type = "EC PRIVATE KEY"
privateBytes, err := x509.MarshalECPrivateKey(privateKey.(*ecdsa.PrivateKey))
if err != nil {
return nil, err
}
privateBlock.Bytes = privateBytes
_ = pem.Encode(caPrivateKeyPEM, privateBlock)
} else {
publicKey = privateKey.(*rsa.PrivateKey).PublicKey
publicKey = publicKey.(*rsa.PublicKey)
privateBlock.Type = "RSA PRIVATE KEY"
privateBlock.Bytes = x509.MarshalPKCS1PrivateKey(privateKey.(*rsa.PrivateKey))
}
ca.PrivateKey = string(pem.EncodeToMemory(privateBlock))
ca.PrivateKey = string(privateBytes)
caBytes, err := x509.CreateCertificate(rand.Reader, rootCA, rootCA, publicKey, privateKey)
caBytes, err := x509.CreateCertificate(rand.Reader, rootCA, rootCA, interPublicKey, interPrivateKey)
if err != nil {
return nil, err
}