Skip to content

Instantly share code, notes, and snippets.

@vanbroup
Created June 23, 2021 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanbroup/edf1e21bb2b52919cc7b4eb712a70b43 to your computer and use it in GitHub Desktop.
Save vanbroup/edf1e21bb2b52919cc7b4eb712a70b43 to your computer and use it in GitHub Desktop.
Convert PEM certificates to PKCS7 bundle (.p7b)
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