Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Created August 13, 2021 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinoChiesa/e3ecc49cfde3f3799c50a0331a38a9ff to your computer and use it in GitHub Desktop.
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
#!/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