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 x509 -signkey renews a self-signed certificate with the same key

Openssl
~/tips/tip-openssl-x509-signkey-renew-self-signed

openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem

Re-signs an existing self-signed certificate with new dates, keeping its key and extensions.

Self-signed certificates tend to expire at the worst moment. For a test setup I don’t want a new key and a new request, I just want more days on the one I have.

openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem

With -signkey, OpenSSL signs the certificate again with the private key you give it. The docs say that the issuer is set to the subject, so the result is self-issued, and that all certificate extensions are retained by default. -clrext drops them. Unless you pass -preserve_dates, the start date becomes the current time and the end date follows from -days. The default is 30 days, which is why a short test certificate dies so soon.

On a test certificate that has a subjectAltName, the new file gets fresh dates and keeps the same public key and the same DNS: name. The serial number stays the same too, which I would not rely on if a client keeps a list of serials.

Check the result with:

openssl x509 -in cert-new.pem -noout -dates -ext subjectAltName

Caveats: this only makes sense for a certificate that signs itself. For one that a CA signed, you need a new request or a new signing step. Also, -days and -preserve_dates cannot be used together. And anything that pinned the old certificate will notice the change, because the dates and so the fingerprint are different.

Further reading: openssl-x509 documentation

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d | openssl s_client -connect example.com:443 -servername example.com -quiet -keylogfile keys.log All Openssl tips (34) All topics

Link