add json support and print just first one
This commit is contained in:
66
main.go
66
main.go
@@ -2,22 +2,60 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"encoding/json"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 {
|
jsonOut := flag.Bool("json", false, "output JSON with all certs/fields")
|
||||||
fmt.Fprintln(os.Stderr, "usage: inspect <cert.pem>")
|
flag.Parse()
|
||||||
|
if flag.NArg() < 1 {
|
||||||
|
fmt.Println("usage: certinfo [--json] <foo.crt>")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
data, err := ioutil.ReadFile(os.Args[1])
|
path := flag.Arg(0)
|
||||||
|
|
||||||
|
certs, err := ParseCertinfo(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fmt.Println("parse error:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if *jsonOut {
|
||||||
|
jsonBytes, err := json.Marshal(certs)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("marshal error:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonBytes))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Usually just the first one is interesting
|
||||||
|
cert := certs[0]
|
||||||
|
|
||||||
|
fmt.Println("Subject:", cert.Subject.String())
|
||||||
|
fmt.Println("Issuer:", cert.Issuer.String())
|
||||||
|
fmt.Println("Serial:", cert.SerialNumber.String())
|
||||||
|
fmt.Println("NotBefore:", cert.NotBefore)
|
||||||
|
fmt.Println("NotAfter:", cert.NotAfter)
|
||||||
|
if len(cert.DNSNames) > 0 {
|
||||||
|
fmt.Println("DNS SANs:", cert.DNSNames)
|
||||||
|
}
|
||||||
|
fmt.Println("Signature Algorithm:", cert.SignatureAlgorithm)
|
||||||
|
fmt.Println("Public Key Algorithm:", cert.PublicKeyAlgorithm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseCertinfo(filename string) ([]*x509.Certificate, error) {
|
||||||
|
data, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var certs []*x509.Certificate
|
||||||
for {
|
for {
|
||||||
var block *pem.Block
|
var block *pem.Block
|
||||||
block, data = pem.Decode(data)
|
block, data = pem.Decode(data)
|
||||||
@@ -25,26 +63,14 @@ func main() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if block.Type != "CERTIFICATE" {
|
if block.Type != "CERTIFICATE" {
|
||||||
|
fmt.Fprintf(os.Stderr, "warning: Ignoring block type: %s\n", block.Type)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cert, err := x509.ParseCertificate(block.Bytes)
|
cert, err := x509.ParseCertificate(block.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "parse error: %v\n", err)
|
return nil, err
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
fmt.Println("-----")
|
certs = append(certs, cert)
|
||||||
fmt.Printf("Subject: %s\n", cert.Subject.String())
|
|
||||||
fmt.Printf("Issuer: %s\n", cert.Issuer.String())
|
|
||||||
fmt.Printf("Serial: %s\n", cert.SerialNumber.String())
|
|
||||||
fmt.Printf("NotBefore: %s\n", cert.NotBefore)
|
|
||||||
fmt.Printf("NotAfter: %s\n", cert.NotAfter)
|
|
||||||
if len(cert.DNSNames) > 0 {
|
|
||||||
fmt.Printf("DNS SANs: %v\n", cert.DNSNames)
|
|
||||||
}
|
|
||||||
if len(cert.URIs) > 0 {
|
|
||||||
fmt.Printf("URI SANs: %v\n", cert.URIs)
|
|
||||||
}
|
|
||||||
fmt.Printf("Signature Algorithm: %s\n", cert.SignatureAlgorithm)
|
|
||||||
fmt.Printf("Public Key Algorithm: %s\n", cert.PublicKeyAlgorithm)
|
|
||||||
}
|
}
|
||||||
|
return certs, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user