brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: openssl s_client -verify_hostname checks the name the way a browser does

Openssl
~/tips/tip-openssl-s-client-verify-hostname

echo | openssl s_client -connect example.com:443 -servername example.com \

Make s_client fail on a wrong host name or untrusted chain instead of just printing a warning.

Plain s_client connects even when the certificate is wrong. It prints a verify error somewhere in the output and carries on, which is easy to miss.

Two options make it strict. -verify_hostname checks the certificate against a name, and -verify_return_error aborts the handshake on any verification error:

echo | openssl s_client -connect example.com:443 -servername example.com \
  -verify_hostname example.com -verify_return_error -brief

With a good certificate you see Verification: OK and the connection details, and the exit code is 0. A name that is not in the certificate gives verify error:num=62:hostname mismatch, an untrusted chain something like unable to get local issuer certificate, and the command exits with 1.

I like it as a quick check before switching DNS to a new server: connect to the new IP with -connect 203.0.113.10:443, but keep -servername and -verify_hostname on the real name. Then you test the certificate the new machine will really serve.

Trust comes from the CA store OpenSSL was built with, which may not be the one your browser uses. For a private CA add -CAfile ca.pem. The name check needs OpenSSL 1.1.0 or newer. And note that -servername only picks which certificate the server sends; -verify_hostname is the option that actually checks it.

Further reading: OpenSSL: openssl-s_client, OpenSSL: verification options

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d All Openssl tips (34) All topics

Link