freedombone-addcert 3.4KB

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