From f23e4e0d0cf48153dbc5134cf1bf1bb7189c3005 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Tue, 8 Dec 2015 19:50:09 +0100 Subject: Various fixes. --- letsencrypt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'letsencrypt') diff --git a/letsencrypt b/letsencrypt index 50406f7..5c10ea1 100755 --- a/letsencrypt +++ b/letsencrypt @@ -16,7 +16,7 @@ declare COMMAND ACCOUNTKEY declare -l GENKEY declare RUNAS QUIET= DEBUG= -declare SRVCERT= CHAIN= CSR SRVKEY +declare SRVCRT= CHAIN= CSR SRVKEY declare -l HASH= declare SUBJECT=/ declare SAN= @@ -96,7 +96,7 @@ while [ $# -gt 0 ]; do --quiet|-q) QUIET=1;; --debug) DEBUG=1;; - --output=*) SRVCERT="${1#*=}";; + --output=*) SRVCRT="${1#*=}";; --chain) CHAIN=1;; --csr=*) CSR="${1#*=}";; --key=*) SRVKEY="${1#*=}";; @@ -160,7 +160,7 @@ if [ "$COMMAND" = 'revoke-cert' ]; then exit 1 fi elif [ "$COMMAND" = 'new-cert' ]; then - if [ ! "${SRVCERT:-}" ]; then + if [ ! "${SRVCRT:-}" ]; then echo "Error: Missing --output" >&2 exit 1 fi @@ -270,10 +270,12 @@ if [ "$COMMAND" = 'new-cert' ]; then # TODO # Verify: dump and compare public keys # Valid cert, signed by the right CA - - # Copy "$x509" to "$SRVCERT", possibly chained # https://crt.sh/?q=cse-fresti.cse.chalmers.se&iCAID=7395 - cp "$x509" "$SRVCERT" + + # if it doesn't exist, create the file with mode 0644 minus the process's umask(2) + [ -e "$SRVCRT" ] || touch "$SRVCRT" + cat "$x509" >"$SRVCRT" + [ ! "$DEBUG" ] || openssl x509 -noout -text <"$SRVCRT" for (( i=0; i<${#NOTIFY[@]}; i++ )); do ${NOTIFY[$i]} -- cgit v1.2.3 From 594b65f8960c38fde1a8ffd567df852a69a670ff Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Wed, 9 Dec 2015 00:10:14 +0100 Subject: Add an option --min-age. --- letsencrypt | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'letsencrypt') diff --git a/letsencrypt b/letsencrypt index 5c10ea1..4c932e4 100755 --- a/letsencrypt +++ b/letsencrypt @@ -17,6 +17,7 @@ declare -l GENKEY declare RUNAS QUIET= DEBUG= declare SRVCRT= CHAIN= CSR SRVKEY +delcale -i MIN_AGE=0 declare -l HASH= declare SUBJECT=/ declare SAN= @@ -62,6 +63,8 @@ usage() { (default: "digitalSignature,keyEncipherment,keyCertSign") --chain Store not only the server certificate in the file specified with --output, but also the CA's + --min-age=SECONDS Don't do anything if the certificate specified by --output exists and its expiration + is more than SECONDS ahead. --output=FILE Where to store the issued (signed) X.509 certificate --notify=COMMAND Command to run upon success. (This option can be repeated.) @@ -97,6 +100,7 @@ while [ $# -gt 0 ]; do --debug) DEBUG=1;; --output=*) SRVCRT="${1#*=}";; + --min-age=*) MIN_AGE="${1#*=}";; --chain) CHAIN=1;; --csr=*) CSR="${1#*=}";; --key=*) SRVKEY="${1#*=}";; @@ -164,6 +168,12 @@ elif [ "$COMMAND" = 'new-cert' ]; then echo "Error: Missing --output" >&2 exit 1 fi + if [ -s "$SRVCRT" ] && \ + exp=$(openssl x509 -noout -enddate <"$SRVCRT" 2>/dev/null) && \ + [ $(( $(date -d "${exp#*=}" +%s) - $(date +%s))) -gt $MIN_AGE ]; then + [ ! "$DEBUG" ] || echo "Expiration date ($(date -d"${exp#*=}")) is too far away, come back later." >&2 + exit 0 + fi # Generate a Certificate Signing Request if need be if [ ${CSR+x} ]; then -- cgit v1.2.3 From 8eb679e08e08b4bb992c8d43f57c3458aab4cf1a Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Wed, 9 Dec 2015 00:17:20 +0100 Subject: Ship LE's intermediate CA files and add cert validation. --- letsencrypt | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'letsencrypt') diff --git a/letsencrypt b/letsencrypt index 4c932e4..7465378 100755 --- a/letsencrypt +++ b/letsencrypt @@ -11,13 +11,14 @@ WWW_USER=www-data WWW_GROUP=www-data ACME_WEBSERVER=acme-webserver ACME_CLIENT=acme-slave +CAfile=/usr/share/lets-encrypt/letsencryptauthorityx1.pem declare COMMAND ACCOUNTKEY declare -l GENKEY declare RUNAS QUIET= DEBUG= declare SRVCRT= CHAIN= CSR SRVKEY -delcale -i MIN_AGE=0 +declare -i MIN_AGE=0 declare -l HASH= declare SUBJECT=/ declare SAN= @@ -168,7 +169,7 @@ elif [ "$COMMAND" = 'new-cert' ]; then echo "Error: Missing --output" >&2 exit 1 fi - if [ -s "$SRVCRT" ] && \ + if [ -s "$SRVCRT" ] && [ $MIN_AGE -gt 0 ] && \ exp=$(openssl x509 -noout -enddate <"$SRVCRT" 2>/dev/null) && \ [ $(( $(date -d "${exp#*=}" +%s) - $(date +%s))) -gt $MIN_AGE ]; then [ ! "$DEBUG" ] || echo "Expiration date ($(date -d"${exp#*=}")) is too far away, come back later." >&2 @@ -277,17 +278,35 @@ while read data; do done >"$pipe" if [ "$COMMAND" = 'new-cert' ]; then - # TODO - # Verify: dump and compare public keys - # Valid cert, signed by the right CA - # https://crt.sh/?q=cse-fresti.cse.chalmers.se&iCAID=7395 + # https://crt.sh/?q=mail.fripost.org&iCAID=7395 + # https://crt.sh/?spkisha1=$sha1 + + # Ensure the cert's pubkey matches that of the CSR, and that it's signed by the intended CA + if [ ! -s "$x509" ] || + ! diff <(openssl req -in "$CSR" -pubkey -noout) \ + <(openssl x509 -in "$x509" -pubkey -noout) >/dev/null || + ! openssl verify -CAfile "$CAfile" -purpose sslserver -x509_strict <"$x509" >/dev/null; then + echo "Error: Got an invalid X.509 certificate from the ACME server!" >&2 + exit 1 + fi # if it doesn't exist, create the file with mode 0644 minus the process's umask(2) [ -e "$SRVCRT" ] || touch "$SRVCRT" cat "$x509" >"$SRVCRT" [ ! "$DEBUG" ] || openssl x509 -noout -text <"$SRVCRT" + if [ ! "$QUIET" ]; then + echo "X.509 certificate $SRVCRT has been updated or renewed" + echo + openssl x509 -noout \ + -text -certopt no_header,no_version,no_pubkey,no_sigdump \ + -fingerprint -sha256 <"$SRVCRT" + fi + for (( i=0; i<${#NOTIFY[@]}; i++ )); do ${NOTIFY[$i]} done + +else + [ "$QUIET" ] || echo OK fi -- cgit v1.2.3