diff --git a/display.go b/display.go index f74c372..8623764 100644 --- a/display.go +++ b/display.go @@ -18,8 +18,8 @@ package main import ( "bytes" - "crypto/x509" "encoding/hex" + "fmt" "os" "strings" "text/template" @@ -54,10 +54,11 @@ Email Addresses: {{range .EmailAddresses}} Serial Number: {{.SerialNumber}} {{end}} ` -// displayCert takes in an x509 Certificate object and prints out relevant +// displayCert takes in an x509 Certificate object and an alias +// (for jckes certs, blank otherwise), and prints out relevant // information. Start and end dates are colored based on whether or not // the certificate is expired, not expired, or close to expiring. -func displayCert(cert *x509.Certificate) { +func displayCert(cert certWithAlias) { funcMap := template.FuncMap{ "hexify": hexify, "certStart": certStart, @@ -65,7 +66,10 @@ func displayCert(cert *x509.Certificate) { } t := template.New("Cert template").Funcs(funcMap) t, _ = t.Parse(layout) - t.Execute(os.Stdout, cert) + if cert.alias != "" { + fmt.Println("Alias:", cert.alias) + } + t.Execute(os.Stdout, cert.cert) } diff --git a/main.go b/main.go index 43deb14..1019c76 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,11 @@ var fileExtToFormat = map[string]string{ ".jceks": "JCEKS", } +type certWithAlias struct { + alias string + cert *x509.Certificate +} + func main() { switch kingpin.MustParse(app.Parse(os.Args[1:])) { case dump.FullCommand(): // Dump certificate @@ -81,12 +86,13 @@ func formatForFile(filename, format string) (string, bool) { } // getCerts takes in a filename and format type and returns an -// array of all the certificates found in that file. If no format +// array of all the certificates found in that file along with aliases +// for each cert if the format of the input was jceks. If no format // is specified for the file, getCerts guesses what format was used // based on the file extension used in the file name. If it can't // guess based on this it returns and error. -func getCerts(file, format string) ([]*x509.Certificate, error) { - var certs []*x509.Certificate +func getCerts(file, format string) ([]certWithAlias, error) { + var certs []certWithAlias data, _ := ioutil.ReadFile(file) switch format { case "PEM": @@ -96,7 +102,7 @@ func getCerts(file, format string) ([]*x509.Certificate, error) { if err != nil { return nil, err } - certs = append(certs, cert) + certs = append(certs, certWithAlias{cert: cert}) block, data = pem.Decode(data) } case "PKCS12": @@ -113,7 +119,7 @@ func getCerts(file, format string) ([]*x509.Certificate, error) { if err != nil { return nil, err } - certs = append(certs, cert) + certs = append(certs, certWithAlias{cert: cert}) } } case "JCEKS": @@ -129,7 +135,7 @@ func getCerts(file, format string) ([]*x509.Certificate, error) { if err != nil { return nil, err } - certs = append(certs, cert) + certs = append(certs, certWithAlias{cert: cert, alias: alias}) } for _, alias := range keyStore.ListPrivateKeys() { fmt.Printf("Enter password for alias [%s]: ", alias) @@ -138,7 +144,9 @@ func getCerts(file, format string) ([]*x509.Certificate, error) { if err != nil { return nil, err } - certs = append(certs, certArr...) + for _, cert := range certArr { + certs = append(certs, certWithAlias{cert: cert, alias: alias}) + } } default: return nil, fmt.Errorf("unknown file type: %s", format)