Advertisement
fruffl

SSL certificate factory

Nov 12th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. export SCRIPT_PATH=$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )
  4.  
  5. DEFAULT_DOMAIN='reverse.com'
  6. DEFAULT_IP='123.123.123.123'
  7.  
  8. # create certificates
  9. # $ make tls-server example.com ssl
  10. # $ make tls-server sub.example.com ssl
  11. # creates example.com.ssl.tls-server.crt:
  12. # - request created with example.com.tls-server.cnf
  13. # - signed by example.com.ca-tls.crt using config example.com.ca-tls.cnf
  14.  
  15.  
  16. check_result()
  17. {
  18.     if [ $1 -ne 0 ]; then
  19.         echo -e "\e[91m        Error\e[39m $2"
  20.         exit $1
  21.     fi
  22.     echo -e "\e[92m        +\e[39m"
  23. }
  24.  
  25. warn_result()
  26. {
  27.     if [ $1 -ne 0 ]; then
  28.         echo -e "\e[93m        Warning\e[39m $2"
  29.     fi
  30. }
  31.  
  32. check_prompt()
  33. {
  34.     if [ $1 -ne 0 ]; then
  35.         warn_result "$@"
  36.         read -p 'Would you like to continue [y/n]: ' answer
  37.         if [ "$answer" != 'y' ] && [ "$answer" != 'Y'  ]; then
  38.             echo 'Goodbye'
  39.             exit 1
  40.         fi
  41.     fi
  42. }
  43.    
  44. domainPath()
  45. {
  46.     local domain=$1
  47.     local lookup="$SCRIPT_PATH/$DEFAULT_DOMAIN/lookup"
  48.     local path=$lookup/$domain
  49.    
  50.     if [ -e $path ]; then
  51.         echo $(head -n 1 $path)
  52.     fi
  53. }
  54.  
  55. # type: for avail crt-types see crt-*/etc/*.cnf {email|tls-client|tls-server|code-signing}
  56. # domain: a valid domain, stored in /lookup
  57. # fileName: a unique filename. If the file exists, the last version will be revoked as affiliationChanged
  58. #
  59. # files will be stored in ./crt-*/$domain.$filename.$type.ext
  60. # ---
  61. # make tls-server example.com ssl
  62. # ---
  63. make()
  64. {
  65.     local type=$1
  66.     local domain=$2
  67.     local fileName=$3
  68.    
  69.    
  70.     local path=$(domainPath $domain)
  71.     if [ -z $path ]; then
  72.         check_result 1 "domain $domain not found in $SCRIPT_PATH/$DEFAULT_DOMAIN"
  73.     fi
  74.    
  75.     local baseType
  76.     case "$type" in
  77.             email)
  78.                 baseType='email'
  79.                 echo "create $baseType/$type"
  80.                 ;;
  81.             tls-server|tls-client)
  82.                 baseType='tls'
  83.                 echo "create $baseType/$type"
  84.                 ;;
  85.             code-signing)
  86.                 baseType='software'
  87.                 echo "create $baseType/$type"
  88.                 ;;
  89.             *)
  90.                 check_result 1 "invalid request type $type; {email|tls-client|tls-server|code-signing}"
  91.                 exit 1
  92.     esac
  93.    
  94.     local lvl=$(grep -o '/intermediate/' <<< "$path" | wc -l)
  95.     case "$lvl" in
  96.         0)
  97.             export CA_0_SCRIPT_PATH="$path"
  98.             ;;
  99.         1)
  100.             export CA_1_SCRIPT_PATH="$path"
  101.             ;;
  102.         2)
  103.             export CA_2_SCRIPT_PATH="$path"
  104.             ;;
  105.     esac
  106.    
  107.     local caCrl="$path/ca-$baseType/db/$domain.ca-$baseType.crl"
  108.     local caCnf="$path/ca-$baseType/etc/$domain.ca-$baseType.cnf"
  109.     local caPwd="$path/ca-$baseType/private/$domain.ca-$baseType.pwd"
  110.     local csrCnf="$path/crt-$baseType/etc/$domain.$type.cnf"
  111.     local key="$path/crt-$baseType/private/$domain.$fileName.$type.key"
  112.     local csr="$path/crt-$baseType/$domain.$fileName.$type.csr"
  113.     local crt="$path/crt-$baseType/$domain.$fileName.$type.crt"
  114.     local p12="$path/crt-$baseType/$domain.$fileName.$type.p12"
  115.     local chain="$path/$domain.ca-$baseType.chain.pem"
  116.    
  117.     if [ -e $crt ]; then
  118.         local lastSrl=$(openssl x509 -in $crt -serial -noout)
  119.         local lastFnr=$(openssl x509 -in $crt -fingerprint -noout)
  120.        
  121.         check_prompt 1 "certificate $domain.$fileName.$type.crt exists as \n\t$lastSrl\n\t$lastFnr\nRevoke this version and create a new verion."
  122.        
  123.         echo 'Please set the revokation reason, type any other key to continue without setting the CRL reason.'
  124.         echo '[1] unspecified'
  125.         echo '[2] keyCompromise'
  126.         echo '[3] CACompromise'
  127.         echo '[4] affiliationChanged'
  128.         echo '[5] superseded'
  129.         echo '[6] cessationOfOperation'
  130.         echo '[7] certificateHold'
  131.         echo '[8] removeFromCRL'
  132.         read -p 'Please set the revokation reason [1-8]: ' answer
  133.        
  134.         local revokationReason=no
  135.         case "$answer" in
  136.                 1)
  137.                     revokationReason=unspecified
  138.                     ;;
  139.                 2)
  140.                     revokationReason=keyCompromise
  141.                     ;;
  142.                 3)
  143.                     revokationReason=CACompromise
  144.                     ;;
  145.                 4)
  146.                     revokationReason=affiliationChanged
  147.                     ;;
  148.                 5)
  149.                     revokationReason=superseded
  150.                     ;;
  151.                 6)
  152.                     revokationReason=cessationOfOperation
  153.                     ;;
  154.                 7)
  155.                     revokationReason=certificateHold
  156.                     ;;
  157.                 8)
  158.                     revokationReason=removeFromCRL
  159.                     ;;
  160.                 *)
  161.                     warn_result 1 'Nothing matched. Continue without setting a reason.'
  162.         esac
  163.        
  164.         if [ $revokationReason == 'no' ]; then
  165.             openssl ca \
  166.                 -config $caCnf \
  167.                 -revoke $crt \
  168.                 -passin file:$caPwd
  169.         else
  170.             openssl ca \
  171.                 -config $caCnf \
  172.                 -revoke $crt \
  173.                 -passin file:$caPwd \
  174.                 -crl_reason $revokationReason
  175.         fi
  176.        
  177.         openssl ca -gencrl \
  178.             -config $caCnf \
  179.             -out $caCrl \
  180.             -passin file:$caPwd
  181.     fi
  182.    
  183.     # note -subj
  184.     # if you want to use multiple values you should remove the -subj and use the prompt.
  185.     # /O/ST must match, see CA-configs.
  186.     # I prefer to bypass the prompt for ~300 domains with ~2k subdomains.
  187.     case "$type" in
  188.             tls-server)
  189.                 export SAN="DNS:$domain,DNS:*.$domain"
  190.                 openssl req -new \
  191.                     -config $csrCnf \
  192.                     -out $csr \
  193.                     -keyout $key \
  194.                     -subj "/C=BE/ST=Antwerp/O=### Network $domain/CN=$domain"
  195.                
  196.                 openssl ca \
  197.                     -batch \
  198.                     -config $caCnf \
  199.                     -in $csr \
  200.                     -out $crt \
  201.                     -passin file:$caPwd \
  202.                     -extensions server_ext
  203.                
  204.                 openssl pkcs12 -export \
  205.                     -name "$domain (TLS Network Component)" \
  206.                     -inkey $key \
  207.                     -passout pass:\
  208.                     -in $crt \
  209.                     -certfile $chain \
  210.                     -out $p12
  211.                        
  212.                 ;;
  213.                
  214.             *)
  215.                 check_result 1 "invalid request type $type; {email|tls-client|tls-server|code-signing}"
  216.                 exit 1
  217.     esac
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement