|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/pem" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +func usage() { |
| 15 | + fmt.Fprintf(os.Stderr, "Usage: certinfo url|file\n") |
| 16 | + fmt.Fprintf(os.Stderr, "Examples:\n") |
| 17 | + fmt.Fprintf(os.Stderr, " certinfo https://google.com\n") |
| 18 | + fmt.Fprintf(os.Stderr, " certinfo mycert.pem\n") |
| 19 | + os.Exit(1) |
| 20 | +} |
| 21 | + |
| 22 | +func getHttpsCerts(url string) ([]*x509.Certificate, error) { |
| 23 | + |
| 24 | + conf := &tls.Config{ |
| 25 | + InsecureSkipVerify: true, |
| 26 | + } |
| 27 | + |
| 28 | + host := strings.TrimPrefix(url, "https://") |
| 29 | + if idx := strings.Index(host, "/"); idx != -1 { |
| 30 | + host = host[:idx] |
| 31 | + } |
| 32 | + if idx := strings.Index(host, ":"); idx == -1 { |
| 33 | + host += ":443" // Default to port 443 for HTTPS |
| 34 | + } |
| 35 | + |
| 36 | + conn, err := tls.Dial("tcp", host, conf) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("error dialing %s: %w", host, err) |
| 39 | + } |
| 40 | + defer conn.Close() |
| 41 | + |
| 42 | + return conn.ConnectionState().PeerCertificates, nil |
| 43 | +} |
| 44 | + |
| 45 | +func main() { |
| 46 | + |
| 47 | + if len(os.Args) < 2 { |
| 48 | + usage() |
| 49 | + } |
| 50 | + |
| 51 | + var certs []*x509.Certificate |
| 52 | + |
| 53 | + for _, arg := range os.Args[1:] { |
| 54 | + if strings.HasPrefix(arg, "https://") { |
| 55 | + httpsCerts, httpsErr := getHttpsCerts(arg) |
| 56 | + if httpsErr != nil { |
| 57 | + log.Fatalf("Error getting HTTPS certificates: %v", httpsErr) |
| 58 | + } |
| 59 | + certs = httpsCerts |
| 60 | + } else { |
| 61 | + fileCert, fileErr := os.ReadFile(arg) |
| 62 | + if fileErr != nil { |
| 63 | + log.Fatalf("Error reading certificate file: %v", fileErr) |
| 64 | + } |
| 65 | + if bytes.HasPrefix(fileCert, []byte("-----BEGIN ")) { |
| 66 | + var block *pem.Block |
| 67 | + rest := fileCert |
| 68 | + certs = make([]*x509.Certificate, 0) |
| 69 | + for { |
| 70 | + block, rest = pem.Decode(rest) |
| 71 | + if block == nil { |
| 72 | + break |
| 73 | + } |
| 74 | + cert, parseErr := x509.ParseCertificate(block.Bytes) |
| 75 | + if parseErr != nil { |
| 76 | + log.Fatalf("Error parsing PEM certificate: %v", parseErr) |
| 77 | + } |
| 78 | + certs = append(certs, cert) |
| 79 | + } |
| 80 | + } else { |
| 81 | + cert, parseErr := x509.ParseCertificate(fileCert) |
| 82 | + if parseErr != nil { |
| 83 | + log.Fatalf("Error parsing certificate: %v", parseErr) |
| 84 | + } |
| 85 | + certs = make([]*x509.Certificate, 1) |
| 86 | + certs[0] = cert |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Printf("Certificate Information for %s:\n", arg) |
| 91 | + for idx, cert := range certs { |
| 92 | + fmt.Printf("\tCertificate %d:\n", idx+1) |
| 93 | + fmt.Printf("\t\tIssuer : %s\n", cert.Issuer) |
| 94 | + fmt.Printf("\t\tCommon Name : %s \n", cert.Issuer.CommonName) |
| 95 | + fmt.Printf("\t\tSubject : %s\n", cert.Subject) |
| 96 | + fmt.Printf("\t\tCommon Name : %s \n", cert.Subject.CommonName) |
| 97 | + fmt.Printf("\t\tStart : %s \n", cert.NotBefore.Format("2006-01-02")) |
| 98 | + fmt.Printf("\t\tExpiry : %s \n", cert.NotAfter.Format("2006-01-02")) |
| 99 | + fmt.Printf("\t\tKey Usage : %v \n", cert.KeyUsage) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments