Created
August 13, 2021 19:20
-
-
Save DinoChiesa/e3ecc49cfde3f3799c50a0331a38a9ff to your computer and use it in GitHub Desktop.
bash script, depends on openssl and perl, to show cert chain subject and issuer for a host. Runs on OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# uncomment to debug | |
# set -x | |
usage() { | |
printf "%s: display subject and issuer of certificates in the chain that a host presents\n" $0 | |
printf "usage:\n" | |
printf " %s HOSTNAME\n" $0 | |
printf " show cert subject and issuer\n\n" | |
exit 0 | |
} | |
showcertinfo() { | |
local servername=$1 | |
local foundSni=$(echo -n | openssl s_client -connect ${servername}:443 -servername ${servername} 2>&1 | openssl x509 -noout -text | grep ${servername}) | |
local opensslcmd | |
if [[ -z "$foundSni" ]] ; then | |
opensslcmd="openssl s_client -showcerts -connect ${servername}:443" | |
else | |
opensslcmd="openssl s_client -showcerts -connect ${servername}:443 -servername ${servername}" | |
fi | |
$opensslcmd < /dev/null 2> /dev/null | perl -e 'while(<>) { | |
if($_ =~ /^ +i:.+$/) { | |
$issuer = $_; | |
chop $issuer; | |
} | |
if($_ =~ /^ [0-9] s:.+$/) { | |
$subject = $_; | |
chop $subject; | |
} | |
if($_ =~ /^\-\-\-\-\-END CERTIFICATE\-+$/) { | |
print " $subject\n"; | |
print " $issuer\n\n"; | |
} | |
}' | |
} | |
if [[ $# -lt 1 ]] | |
then | |
usage | |
fi | |
while test $# -gt 0 | |
do | |
case $1 in | |
# -h|--help|usage|show this usage | |
-h|--help) | |
usage | |
exit 1 | |
;; | |
*) | |
showcertinfo "$1" | |
esac | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment