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 req -x509 makes a self-signed test certificate in one command

Openssl
~/tips/tip-security-openssl-req-self-signed-cert

openssl req -x509 -newkey rsa:2048 -days 365 \

Creates a private key and a self-signed certificate with a subjectAltName, for testing HTTPS locally.

For a local test of HTTPS you do not need a certificate authority. A self-signed certificate takes one command, as long as you keep its limits in mind.

openssl req -x509 -newkey rsa:2048 -days 365 \
  -subj "/CN=localhost" \
  -addext "subjectAltName = DNS:localhost" \
  -keyout key.pem -out cert.pem -noenc

-x509 outputs a self-signed certificate instead of a certificate request, which the manual says is typically used for test certificates. -newkey rsa:2048 generates the key, -keyout and -out name the files, and -subj saves you the interactive questions.

Set -days yourself. The manual gives 30 days as the default. The -addext line adds a subjectAltName, which matters because current browsers check the name there. -noenc leaves the private key unencrypted. It replaces -nodes, which is deprecated since OpenSSL 3.0, but on older versions you may still need -nodes.

The caveats are practical. Browsers will warn about this certificate, because no trusted authority signed it, and you have to accept the warning or import the certificate yourself. Anyone who gets key.pem can impersonate your test site, since the key has no passphrase. Never use a certificate like this in production. For a public site, a free certificate from Let’s Encrypt is the better route.

Further reading: OpenSSL: openssl-req

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