Last active
December 5, 2024 18:02
-
-
Save SoMuchForSubtlety/f0d35cdbe5e0ecbcd26b87a88a790f46 to your computer and use it in GitHub Desktop.
P12/PKCS#12 to tls.Certificate in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package certutil | |
| import ( | |
| "crypto/tls" | |
| "fmt" | |
| "software.sslmate.com/src/go-pkcs12" | |
| ) | |
| func PKCS12ToCertChain(pfxData []byte, password string) (tls.Certificate, error) { | |
| privateKey, leafCert, caCerts, err := pkcs12.DecodeChain(pfxData, password) | |
| if err != nil { | |
| return tls.Certificate{}, fmt.Errorf("Failed to decode chain: %w", err) | |
| } | |
| certBytes := [][]byte{leafCert.Raw} | |
| for _, ca := range caCerts { | |
| certBytes = append(certBytes, ca.Raw) | |
| } | |
| return tls.Certificate{ | |
| Certificate: certBytes, | |
| PrivateKey: privateKey, | |
| }, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment