173 lines
4.0 KiB
Bash
173 lines
4.0 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
umask 077
|
|
|
|
PKI_DIR="${LAB7_PKI_DIR:-$HOME/.local/share/mtusi2026lab7/pki}"
|
|
|
|
mkdir -p "$PKI_DIR"
|
|
chmod 700 "$PKI_DIR"
|
|
|
|
# Never overwrite an existing CA.
|
|
if [[ -e "$PKI_DIR/ca.key" || -e "$PKI_DIR/ca.crt" ]]; then
|
|
echo "ERROR: PKI already exists at $PKI_DIR"
|
|
echo "Refusing to replace existing keys."
|
|
exit 1
|
|
fi
|
|
|
|
for tool in openssl mktemp; do
|
|
command -v "$tool" >/dev/null || {
|
|
echo "Missing tool: $tool"
|
|
exit 1
|
|
}
|
|
done
|
|
|
|
WORK_DIR="$(mktemp -d)"
|
|
trap 'rm -rf "$WORK_DIR"' EXIT
|
|
|
|
create_ca() {
|
|
local prefix="$1"
|
|
local common_name="$2"
|
|
|
|
openssl genpkey \
|
|
-algorithm RSA \
|
|
-pkeyopt rsa_keygen_bits:3072 \
|
|
-out "$PKI_DIR/$prefix.key" 2>/dev/null
|
|
|
|
openssl req -x509 -new -sha256 \
|
|
-days 365 \
|
|
-key "$PKI_DIR/$prefix.key" \
|
|
-out "$PKI_DIR/$prefix.crt" \
|
|
-subj "/CN=$common_name" \
|
|
-addext "basicConstraints=critical,CA:TRUE,pathlen:0" \
|
|
-addext "keyUsage=critical,keyCertSign,cRLSign"
|
|
|
|
chmod 600 "$PKI_DIR/$prefix.key"
|
|
chmod 644 "$PKI_DIR/$prefix.crt"
|
|
}
|
|
|
|
create_leaf() {
|
|
local name="$1"
|
|
local common_name="$2"
|
|
local san="$3"
|
|
local usage="$4"
|
|
local ca_prefix="${5:-ca}"
|
|
|
|
openssl genpkey \
|
|
-algorithm RSA \
|
|
-pkeyopt rsa_keygen_bits:2048 \
|
|
-out "$PKI_DIR/$name.key" 2>/dev/null
|
|
|
|
openssl req -new -sha256 \
|
|
-key "$PKI_DIR/$name.key" \
|
|
-out "$WORK_DIR/$name.csr" \
|
|
-subj "/CN=$common_name"
|
|
|
|
cat > "$WORK_DIR/$name.ext" <<EOF
|
|
basicConstraints=critical,CA:FALSE
|
|
keyUsage=critical,digitalSignature,keyEncipherment
|
|
extendedKeyUsage=$usage
|
|
subjectAltName=$san
|
|
EOF
|
|
|
|
openssl x509 -req -sha256 \
|
|
-in "$WORK_DIR/$name.csr" \
|
|
-CA "$PKI_DIR/$ca_prefix.crt" \
|
|
-CAkey "$PKI_DIR/$ca_prefix.key" \
|
|
-CAserial "$WORK_DIR/$ca_prefix.srl" \
|
|
-CAcreateserial \
|
|
-days 90 \
|
|
-out "$PKI_DIR/$name.crt" \
|
|
-extfile "$WORK_DIR/$name.ext" 2>/dev/null
|
|
|
|
chmod 600 "$PKI_DIR/$name.key"
|
|
chmod 644 "$PKI_DIR/$name.crt"
|
|
}
|
|
|
|
echo "Generating lab7 private CA..."
|
|
create_ca ca "MTUSI Lab7 Local CA"
|
|
|
|
echo "Generating HTTPS ingress certificate..."
|
|
create_leaf \
|
|
ingress \
|
|
localhost \
|
|
"DNS:localhost,IP:127.0.0.1" \
|
|
serverAuth
|
|
|
|
echo "Generating API server certificates..."
|
|
create_leaf \
|
|
api1 \
|
|
api1 \
|
|
"DNS:api1,DNS:messenger-api.internal" \
|
|
serverAuth
|
|
|
|
create_leaf \
|
|
api2 \
|
|
api2 \
|
|
"DNS:api2,DNS:messenger-api.internal" \
|
|
serverAuth
|
|
|
|
echo "Generating authorized LB client certificate..."
|
|
create_leaf \
|
|
lb-client \
|
|
lb.messenger.internal \
|
|
"DNS:lb.messenger.internal" \
|
|
clientAuth
|
|
|
|
echo "Generating unauthorized client identity..."
|
|
create_leaf \
|
|
unauthorized-client \
|
|
unauthorized.messenger.internal \
|
|
"DNS:unauthorized.messenger.internal" \
|
|
clientAuth
|
|
|
|
echo "Generating separate untrusted CA..."
|
|
create_ca rogue-ca "MTUSI Lab7 Untrusted Test CA"
|
|
|
|
create_leaf \
|
|
rogue-client \
|
|
rogue.messenger.internal \
|
|
"DNS:rogue.messenger.internal" \
|
|
clientAuth \
|
|
rogue-ca
|
|
|
|
echo "Verifying issued certificates..."
|
|
|
|
openssl verify \
|
|
-CAfile "$PKI_DIR/ca.crt" \
|
|
-purpose sslserver \
|
|
-verify_hostname localhost \
|
|
"$PKI_DIR/ingress.crt"
|
|
|
|
openssl verify \
|
|
-CAfile "$PKI_DIR/ca.crt" \
|
|
-purpose sslserver \
|
|
-verify_hostname api1 \
|
|
"$PKI_DIR/api1.crt"
|
|
|
|
openssl verify \
|
|
-CAfile "$PKI_DIR/ca.crt" \
|
|
-purpose sslserver \
|
|
-verify_hostname api2 \
|
|
"$PKI_DIR/api2.crt"
|
|
|
|
openssl verify \
|
|
-CAfile "$PKI_DIR/ca.crt" \
|
|
-purpose sslclient \
|
|
"$PKI_DIR/lb-client.crt"
|
|
|
|
openssl verify \
|
|
-CAfile "$PKI_DIR/ca.crt" \
|
|
-purpose sslclient \
|
|
"$PKI_DIR/unauthorized-client.crt"
|
|
|
|
echo
|
|
echo "PASS: local CA and TLS certificates generated."
|
|
echo "PKI directory: $PKI_DIR"
|
|
echo "CA certificate: $PKI_DIR/ca.crt"
|
|
echo
|
|
echo "IMPORTANT:"
|
|
echo "- Never commit private keys."
|
|
echo "- Keep the CA private key operator-only."
|
|
echo "- Do not copy the PKI directory into Docker images."
|
|
echo "- Unauthorized and rogue certificates are test fixtures only."
|