From 7fd5d218b6fc5d37efc81a6307cc4fbcd6cdadca Mon Sep 17 00:00:00 2001 From: Matthew McPherrin Date: Thu, 16 Jun 2016 14:43:56 -0700 Subject: [PATCH] Add a command to connect to a TLS server and print its presented certs --- display.go | 2 +- main.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/display.go b/display.go index 491cf21..574131b 100644 --- a/display.go +++ b/display.go @@ -68,7 +68,7 @@ Warnings: {{range . | certWarnings}} ` // displayCert takes in an x509 Certificate object and an alias -// (for jckes certs, blank otherwise), and prints out relevant +// (for jceks 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 certWithAlias) { diff --git a/main.go b/main.go index 42907fe..3d4321e 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ package main import ( "bufio" + "crypto/tls" "crypto/x509" "encoding/binary" "encoding/pem" @@ -43,6 +44,9 @@ var ( dump = app.Command("dump", "Display information about a certificate.") dumpFiles = dump.Arg("file", "Certificate file to dump (or stdin if not specified).").ExistingFiles() dumpType = dump.Flag("format", "Format of given input (heuristic guess if not specified).").String() + + connect = app.Command("connect", "Connect to a server and print its certificate") + connectTo = connect.Arg("server:port", "Hostname or IP to connect to").String() ) var fileExtToFormat = map[string]string{ @@ -89,6 +93,19 @@ func main() { readCerts(wg, certs, files) wg.Wait() + case connect.FullCommand(): // Get certs by connecting to a server + conn, err := tls.Dial("tcp", *connectTo, &tls.Config{ + InsecureSkipVerify: true, + }) + if err != nil { + fmt.Fprintf(os.Stderr, "Error connecting: %v\n", err) + os.Exit(1) + } + defer conn.Close() + for i, cert := range conn.ConnectionState().PeerCertificates { + fmt.Printf("** CERTIFICATE %d **\n", i+1) + displayCert(certWithAlias{cert: cert}) + } } }