freedombone-addcert 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. COUNTRY_CODE="US"
  31. AREA="Free Speech Zone"
  32. LOCATION="Freedomville"
  33. ORGANISATION="Freedombone"
  34. UNIT="Freedombone Unit"
  35. EXTENSIONS=""
  36. function show_help {
  37. echo ''
  38. echo 'freedombone-addcert -h [hostname] -c [country code] -a [area] -l [location]'
  39. echo ' -o [organisation] -u [unit]'
  40. echo ''
  41. echo 'Creates a self-signed certificate for the given hostname'
  42. echo ''
  43. echo ' --help Show help'
  44. echo ' -h --hostname [name] Hostname'
  45. echo ' -c --country [code] Optional country code (eg. US, GB, etc)'
  46. echo ' -a --area [description] Optional area description'
  47. echo ' -l --location [locn] Optional location name'
  48. echo ' -o --organisation [name] Optional organisation name'
  49. echo ' -u --unit [name] Optional unit name'
  50. echo ' --ca Certificate authority cert'
  51. echo ''
  52. exit 0
  53. }
  54. while [[ $# > 1 ]]
  55. do
  56. key="$1"
  57. case $key in
  58. --help)
  59. show_help
  60. ;;
  61. -h|--hostname)
  62. shift
  63. HOSTNAME="$1"
  64. ;;
  65. -c|--country)
  66. shift
  67. COUNTRY_CODE="$1"
  68. ;;
  69. -a|--area)
  70. shift
  71. AREA="$1"
  72. ;;
  73. -l|--location)
  74. shift
  75. LOCATION="$1"
  76. ;;
  77. -o|--organisation)
  78. shift
  79. ORGANISATION="$1"
  80. ;;
  81. -u|--unit)
  82. shift
  83. UNIT="$1"
  84. ;;
  85. --ca)
  86. EXTENSIONS="-extensions v3_ca"
  87. ;;
  88. *)
  89. # unknown option
  90. ;;
  91. esac
  92. shift
  93. done
  94. if [ ! $HOSTNAME ]; then
  95. echo 'No hostname specified'
  96. exit 5748
  97. fi
  98. if ! which openssl > /dev/null ;then
  99. echo "$0: openssl is not installed, exiting" 1>&2
  100. exit 5689
  101. fi
  102. openssl req -x509 $EXTENSIONS -nodes -days 3650 -sha256 -subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" -newkey rsa:4096 -keyout /etc/ssl/private/$HOSTNAME.key -out /etc/ssl/certs/$HOSTNAME.crt
  103. openssl dhparam -check -text -5 1024 -out /etc/ssl/certs/$HOSTNAME.dhparam
  104. chmod 400 /etc/ssl/private/$HOSTNAME.key
  105. chmod 640 /etc/ssl/certs/$HOSTNAME.crt
  106. chmod 640 /etc/ssl/certs/$HOSTNAME.dhparam
  107. if [ -f /etc/init.d/nginx ]; then
  108. /etc/init.d/nginx reload
  109. fi
  110. # add the public certificate to a separate directory
  111. # so that we can redistribute it easily
  112. if [ ! -d /etc/ssl/mycerts ]; then
  113. mkdir /etc/ssl/mycerts
  114. fi
  115. cp /etc/ssl/certs/$HOSTNAME.crt /etc/ssl/mycerts
  116. # Create a bundle of your certificates
  117. cat /etc/ssl/mycerts/*.crt > /etc/ssl/freedombone-bundle.crt
  118. tar -czvf /etc/ssl/freedombone-certs.tar.gz /etc/ssl/mycerts/*.crt
  119. exit 0