code wiki / (root) / nx_https_get_live_real_ca_test.nx

nx_https_get_live_real_ca_test.nx source

↩ module page · 182 lines · 7074 B

1// nx_https_get_live_real_ca_test.nx -- THE final Arc A milestone. 2// Loads the real Mozilla certdata.txt bundle via the substrate's 3// shipped certdata parser + trust store loader, then attempts a 4// LIVE HTTPS GET against real public example.com using the 5// populated TrustStore. 6// 7// If this succeeds end-to-end, the substrate has its FIRST EVER 8// byte of real public HTTPS response data, validated through a 9// real CA chain rooted at a real Mozilla anchor. 10// 11// expect_exit: 0 12// license_tier: ORIGINAL 13 14import "nx_syscalls.nx" 15import "nx_x509_trust_store.nx" 16import "nx_trust_store_load_from_certdata.nx" 17import "nx_tls13_client_validate_certificate.nx" 18import "nx_tls13_client_session_run.nx" 19import "nx_https_url_for_fetch.nx" 20import "nx_https_url_connect.nx" 21import "nx_https_get.nx" 22import "nx_https_get_complete.nx" 23import "nx_http_response_parse.nx" 24 25func dump_dec(label0: i64, label1: i64, v: i64) -> i64 { 26 let lab: *u8 = sys_mmap(8) 27 lab[0] = label0 as u8; lab[1] = label1 as u8; lab[2] = 0x3D 28 sys_write(2, lab, 3) 29 var av: i64 = v 30 if av < 0 { 31 let neg: *u8 = sys_mmap(8); neg[0]=0x2D; sys_write(2, neg, 1) 32 av = 0 - av 33 } 34 if av == 0 { 35 let z: *u8 = sys_mmap(8); z[0]=0x30; sys_write(2, z, 1) 36 } else { 37 // Up to 10 digits 38 let buf: *u8 = sys_mmap(16) 39 var pos: i64 = 0 40 var x: i64 = av 41 while x > 0 { 42 buf[pos] = (0x30 + (x % 10)) as u8 43 x = x / 10 44 pos = pos + 1 45 } 46 // reverse 47 let out: *u8 = sys_mmap(16) 48 var oi: i64 = 0 49 while oi < pos { 50 out[oi] = buf[pos - 1 - oi] 51 oi = oi + 1 52 } 53 sys_write(2, out, pos) 54 } 55 let nl: *u8 = sys_mmap(8); nl[0]=0x0A; sys_write(2, nl, 1) 56 return 0 57} 58 59func main() -> i64 { 60 // ---- Load real Mozilla certdata.txt into a TrustStore ---- 61 let path: *u8 = sys_mmap(64) 62 path[0]=0x2F; path[1]=0x74; path[2]=0x6D; path[3]=0x70 // /tmp 63 path[4]=0x2F // / 64 path[5]=0x6D; path[6]=0x6F; path[7]=0x7A; path[8]=0x69 // mozi 65 path[9]=0x6C; path[10]=0x6C; path[11]=0x61 // lla 66 path[12]=0x5F // _ 67 path[13]=0x63; path[14]=0x65; path[15]=0x72; path[16]=0x74 68 path[17]=0x64; path[18]=0x61; path[19]=0x74; path[20]=0x61 // certdata 69 path[21]=0x2E; path[22]=0x74; path[23]=0x78; path[24]=0x74 // .txt 70 path[25]=0 // NUL 71 72 let r: i64 = nx_trust_store_load_from_certdata(path, 300, 4194304) 73 dump_dec(0x4C, 0x4F, r) // "LO=" load result 74 if r <= 0 { return 1 } 75 let store: *TrustStore = r as *TrustStore 76 let n: i64 = trust_store_count(store) 77 dump_dec(0x43, 0x41, n) // "CA=" count 78 79 if n <= 0 { return 2 } 80 if n < 50 { return 3 } // expect 100+ CAs 81 82 // ---- LIVE HTTPS GET against example.com with real CA store ---- 83 let url: *u8 = sys_mmap(64) 84 url[0]=0x68; url[1]=0x74; url[2]=0x74; url[3]=0x70; url[4]=0x73 85 url[5]=0x3A; url[6]=0x2F; url[7]=0x2F 86 url[8]=0x65; url[9]=0x78; url[10]=0x61; url[11]=0x6D 87 url[12]=0x70; url[13]=0x6C; url[14]=0x65; url[15]=0x2E 88 url[16]=0x63; url[17]=0x6F; url[18]=0x6D 89 url[19]=0x2F 90 url[20]=0 91 92 let cr: *u8 = sys_mmap(32) 93 var i: i64 = 0 94 while i < 32 { cr[i] = (0xC0 + i) as u8; i = i + 1 } 95 let priv: *u8 = sys_mmap(32) 96 i = 0 97 while i < 32 { priv[i] = (0xA0 + i) as u8; i = i + 1 } 98 99 // First call session_run directly to get the INNER verdict. 100 let url_p: *NxUrl = nx_url_new() 101 let target_raw: *u8 = sys_mmap(32) 102 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget 103 target.url = url_p 104 target.port = 0 105 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 41 } 106 107 let fd_p: *i64 = sys_mmap(16) as *i64 108 if nx_https_url_connect(target, url, 1779284141, fd_p) != NX_HTTPS_CONNECT_OK { return 42 } 109 let fd: i64 = *fd_p 110 111 let val_ctx_raw: *u8 = sys_mmap(64) 112 let val_ctx: *TlsValidationContext = val_ctx_raw as *TlsValidationContext 113 val_ctx.store = store 114 val_ctx.sni_host = url + target.url.host_off 115 val_ctx.sni_host_len = target.url.host_len 116 val_ctx.now_epoch = sys_now_realtime_sec() // REAL clock: hardcoded epochs rot when live sites renew certs (B1 root cause) 117 118 let sr: i64 = nx_tls13_client_session_run( 119 fd, url + target.url.host_off, target.url.host_len, 120 cr, priv, val_ctx 121 ) 122 dump_dec(0x53, 0x52, sr) // "SR=" session_run verdict 123 if sr <= 0 { 124 sys_close(fd) 125 // Handshake failure -- surface the verdict as exit code. 126 return 200 + (0 - sr) 127 } 128 129 // ---- Session reached CONNECTED -- send real HTTP GET ---- 130 let session: *Tls13ClientSession = sr as *Tls13ClientSession 131 let buf: *u8 = sys_mmap(65536) 132 let path: *u8 = sys_mmap(4) 133 path[0]=0x2F // "/" 134 let gc: i64 = nx_https_get_complete( 135 session, fd, 136 path, 1, 137 url + target.url.host_off, target.url.host_len, 138 buf, 65536 139 ) 140 sys_close(fd) 141 dump_dec(0x47, 0x43, gc) // "GC=" get_complete verdict 142 if gc < 0 { return 100 + (0 - gc) } 143 144 // ---- Parse HTTP/1.1 response structure ---- 145 let r: *i64 = sys_mmap(128) as *i64 146 let pv: i64 = nx_http_response_parse(buf, gc, r) 147 dump_dec(0x50, 0x56, pv) // "PV=" parse verdict 148 if pv != 0 { return 50 } 149 let status: i64 = r[1] 150 dump_dec(0x53, 0x54, status) // "ST=" status code 151 if status != 200 { return 51 } 152 let body_off: i64 = r[6] 153 let body_kind: i64 = r[8] 154 dump_dec(0x42, 0x4F, body_off) // "BO=" body offset 155 dump_dec(0x42, 0x4B, body_kind) // "BK=" body kind (2 = chunked) 156 157 // ---- Dechunk the body to extract pure HTML ---- 158 let html: *u8 = sys_mmap(8192) 159 let html_len: i64 = nx_http_dechunk(buf + body_off, gc - body_off, html, 8192) 160 dump_dec(0x48, 0x4C, html_len) // "HL=" decoded HTML length 161 162 if html_len < 0 { return 60 } 163 164 // ---- Save raw HTML to /tmp/example_com.html for a separate 165 // bits-up render stage to pick up (compose-by-file, keeps 166 // this test under the bootstrap parser's module-const cap; 167 // rendering happens in nx_html_to_text_real_test.nx). 168 let outpath: *u8 = "/tmp/example_com.html\x00" 169 let ofd: i64 = sys_openat_wr(outpath, 0x1A4) // 0644 170 if ofd <= 0 { return 70 } 171 sys_write(ofd, html, html_len) 172 sys_close(ofd) 173 174 let banner: *u8 = sys_mmap(64) 175 banner[0]=0x48; banner[1]=0x54; banner[2]=0x4D; banner[3]=0x4C // "HTML" 176 banner[4]=0x3D; banner[5]=0x0A 177 sys_write(1, banner, 6) 178 sys_write(1, html, html_len) 179 let nl2: *u8 = sys_mmap(8); nl2[0]=0x0A 180 sys_write(1, nl2, 1) 181 return 0 182}