From 8ed9ad168a717802525e51d6f349ff3877594373 Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Fri, 25 Mar 2022 20:10:36 +0800 Subject: [PATCH] fix #14: use ParsePKCS1PrivateKey --- cert/cert.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cert/cert.go b/cert/cert.go index 731a9cc..ef522ad 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -15,6 +15,7 @@ import ( "net" "os" "path/filepath" + "strings" "sync" "time" @@ -148,15 +149,26 @@ func (ca *CA) load() error { return fmt.Errorf("%v 中不存在 CERTIFICATE", caFile) } + var privateKey *rsa.PrivateKey key, err := x509.ParsePKCS8PrivateKey(keyDERBlock.Bytes) if err != nil { - return err - } - if v, ok := key.(*rsa.PrivateKey); ok { - ca.PrivateKey = *v + // fix #14 + if strings.Contains(err.Error(), "use ParsePKCS1PrivateKey instead") { + privateKey, err = x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes) + if err != nil { + return err + } + } else { + return err + } } else { - return errors.New("found unknown rsa private key type in PKCS#8 wrapping") + if v, ok := key.(*rsa.PrivateKey); ok { + privateKey = v + } else { + return errors.New("found unknown rsa private key type in PKCS#8 wrapping") + } } + ca.PrivateKey = *privateKey x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes) if err != nil {