Generate a 10-year root CA in entrypoint.sh and sign the server leaf with it. Only the root CA is distributed to clients (including GPO); the leaf is served by the app and is reissued automatically when CERT_HOSTNAMES changes. Update the Windows installer, ZIP package, settings UI and docs for the two-tier scheme.
91 lines
2.5 KiB
Bash
Executable File
91 lines
2.5 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
CERT_DIR=/app/certs
|
|
ROOT_CERT="$CERT_DIR/root-ca.pem"
|
|
ROOT_KEY="$CERT_DIR/root-ca-key.pem"
|
|
LEAF_CERT="$CERT_DIR/cert.pem"
|
|
LEAF_KEY="$CERT_DIR/key.pem"
|
|
SAN_FILE="$CERT_DIR/.san"
|
|
|
|
cert_san_type() {
|
|
case "$1" in
|
|
*.*.*.*) printf 'IP:%s' "$1" ;;
|
|
*) printf 'DNS:%s' "$1" ;;
|
|
esac
|
|
}
|
|
|
|
CERT_CN="whatido.local"
|
|
SAN="DNS:localhost,IP:127.0.0.1"
|
|
|
|
i=100
|
|
while [ "$i" -le 200 ]; do
|
|
SAN="$SAN,IP:192.168.88.$i"
|
|
i=$((i + 1))
|
|
done
|
|
|
|
FIRST_HOST=1
|
|
OLD_IFS=$IFS
|
|
IFS=','
|
|
for host in ${CERT_HOSTNAMES:-}; do
|
|
host=$(printf '%s' "$host" | tr -d '[:space:]')
|
|
[ -n "$host" ] || continue
|
|
if [ "$FIRST_HOST" = "1" ]; then
|
|
CERT_CN="$host"
|
|
FIRST_HOST=0
|
|
fi
|
|
SAN="$SAN,$(cert_san_type "$host")"
|
|
done
|
|
IFS=$OLD_IFS
|
|
|
|
SAN_SIG=$(printf '%s' "$SAN" | sha256sum | cut -d' ' -f1)
|
|
OLD_SIG=$(cat "$SAN_FILE" 2>/dev/null || true)
|
|
ROOT_NEW=0
|
|
LEAF_NEW=0
|
|
|
|
mkdir -p "$CERT_DIR"
|
|
|
|
if [ ! -f "$ROOT_CERT" ] || [ ! -f "$ROOT_KEY" ]; then
|
|
openssl req -x509 -nodes -newkey rsa:4096 -days 3650 \
|
|
-keyout "$ROOT_KEY" -out "$ROOT_CERT" \
|
|
-subj "/O=WhatIDo/CN=WhatIDo Local Root CA" \
|
|
-addext "basicConstraints=critical,CA:TRUE,pathlen:0" \
|
|
-addext "keyUsage=critical,keyCertSign,cRLSign" \
|
|
-addext "subjectKeyIdentifier=hash" \
|
|
-addext "authorityKeyIdentifier=keyid:always"
|
|
ROOT_NEW=1
|
|
echo "TLS: создан корневой CA — CN=WhatIDo Local Root CA"
|
|
fi
|
|
|
|
if [ ! -f "$LEAF_CERT" ] || [ ! -f "$LEAF_KEY" ] || [ "$ROOT_NEW" = "1" ] || [ "$OLD_SIG" != "$SAN_SIG" ]; then
|
|
LEAF_NEW=1
|
|
elif ! openssl verify -CAfile "$ROOT_CERT" "$LEAF_CERT" >/dev/null 2>&1; then
|
|
LEAF_NEW=1
|
|
fi
|
|
|
|
if [ "$LEAF_NEW" = "1" ]; then
|
|
openssl genrsa -out "$LEAF_KEY" 2048
|
|
openssl req -new -key "$LEAF_KEY" -out "$CERT_DIR/leaf.csr" -subj "/O=WhatIDo/CN=$CERT_CN"
|
|
|
|
cat > "$CERT_DIR/leaf.ext" <<EOF
|
|
basicConstraints=critical,CA:FALSE
|
|
keyUsage=critical,digitalSignature,keyEncipherment
|
|
extendedKeyUsage=serverAuth
|
|
subjectAltName=$SAN
|
|
subjectKeyIdentifier=hash
|
|
authorityKeyIdentifier=keyid,issuer
|
|
EOF
|
|
|
|
openssl x509 -req -in "$CERT_DIR/leaf.csr" -CA "$ROOT_CERT" -CAkey "$ROOT_KEY" \
|
|
-CAcreateserial -out "$LEAF_CERT" -days 825 -sha256 -extfile "$CERT_DIR/leaf.ext"
|
|
|
|
rm -f "$CERT_DIR/leaf.csr" "$CERT_DIR/leaf.ext"
|
|
printf '%s' "$SAN_SIG" > "$SAN_FILE"
|
|
echo "TLS: серверный сертификат создан — CN=$CERT_CN"
|
|
else
|
|
echo "TLS: серверный сертификат уже существует, генерация пропущена"
|
|
fi
|
|
|
|
chmod 600 "$ROOT_KEY" "$LEAF_KEY"
|
|
exec "$@"
|