2015-03-06 09:12:26 -05:00
|
|
|
#!/usr/bin/env bash
|
2014-10-28 14:55:42 -04:00
|
|
|
|
|
|
|
trap "exit 1" TERM
|
|
|
|
export TOP_PID=$$
|
|
|
|
|
|
|
|
readonly PROGRAM_NAME=$(basename $0)
|
|
|
|
readonly PROGRAM_LOC=$(readlink -m $(dirname $0))
|
|
|
|
readonly ARGS="$@"
|
|
|
|
readonly ARG_COUNT="$#"
|
|
|
|
|
|
|
|
error () {
|
|
|
|
local readonly message=$1; shift
|
|
|
|
|
|
|
|
echo $message
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
file_error () {
|
|
|
|
local readonly file=$1; shift
|
|
|
|
|
|
|
|
error "File $file does not exist"
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_error () {
|
|
|
|
local readonly file=$1; shift
|
|
|
|
local readonly cert_type=$1; shift
|
|
|
|
|
|
|
|
error "File $file is not a $cert_type file"
|
|
|
|
}
|
|
|
|
|
|
|
|
usage () {
|
|
|
|
echo usage: $POGRAM_NAME CERTIFICATE_FILE KEY_FILE CSR_FILE
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
md5 () {
|
|
|
|
local readonly modulus=$1; shift
|
|
|
|
|
|
|
|
echo $modulus \
|
|
|
|
| openssl md5
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_certificate () {
|
|
|
|
local readonly certificate=$1; shift
|
|
|
|
out=$( openssl x509 -noout -modulus -in $certificate 2>/dev/null )
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
hash_error $certificate certificate
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $(md5 $out)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_key () {
|
|
|
|
local readonly key=$1; shift
|
|
|
|
out=$( openssl rsa -noout -modulus -in $key 2>/dev/null )
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
hash_error $key key
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $(md5 $out)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_csr () {
|
|
|
|
local readonly csr=$1; shift
|
|
|
|
out=$( openssl req -noout -modulus -in $csr 2>/dev/null )
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
hash_error $csr csr
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $(md5 $out)
|
|
|
|
}
|
|
|
|
|
|
|
|
incorrect_number_of_arguments () {
|
|
|
|
test $ARG_COUNT -ne 3
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
file? () {
|
2014-10-28 14:55:42 -04:00
|
|
|
local readonly file=$1
|
|
|
|
|
|
|
|
[[ -f $file ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
main () {
|
|
|
|
local readonly certificate=$1; shift
|
|
|
|
local readonly key=$1; shift
|
|
|
|
local readonly csr=$1; shift
|
|
|
|
|
|
|
|
if incorrect_number_of_arguments; then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
if ! file? $certificate; then
|
2014-10-28 14:55:42 -04:00
|
|
|
file_error $certificate
|
|
|
|
fi
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
if ! file? $key; then
|
2014-10-28 14:55:42 -04:00
|
|
|
file_error $key
|
|
|
|
fi
|
|
|
|
|
2015-03-06 09:12:26 -05:00
|
|
|
if ! file? $csr; then
|
2014-10-28 14:55:42 -04:00
|
|
|
file_error $csr
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $(hash_certificate $certificate)
|
|
|
|
echo $(hash_key $key)
|
|
|
|
echo $(hash_csr $csr)
|
|
|
|
}
|
|
|
|
|
|
|
|
main $ARGS
|