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 / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test suite for om/tls/x509.om. Usage: sh om/tls/test/x509/run.sh OMC |
| 4 | # Builds om2 + sha2 hkdf ec rsa x509 + driver.om into build/tls/x509/, runs vectors/cases.txt (plus a |
| 5 | # record for the system store), compares every chain case with openssl verify (differential; the |
| 6 | # intentional differences are listed in vectors/openssl-diff.txt), and runs the source mutants. |
| 7 | # Exit 0 only when everything passes; final line "X509 PASS cases=N (openssl-parity=P/N) mutants=M/M". |
| 8 | set -eu |
| 9 | omc=${1:?usage: run.sh OMC} |
| 10 | here=$(cd "$(dirname "$0")" && pwd) |
| 11 | tls=$(cd "$here/../.." && pwd) |
| 12 | root=$(cd "$tls/../.." && pwd) |
| 13 | pin=$(sed -n 's/^OMC_PIN=//p' "$root/om/units.sh") |
| 14 | out="$root/build/tls/x509" |
| 15 | vec="$here/vectors" |
| 16 | sys=/etc/ssl/certs/ca-certificates.crt |
| 17 | mkdir -p "$out" |
| 18 | bad=0 |
| 19 | say() { printf '%s\n' "$*"; } |
| 20 | fail() { say "FAIL $*"; bad=1; } |
| 21 | run() { nice -n 19 ionice -c3 "$@"; } |
| 22 | |
| 23 | [ "$(sha256sum < "$omc" | cut -c1-64)" = "$pin" ] || { say "FAIL omc is not the pinned compiler"; exit 1; } |
| 24 | |
| 25 | # build LIB DRIVER BIN |
| 26 | build() { |
| 27 | { echo om2; cat "$tls/sha2.om" "$tls/hkdf.om" "$tls/ec.om" "$tls/rsa.om" "$1" "$2"; } > "$3.om" |
| 28 | if ! run "$omc" < "$3.om" > "$3" 2> "$3.err" || [ -s "$3.err" ]; then |
| 29 | say "omc refused $3.om: $(cat "$3.err")" |
| 30 | return 1 |
| 31 | fi |
| 32 | chmod +x "$3" |
| 33 | } |
| 34 | |
| 35 | build "$tls/x509.om" "$here/driver.om" "$out/driver" || { say "X509 FAIL build"; exit 1; } |
| 36 | say "omc sha256 $pin" |
| 37 | say "library sha256 $(sha256sum < "$tls/x509.om" | cut -c1-64) ($(grep -c '^fn ' "$tls/x509.om") functions)" |
| 38 | say "driver sha256 $(sha256sum < "$out/driver" | cut -c1-64)" |
| 39 | |
| 40 | # the system store record: every BEGIN CERTIFICATE block of the bundle must load |
| 41 | nsys=$(grep -c -- '-----BEGIN CERTIFICATE-----' "$sys") |
| 42 | { cat "$vec/cases.txt"; echo "S system_store $nsys SYS"; } > "$out/cases.txt" |
| 43 | ncase=$(grep -c '^C ' "$out/cases.txt") |
| 44 | |
| 45 | # drive FILE BIN NAME -> sets rc, last |
| 46 | drive() { |
| 47 | set +e |
| 48 | (cd "$vec" && run "$2" < "$1") > "$out/$3.out" 2>&1 |
| 49 | rc=$? |
| 50 | set -e |
| 51 | last=$(tail -n 1 "$out/$3.out") |
| 52 | } |
| 53 | |
| 54 | drive "$out/cases.txt" "$out/driver" main |
| 55 | checks=$(printf '%s\n' "$last" | awk '$1 == "x509" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }') |
| 56 | if [ "$rc" -eq 0 ] && [ -n "$checks" ]; then |
| 57 | say "PASS cases.txt: $last" |
| 58 | grep '^fuzz ' "$out/main.out" | sed 's/^/PASS /' |
| 59 | else |
| 60 | fail "cases.txt: exit $rc: $(grep -E 'MISMATCH|driver' "$out/main.out" | head -n 20 | tr '\n' ' ')" |
| 61 | fi |
| 62 | fuzzn=$(awk '$1 == "fuzz" { sub("n=", "", $3); s += $3 } END { print s + 0 }' "$out/main.out") |
| 63 | |
| 64 | # differential: openssl verify for every C record |
| 65 | parity=0 |
| 66 | : > "$out/diff.txt" |
| 67 | if command -v openssl > /dev/null 2>&1; then |
| 68 | say "openssl $(openssl version)" |
| 69 | while read -r tag name want now host store certs; do |
| 70 | [ "$tag" = C ] || continue |
| 71 | set -- $certs |
| 72 | [ "$store" = SYS ] && ca=$sys || ca="$vec/$store" |
| 73 | leaf="$out/ossl-leaf.pem" |
| 74 | unt="$out/ossl-untrusted.pem" |
| 75 | : > "$unt" |
| 76 | ok=1 |
| 77 | openssl x509 -inform DER -in "$vec/$1" -out "$leaf" 2> /dev/null || ok=0 |
| 78 | shift |
| 79 | for c in "$@"; do openssl x509 -inform DER -in "$vec/$c" >> "$unt" 2> /dev/null || ok=0; done |
| 80 | if [ "$ok" -eq 1 ]; then |
| 81 | uarg="" |
| 82 | [ -s "$unt" ] && uarg="-untrusted $unt" |
| 83 | if run openssl verify -x509_strict -purpose sslserver -verify_hostname "$host" -attime "$now" \ |
| 84 | -CAfile "$ca" $uarg "$leaf" > "$out/ossl.txt" 2>&1; then o=accept; else o=reject; fi |
| 85 | else |
| 86 | o=reject |
| 87 | fi |
| 88 | [ "$want" = X509_OK ] && m=accept || m=reject |
| 89 | listed=$(awk -v n="$name" '$1 == n { print "y" }' "$vec/openssl-diff.txt") |
| 90 | if [ "$o" = "$m" ]; then |
| 91 | parity=$((parity + 1)) |
| 92 | [ -z "$listed" ] || fail "openssl-diff.txt lists $name but openssl agrees ($o)" |
| 93 | else |
| 94 | echo "$name x509=$m openssl=$o $(grep -m1 'error' "$out/ossl.txt" | tr -s ' ')" >> "$out/diff.txt" |
| 95 | [ -n "$listed" ] || fail "unlisted openssl difference: $name x509=$m openssl=$o: $(tr '\n' ' ' < "$out/ossl.txt")" |
| 96 | fi |
| 97 | done < "$out/cases.txt" |
| 98 | sed 's/^/DIFF /' "$out/diff.txt" |
| 99 | else |
| 100 | say "NOT_RUN openssl differential: openssl not installed" |
| 101 | fail "differential not run" |
| 102 | fi |
| 103 | |
| 104 | # mutants: each must make the driver finish normally with exit 1 and MISMATCH lines |
| 105 | mut=0 |
| 106 | mutok=0 |
| 107 | # mutant SED NAME [ROWS]: library mutant. Detected when the driver finishes with exit 1 and MISMATCH |
| 108 | # lines, or dies on an Omega runtime trap (exit 101..114: the main run fails on any trap, so the mutant |
| 109 | # is caught). Any other status (a launch failure 126/127, a signal 128+) is not a detection. With ROWS |
| 110 | # (strict): only exit 1 with the completed driver summary (as many checks as the unmutated run, $checks) |
| 111 | # and a MISMATCH for every named row counts. |
| 112 | mutant() { |
| 113 | mut=$((mut + 1)) |
| 114 | sed "$1" "$tls/x509.om" > "$out/mut-$2-lib.om" |
| 115 | if cmp -s "$tls/x509.om" "$out/mut-$2-lib.om"; then fail "mutant $2: sed changed nothing"; return; fi |
| 116 | if ! build "$out/mut-$2-lib.om" "$here/driver.om" "$out/mut-$2"; then fail "mutant $2: build"; return; fi |
| 117 | drive "$out/cases.txt" "$out/mut-$2" "mut-$2" |
| 118 | n=$(grep -c '^MISMATCH ' "$out/mut-$2.out" || true) |
| 119 | if [ -n "${3:-}" ]; then |
| 120 | miss= |
| 121 | for r in $3; do grep -q "^MISMATCH line [0-9][0-9]* $r got " "$out/mut-$2.out" || miss="$miss $r"; done |
| 122 | if [ "$rc" -eq 1 ] && [ "$n" -gt 0 ] && [ -z "$miss" ] && [ -n "${checks:-}" ] && printf '%s\n' "$last" | grep -q "^x509 driver: $checks checks, $n failed$"; then |
| 123 | mutok=$((mutok + 1)) |
| 124 | say "PASS mutant $2 detected ($n MISMATCH, required rows:$(printf ' %s' $3))" |
| 125 | else |
| 126 | fail "mutant $2 not detected as required: exit $rc, $n MISMATCH, rows without a MISMATCH:${miss:- none}: $last" |
| 127 | fi |
| 128 | return |
| 129 | fi |
| 130 | if [ "$rc" -eq 1 ] && [ "$n" -gt 0 ] && printf '%s\n' "$last" | grep -q "^x509 driver: [0-9][0-9]* checks, $n failed$"; then |
| 131 | mutok=$((mutok + 1)) |
| 132 | say "PASS mutant $2 detected ($n MISMATCH: $(grep '^MISMATCH ' "$out/mut-$2.out" | head -n 2 | cut -d' ' -f4 | tr '\n' ' '))" |
| 133 | elif [ "$rc" -ge 101 ] && [ "$rc" -le 114 ]; then |
| 134 | mutok=$((mutok + 1)) |
| 135 | say "PASS mutant $2 detected (driver trapped, exit $rc, after $n MISMATCH)" |
| 136 | else |
| 137 | fail "mutant $2 not detected: exit $rc: $last" |
| 138 | fi |
| 139 | } |
| 140 | # name matching: a wildcard with only one further label ("*.test") is accepted |
| 141 | mutant 's/^if or eq lab 0 eq dots 0$/if eq lab 0/' name-wildcard-labels |
| 142 | # date comparison: notAfter compared the wrong way round |
| 143 | mutant 's/^if le get r X5RNA nw$/if gt get r X5RNA nw/' date-not-after |
| 144 | # date boundary: now == notAfter accepted again (RFC-inclusive bound that OpenSSL refuses) |
| 145 | mutant 's/^if le get r X5RNA nw$/if lt get r X5RNA nw/' date-at-not-after |
| 146 | # trust anchors: a store entry that is not self-issued accepted as an anchor |
| 147 | mutant 's/^if eq chain_self kd w 0$/if eq chain_self kd w 9/' anchor-self-issued |
| 148 | # basicConstraints: cA FALSE accepted for an issuer |
| 149 | mutant 's/^if eq get w add X5RSZ X5RCA 0$/if eq get w add X5RSZ X5RCA 2/' bc-ca-flag |
| 150 | # security review finding 1: unique IDs repeated or out of order accepted |
| 151 | mutant 's/^if or eq v 0 le g k$/if eq v 0/' unique-id-order |
| 152 | # finding 5: nameConstraints refused only when critical (the pre-review behaviour) |
| 153 | mutant 's/^if eq o 20$/if eq o 99/' name-constraints-noncritical |
| 154 | # finding 6: modulus above 4096 bits accepted for presented keys |
| 155 | mutant 's/^if or lt b 2048 gt b 4096$/if lt b 2048/' rsa-modulus-max |
| 156 | # finding 6: exponent policy dropped (only e >= 1) |
| 157 | mutant 's/^if or lt v 3 eq and v 1 0$/if lt v 1/' rsa-exponent |
| 158 | # anchor key size: the exponent policy merged into the bit count again (an out-of-policy exponent adds |
| 159 | # 2^32 to X5RBITS), so a 1024-bit anchor with e = 2^32+1 passes chain_sig (anchor_rsa1024_e2_32_plus_1) |
| 160 | mutant 's/^ret bits$/ret add bits mul sub 1 x509_rsaeok k 4294967296/' size-exponent-merged anchor_rsa1024_e2_32_plus_1 |
| 161 | # finding 2: no leaf size limit |
| 162 | mutant 's/^if gt get w add X5WIX 1 X5LMAX$/if gt get w add X5WIX 1 X5BIG/' leaf-size |
| 163 | # finding 2: element caps removed: a 10001-element list hits the loop bound (trap) |
| 164 | mutant 's/^if gt n X5NMAX$/if gt n 99999/' elemcap |
| 165 | # omega-mv #833 suite audit: each new check removed must be caught by its cases.txt vectors |
| 166 | mutant 's/^if lt np 5$/if lt np 1/' san-bare-star-bounds |
| 167 | mutant 's/^if ne x509_dupx c f s 0$/if ne x509_dupx c f s 9/' duplicate-extension |
| 168 | mutant 's/^if ne x509_serok part c get h 1 sub get h 2 get h 1 0$/if eq x509_serok part c get h 1 sub get h 2 get h 1 9/' serial-profile |
| 169 | mutant 's/^ret x509_vtimes c get h 1 get h 2$/ret 0/' time-encoding |
| 170 | mutant 's/^if gt sub b s 3$/if gt sub b s 9/' ku-octets |
| 171 | mutant 's/^if eq and get r X5RSEEN 128 0$/if eq and get r X5RSEEN 128 9/' leaf-aki |
| 172 | mutant 's/^if ne and get w X5RKUB 4 0$/if ne and get w X5RKUB 0 0/' leaf-keycertsign |
| 173 | mutant '0,/^v = chain_time part w X5RSZ X5RSZ get w X5WNOW$/s//v = ulong 0/' anchor-validity |
| 174 | mutant 's/^if eq and get w add X5RSZ X5RSEEN 256 0$/if eq and get w add X5RSZ X5RSEEN 256 9/' intermediate-ski |
| 175 | mutant 's/^if ne and b 7 0$/if ne and b 0 0/' rsa-octets |
| 176 | mutant 's/^if gt get w X5WLNB chain_dcut add s 65$/if gt get w X5WLNB 99999999999/' mozilla-distrust |
| 177 | # expectation mutant: one expected code changed |
| 178 | mut=$((mut + 1)) |
| 179 | sed 's/^C rsa_good X509_OK /C rsa_good X509_ERR_EXPIRED /' "$out/cases.txt" > "$out/mut-expect.txt" |
| 180 | drive "$out/mut-expect.txt" "$out/driver" mut-expect |
| 181 | if [ "$rc" -eq 1 ] && printf '%s\n' "$last" | grep -q '^x509 driver: [0-9][0-9]* checks, 1 failed$' && grep -q '^MISMATCH .* rsa_good ' "$out/mut-expect.out"; then |
| 182 | mutok=$((mutok + 1)) |
| 183 | say "PASS mutant expectation detected" |
| 184 | else |
| 185 | fail "expectation mutant not detected: exit $rc: $last" |
| 186 | fi |
| 187 | |
| 188 | # M1 wiring through x509_chain_verify: the first X5DIST entry replaced by the crafted root |
| 189 | # (lXbase notBefore 2026-01-01 = 1767225600); refused for cutoffs before it, accepted at it |
| 190 | hx=$(sha256sum < "$vec/crafted/rootX.der" | cut -c1-64) |
| 191 | for pc in 0000000000:X509_ERR_UNKNOWN_ISSUER 1767225599:X509_ERR_UNKNOWN_ISSUER 1767225600:X509_OK; do |
| 192 | cut=${pc%%:*} |
| 193 | want=${pc#*:} |
| 194 | mut=$((mut + 1)) |
| 195 | sed "s/^text X5DIST \"[0-9a-f]* [0-9]* /text X5DIST \"$hx $cut /" "$tls/x509.om" > "$out/plant-$cut-lib.om" |
| 196 | if cmp -s "$tls/x509.om" "$out/plant-$cut-lib.om" || ! build "$out/plant-$cut-lib.om" "$here/driver.om" "$out/plant-$cut"; then |
| 197 | fail "planted distrust $cut: no build" |
| 198 | continue |
| 199 | fi |
| 200 | echo "C planted_distrust_$cut $want 1792972800 www.example.test crafted/storeX.pem crafted/lXbase.der" > "$out/plant-$cut.txt" |
| 201 | drive "$out/plant-$cut.txt" "$out/plant-$cut" "plant-$cut" |
| 202 | if [ "$rc" -eq 0 ] && printf '%s\n' "$last" | grep -q '^x509 driver: 1 checks, 0 failed$'; then |
| 203 | mutok=$((mutok + 1)) |
| 204 | say "PASS planted distrust cutoff $cut -> $want" |
| 205 | else |
| 206 | fail "planted distrust cutoff $cut: exit $rc: $(cat "$out/plant-$cut.out" | tr '\n' ' ')" |
| 207 | fi |
| 208 | done |
| 209 | |
| 210 | say "driver checks=${checks:-0} fuzz-mutations=$fuzzn" |
| 211 | if [ "$bad" -ne 0 ]; then |
| 212 | say "X509 FAIL cases=$ncase (openssl-parity=$parity/$ncase) mutants=$mutok/$mut" |
| 213 | exit 1 |
| 214 | fi |
| 215 | say "X509 PASS cases=$ncase (openssl-parity=$parity/$ncase) mutants=$mutok/$mut" |