|
|
@@ -17,10 +17,12 @@ package template
|
|
|
import (
|
|
|
"bytes"
|
|
|
"crypto/x509"
|
|
|
+ "encoding/base64"
|
|
|
"encoding/pem"
|
|
|
"fmt"
|
|
|
|
|
|
"golang.org/x/crypto/pkcs12"
|
|
|
+ gopkcs12 "software.sslmate.com/src/go-pkcs12"
|
|
|
)
|
|
|
|
|
|
func pkcs12keyPass(pass, input string) (string, error) {
|
|
|
@@ -108,3 +110,29 @@ func pkcs12certPass(pass, input string) (string, error) {
|
|
|
func pkcs12cert(input string) (string, error) {
|
|
|
return pkcs12certPass("", input)
|
|
|
}
|
|
|
+
|
|
|
+func pemToPkcs12(cert, key string) (string, error) {
|
|
|
+ return pemToPkcs12Pass(cert, key, "")
|
|
|
+}
|
|
|
+
|
|
|
+func pemToPkcs12Pass(cert, key, pass string) (string, error) {
|
|
|
+ certPem, _ := pem.Decode([]byte(cert))
|
|
|
+ keyPem, _ := pem.Decode([]byte(key))
|
|
|
+
|
|
|
+ parsedCert, err := x509.ParseCertificate(certPem.Bytes)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ parsedKey, err := parsePrivateKey(keyPem.Bytes)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ pfx, err := gopkcs12.Modern.Encode(parsedKey, parsedCert, nil, pass)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ return base64.StdEncoding.EncodeToString(pfx), nil
|
|
|
+}
|