code wiki / _hdl_build / nx_quic_hs_test.nx
nx_quic_hs_test.nx source
↩ module page · 56 lines · 2832 B
1// nx_quic_hs_gate.nx -- RUNG 6b gate: TLS 1.3 handshake-message framing (RFC 8446 sec 4) + CRYPTO-frame
2// out-of-order reassembly. Native, sovereign. Flat checks. license_tier: ORIGINAL
3import "nx_quic_hs.nx"
4import "nx_g_pn_lite_lib.nx"
5import "nx_g_check_lib.nx"
6import "nx_g_puts_lib.nx"
7
8func main() -> i64 {
9 g_puts("nx_quic_hs gate -- TLS 1.3 handshake framing + CRYPTO reassembly (RFC 8446 sec 4)\n" as *u8)
10 var pass: i64=0; var total: i64=0
11 let info: *i64 = sys_mmap(8*3) as *i64
12
13 // ---- encode a ClientHello-framed message: type 1, 300-byte body ----
14 let body: *u8 = sys_mmap(300)
15 var i: i64=0; while i<300 { body[i]=((i*11+7)&255) as u8; i=i+1 }
16 let msg: *u8 = sys_mmap(512)
17 let mlen: i64 = quic_hs_encode(msg, HS_CLIENT_HELLO, body, 300)
18 var e_ok: i64=1
19 if mlen != 304 { e_ok=0 }
20 if msg[0] != (1 as u8) { e_ok=0 }
21 if msg[1] != (0 as u8) { e_ok=0 } // uint24 of 300 = 00 01 2c
22 if msg[2] != (1 as u8) { e_ok=0 }
23 if msg[3] != (0x2c as u8) { e_ok=0 }
24 pass = pass + g_check("encode ClientHello: type=1, uint24 len=00012c, total 304B" as *u8, e_ok); total=total+1
25
26 // ---- parse it back, body byte-exact ----
27 var p_ok: i64=1
28 if quic_hs_parse(msg, mlen, info) != 0 { p_ok=0 }
29 if info[0] != HS_CLIENT_HELLO { p_ok=0 }
30 if info[1] != 300 { p_ok=0 }
31 var k: i64=0; while k<300 { if msg[info[2]+k] != body[k] { p_ok=0; k=300 } else { k=k+1 } }
32 pass = pass + g_check("parse: type=client_hello, len=300, body byte-exact" as *u8, p_ok); total=total+1
33
34 // ---- out-of-order CRYPTO-fragment reassembly (frag@100 first, then frag@0) ----
35 let hsbuf: *u8 = sys_mmap(512)
36 var hwm: i64=0
37 hwm = quic_hs_reassemble(hsbuf, (msg as i64 + 100) as *u8, 100, 204, hwm) // tail first
38 hwm = quic_hs_reassemble(hsbuf, msg, 0, 100, hwm) // head second
39 var r_ok: i64=1
40 if hwm != 304 { r_ok=0 }
41 var j: i64=0; while j<304 { if hsbuf[j] != msg[j] { r_ok=0; j=304 } else { j=j+1 } }
42 if quic_hs_parse(hsbuf, hwm, info) != 0 { r_ok=0 }
43 if info[0] != HS_CLIENT_HELLO { r_ok=0 }
44 if info[1] != 300 { r_ok=0 }
45 pass = pass + g_check("out-of-order CRYPTO reassembly (tail@100 then head@0) -> exact, parses" as *u8, r_ok); total=total+1
46
47 // ---- truncation rejected ----
48 var t_ok: i64=0
49 if quic_hs_parse(msg, 100, info) == 0 - 1 { t_ok=1 }
50 pass = pass + g_check("truncated handshake message rejected (body 300 > available 96)" as *u8, t_ok); total=total+1
51
52 g_puts("---- quic_hs gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
53 if pass == total { g_puts("VERDICT: GREEN (TLS 1.3 handshake framing + CRYPTO reassembly, RFC 8446 sec 4, sovereign)\n" as *u8); return 0 }
54 g_puts("VERDICT: RED\n" as *u8)
55 return 1
56}