|
|
|
@ -1,11 +1,13 @@
|
|
|
|
|
package cert
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"b612.me/starcrypto"
|
|
|
|
|
"b612.me/starlog"
|
|
|
|
|
"crypto/dsa"
|
|
|
|
|
"crypto/ecdh"
|
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
"crypto/ed25519"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/rsa"
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"encoding/pem"
|
|
|
|
@ -294,7 +296,59 @@ func ParseCert(data []byte, pwd string) {
|
|
|
|
|
default:
|
|
|
|
|
starlog.Green("未知公钥类型\n")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
continue
|
|
|
|
|
case "RSA PRIVATE KEY":
|
|
|
|
|
n, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析私钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
starlog.Infof("这是一个RSA私钥文件\n")
|
|
|
|
|
starlog.Green("私钥位数:%d\n", n.Size())
|
|
|
|
|
starlog.Green("私钥长度:%d\n", n.N.BitLen())
|
|
|
|
|
starlog.Green("私钥指数:%d\n", n.E)
|
|
|
|
|
starlog.Green("私钥系数:%d\n", n.D)
|
|
|
|
|
starlog.Green("私钥质数p:%d\n", n.Primes[0])
|
|
|
|
|
starlog.Green("私钥质数q:%d\n", n.Primes[1])
|
|
|
|
|
starlog.Green("私钥系数dP:%d\n", n.Precomputed.Dp)
|
|
|
|
|
starlog.Green("私钥系数dQ:%d\n", n.Precomputed.Dq)
|
|
|
|
|
starlog.Green("私钥系数qInv:%d\n", n.Precomputed.Qinv)
|
|
|
|
|
case "RSA PUBLIC KEY":
|
|
|
|
|
n, err := x509.ParsePKCS1PublicKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析公钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
starlog.Green("这是一个RSA公钥\n")
|
|
|
|
|
starlog.Green("公钥位数:%d\n", n.Size())
|
|
|
|
|
starlog.Green("公钥长度:%d\n", n.N.BitLen())
|
|
|
|
|
starlog.Green("公钥指数:%d\n", n.E)
|
|
|
|
|
case "EC PRIVATE KEY":
|
|
|
|
|
n, err := x509.ParseECPrivateKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析私钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
starlog.Green("这是一个ECDSA私钥\n")
|
|
|
|
|
starlog.Green("私钥位数:%d\n", n.Curve.Params().BitSize)
|
|
|
|
|
starlog.Green("私钥曲线:%s\n", n.Curve.Params().Name)
|
|
|
|
|
starlog.Green("私钥长度:%d\n", n.Params().BitSize)
|
|
|
|
|
starlog.Green("私钥系数:%d\n", n.D)
|
|
|
|
|
starlog.Green("私钥公钥X:%d\n", n.PublicKey.X)
|
|
|
|
|
starlog.Green("私钥公钥Y:%d\n", n.PublicKey.Y)
|
|
|
|
|
case "EC PUBLIC KEY":
|
|
|
|
|
p, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析公钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
n := p.(*ecdsa.PublicKey)
|
|
|
|
|
starlog.Green("这是一个ECDSA公钥\n")
|
|
|
|
|
starlog.Green("公钥位数:%d\n", n.Curve.Params().BitSize)
|
|
|
|
|
starlog.Green("公钥曲线:%s\n", n.Curve.Params().Name)
|
|
|
|
|
starlog.Green("公钥长度:%d\n", n.Params().BitSize)
|
|
|
|
|
starlog.Green("公钥公钥X:%d\n", n.X)
|
|
|
|
|
starlog.Green("公钥公钥Y:%d\n", n.Y)
|
|
|
|
|
default:
|
|
|
|
|
starlog.Infof("未知证书文件类型\n")
|
|
|
|
|
}
|
|
|
|
@ -556,6 +610,62 @@ func GetCert(data []byte, pwd string) ([]any, []x509.Certificate, error) {
|
|
|
|
|
starlog.Green("未知公钥类型\n")
|
|
|
|
|
}
|
|
|
|
|
return common, certs, nil
|
|
|
|
|
case "RSA PRIVATE KEY":
|
|
|
|
|
n, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析私钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
starlog.Infof("这是一个RSA私钥文件\n")
|
|
|
|
|
starlog.Green("私钥位数:%d\n", n.Size())
|
|
|
|
|
starlog.Green("私钥长度:%d\n", n.N.BitLen())
|
|
|
|
|
starlog.Green("私钥指数:%d\n", n.E)
|
|
|
|
|
starlog.Green("私钥系数:%d\n", n.D)
|
|
|
|
|
starlog.Green("私钥质数p:%d\n", n.Primes[0])
|
|
|
|
|
starlog.Green("私钥质数q:%d\n", n.Primes[1])
|
|
|
|
|
starlog.Green("私钥系数dP:%d\n", n.Precomputed.Dp)
|
|
|
|
|
starlog.Green("私钥系数dQ:%d\n", n.Precomputed.Dq)
|
|
|
|
|
starlog.Green("私钥系数qInv:%d\n", n.Precomputed.Qinv)
|
|
|
|
|
common = append(common, n)
|
|
|
|
|
case "RSA PUBLIC KEY":
|
|
|
|
|
n, err := x509.ParsePKCS1PublicKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析公钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
starlog.Green("这是一个RSA公钥\n")
|
|
|
|
|
starlog.Green("公钥位数:%d\n", n.Size())
|
|
|
|
|
starlog.Green("公钥长度:%d\n", n.N.BitLen())
|
|
|
|
|
starlog.Green("公钥指数:%d\n", n.E)
|
|
|
|
|
common = append(common, n)
|
|
|
|
|
case "EC PRIVATE KEY":
|
|
|
|
|
n, err := x509.ParseECPrivateKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析私钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
starlog.Green("这是一个ECDSA私钥\n")
|
|
|
|
|
starlog.Green("私钥位数:%d\n", n.Curve.Params().BitSize)
|
|
|
|
|
starlog.Green("私钥曲线:%s\n", n.Curve.Params().Name)
|
|
|
|
|
starlog.Green("私钥长度:%d\n", n.Params().BitSize)
|
|
|
|
|
starlog.Green("私钥系数:%d\n", n.D)
|
|
|
|
|
starlog.Green("私钥公钥X:%d\n", n.PublicKey.X)
|
|
|
|
|
starlog.Green("私钥公钥Y:%d\n", n.PublicKey.Y)
|
|
|
|
|
common = append(common, n)
|
|
|
|
|
case "EC PUBLIC KEY":
|
|
|
|
|
p, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("解析公钥错误:%s\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
n := p.(*ecdsa.PublicKey)
|
|
|
|
|
starlog.Green("这是一个ECDSA公钥\n")
|
|
|
|
|
starlog.Green("公钥位数:%d\n", n.Curve.Params().BitSize)
|
|
|
|
|
starlog.Green("公钥曲线:%s\n", n.Curve.Params().Name)
|
|
|
|
|
starlog.Green("公钥长度:%d\n", n.Params().BitSize)
|
|
|
|
|
starlog.Green("公钥公钥X:%d\n", n.X)
|
|
|
|
|
starlog.Green("公钥公钥Y:%d\n", n.Y)
|
|
|
|
|
common = append(common, n)
|
|
|
|
|
default:
|
|
|
|
|
starlog.Infof("未知证书文件类型\n")
|
|
|
|
|
}
|
|
|
|
@ -604,7 +714,7 @@ func Pkcs1(data []byte, pwd string, originName string, outpath string) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, v := range keys {
|
|
|
|
|
for idx, v := range keys {
|
|
|
|
|
if v == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
@ -614,23 +724,120 @@ func Pkcs1(data []byte, pwd string, originName string, outpath string) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(outpath+"/"+originName+".pkcs1", pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: data}), 0644)
|
|
|
|
|
err = os.WriteFile(fmt.Sprintf("%s/%s_%v.pkcs1", outpath, originName, idx), pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: data}), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
starlog.Green("已将私钥保存到%s\n", outpath+"/"+originName+".pkcs8")
|
|
|
|
|
starlog.Green("已将私钥保存到%s\n", fmt.Sprintf("%s/%s_%v.pkcs1", outpath, originName, idx))
|
|
|
|
|
}
|
|
|
|
|
case *rsa.PublicKey:
|
|
|
|
|
data = x509.MarshalPKCS1PublicKey(n)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(outpath+"/"+originName+".pub.pkcs1", pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: data}), 0644)
|
|
|
|
|
err = os.WriteFile(fmt.Sprintf("%s/%s_%v.pub.pkcs1", outpath, originName, idx), pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: data}), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
starlog.Green("已将公钥保存到%s\n", fmt.Sprintf("%s/%s_%v.pub.pkcs1", outpath, originName, idx))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Pkcs12(keys []any, certs []x509.Certificate, enPwd string, originName string, outpath string) error {
|
|
|
|
|
var priv any
|
|
|
|
|
for _, v := range keys {
|
|
|
|
|
if v == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
switch n := v.(type) {
|
|
|
|
|
case *ecdsa.PrivateKey, *rsa.PrivateKey, *dsa.PrivateKey, *ed25519.PrivateKey, *ecdh.PrivateKey:
|
|
|
|
|
priv = n
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if priv == nil {
|
|
|
|
|
return errors.New("未找到私钥")
|
|
|
|
|
}
|
|
|
|
|
if len(certs) == 0 {
|
|
|
|
|
return errors.New("未找到证书")
|
|
|
|
|
}
|
|
|
|
|
mainCert := certs[0]
|
|
|
|
|
var certChain []*x509.Certificate
|
|
|
|
|
for _, v := range certs[1:] {
|
|
|
|
|
certChain = append(certChain, &v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pfxData, err := pkcs12.Encode(rand.Reader, priv, &mainCert, certChain, enPwd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(outpath+"/"+originName+".pfx", pfxData, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
starlog.Errorf("保存失败:%s\n", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
starlog.Green("已将PKCS12文件保存到%s\n", outpath+"/"+originName+".pfx")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Tran(data []byte, pwd string, originName string, outpath string) error {
|
|
|
|
|
keys, _, err := GetCert(data, pwd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for idx, v := range keys {
|
|
|
|
|
if v == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
switch n := v.(type) {
|
|
|
|
|
case *ecdsa.PrivateKey, *rsa.PrivateKey:
|
|
|
|
|
data, err = starcrypto.EncodePrivateKey(n, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(fmt.Sprintf("%s/%s_%v.tran.key", outpath, originName, idx), data, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
starlog.Green("已将公钥保存到%s\n", outpath+"/"+originName+".pub.pkcs1")
|
|
|
|
|
starlog.Green("已将私钥保存到%s\n", fmt.Sprintf("%s/%s_%v.tran.key", outpath, originName, idx))
|
|
|
|
|
}
|
|
|
|
|
case *ecdsa.PublicKey, *rsa.PublicKey:
|
|
|
|
|
data, err = starcrypto.EncodePublicKey(n)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(fmt.Sprintf("%s/%s_%v.tran.pub", outpath, originName, idx), data, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
starlog.Green("已将公钥保存到%s\n", fmt.Sprintf("%s/%s_%v.tran.pub", outpath, originName, idx))
|
|
|
|
|
}
|
|
|
|
|
case *dsa.PrivateKey, *ed25519.PrivateKey, *ecdh.PrivateKey:
|
|
|
|
|
data, err = x509.MarshalPKCS8PrivateKey(n)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(outpath+"/"+originName+".tran.key", pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: data}), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
starlog.Green("已将私钥保存到%s\n", outpath+"/"+originName+".tran.key")
|
|
|
|
|
}
|
|
|
|
|
case *dsa.PublicKey, *ed25519.PublicKey, *ecdh.PublicKey:
|
|
|
|
|
data, err = x509.MarshalPKIXPublicKey(n)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(outpath+"/"+originName+".tran.pub", pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: data}), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
starlog.Green("已将公钥保存到%s\n", outpath+"/"+originName+".tran.pub")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|