This is a text-only version of the following page on https://raymii.org:
---
Title : OpenSSL: Get all certificates from a website in plain text
Author : Remy van Elst
Date : 04-02-2014
URL : https://raymii.org/s/articles/OpenSSL_-_Get_all_certificates_from_a_website_in_plain_text.html
Format : Markdown/HTML
---
This snippet shows you how to get all certificates of a website in plain text.
With a few OpenSSL commands one can get the website certificate plus
intermediate certificates, however, if you feed that output to OpenSSL it only
works on the first certificate. Using a bit of sed and bash magic we can feed
all certificates one by one to OpenSSL.
Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:
I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!
Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!
### Website certificate
You can request a website's certificate using `openssl s_client`. To view the
public key from google.com we can use the following command:
openssl s_client -connect google.com:443 &1 < /dev/null | sed -n '/-----BEGIN/,/-----END/p'
This will output only the public key.
### Plain text
If we feed that output into openssl again, we can output the certificate
information in plain text:
openssl s_client -connect google.com:443 2>&1 < /dev/null | sed -n '/-----BEGIN/,/-----END/p' | openssl x509 -noout -text
This is all the information about a certificate:
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
66:79:66:fc:e2:c7:d0:2d
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=US, O=Google Inc, CN=Google Internet Authority G2
Validity
Not Before: Jan 15 14:34:20 2014 GMT
Not After : May 15 00:00:00 2014 GMT
Subject: C=US, ST=California, L=Mountain View, O=Google Inc, CN=*.google.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:a3:cb:4e:db:d1:59:dc:13:b2:89:1d:d0:31:2c:
fb:f2:b2:55:a8:76:3c:d3:2b:00:f8:87:e6:b0:80:
af:8f:46:ed:d8:35:72:1e:d0:55:26:a4:09:7e:71:
b4:6b:28:df:c4:54:b3:17:5f:31:77:d1:a2:eb:2a:
f4:e5:7c:5e:7c:2a:3d:39:b3:56:64:bf:65:45:86:
ef:e1:b4:94:7b:df:6e:b9:e9:c7:ef:b0:12:b8:31:
54:58:a9:39:bd:33:8f:df:53:76:da:49:ae:bf:5e:
f3:e7:f3:2f:6a:e5:9e:d9:fd:58:99:45:f7:a0:6f:
23:6e:88:8f:1d:3e:c3:50:28:a1:d6:ba:a6:56:50:
f4:c0:a9:73:37:96:75:42:5b:2f:65:18:54:17:fa:
51:f7:74:f2:59:18:dc:e4:33:fb:88:d5:60:e4:7e:
f7:65:57:02:fd:8d:f6:e3:74:53:c5:27:30:ec:85:
b0:db:43:f0:1f:af:e5:c1:f5:c7:06:8b:11:a8:20:
50:df:98:72:b7:4b:0f:73:e7:d9:7a:b8:3d:51:e7:
bc:d2:0f:12:27:d0:e2:f6:2e:0a:a9:c1:78:df:d6:
d1:5f:59:9f:a2:44:ef:7c:24:ca:b9:f8:7e:fe:c2:
84:66:f7:f8:9d:41:19:42:61:90:52:5c:68:33:b8:
b6:61
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Extended Key Usage:
TLS Web Server Authentication, TLS Web Client Authentication
X509v3 Subject Alternative Name:
DNS:*.google.com, DNS:*.android.com, DNS:*.appengine.google.com, DNS:*.cloud.google.com, DNS:*.google-analytics.com, DNS:*.google.ca, DNS:*.google.cl, DNS:*.google.co.in, DNS:*.google.co.jp, DNS:*.google.co.uk, DNS:*.google.com.ar, DNS:*.google.com.au, DNS:*.google.com.br, DNS:*.google.com.co, DNS:*.google.com.mx, DNS:*.google.com.tr, DNS:*.google.com.vn, DNS:*.google.de, DNS:*.google.es, DNS:*.google.fr, DNS:*.google.hu, DNS:*.google.it, DNS:*.google.nl, DNS:*.google.pl, DNS:*.google.pt, DNS:*.googleapis.cn, DNS:*.googlecommerce.com, DNS:*.googlevideo.com, DNS:*.gstatic.com, DNS:*.urchin.com, DNS:*.url.google.com, DNS:*.youtube-nocookie.com, DNS:*.youtube.com, DNS:*.youtubeeducation.com, DNS:*.ytimg.com, DNS:android.com, DNS:g.co, DNS:goo.gl, DNS:google-analytics.com, DNS:google.com, DNS:googlecommerce.com, DNS:urchin.com, DNS:youtu.be, DNS:youtube.com, DNS:youtubeeducation.com
Authority Information Access:
CA Issuers - URI:http://pki.google.com/GIAG2.crt
OCSP - URI:http://clients1.google.com/ocsp
X509v3 Subject Key Identifier:
61:54:7B:A4:44:E8:68:E2:D3:0D:CC:77:D4:29:35:3B:58:23:60:B3
X509v3 Basic Constraints: critical
CA:FALSE
X509v3 Authority Key Identifier:
keyid:4A:DD:06:16:1B:BC:F6:68:B5:76:F5:81:B6:BB:62:1A:BA:5A:81:2F
X509v3 Certificate Policies:
Policy: 1.3.6.1.4.1.11129.2.5.1
X509v3 CRL Distribution Points:
Full Name:
URI:http://pki.google.com/GIAG2.crl
Signature Algorithm: sha1WithRSAEncryption
6f:7b:07:5c:59:ce:7d:52:b3:bc:26:c7:7c:e8:6e:32:aa:fc:
3e:67:0a:51:1c:4b:6e:bb:7d:69:eb:fc:7a:02:76:59:bf:86:
97:50:ed:bd:ec:88:7c:d3:a0:a3:a6:27:fe:a0:21:55:76:da:
1b:7c:22:82:1e:31:73:d3:25:33:2d:72:de:63:36:18:50:9e:
49:bf:e3:78:c7:b4:33:38:b7:f8:10:93:a0:fa:5d:a7:4d:78:
48:74:7f:dd:c6:aa:8f:eb:26:de:e3:6b:4f:4b:c7:3a:0c:b7:
4c:c3:a9:bf:2b:46:b8:50:37:0b:05:e5:92:e4:9c:f3:36:bb:
f1:75:ad:20:31:99:1e:3d:83:6e:e1:dd:13:67:1a:2a:f7:c6:
e1:1d:4d:c4:4f:f8:e0:88:c5:3e:17:8b:15:5e:71:3d:69:99:
56:ba:fb:7f:07:d6:33:0a:d0:27:aa:9d:ce:77:a9:af:a1:93:
10:8c:27:48:1a:1f:62:48:04:8f:64:51:6d:52:e9:60:6d:a9:
98:7b:43:d6:cc:66:69:65:40:d6:57:ff:06:f5:73:59:8f:1b:
87:9b:36:5b:41:06:4a:5e:25:69:bb:f0:70:49:72:0b:d8:67:
7b:ed:d4:b3:c8:79:c8:58:5e:f3:f1:f5:98:fa:80:03:d3:7d:
8a:d0:c8:76
### Multiple certificates
Using the `-showcerts` option of `s_client` we can show all certificates the
website sends, including the issuing and intermediate certificates:
openssl s_client -connect google.com:443 -showcerts 2>&1 < /dev/null
If we want to receive those certificates without all the other output we can use
the same sed trick as before:
openssl s_client -connect google.com:443 -showcerts 2>&1 < /dev/null | sed -n '/-----BEGIN/,/-----END/p'
However, if we feed that into openssl again to receive the plain text output we
only get output for the first certificate:
openssl s_client -connect google.com:443 -showcerts 2>&1 < /dev/null | sed -n '/-----BEGIN/,/-----END/p' | openssl x509 -noout -text
According to the OpenSSL documentation the x509 option only supports one
certificate at a time. Using the following one liner we:
* Set the Internal Field Separator to a colon `(:)`.
* Get all the certificates for google.com in a variable named `certificates`.
* We use a group of sed commands to first print the certificates, then we substitute the beginning of the `-----BEGIN` line with a colon `(:)` and then the original `----BEGIN` line resulting in `:-----BEGIN`.
* We do a for loop on all certificates in the output, split by a colon `(:)`.
* We feed those one by one to OpenSSL
* We change the Internal Field Seperator back to what it was.
This is the command:
OLDIFS=$IFS; IFS=':' certificates=$(openssl s_client -connect google.com:443
-showcerts -tlsextdebug 2>&1 &1 &1 &1 &1 .
Just to be clear, the information on this website is for meant for educational
purposes and you use it at your own risk. I do not take responsibility if you
screw something up. Use common sense, do not 'rm -rf /' as root for example.
If you have any questions then do not hesitate to contact me.
See https://raymii.org/s/static/About.html for details.