nish 0.36.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +8 -7
  2. package/docs/AI.md +75 -14
  3. package/docs/INSTALL.md +92 -71
  4. package/package.json +12 -7
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/scripts/fuzz.sh +59 -8
  22. package/scripts/gen-pow10-table.py +1 -1
  23. package/scripts/postinstall.mjs +8 -7
  24. package/scripts/size-report.sh +2 -2
  25. package/scripts/undocumented.txt +14 -0
  26. package/std/LICENSE-fdlibm +8 -0
  27. package/std/README.md +109 -82
  28. package/std/array.ts +266 -0
  29. package/std/bytes.ts +23 -19
  30. package/std/compress/checksum.ts +1 -1
  31. package/std/crypto/aes.ts +48 -43
  32. package/std/crypto/base64url.ts +7 -5
  33. package/std/crypto/bcrypt-batch.ts +13 -13
  34. package/std/crypto/bcrypt.ts +33 -27
  35. package/std/crypto/chacha20poly1305.ts +16 -14
  36. package/std/crypto/ed25519.ts +19 -16
  37. package/std/crypto/hmac.ts +7 -6
  38. package/std/crypto/jwt.ts +23 -20
  39. package/std/crypto/p256.ts +63 -56
  40. package/std/crypto/p384.ts +23 -20
  41. package/std/crypto/pki.ts +18 -15
  42. package/std/crypto/rsa.ts +23 -19
  43. package/std/crypto/x25519.ts +11 -10
  44. package/std/crypto/x509.ts +69 -62
  45. package/std/host/ring.ts +124 -0
  46. package/std/host/text.ts +86 -0
  47. package/std/json-equal.ts +12 -11
  48. package/std/json-number.ts +12 -12
  49. package/std/json-tokenizer.ts +26 -22
  50. package/std/json-typed.ts +92 -78
  51. package/std/json-value.ts +38 -33
  52. package/std/json.ts +32 -28
  53. package/std/math.ts +743 -41
  54. package/std/net/dns-resolver.ts +46 -6
  55. package/std/net/dns.ts +50 -13
  56. package/std/net/fetch.ts +1744 -295
  57. package/std/net/handler.ts +30 -0
  58. package/std/net/hpack.ts +29 -26
  59. package/std/net/http-compress.ts +42 -28
  60. package/std/net/http-fields.ts +19 -4
  61. package/std/net/http1-client-codec.ts +29 -25
  62. package/std/net/http1-codec.ts +62 -91
  63. package/std/net/http1-server.ts +79 -57
  64. package/std/net/http2-frame.ts +17 -16
  65. package/std/net/http2-tls.ts +17 -15
  66. package/std/net/http2.ts +67 -54
  67. package/std/net/http3-frame.ts +11 -10
  68. package/std/net/http3-server.ts +36 -35
  69. package/std/net/http3.ts +70 -58
  70. package/std/net/postgres-async.ts +1005 -0
  71. package/std/net/postgres.ts +185 -164
  72. package/std/net/prefork.ts +38 -34
  73. package/std/net/qpack.ts +15 -14
  74. package/std/net/quic-conn-ack.ts +1 -0
  75. package/std/net/quic-conn-cid.ts +11 -0
  76. package/std/net/quic-conn-params.ts +41 -14
  77. package/std/net/quic-datagram.ts +7 -0
  78. package/std/net/quic-frame.ts +48 -12
  79. package/std/net/quic-listener.ts +61 -44
  80. package/std/net/quic-packet.ts +71 -52
  81. package/std/net/quic-recovery.ts +59 -21
  82. package/std/net/quic-stream.ts +83 -55
  83. package/std/net/quic.ts +204 -152
  84. package/std/net/serve.ts +763 -0
  85. package/std/net/tls/client.ts +1 -0
  86. package/std/net/tls/codec.ts +81 -12
  87. package/std/net/tls/record-client.ts +30 -18
  88. package/std/net/tls/record-server.ts +74 -46
  89. package/std/net/tls/record.ts +33 -24
  90. package/std/net/tls/ticket.ts +2 -0
  91. package/std/net/tls/verify.ts +8 -7
  92. package/std/net/tls-tcp.ts +58 -50
  93. package/std/net/tls.ts +55 -43
  94. package/std/net/url.ts +12 -9
  95. package/std/net/websocket.ts +23 -19
  96. package/std/net/webtransport.ts +24 -18
  97. package/std/random.ts +8 -6
  98. package/std/regex.ts +70 -59
  99. package/std/sync/ring.ts +24 -21
  100. package/std/sync/shm.ts +9 -7
  101. package/std/term-io.ts +17 -14
  102. package/std/term-probe.ts +11 -10
  103. package/std/term-rate.ts +71 -61
  104. package/std/term.ts +73 -59
  105. package/std/text.ts +242 -15
  106. package/std/threads.ts +19 -15
  107. package/std/yaml-json.ts +17 -15
  108. package/std/yaml-write.ts +21 -14
@@ -23,14 +23,16 @@
23
23
  * cleartext until `install` gives it a suite and a traffic secret, then
24
24
  * TLSCiphertext (§5.2) under the AEAD the suite names. Each record's nonce is
25
25
  * the traffic IV XORed with the 64-bit record sequence number (§5.3), and its
26
- * additional data is the record's own header. Inside, the content is followed
27
- * by its real type and by zero padding (§5.4); `open` finds the type by
28
- * reading every byte of the decrypted record whatever the padding is, so how
29
- * much padding there was is not in its timing, and refuses a record that is
30
- * padding and nothing else. Both `seal` and `open` write into a buffer the
31
- * caller owns and answer a count, and what they allocate on the way dies with
32
- * the call (docs/LANGUAGE.md, "Memory model"), so a connection that has
33
- * finished its handshake moves records without moving the arena.
26
+ * additional data is the record's own header.
27
+ *
28
+ * Inside, the content is followed by its real type and by zero padding (§5.4);
29
+ * `open` finds the type by reading every byte of the decrypted record whatever
30
+ * the padding is, so how much padding there was is not in its timing, and
31
+ * refuses a record that is padding and nothing else. Both `seal` and `open`
32
+ * write into a buffer the caller owns and answer a count, and what they
33
+ * allocate on the way dies with the call (docs/LANGUAGE.md, "Memory model"), so
34
+ * a connection that has finished its handshake moves records without moving the
35
+ * arena.
34
36
  *
35
37
  * **Refusals are alerts.** Every function that can refuse answers the alert to
36
38
  * send, negated, as a negative count: `-TLS_ALERT_BAD_RECORD_MAC` for a record
@@ -41,17 +43,20 @@
41
43
  * sends can make it panic.
42
44
  *
43
45
  * **Secrets.** A traffic secret arrives as plain bytes, since that is how
44
- * `nish/net/tls` hands it over. The key, the IV and the AES key schedule
45
- * derived from it are held in the protection's fields, because a `Secret` may
46
- * not be one (NL2430), in arrays the protection reuses for every key it is
47
- * given, and are zeroed when it is installed again or cleared — the key and
48
- * IV with `secureZero`, the schedule with `aesKeyWipe`, the volatile `wipe`
49
- * of its round keys (the kernels' powers of H included) and of H. The key and
50
- * IV are derived straight into place, in an HKDF scratch that is wiped after
51
- * each derivation, and the schedule is expanded into place by `aesKeyInto`
52
- * (TLS-2 in `docs/security/tls.md`). A caller that holds the traffic secret
53
- * as a `Secret` installs it with `installHeld` and ratchets it in place with
54
- * `advanceHeld`, so the secret itself is never plain outside `exposeWith`.
46
+ * `nish/net/tls` hands it over.
47
+ *
48
+ * The key, the IV and the AES key schedule derived from it are held in the
49
+ * protection's fields, because a `Secret` may not be one (NL2430), in arrays
50
+ * the protection reuses for every key it is given, and are zeroed when it is
51
+ * installed again or cleared — the key and IV with `secureZero`, the schedule
52
+ * with `aesKeyWipe`, the volatile `wipe` of its round keys (the kernels' powers
53
+ * of H included) and of H. The key and IV are derived straight into place, in
54
+ * an HKDF scratch that is wiped after each derivation, and the schedule is
55
+ * expanded into place by `aesKeyInto` (TLS-2 in `docs/security/tls.md`).
56
+ *
57
+ * A caller that holds the traffic secret as a `Secret` installs it with
58
+ * `installHeld` and ratchets it in place with `advanceHeld`, so the secret
59
+ * itself is never plain outside `exposeWith`.
55
60
  *
56
61
  * The record layer is sans-IO: `nish/net/tls/record-server` runs it over a
57
62
  * stream for `TlsServer`, and `nish/net/tls-tcp` puts that on a socket.
@@ -390,8 +395,11 @@ export class TlsRecordProtection {
390
395
  contentType: i32 = 0
391
396
  /** The arrays `key` and `aes` point into: AES-128's and ChaCha20's or AES-256's. */
392
397
  key16: u8[]
398
+ /** The 32-byte key array `key` points into for ChaCha20-Poly1305 and AES-256-GCM; `clear` zeroes it. */
393
399
  key32: u8[]
400
+ /** The expanded AES-128 key schedule `aes` points to under AES-128-GCM, 10 rounds; `clear` wipes it. */
394
401
  aes128: AesKey
402
+ /** The expanded AES-256 key schedule `aes` points to under AES-256-GCM, 14 rounds; `clear` wipes it. */
395
403
  aes256: AesKey
396
404
  /** Where `advance` derives the next traffic secret: HashLen bytes of it. */
397
405
  next: u8[]
@@ -700,14 +708,15 @@ export class TlsRecordProtection {
700
708
  * `contentType`. Cleartext, the content is the body and the type the
701
709
  * header's: `application_data` is `unexpected_message`, since it is never
702
710
  * sent in the clear, and a body over 2^14 bytes is `record_overflow`.
711
+ *
703
712
  * Protected, the header's type must be `application_data`
704
713
  * (`unexpected_message` otherwise), the body must authenticate under this
705
714
  * record's nonce with the header as additional data (`bad_record_mac`), the
706
- * decrypted record may be no longer than 2^14 + 1 bytes
707
- * (`record_overflow`), and its last non-zero byte is the real type — one
708
- * that is all zeros is `unexpected_message` (§5.4). Every refusal is the
709
- * alert negated; a window outside `record`, a `len` that is not the
710
- * header's, or too little room at `out[at]` is `-TLS_ALERT_INTERNAL_ERROR`.
715
+ * decrypted record may be no longer than 2^14 + 1 bytes (`record_overflow`),
716
+ * and its last non-zero byte is the real type — one that is all zeros is
717
+ * `unexpected_message` (§5.4). Every refusal is the alert negated; a window
718
+ * outside `record`, a `len` that is not the header's, or too little room at
719
+ * `out[at]` is `-TLS_ALERT_INTERNAL_ERROR`.
711
720
  *
712
721
  * A protected record that fails to authenticate leaves the sequence number
713
722
  * where it was; one that authenticates spends its number even when its
@@ -195,7 +195,9 @@ export class TlsTicketState {
195
195
  suite: i32
196
196
  /** The ticket's lifetime in seconds, at most `TLS_TICKET_LIFETIME_SECONDS`. */
197
197
  lifetime: i32
198
+ /** How many bytes of `serverName` the name fills, 0 to `TLS_TICKET_SERVER_NAME_MAX`; 0 when the client sent no name. */
198
199
  serverNameLength: i32
200
+ /** How many bytes of `psk` the PSK fills: 32 or 48, the suite's hash length. */
199
201
  pskLength: i32
200
202
 
201
203
  /**
@@ -12,13 +12,14 @@
12
12
  * SHA-256 on a P-256 key, ECDSA P-384 with SHA-384 on a P-384 key, RSA-PSS
13
13
  * (rsae, salt as long as the hash) on an RSA key of at least 2048 bits — and
14
14
  * the signature must verify over `TlsClient.verifyContent()` under that key
15
- * (RFC 8446 §4.4.3). Then the chain, from the leaf the server sent to a root
16
- * in `trust`, for the name the caller dialled. A server with no certificate
17
- * or a leaf that does not parse is `bad_certificate`, as is a leaf whose key
18
- * is of any other kind (Ed25519, a short RSA modulus); a signature that does
19
- * not verify, or a scheme that does not fit the key, is `decrypt_error`.
20
- * The signature goes first so a forged CertificateVerify reads as what it is
21
- * whatever the chain would have said.
15
+ * (RFC 8446 §4.4.3). Then the chain, from the leaf the server sent to a root in
16
+ * `trust`, for the name the caller dialled.
17
+ *
18
+ * A server with no certificate or a leaf that does not parse is
19
+ * `bad_certificate`, as is a leaf whose key is of any other kind (Ed25519, a
20
+ * short RSA modulus); a signature that does not verify, or a scheme that does
21
+ * not fit the key, is `decrypt_error`. The signature goes first so a forged
22
+ * CertificateVerify reads as what it is whatever the chain would have said.
22
23
  *
23
24
  * **The chain is `nish/crypto/pki`'s.** `pkiVerifyServer` builds a path from
24
25
  * the leaf to a root of `trust`'s store and checks every certificate on it —
@@ -29,36 +29,40 @@
29
29
  * stops asking to be written. An event with the hang-up bit (4) goes to
30
30
  * `readable`, which is where a closed or failed socket is noticed.
31
31
  *
32
- * **Slots, built on first use; record buffers, borrowed.** The pool's size
33
- * is a cap: the constructor builds no slot, and the first `accept` into a
34
- * slot builds its handshake state and keys (TLS-3) and keeps them, so a pool
35
- * sized for its peak costs at start-up only what does not depend on the
36
- * size. `accept` hands the next connection the slot closed last, or failing
37
- * that the next never used, and reuses it once the program `close`s it
38
- * (WP34 N9). The record buffers — the bytes to send, the plaintext waiting
39
- * for the program, the record being opened and the record being read — are
40
- * borrowed from pools this server owns while a connection uses the slot, and
41
- * given back zeroed (`BufferPool.giveSecret`) when it ends, so a free slot
42
- * holds none of them ("What moves between connections" in
43
- * `docs/security/tls.md`). When every slot is busy a new connection is
44
- * accepted and closed at once, so the listener does not stay readable; the
45
- * program sees `TLS_TCP_POOL_FULL`. Each slot keeps its own `TlsServer`, which `accept` restarts for the connection, so neither the
46
- * handshake nor application data allocates anything that outlives a call:
47
- * a thousand connections through one slot leave `Arena.mark()` where the
48
- * first left it, under every suite (TLS-3 in `docs/security/tls.md`,
49
- * `tests/link/net_tls_memory`).
32
+ * **Slots, built on first use; record buffers, borrowed.** The pool's size is a
33
+ * cap: the constructor builds no slot, and the first `accept` into a slot
34
+ * builds its handshake state and keys (TLS-3) and keeps them, so a pool sized
35
+ * for its peak costs at start-up only what does not depend on the size.
36
+ * `accept` hands the next connection the slot closed last, or failing that the
37
+ * next never used, and reuses it once the program `close`s it (WP34 N9).
38
+ *
39
+ * The record buffers — the bytes to send, the plaintext waiting for the
40
+ * program, the record being opened and the record being read — are borrowed
41
+ * from pools this server owns while a connection uses the slot, and given back
42
+ * zeroed (`BufferPool.giveSecret`) when it ends, so a free slot holds none of
43
+ * them ("What moves between connections" in `docs/security/tls.md`). When every
44
+ * slot is busy a new connection is accepted and closed at once, so the listener
45
+ * does not stay readable; the program sees `TLS_TCP_POOL_FULL`.
46
+ *
47
+ * Each slot keeps its own `TlsServer`, which `accept` restarts for the
48
+ * connection, so neither the handshake nor application data allocates anything
49
+ * that outlives a call: a thousand connections through one slot leave
50
+ * `Arena.mark()` where the first left it, under every suite (TLS-3 in
51
+ * `docs/security/tls.md`, `tests/link/net_tls_memory`).
50
52
  *
51
53
  * **Randomness is the caller's.** `accept` takes the 32-byte server random and
52
54
  * the 32-byte x25519 private key the handshake uses, as `TlsServer` does, so a
53
55
  * test replays RFC 8448's trace and a server draws them with
54
56
  * `crypto.getRandomValues`. `accept` copies both into the slot and wipes the
55
57
  * caller's key array, so one buffer of each refilled for every `accept` is
56
- * safe, and refuses either when it is not 32 bytes before taking a
57
- * connection; the slot's copy of the key is wiped once the ServerHello is
58
- * written, when the handshake fails, or when the slot is closed. **Signing is the caller's**, as `TlsServer`'s is: `signP256` signs
59
- * the CertificateVerify with a P-256 key the caller holds as a `Secret` and
60
- * borrows for the call, and `signatureInput` / `sign` hand the input out and
61
- * take any signature back.
58
+ * safe, and refuses either when it is not 32 bytes before taking a connection;
59
+ * the slot's copy of the key is wiped once the ServerHello is written, when the
60
+ * handshake fails, or when the slot is closed.
61
+ *
62
+ * **Signing is the caller's**, as `TlsServer`'s is: `signP256` signs the
63
+ * CertificateVerify with a P-256 key the caller holds as a `Secret` and borrows
64
+ * for the call, and `signatureInput` / `sign` hand the input out and take any
65
+ * signature back.
62
66
  *
63
67
  * **Resumption is opt-in, and its keys are the caller's.** After
64
68
  * `enableTickets`, a slot whose interest says `TLS_RECORD_TICKET` waits on
@@ -75,23 +79,25 @@
75
79
  * `close` frees the slot at once; `shutdown` sends the `close_notify` as the
76
80
  * socket takes it, and the slot is done when its interest says so.
77
81
  *
78
- * **A closed connection lingers until the client hangs up.** Once a
79
- * connection the server closed has sent everything, the slot shuts the
80
- * socket's write side — the client reads the response, the `close_notify`
81
- * and then the end of the stream — and reads and throws away what the
82
- * client still sends, until its own end of stream, before its interest says
83
- * `TLS_RECORD_DONE` (RFC 9112 §9.6's staged close). A socket closed with
84
- * the client's bytes unread, or that the client writes to after the close,
85
- * answers with a TCP reset, and a client may report that reset instead of
86
- * the response it was sent: Chromium's `ERR_CONNECTION_RESET` after an HTTP/2
87
- * `GOAWAY`, when its SETTINGS acknowledgement crossed the server's close. A
88
- * lingering slot holds no key and no record buffer, asks only to be read,
89
- * and gives up past `TLS_TCP_LINGER_LIMIT` bytes. A connection that failed,
90
- * whose client sent its own `close_notify` (it writes nothing after it,
91
- * RFC 8446 §6.1) or whose client's stream had already ended, is done at
92
- * once. A client that never hangs up keeps its slot, as one that never sends
93
- * does: how long a slot may wait is the program's (`docs/security/tls.md`,
94
- * TLS-6 and TLS-4).
82
+ * **A closed connection lingers until the client hangs up.** Once a connection
83
+ * the server closed has sent everything, the slot shuts the socket's write side
84
+ * — the client reads the response, the `close_notify` and then the end of the
85
+ * stream — and reads and throws away what the client still sends, until its own
86
+ * end of stream, before its interest says `TLS_RECORD_DONE` (RFC 9112 §9.6's
87
+ * staged close).
88
+ *
89
+ * A socket closed with the client's bytes unread, or that the client writes to
90
+ * after the close, answers with a TCP reset, and a client may report that reset
91
+ * instead of the response it was sent: Chromium's `ERR_CONNECTION_RESET` after
92
+ * an HTTP/2 `GOAWAY`, when its SETTINGS acknowledgement crossed the server's
93
+ * close. A lingering slot holds no key and no record buffer, asks only to be
94
+ * read, and gives up past `TLS_TCP_LINGER_LIMIT` bytes.
95
+ *
96
+ * A connection that failed, whose client sent its own `close_notify` (it writes
97
+ * nothing after it, RFC 8446 §6.1) or whose client's stream had already ended,
98
+ * is done at once. A client that never hangs up keeps its slot, as one that
99
+ * never sends does: how long a slot may wait is the program's
100
+ * (`docs/security/tls.md`, TLS-6 and TLS-4).
95
101
  *
96
102
  * Written from RFC 8446 and docs/LANGUAGE.md's `nish:net`, not ported from
97
103
  * another implementation.
@@ -863,6 +869,7 @@ export class TlsTcpServer {
863
869
  * connect leaves the client done, its failure in `failure()`.
864
870
  */
865
871
  export class TlsTcpClient {
872
+ /** The TLS record layer over the socket: its `tls` is the handshake, read for the negotiated suite and `alpnChosen`. */
866
873
  conn: TlsRecordClient
867
874
  private trust: TlsClientTrust
868
875
  /** The socket, -1 once `close` has run or when `tcpConnect` refused at once. */
@@ -1316,17 +1323,18 @@ const tlsTcpDeadline = (timeoutMs: i32): i64 => monotonicNanos() + toI64(timeout
1316
1323
 
1317
1324
  /**
1318
1325
  * Opens a TLS 1.3 connection to the server at `addr` (`nish:net`'s 18-byte
1319
- * form) for `serverName`, offering `alpn`, authenticated against the
1320
- * system's roots, and waits in a loop of its own until the handshake is done
1321
- * and the client's last flight sent, or it fails, or `timeoutMs` milliseconds
1322
- * pass, reading the roots included. Answers the client either way:
1323
- * `open()` says whether it opened; otherwise `failure()` says why — the TCP
1324
- * failure, the alert, or `TLS_TCP_TIMED_OUT`, the client closed. Nothing is
1325
- * looked up: to reach a name, resolve it first with `nish/net/dns`'s
1326
+ * form) for `serverName`, offering `alpn`, authenticated against the system's
1327
+ * roots, and waits in a loop of its own until the handshake is done and the
1328
+ * client's last flight sent, or it fails, or `timeoutMs` milliseconds pass,
1329
+ * reading the roots included. Answers the client either way: `open()` says
1330
+ * whether it opened; otherwise `failure()` says why — the TCP failure, the
1331
+ * alert, or `TLS_TCP_TIMED_OUT`, the client closed.
1332
+ *
1333
+ * Nothing is looked up: to reach a name, resolve it first with `nish/net/dns`'s
1326
1334
  * `resolveHost`; the name the certificate must carry is always `serverName`,
1327
1335
  * never one taken from `addr`. The open client's socket is in no loop: the
1328
- * program adds it to its own, or uses `writeAll` and `readWait`. The roots
1329
- * are read on every call; a program that connects often reads them once with
1336
+ * program adds it to its own, or uses `writeAll` and `readWait`. The roots are
1337
+ * read on every call; a program that connects often reads them once with
1330
1338
  * `tlsClientSystemTrust` and passes them to `tlsConnectWith`.
1331
1339
  *
1332
1340
  * @param addr The server's address and port in `nish:net`'s 18-byte form.
package/std/net/tls.ts CHANGED
@@ -42,36 +42,38 @@
42
42
  * says the carrier is QUIC. 0-RTT and client authentication are not here:
43
43
  * an early-data offer is ignored.
44
44
  *
45
- * **Resumption.** A server the carrier calls `enableTickets` on, over TCP
46
- * only, resumes with a session ticket in `psk_dhe_ke` mode (RFC 8446 §2.2,
47
- * §4.2.11) and issues one after each handshake. The ticket keys and the
48
- * strike register are the program's, lent to each call and never kept: a
49
- * ClientHello that offers a ticket stops at `TLS_STATE_WAIT_TICKET`, and
50
- * `resume` reads the identities in the client's order, verifies the binder of
51
- * the one it selects, records its id, and answers a resumed flight — or a full
52
- * handshake when no identity can be used (`docs/security/tls.md`,
53
- * "Resumption"). Once connected, `issue` writes one NewSessionTicket at the
54
- * Application level. Sealing draws the ticket's nonce and `issue` the
55
- * `ticket_age_add` from the random device; nothing else here does. Over QUIC
56
- * the hand-off is never called, so a PSK offer gets a full handshake.
45
+ * **Resumption.** A server the carrier calls `enableTickets` on, over TCP only,
46
+ * resumes with a session ticket in `psk_dhe_ke` mode (RFC 8446 §2.2, §4.2.11)
47
+ * and issues one after each handshake. The ticket keys and the strike register
48
+ * are the program's, lent to each call and never kept: a ClientHello that
49
+ * offers a ticket stops at `TLS_STATE_WAIT_TICKET`, and `resume` reads the
50
+ * identities in the client's order, verifies the binder of the one it selects,
51
+ * records its id, and answers a resumed flight — or a full handshake when no
52
+ * identity can be used (`docs/security/tls.md`, "Resumption").
53
+ *
54
+ * Once connected, `issue` writes one NewSessionTicket at the Application level.
55
+ * Sealing draws the ticket's nonce and `issue` the `ticket_age_add` from the
56
+ * random device; nothing else here does. Over QUIC the hand-off is never
57
+ * called, so a PSK offer gets a full handshake.
57
58
  *
58
59
  * **Refusals are alerts.** `receive` and `sign` answer 0 or a TLS alert
59
60
  * description (`TLS_ALERT_*` in `nish/net/tls/codec`); after one the server
60
61
  * is in `TLS_STATE_FAILED` and answers the same alert to every call. Nothing
61
62
  * the peer sends can make it panic.
62
63
  *
63
- * **Secrets.** Every secret the server holds is a `Secret` (`nish:secret`):
64
- * the ECDHE secret and the words it is computed in, wiped on every path; the
64
+ * **Secrets.** Every secret the server holds is a `Secret` (`nish:secret`): the
65
+ * ECDHE secret and the words it is computed in, wiped on every path; the
65
66
  * ephemeral key, copied into the `ephemeral` field by `restart` with the
66
67
  * caller's array zeroed; and the handshake, traffic, exporter and resumption
67
68
  * secrets, and a resumed handshake's PSK, early secret and binder key, fields
68
69
  * of the two `TlsSecretPool`s, read only inside `expose`.
69
- * `wipe` zeroes them all, and `restart` calls it before the next connection;
70
- * the carriers call it when a connection ends, and `wipeSecret` and
71
- * `wipeEphemeral` as each secret's work is done. `fail` wipes the resumption
72
- * master secret and the PSK's secrets, and `issue` the resumption master
73
- * secret once the ticket is sealed (CLAUDE.md §Security; TLS-1 in
74
- * `docs/security/tls.md`).
70
+ *
71
+ * `wipe` zeroes them all, and `restart` calls it before the next connection;
72
+ * the carriers call it when a connection ends, and `wipeSecret` and
73
+ * `wipeEphemeral` as each secret's work is done. `fail` wipes the resumption
74
+ * master secret and the PSK's secrets, and `issue` the resumption master secret
75
+ * once the ticket is sealed (CLAUDE.md §Security; TLS-1 in
76
+ * `docs/security/tls.md`).
75
77
  *
76
78
  * **Memory.** A `TlsServer` keeps its state in buffers it allocates once —
77
79
  * the messages in and out, the transcript, the secrets, the HKDF scratch —
@@ -727,10 +729,15 @@ const TLS_CERTIFICATE_VERIFY_PREFIX: i32 = 98
727
729
  * `wipe` zeroes them too (TLS-1).
728
730
  */
729
731
  export class TlsSecretPool {
732
+ /** `handshake_secret` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
730
733
  readonly handshake: Secret<u8[]>
734
+ /** `client_handshake_traffic_secret` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
731
735
  readonly clientHandshake: Secret<u8[]>
736
+ /** `server_handshake_traffic_secret` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
732
737
  readonly serverHandshake: Secret<u8[]>
738
+ /** `client_application_traffic_secret_0` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
733
739
  readonly clientApplication: Secret<u8[]>
740
+ /** `server_application_traffic_secret_0` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
734
741
  readonly serverApplication: Secret<u8[]>
735
742
  /** `exporter_master_secret` (RFC 8446 §7.5), for a caller's own exporters. */
736
743
  readonly exporter: Secret<u8[]>
@@ -933,20 +940,22 @@ const tlsServerTicketCapacity = (state: TlsTicketState): i32 => {
933
940
  *
934
941
  * **What it allocates.** Everything it keeps is allocated by the constructor
935
942
  * and reused by `restart`: the input and output buffers, the transcript's
936
- * hashers, the HKDF scratch, the ephemeral key's 32 bytes, and HashLen bytes for every secret
937
- * under each hash, which each derivation writes over in place and `wipe`
938
- * zeroes before the next connection starts. `receive` and
939
- * `sign` do their work inside `using a = arena()` blocks, so the compiler has
940
- * proved that what they allocate on the way dies before they return. Four
941
- * things grow or are made outside those blocks: the input buffer, doubled up
942
- * to the largest message when a ClientHello does not fit (once per slot);
943
- * the Handshake-level output, when a signature is longer than an ECDSA one;
944
- * the `serverName` string, made when the client's host name differs from the
945
- * last one this server held; and, over QUIC only, the array the client's
946
- * transport parameters are copied into, which grows only when a client's
947
- * are longer than any before (TLS-3).
943
+ * hashers, the HKDF scratch, the ephemeral key's 32 bytes, and HashLen bytes
944
+ * for every secret under each hash, which each derivation writes over in place
945
+ * and `wipe` zeroes before the next connection starts. `receive` and `sign` do
946
+ * their work inside `using a = arena()` blocks, so the compiler has proved that
947
+ * what they allocate on the way dies before they return.
948
+ *
949
+ * Four things grow or are made outside those blocks: the input buffer, doubled
950
+ * up to the largest message when a ClientHello does not fit (once per slot);
951
+ * the Handshake-level output, when a signature is longer than an ECDSA one; the
952
+ * `serverName` string, made when the client's host name differs from the last
953
+ * one this server held; and, over QUIC only, the array the client's transport
954
+ * parameters are copied into, which grows only when a client's are longer than
955
+ * any before (TLS-3).
948
956
  */
949
957
  export class TlsServer {
958
+ /** The configuration this server was built with: its certificate chain, ALPN list and QUIC transport parameters. */
950
959
  config: TlsServerConfig
951
960
  private serverRandom: u8[]
952
961
  /** The connection's x25519 private key, copied in by `restart` and wiped by the record layer once the ServerHello is written. */
@@ -980,6 +989,7 @@ export class TlsServer {
980
989
  private none: u8[]
981
990
  /** The secrets under each hash (`TlsSecretPool`); `secrets()` answers the negotiated hash's. */
982
991
  secrets256: TlsSecretPool
992
+ /** The secrets under SHA-384, for TLS_AES_256_GCM_SHA384. */
983
993
  secrets384: TlsSecretPool
984
994
  /** The ClientHello last read, as windows into `input`. */
985
995
  private hello: TlsClientHelloView
@@ -1022,6 +1032,7 @@ export class TlsServer {
1022
1032
  * bit, since its place is still the one it was derived into.
1023
1033
  */
1024
1034
  held: i32 = 0
1035
+ /** Where the handshake stands: one of the `TLS_STATE_*` constants, `TLS_STATE_CONNECTED` once it is done and `TLS_STATE_FAILED` after an alert. */
1025
1036
  state: i32 = 0
1026
1037
  /** The alert the server failed with, or 0. */
1027
1038
  alert: i32 = 0
@@ -2180,17 +2191,18 @@ export class TlsServer {
2180
2191
  }
2181
2192
 
2182
2193
  /**
2183
- * Once the handshake is done, at most once a connection: seals a ticket
2184
- * for it under `key`, which `info` describes, and writes the
2185
- * NewSessionTicket carrying it at the Application level, for the carrier
2186
- * to send under the server's application keys. Answers whether one was
2187
- * written: none is when tickets are off, the client did not list
2188
- * `psk_dhe_ke`, or the key does not seal. On a connected connection's
2189
- * first call the resumption master secret is wiped before it returns,
2190
- * written or not, so a carrier that issues no ticket still calls this, or
2191
- * `wipeResumption`. Before the handshake is done, or on a second call, it
2192
- * answers false and touches nothing: a secret wiped then would leave no
2193
- * ticket to issue, and one wiped already has nothing left to wipe.
2194
+ * Once the handshake is done, at most once a connection: seals a ticket for
2195
+ * it under `key`, which `info` describes, and writes the NewSessionTicket
2196
+ * carrying it at the Application level, for the carrier to send under the
2197
+ * server's application keys. Answers whether one was written: none is when
2198
+ * tickets are off, the client did not list `psk_dhe_ke`, or the key does not
2199
+ * seal. On a connected connection's first call the resumption master secret
2200
+ * is wiped before it returns, written or not, so a carrier that issues no
2201
+ * ticket still calls this, or `wipeResumption`.
2202
+ *
2203
+ * Before the handshake is done, or on a second call, it answers false and
2204
+ * touches nothing: a secret wiped then would leave no ticket to issue, and
2205
+ * one wiped already has nothing left to wipe.
2194
2206
  *
2195
2207
  * @param key The current ticket key, lent: read only inside `exposeWith`, and still the carrier's to wipe.
2196
2208
  * @param info The key's record, which counts the ticket when one is sealed.
package/std/net/url.ts CHANGED
@@ -1057,15 +1057,18 @@ const urlFinish = (u: Url): Url | null => {
1057
1057
 
1058
1058
  /**
1059
1059
  * The absolute URL `s` split into its parts, or null when it is not one this
1060
- * module accepts. `s` is refused when it has no scheme, holds a byte outside
1061
- * printable ASCII (a control byte, a space, DEL or anything past 0x7F), a
1062
- * backslash or a `%` not followed by two hex digits, or its authority has more
1063
- * than one `@`, a host with a byte outside RFC 3986's unreserved set (so a
1064
- * percent-encoded one), a host whose last label is numeric but that is not a
1065
- * dotted-decimal IPv4 address, a malformed IPv6 literal or a port that is not
1066
- * digits or is above 65535. An http, https, ws or wss URL needs a non-empty
1067
- * host. The path's dot segments are removed, as RFC 3986 §5.2.2 removes them
1068
- * from a reference with a scheme. Nothing is decoded. It allocates its answer.
1060
+ * module accepts.
1061
+ *
1062
+ * `s` is refused when it has no scheme, holds a byte outside printable ASCII (a
1063
+ * control byte, a space, DEL or anything past 0x7F), a backslash or a `%` not
1064
+ * followed by two hex digits, or its authority has more than one `@`, a host
1065
+ * with a byte outside RFC 3986's unreserved set (so a percent-encoded one), a
1066
+ * host whose last label is numeric but that is not a dotted-decimal IPv4
1067
+ * address, a malformed IPv6 literal or a port that is not digits or is
1068
+ * above 65535. An http, https, ws or wss URL needs a non-empty host.
1069
+ *
1070
+ * The path's dot segments are removed, as RFC 3986 §5.2.2 removes them from a
1071
+ * reference with a scheme. Nothing is decoded. It allocates its answer.
1069
1072
  *
1070
1073
  * @param s The absolute URL, such as `https://example.com/a?b#c`.
1071
1074
  * @returns The URL's parts, still percent-encoded, `scheme` and `host` lower-cased; or null when `s` is refused.
@@ -657,13 +657,15 @@ export const websocketFrameSize = (len: i32, masked: boolean): i32 => {
657
657
  * `payload[off .. off + len)` and, when `mask` is not null, masked with that
658
658
  * 4-byte key. A client masks every frame with a fresh key from
659
659
  * `crypto.getRandomValues` (§5.3); a server passes null. The length takes the
660
- * shortest of its three forms. Answers where the frame ends, `WS_NO_ROOM` when
661
- * it does not fit, or `WS_REFUSED` for an opcode RFC 6455 does not define, a
662
- * control frame that is not FIN or is longer than 125 bytes, and a mask that
663
- * is not 4 bytes; on either refusal `out` is untouched. A window outside
664
- * `payload` panics, and so does a payload longer than 2^31 - 15 bytes. A text
665
- * frame's payload is not checked here, because a fragment may end inside a
666
- * character; `websocketIsUtf8` checks a whole message.
660
+ * shortest of its three forms.
661
+ *
662
+ * Answers where the frame ends, `WS_NO_ROOM` when it does not fit, or
663
+ * `WS_REFUSED` for an opcode RFC 6455 does not define, a control frame that is
664
+ * not FIN or is longer than 125 bytes, and a mask that is not 4 bytes; on
665
+ * either refusal `out` is untouched. A window outside `payload` panics, and so
666
+ * does a payload longer than 2^31 - 15 bytes. A text frame's payload is not
667
+ * checked here, because a fragment may end inside a character;
668
+ * `websocketIsUtf8` checks a whole message.
667
669
  *
668
670
  * @param out The buffer the frame is written into.
669
671
  * @param at The index in `out` the frame starts at; one outside `out` answers `WS_NO_ROOM`.
@@ -1127,18 +1129,20 @@ export class WsHandshake {
1127
1129
  /**
1128
1130
  * Reads the server's answer to `websocketUpgradeRequest(…, key, offered)`
1129
1131
  * (§4.1, the client's checks of the response): call it at the parser's
1130
- * `HTTP1_HEAD`. It fails closed, and the connection is closed on any failure:
1131
- * a status other than 101 (`WS_HS_STATUS`; a redirect is not followed), a
1132
- * version other than HTTP/1.1, `Upgrade` absent, repeated or not `websocket`,
1133
- * no `Connection` listing `upgrade`, `Sec-WebSocket-Accept` absent, repeated
1134
- * or not `websocketAcceptKey(key)` (a forged or replayed answer, or any answer
1135
- * when `key` is not a valid key), any
1136
- * `Sec-WebSocket-Extensions` (this client offers none, so none may be in use),
1137
- * and a `Sec-WebSocket-Protocol` that is present when nothing was offered,
1138
- * repeated, a list, or not one of `offered`. On `WS_HS_OK` the parser's next
1139
- * events are `HTTP1_END` and `HTTP1_UPGRADE`; its `tunnelBytes()` go to
1140
- * `WsDecoder(false, …)`, and every frame the client sends is masked with a
1141
- * fresh mask key from `crypto.getRandomValues` (§5.3).
1132
+ * `HTTP1_HEAD`.
1133
+ *
1134
+ * It fails closed, and the connection is closed on any failure: a status other
1135
+ * than 101 (`WS_HS_STATUS`; a redirect is not followed), a version other than
1136
+ * HTTP/1.1, `Upgrade` absent, repeated or not `websocket`, no `Connection`
1137
+ * listing `upgrade`, `Sec-WebSocket-Accept` absent, repeated or not
1138
+ * `websocketAcceptKey(key)` (a forged or replayed answer, or any answer when
1139
+ * `key` is not a valid key), any `Sec-WebSocket-Extensions` (this client offers
1140
+ * none, so none may be in use), and a `Sec-WebSocket-Protocol` that is present
1141
+ * when nothing was offered, repeated, a list, or not one of `offered`.
1142
+ *
1143
+ * On `WS_HS_OK` the parser's next events are `HTTP1_END` and `HTTP1_UPGRADE`;
1144
+ * its `tunnelBytes()` go to `WsDecoder(false, …)`, and every frame the client
1145
+ * sends is masked with a fresh mask key from `crypto.getRandomValues` (§5.3).
1142
1146
  *
1143
1147
  * @param response The client's parser, at the `HTTP1_HEAD` of the response.
1144
1148
  * @param key The key the request sent, from `websocketClientKey`.
@@ -27,24 +27,30 @@
27
27
  * }
28
28
  * };
29
29
  *
30
- * **Events.** `next()` answers a `WT_*` event, or one of `nish/net/http3`'s
31
- * for a plain request, which passes through untouched so one connection can
32
- * serve both: `WT_SESSION` (a session asked for: `sessionId`, the CONNECT
33
- * stream's ID, and `fields`; the program `accept`s or `refuse`s it),
34
- * `WT_DATAGRAM` (a datagram of an accepted session, a window onto `data`),
35
- * `WT_STREAM` (the client opened a stream of an accepted session: `stream`,
36
- * `bidirectional`), `WT_STREAM_DATA` (its bytes, a window onto `data`),
37
- * `WT_STREAM_END` (its FIN), `WT_STREAM_RESET` (the client reset its side:
38
- * `errorCode`, the HTTP/3 code, and `appCode`, the WebTransport code it maps
39
- * to or -1), `WT_STREAM_STOPPED` (the client asked this side to stop writing:
40
- * the same two codes), `WT_WRITABLE` (a stream, or a CONNECT stream whose
41
- * capsule was held back, can take more), `WT_DRAIN` (the client sent
42
- * DRAIN_WEBTRANSPORT_SESSION) and `WT_CLOSED` (the session is over:
43
- * `errorCode`, the CLOSE capsule's code or 0, the reason a window onto
44
- * `data`, and `closedByPeer`). Each event names its session in `sessionId`
45
- * and in `session`, its index in the table, `0 .. webtransportSessions - 1`,
46
- * for a program's own per-session arrays. A window is valid until the next
47
- * call.
30
+ * **Events.**
31
+ *
32
+ * `next()` answers a `WT_*` event, or one of `nish/net/http3`'s for a plain
33
+ * request, which passes through untouched so one connection can serve both:
34
+ * - `WT_SESSION` (a session asked for: `sessionId`, the CONNECT stream's ID,
35
+ * and `fields`; the program `accept`s or `refuse`s it),
36
+ * - `WT_DATAGRAM` (a datagram of an accepted session, a window onto `data`),
37
+ * - `WT_STREAM` (the client opened a stream of an accepted session: `stream`,
38
+ * `bidirectional`),
39
+ * - `WT_STREAM_DATA` (its bytes, a window onto `data`),
40
+ * - `WT_STREAM_END` (its FIN),
41
+ * - `WT_STREAM_RESET` (the client reset its side: `errorCode`, the HTTP/3 code,
42
+ * and `appCode`, the WebTransport code it maps to or -1),
43
+ * - `WT_STREAM_STOPPED` (the client asked this side to stop writing: the same
44
+ * two codes),
45
+ * - `WT_WRITABLE` (a stream, or a CONNECT stream whose capsule was held back,
46
+ * can take more),
47
+ * - `WT_DRAIN` (the client sent DRAIN_WEBTRANSPORT_SESSION) and `WT_CLOSED`
48
+ * (the session is over: `errorCode`, the CLOSE capsule's code or 0, the
49
+ * reason a window onto `data`, and `closedByPeer`).
50
+ *
51
+ * Each event names its session in `sessionId` and in `session`, its index in
52
+ * the table, `0 .. webtransportSessions - 1`, for a program's own per-session
53
+ * arrays. A window is valid until the next call.
48
54
  *
49
55
  * **Writes.** `sendDatagram` (the quarter stream ID, then the payload, one
50
56
  * QUIC DATAGRAM frame; `maxDatagramPayload` says how much fits), `openStream`,
package/std/random.ts CHANGED
@@ -27,12 +27,14 @@
27
27
  * two's-complement wrap natively and answer `(a + b) | 0` and `Math.imul(a, b)`
28
28
  * under Node (docs/LANGUAGE.md, "nish:unsafe"); every shift has a constant
29
29
  * count, and `^` and `>>> 16` answer the same 32 bits in JavaScript as in the
30
- * compiled program. `nextInt` reads its span and its draws as unsigned
31
- * counts with no `u32` (whose conversions saturate under Node): the sign bit
32
- * flipped orders them under a signed compare, and the remainder is taken in
33
- * pieces that stay below 2^31. `nextF64` builds its double from integers
34
- * below 2^53, so its products and sums are exact both ways. The program a test prints under
35
- * Node is the one it prints compiled (docs/RUN_UNDER_NODE.md).
30
+ * compiled program.
31
+ *
32
+ * `nextInt` reads its span and its draws as unsigned counts with no `u32`
33
+ * (whose conversions saturate under Node): the sign bit flipped orders them
34
+ * under a signed compare, and the remainder is taken in pieces that stay below
35
+ * 2^31. `nextF64` builds its double from integers below 2^53, so its products
36
+ * and sums are exact both ways. The program a test prints under Node is the one
37
+ * it prints compiled (docs/RUN_UNDER_NODE.md).
36
38
  *
37
39
  * **It is not for cryptography**: a key or a nonce comes from
38
40
  * `crypto.getRandomValues`.