Skip to content

Instantly share code, notes, and snippets.

@zeehio
Last active January 31, 2025 09:58
Show Gist options
  • Save zeehio/8a11138d4d19ac705a80a2f35de9db74 to your computer and use it in GitHub Desktop.
Save zeehio/8a11138d4d19ac705a80a2f35de9db74 to your computer and use it in GitHub Desktop.
Extract certificate chain from a URL you visit
#!/bin/bash
# Derived from https://unix.stackexchange.com/a/487546/378468
URL=www.google.com
openssl s_client -showcerts -verify 5 -servername "$URL" -connect "$URL":443 < /dev/null | \
awk '/BEGIN CERTIFICATE/,/END CERTIFICATE/{ if(/BEGIN CERTIFICATE/){a++}; out="cert"a".pem"; print >out}'
for cert in *.pem; do
newname=$(openssl x509 -noout -subject -in $cert | sed -nE 's/.*CN ?= ?([^,]*).*/\1/; s/[ ,.*]/_/g; s/__/_/g; s/_-_/-/; s/^_//g;p' | tr '[:upper:]' '[:lower:]').pem
echo "${newname}";
mv "${cert}" "${newname}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment