Wednesday, 8 June 2016

How to Validate SSL Certificates with bash script?

Prerequisites

  1. SSL Certificates
  2. OpensSSL to be Installed


Step1: Prompt for the SSL Certificates File Paths

echo "Reference"
echo "---------"
echo "enduser-example.com.crt
enduser-example.com.key
enduser-example.com.chain"
echo ""
echo ""
echo "Please Provide full Path for Certificate (enduser-example.com.crt)"

            if [ -e $PATH_CERT_FILE ];then
                    #Do Nothing
               echo ""
            else
                    echo "Wrong Path"
                exit 1
            fi

echo "Please Provide full path for Intermediate Certificate (enduser-example.com.chain|gd_bundle.crt)"
            read PATH_INTERMEDIATE_CERT
                        if [ -e $PATH_CERT_FILE ];then
                                #Do Nothing
                                echo ""
                        else
                                echo "Wrong Path"
                                exit 1
                        fi

echo "Please Provide the full path for Private Key File(enduser-example.com.key)"
            read PATH_KEY_FILE
                        if [ -e $PATH_CERT_FILE ];then
                                #Do Nothing
                                echo ""
                        else
                                echo "Wrong Path"
                                exit 1
                        fi

Step2: validate the certificates with OpenSSL tool

#Validating Certificates
_status=$(openssl verify -CAfile $PATH_INTERMEDIATE_CERT  $PATH_CERT_FILE | grep OK)
            result=$(echo $_status | cut -f2 -d ':')
            if [ "$result" != " OK" ]; then
                echo "Please Verify The Certificates and Try Again.."
                exit 1
            fi

How to change VHost Name & respective SSL Certificates with Bash Script?

Step1: change the VHost ServerName/Domain Name with sed
sed -i "s@OldDomain.*@NewDomain@g"

Step2: Prompt for the New Certificates Absolute Path& Validate them from above Procedure
Step3: On Successful validation, copy the certificate files to new Folder.

            #Configuring the Certificates
            _date=$(date +"%H%M%S")
            mkdir -p /etc/apache2/$_date
            echo $_date
            cp $PATH_CERT_FILE    /etc/apache2/"$_date"/
            cp $PATH_INTERMEDIATE_CERT /etc/apache2/"$_date"/
            cp $PATH_KEY_FILE  /etc/apache2/"$_date"/       


Step4: Copy the absolute path of each certificate to a variable.
            _CERT_FILE=$(echo "$PATH_CERT_FILE"| rev |cut -f1 -d '/' |rev)
            NEW_PATH_CERT_FILE=/etc/apache2/"$_date"/"$_CERT_FILE"

                _INTERMEDIATE_CERT_FILE=$(echo "$PATH_INTERMEDIATE_CERT"| rev |cut -f1 -d '/' |rev)
            NEW_PATH_INTERMEDIATE_CERT=/etc/apache2/"$_date"/"$_INTERMEDIATE_CERT_FILE"

            _KEY_FILE=$(echo "$PATH_KEY_FILE"| rev |cut -f1 -d '/' |rev)    
            NEW_PATH_KEY_FILE=/etc/apache2/"$_date"/"$_KEY_FILE"

Step5: Replace the SSL File Paths in Apache Configuration files.
            #Seetting New SSL Paths in Apache Configuration
            sed -i "s@SSLCertificateFile.*@SSLCertificateFile  $NEW_PATH_CERT_FILE@g"  /etc/apache2/sites-available/zerooneprograms.conf
            sed -i "s@SSLCertificateKeyFile.*@SSLCertificateKeyFile  $NEW_PATH_KEY_FILE@g"  /etc/apache2/sites-available/zerooneprograms.conf
            sed -i "s@SSLCertificateChainFile.*@SSLCertificateChainFile  $NEW_PATH_INTERMEDIATE_CERT@g"  /etc/apache2/sites-available/zerooneprograms.conf

Step6: Check the Apache Syntax and restart the web server.

No comments:

Post a Comment