freedombone-renew-cert 7.6KB

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