freedombone-addcert 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # A script for creating self-signed certificates on Debian
  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. LETSENCRYPT_HOSTNAME=
  31. COUNTRY_CODE="US"
  32. AREA="Free Speech Zone"
  33. LOCATION="Freedomville"
  34. ORGANISATION="Freedombone"
  35. UNIT="Freedombone Unit"
  36. EXTENSIONS=""
  37. NODH=
  38. DH_KEYLENGTH=2048
  39. INSTALL_DIR=/root/build
  40. LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
  41. function show_help {
  42. echo ''
  43. echo 'freedombone-addcert -h [hostname] -c [country code] -a [area] -l [location]'
  44. echo ' -o [organisation] -u [unit] --ca "" --nodh ""'
  45. echo ''
  46. echo 'Creates a self-signed certificate for the given hostname'
  47. echo ''
  48. echo ' --help Show help'
  49. echo ' -h --hostname [name] Hostname'
  50. echo ' -e --letsencrypt [hostname] Hostname to use with Lets Encrypt'
  51. echo ' -s --server [url] Lets Encrypt server URL'
  52. echo ' -c --country [code] Optional country code (eg. US, GB, etc)'
  53. echo ' -a --area [description] Optional area description'
  54. echo ' -l --location [locn] Optional location name'
  55. echo ' -o --organisation [name] Optional organisation name'
  56. echo ' -u --unit [name] Optional unit name'
  57. echo ' --dhkey [bits] DH key length in bits'
  58. echo ' --nodh "" Do not calculate DH params'
  59. echo ' --ca "" Certificate authority cert'
  60. echo ''
  61. exit 0
  62. }
  63. while [[ $# > 1 ]]
  64. do
  65. key="$1"
  66. case $key in
  67. --help)
  68. show_help
  69. ;;
  70. -h|--hostname)
  71. shift
  72. HOSTNAME="$1"
  73. ;;
  74. -e|--letsencrypt)
  75. shift
  76. LETSENCRYPT_HOSTNAME="$1"
  77. ;;
  78. -s|--server)
  79. shift
  80. LETSENCRYPT_SERVER="$1"
  81. ;;
  82. -c|--country)
  83. shift
  84. COUNTRY_CODE="$1"
  85. ;;
  86. -a|--area)
  87. shift
  88. AREA="$1"
  89. ;;
  90. -l|--location)
  91. shift
  92. LOCATION="$1"
  93. ;;
  94. -o|--organisation)
  95. shift
  96. ORGANISATION="$1"
  97. ;;
  98. -u|--unit)
  99. shift
  100. UNIT="$1"
  101. ;;
  102. --ca)
  103. shift
  104. EXTENSIONS="-extensions v3_ca"
  105. ORGANISATION="Freedombone-CA"
  106. ;;
  107. --nodh)
  108. shift
  109. NODH="true"
  110. ;;
  111. --dhkey)
  112. shift
  113. DH_KEYLENGTH=${1}
  114. ;;
  115. *)
  116. # unknown option
  117. ;;
  118. esac
  119. shift
  120. done
  121. if [ ! $HOSTNAME ]; then
  122. if [ ! $LETSENCRYPT_HOSTNAME ]; then
  123. echo 'No hostname specified'
  124. exit 5748
  125. fi
  126. fi
  127. if ! which openssl > /dev/null ;then
  128. echo "$0: openssl is not installed, exiting" 1>&2
  129. exit 5689
  130. fi
  131. if [ ! -d /etc/ssl/mycerts ]; then
  132. mkdir /etc/ssl/mycerts
  133. fi
  134. if [ $LETSENCRYPT_HOSTNAME ]; then
  135. CERTFILE=$LETSENCRYPT_HOSTNAME
  136. if [ ! -d $INSTALL_DIR ]; then
  137. mkdir -p $INSTALL_DIR
  138. fi
  139. cd $INSTALL_DIR
  140. # obtain the repo
  141. if [ ! -d $INSTALL_DIR/letsencrypt ]; then
  142. git clone https://github.com/letsencrypt/letsencrypt
  143. if [ ! -d $INSTALL_DIR/letsencrypt ]; then
  144. exit 76283
  145. fi
  146. else
  147. cd $INSTALL_DIR/letsencrypt
  148. git stash
  149. git pull
  150. fi
  151. cd $INSTALL_DIR/letsencrypt
  152. # TODO this requires user interaction - is there a non-interactive mode?
  153. ./letsencrypt-auto certonly --server $LETSENCRYPT_SERVER --standalone -d $LETSENCRYPT_HOSTNAME
  154. if [ ! "$?" = "0" ]; then
  155. echo "Failed to install letsencrypt for domain $LETSENCRYPT_HOSTNAME"
  156. exit 63216
  157. fi
  158. # replace some legacy filenames
  159. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt ]; then
  160. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  161. fi
  162. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt ]; then
  163. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  164. fi
  165. sed -i "s|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem|g" /etc/nginx/sites-available/$LETSENCRYPT_HOSTNAME
  166. sed -i "s|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem|g" /etc/nginx/sites-available/$LETSENCRYPT_HOSTNAME
  167. # link the private key
  168. if [ -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key ]; then
  169. if [ ! -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old ]; then
  170. mv /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old
  171. fi
  172. fi
  173. ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/privkey.pem /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
  174. # link the public key
  175. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem ]; then
  176. if [ ! -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old ]; then
  177. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old
  178. fi
  179. fi
  180. ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  181. cp /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/mycerts/${LETSENCRYPT_HOSTNAME}.pem
  182. else
  183. CERTFILE=$HOSTNAME
  184. if [[ $ORGANISATION == "Freedombone-CA" ]]; then
  185. CERTFILE="ca-$HOSTNAME"
  186. fi
  187. openssl req -x509 $EXTENSIONS -nodes -days 3650 -sha256 \
  188. -subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" \
  189. -newkey rsa:4096 -keyout /etc/ssl/private/$CERTFILE.key \
  190. -out /etc/ssl/certs/$CERTFILE.crt
  191. chmod 400 /etc/ssl/private/$CERTFILE.key
  192. chmod 640 /etc/ssl/certs/$CERTFILE.crt
  193. cp /etc/ssl/certs/$CERTFILE.crt /etc/ssl/mycerts
  194. fi
  195. # generate DH params
  196. if [ ! $NODH ]; then
  197. if [ ! -f /etc/ssl/certs/$CERTFILE.dhparam ]; then
  198. openssl dhparam -check -text -5 $DH_KEYLENGTH -out /etc/ssl/certs/$CERTFILE.dhparam
  199. chmod 640 /etc/ssl/certs/$CERTFILE.dhparam
  200. fi
  201. fi
  202. if [ -f /etc/init.d/nginx ]; then
  203. /etc/init.d/nginx reload
  204. fi
  205. # Create a bundle of your certificates
  206. cat /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem > /etc/ssl/freedombone-bundle.crt
  207. tar -czvf /etc/ssl/freedombone-certs.tar.gz /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem
  208. exit 0