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().
This commit is contained in:
Christopher Denny
2016-05-31 15:59:43 -07:00
parent c13b09abe7
commit c8e6ba9f39
+22 -3
View File
@@ -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)
}