Skip to content

Instantly share code, notes, and snippets.

@ruiwen
Last active May 8, 2022 07:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruiwen/f7aaf042e4c6dd07d7d91329f6eafefb to your computer and use it in GitHub Desktop.
Save ruiwen/f7aaf042e4c6dd07d7d91329f6eafefb to your computer and use it in GitHub Desktop.
Obtain Base64-encoded SHA256 hash of a servers OpenSSL pubkey used with `curl`'s `--pinnedpubkey`
# Obtaining server certificate
openssl s_client -CAfile ca.crt -connect "server.domain.com:443" < /dev/null 2> /dev/null | openssl x509 -outform PEM > server.crt
# You may get an error like the following
# CONNECTED(00000003)
# 140048174458520:error:140790E5:SSL routines:ssl23_write:ssl handshake failure:s23_lib.c:177:
# ---
# no peer certificate available
# ---
# No client certificate CA names sent
# ---
# SSL handshake has read 0 bytes and written 305 bytes
# ---
#
# in which case you might have to use SNI (server name indication), like so
openssl s_client -CAfile ca.crt -servername "server.domain.com" -connect "server.domain.com:443" < /dev/null 2> /dev/null | openssl x509 -outform PEM > server.crt
# Obtaining server public key
openssl s_client -CAfile ca.crt -connect "server.domain.com:443" < /dev/null 2> /dev/null | openssl x509 -pubkey -noout
# Obtaining base64 encoded, SHA256 hash of pub key
sed '1d;$d' server.pub | tr -d '\n' | base64 -d -w 0 | openssl dgst -sha256 -binary | base64 -w 0
# Obtaining fingerprint from cert
openssl x509 -noout -in server.crt -fingerprint > leanplum.fingerprint
# Using it with curl
curl -vv --cacert ca.crt --pinnedpubkey "sha256//<results from above step>" "https://server.domain.com"
@ihamadfuad
Copy link

sed '1d;$d' server.pub | tr -d '\n' | base64 -d -w 0 | openssl dgst -sha256 -binary | base64 -w 0

Returns:

sed: server.pub: No such file or directory
base64: invalid option -- w
Usage:	base64 [-hvDd] [-b num] [-i in_file] [-o out_file]
  -h, --help     display this message
  -Dd, --decode   decodes input
  -b, --break    break encoded string into num character lines
  -i, --input    input file (default: "-" for stdin)
  -o, --output   output file (default: "-" for stdout)
base64: invalid option -- w
Usage:	base64 [-hvDd] [-b num] [-i in_file] [-o out_file]
  -h, --help     display this message
  -Dd, --decode   decodes input
  -b, --break    break encoded string into num character lines
  -i, --input    input file (default: "-" for stdin)
  -o, --output   output file (default: "-" for stdout)

@ihamadfuad
Copy link

What is server.pub?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment