Security Basics — SSH, TLS and Certificates›HTTPS and certificates · Cheat sheet & self-check

HTTPS and certificates · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

07 · From HTTP to HTTPS

Look at a site's TLS

curl -vI https://example.comHandshake summary, certificate subject, issuer and dates, then headers
openssl s_client -connect example.com:443 -servername example.com </dev/nullFull handshake, chain and negotiated version
curl -sI https://example.com | grep -i strict-transportIs HSTS set?
curl -sI http://example.com | grep -i locationDoes HTTP redirect to HTTPS?

Words

SSLOld name; all SSL versions are broken and disabled. Today it's TLS
TLS 1.2 / 1.3Current versions; 1.3 is faster and simpler
SNIThe hostname the client asks for, so one IP can serve many sites
HSTSHeader telling browsers: only ever use HTTPS for this site

08 · Anatomy of a certificate

Read a certificate

openssl x509 -in cert.pem -noout -textEverything on the certificate
openssl x509 -in cert.pem -noout -subject -issuer -datesWho, issued by whom, valid from/to
openssl x509 -in cert.pem -noout -ext subjectAltNameThe names it is valid for
openssl x509 -in cert.pem -noout -fingerprint -sha256SHA-256 fingerprint
openssl s_client -connect host:443 -servername host </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName -datesRead 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 -textCheck a CSR before sending it
openssl x509 -in cert.der -inform der -out cert.pemDER → PEM
openssl pkcs12 -export -inkey site.key -in cert.pem -certfile chain.pem -out site.pfxKey + certificate + chain → PKCS#12 (.pfx)
openssl pkcs12 -in site.pfx -nodesLook inside a .pfx

09 · Certificate authorities and the chain of trust

Chains

openssl s_client -connect host:443 -servername host -showcerts </dev/nullPrint every certificate the server sends
openssl verify -CAfile root.pem -untrusted intermediate.pem leaf.pemVerify a leaf through an intermediate to a root
cat leaf.pem intermediate.pem > fullchain.pemBuild the chain file a server should send (leaf first)

Trust stores

/etc/ssl/certs/ca-certificates.crtDebian/Ubuntu system bundle
sudo cp myca.crt /usr/local/share/ca-certificates/ && sudo update-ca-certificatesTrust a private CA on Debian/Ubuntu
sudo cp myca.crt /etc/pki/ca-trust/source/anchors/ && sudo update-ca-trustTrust a private CA on RHEL/Fedora
keytool -importcert -alias myca -file myca.crt -cacertsTrust a private CA in Java's store

Public CA controls

dig CAA example.comWhich CAs may issue for this domain
crt.shSearch Certificate Transparency logs for certificates issued for your domains

10 · Self-signed certificates and your own CA

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.extCA signs the server certificate
openssl verify -CAfile ca.crt web.crtCheck the chain

Trust it

curl --cacert ca.crt https://web.lab.testTrust the CA for one command
sudo cp ca.crt /usr/local/share/ca-certificates/lab-root.crt && sudo update-ca-certificatesTrust it system-wide (Debian/Ubuntu)

11 · Free, automated certificates with ACME

certbot (nginx)

sudo apt install certbot python3-certbot-nginxInstall certbot with the nginx plugin
sudo certbot --nginx -d example.com -d www.example.comGet a certificate and configure nginx
sudo certbot certonly --webroot -w /var/www/html -d example.comGet a certificate only, using a web root
sudo certbot renew --dry-runTest renewal against the staging CA
sudo certbot certificatesList certificates and their expiry
systemctl list-timers | grep certbotConfirm the automatic renewal timer exists

Files

/etc/letsencrypt/live/<name>/fullchain.pemCertificate + intermediate (use this for ssl_certificate)
/etc/letsencrypt/live/<name>/privkey.pemPrivate key
/etc/letsencrypt/renewal-hooks/deploy/Scripts run after a successful renewal (e.g. reload a service)

12 · Certificate expiry and renewal

Check expiry

openssl x509 -in cert.pem -noout -enddateExpiry date of a certificate file
openssl x509 -in cert.pem -noout -checkend 2592000Exit 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 -enddateExpiry of the certificate a live server sends
curl -vI https://host 2>&1 | grep 'expire date'Quick check with curl
sudo certbot certificatesExpiry of certbot-managed certificates

Monitor

probe_ssl_earliest_cert_expiry - time() < 21*86400Prometheus blackbox exporter alert: under 21 days left
crt.shFind certificates issued for your domains (inventory)

13 · Fixing common TLS errors

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/nullExactly 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 subjectAltNameNames, issuer and dates of the served certificate
timedatectlIs the clock right and synchronised?
openssl s_client -connect host:443 -tls1_2 </dev/nullForce a TLS version to test support