// packages/noise_tunnel/examples/demo.vk // Self-contained verification suite & runnable example for `noise_tunnel` and `noise_tunnel/socks5`. use [ bits inet = io/net io nt = noise_tunnel socks5 = noise_tunnel/socks5 ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] println (str/join ['[PASS] ' label] '') .ok ] // --- 1. `Noise_NN_25519_ChaChaPoly_SHA256` Ephemeral Handshake & Transport Rekeying --- mut nn_i = nt/nn-initiator-new/prologue (bytes/from-str 'veko-nn-v1') mut nn_r = nt/nn-responder-new/prologue (bytes/from-str 'veko-nn-v1') nn_m1 = nt/nn-write-msg1 mut nn_i (bytes/from-str 'hello-nn') nn_p1 = nt/nn-read-msg1 mut nn_r nn_m1 nn_m2_r = nt/nn-write-msg2 mut nn_r (bytes/from-str 'ack-nn') nn_done = nt/nn-read-msg2 mut nn_i nn_m2_r.wire mut nn_cli = nn_done.session mut nn_srv = nn_m2_r.session _ = nt/transport-rekey-tx mut nn_cli _ = nt/transport-rekey-rx mut nn_srv rekey_ct = nt/transport-encrypt mut nn_cli (bytes/from-str 'post-rekey-secret') rekey_pt = nt/transport-decrypt mut nn_srv rekey_ct nn_ok = (bytes/to-str nn_p1) == 'hello-nn' and (bytes/to-str nn_done.payload) == 'ack-nn' and (bits/ct-eq? nn_cli.handshake_hash nn_srv.handshake_hash) and (bytes/to-str rekey_pt) == 'post-rekey-secret' _ = assert-true nn_ok 'Noise_NN_25519_ChaChaPoly_SHA256 handshake, transcript binding, and ยง4.2 transport rekeying' // --- 2. `Noise_IKpsk2_25519_ChaChaPoly_SHA256` Mutual-Auth + PSK Handshake --- init_static = nt/keypair-from-seed (bytes/fill 32 0x11) resp_static = nt/keypair-from-seed (bytes/fill 32 0x22) shared_psk = bytes/fill 32 0x5a prol_bytes = bytes/from-str 'veko-wireguard-tunnel-v1' mut ik_i = nt/ik-initiator-new/prologue init_static resp_static.pub_key shared_psk prol_bytes mut ik_r = nt/ik-responder-new/prologue resp_static shared_psk prol_bytes msg1 = nt/ik-write-msg1 mut ik_i (bytes/from-str 'agent-reg:edge-eu-1') // Initiator static public key is AEAD-encrypted inside msg1 (never transmitted in cleartext!) static_hidden = (bits/index-of msg1 init_static.pub_key 0) < 0 r1 = nt/ik-read-msg1 mut ik_r msg1 m2_r = nt/ik-write-msg2 mut ik_r (bytes/from-str 'assigned:tunnel-01') done = nt/ik-read-msg2 mut ik_i m2_r.wire mut cli_sess = done.session mut srv_sess = m2_r.session ik_ok_1 = static_hidden and (bits/ct-eq? r1.remote_static init_static.pub_key) and (bits/ct-eq? srv_sess.remote_static init_static.pub_key) ik_ok_2 = (bytes/to-str r1.payload) == 'agent-reg:edge-eu-1' and (bytes/to-str done.payload) == 'assigned:tunnel-01' and (bits/ct-eq? cli_sess.handshake_hash srv_sess.handshake_hash) _ = assert-true (ik_ok_1 and ik_ok_2) 'Noise_IKpsk2_25519_ChaChaPoly_SHA256 mutual static-key authentication, identity hiding, and PSK binding' // --- 3. Wrong-PSK & Tampered-Ciphertext Constant-Time Rejection --- wrong_psk = bytes/fill 32 0x5b mut bad_i = nt/ik-initiator-new/prologue init_static resp_static.pub_key shared_psk prol_bytes mut bad_r = nt/ik-responder-new/prologue resp_static wrong_psk prol_bytes bad_m1 = nt/ik-write-msg1 mut bad_i (bytes/from-str 'probe') _ = nt/ik-read-msg1 mut bad_r bad_m1 bad_m2 = nt/ik-write-msg2 mut bad_r (bytes/from-str 'wrong-psk-reply') psk_res = nt/ik-read-msg2/or mut bad_i bad_m2.wire [ '__psk_rejected__' ] // Flip 1 bit in encrypted static identity inside msg1 mut tampered_m1 = bytes/with-cap (len msg1) _ = bytes/push-all mut tampered_m1 msg1 _ = bytes/set mut tampered_m1 40 (tampered_m1[40] ^ 0x01) mut fresh_r = nt/ik-responder-new/prologue resp_static shared_psk prol_bytes tamper_res = nt/ik-read-msg1/or mut fresh_r tampered_m1 [ '__tamper_rejected__' ] _ = assert-true (psk_res == '__psk_rejected__' and tamper_res == '__tamper_rejected__') 'wrong PSK and 1-bit tampered handshake ciphertext rejected cleanly' // --- 4. Multiplexed Encrypted Reverse Tunnel (`OPEN_STREAM`, `STREAM_DATA`, `PING`/`PONG`, `CLOSE_STREAM`) --- mut gw_mux = nt/new-tunnel-mux true mut agent_mux = nt/new-tunnel-mux false // Reverse gateway opens two concurrent logical streams (IDs 2 and 4) toward internal services behind the agent open_s2 = nt/mux-open-stream mut gw_mux mut srv_sess '10.0.1.25:5432' open_s4 = nt/mux-open-stream mut gw_mux mut srv_sess '10.0.1.30:8080' _ = nt/mux-ingest-packet mut agent_mux mut cli_sess (map/get open_s2 'packet') _ = nt/mux-ingest-packet mut agent_mux mut cli_sess (map/get open_s4 'packet') // Interleave chunks on streams 2 and 4 in both directions + keepalive PING/PONG sid2 = as-i64 (map/get open_s2 'stream_id') sid4 = as-i64 (map/get open_s4 'stream_id') pkt_2a = nt/mux-send-data mut srv_sess sid2 (bytes/from-str 'SELECT ') pkt_4a = nt/mux-send-data mut srv_sess sid4 (bytes/from-str 'GET /healthz HTTP/1.1\r\n\r\n') pkt_2b = nt/mux-send-data mut srv_sess sid2 (bytes/from-str '1;') _ = nt/mux-ingest-packet mut agent_mux mut cli_sess pkt_2a _ = nt/mux-ingest-packet mut agent_mux mut cli_sess pkt_4a _ = nt/mux-ingest-packet mut agent_mux mut cli_sess pkt_2b // Agent sends keepalive PING -> Gateway auto-generates encrypted PONG ping_pkt = nt/mux-ping mut cli_sess (bytes/from-str 'hb-0001') ing_ping = nt/mux-ingest-packet mut gw_mux mut srv_sess ping_pkt _ = nt/mux-ingest-packet mut agent_mux mut cli_sess (map/get ing_ping 'reply_packet') // Agent sends response data on stream 4 and closes stream 4 rep_4 = nt/mux-send-data mut cli_sess sid4 (bytes/from-str 'HTTP/1.1 200 OK\r\n\r\n') cls_4 = nt/mux-close-stream mut agent_mux mut cli_sess sid4 _ = nt/mux-ingest-packet mut gw_mux mut srv_sess rep_4 _ = nt/mux-ingest-packet mut gw_mux mut srv_sess cls_4 sql_recv = bytes/to-str (nt/mux-drain-stream mut agent_mux sid2) http_req = bytes/to-str (nt/mux-drain-stream mut agent_mux sid4) http_resp = bytes/to-str (nt/mux-drain-stream mut gw_mux sid4) mux_ok = sid2 == 2 and sid4 == 4 and sql_recv == 'SELECT 1;' and http_req == 'GET /healthz HTTP/1.1\r\n\r\n' and http_resp == 'HTTP/1.1 200 OK\r\n\r\n' and (bytes/to-str agent_mux.last_pong) == 'hb-0001' and (map/has? gw_mux.closed_ids '4') _ = assert-true mux_ok 'multiplexed encrypted reverse tunnel streams (OPEN_STREAM, STREAM_DATA, PING/PONG, CLOSE_STREAM)' // --- 5. RFC 1928 + RFC 1929 SOCKS5 Wire Codec (IPv4, Domain, IPv6) & Live `io/Stream` Handshake --- req_v4 = socks5/decode-connect-req (socks5/encode-connect-req '10.24.0.8' 5432) req_dom = socks5/decode-connect-req (socks5/encode-connect-req 'db.internal.veko' 6432) req_v6 = socks5/decode-connect-req (socks5/encode-connect-req '2001:db8::42' 443) atyp_ok = req_v4.atyp == (socks5/ATYP-IPV4) and req_v4.host == '10.24.0.8' and req_dom.atyp == (socks5/ATYP-DOMAIN) and req_dom.host == 'db.internal.veko' and req_v6.atyp == (socks5/ATYP-IPV6) and req_v6.host == '2001:db8::42' and req_v6.port == 443 _ = assert-true atyp_ok 'SOCKS5 CONNECT codec roundtrips IPv4 (0x01), Domain (0x03), and IPv6 (0x04) ATYP' // Live loopback TCP `io/Stream` SOCKS5 handshake with RFC 1929 Username/Password authentication ln = net/listen-tcp .ip '127.0.0.1:0' port_n = str/to-i64 (str/split (net/addr ln) ':')[1] socks_srv = spawn [ // Connection 1: valid RFC 1929 credentials + IPv6 target c1 = net/accept ln mut s1 = inet/tcp-stream (as-i64 c1) 'socks-cli-1' creq1 = socks5/server-accept/require_user/require_pass mut s1 'tunnel-op' 's3cr3t-veko-key' payload_in = io/read mut s1 64 _ = io/write-all mut s1 (bytes/from-str (str/join ['proxied->' creq1.host ':' (mold creq1.port) ':' (bytes/to-str payload_in)] '')) _ = io/close mut s1 // Connection 2: invalid RFC 1929 password -> rejected in constant time c2 = net/accept ln mut s2 = inet/tcp-stream (as-i64 c2) 'socks-cli-2' rej = socks5/server-accept/require_user/require_pass/or mut s2 'tunnel-op' 's3cr3t-veko-key' [ socks5/ConnectRequest { cmd = 0, atyp = 0, host = '__auth_denied__', port = 0 } ] _ = io/close mut s2 _ = net/close ln rej.host ] mut cli1 = inet/dial-tcp '127.0.0.1' port_n rep1 = socks5/client-handshake/username/password mut cli1 '2001:db8::99' 8443 'tunnel-op' 's3cr3t-veko-key' _ = io/write-all mut cli1 (bytes/from-str 'PING-UPSTREAM') tunneled_echo = bytes/to-str (io/read-all mut cli1) _ = io/close mut cli1 mut cli2 = inet/dial-tcp '127.0.0.1' port_n bad_auth = socks5/client-handshake/username/password/or mut cli2 'internal.veko' 80 'tunnel-op' 'wrong-pass' [ socks5/SocksReply { rep = 1, bind_atyp = 1, bind_host = '0.0.0.0', bind_port = 0 } ] _ = io/close mut cli2 srv_rej_host = task/await socks_srv socks_live_ok = rep1.rep == 0 and tunneled_echo == 'proxied->2001:db8::99:8443:PING-UPSTREAM' and bad_auth.rep == 1 and srv_rej_host == '__auth_denied__' _ = assert-true socks_live_ok 'live io/Stream SOCKS5 + RFC 1929 constant-time auth handshake and wrong-password rejection' println 'noise_tunnel: all verification checks passed'