code wiki / (root) / nx_browser_own_site_live_test.nx

nx_browser_own_site_live_test.nx source

↩ module page · 143 lines · 5303 B

1// nx_browser_own_site_live_test.nx -- Phase-5 keystone row: LIVE 2// sovereign HTTPS fetch of OUR OWN site (nishifamily.com) through 3// the full bits-up stack: DNS -> TCP -> TLS 1.3 handshake (incl. 4// X.509 chain validation vs the real Mozilla trust store) -> HTTP 5// GET / -> parse -> save body to /tmp/nishi_own.html for the render 6// stage (compose-by-file, same pattern as example_com.html). 7// 8// Prereq: /tmp/mozilla_certdata.txt staged (real NSS bundle). 9// Exit 0 = the browser can fetch its own home page end-to-end. 10// Nonzero exit names the failing stage (gate-row contract). 11// 12// expect_exit: 0 13// license_tier: ORIGINAL 14 15import "nx_syscalls.nx" 16import "nx_x509_trust_store.nx" 17import "nx_trust_store_load_from_certdata.nx" 18import "nx_tls13_client_validate_certificate.nx" 19import "nx_tls13_client_session_run.nx" 20import "nx_https_url_for_fetch.nx" 21import "nx_https_url_connect.nx" 22import "nx_https_get.nx" 23import "nx_https_get_complete.nx" 24import "nx_http_response_parse.nx" 25 26func os_dec(label0: i64, label1: i64, v: i64) -> i64 { 27 let lab: *u8 = sys_mmap(8) 28 lab[0] = label0 as u8; lab[1] = label1 as u8; lab[2] = 0x3D 29 sys_write(1, lab, 3) 30 var av: i64 = v 31 if av < 0 { 32 let neg: *u8 = sys_mmap(8); neg[0] = 0x2D; sys_write(1, neg, 1) 33 av = 0 - av 34 } 35 if av == 0 { 36 let z: *u8 = sys_mmap(8); z[0] = 0x30; sys_write(1, z, 1) 37 } 38 if av > 0 { 39 let buf: *u8 = sys_mmap(32) 40 var pos: i64 = 0 41 var x: i64 = av 42 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 } 43 let out: *u8 = sys_mmap(32) 44 var oi: i64 = 0 45 while oi < pos { out[oi] = buf[pos - 1 - oi]; oi = oi + 1 } 46 sys_write(1, out, pos) 47 } 48 let nl: *u8 = sys_mmap(8); nl[0] = 0x0A; sys_write(1, nl, 1) 49 return 0 50} 51 52func main() -> i64 { 53 // ---- Trust store from the real Mozilla bundle ---- 54 let cpath: *u8 = "/tmp/mozilla_certdata.txt\x00" 55 let r: i64 = nx_trust_store_load_from_certdata(cpath, 300, 4194304) 56 if r <= 0 { os_dec(0x4C, 0x4F, r); return 1 } 57 let store: *TrustStore = r as *TrustStore 58 let n: i64 = trust_store_count(store) 59 os_dec(0x43, 0x41, n) // CA= 60 if n < 50 { return 3 } 61 62 // ---- Target: our own site ---- 63 let url: *u8 = "https://nishifamily.com/\x00" 64 65 let cr: *u8 = sys_mmap(32) 66 var i: i64 = 0 67 while i < 32 { cr[i] = (0xC0 + i) as u8; i = i + 1 } 68 let priv: *u8 = sys_mmap(32) 69 i = 0 70 while i < 32 { priv[i] = (0xA0 + i) as u8; i = i + 1 } 71 72 let url_p: *NxUrl = nx_url_new() 73 let target_raw: *u8 = sys_mmap(32) 74 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget 75 target.url = url_p 76 target.port = 0 77 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 41 } 78 79 let fd_p: *i64 = sys_mmap(16) as *i64 80 if nx_https_url_connect(target, url, 1781200001, fd_p) != NX_HTTPS_CONNECT_OK { return 42 } 81 let fd: i64 = *fd_p 82 sys_set_socket_timeout(fd, 10) 83 84 let val_ctx_raw: *u8 = sys_mmap(64) 85 let val_ctx: *TlsValidationContext = val_ctx_raw as *TlsValidationContext 86 val_ctx.store = store 87 val_ctx.sni_host = url + target.url.host_off 88 val_ctx.sni_host_len = target.url.host_len 89 val_ctx.now_epoch = sys_now_realtime_sec() // REAL clock: hardcoded epochs rot when live sites renew certs (B1 root cause) 90 91 let sr: i64 = nx_tls13_client_session_run( 92 fd, url + target.url.host_off, target.url.host_len, 93 cr, priv, val_ctx 94 ) 95 os_dec(0x53, 0x52, sr) // SR= 96 if sr <= 0 { sys_close(fd); return 200 + (0 - sr) } 97 98 // ---- TLS CONNECTED to our own server: HTTP GET / ---- 99 let session: *Tls13ClientSession = sr as *Tls13ClientSession 100 let buf: *u8 = sys_mmap(262144) 101 let path: *u8 = "/\x00" 102 let gc: i64 = nx_https_get_complete( 103 session, fd, 104 path, 1, 105 url + target.url.host_off, target.url.host_len, 106 buf, 262144 107 ) 108 sys_close(fd) 109 os_dec(0x47, 0x43, gc) // GC= 110 if gc < 0 { return 100 + (0 - gc) } 111 112 let resp: *i64 = sys_mmap(128) as *i64 113 let pv: i64 = nx_http_response_parse(buf, gc, resp) 114 os_dec(0x50, 0x56, pv) // PV= 115 if pv != 0 { return 50 } 116 let status: i64 = resp[1] 117 os_dec(0x53, 0x54, status) // ST= 118 if status != 200 { return 51 } 119 let body_off: i64 = resp[6] 120 let body_kind: i64 = resp[8] 121 os_dec(0x42, 0x4B, body_kind) // BK= 122 123 // ---- Body bytes (dechunk if needed) -> /tmp/nishi_own.html ---- 124 let html: *u8 = sys_mmap(262144) 125 var html_len: i64 = 0 126 if body_kind == 2 { 127 html_len = nx_http_dechunk(buf + body_off, gc - body_off, html, 262144) 128 } 129 if body_kind != 2 { 130 html_len = gc - body_off 131 var ci: i64 = 0 132 while ci < html_len { html[ci] = buf[body_off + ci]; ci = ci + 1 } 133 } 134 os_dec(0x48, 0x4C, html_len) // HL= 135 if html_len <= 0 { return 60 } 136 137 let outpath: *u8 = "/tmp/nishi_own.html\x00" 138 let ofd: i64 = sys_openat_wr(outpath, 0x1A4) 139 if ofd <= 0 { return 70 } 140 sys_write(ofd, html, html_len) 141 sys_close(ofd) 142 return 0 143}