freedombone-renew-cert 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 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 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 General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU 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. 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. ${PROJECT_NAME}-addcert -e $HOSTNAME -s $LETSENCRYPT_SERVER --dhkey $DH_KEYLENGTH
  52. if [ ! "$?" = "0" ]; then
  53. echo $"Unable to add Let's encrypt certificate"
  54. exit 6328
  55. fi
  56. else
  57. echo $"Renewing Let's Encrypt certificate"
  58. letsencrypt renew \
  59. --cert-path /etc/letsencrypt/live/${HOSTNAME}/fullchain.pem \
  60. --key-path /etc/letsencrypt/live/${HOSTNAME}/privkey.pem
  61. if [ ! "$?" = "0" ]; then
  62. echo $"Unable to renew Let's encrypt certificate"
  63. exit 2624
  64. fi
  65. fi
  66. # Ensure that links are in place
  67. ln -s /etc/letsencrypt/live/${HOSTNAME}/privkey.pem /etc/ssl/private/${HOSTNAME}.key
  68. ln -s /etc/letsencrypt/live/${HOSTNAME}/fullchain.pem /etc/ssl/certs/${HOSTNAME}.pem
  69. ${PROJECT_NAME}-pin-cert $HOSTNAME
  70. }
  71. function renew_startssl {
  72. echo $'Renewing StartSSL certificate'
  73. if [ -s /etc/ssl/certs/$HOSTNAME.new.crt ]; then
  74. if ! grep -q "-BEGIN CERTIFICATE-" /etc/ssl/certs/$HOSTNAME.new.crt; then
  75. echo $'/etc/ssl/certs/$HOSTNAME.new.crt does not contain a public key'
  76. return
  77. fi
  78. cp /etc/ssl/certs/$HOSTNAME.new.crt /etc/ssl/certs/$HOSTNAME.crt
  79. if [ ! -d /etc/ssl/roots ]; then
  80. mkdir /etc/ssl/roots
  81. fi
  82. if [ ! -d /etc/ssl/chains ]; then
  83. mkdir /etc/ssl/chains
  84. fi
  85. # download intermediate certs
  86. wget "http://www.startssl.com/certs/ca.pem" --output-document="/etc/ssl/roots/startssl-root.ca"
  87. wget "http://www.startssl.com/certs/sub.class1.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class1.server.ca.pem"
  88. wget "http://www.startssl.com/certs/sub.class2.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class2.server.ca.pem"
  89. wget "http://www.startssl.com/certs/sub.class3.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class3.server.ca.pem"
  90. ln -s "/etc/ssl/roots/startssl-root.ca" "/etc/ssl/roots/$HOSTNAME-root.ca"
  91. ln -s "/etc/ssl/chains/startssl-sub.class1.server.ca.pem" "/etc/ssl/chains/$HOSTNAME.ca"
  92. cp "/etc/ssl/certs/$HOSTNAME.crt" "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  93. test -e "/etc/ssl/chains/$HOSTNAME.ca" && cat "/etc/ssl/chains/$HOSTNAME.ca" >> "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  94. test -e "/etc/ssl/roots/$HOSTNAME-root.ca" && cat "/etc/ssl/roots/$HOSTNAME-root.ca" >> "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  95. # remove the password from the private cert
  96. openssl rsa -in /etc/ssl/private/$HOSTNAME.key -out /etc/ssl/private/$HOSTNAME.new.key
  97. cp /etc/ssl/private/$HOSTNAME.new.key /etc/ssl/private/$HOSTNAME.key
  98. shred -zu /etc/ssl/private/$HOSTNAME.new.key
  99. # bundle the cert
  100. cat /etc/ssl/certs/$HOSTNAME.crt /etc/ssl/chains/startssl-sub.class1.server.ca.pem > /etc/ssl/certs/$HOSTNAME.bundle.crt
  101. # add it to mycerts
  102. cp /etc/ssl/certs/$HOSTNAME.bundle.crt /etc/ssl/mycerts
  103. cat /etc/ssl/mycerts/*.crt > /etc/ssl/${PROJECT_NAME}-bundle.crt
  104. tar -czvf /etc/ssl/${PROJECT_NAME}-certs.tar.gz /etc/ssl/mycerts/*.crt
  105. # create backups
  106. if [ ! -d /etc/ssl/backups ]; then
  107. mkdir /etc/ssl/backups
  108. fi
  109. if [ ! -d /etc/ssl/backups/certs ]; then
  110. mkdir /etc/ssl/backups/certs
  111. fi
  112. if [ ! -d /etc/ssl/backups/private ]; then
  113. mkdir /etc/ssl/backups/private
  114. fi
  115. cp /etc/ssl/certs/$HOSTNAME* /etc/ssl/backups/certs/
  116. cp /etc/ssl/private/$HOSTNAME* /etc/ssl/backups/private/
  117. chmod -R 400 /etc/ssl/backups/certs/*
  118. chmod -R 400 /etc/ssl/backups/private/*
  119. rm /etc/ssl/certs/$HOSTNAME.new.crt
  120. rm /etc/ssl/requests/$HOSTNAME.csr
  121. # update your site to include the bundle
  122. sed -i "s|$HOSTNAME.crt|$HOSTNAME.bundle.crt|g" /etc/nginx/sites-available/$HOSTNAME
  123. echo $'Certificate installed'
  124. service nginx restart
  125. return
  126. fi
  127. if [ -f /etc/ssl/requests/$HOSTNAME.csr ]; then
  128. echo $'Certificate request already created:'
  129. echo ''
  130. cat /etc/ssl/requests/$HOSTNAME.csr
  131. echo ''
  132. echo $"Save the requested public key to /etc/ssl/certs/$HOSTNAME.new.crt"
  133. echo $'then run this command again.'
  134. echo ''
  135. return
  136. fi
  137. openssl genrsa -out /etc/ssl/private/$HOSTNAME.new.key 2048
  138. chown root:ssl-cert /etc/ssl/private/$HOSTNAME.new.key
  139. chmod 440 /etc/ssl/private/$HOSTNAME.new.key
  140. if [ ! -d /etc/ssl/requests ]; then
  141. mkdir /etc/ssl/requests
  142. fi
  143. openssl req -new -sha256 -key /etc/ssl/private/$HOSTNAME.new.key -out /etc/ssl/requests/$HOSTNAME.csr
  144. echo ''
  145. cat /etc/ssl/requests/$HOSTNAME.csr
  146. echo ''
  147. echo $'On the StartSSL site select Certificates Wizard then'
  148. echo $'Web server SSL/TLS Certificate. You can then click on "skip"'
  149. echo $'and then copy and paste the above certificate request into the text'
  150. echo $'entry box. You may now need to wait a few hours for a confirmation'
  151. echo $'email indicating that the new certificate was created.'
  152. echo ''
  153. echo $'Once you have retrieved the new public certificate paste it to:'
  154. echo $"/etc/ssl/certs/$HOSTNAME.new.crt then run this command again."
  155. echo ''
  156. ${PROJECT_NAME}-pin-cert $HOSTNAME
  157. }
  158. while [[ $# > 1 ]]
  159. do
  160. key="$1"
  161. case $key in
  162. --help)
  163. show_help
  164. ;;
  165. -h|--hostname)
  166. shift
  167. HOSTNAME="$1"
  168. ;;
  169. -p|--provider)
  170. shift
  171. PROVIDER="$1"
  172. ;;
  173. *)
  174. # unknown option
  175. ;;
  176. esac
  177. shift
  178. done
  179. if [ ! $HOSTNAME ]; then
  180. echo $'No hostname specified'
  181. exit 5748
  182. fi
  183. if ! which openssl > /dev/null ;then
  184. echo $"$0: openssl is not installed, exiting" 1>&2
  185. exit 5689
  186. fi
  187. # check that the web site exists
  188. if [ ! -f /etc/nginx/sites-available/$HOSTNAME ]; then
  189. echo $"/etc/nginx/sites-available/$HOSTNAME does not exist"
  190. exit 7598
  191. fi
  192. if [[ $PROVIDER == 'startssl' || $PROVIDER == 'StartSSL' ]]; then
  193. renew_startssl
  194. else
  195. if [[ $PROVIDER == 'letsencrypt' ]]; then
  196. renew_letsencrypt
  197. else
  198. echo $"$PROVIDER is not currently supported"
  199. fi
  200. fi
  201. exit 0