#!/bin/bash
if [ ! -x /usr/bin/diff ]; then
        echo "This script requires OpenSSL and GNU Diffutils."
        exit 1
fi
if [ ! -x /usr/bin/openssl ]; then
        echo "This script requires OpenSSL and GNU Diffutils."
        exit 1
fi

echo "This script assumes that your Certificate Request (CSR), Certificate, and Private Key are all in the same directory."
read -p "Press enter to continue or Ctrl+C to quit." continue

read -p "Certificates Path (eg. /etc/pki/ssl): " path
read -p "CSR Filename: " csrin
read -p "Certificate Filename: " certin
read -p "Private Key Filename: " keyin
echo ""

attr=0

csr=$(openssl req -noout -modulus -in $path/$csrin | openssl md5 | sed -e 's/(stdin)= //g')
cert=$(openssl x509 -noout -modulus -in $path/$certin | openssl md5 | sed -e 's/(stdin)= //g')
key=$(openssl rsa -noout -modulus -in $path/$keyin | openssl md5 | sed -e 's/(stdin)= //g')

if [ $csr == $cert ]; then
        attr=$((attr+1))
fi
if [ $cert == $key ]; then
        attr=$((attr+10))
fi
if [ $csr == $key ]; then
        attr=$((attr+100))
fi

if [ $attr == 0 ]; then
        echo "Matched: None"
        echo "NOT Matched: CSR, Certificate, Private Key"
elif [ $attr == 1 ]; then
        echo "Matched: CSR, Certificate"
        echo "NOT Matched: Private Key"
elif [ $attr == 10 ]; then
        echo "Matched: Certificate, Private Key"
        echo "NOT Matched: CSR"
elif [ $attr == 100 ]; then
        echo "Matched: CSR, Private Key"
        echo "NOT Matched: Certificate"
elif [ $attr == 111 ]; then
        echo "Matched: CSR, Certificate, Private Key"
        echo "NOT Matched: None"
fi
