Skip to content

Instantly share code, notes, and snippets.

@003random
Last active March 20, 2024 15:05
Show Gist options
  • Save 003random/714b53bfdc9068f6a832f50ba2690ff5 to your computer and use it in GitHub Desktop.
Save 003random/714b53bfdc9068f6a832f50ba2690ff5 to your computer and use it in GitHub Desktop.
Prints SSL certificate info from a given URL. Including the encoded public key and cert itself
package main
import (
"fmt"
"os"
"bytes"
"encoding/pem"
"crypto/tls"
"crypto/x509"
)
func main() {
conn, err := tls.Dial("tcp", os.Args[1] + ":443", &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
fmt.Println(err)
return
}
var encodedCert bytes.Buffer
err = pem.Encode(&encodedCert, &pem.Block{
Type: "CERTIFICATE",
Bytes: conn.ConnectionState().PeerCertificates[0].Raw,
})
if err != nil {
fmt.Println(err)
return
}
p, err := x509.MarshalPKIXPublicKey(conn.ConnectionState().PeerCertificates[0].PublicKey)
if err != nil {
fmt.Println(err)
return
}
publicKey := string(pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: p,
}))
fmt.Print("Issuer: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].Issuer)
fmt.Print("\nSubject: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].Subject)
fmt.Print("\nSerial Number: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].SerialNumber)
fmt.Print("\nVersion: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].Version)
fmt.Print("\nNot Before: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].NotBefore)
fmt.Print("\nNot After: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].NotAfter)
fmt.Print("\nEmail Addresses: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].EmailAddresses)
fmt.Print("\nIP Addresses: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].IPAddresses)
fmt.Print("\nPermitted DNS Domains: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].PermittedDNSDomains)
fmt.Print("\nExcluded DNS Domains: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].ExcludedDNSDomains)
fmt.Print("\nPermitted IP Ranges: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].PermittedIPRanges)
fmt.Print("\nEXcluded IP Ranges: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].ExcludedIPRanges)
fmt.Print("\nPermitted Email Addresses: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].PermittedEmailAddresses)
fmt.Print("\nExcluded Email Addresses: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].ExcludedEmailAddresses)
fmt.Print("\nPermitted URI Domains: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].PermittedURIDomains)
fmt.Print("\nExlucded URI Domains: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].ExcludedURIDomains)
fmt.Print("\nOCSP Server: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].OCSPServer)
fmt.Print("\nIssuing Certificate URL Server: ")
fmt.Print(conn.ConnectionState().PeerCertificates[0].IssuingCertificateURL)
fmt.Print("\nDNS Names: ")
fmt.Println(conn.ConnectionState().PeerCertificates[0].DNSNames)
fmt.Println("\nPublic Key: ")
fmt.Println(publicKey)
fmt.Println("Cert: ")
fmt.Println(encodedCert.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment