OpenSSL – Decode/Read CSR

Sometimes you when a SSL CSR isn’t named properly you need to get the details of it, the code below will help.

Note: Depending on version and OS the placement numbers (-f5 for example) may be slightly off.

#!/bin/sh

#  ReadSSLCSR.sh
#  
#
#  Created by David Kittell on 1/5/18.
#

csrFile="$1"
csrText=$(openssl req -in $csrFile -noout -subject)
csrCountry=$(echo $csrText | cut -d '/' -f2 | cut -d '=' -f2)
csrState=$(echo $csrText | cut -d '/' -f3 | cut -d '=' -f2)
csrCity=$(echo $csrText | cut -d '/' -f4 | cut -d '=' -f2)
csrOrganization=$(echo $csrText | cut -d '/' -f5 | cut -d '=' -f2)
csrDepartment=$(echo $csrText | cut -d '/' -f6 | cut -d '=' -f2)
csrCN=$(echo $csrText | cut -d '/' -f7 | cut -d '=' -f2)
csrEmail=$(echo $csrText | cut -d '/' -f8 | cut -d '=' -f2)

clear
echo "CSR Details Of $csrFile:"
echo "Country:       $csrCountry"
echo "State:         $csrState"
echo "City:          $csrCity"
echo "Organization:  $csrOrganization"
echo "Department:    $csrDepartment"
echo "Common Name:   $csrCN"
echo "Email Address: $csrEmail"

Once you have created the shell script call to it with the code below. Note: the file extension isn’t really necessary as long as the text is truly a CSR.

sh ReadSSLCSR.sh ~/RandomCSR.csr
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.