code wiki / _hdl_build / nx_docportal_at_rest_gate.nx
nx_docportal_at_rest_gate.nx source
↩ module page · 143 lines · 8285 B
1// nx_docportal_at_rest_gate.nx -- MEASURED proof that the P1 at-rest SWAP fired at the REAL store site: it drives
2// the pure router da_handle (the exact code changed in nx_docportal_admin_lib) with crafted upload requests and
3// inspects the sovereign store. Fresh nonce domain per run. STANDALONE -- no socket, no daemon process, no deploy.
4// T1 POST /admin/upload visibility=private -> 200 UPLOADED, and the STORED doc:<cid> begins with the NXENC1 magic
5// (the confidential upload is ENCRYPTED at rest, not plaintext).
6// T2 doc_get(private) round-trips the encrypted doc BYTE-IDENTICAL (P2's private route will read exactly this).
7// T3 POST /admin/upload visibility=public -> 200 UPLOADED, and the STORED doc:<cid> is PLAINTEXT (no magic, equals
8// the body) -- the PUBLIC corpus path is unchanged at the same call site.
9// Print pass=3 + GREEN/RED, append knowledge/status/dp_at_rest_wire_gate.log. license_tier: ORIGINAL
10import "nx_docportal_admin_lib.nx" // da_handle + DaCtx (the swapped store site)
11import "nx_doc_at_rest.nx" // doc_get / at_is_magic
12import "nx_docportal_lib.nx" // dp_cid / dp_prefix / dp_key / DP_VIS_* + ss_open/ss_hget
13
14func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
15func g_num(v: i64) -> i64 {
16 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
17 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
18 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
19 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
20}
21func g_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
22func g_wn(fd: i64, v: i64) -> i64 {
23 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }
24 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
25 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
26 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(fd, bb, k); return 0
27}
28func g_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
29func g_cat(out: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { out[o] = s[i]; o = o + 1; i = i + 1 } return o }
30func g_catnum(out: *u8, o: i64, v: i64) -> i64 {
31 if v == 0 { out[o] = 48 as u8; return o + 1 }
32 var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0
33 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
34 var i: i64 = 0; while i < k { out[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } return o
35}
36func g_eqbytes(a: *u8, b: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { if a[i] != b[i] { return 0 } i = i + 1 } return 1 }
37func g_starts(buf: *u8, n: i64, s: *u8) -> i64 { let sn: i64 = g_slen(s); if n < sn { return 0 } var i: i64 = 0; while i < sn { if buf[i] != s[i] { return 0 } i = i + 1 } return 1 }
38func g_contains(hay: *u8, hn: i64, ndl: *u8) -> i64 {
39 let nl: i64 = g_slen(ndl); if nl == 0 { return 0 }
40 if hn < nl { return 0 }
41 var i: i64 = 0
42 while i + nl <= hn {
43 var j: i64 = 0; var hit: i64 = 1
44 while j < nl { if hay[i + j] != ndl[j] { hit = 0; j = nl } else { j = j + 1 } }
45 if hit == 1 { return 1 }
46 i = i + 1
47 }
48 return 0
49}
50
51func main() -> i64 {
52 g_puts("=== DOCPORTAL AT-REST WIRE GATE (da_handle upload -> confidential encrypted, public plaintext) ===\n" as *u8)
53 let nonce: i64 = sys_now_realtime_sec()
54 let dom: *u8 = sys_mmap(64)
55 var dn: i64 = g_cat(dom, 0, "dpwire" as *u8); dn = g_catnum(dom, dn, nonce); dom[dn] = 0 as u8
56
57 let dctx: *DaCtx = sys_mmap(16) as *DaCtx
58 dctx.user_level = 2
59 let req: *u8 = sys_mmap(8192)
60 let out: *u8 = sys_mmap(524288)
61 let pq: *i64 = sys_mmap(16) as *i64
62 let lq: *i64 = sys_mmap(16) as *i64
63
64 var pass: i64 = 0
65
66 // T1: PRIVATE upload through da_handle -> stored doc has NXENC1 magic
67 let ptE: *u8 = "WIRE-CONFIDENTIAL-CANARY client vault document via the live upload handler." as *u8
68 let ptElen: i64 = g_slen(ptE)
69 var r: i64 = g_cat(req, 0, "POST /admin/upload?domain=" as *u8)
70 r = g_cat(req, r, dom)
71 r = g_cat(req, r, "&visibility=private HTTP/1.1\r\nHost: x\r\n\r\n" as *u8)
72 r = g_cat(req, r, ptE)
73 let resp1: i64 = da_handle(dctx, req, r, out)
74 let cidE: i64 = dp_cid(ptE, ptElen)
75 let prefixP: *u8 = sys_mmap(512); dp_prefix(dom, DP_VIS_PRIVATE, prefixP)
76 let dkeyE: *u8 = sys_mmap(64); dp_key(cidE, dkeyE)
77 var magicok: i64 = 0
78 var rawElen: i64 = 0
79 let hP: *i64 = ss_open(prefixP)
80 if (hP as i64) != 0 {
81 if ss_hget(hP, dkeyE, pq, lq) == 1 {
82 rawElen = lq[0]
83 magicok = at_is_magic(pq[0] as *u8, rawElen)
84 }
85 }
86 let resp1ok: i64 = g_starts(out, resp1, "HTTP/1.1 200" as *u8)
87 let resp1up: i64 = g_contains(out, resp1, "UPLOADED" as *u8)
88 var t1: i64 = 0
89 if resp1ok == 1 { if resp1up == 1 { if magicok == 1 { t1 = 1 } } }
90 pass = pass + t1
91 g_puts(" T1 private upload -> 200 UPLOADED + stored ENCRYPTED (magic=" as *u8); g_num(magicok); g_puts(" rawlen=" as *u8); g_num(rawElen); g_puts("): " as *u8)
92 if t1 == 1 { g_puts("PASS\n" as *u8) } else { g_puts("FAIL\n" as *u8) }
93
94 // T2: doc_get(private) round-trips byte-identical
95 let outE: *u8 = sys_mmap(ptElen + 128)
96 let gotE: i64 = doc_get(dom, DP_VIS_PRIVATE, cidE, outE)
97 var t2: i64 = 0
98 if gotE == ptElen { if g_eqbytes(outE, ptE, ptElen) == 1 { t2 = 1 } }
99 pass = pass + t2
100 g_puts(" T2 doc_get(private) round-trips (len=" as *u8); g_num(gotE); g_puts(" of " as *u8); g_num(ptElen); g_puts("): " as *u8)
101 if t2 == 1 { g_puts("PASS\n" as *u8) } else { g_puts("FAIL\n" as *u8) }
102
103 // T3: PUBLIC upload through da_handle -> stored PLAINTEXT (no magic, equals body) -- public path unchanged
104 let ptP: *u8 = "WIRE-PUBLIC public estate planning article for the corpus." as *u8
105 let ptPlen: i64 = g_slen(ptP)
106 var r2: i64 = g_cat(req, 0, "POST /admin/upload?domain=" as *u8)
107 r2 = g_cat(req, r2, dom)
108 r2 = g_cat(req, r2, "&visibility=public&pub_search=1 HTTP/1.1\r\nHost: x\r\n\r\n" as *u8)
109 r2 = g_cat(req, r2, ptP)
110 let resp2: i64 = da_handle(dctx, req, r2, out)
111 let cidP: i64 = dp_cid(ptP, ptPlen)
112 let prefixPub: *u8 = sys_mmap(512); dp_prefix(dom, DP_VIS_PUBLIC, prefixPub)
113 let dkeyP: *u8 = sys_mmap(64); dp_key(cidP, dkeyP)
114 var pubNoMagic: i64 = 0
115 var pubPlain: i64 = 0
116 let hPub: *i64 = ss_open(prefixPub)
117 if (hPub as i64) != 0 {
118 if ss_hget(hPub, dkeyP, pq, lq) == 1 {
119 let pubvp: *u8 = pq[0] as *u8
120 let publen: i64 = lq[0]
121 if at_is_magic(pubvp, publen) == 0 { pubNoMagic = 1 }
122 if publen == ptPlen { if g_eqbytes(pubvp, ptP, ptPlen) == 1 { pubPlain = 1 } }
123 }
124 }
125 let resp2ok: i64 = g_starts(out, resp2, "HTTP/1.1 200" as *u8)
126 let resp2up: i64 = g_contains(out, resp2, "UPLOADED" as *u8)
127 var t3: i64 = 0
128 if resp2ok == 1 { if resp2up == 1 { if pubNoMagic == 1 { if pubPlain == 1 { t3 = 1 } } } }
129 pass = pass + t3
130 g_puts(" T3 public upload -> 200 UPLOADED + stored PLAINTEXT (no_magic=" as *u8); g_num(pubNoMagic); g_puts(" plain=" as *u8); g_num(pubPlain); g_puts("): " as *u8)
131 if t3 == 1 { g_puts("PASS\n" as *u8) } else { g_puts("FAIL\n" as *u8) }
132
133 g_puts("----\nDP-AT-REST-WIRE rows=3 pass=" as *u8); g_num(pass); g_puts("\n" as *u8)
134 let lg: i64 = sys_openat_append("knowledge/status/dp_at_rest_wire_gate.log" as *u8, 0x1a4)
135 if lg >= 0 {
136 g_w(lg, "DP-AT-REST-WIRE private_encrypted=" as *u8); g_wn(lg, t1); g_w(lg, " private_roundtrip=" as *u8); g_wn(lg, t2)
137 g_w(lg, " public_plaintext=" as *u8); g_wn(lg, t3); g_w(lg, " rows=3 pass=" as *u8); g_wn(lg, pass)
138 if pass == 3 { g_w(lg, " verdict=GREEN\n" as *u8) } else { g_w(lg, " verdict=RED\n" as *u8) }
139 sys_close(lg)
140 }
141 if pass == 3 { g_puts("DP-AT-REST-WIRE GREEN (confidential upload encrypted at the real store site, public plaintext unchanged -- measured)\n" as *u8); sys_exit(0); return 0 }
142 g_puts("DP-AT-REST-WIRE RED\n" as *u8); sys_exit(1); return 1
143}