freedombone-renew-cert 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. HOSTNAME=
  30. PROVIDER='startssl'
  31. function show_help {
  32. echo ''
  33. echo 'freedombone-renew-cert -h [hostname] -p [provider]'
  34. echo ''
  35. echo 'Makes it easier to renew a ssl/tls certificate for a website'
  36. echo ''
  37. echo ' --help Show help'
  38. echo ' -h --hostname [name] Hostname'
  39. echo ' -p --provider [name] eg. startssl'
  40. echo ''
  41. exit 0
  42. }
  43. function renew_startssl {
  44. echo 'Renewing StartSSL certificate'
  45. if [ -s /etc/ssl/certs/$HOSTNAME.new.crt ]; then
  46. if ! grep -q "-BEGIN CERTIFICATE-" /etc/ssl/certs/$HOSTNAME.new.crt; then
  47. echo '/etc/ssl/certs/$HOSTNAME.new.crt does not contain a public key'
  48. return
  49. fi
  50. cp /etc/ssl/certs/$HOSTNAME.new.crt /etc/ssl/certs/$HOSTNAME.crt
  51. if [ ! -d /etc/ssl/roots ]; then
  52. mkdir /etc/ssl/roots
  53. fi
  54. if [ ! -d /etc/ssl/chains ]; then
  55. mkdir /etc/ssl/chains
  56. fi
  57. # download intermediate certs
  58. wget "http://www.startssl.com/certs/ca.pem" --output-document="/etc/ssl/roots/startssl-root.ca"
  59. wget "http://www.startssl.com/certs/sub.class1.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class1.server.ca.pem"
  60. wget "http://www.startssl.com/certs/sub.class2.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class2.server.ca.pem"
  61. wget "http://www.startssl.com/certs/sub.class3.server.ca.pem" --output-document="/etc/ssl/chains/startssl-sub.class3.server.ca.pem"
  62. ln -s "/etc/ssl/roots/startssl-root.ca" "/etc/ssl/roots/$HOSTNAME-root.ca"
  63. ln -s "/etc/ssl/chains/startssl-sub.class1.server.ca.pem" "/etc/ssl/chains/$HOSTNAME.ca"
  64. cp "/etc/ssl/certs/$HOSTNAME.crt" "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  65. test -e "/etc/ssl/chains/$HOSTNAME.ca" && cat "/etc/ssl/chains/$HOSTNAME.ca" >> "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  66. test -e "/etc/ssl/roots/$HOSTNAME-root.ca" && cat "/etc/ssl/roots/$HOSTNAME-root.ca" >> "/etc/ssl/certs/$HOSTNAME.crt+chain+root"
  67. # remove the password from the private cert
  68. openssl rsa -in /etc/ssl/private/$HOSTNAME.key -out /etc/ssl/private/$HOSTNAME.new.key
  69. cp /etc/ssl/private/$HOSTNAME.new.key /etc/ssl/private/$HOSTNAME.key
  70. shred -zu /etc/ssl/private/$HOSTNAME.new.key
  71. # bundle the cert
  72. cat /etc/ssl/certs/$HOSTNAME.crt /etc/ssl/chains/startssl-sub.class1.server.ca.pem > /etc/ssl/certs/$HOSTNAME.bundle.crt
  73. # add it to mycerts
  74. cp /etc/ssl/certs/$HOSTNAME.bundle.crt /etc/ssl/mycerts
  75. cat /etc/ssl/mycerts/*.crt > /etc/ssl/freedombone-bundle.crt
  76. tar -czvf /etc/ssl/freedombone-certs.tar.gz /etc/ssl/mycerts/*.crt
  77. # create backups
  78. if [ ! -d /etc/ssl/backups ]; then
  79. mkdir /etc/ssl/backups
  80. fi
  81. if [ ! -d /etc/ssl/backups/certs ]; then
  82. mkdir /etc/ssl/backups/certs
  83. fi
  84. if [ ! -d /etc/ssl/backups/private ]; then
  85. mkdir /etc/ssl/backups/private
  86. fi
  87. cp /etc/ssl/certs/$HOSTNAME* /etc/ssl/backups/certs/
  88. cp /etc/ssl/private/$HOSTNAME* /etc/ssl/backups/private/
  89. chmod -R 400 /etc/ssl/backups/certs/*
  90. chmod -R 400 /etc/ssl/backups/private/*
  91. rm /etc/ssl/certs/$HOSTNAME.new.crt
  92. rm /etc/ssl/requests/$HOSTNAME.csr
  93. # update your site to include the bundle
  94. sed -i "s|$HOSTNAME.crt|$HOSTNAME.bundle.crt|g" /etc/nginx/sites-available/$HOSTNAME
  95. echo 'Certificate installed'
  96. service nginx restart
  97. return
  98. fi
  99. if [ -f /etc/ssl/requests/$HOSTNAME.csr ]; then
  100. echo 'Certificate request already created:'
  101. echo ''
  102. cat /etc/ssl/requests/$HOSTNAME.csr
  103. echo ''
  104. echo "Save the requested public key to /etc/ssl/certs/$HOSTNAME.new.crt"
  105. echo 'then run this command again.'
  106. echo ''
  107. return
  108. fi
  109. openssl genrsa -out /etc/ssl/private/$HOSTNAME.new.key 2048
  110. chown root:ssl-cert /etc/ssl/private/$HOSTNAME.new.key
  111. chmod 440 /etc/ssl/private/$HOSTNAME.new.key
  112. if [ ! -d /etc/ssl/requests ]; then
  113. mkdir /etc/ssl/requests
  114. fi
  115. openssl req -new -sha256 -key /etc/ssl/private/$HOSTNAME.new.key -out /etc/ssl/requests/$HOSTNAME.csr
  116. echo ''
  117. cat /etc/ssl/requests/$HOSTNAME.csr
  118. echo ''
  119. echo 'On the StartSSL site select Certificates Wizard then'
  120. echo 'Web server SSL/TLS Certificate. You can then click on "skip"'
  121. echo 'and then copy and paste the above certificate request into the text'
  122. echo 'entry box. You may now need to wait a few hours for a confirmation'
  123. echo 'email indicating that the new certificate was created.'
  124. echo ''
  125. echo 'Once you have retrieved the new public certificate paste it to:'
  126. echo "/etc/ssl/certs/$HOSTNAME.new.crt then run this command again."
  127. echo ''
  128. }
  129. while [[ $# > 1 ]]
  130. do
  131. key="$1"
  132. case $key in
  133. --help)
  134. show_help
  135. ;;
  136. -h|--hostname)
  137. shift
  138. HOSTNAME="$1"
  139. ;;
  140. -p|--provider)
  141. shift
  142. PROVIDER="$1"
  143. ;;
  144. *)
  145. # unknown option
  146. ;;
  147. esac
  148. shift
  149. done
  150. if [ ! $HOSTNAME ]; then
  151. echo 'No hostname specified'
  152. exit 5748
  153. fi
  154. if ! which openssl > /dev/null ;then
  155. echo "$0: openssl is not installed, exiting" 1>&2
  156. exit 5689
  157. fi
  158. # check that the web site exists
  159. if [ ! -f /etc/nginx/sites-available/$HOSTNAME ]; then
  160. echo "/etc/nginx/sites-available/$HOSTNAME does not exist"
  161. exit 7598
  162. fi
  163. if [[ $PROVIDER == 'startssl' || $PROVIDER == 'StartSSL' ]]; then
  164. renew_startssl
  165. else
  166. echo "$PROVIDER is not currently supported"
  167. fi
  168. exit 0