nx_uxf_fetch_protobuf.nx source
↩ module page · 124 lines · 4670 B
1// nx_uxf_fetch_protobuf.nx -- UXF arc R1b-3: SOVEREIGN external fetch of the SECOND
2// decoupling source (Protocol Buffers: reserved field-numbers + backward/forward
3// compatibility = the schema-evolution discipline, in explicit terminology -- the
4// corroborating counterpart to Avro's self-describing-schema decoupling).
5//
6// Verbatim clone of the proven nx_uxf_fetch_avro HTTPS path; only URL/path/output differ.
7// Saves raw to knowledge/fetched/protobuf_decoupling_sovereign.raw for the multi-source
8// extractor (nx_uxf_extract). Sovereign: no browser, no node, no curl.
9// expect_exit: 0 license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_csprng.nx"
12import "nx_x509_trust_store.nx"
13import "nx_trust_store_load_from_certdata.nx"
14import "nx_tls13_client_validate_certificate.nx"
15import "nx_tls13_client_session_run.nx"
16import "nx_https_url_for_fetch.nx"
17import "nx_https_url_connect.nx"
18import "nx_https_get.nx"
19import "nx_https_get_complete.nx"
20import "nx_http_response_parse.nx"
21const K_MAGIC_4194304: i64 = 4194304
22const K_MAGIC_2097152: i64 = 2097152
23
24func dump_dec(label0: i64, label1: i64, v: i64) -> i64 {
25 let lab: *u8 = sys_mmap(8)
26 lab[0] = label0 as u8; lab[1] = label1 as u8; lab[2] = 0x3D
27 sys_write(2, lab, 3)
28 var av: i64 = v
29 if av < 0 { let neg: *u8 = sys_mmap(8); neg[0]=0x2D; sys_write(2, neg, 1); av = 0 - av }
30 if av == 0 { let z: *u8 = sys_mmap(8); z[0]=0x30; sys_write(2, z, 1) }
31 else {
32 let buf: *u8 = sys_mmap(16)
33 var pos: i64 = 0
34 var x: i64 = av
35 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }
36 let out: *u8 = sys_mmap(16)
37 var oi: i64 = 0
38 while oi < pos { out[oi] = buf[pos - 1 - oi]; oi = oi + 1 }
39 sys_write(2, out, pos)
40 }
41 let nl: *u8 = sys_mmap(8); nl[0]=0x0A; sys_write(2, nl, 1)
42 return 0
43}
44
45func main() -> i64 {
46 let cpath: *u8 = "/tmp/mozilla_certdata.txt\x00"
47 let r: i64 = nx_trust_store_load_from_certdata(cpath, 512, K_MAGIC_4194304)
48 dump_dec(0x4C, 0x4F, r)
49 if r <= 0 { return 1 }
50 let store: *TrustStore = r as *TrustStore
51 let n: i64 = trust_store_count(store)
52 dump_dec(0x43, 0x41, n)
53 if n < 50 { return 3 }
54
55 let url: *u8 = "https://en.wikipedia.org/wiki/Protocol_Buffers\x00"
56
57 let cr: *u8 = sys_mmap(32)
58 var i: i64 = 0
59 nx_csprng_fill(cr, 32) // CWE-330 (debt 1785970852): was the constant 0xC0..0xDF
60 let priv: *u8 = sys_mmap(32)
61 i = 0
62 nx_csprng_fill(priv, 32) // CWE-330: the X25519 scalar was the constant 0xA0..0xBF on EVERY session
63
64 let url_p: *NxUrl = nx_url_new()
65 let target_raw: *u8 = sys_mmap(32)
66 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget
67 target.url = url_p
68 target.port = 0
69 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 41 }
70
71 let fd_p: *i64 = sys_mmap(16) as *i64
72 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { return 42 }
73 let fd: i64 = *fd_p
74
75 let val_ctx_raw: *u8 = sys_mmap(64)
76 let val_ctx: *TlsValidationContext = val_ctx_raw as *TlsValidationContext
77 val_ctx.store = store
78 val_ctx.sni_host = url + target.url.host_off
79 val_ctx.sni_host_len = target.url.host_len
80 val_ctx.now_epoch = sys_now_realtime_sec()
81
82 let sr: i64 = nx_tls13_client_session_run(
83 fd, url + target.url.host_off, target.url.host_len,
84 cr, priv, val_ctx
85 )
86 dump_dec(0x53, 0x52, sr)
87 if sr <= 0 { sys_close(fd); return 200 + (0 - sr) }
88
89 let session: *Tls13ClientSession = sr as *Tls13ClientSession
90 let buf: *u8 = sys_mmap(K_MAGIC_2097152)
91 let path: *u8 = "/wiki/Protocol_Buffers\x00"
92 let gc: i64 = nx_https_get_complete(
93 session, fd, path, 22,
94 url + target.url.host_off, target.url.host_len,
95 buf, K_MAGIC_2097152
96 )
97 sys_close(fd)
98 dump_dec(0x47, 0x43, gc)
99 if gc < 0 { return 100 + (0 - gc) }
100
101 let rs: *i64 = sys_mmap(128) as *i64
102 let pv: i64 = nx_http_response_parse(buf, gc, rs)
103 dump_dec(0x50, 0x56, pv)
104 let status: i64 = rs[1]
105 dump_dec(0x53, 0x54, status)
106 let body_off: i64 = rs[6]
107 let body_kind: i64 = rs[8]
108 dump_dec(0x42, 0x4F, body_off)
109 dump_dec(0x42, 0x4B, body_kind)
110
111 let outpath: *u8 = "knowledge/fetched/protobuf_decoupling_sovereign.raw\x00"
112 let ofd: i64 = sys_openat_wr(outpath, 0x1A4)
113 if ofd <= 0 { return 70 }
114 sys_write(ofd, buf, gc)
115 sys_close(ofd)
116
117 let banner: *u8 = "PROTOBUF-SOVEREIGN-FETCH-OK\n\x00"
118 var bi: i64 = 0
119 while banner[bi] != (0 as u8) { bi = bi + 1 }
120 sys_write(1, banner, bi)
121
122 if status != 200 { return 51 }
123 return 0
124}