freedombone-renew-cert 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # A script for renewing SSL/TLS certificates
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-renew-cert
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. HOSTNAME=
  32. PROVIDER='startssl'
  33. DH_KEYLENGTH=2048
  34. LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
  35. INSTALL_DIR=/root/build
  36. function show_help {
  37. echo ''
  38. echo $"${PROJECT_NAME}-renew-cert -h [hostname] -p [provider]"
  39. echo ''
  40. echo $'Makes it easier to renew a ssl/tls certificate for a website'
  41. echo ''
  42. echo $' --help Show help'
  43. echo $' -h --hostname [name] Hostname'
  44. echo $' -p --provider [name] eg. startssl/letsencrypt'
  45. echo ''
  46. exit 0
  47. }
  48. function renew_letsencrypt {
  49. if [ ! -f /etc/letsencrypt/live/${HOSTNAME}/fullchain.pem ]; then
  50. echo $"Adding Let's Encrypt certificate"
  51. else
  52. echo $"Renewing Let's Encrypt certificate"
  53. fi
  54. if ! ${PROJECT_NAME}-addcert -e $HOSTNAME -s $LETSENCRYPT_SERVER --dhkey $DH_KEYLENGTH; then
  55. echo $"Unable to add Let's encrypt certificate"
  56. exit 6328
  57. fi
  58. # Ensure that links are in place
  59. ln -s /etc/letsencrypt/live/${HOSTNAME}/privkey.pem /etc/ssl/private/${HOSTNAME}.key
  60. ln -s /etc/letsencrypt/live/${HOSTNAME}/fullchain.pem /etc/ssl/certs/${HOSTNAME}.pem
  61. ${PROJECT_NAME}-pin-cert $HOSTNAME remove
  62. }
  63. function renew_startssl {
  64. echo $'Renewing StartSSL certificate'
  65. if [ -s /etc/ssl/certs/$HOSTNAME.new.crt ]; then
  66. if ! grep -q "-BEGIN CERTIFICATE-" /etc/ssl/certs/$HOSTNAME.new.crt; then
  67. echo $'/etc/ssl/certs/$HOSTNAME.new.crt does not contain a public key'
  68. return
  69. fi
  70. cp /etc/ssl/certs/$HOSTNAME.new.crt /etc/ssl/certs/$HOSTNAME.crt
  71. if [ ! -d /etc/ssl/roots ]; then
  72. mkdir /etc/ssl/roots
  73. fi
  74. if [ ! -d /etc/ssl/chains ]; then
  75. mkdir /etc/ssl/chains
  76. fi
  77. # download intermediate certs
  78. wget "http://www.startssl.com/certs/ca.pem" --output-document="/etc/ssl/roots/startssl-root.ca"
  79. wget "http://www.startssl.com/certs/sub.class1.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class1.server.ca.pem"
  80. wget "http://www.startssl.com/certs/sub.class2.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class2.server.ca.pem"
  81. wget "http://www.startssl.com/certs/sub.class3.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class3.server.ca.pem"
  82. ln -s "/etc/ssl/roots/startssl-root.ca" "/etc/ssl/roots/$HOSTNAME-root.ca"
  83. ln -s "/etc/ssl/chains/startssl-sub.class1.server.ca.pem" "/etc/ssl/chains/$HOSTNAME.ca"
  84. cp "/etc/ssl/certs/$HOSTNAME.crt" "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  85. test -e "/etc/ssl/chains/$HOSTNAME.ca" && cat "/etc/ssl/chains/$HOSTNAME.ca" >> "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  86. test -e "/etc/ssl/roots/$HOSTNAME-root.ca" && cat "/etc/ssl/roots/$HOSTNAME-root.ca" >> "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  87. # remove the password from the private cert
  88. openssl rsa -in /etc/ssl/private/$HOSTNAME.key -out /etc/ssl/private/$HOSTNAME.new.key
  89. cp /etc/ssl/private/$HOSTNAME.new.key /etc/ssl/private/$HOSTNAME.key
  90. shred -zu /etc/ssl/private/$HOSTNAME.new.key
  91. # bundle the cert
  92. cat /etc/ssl/certs/$HOSTNAME.crt /etc/ssl/chains/startssl-sub.class1.server.ca.pem > /etc/ssl/certs/$HOSTNAME.bundle.crt
  93. # add it to mycerts
  94. cp /etc/ssl/certs/$HOSTNAME.bundle.crt /etc/ssl/mycerts
  95. cat /etc/ssl/mycerts/*.crt > /etc/ssl/${PROJECT_NAME}-bundle.crt
  96. tar -czvf /etc/ssl/${PROJECT_NAME}-certs.tar.gz /etc/ssl/mycerts/*.crt
  97. # create backups
  98. if [ ! -d /etc/ssl/backups ]; then
  99. mkdir /etc/ssl/backups
  100. fi
  101. if [ ! -d /etc/ssl/backups/certs ]; then
  102. mkdir /etc/ssl/backups/certs
  103. fi
  104. if [ ! -d /etc/ssl/backups/private ]; then
  105. mkdir /etc/ssl/backups/private
  106. fi
  107. cp /etc/ssl/certs/$HOSTNAME* /etc/ssl/backups/certs/
  108. cp /etc/ssl/private/$HOSTNAME* /etc/ssl/backups/private/
  109. chmod -R 400 /etc/ssl/backups/certs/*
  110. chmod -R 400 /etc/ssl/backups/private/*
  111. rm /etc/ssl/certs/$HOSTNAME.new.crt
  112. rm /etc/ssl/requests/$HOSTNAME.csr
  113. # update your site to include the bundle
  114. sed -i "s|$HOSTNAME.crt|$HOSTNAME.bundle.crt|g" /etc/nginx/sites-available/$HOSTNAME
  115. echo $'Certificate installed'
  116. systemctl restart nginx
  117. return
  118. fi
  119. if [ -f /etc/ssl/requests/$HOSTNAME.csr ]; then
  120. echo $'Certificate request already created:'
  121. echo ''
  122. cat /etc/ssl/requests/$HOSTNAME.csr
  123. echo ''
  124. echo $"Save the requested public key to /etc/ssl/certs/$HOSTNAME.new.crt"
  125. echo $'then run this command again.'
  126. echo ''
  127. return
  128. fi
  129. openssl genrsa -out /etc/ssl/private/$HOSTNAME.new.key 2048
  130. chown root:ssl-cert /etc/ssl/private/$HOSTNAME.new.key
  131. chmod 440 /etc/ssl/private/$HOSTNAME.new.key
  132. if [ ! -d /etc/ssl/requests ]; then
  133. mkdir /etc/ssl/requests
  134. fi
  135. openssl req -new -sha256 -key /etc/ssl/private/$HOSTNAME.new.key -out /etc/ssl/requests/$HOSTNAME.csr
  136. echo ''
  137. cat /etc/ssl/requests/$HOSTNAME.csr
  138. echo ''
  139. echo $'On the StartSSL site select Certificates Wizard then'
  140. echo $'Web server SSL/TLS Certificate. You can then click on "skip"'
  141. echo $'and then copy and paste the above certificate request into the text'
  142. echo $'entry box. You may now need to wait a few hours for a confirmation'
  143. echo $'email indicating that the new certificate was created.'
  144. echo ''
  145. echo $'Once you have retrieved the new public certificate paste it to:'
  146. echo $"/etc/ssl/certs/$HOSTNAME.new.crt then run this command again."
  147. echo ''
  148. ${PROJECT_NAME}-pin-cert $HOSTNAME remove
  149. }
  150. while [ $# -gt 1 ]
  151. do
  152. key="$1"
  153. case $key in
  154. --help)
  155. show_help
  156. ;;
  157. -h|--hostname)
  158. shift
  159. HOSTNAME="$1"
  160. ;;
  161. -p|--provider)
  162. shift
  163. PROVIDER="$1"
  164. ;;
  165. *)
  166. # unknown option
  167. ;;
  168. esac
  169. shift
  170. done
  171. if [ ! "$HOSTNAME" ]; then
  172. echo $'No hostname specified'
  173. exit 5748
  174. fi
  175. if ! which openssl > /dev/null ;then
  176. echo $"$0: openssl is not installed, exiting" 1>&2
  177. exit 5689
  178. fi
  179. # check that the web site exists
  180. if [ ! -f "/etc/nginx/sites-available/$HOSTNAME" ]; then
  181. echo $"/etc/nginx/sites-available/$HOSTNAME does not exist"
  182. exit 7598
  183. fi
  184. if [[ $PROVIDER == 'startssl' || $PROVIDER == 'StartSSL' ]]; then
  185. renew_startssl
  186. else
  187. if [[ $PROVIDER == 'letsencrypt' ]]; then
  188. renew_letsencrypt
  189. else
  190. echo $"$PROVIDER is not currently supported"
  191. fi
  192. fi
  193. exit 0