import "github.com/ggpslop/go-pkcs12"
This package is forked from software.sslmate.com/src/go-pkcs12, which doesn't support a very flexible PKCS12 API.
It implements exactly the same API as software.sslmate.com/src/go-pkcs12, but adds:
- Builder struct, that allows the construction of an arbitrarily complex PKCS12 (multiple PrivateKeyEntries + multiple TrustedCertificateEntry + friendly names / aliases);
- DecodeAll function, that allows the decoding of an arbitrarily complex PKCS12 built with the Builder struct.
Note that only DER-encoded PKCS#12 files are supported, even though PKCS#12 allows BER encoding. This is because encoding/asn1 only supports DER.
Note that although the source code and issue tracker for this package are hosted on GitHub, the import path is:
github.com/ggpslop/go-pkcs12
Please be sure to use this path when you go get and import this package.
import "github.com/ggpslop/go-pkcs12"
var password = []byte("password")
// create the pkcs12 new builder. Options object is optional.
var builder, err = pkcs12.NewBuilder(pkcs12.Modern2023, password, pkcs12.Options{
orderFirstTrusts: true,
prvKeyEntryLen: 2,
trustCertLen: 1,
})
if err != nil {
panic(err)
}
// add first private key / certificate pair.
err = builder.SetPrivateKeyEntry(
"first_key_cert_pair",
privateKey1,
leafCertificate1,
chain1,
nil,
)
if err != nil {
panic(err)
}
// add second private key / certificate pair.
err = builder.SetPrivateKeyEntry(
"second_key_cert_pair",
privateKey2,
leafCertificate2,
nil,
[]byte("specificPrivateKeyPassword"),
)
if err != nil {
panic(err)
}
// add the only trusted certificate.
err = builder.SetTrustedCertificateEntry("only_trust", trustCertificate)
if err != nil {
panic(err)
}
// build all in memory.
pfx, err := builder.Build()
if err != nil {
panic(err)
}
// use pfximport "github.com/ggpslop/go-pkcs12"
var mainPassword = []byte("password")
// pfx was previously created using the Builder struct.
var store, err = DecodeAll(pfx, mainPassword, []Password{{
FriendlyName: "second_key_cert_pair",
Pass: []byte("specificPrivateKeyPassword"),
}})
if err != nil {
t.Fatal(err)
}
// print decoded pfx.
fmt.Println("Trusted:")
for _, trust := range store.Trusts {
fmt.Println(trust.Subject.String())
}
fmt.Println("KeyCertPair:")
for i, pair := range store.Pairs {
fmt.Printf("[%d]\n", i+1)
fmt.Println("PrvKey", pair.PrvKey != nil)
fmt.Println(pair.Cert.Subject.String())
fmt.Println("\tChain:")
for _, cert := range pair.CaCerts {
fmt.Println("\t", cert.Subject.String())
}
}Open an issue or PR at https://github.com/ggpslop/go-pkcs12