Files
mtusi2026lab7/scripts/bootstrap-tls.sh

35 lines
1.2 KiB
Bash

#!/usr/bin/env sh
set -eu
repo_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
out_dir=${1:-"$repo_dir/secrets/tls"}
days=${TLS_VALIDITY_DAYS:-30}
command -v openssl >/dev/null 2>&1 || {
echo 'openssl is required and was not found in PATH' >&2
exit 1
}
mkdir -p "$out_dir"
umask 077
cat >"$out_dir/server.ext" <<'EOF'
basicConstraints=critical,CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
extendedKeyUsage=serverAuth
subjectAltName=DNS:localhost,IP:127.0.0.1
EOF
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out "$out_dir/ca.key"
openssl req -x509 -new -sha256 -key "$out_dir/ca.key" -days "$days" \
-subj '/CN=HLA Lab 7 Local CA' -out "$out_dir/ca.crt"
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out "$out_dir/server.key"
openssl req -new -sha256 -key "$out_dir/server.key" -subj '/CN=localhost' -out "$out_dir/server.csr"
openssl x509 -req -sha256 -in "$out_dir/server.csr" -CA "$out_dir/ca.crt" \
-CAkey "$out_dir/ca.key" -CAcreateserial -days "$days" \
-extfile "$out_dir/server.ext" -out "$out_dir/server.crt"
rm -f "$out_dir/server.csr" "$out_dir/server.ext" "$out_dir/ca.srl"
printf 'Created local TLS material in %s\n' "$out_dir"
printf 'Trust this CA only for the lab: %s/ca.crt\n' "$out_dir"