freedombone-pin-cert 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Performs certificate pinning (HPKP) on a given domain name
  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. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-pin-cert
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. DOMAIN_NAME=$1
  33. KEY_FILENAME=/etc/ssl/private/${DOMAIN_NAME}.key
  34. SITE_FILENAME=/etc/nginx/sites-available/${DOMAIN_NAME}
  35. if [ ! -f "$KEY_FILENAME" ]; then
  36. echo $"No certificate found for $DOMAIN_NAME"
  37. exit 1
  38. fi
  39. if [ ! -f "$SITE_FILENAME" ]; then
  40. exit 0
  41. fi
  42. KEY_HASH=$(openssl rsa -in $KEY_FILENAME -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64)
  43. PIN_HEADER="add_header Public-Key-Pins 'pin-sha256=\"${KEY_HASH}\"; max-age=5184000; includeSubDomains';"
  44. if ! grep -q "add_header Public-Key-Pins" $SITE_FILENAME; then
  45. sed -i "/ssl_ciphers.*/a $PIN_HEADER" $SITE_FILENAME
  46. else
  47. sed -i "s/add_header Public-Key-Pins.*/$PIN_HEADER/g" $SITE_FILENAME
  48. fi
  49. systemctl restart nginx
  50. if ! grep -q "add_header Public-Key-Pins" $SITE_FILENAME; then
  51. echo $'Pinning failed'
  52. fi
  53. echo "Pinned $DOMAIN_NAME with hash $KEY_HASH"
  54. exit 0