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) }