freedombone-dhparam 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Creates or re-calculates Diffie-Hellman parameters
  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}-dhparam
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. HOSTNAME=
  33. KEYLENGTH=2048
  34. RECALCULATE="no"
  35. FAST=''
  36. function show_help {
  37. echo ''
  38. echo $"${PROJECT_NAME}-dhparam -h [hostname] -l [length in bits] --recalc [yes|no] --fast [yes|no]"
  39. echo ''
  40. exit 0
  41. }
  42. function calc_dh {
  43. openssl dhparam -check -text $FAST $KEYLENGTH -out ${1}
  44. if [ ! "$?" = "0" ]; then
  45. exit 3674
  46. fi
  47. chmod 640 ${1}
  48. }
  49. function regenerate_dh_keys {
  50. for file in /etc/ssl/mycerts/*
  51. do
  52. if [[ -f $file ]]; then
  53. filename=/etc/ssl/certs/$(echo $file | awk -F '/etc/ssl/mycerts/' '{print $2}' | awk -F '.crt' '{print $1}').dhparam
  54. if [ -f $filename ]; then
  55. calc_dh $filename
  56. echo $"Recalculated DH params for $filename"
  57. fi
  58. fi
  59. done
  60. }
  61. while [[ $# > 1 ]]
  62. do
  63. key="$1"
  64. case $key in
  65. --help)
  66. show_help
  67. ;;
  68. -h|--hostname)
  69. shift
  70. HOSTNAME="$1"
  71. ;;
  72. -l|--dhkey)
  73. shift
  74. KEYLENGTH=${1}
  75. ;;
  76. --recalc)
  77. shift
  78. RECALCULATE=${1}
  79. ;;
  80. --fast)
  81. shift
  82. if [[ ${1} == "yes" || ${1} == "y" ]]; then
  83. FAST='-dsaparam'
  84. fi
  85. ;;
  86. *)
  87. # unknown option
  88. ;;
  89. esac
  90. shift
  91. done
  92. if [[ $RECALCULATE == "yes" || $RECALCULATE == "y" ]]; then
  93. regenerate_dh_keys
  94. exit 0
  95. fi
  96. if [ ! $HOSTNAME ]; then
  97. echo $'No hostname specified'
  98. exit 5728
  99. fi
  100. if ! which openssl > /dev/null ;then
  101. echo $"$0: openssl is not installed, exiting" 1>&2
  102. exit 5689
  103. fi
  104. if [ ! -d /etc/ssl/mycerts ]; then
  105. mkdir -p /etc/ssl/mycerts
  106. fi
  107. calc_dh /etc/ssl/certs/$HOSTNAME.dhparam
  108. systemctl reload nginx
  109. exit 0