HTTPS and certificates · wrap-up
Cheat sheet & self-check
Every command from this section on one page.
Look at a site's TLS
curl -vI https://example.com | Handshake summary, certificate subject, issuer and dates, then headers |
openssl s_client -connect example.com:443 -servername example.com </dev/null | Full handshake, chain and negotiated version |
curl -sI https://example.com | grep -i strict-transport | Is HSTS set? |
curl -sI http://example.com | grep -i location | Does HTTP redirect to HTTPS? |
Words
SSL | Old name; all SSL versions are broken and disabled. Today it's TLS |
TLS 1.2 / 1.3 | Current versions; 1.3 is faster and simpler |
SNI | The hostname the client asks for, so one IP can serve many sites |
HSTS | Header telling browsers: only ever use HTTPS for this site |
Read a certificate
openssl x509 -in cert.pem -noout -text | Everything on the certificate |
openssl x509 -in cert.pem -noout -subject -issuer -dates | Who, issued by whom, valid from/to |
openssl x509 -in cert.pem -noout -ext subjectAltName | The names it is valid for |
openssl x509 -in cert.pem -noout -fingerprint -sha256 | SHA-256 fingerprint |
openssl s_client -connect host:443 -servername host </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName -dates | Read the certificate a live server sends |
CSRs and formats
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout site.key -out site.csr -subj '/CN=site.example.com' -addext 'subjectAltName=DNS:site.example.com' | New key and CSR with a SAN |
openssl req -in site.csr -noout -text | Check a CSR before sending it |
openssl x509 -in cert.der -inform der -out cert.pem | DER → PEM |
openssl pkcs12 -export -inkey site.key -in cert.pem -certfile chain.pem -out site.pfx | Key + certificate + chain → PKCS#12 (.pfx) |
openssl pkcs12 -in site.pfx -nodes | Look inside a .pfx |
Chains
openssl s_client -connect host:443 -servername host -showcerts </dev/null | Print every certificate the server sends |
openssl verify -CAfile root.pem -untrusted intermediate.pem leaf.pem | Verify a leaf through an intermediate to a root |
cat leaf.pem intermediate.pem > fullchain.pem | Build the chain file a server should send (leaf first) |
Trust stores
/etc/ssl/certs/ca-certificates.crt | Debian/Ubuntu system bundle |
sudo cp myca.crt /usr/local/share/ca-certificates/ && sudo update-ca-certificates | Trust a private CA on Debian/Ubuntu |
sudo cp myca.crt /etc/pki/ca-trust/source/anchors/ && sudo update-ca-trust | Trust a private CA on RHEL/Fedora |
keytool -importcert -alias myca -file myca.crt -cacerts | Trust a private CA in Java's store |
Public CA controls
dig CAA example.com | Which CAs may issue for this domain |
crt.sh | Search Certificate Transparency logs for certificates issued for your domains |
Self-signed in one command
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout self.key -out self.crt -days 30 -subj '/CN=app.lab.test' -addext 'subjectAltName=DNS:app.lab.test' | Key + self-signed certificate valid 30 days |
Private CA
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout ca.key -out ca.crt -days 3650 -subj '/CN=Lab Root CA' -addext 'basicConstraints=critical,CA:TRUE' -addext 'keyUsage=critical,keyCertSign,cRLSign' | Create the CA |
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout web.key -out web.csr -subj '/CN=web.lab.test' | Server key + CSR |
openssl x509 -req -in web.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out web.crt -days 90 -extfile web.ext | CA signs the server certificate |
openssl verify -CAfile ca.crt web.crt | Check the chain |
Trust it
curl --cacert ca.crt https://web.lab.test | Trust the CA for one command |
sudo cp ca.crt /usr/local/share/ca-certificates/lab-root.crt && sudo update-ca-certificates | Trust it system-wide (Debian/Ubuntu) |
certbot (nginx)
sudo apt install certbot python3-certbot-nginx | Install certbot with the nginx plugin |
sudo certbot --nginx -d example.com -d www.example.com | Get a certificate and configure nginx |
sudo certbot certonly --webroot -w /var/www/html -d example.com | Get a certificate only, using a web root |
sudo certbot renew --dry-run | Test renewal against the staging CA |
sudo certbot certificates | List certificates and their expiry |
systemctl list-timers | grep certbot | Confirm the automatic renewal timer exists |
Files
/etc/letsencrypt/live/<name>/fullchain.pem | Certificate + intermediate (use this for ssl_certificate) |
/etc/letsencrypt/live/<name>/privkey.pem | Private key |
/etc/letsencrypt/renewal-hooks/deploy/ | Scripts run after a successful renewal (e.g. reload a service) |
Check expiry
openssl x509 -in cert.pem -noout -enddate | Expiry date of a certificate file |
openssl x509 -in cert.pem -noout -checkend 2592000 | Exit code 1 if it expires within 30 days (2592000 s) |
echo | openssl s_client -connect host:443 -servername host 2>/dev/null | openssl x509 -noout -enddate | Expiry of the certificate a live server sends |
curl -vI https://host 2>&1 | grep 'expire date' | Quick check with curl |
sudo certbot certificates | Expiry of certbot-managed certificates |
Monitor
probe_ssl_earliest_cert_expiry - time() < 21*86400 | Prometheus blackbox exporter alert: under 21 days left |
crt.sh | Find certificates issued for your domains (inventory) |
Diagnose
curl -v https://host 2>&1 | grep -E 'SSL|subject|issuer|expire|error' | First look: version, names, dates, error |
openssl s_client -connect host:443 -servername host -showcerts </dev/null | Exactly what the server sends; 'Verify return code' at the end |
openssl s_client -connect host:443 -servername host </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName | Names, issuer and dates of the served certificate |
timedatectl | Is the clock right and synchronised? |
openssl s_client -connect host:443 -tls1_2 </dev/null | Force a TLS version to test support |