Skip to content

Instantly share code, notes, and snippets.

@bferg
Created February 27, 2012 19:15
Show Gist options
  • Save bferg/1926416 to your computer and use it in GitHub Desktop.
Save bferg/1926416 to your computer and use it in GitHub Desktop.
Compare SSL files' key references
#!/bin/bash
#
# Compare SSL files' key references.
# Take a list of SSL certificate (.crt), certificate sign request
# (.csr) and/or key (.key) files, extract the modulus and compare, and
# return a nonzero exit code if they do not match.
#
check=''
for name in "$@"; do
if [ "${name: -3}" == "crt" ]; then
op="x509"
fi
if [ "${name: -3}" == "csr" ]; then
op="req"
fi
if [ "${name: -3}" == "key" ]; then
op="rsa"
fi
csum=`openssl $op -noout -modulus -in $name | openssl md5`
if [ ${#check} -eq 0 ]; then
check=$csum
else
if [ "$check" != "$csum" ]; then
echo "Key mismatch"
exit 1
fi
fi
done
echo "Keys match"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment