freedombone-renew-cert 7.8KB

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