freedombone-addcert 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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-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 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}-addcert
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. HOSTNAME=
  33. LETSENCRYPT_HOSTNAME=
  34. COUNTRY_CODE="US"
  35. AREA="Free Speech Zone"
  36. LOCATION="Freedomville"
  37. ORGANISATION="Freedombone"
  38. UNIT="Freedombone Unit"
  39. EXTENSIONS=""
  40. NODH=
  41. DH_KEYLENGTH=2048
  42. INSTALL_DIR=/root/build
  43. LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
  44. LETSENCRYPT_REPO='https://github.com/letsencrypt/letsencrypt'
  45. function show_help {
  46. echo ''
  47. echo $"${PROJECT_NAME}-addcert -h [hostname] -c [country code] -a [area] -l [location]"
  48. echo $' -o [organisation] -u [unit] --ca "" --nodh ""'
  49. echo ''
  50. echo $'Creates a self-signed certificate for the given hostname'
  51. echo ''
  52. echo $' --help Show help'
  53. echo $' -h --hostname [name] Hostname'
  54. echo $' -e --letsencrypt [hostname] Hostname to use with Lets Encrypt'
  55. echo $' -s --server [url] Lets Encrypt server URL'
  56. echo $' -c --country [code] Optional country code (eg. US, GB, etc)'
  57. echo $' -a --area [description] Optional area description'
  58. echo $' -l --location [locn] Optional location name'
  59. echo $' -o --organisation [name] Optional organisation name'
  60. echo $' -u --unit [name] Optional unit name'
  61. echo $' --dhkey [bits] DH key length in bits'
  62. echo $' --nodh "" Do not calculate DH params'
  63. echo $' --ca "" Certificate authority cert'
  64. echo ''
  65. exit 0
  66. }
  67. while [[ $# > 1 ]]
  68. do
  69. key="$1"
  70. case $key in
  71. --help)
  72. show_help
  73. ;;
  74. -h|--hostname)
  75. shift
  76. HOSTNAME="$1"
  77. ;;
  78. -e|--letsencrypt)
  79. shift
  80. LETSENCRYPT_HOSTNAME="$1"
  81. ;;
  82. -s|--server)
  83. shift
  84. LETSENCRYPT_SERVER="$1"
  85. ;;
  86. -c|--country)
  87. shift
  88. COUNTRY_CODE="$1"
  89. ;;
  90. -a|--area)
  91. shift
  92. AREA="$1"
  93. ;;
  94. -l|--location)
  95. shift
  96. LOCATION="$1"
  97. ;;
  98. -o|--organisation)
  99. shift
  100. ORGANISATION="$1"
  101. ;;
  102. -u|--unit)
  103. shift
  104. UNIT="$1"
  105. ;;
  106. --ca)
  107. shift
  108. EXTENSIONS="-extensions v3_ca"
  109. ORGANISATION="Freedombone-CA"
  110. ;;
  111. --nodh)
  112. shift
  113. NODH="true"
  114. ;;
  115. --dhkey)
  116. shift
  117. DH_KEYLENGTH=${1}
  118. ;;
  119. *)
  120. # unknown option
  121. ;;
  122. esac
  123. shift
  124. done
  125. if [ ! $HOSTNAME ]; then
  126. if [ ! $LETSENCRYPT_HOSTNAME ]; then
  127. echo $'No hostname specified'
  128. exit 5748
  129. fi
  130. fi
  131. if ! which openssl > /dev/null ;then
  132. echo $"$0: openssl is not installed, exiting" 1>&2
  133. exit 5689
  134. fi
  135. if [ ! -d /etc/ssl/mycerts ]; then
  136. mkdir /etc/ssl/mycerts
  137. fi
  138. CERTFILE=$HOSTNAME
  139. function add_cert_letsencrypt {
  140. CERTFILE=$LETSENCRYPT_HOSTNAME
  141. if [ ! -d $INSTALL_DIR ]; then
  142. mkdir -p $INSTALL_DIR
  143. fi
  144. cd $INSTALL_DIR
  145. # obtain the repo
  146. if [ ! -d ${INSTALL_DIR}/letsencrypt ]; then
  147. git clone $LETSENCRYPT_REPO
  148. if [ ! -d ${INSTALL_DIR}/letsencrypt ]; then
  149. exit 76283
  150. fi
  151. else
  152. cd ${INSTALL_DIR}/letsencrypt
  153. git stash
  154. git pull
  155. fi
  156. # stop the web server
  157. systemctl stop nginx
  158. cd ${INSTALL_DIR}/letsencrypt
  159. ./letsencrypt-auto certonly --server $LETSENCRYPT_SERVER --standalone -d $LETSENCRYPT_HOSTNAME --renew-by-default
  160. if [ ! "$?" = "0" ]; then
  161. echo $"Failed to install letsencrypt for domain $LETSENCRYPT_HOSTNAME"
  162. systemctl start nginx
  163. exit 63216
  164. fi
  165. # replace some legacy filenames
  166. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt ]; then
  167. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  168. fi
  169. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt ]; then
  170. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  171. fi
  172. 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
  173. 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
  174. # link the private key
  175. if [ -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key ]; then
  176. if [ ! -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old ]; then
  177. mv /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old
  178. else
  179. rm -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
  180. fi
  181. fi
  182. ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/privkey.pem /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
  183. # link the public key
  184. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem ]; then
  185. if [ ! -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old ]; then
  186. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old
  187. else
  188. rm -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  189. fi
  190. fi
  191. ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  192. cp /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/mycerts/${LETSENCRYPT_HOSTNAME}.pem
  193. systemctl start nginx
  194. ${PROJECT_NAME}-pin-cert $LETSENCRYPT_HOSTNAME
  195. if [ ! "$?" = "0" ]; then
  196. echo $"Certificate for $LETSENCRYPT_HOSTNAME could not be pinned"
  197. exit 62878
  198. fi
  199. }
  200. function add_cert_selfsigned {
  201. if [[ $ORGANISATION == "Freedombone-CA" ]]; then
  202. CERTFILE="ca-$HOSTNAME"
  203. fi
  204. openssl req -x509 ${EXTENSIONS} -nodes -days 3650 -sha256 \
  205. -subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" \
  206. -newkey rsa:4096 -keyout /etc/ssl/private/${CERTFILE}.key \
  207. -out /etc/ssl/certs/${CERTFILE}.crt
  208. chmod 400 /etc/ssl/private/${CERTFILE}.key
  209. chmod 640 /etc/ssl/certs/${CERTFILE}.crt
  210. cp /etc/ssl/certs/${CERTFILE}.crt /etc/ssl/mycerts
  211. ${PROJECT_NAME}-pin-cert $CERTFILE
  212. if [ ! "$?" = "0" ]; then
  213. echo $"Certificate for $CERTFILE could not be pinned"
  214. exit 62879
  215. fi
  216. }
  217. function generate_dh_params {
  218. if [ ! $NODH ]; then
  219. if [ ! -f /etc/ssl/certs/${CERTFILE}.dhparam ]; then
  220. ${PROJECT_NAME}-dhparam -h ${CERTFILE} --fast yes
  221. fi
  222. fi
  223. }
  224. function restart_web_server {
  225. if [ -f /etc/init.d/nginx ]; then
  226. /etc/init.d/nginx reload
  227. fi
  228. }
  229. function make_cert_bundle {
  230. # Create a bundle of your certificates
  231. cat /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem > /etc/ssl/${PROJECT_NAME}-bundle.crt
  232. tar -czvf /etc/ssl/${PROJECT_NAME}-certs.tar.gz /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem
  233. }
  234. function create_cert {
  235. if [ $LETSENCRYPT_HOSTNAME ]; then
  236. add_cert_letsencrypt
  237. else
  238. add_cert_selfsigned
  239. fi
  240. }
  241. create_cert
  242. generate_dh_params
  243. restart_web_server
  244. make_cert_bundle
  245. exit 0