Created
June 23, 2021 09:45
-
-
Save vanbroup/edf1e21bb2b52919cc7b4eb712a70b43 to your computer and use it in GitHub Desktop.
Convert PEM certificates to PKCS7 bundle (.p7b)
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 main | |
import ( | |
"crypto/x509" | |
"encoding/pem" | |
"fmt" | |
"log" | |
"go.mozilla.org/pkcs7" | |
) | |
var pemCertificates = `` | |
func main() { | |
p7b, err := pkcs7.NewSignedData(nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for block, rest := pem.Decode([]byte(pemCertificates)); block != nil; block, rest = pem.Decode(rest) { | |
switch block.Type { | |
case "CERTIFICATE": | |
cert, err := x509.ParseCertificate(block.Bytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
p7b.AddCertificate(cert) | |
} | |
} | |
p7bBytes, err := p7b.Finish() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(pem.EncodeToMemory(&pem.Block{Type: "PKCS7", Bytes: p7bBytes}))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment