omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / x509 / gen_pki.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test tooling only: build the X.509 test PKI for om/tls/x509.om with the openssl CLI and write the |
| 4 | # committed fixtures into vectors/pki/ (DER certificates, PEM stores, CertificateVerify messages and |
| 5 | # signatures). Keys are fresh random keys, so a rerun produces different (equally valid) fixtures; |
| 6 | # cases.txt does not depend on key bytes. Usage: sh gen_pki.sh (OpenSSL 3.0 or later) |
| 7 | set -eu |
| 8 | here=$(cd "$(dirname "$0")" && pwd) |
| 9 | out="$here/vectors/pki" |
| 10 | w=$(mktemp -d) |
| 11 | trap 'rm -rf "$w"' EXIT |
| 12 | rm -rf "$out" |
| 13 | mkdir -p "$out" "$w/new" |
| 14 | : > "$w/index.txt" |
| 15 | echo 1000 > "$w/serial" |
| 16 | cat > "$w/ca.cnf" <<EOF |
| 17 | [ ca ] |
| 18 | default_ca = d |
| 19 | [ d ] |
| 20 | dir = $w |
| 21 | database = $w/index.txt |
| 22 | new_certs_dir = $w/new |
| 23 | serial = $w/serial |
| 24 | policy = p |
| 25 | unique_subject = no |
| 26 | copy_extensions = none |
| 27 | email_in_dn = no |
| 28 | [ p ] |
| 29 | commonName = supplied |
| 30 | organizationName = optional |
| 31 | EOF |
| 32 | cat > "$w/ext.cnf" <<'EOF' |
| 33 | [ ca_root ] |
| 34 | basicConstraints = critical,CA:TRUE |
| 35 | keyUsage = critical,keyCertSign,cRLSign |
| 36 | subjectKeyIdentifier = hash |
| 37 | [ ca_int ] |
| 38 | basicConstraints = critical,CA:TRUE |
| 39 | keyUsage = critical,keyCertSign,cRLSign |
| 40 | subjectKeyIdentifier = hash |
| 41 | authorityKeyIdentifier = keyid |
| 42 | [ ca_int0 ] |
| 43 | basicConstraints = critical,CA:TRUE,pathlen:0 |
| 44 | keyUsage = critical,keyCertSign,cRLSign |
| 45 | subjectKeyIdentifier = hash |
| 46 | authorityKeyIdentifier = keyid |
| 47 | [ ca_noca ] |
| 48 | basicConstraints = critical,CA:FALSE |
| 49 | keyUsage = critical,keyCertSign,cRLSign |
| 50 | subjectKeyIdentifier = hash |
| 51 | authorityKeyIdentifier = keyid |
| 52 | [ ca_noku ] |
| 53 | basicConstraints = critical,CA:TRUE |
| 54 | keyUsage = critical,digitalSignature |
| 55 | subjectKeyIdentifier = hash |
| 56 | authorityKeyIdentifier = keyid |
| 57 | [ ca_nc ] |
| 58 | basicConstraints = critical,CA:TRUE |
| 59 | keyUsage = critical,keyCertSign,cRLSign |
| 60 | nameConstraints = critical,permitted;DNS:example.test |
| 61 | subjectKeyIdentifier = hash |
| 62 | authorityKeyIdentifier = keyid |
| 63 | [ ca_ekucl ] |
| 64 | basicConstraints = critical,CA:TRUE |
| 65 | keyUsage = critical,keyCertSign,cRLSign |
| 66 | extendedKeyUsage = clientAuth |
| 67 | subjectKeyIdentifier = hash |
| 68 | authorityKeyIdentifier = keyid |
| 69 | [ leaf ] |
| 70 | basicConstraints = critical,CA:FALSE |
| 71 | keyUsage = critical,digitalSignature,keyEncipherment |
| 72 | extendedKeyUsage = serverAuth |
| 73 | subjectAltName = DNS:www.example.test,DNS:example.test |
| 74 | authorityKeyIdentifier = keyid |
| 75 | [ leaf_ec ] |
| 76 | basicConstraints = critical,CA:FALSE |
| 77 | keyUsage = critical,digitalSignature |
| 78 | extendedKeyUsage = serverAuth |
| 79 | subjectAltName = DNS:www.example.test,DNS:example.test |
| 80 | authorityKeyIdentifier = keyid |
| 81 | [ leaf_min ] |
| 82 | subjectAltName = DNS:www.example.test |
| 83 | [ leaf_wild ] |
| 84 | basicConstraints = critical,CA:FALSE |
| 85 | extendedKeyUsage = serverAuth |
| 86 | subjectAltName = DNS:*.wild.test |
| 87 | [ leaf_wild1 ] |
| 88 | basicConstraints = critical,CA:FALSE |
| 89 | extendedKeyUsage = serverAuth |
| 90 | subjectAltName = DNS:*.test |
| 91 | [ leaf_part ] |
| 92 | basicConstraints = critical,CA:FALSE |
| 93 | extendedKeyUsage = serverAuth |
| 94 | subjectAltName = DNS:f*.wild.test |
| 95 | [ leaf_crit ] |
| 96 | basicConstraints = critical,CA:FALSE |
| 97 | extendedKeyUsage = serverAuth |
| 98 | subjectAltName = DNS:www.example.test |
| 99 | 1.3.6.1.4.1.55555.1 = critical,ASN1:NULL |
| 100 | [ leaf_noncrit ] |
| 101 | basicConstraints = critical,CA:FALSE |
| 102 | extendedKeyUsage = serverAuth |
| 103 | subjectAltName = DNS:www.example.test |
| 104 | 1.3.6.1.4.1.55555.1 = ASN1:NULL |
| 105 | [ leaf_client ] |
| 106 | basicConstraints = critical,CA:FALSE |
| 107 | keyUsage = critical,digitalSignature |
| 108 | extendedKeyUsage = clientAuth |
| 109 | subjectAltName = DNS:www.example.test |
| 110 | [ leaf_ca ] |
| 111 | basicConstraints = critical,CA:TRUE |
| 112 | keyUsage = critical,digitalSignature,keyCertSign |
| 113 | subjectAltName = DNS:www.example.test |
| 114 | [ leaf_nosan ] |
| 115 | basicConstraints = critical,CA:FALSE |
| 116 | extendedKeyUsage = serverAuth |
| 117 | [ leaf_kuenc ] |
| 118 | basicConstraints = critical,CA:FALSE |
| 119 | keyUsage = critical,keyEncipherment |
| 120 | extendedKeyUsage = serverAuth |
| 121 | subjectAltName = DNS:www.example.test |
| 122 | [ leaf_self ] |
| 123 | basicConstraints = critical,CA:FALSE |
| 124 | keyUsage = critical,digitalSignature,keyCertSign |
| 125 | extendedKeyUsage = serverAuth |
| 126 | subjectAltName = DNS:www.example.test |
| 127 | EOF |
| 128 | ossl() { nice -n 19 ionice -c3 openssl "$@"; } |
| 129 | key() { # key NAME SPEC |
| 130 | case $2 in |
| 131 | rsa*) ossl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:${2#rsa} -out "$w/$1.key" 2> /dev/null ;; |
| 132 | pss) ossl genpkey -algorithm RSA-PSS -pkeyopt rsa_keygen_bits:2048 -out "$w/$1.key" 2> /dev/null ;; |
| 133 | p256) ossl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -pkeyopt ec_param_enc:named_curve -out "$w/$1.key" ;; |
| 134 | p384) ossl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-384 -pkeyopt ec_param_enc:named_curve -out "$w/$1.key" ;; |
| 135 | ed25519) ossl genpkey -algorithm ED25519 -out "$w/$1.key" ;; |
| 136 | esac |
| 137 | } |
| 138 | # cert NAME KEYSPEC ISSUER(or self) EXT START END MD [SIGOPT...] |
| 139 | cert() { |
| 140 | n=$1 ks=$2 is=$3 ext=$4 sd=$5 ed=$6 md=$7 |
| 141 | shift 7 |
| 142 | [ -f "$w/$n.key" ] || key "$n" "$ks" |
| 143 | ossl req -new -key "$w/$n.key" -subj "/O=omtls test/CN=$n" -out "$w/$n.csr" |
| 144 | so="" |
| 145 | for o in "$@"; do so="$so -sigopt $o"; done |
| 146 | if [ "$is" = self ]; then |
| 147 | ossl ca -batch -config "$w/ca.cnf" -selfsign -keyfile "$w/$n.key" -in "$w/$n.csr" -out "$w/$n.pem" \ |
| 148 | -startdate "$sd" -enddate "$ed" -md "$md" -extfile "$w/ext.cnf" -extensions "$ext" -notext $so 2> "$w/err" || { cat "$w/err"; exit 1; } |
| 149 | else |
| 150 | ossl ca -batch -config "$w/ca.cnf" -cert "$w/$is.pem" -keyfile "$w/$is.key" -in "$w/$n.csr" -out "$w/$n.pem" \ |
| 151 | -startdate "$sd" -enddate "$ed" -md "$md" -extfile "$w/ext.cnf" -extensions "$ext" -notext $so 2> "$w/err" || { cat "$w/err"; exit 1; } |
| 152 | fi |
| 153 | ossl x509 -in "$w/$n.pem" -outform DER -out "$out/$n.der" |
| 154 | } |
| 155 | R0=200101000000Z; R1=20510101000000Z |
| 156 | I0=240101000000Z; I1=360101000000Z |
| 157 | L0=260101000000Z; L1=270101000000Z |
| 158 | PSS256="rsa_padding_mode:pss rsa_pss_saltlen:digest" |
| 159 | # roots |
| 160 | cert rootR rsa2048 self ca_root $R0 $R1 sha256 |
| 161 | cert rootE p384 self ca_root $R0 $R1 sha384 |
| 162 | cert rootX rsa2048 self ca_root $R0 $R1 sha256 |
| 163 | # same subject name as rootR, different key (store must try every candidate) |
| 164 | key rootRdup rsa2048 |
| 165 | ossl req -new -key "$w/rootRdup.key" -subj "/O=omtls test/CN=rootR" -out "$w/rootRdup.csr" |
| 166 | ossl ca -batch -config "$w/ca.cnf" -selfsign -keyfile "$w/rootRdup.key" -in "$w/rootRdup.csr" -out "$w/rootRdup.pem" \ |
| 167 | -startdate $R0 -enddate $R1 -md sha256 -extfile "$w/ext.cnf" -extensions ca_root -notext 2> /dev/null |
| 168 | # intermediates |
| 169 | cert intR rsa2048 rootR ca_int $I0 $I1 sha256 |
| 170 | cert intE p256 rootE ca_int $I0 $I1 sha384 |
| 171 | cert intRexp rsa2048 rootR ca_int 200101000000Z 250101000000Z sha256 |
| 172 | cert intNoCA rsa2048 rootR ca_noca $I0 $I1 sha256 |
| 173 | cert intNoKU rsa2048 rootR ca_noku $I0 $I1 sha256 |
| 174 | cert intP0 rsa2048 rootR ca_int0 $I0 $I1 sha256 |
| 175 | cert intP0b rsa2048 intP0 ca_int $I0 $I1 sha256 |
| 176 | cert intNC rsa2048 rootR ca_nc $I0 $I1 sha256 |
| 177 | cert intEKUc rsa2048 rootR ca_ekucl $I0 $I1 sha256 |
| 178 | cert intPSS rsa2048 rootR ca_int $I0 $I1 sha384 rsa_padding_mode:pss rsa_pss_saltlen:digest |
| 179 | prev=rootR |
| 180 | for i in 1 2 3 4 5 6 7 8; do cert "d$i" rsa2048 $prev ca_int $I0 $I1 sha256; prev=d$i; done |
| 181 | # leaves under intR |
| 182 | cert lR rsa2048 intR leaf $L0 $L1 sha256 |
| 183 | cert lR384 rsa2048 intR leaf $L0 $L1 sha384 |
| 184 | cert lR512 rsa2048 intR leaf $L0 $L1 sha512 |
| 185 | cert lRmin rsa2048 intR leaf_min $L0 $L1 sha256 |
| 186 | cert lPSS256 rsa2048 intR leaf $L0 $L1 sha256 $PSS256 |
| 187 | cert lPSS384 rsa3072 intR leaf $L0 $L1 sha384 $PSS256 |
| 188 | cert lPSS512 rsa4096 intR leaf $L0 $L1 sha512 $PSS256 |
| 189 | cert lExp rsa2048 intR leaf 240101000000Z 250101000000Z sha256 |
| 190 | cert lNyv rsa2048 intR leaf 280101000000Z 290101000000Z sha256 |
| 191 | cert l2050 rsa2048 intR leaf 490101000000Z 20500101000000Z sha256 |
| 192 | cert lWild rsa2048 intR leaf_wild $L0 $L1 sha256 |
| 193 | cert lWild1 rsa2048 intR leaf_wild1 $L0 $L1 sha256 |
| 194 | cert lPart rsa2048 intR leaf_part $L0 $L1 sha256 |
| 195 | cert lCrit rsa2048 intR leaf_crit $L0 $L1 sha256 |
| 196 | cert lNonCrit rsa2048 intR leaf_noncrit $L0 $L1 sha256 |
| 197 | cert lClient rsa2048 intR leaf_client $L0 $L1 sha256 |
| 198 | cert lCA rsa2048 intR leaf_ca $L0 $L1 sha256 |
| 199 | cert lNoSAN rsa2048 intR leaf_nosan $L0 $L1 sha256 |
| 200 | cert lKUenc rsa2048 intR leaf_kuenc $L0 $L1 sha256 |
| 201 | cert lR1024 rsa1024 intR leaf $L0 $L1 sha256 |
| 202 | cert lSHA1 rsa2048 intR leaf $L0 $L1 sha1 |
| 203 | cert lRpss pss intR leaf $L0 $L1 sha256 |
| 204 | cert lEd ed25519 intR leaf $L0 $L1 sha256 |
| 205 | cert lSelf rsa2048 self leaf_self $L0 $L1 sha256 |
| 206 | # leaves under other intermediates |
| 207 | cert lE256 p256 intE leaf_ec $L0 $L1 sha256 |
| 208 | cert lE384 p384 intE leaf_ec $L0 $L1 sha512 |
| 209 | cert lE384b p384 rootE leaf_ec $L0 $L1 sha384 |
| 210 | cert lEint p256 intE leaf_ec $L0 $L1 sha384 |
| 211 | cert lViaExp rsa2048 intRexp leaf $L0 $L1 sha256 |
| 212 | cert lViaNoCA rsa2048 intNoCA leaf $L0 $L1 sha256 |
| 213 | cert lViaNoKU rsa2048 intNoKU leaf $L0 $L1 sha256 |
| 214 | cert lViaP0 rsa2048 intP0b leaf $L0 $L1 sha256 |
| 215 | cert lViaNC rsa2048 intNC leaf $L0 $L1 sha256 |
| 216 | cert lViaEKUc rsa2048 intEKUc leaf $L0 $L1 sha256 |
| 217 | cert lViaPSS rsa2048 intPSS leaf $L0 $L1 sha256 |
| 218 | cert lD7 rsa2048 d7 leaf $L0 $L1 sha256 |
| 219 | cert lD8 rsa2048 d8 leaf $L0 $L1 sha256 |
| 220 | # stores |
| 221 | cat "$w/rootR.pem" > "$out/storeR.pem" |
| 222 | cat "$w/rootE.pem" > "$out/storeE.pem" |
| 223 | cat "$w/rootX.pem" > "$out/storeX.pem" |
| 224 | cat "$w/rootRdup.pem" "$w/rootR.pem" > "$out/storeDup.pem" |
| 225 | cat "$w/intR.pem" > "$out/storeInt.pem" |
| 226 | { cat "$w/rootX.pem"; printf 'junk line outside any block\r\n'; cat "$w/rootE.pem" "$w/rootR.pem"; } > "$out/storeAll.pem" |
| 227 | # a store whose second block has a bad base64 character |
| 228 | { cat "$w/rootX.pem"; sed '3s/^./*/' "$w/rootR.pem"; } > "$out/storeBad.pem" |
| 229 | # damaged certificates |
| 230 | python3 - "$out" <<'EOF' |
| 231 | import sys |
| 232 | d = sys.argv[1] |
| 233 | b = bytearray(open(d + "/lR.der", "rb").read()) |
| 234 | b[-5] ^= 0x01 |
| 235 | open(d + "/lRbadsig.der", "wb").write(b) |
| 236 | b = bytearray(open(d + "/lR.der", "rb").read()) |
| 237 | open(d + "/lRtrunc.der", "wb").write(b[:-10]) |
| 238 | # outer signatureAlgorithm sha256WithRSAEncryption -> sha384WithRSAEncryption; tbs keeps sha256 |
| 239 | oid = bytes.fromhex("2a864886f70d01010b") |
| 240 | i = b.rfind(oid) |
| 241 | b[i + len(oid) - 1] = 0x0c |
| 242 | open(d + "/lRalgmis.der", "wb").write(b) |
| 243 | import random |
| 244 | r = random.Random(509) |
| 245 | open(d + "/garbage.der", "wb").write(bytes([0x30, 0x82, 0x01, 0x00]) + bytes(r.randrange(256) for _ in range(256))) |
| 246 | EOF |
| 247 | # CertificateVerify fixtures: content is the TLS 1.3 server signed content (RFC 8446 4.4.3) over a |
| 248 | # fixed 32-byte transcript hash |
| 249 | { i=0; while [ $i -lt 64 ]; do printf ' '; i=$((i + 1)); done |
| 250 | printf 'TLS 1.3, server CertificateVerify\000' |
| 251 | printf '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' | xxd -r -p; } > "$out/cv.msg" |
| 252 | sg() { ossl dgst "-$1" -sign "$w/$2.key" $3 -out "$out/$4" "$out/cv.msg"; } |
| 253 | sg sha256 lE256 "" cv_e256.sig |
| 254 | sg sha384 lE384 "" cv_e384.sig |
| 255 | sg sha256 lR "-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:digest" cv_r256.sig |
| 256 | sg sha384 lPSS384 "-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:digest" cv_r384.sig |
| 257 | sg sha512 lPSS512 "-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:digest" cv_r512.sig |
| 258 | sg sha256 lR "" cv_rpkcs1.sig |
| 259 | sg sha256 lR1024 "-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:digest" cv_r1024.sig |
| 260 | python3 - "$out" <<'EOF' |
| 261 | import sys |
| 262 | d = sys.argv[1] |
| 263 | m = bytearray(open(d + "/cv.msg", "rb").read()) |
| 264 | m[-1] ^= 1 |
| 265 | open(d + "/cv_bad.msg", "wb").write(m) |
| 266 | EOF |
| 267 | ls "$out" | wc -l |