← Back to Demos & Examples

21_io_streams_dns_and_pqc_tls.vk

Raw .vk
// Example 21: Connected I/O Streams, DNS Wire Codec, Hybrid Post-Quantum TLS 1.3 (X25519MLKEM768) & HTTP/2
// Demonstrates Phase 3A/3B libraries: `rand/uuid-v7`, `io/fs`, `io/open`, `net/dns`,
// hybrid post-quantum TLS 1.3 (`0x11EC` = X25519 + FIPS 203 ML-KEM-768) with ALPN `'h2'`,
// and RFC 9113 `net/http2` RPC over `tls13/tls-stream`.
//
// Track A (ORO VM) : veko run --allow-net --allow-listen --allow-private-net examples/21_io_streams_dns_and_pqc_tls.vk
// Track B (Wasm 3) : veko run --wasm --check-slabs --fuel=0 --allow-net --allow-listen --allow-private-net --allow-fs=. examples/21_io_streams_dns_and_pqc_tls.vk
// Track C (Native) : veko compile --native examples/21_io_streams_dns_and_pqc_tls.vk -o dist/21.exe ; ./dist/21.exe

use [
	io
	ifs   = io/fs
	iopen = io/open
	rnd   = rand
	dns   = net/dns
	pem   = codec/pem
	sha   = crypto/sha256
	x509  = crypto/x509
	tls13 = net/tls13
	h2    = net/http2
]

// 1. Time-ordered RFC 9562 UUID v7 backed by `time/unix`
u1 = rnd/uuid-v7
u2 = rnd/uuid-v7
if (len u1) != 36 or (len u2) != 36 or u1[14..15] != '7' [
	err 'example21: invalid UUID v7'
]
println (str/join ['uuid7=' u1] '')

// 2. Positioned `io/fs` + unified `io/open` (`mem://` and `file://`)
tmp_path = str/join [(os/temp-dir) '/veko_ex21_stream.dat'] ''
_ = os/remove/or tmp_path [.ok]
defer [ _ = os/remove/or tmp_path [.ok] ]

mut wf = ifs/open-write tmp_path
_ = io/write-all mut wf (bytes/from-str 'veko-phase3-io-stream')
_ = ifs/sync wf
_ = io/close mut wf

mut ms = iopen/open 'mem://hello-veko'
mem_txt = bytes/to-str (io/read-all mut ms)
mut fs_st = iopen/open (str/join ['file://' tmp_path] '')
file_txt = bytes/to-str (io/read-all mut fs_st)
_ = io/close mut fs_st
if mem_txt != 'hello-veko' or file_txt != 'veko-phase3-io-stream' [
	err 'example21: io/open mismatch'
]
println (str/join ['io=' mem_txt '|' file_txt] '')

// 3. Pure-Veko RFC 1035 DNS wire codec + `/etc/hosts` & `net/resolve`
qwire = dns/encode-query 0x2026 'api.veko.dev' dns/TYPE-A
hosts = dns/parse-hosts '127.0.0.1 localhost api.veko.local\n'
local_ips = net/resolve 'localhost'
if (len qwire) < 20 or (len local_ips) == 0 or not (map/has? hosts 'api.veko.local') [
	err 'example21: dns/resolve failed'
]
println (str/join ['dns=q' (mold (len qwire)) 'B|localhost=' (str/append local_ips[0] '')] '')

// 4. Live loopback Hybrid Post-Quantum TLS 1.3 (X25519MLKEM768) + ALPN 'h2' + HTTP/2
ca_seed = sha/sha256 (bytes/from-str 'veko-ex21-pqc-ca')
leaf_seed = sha/sha256 (bytes/from-str 'veko-ex21-pqc-leaf')
ca_der = x509/mint-ed25519-cert {
	issuer_cn = 'Veko PQC Root', subject_cn = 'Veko PQC Root', is_ca = true
	subject_seed = ca_seed, signer_seed = ca_seed, serial = 101
}
leaf_der = x509/mint-ed25519-cert {
	issuer_cn = 'Veko PQC Root', subject_cn = 'pqc.veko.local', is_ca = false
	san_dns = ['pqc.veko.local'], san_ips = ['127.0.0.1']
	subject_seed = leaf_seed, signer_seed = ca_seed, serial = 102
}
roots_pem = pem/pem-encode 'CERTIFICATE' ca_der

ln = net/listen-tcp .ip '127.0.0.1:0'
port = str/to-i64 (str/split (net/addr ln) ':')[1]
done_ch = chan/new 1

srv = spawn [
	sock = net/accept ln
	mut sconn = tls13/server-handshake sock [leaf_der ca_der] leaf_seed
	mut tst = tls13/tls-stream sconn
	mut h2s = h2/server-accept mut tst
	req = h2/read-request mut h2s
	reply = str/join ['pqc-h2:' req.method ':' req.path ':group=' sconn.group ':alpn=' sconn.alpn] ''
	_ = h2/write-response mut h2s req.stream_id 200 { 'content-type' = 'text/plain' } (bytes/from-str reply)
	_ = chan/recv done_ch
	mut s_st = h2s.stream
	_ = io/close mut s_st
	sconn.group
]

mut cconn = tls13/connect '127.0.0.1' port {
	server_name = 'pqc.veko.local'
	roots_pem   = roots_pem
	alpn        = 'h2'
	pqc         = true
}
mut ctst = tls13/tls-stream cconn
mut h2c = h2/client-open mut ctst
resp = h2/request mut h2c 'GET' '/v1/quantum' 'pqc.veko.local' {} (bytes/with-cap 0)
_ = chan/send done_ch .ok
mut c_st = h2c.stream
_ = io/close mut c_st
srv_group = task/await srv
_ = net/close ln

if err? resp [
	err (str/join ['example21: HTTP/2 request failed: ' (mold resp)] '')
]
body_txt = bytes/to-str resp.body
if resp.status != 200 or cconn.group != 'X25519MLKEM768' or srv_group != 'X25519MLKEM768' or cconn.alpn != 'h2' [
	err (str/join ['example21: PQC TLS1.3 + HTTP/2 mismatch: status=' (mold resp.status) ' group=' (mold cconn.group)] '')
]
println (str/join ['pqc_h2=' (mold resp.status) '|' body_txt] '')