| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/dsa" |
| 6 |
"crypto/ecdsa" |
| 7 |
"crypto/elliptic" |
| 8 |
"crypto/rsa" |
| 9 |
"crypto/tls" |
| 10 |
"crypto/x509" |
| 11 |
"crypto/x509/pkix" |
| 12 |
"flag" |
| 13 |
"fmt" |
| 14 |
"io/ioutil" |
| 15 |
"os" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"github.com/cloudflare/cfssl/helpers" |
| 19 |
) |
| 20 |
|
| 21 |
func certPublic(cert *x509.Certificate) string { |
| 22 |
switch pub := cert.PublicKey.(type) { |
| 23 |
case *rsa.PublicKey: |
| 24 |
return fmt.Sprintf("RSA-%d", pub.N.BitLen()) |
| 25 |
case *ecdsa.PublicKey: |
| 26 |
switch pub.Curve { |
| 27 |
case elliptic.P256(): |
| 28 |
return "ECDSA-prime256v1" |
| 29 |
case elliptic.P384(): |
| 30 |
return "ECDSA-secp384r1" |
| 31 |
case elliptic.P521(): |
| 32 |
return "ECDSA-secp521r1" |
| 33 |
default: |
| 34 |
return "ECDSA (unknown curve)" |
| 35 |
} |
| 36 |
case *dsa.PublicKey: |
| 37 |
return "DSA" |
| 38 |
default: |
| 39 |
return "Unknown" |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func displayName(name pkix.Name) string { |
| 44 |
var ns []string |
| 45 |
|
| 46 |
if name.CommonName != "" { |
| 47 |
ns = append(ns, name.CommonName) |
| 48 |
} |
| 49 |
|
| 50 |
for i := range name.Country { |
| 51 |
ns = append(ns, fmt.Sprintf("C=%s", name.Country[i])) |
| 52 |
} |
| 53 |
|
| 54 |
for i := range name.Organization { |
| 55 |
ns = append(ns, fmt.Sprintf("O=%s", name.Organization[i])) |
| 56 |
} |
| 57 |
|
| 58 |
for i := range name.OrganizationalUnit { |
| 59 |
ns = append(ns, fmt.Sprintf("OU=%s", name.OrganizationalUnit[i])) |
| 60 |
} |
| 61 |
|
| 62 |
for i := range name.Locality { |
| 63 |
ns = append(ns, fmt.Sprintf("L=%s", name.Locality[i])) |
| 64 |
} |
| 65 |
|
| 66 |
for i := range name.Province { |
| 67 |
ns = append(ns, fmt.Sprintf("ST=%s", name.Province[i])) |
| 68 |
} |
| 69 |
|
| 70 |
if len(ns) > 0 { |
| 71 |
return "/" + strings.Join(ns, "/") |
| 72 |
} |
| 73 |
|
| 74 |
return "*** no subject information ***" |
| 75 |
} |
| 76 |
|
| 77 |
func keyUsages(ku x509.KeyUsage) string { |
| 78 |
var uses []string |
| 79 |
|
| 80 |
for u, s := range keyUsage { |
| 81 |
if (ku & u) != 0 { |
| 82 |
uses = append(uses, s) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return strings.Join(uses, ", ") |
| 87 |
} |
| 88 |
|
| 89 |
func extUsage(ext []x509.ExtKeyUsage) string { |
| 90 |
ns := make([]string, 0, len(ext)) |
| 91 |
for i := range ext { |
| 92 |
ns = append(ns, extKeyUsages[ext[i]]) |
| 93 |
} |
| 94 |
|
| 95 |
return strings.Join(ns, ", ") |
| 96 |
} |
| 97 |
|
| 98 |
func showBasicConstraints(cert *x509.Certificate) { |
| 99 |
fmt.Printf("\tBasic constraints: ") |
| 100 |
if cert.BasicConstraintsValid { |
| 101 |
fmt.Printf("valid") |
| 102 |
} else { |
| 103 |
fmt.Printf("invalid") |
| 104 |
} |
| 105 |
|
| 106 |
if cert.IsCA { |
| 107 |
fmt.Printf(", is a CA certificate") |
| 108 |
} |
| 109 |
|
| 110 |
if (cert.MaxPathLen == 0 && cert.MaxPathLenZero) || (cert.MaxPathLen > 0) { |
| 111 |
fmt.Printf(", max path length %d", cert.MaxPathLen) |
| 112 |
} |
| 113 |
|
| 114 |
fmt.Printf("\n") |
| 115 |
} |
| 116 |
|
| 117 |
const oneTrueDateFormat = "2006-01-02T15:04:05-0700" |
| 118 |
|
| 119 |
var dateFormat string |
| 120 |
|
| 121 |
func wrapPrint(text string, indent int) { |
| 122 |
tabs := "" |
| 123 |
for i := 0; i < indent; i++ { |
| 124 |
tabs += "\t" |
| 125 |
} |
| 126 |
|
| 127 |
fmt.Printf(tabs+"%s\n", wrap(text, indent)) |
| 128 |
} |
| 129 |
|
| 130 |
func displayCert(cert *x509.Certificate) { |
| 131 |
fmt.Println("CERTIFICATE") |
| 132 |
fmt.Println(wrap("Subject: "+displayName(cert.Subject), 0)) |
| 133 |
fmt.Println(wrap("Issuer: "+displayName(cert.Issuer), 0)) |
| 134 |
fmt.Printf("\tSignature algorithm: %s / %s\n", sigAlgoPK(cert.SignatureAlgorithm), |
| 135 |
sigAlgoHash(cert.SignatureAlgorithm)) |
| 136 |
fmt.Println("Details:") |
| 137 |
wrapPrint("Public key: "+certPublic(cert), 1) |
| 138 |
fmt.Printf("\tSerial number: %s\n", cert.SerialNumber) |
| 139 |
|
| 140 |
if len(cert.AuthorityKeyId) > 0 { |
| 141 |
fmt.Printf("\t%s\n", wrap("AKI: "+dumpHex(cert.AuthorityKeyId), 1)) |
| 142 |
} |
| 143 |
if len(cert.SubjectKeyId) > 0 { |
| 144 |
fmt.Printf("\t%s\n", wrap("SKI: "+dumpHex(cert.SubjectKeyId), 1)) |
| 145 |
} |
| 146 |
|
| 147 |
wrapPrint("Valid from: "+cert.NotBefore.Format(dateFormat), 1) |
| 148 |
fmt.Printf("\t until: %s\n", cert.NotAfter.Format(dateFormat)) |
| 149 |
fmt.Printf("\tKey usages: %s\n", keyUsages(cert.KeyUsage)) |
| 150 |
|
| 151 |
if len(cert.ExtKeyUsage) > 0 { |
| 152 |
fmt.Printf("\tExtended usages: %s\n", extUsage(cert.ExtKeyUsage)) |
| 153 |
} |
| 154 |
|
| 155 |
showBasicConstraints(cert) |
| 156 |
|
| 157 |
validNames := make([]string, 0, len(cert.DNSNames)+len(cert.EmailAddresses)+len(cert.IPAddresses)) |
| 158 |
for i := range cert.DNSNames { |
| 159 |
validNames = append(validNames, "dns:"+cert.DNSNames[i]) |
| 160 |
} |
| 161 |
|
| 162 |
for i := range cert.EmailAddresses { |
| 163 |
validNames = append(validNames, "email:"+cert.EmailAddresses[i]) |
| 164 |
} |
| 165 |
|
| 166 |
for i := range cert.IPAddresses { |
| 167 |
validNames = append(validNames, "ip:"+cert.IPAddresses[i].String()) |
| 168 |
} |
| 169 |
|
| 170 |
sans := fmt.Sprintf("SANs (%d): %s\n", len(validNames), strings.Join(validNames, ", ")) |
| 171 |
wrapPrint(sans, 1) |
| 172 |
|
| 173 |
l := len(cert.IssuingCertificateURL) |
| 174 |
if l != 0 { |
| 175 |
var aia string |
| 176 |
if l == 1 { |
| 177 |
aia = "AIA" |
| 178 |
} else { |
| 179 |
aia = "AIAs" |
| 180 |
} |
| 181 |
wrapPrint(fmt.Sprintf("%d %s:", l, aia), 1) |
| 182 |
for _, url := range cert.IssuingCertificateURL { |
| 183 |
wrapPrint(url, 2) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
l = len(cert.OCSPServer) |
| 188 |
if l > 0 { |
| 189 |
title := "OCSP server" |
| 190 |
if l > 1 { |
| 191 |
title += "s" |
| 192 |
} |
| 193 |
wrapPrint(title+":\n", 1) |
| 194 |
for _, ocspServer := range cert.OCSPServer { |
| 195 |
wrapPrint(fmt.Sprintf("- %s\n", ocspServer), 2) |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
func displayAllCerts(in []byte, leafOnly bool) { |
| 201 |
certs, err := helpers.ParseCertificatesPEM(in) |
| 202 |
if err != nil { |
| 203 |
certs, _, err = helpers.ParseCertificatesDER(in, "") |
| 204 |
if err != nil { |
| 205 |
Warn(TranslateCFSSLError(err), "failed to parse certificates") |
| 206 |
return |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if len(certs) == 0 { |
| 211 |
Warnx("no certificates found") |
| 212 |
return |
| 213 |
} |
| 214 |
|
| 215 |
if leafOnly { |
| 216 |
displayCert(certs[0]) |
| 217 |
return |
| 218 |
} |
| 219 |
|
| 220 |
for i := range certs { |
| 221 |
displayCert(certs[i]) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
func displayAllCertsWeb(uri string, leafOnly bool) { |
| 226 |
ci := getConnInfo(uri) |
| 227 |
conn, err := tls.Dial("tcp", ci.Addr, permissiveConfig()) |
| 228 |
if err != nil { |
| 229 |
Warn(err, "couldn't connect to %s", ci.Addr) |
| 230 |
return |
| 231 |
} |
| 232 |
defer conn.Close() |
| 233 |
|
| 234 |
state := conn.ConnectionState() |
| 235 |
conn.Close() |
| 236 |
|
| 237 |
conn, err = tls.Dial("tcp", ci.Addr, verifyConfig(ci.Host)) |
| 238 |
if err == nil { |
| 239 |
err = conn.VerifyHostname(ci.Host) |
| 240 |
if err == nil { |
| 241 |
state = conn.ConnectionState() |
| 242 |
} |
| 243 |
conn.Close() |
| 244 |
} else { |
| 245 |
Warn(err, "TLS verification error with server name %s", ci.Host) |
| 246 |
} |
| 247 |
|
| 248 |
if len(state.PeerCertificates) == 0 { |
| 249 |
Warnx("no certificates found") |
| 250 |
return |
| 251 |
} |
| 252 |
|
| 253 |
if leafOnly { |
| 254 |
displayCert(state.PeerCertificates[0]) |
| 255 |
return |
| 256 |
} |
| 257 |
|
| 258 |
if len(state.VerifiedChains) == 0 { |
| 259 |
Warnx("no verified chains found; using peer chain") |
| 260 |
for i := range state.PeerCertificates { |
| 261 |
displayCert(state.PeerCertificates[i]) |
| 262 |
} |
| 263 |
} else { |
| 264 |
fmt.Println("TLS chain verified successfully.") |
| 265 |
for i := range state.VerifiedChains { |
| 266 |
fmt.Printf("--- Verified certificate chain %d ---\n", i+1) |
| 267 |
for j := range state.VerifiedChains[i] { |
| 268 |
displayCert(state.VerifiedChains[i][j]) |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func main() { |
| 275 |
var leafOnly bool |
| 276 |
flag.StringVar(&dateFormat, "s", oneTrueDateFormat, "date `format` in Go time format") |
| 277 |
flag.BoolVar(&leafOnly, "l", false, "only show the leaf certificate") |
| 278 |
flag.Parse() |
| 279 |
|
| 280 |
if flag.NArg() == 0 || (flag.NArg() == 1 && flag.Arg(0) == "-") { |
| 281 |
certs, err := ioutil.ReadAll(os.Stdin) |
| 282 |
if err != nil { |
| 283 |
Warn(err, "couldn't read certificates from standard input") |
| 284 |
os.Exit(1) |
| 285 |
} |
| 286 |
|
| 287 |
// This is needed for getting certs from JSON/jq. |
| 288 |
certs = bytes.TrimSpace(certs) |
| 289 |
certs = bytes.Replace(certs, []byte(`\n`), []byte{0xa}, -1) |
| 290 |
certs = bytes.Trim(certs, `"`) |
| 291 |
displayAllCerts(certs, leafOnly) |
| 292 |
} else { |
| 293 |
for _, filename := range flag.Args() { |
| 294 |
fmt.Printf("--%s ---\n", filename) |
| 295 |
if strings.HasPrefix(filename, "https://") { |
| 296 |
displayAllCertsWeb(filename, leafOnly) |
| 297 |
} else { |
| 298 |
in, err := ioutil.ReadFile(filename) |
| 299 |
if err != nil { |
| 300 |
Warn(err, "couldn't read certificate") |
| 301 |
continue |
| 302 |
} |
| 303 |
|
| 304 |
displayAllCerts(in, leafOnly) |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |