|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Create a private CA that issues one (wildcard) server certificate |
| 6 | +# Reduce the risks introduced by (accidentally) adding the CA certificate to a developer's trust store: |
| 7 | +# - Delete the CA's private key after issuing the server certificate to prevent additional certificates |
| 8 | +# from being issued (i.e. signed) by the CA |
| 9 | +# - Add a critical nameConstraint extension to the CA certificate restricting it to localhost and the |
| 10 | +# intended domain |
| 11 | +# |
| 12 | +# This script works on OSX and Linux. Requires bash >= 4, openssl >= 3 |
| 13 | + |
| 14 | +#### Configuration #### |
| 15 | +# Will issue a wildcard (star) TLS server certificate for this DNS domain |
| 16 | +# Do not add the "*". E.g. "example.com" |
| 17 | +CERT_DOMAIN='dev.openconext.local' |
| 18 | + |
| 19 | +# Validity of the TLS server certificate in days |
| 20 | +# The current CA/browser forum limit for end-entity certificates is 398 |
| 21 | +CERT_VALID_DAYS=398 |
| 22 | + |
| 23 | +# A name to identity the CA. The CA_NAME is used as part of the CA's Subject DN. |
| 24 | +# E.g. "ACME dev CA" |
| 25 | +CA_NAME="OpenConext dev CA" |
| 26 | +#### End configuration #### |
| 27 | + |
| 28 | +# Create a temporary working dir for holding private key, etc |
| 29 | +# Add a trap to ensure it is deleted |
| 30 | +TMPDIR="$(mktemp -d "devca.XXXXXXXX")" |
| 31 | +chmod 700 "$TMPDIR" |
| 32 | +trap 'rm -rf "$TMPDIR"' EXIT INT TERM |
| 33 | + |
| 34 | +# Add the current date and time (UTC) to the CA_NAME to make it identifiable |
| 35 | +CA_NAME+=" ($(date -u '+%Y-%m-%d %H:%M:%S UTC'))" |
| 36 | + |
| 37 | +# Nameconstraint extension for in the CA certificate |
| 38 | +NC_EXT="critical" # critical: A client MUST fail if it does not understand this extension |
| 39 | +NC_EXT+=",permitted;DNS:.${CERT_DOMAIN}" # Allow issuing for DNS domain only |
| 40 | +NC_EXT+=",excluded;IP:0.0.0.0/0.0.0.0" # Exclude all IPv4 |
| 41 | +NC_EXT+=",excluded;IP:0:0:0:0:0:0:0:0/0:0:0:0:0:0:0:0" # Exclude all IPv6 |
| 42 | + |
| 43 | +CA_VALID_DAYS=$((CERT_VALID_DAYS + 1)) # Nr of days that the CA certificate is valid |
| 44 | + |
| 45 | +# Create the CA |
| 46 | +# Use secp384r1 (NIST P-384) for the key and sign with SHA-384 |
| 47 | +openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:secp384r1 -noenc \ |
| 48 | + -keyout "$TMPDIR/ca.key" -out "$TMPDIR/ca.crt" \ |
| 49 | + -days "$CA_VALID_DAYS" -sha384 \ |
| 50 | + -subj "/CN=${CA_NAME}" \ |
| 51 | + -addext "basicConstraints=critical,CA:TRUE,pathlen:0" \ |
| 52 | + -addext "keyUsage=critical,keyCertSign" \ |
| 53 | + -addext "nameConstraints=${NC_EXT}" \ |
| 54 | + -addext "subjectKeyIdentifier=hash" \ |
| 55 | + -config /dev/null |
| 56 | +chmod 400 "$TMPDIR/ca.key" |
| 57 | + |
| 58 | +# Print CA certificate |
| 59 | +openssl x509 -in "$TMPDIR/ca.crt" -noout -text |
| 60 | + |
| 61 | +# Create the TLS server certificate wildcard certificate for the domain |
| 62 | +# Use the P-256 (NIST) curve |
| 63 | +openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc \ |
| 64 | + -keyout "$TMPDIR/cert.key" -out "$TMPDIR/cert.crt" \ |
| 65 | + -CA "$TMPDIR/ca.crt" -CAkey "$TMPDIR/ca.key" \ |
| 66 | + -days "$CERT_VALID_DAYS" -sha256 \ |
| 67 | + -subj "/CN=*.${CERT_DOMAIN}" \ |
| 68 | + -addext "keyUsage=critical,digitalSignature" \ |
| 69 | + -addext "extendedKeyUsage=serverAuth" \ |
| 70 | + -addext "subjectAltName=DNS:*.${CERT_DOMAIN}" \ |
| 71 | + -addext "subjectKeyIdentifier=hash" \ |
| 72 | + -addext "authorityKeyIdentifier=keyid:always" \ |
| 73 | + -config /dev/null |
| 74 | +chmod 400 "$TMPDIR/cert.key" |
| 75 | + |
| 76 | +# We don't want to issue any more certificates with this CA |
| 77 | +rm -f "$TMPDIR/ca.key" |
| 78 | + |
| 79 | +# Print the server certificate |
| 80 | +openssl x509 -in "$TMPDIR/cert.crt" -noout -text |
| 81 | + |
| 82 | +# Copy certificate into the haproxy config |
| 83 | +# haproxy.crt: the CA certificate for others to trust, this pushed into the containers at startup |
| 84 | +# haproxy.pem: the server certificate and key for haproxy itself. |
| 85 | +# The order of the components must for haproxy must be: |
| 86 | +# - Server certificate |
| 87 | +# - Intermediate (which we do not have) |
| 88 | +# - CA certificate |
| 89 | +# - Server certificate private key |
| 90 | +SCRIPT_DIR=$(dirname $0) |
| 91 | +HAPROXY_CORE_DIR="$(CDPATH= cd -- "$SCRIPT_DIR/../haproxy" && pwd -P)" |
| 92 | + |
| 93 | +# CA Cert for use by the containers |
| 94 | +cp "$TMPDIR/ca.crt" "$HAPROXY_CORE_DIR/haproxy.crt" |
| 95 | +chmod 644 "$HAPROXY_CORE_DIR/haproxy.crt" |
| 96 | +echo "Wrote $HAPROXY_CORE_DIR/haproxy.crt" |
| 97 | + |
| 98 | +# HAProxy cert |
| 99 | +cp "$TMPDIR/cert.crt" "$HAPROXY_CORE_DIR/haproxy.pem" |
| 100 | +chmod 644 "$HAPROXY_CORE_DIR/haproxy.crt" |
| 101 | +cat "$TMPDIR/ca.crt" >> "$HAPROXY_CORE_DIR/haproxy.pem" |
| 102 | +cat "$TMPDIR/cert.key" >> "$HAPROXY_CORE_DIR/haproxy.pem" |
| 103 | +echo "Wrote $HAPROXY_CORE_DIR/haproxy.pem" |
| 104 | + |
| 105 | +rm -rf "$TMPDIR" |
0 commit comments