From c8e6ba9f391793cf18ffb706f0ade4f7256bb7bd Mon Sep 17 00:00:00 2001 From: Christopher Denny Date: Tue, 31 May 2016 15:59:43 -0700 Subject: [PATCH] Changed pkcs12 support to first convert to pem. THIS DOES NOT WORK PROPERLY YET. Still need to look into why using pkcs12.ToPem causes errors when parsing the resulting data when using x509.ParseCertificates(). --- main.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index b76eee6..0e502d5 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,14 @@ package main import ( + "bufio" "crypto/x509" "encoding/hex" "encoding/pem" "fmt" "io/ioutil" "os" + "strings" "golang.org/x/crypto/pkcs12" "gopkg.in/alecthomas/kingpin.v2" @@ -17,7 +19,7 @@ var ( dump = app.Command("dump", "Display information about a certificate.") dumpFile = dump.Arg("file", "Certificate file to dump.").Required().String() - dumpType = dump.Flag("format", "Format of given input. If unspecified, certigo guesses based on file extension").Short('f').String() + dumpType = dump.Flag("format", "Format of given input. If unspecified, certigo guesses based on file extension").Default("guess").Short('f').String() ) func main() { @@ -51,11 +53,28 @@ func getCerts(file, format string) ([]*x509.Certificate, error) { block, data = pem.Decode(data) } case "PKCS12": - _, cert, err := pkcs12.Decode(data, "password") + scanner := bufio.NewReader(os.Stdin) + fmt.Print("Enter password: ") + password, _ := scanner.ReadString('\n') + blocks, err := pkcs12.ToPEM(data, strings.TrimSuffix(password, "\n")) if err != nil { return nil, err } - certs = append(certs, cert) + for _, block := range blocks { + cert, err := x509.ParseCertificates(block.Bytes) + if err != nil { + return nil, err + } + certs = append(certs, cert[0]) + } + case "guess": + if strings.HasSuffix(file, "pem") { + return getCerts(file, "PEM") + } else if strings.HasSuffix(file, "p12") || strings.HasSuffix(file, "pks") { + return getCerts(file, "PKCS12") + } else { + return getCerts(file, ", couldn't guess format") + } default: return nil, fmt.Errorf("unknown type %s", format) }