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: curl pinnedpubkey refuses a server whose key is not the one you expect

Curl
~/tips/tip-curl-pinnedpubkey

openssl x509 -in server.pem -pubkey -noout \

Pin the server's public key, so even a valid certificate with another key is refused.

A certificate check asks whether any trusted CA vouches for the server. Pinning asks a narrower question: is this the exact key I saw before?

First compute the SHA-256 hash of the public key from the certificate, with OpenSSL:

openssl x509 -in server.pem -pubkey -noout \
  | openssl pkey -pubin -outform der \
  | openssl dgst -sha256 -binary | openssl base64

Then give that value to curl with the sha256// prefix:

curl --pinnedpubkey 'sha256//18RxxiyJbWkbwmzalr8RN37BYCM11PphupqQxtrLIY4=' \
  https://nas.example.com/

If the key does not match, curl stops with exit code 90 before it sends anything. You can list several hashes separated by ;, which helps when you rotate keys.

I think this suits a script that talks to one box you control, like a backup target, and much less a public site. Let’s Encrypt clients often make a new key at every renewal, so a pin on a public site can break every few months. If you pin, renew with the same key or add the next key’s hash in advance. The normal certificate checks still run, so pinning comes on top of them.

Further reading: curl man page, curl docs: SSL certificate verification

Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link