nx_quic_hs.nx source
↩ module page · 47 lines · 2107 B
1// nx_quic_hs.nx -- RUNG 6b of the sovereign QUIC transport: TLS 1.3 handshake-message framing (RFC 8446
2// sec 4) + CRYPTO-frame offset reassembly. In QUIC, TLS handshake messages do NOT use TLS records -- they
3// are carried as a byte stream inside CRYPTO frames (R4), each fragment at an offset. R6b frames the
4// messages (type + uint24 length + body) and reassembles fragments into the contiguous handshake buffer
5// that the sovereign TLS state machine (R6d) consumes. No float. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7
8// HandshakeType (RFC 8446 sec 4)
9const HS_CLIENT_HELLO: i64 = 1
10const HS_SERVER_HELLO: i64 = 2
11const HS_ENCRYPTED_EXTENSIONS: i64 = 8
12const HS_CERTIFICATE: i64 = 11
13const HS_CERTIFICATE_VERIFY: i64 = 15
14const HS_FINISHED: i64 = 20
15
16// encode a handshake message: [msg_type(1)][uint24 length BE][body]. returns total bytes.
17func quic_hs_encode(out: *u8, msg_type: i64, body: *u8, body_len: i64) -> i64 {
18 out[0] = msg_type as u8
19 out[1] = ((body_len >> 16) & 0xff) as u8
20 out[2] = ((body_len >> 8) & 0xff) as u8
21 out[3] = (body_len & 0xff) as u8
22 var i: i64 = 0; while i < body_len { out[4 + i] = body[i]; i = i + 1 }
23 return 4 + body_len
24}
25
26// parse a handshake message header. info[0]=msg_type, info[1]=body length, info[2]=body offset (=4).
27// returns 0 ok / -1 truncated.
28func quic_hs_parse(buf: *u8, n: i64, info: *i64) -> i64 {
29 if n < 4 { return 0 - 1 }
30 info[0] = buf[0] as i64
31 let blen: i64 = ((buf[1] as i64) << 16) | ((buf[2] as i64) << 8) | (buf[3] as i64)
32 if 4 + blen > n { return 0 - 1 }
33 info[1] = blen
34 info[2] = 4
35 return 0
36}
37
38// place a CRYPTO-frame fragment at its stream offset in the handshake reassembly buffer; return the
39// new high-water mark (offset+dlen). Out-of-order fragments work because each writes at its own offset.
40func quic_hs_reassemble(hsbuf: *u8, data: *u8, offset: i64, dlen: i64, hwm: i64) -> i64 {
41 var i: i64 = 0; while i < dlen { hsbuf[offset + i] = data[i]; i = i + 1 }
42 let end: i64 = offset + dlen
43 if end > hwm { return end }
44 return hwm
45}
46
47func main() -> i64 { return 0 }