Skip to content

Commit 2291d6f

Browse files
committed
Dynamically create CA for issuing the *.dev.openconext.local server certificate from start-dev-env.sh that is trusted by browsers
- Do not keep the CA private key so issuing additional certificates is not possible - Restrict the CA to issuing for the .dev.openconext.local domain - Set lifetime to the CA/Broswer maxium acceptable length - Use ECC keys, typical for modern CAs Because the this CA is restricted to the .dev.openconext.local domain, is safe to add to your OS/browser trust store. This allows e.g. testing webauthn in browsers that require a valid certificate.
1 parent c7dc2ce commit 2291d6f

7 files changed

Lines changed: 125 additions & 332 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ stepup/.env
33
stepup/tests/behat/temp.sql
44
/.idea
55
.env
6+
core/haproxy/haproxy.crt
7+
core/haproxy/haproxy.pem
8+
stepup/haproxy/haproxy.crt

‎core/haproxy/haproxy.crt‎

Lines changed: 0 additions & 123 deletions
This file was deleted.

‎core/haproxy/haproxy.pem‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

‎core/scripts/create_dev_ca.sh‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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"

‎core/start-dev-env.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ while [[ $# -gt 0 ]]; do
154154
esac
155155
done
156156

157+
# Check if HAProxy certs exists, if not generate them
158+
if [[ ! -f "$SCRIPT_DIR/haproxy/haproxy.crt" || ! -f "$SCRIPT_DIR/haproxy/haproxy.crt" ]]; then
159+
echo -e "${GREEN}Generating a new CA and HAProxy server certificate${NOCOLOR}"
160+
"${SCRIPT_DIR}/scripts/create_dev_ca.sh"
161+
echo -e "${GREEN}Generated new CA and HAProxy server certificate${NOCOLOR}"
162+
fi
163+
157164
# Run docker compose up command with the previously prepared options
158165
echo "Running: docker compose ${docker_compose_options[*]} up ${docker_compose_up_options[*]}"
159166
exec docker compose "${docker_compose_options[@]}" up "${docker_compose_up_options[@]}"

0 commit comments

Comments
 (0)