v-generate-ssl-cert 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. # info: generate self signed certificate and CSR request
  3. # options: DOMAIN EMAIL COUNTRY STATE CITY ORG UNIT [FORMAT]
  4. #
  5. # The function generates self signed SSL certificate and CSR request
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. domain=$1
  11. domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g')
  12. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  13. domain_alias=$domain
  14. email=$2
  15. country=$3
  16. state=$4
  17. city=$5
  18. org=$6
  19. org_unit=$7
  20. format=${8-shell}
  21. KEY_SIZE=2048
  22. DAYS=365
  23. # Includes
  24. source $VESTA/func/main.sh
  25. source $VESTA/conf/vesta.conf
  26. # Json function
  27. json_list_ssl() {
  28. i='1' # iterator
  29. echo '{'
  30. echo -e "\t\"$domain\": {"
  31. echo " \"CRT\": \"$crt\","
  32. echo " \"KEY\": \"$key\","
  33. echo " \"CSR\": \"$csr\""
  34. echo -e "\t}\n}"
  35. }
  36. # Shell function
  37. shell_list_ssl() {
  38. if [ ! -z "$crt" ]; then
  39. echo -e "$crt"
  40. fi
  41. if [ ! -z "$key" ]; then
  42. echo -e "\n$key"
  43. fi
  44. if [ ! -z "$csr" ]; then
  45. echo -e "\n$csr"
  46. fi
  47. }
  48. #----------------------------------------------------------#
  49. # Verifications #
  50. #----------------------------------------------------------#
  51. check_args '7' "$#" 'DOMAIN EMAIL COUNTRY STATE CITY ORG UNIT [FORMAT]'
  52. validate_format 'domain_alias' 'format'
  53. #----------------------------------------------------------#
  54. # Action #
  55. #----------------------------------------------------------#
  56. # Create temporary work directory
  57. workdir=$(mktemp -d)
  58. cd $workdir
  59. # Generate private key
  60. export PASSPHRASE=gen_password
  61. openssl genrsa -des3 \
  62. -out $domain.key \
  63. -passout env:PASSPHRASE $KEY_SIZE 2>/dev/null
  64. # Generate the CSR
  65. subj="/C=$country/ST=$state/localityName=$city/O=$org"
  66. subj="$subj/organizationalUnitName=$org_unit/commonName=$domain"
  67. subj="$subj/emailAddress=$email"
  68. openssl req -sha256\
  69. -new \
  70. -batch \
  71. -subj "$subj" \
  72. -key $domain.key \
  73. -out $domain.csr \
  74. -passin env:PASSPHRASE >/dev/null 2>&1
  75. # Remove passphrase
  76. cp $domain.key $domain.key.tmp
  77. openssl rsa \
  78. -in $domain.key.tmp \
  79. -out $domain.key \
  80. -passin env:PASSPHRASE >/dev/null 2>&1
  81. rm $domain.key.tmp
  82. # Generate the cert 1 year
  83. openssl x509 -req -sha256 \
  84. -days $DAYS \
  85. -in $domain.csr \
  86. -signkey $domain.key \
  87. -out $domain.crt >/dev/null 2>&1
  88. # Listing certificates
  89. if [ -e "$domain.crt" ]; then
  90. crt=$(cat $domain.crt | sed ':a;N;$!ba;s/\n/\\n/g' )
  91. fi
  92. if [ -e "$domain.key" ]; then
  93. key=$(cat $domain.key | sed ':a;N;$!ba;s/\n/\\n/g' )
  94. fi
  95. if [ -e "$domain.csr" ]; then
  96. csr=$(cat $domain.csr | sed ':a;N;$!ba;s/\n/\\n/g' )
  97. fi
  98. case $format in
  99. json) json_list_ssl ;;
  100. plain) nohead=1; shell_list_ssl ;;
  101. shell) shell_list_ssl ;;
  102. *) check_args '1' '0' '[FORMAT]'
  103. esac
  104. # Delete tmp dir
  105. rm -rf $workdir
  106. #----------------------------------------------------------#
  107. # Vesta #
  108. #----------------------------------------------------------#
  109. # Logging
  110. log_event "$OK" "$EVENT"
  111. exit