Skip to content
suit Artifact [7b0cebd194]
Login

Artifact [7b0cebd194]

Artifact 7b0cebd194c4d379b8864719b1a7f22167ec47b865bd1d45fff2f4213f773bb3:


#!/bin/sh
# Certificates for test/https.sh, made fresh in DIR each run and never kept:
# ca1 (the trusted CA), ca2 (a CA nobody trusts), leaf (localhost, from ca1),
# leafca2 (localhost, from ca2), rsa (localhost, an RSA key, from ca1), and
# where this openssl can backdate one, expired (localhost, from ca1, ended in
# 2025). ECDSA P-256 but for rsa. Each NAME.chain.pem is the leaf and its CA.
# usage: https_certs.sh DIR    (needs openssl)
set -eu
mkdir -p "$1"
cd "$1"
mkca() {
    openssl ecparam -name prime256v1 -genkey -noout -out "$1.key" 2>/dev/null
    openssl req -x509 -new -key "$1.key" -sha256 -days 30 -subj "/CN=suit test $1" \
        -addext basicConstraints=critical,CA:TRUE -addext keyUsage=critical,keyCertSign -out "$1.pem" 2>/dev/null
}
mkca ca1
mkca ca2
printf 'subjectAltName=DNS:localhost\nbasicConstraints=CA:FALSE\nkeyUsage=digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth\n' > ext.cnf
mkleaf() { # NAME CA [x509 options]
    n=$1 ca=$2
    shift 2
    if [ "$n" = rsa ]; then
        openssl genrsa -out "$n.key" 2048 2>/dev/null
    else
        openssl ecparam -name prime256v1 -genkey -noout -out "$n.key" 2>/dev/null
    fi
    openssl req -new -key "$n.key" -subj "/CN=localhost" -out "$n.csr" 2>/dev/null
    openssl x509 -req -in "$n.csr" -CA "$ca.pem" -CAkey "$ca.key" -CAcreateserial -sha256 \
        -extfile ext.cnf "$@" -out "$n.crt" 2>/dev/null
    cat "$n.crt" "$ca.pem" > "$n.chain.pem"
}
mkleaf leaf ca1 -days 30
mkleaf leafca2 ca2 -days 30
mkleaf rsa ca1 -days 30
mkleaf expired ca1 -not_before 20250101000000Z -not_after 20250201000000Z || rm -f expired.*