2015-12-10 16:31:56 +01:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
# Performs certificate pinning (HPKP) on a given domain name
|
|
|
|
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
2016-01-02 22:58:27 +01:00
|
|
|
# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
|
2015-12-10 16:31:56 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2016-02-13 23:09:27 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2015-12-10 16:31:56 +01:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-02-13 23:09:27 +01:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2015-12-10 16:31:56 +01:00
|
|
|
#
|
2016-02-13 23:09:27 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-10 16:31:56 +01:00
|
|
|
|
|
|
|
PROJECT_NAME='freedombone'
|
|
|
|
|
|
|
|
export TEXTDOMAIN=${PROJECT_NAME}-pin-cert
|
|
|
|
export TEXTDOMAINDIR="/usr/share/locale"
|
|
|
|
|
|
|
|
DOMAIN_NAME=$1
|
|
|
|
KEY_FILENAME=/etc/ssl/private/${DOMAIN_NAME}.key
|
|
|
|
SITE_FILENAME=/etc/nginx/sites-available/${DOMAIN_NAME}
|
|
|
|
|
|
|
|
if [ ! -f "$KEY_FILENAME" ]; then
|
|
|
|
echo $"No certificate found for $DOMAIN_NAME"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f "$SITE_FILENAME" ]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
KEY_HASH=$(openssl rsa -in $KEY_FILENAME -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64)
|
|
|
|
|
|
|
|
PIN_HEADER="add_header Public-Key-Pins 'pin-sha256=\"${KEY_HASH}\"; max-age=5184000; includeSubDomains';"
|
|
|
|
if ! grep -q "add_header Public-Key-Pins" $SITE_FILENAME; then
|
2015-12-10 16:38:30 +01:00
|
|
|
sed -i "/ssl_ciphers.*/a $PIN_HEADER" $SITE_FILENAME
|
2015-12-10 16:31:56 +01:00
|
|
|
else
|
|
|
|
sed -i "s/add_header Public-Key-Pins.*/$PIN_HEADER/g" $SITE_FILENAME
|
|
|
|
fi
|
|
|
|
|
|
|
|
systemctl restart nginx
|
|
|
|
|
2015-12-10 16:37:02 +01:00
|
|
|
if ! grep -q "add_header Public-Key-Pins" $SITE_FILENAME; then
|
|
|
|
echo $'Pinning failed'
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Pinned $DOMAIN_NAME with hash $KEY_HASH"
|
|
|
|
|
2015-12-10 16:31:56 +01:00
|
|
|
exit 0
|