nish 0.35.0 → 0.37.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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -3,7 +3,32 @@
3
3
  * (RFC 8446 §4.6.1), and the strike register that makes each one single-use
4
4
  * (§8.1). `docs/security/tls.md`, "Resumption", is the design this follows.
5
5
  *
6
- * A ticket is the key id (16 bytes, public, the additional data) ‖ a fresh
6
+ * import {
7
+ * TLS_TICKET_STRIKES_DEFAULT, TlsTicketKeyInfo, TlsTicketState, TlsTicketStrikes,
8
+ * tlsTicketKeyDraw, tlsTicketKeyDue, tlsTicketOpen, tlsTicketSeal, tlsTicketSize,
9
+ * } from "nish/net/tls/ticket";
10
+ * import { monotonicNanos } from "nish:process";
11
+ * import { wipe } from "nish:secret";
12
+ * // secureZero is a builtin and needs no import
13
+ *
14
+ * const info = new TlsTicketKeyInfo()
15
+ * const key = tlsTicketKeyDraw(info, monotonicNanos())
16
+ * const strikes = new TlsTicketStrikes(TLS_TICKET_STRIKES_DEFAULT)
17
+ *
18
+ * // Issuing: `state` is a TlsTicketState the server filled from a finished handshake.
19
+ * const ticket: u8[] = new Array<u8>(tlsTicketSize(state))
20
+ * const written = tlsTicketSeal(key, info, state, monotonicNanos(), ticket) // 0: send no ticket
21
+ *
22
+ * // Resuming (`TlsServer.resume` does this for a server): `offered` is a ticket the client sent.
23
+ * const now = monotonicNanos()
24
+ * const opened = new TlsTicketState()
25
+ * const fresh = tlsTicketOpen(key, info, offered, now, opened)
26
+ * if (fresh && strikes.take(opened.id, opened.expiresAt(), now)) {
27
+ * // resume with opened.psk[0 .. opened.pskLength), then secureZero(opened.psk)
28
+ * }
29
+ * // when tlsTicketKeyDue(info, now), rotate as "Keys" below says; wipe(key) before it goes
30
+ *
31
+ * **The ticket.** A ticket is the key id (16 bytes, public, the additional data) ‖ a fresh
7
32
  * random nonce (12) ‖ the ChaCha20-Poly1305 ciphertext of the state ‖ its tag
8
33
  * (16). The state is what a resumed handshake reads: the suite, the time the
9
34
  * ticket was issued, its lifetime, the time of the full handshake its chain
@@ -12,36 +37,39 @@
12
37
  * start (8), the server name with a one-byte length, and the PSK with a
13
38
  * one-byte length, every integer big-endian.
14
39
  *
15
- * Every time is `monotonicNanos()`, read by the caller outside `expose` and
40
+ * **Time.** Every time is `monotonicNanos()`, read by the caller outside `expose` and
16
41
  * handed in as `now`: keys never leave the process, so neither does a ticket,
17
42
  * and a clock that cannot step keeps the strike register honest.
18
43
  *
19
- * - **Keys.** A ticket key is a `Secret<u8[]>` of 32 random bytes, which no
20
- * class may hold (NL2430): the program keeps its current and previous keys
21
- * in locals and lends them to `tlsTicketSeal` and `tlsTicketOpen`, which
22
- * read them only inside `exposeWith`. Beside each key it keeps a
23
- * `TlsTicketKeyInfo` — the id, the time it was drawn, how many tickets it has
24
- * sealed, and its seal cap. `tlsTicketKeyDraw` draws a key into a record and
25
- * `tlsTicketKeyDue` says when to replace it. The rotation turn is the
26
- * program's, because a function hands back one `Secret` and may not reassign
27
- * a borrowed one: `wipe(previous)`, `previous = current`,
28
- * `previousInfo.copyFrom(currentInfo)`, `current = tlsTicketKeyDraw(currentInfo, now)`.
29
- * - **What the module enforces from the record.** A key seals for 12 hours of
30
- * awake time after its draw and until its seal cap (2^30 tickets), and opens
31
- * for 14 hours, inclusive; a ticket opens while `now` is no earlier than its
32
- * issue time, no later than issue time plus lifetime, and no later than 24
33
- * hours after its chain started, and only when its sealed lifetime is at
34
- * most 7200 s and its chain start is no later than its issue time.
35
- * - **Wiping.** The plaintext of a seal or an open is a plain `u8[]` made in
36
- * a `using a = arena()` block, wiped with `secureZero` once, at the point
37
- * every path reaches; a failed open also wipes the state's PSK array. The
38
- * seal's exposed function wipes the copy of the plaintext it hands the
39
- * AEAD, and what the AEAD makes from the key is wiped by the AEAD
40
- * (`nish/crypto/chacha20poly1305`).
41
- * - **`TlsTicketStrikes`** records each ticket's id (its nonce) until the
42
- * ticket expires or 7200 s after the recording, whichever is sooner, in a
43
- * table made once at its capacity. It never grows and fails closed when
44
- * full.
44
+ * **Keys.** A ticket key is a `Secret<u8[]>` of 32 random bytes, which no
45
+ * class may hold (NL2430): the program keeps its current and previous keys
46
+ * in locals and lends them to `tlsTicketSeal` and `tlsTicketOpen`, which
47
+ * read them only inside `exposeWith`. Beside each key it keeps a
48
+ * `TlsTicketKeyInfo` — the id, the time it was drawn, how many tickets it has
49
+ * sealed, and its seal cap. `tlsTicketKeyDraw` draws a key into a record and
50
+ * `tlsTicketKeyDue` says when to replace it. The rotation turn is the
51
+ * program's, because a function hands back one `Secret` and may not reassign
52
+ * a borrowed one: `wipe(previous)`, `previous = current`,
53
+ * `previousInfo.copyFrom(currentInfo)`, `current = tlsTicketKeyDraw(currentInfo, now)`.
54
+ *
55
+ * **What the module enforces from the record.** A key seals for 12 hours of
56
+ * awake time after its draw and until its seal cap (2^30 tickets), and opens
57
+ * for 14 hours, inclusive; a ticket opens while `now` is no earlier than its
58
+ * issue time, no later than issue time plus lifetime, and no later than 24
59
+ * hours after its chain started, and only when its sealed lifetime is at
60
+ * most 7200 s and its chain start is no later than its issue time.
61
+ *
62
+ * **Wiping.** The plaintext of a seal or an open is a plain `u8[]` made in
63
+ * a `using a = arena()` block, wiped with `secureZero` once, at the point
64
+ * every path reaches; a failed open also wipes the state's PSK array. The
65
+ * seal's exposed function wipes the copy of the plaintext it hands the
66
+ * AEAD, and what the AEAD makes from the key is wiped by the AEAD
67
+ * (`nish/crypto/chacha20poly1305`).
68
+ *
69
+ * **`TlsTicketStrikes`** records each ticket's id (its nonce) until the
70
+ * ticket expires or 7200 s after the recording, whichever is sooner, in a
71
+ * table made once at its capacity. It never grows and fails closed when
72
+ * full.
45
73
  *
46
74
  * Written from the RFC and the design record, not ported from another
47
75
  * implementation. Private names carry the `tlsTicket` prefix because a `std/`
@@ -119,6 +147,10 @@ export class TlsTicketKeyInfo {
119
147
  /** How many it may seal: `TLS_TICKET_SEAL_CAP` unless the program lowers it. */
120
148
  sealCap: i64
121
149
 
150
+ /**
151
+ * Makes a blank record, nothing sealed yet and the cap at
152
+ * `TLS_TICKET_SEAL_CAP`, for `tlsTicketKeyDraw` to fill.
153
+ */
122
154
  constructor() {
123
155
  this.id = new Array<u8>(TLS_TICKET_KEY_ID_SIZE)
124
156
  this.createdAt = 0
@@ -126,7 +158,12 @@ export class TlsTicketKeyInfo {
126
158
  this.sealCap = TLS_TICKET_SEAL_CAP
127
159
  }
128
160
 
129
- /** Copies `other` into this record, as the rotation turn does from the current key's to the previous one's. */
161
+ /**
162
+ * Copies `other` into this record, as the rotation turn does from the current key's to the previous one's.
163
+ * The id is copied byte by byte into this record's own array, so the two records share nothing.
164
+ *
165
+ * @param other The record to copy from, left unchanged.
166
+ */
130
167
  copyFrom(other: TlsTicketKeyInfo): void {
131
168
  for (let k: i32 = 0; k < TLS_TICKET_KEY_ID_SIZE; k++) {
132
169
  this.id[k] = other.id[k]
@@ -158,9 +195,15 @@ export class TlsTicketState {
158
195
  suite: i32
159
196
  /** The ticket's lifetime in seconds, at most `TLS_TICKET_LIFETIME_SECONDS`. */
160
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. */
161
199
  serverNameLength: i32
200
+ /** How many bytes of `psk` the PSK fills: 32 or 48, the suite's hash length. */
162
201
  pskLength: i32
163
202
 
203
+ /**
204
+ * Makes an empty state, its server name and PSK arrays at their fixed sizes
205
+ * and both lengths 0.
206
+ */
164
207
  constructor() {
165
208
  this.suite = 0
166
209
  this.issuedAt = 0
@@ -173,7 +216,11 @@ export class TlsTicketState {
173
216
  this.id = new Array<u8>(TLS_TICKET_NONCE_SIZE)
174
217
  }
175
218
 
176
- /** When the ticket stops opening: its issue time plus its lifetime. It opens at that instant, and not after. */
219
+ /**
220
+ * When the ticket stops opening: its issue time plus its lifetime. It opens at that instant, and not after.
221
+ *
222
+ * @returns The expiry in `monotonicNanos()`, which `TlsTicketStrikes.take` takes as its `expiresAt`.
223
+ */
177
224
  expiresAt(): i64 {
178
225
  return this.issuedAt + toI64(this.lifetime) * TLS_TICKET_NANOS
179
226
  }
@@ -183,6 +230,10 @@ export class TlsTicketState {
183
230
  * Draws a fresh key: 32 random bytes moved into the `Secret` it answers, a
184
231
  * fresh random id in `info`, `now` as its creation time and a count of 0.
185
232
  * The seal cap is left as the program set it.
233
+ *
234
+ * @param info The record to fill for the new key; its old id, time and count are overwritten.
235
+ * @param now The draw time, in `monotonicNanos()`.
236
+ * @returns The new key, owned by the caller, who keeps it in a local and wipes it when it is replaced.
186
237
  */
187
238
  export const tlsTicketKeyDraw = (info: TlsTicketKeyInfo, now: i64): Secret<u8[]> => {
188
239
  const key: u8[] = new Array<u8>(TLS_TICKET_KEY_SIZE)
@@ -193,7 +244,13 @@ export const tlsTicketKeyDraw = (info: TlsTicketKeyInfo, now: i64): Secret<u8[]>
193
244
  return secret(key)
194
245
  }
195
246
 
196
- /** True when the key `info` describes should be replaced: at the end of its issue window, or at its seal cap. */
247
+ /**
248
+ * True when the key `info` describes should be replaced: at the end of its issue window, or at its seal cap.
249
+ *
250
+ * @param info The current key's record.
251
+ * @param now The time to judge at, in `monotonicNanos()`.
252
+ * @returns True when the program should rotate now; the key still opens tickets for a while after.
253
+ */
197
254
  export const tlsTicketKeyDue = (info: TlsTicketKeyInfo, now: i64): boolean =>
198
255
  now - info.createdAt >= TLS_TICKET_SEAL_WINDOW || info.sealed >= info.sealCap
199
256
 
@@ -226,6 +283,9 @@ const tlsTicketPlainLength = (state: TlsTicketState): i32 => {
226
283
  * The bytes `tlsTicketSeal` makes of `state`, 0 when it cannot seal it: a
227
284
  * suite this server does not know, a PSK of another length than its hash, or
228
285
  * a server name longer than 255 bytes, which is issued no ticket.
286
+ *
287
+ * @param state The state to be sealed, with its suite, server name and PSK lengths set.
288
+ * @returns The ticket's length in bytes, the least `out` that `tlsTicketSeal` accepts, or 0.
229
289
  */
230
290
  export const tlsTicketSize = (state: TlsTicketState): i32 => {
231
291
  const plain: i32 = tlsTicketPlainLength(state)
@@ -294,6 +354,14 @@ const tlsTicketSealExposed = (key: u8[], input: u8[]): u8[] => {
294
354
  * The plaintext is built after the key id and nonce in one array, the one
295
355
  * `exposeWith` hands its function, and wiped once, after the seal and its
296
356
  * refusals.
357
+ *
358
+ * @param key The ticket key, lent: it is read only inside `exposeWith`, and stays the caller's to wipe.
359
+ * @param info The key's record: its id heads the ticket and its count goes up by one on a seal.
360
+ * @param state What the ticket carries; its `issuedAt` is overwritten with `now`, unless the key
361
+ * cannot seal at `now`, the state does not fit a ticket or `out` is too short.
362
+ * @param now The issue time, in `monotonicNanos()`.
363
+ * @param out Where the ticket is written, from index 0; at least `tlsTicketSize(state)` bytes.
364
+ * @returns How many bytes of `out` hold the ticket, or 0 when nothing was sealed.
297
365
  */
298
366
  export const tlsTicketSeal = (
299
367
  key: Secret<u8[]>,
@@ -440,6 +508,14 @@ const tlsTicketNamesKey = (info: TlsTicketKeyInfo, ticket: u8[]): boolean => {
440
508
  * the AEAD ran, the state's PSK array is wiped. The plaintext is wiped once,
441
509
  * after the branch on the tag and on the parse, so the one wipe covers every
442
510
  * path.
511
+ *
512
+ * @param key The ticket key, lent: it is read only inside `exposeWith`, and stays the caller's to wipe.
513
+ * @param info The key's record, whose id the ticket must name.
514
+ * @param ticket The ticket as the client sent it: key id, nonce, ciphertext and tag.
515
+ * @param now The time to judge the key and the ticket at, in `monotonicNanos()`.
516
+ * @param out The state to fill; its PSK array holds secret bytes on true, which the caller wipes once used.
517
+ * @returns True when the ticket opened and is within its bounds; it is not yet checked against the strike
518
+ * register, which `TlsTicketStrikes.take` does.
443
519
  */
444
520
  export const tlsTicketOpen = (
445
521
  key: Secret<u8[]>,
@@ -478,20 +554,40 @@ export const tlsTicketOpen = (
478
554
  */
479
555
  export class TlsTicketStrikes {
480
556
  /** The ids, `TLS_TICKET_NONCE_SIZE` bytes a slot. */
481
- ids: u8[]
482
- /** Each slot's expiry: the entry is held through it, inclusive, and free strictly after. */
557
+ private ids: u8[]
558
+ /**
559
+ * Each slot's expiry: the entry is held through it, inclusive, and free strictly after.
560
+ *
561
+ * @internal Read by `net/tls.ts`.
562
+ */
483
563
  expires: i64[]
484
- /** 1 for a slot that has ever held an id. */
564
+ /**
565
+ * 1 for a slot that has ever held an id.
566
+ *
567
+ * @internal Read by `net/tls.ts`.
568
+ */
485
569
  used: u8[]
486
570
  /** How many `take`s were refused because no entry was free. */
487
571
  refusedFull: i64
488
- /** How many ids the register holds. */
572
+ /**
573
+ * How many ids the register holds.
574
+ *
575
+ * @internal Read by `net/tls.ts`.
576
+ */
489
577
  capacity: i32
490
578
  /** How many ids are held or were held and expired: slots ever used. */
491
579
  filled: i32
492
- /** The farthest from its home slot any id has been recorded: no entry of an id lies further along its chain. */
580
+ /**
581
+ * The farthest from its home slot any id has been recorded: no entry of an id lies further along its chain.
582
+ * `net/tls.ts` reads it to stop a probe.
583
+ */
493
584
  longest: i32
494
585
 
586
+ /**
587
+ * Makes the whole table now, so that no `take` ever allocates; it panics on a capacity below 1.
588
+ *
589
+ * @param capacity How many ids the register can hold at once, `TLS_TICKET_STRIKES_DEFAULT` when in doubt.
590
+ */
495
591
  constructor(capacity: i32) {
496
592
  if (capacity < 1) {
497
593
  panic("TlsTicketStrikes: a capacity below 1")
@@ -505,7 +601,12 @@ export class TlsTicketStrikes {
505
601
  this.refusedFull = 0
506
602
  }
507
603
 
508
- /** The slot an id's probe chain starts at: FNV-1a over its bytes. */
604
+ /**
605
+ * The slot an id's probe chain starts at: FNV-1a over its bytes.
606
+ *
607
+ * @param id The ticket id; only its first `TLS_TICKET_NONCE_SIZE` bytes are hashed.
608
+ * @returns A slot index, from 0 to `capacity - 1`.
609
+ */
509
610
  home(id: u8[]): i32 {
510
611
  let hash: i64 = 2166136261
511
612
  for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE && k < toI32(id.length); k++) {
@@ -514,7 +615,11 @@ export class TlsTicketStrikes {
514
615
  return toI32(hash % toI64(this.capacity))
515
616
  }
516
617
 
517
- /** True when slot `slot` holds `id`. */
618
+ /**
619
+ * True when slot `slot` holds `id`.
620
+ *
621
+ * @internal Read by `net/tls.ts`.
622
+ */
518
623
  holds(slot: i32, id: u8[]): boolean {
519
624
  const at: i32 = slot * TLS_TICKET_NONCE_SIZE
520
625
  for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE && k < toI32(id.length); k++) {
@@ -525,7 +630,13 @@ export class TlsTicketStrikes {
525
630
  return true
526
631
  }
527
632
 
528
- /** How many entries hold `id` at `now`, live or expired: what the tests read to see one id recorded once. */
633
+ /**
634
+ * How many entries hold `id`, live or expired: what the tests read to see one id recorded once.
635
+ * It walks the whole table, so it is for tests and diagnostics, not for the handshake.
636
+ *
637
+ * @param id The ticket id to look for.
638
+ * @returns The number of slots ever used that hold `id`.
639
+ */
529
640
  count(id: u8[]): i32 {
530
641
  let found: i32 = 0
531
642
  for (let slot: i32 = 0; slot < this.capacity; slot++) {
@@ -536,7 +647,12 @@ export class TlsTicketStrikes {
536
647
  return found
537
648
  }
538
649
 
539
- /** How many entries are held at `now`. */
650
+ /**
651
+ * How many entries are held at `now`.
652
+ *
653
+ * @param now The time to judge at, in `monotonicNanos()`; an entry whose expiry is `now` still counts.
654
+ * @returns The number of live entries, from 0 to `capacity`.
655
+ */
540
656
  live(now: i64): i32 {
541
657
  let held: i32 = 0
542
658
  for (let slot: i32 = 0; slot < this.capacity; slot++) {
@@ -557,6 +673,11 @@ export class TlsTicketStrikes {
557
673
  * looks for a free slot and stops at the first, so a `take` costs the
558
674
  * longest probe so far, not the capacity. An id of another length than a
559
675
  * nonce is refused, uncounted.
676
+ *
677
+ * @param id The ticket's id, its nonce (`TlsTicketState.id` after `tlsTicketOpen`).
678
+ * @param expiresAt When the ticket expires, in `monotonicNanos()` (`TlsTicketState.expiresAt()`).
679
+ * @param now The time of the resumption, in `monotonicNanos()`.
680
+ * @returns True when the id was recorded and the ticket may resume; false means refuse the resumption.
560
681
  */
561
682
  take(id: u8[], expiresAt: i64, now: i64): boolean {
562
683
  if (toI32(id.length) !== TLS_TICKET_NONCE_SIZE) {
@@ -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 —
@@ -100,18 +101,38 @@ const TLS_VERIFY_P384_WIDTH: i32 = 48
100
101
  * made with them.
101
102
  */
102
103
  export class TlsClientTrust {
103
- /** The roots, as `pkiVerifyServer` looks them up. */
104
+ /**
105
+ * The roots, as `pkiVerifyServer` looks them up.
106
+ *
107
+ * @internal Read by the free functions of this module.
108
+ */
104
109
  store: PkiTrustStore
105
110
 
111
+ /**
112
+ * Indexes `roots` once, so every connection made with this trust looks its
113
+ * issuers up in the same store rather than scanning the list again.
114
+ *
115
+ * @param roots The certificates to trust as chain anchors, already parsed.
116
+ */
106
117
  constructor(roots: X509Certificate[]) {
107
118
  this.store = pkiTrustStore(roots)
108
119
  }
109
120
  }
110
121
 
111
- /** The trust of `roots`, as `nish/crypto/pki-roots` read them. */
122
+ /**
123
+ * The trust of `roots`, as `nish/crypto/pki-roots` read them.
124
+ *
125
+ * @param roots The roots a loader such as `pkiLoadSystemRoots` read; only their parsed certificates are kept.
126
+ * @returns A trust to hand to `tlsClientAuthenticate`, `tlsClientVerdict` or `tlsClientChain`.
127
+ */
112
128
  export const tlsClientTrust = (roots: PkiRoots): TlsClientTrust => new TlsClientTrust(roots.roots)
113
129
 
114
- /** The trust of the system's roots (`pkiLoadSystemRoots`): what a client trusts unless it is given a store. */
130
+ /**
131
+ * The trust of the system's roots (`pkiLoadSystemRoots`): what a client trusts unless it is given a store.
132
+ * It reads the roots from disk each time it is called, so a program makes it once and shares it.
133
+ *
134
+ * @returns A trust over the roots `SSL_CERT_FILE` and `SSL_CERT_DIR` name, or else the system bundle's.
135
+ */
115
136
  export const tlsClientSystemTrust = (): TlsClientTrust => tlsClientTrust(pkiLoadSystemRoots())
116
137
 
117
138
  /**
@@ -119,6 +140,12 @@ export const tlsClientSystemTrust = (): TlsClientTrust => tlsClientTrust(pkiLoad
119
140
  * verifies over `content` under `leaf`'s key. `false` for a scheme that
120
141
  * does not fit the key's kind and for every scheme this client does not
121
142
  * offer.
143
+ *
144
+ * @param leaf The server's leaf certificate, whose public key the signature must verify under.
145
+ * @param scheme The `TLS_SIGNATURE_*` code point the CertificateVerify names.
146
+ * @param content The bytes the server signed, as RFC 8446 §4.4.3 builds them from the transcript hash.
147
+ * @param signature The signature as sent: DER for ECDSA, the raw modulus-sized value for RSA-PSS.
148
+ * @returns True only when the signature verifies under `leaf`'s key with `scheme`.
122
149
  */
123
150
  export const tlsVerifyCertificateVerify = (
124
151
  leaf: X509Certificate,
@@ -176,6 +203,13 @@ const tlsVerifyKeyUsable = (leaf: X509Certificate): boolean => {
176
203
  * The chain step: 0 when `chain` (DER, leaf first) leads to a root in
177
204
  * `trust` and names `serverName` at `nowMs`, else the alert —
178
205
  * `certificate_expired`, `unknown_ca` or `bad_certificate`.
206
+ *
207
+ * @param chain Each certificate's DER as the server sent it, the leaf first.
208
+ * @param serverName The name or address dialled, ASCII (an internationalised name in its A-label form).
209
+ * @param nowMs The instant to judge validity at, in milliseconds since 1970-01-01 UTC.
210
+ * @param trust The roots the chain must lead to.
211
+ * @returns 0 when the chain is accepted, else `TLS_ALERT_CERTIFICATE_EXPIRED` (also for a certificate not
212
+ * yet valid), `TLS_ALERT_UNKNOWN_CA`, or `TLS_ALERT_BAD_CERTIFICATE` for every other refusal.
179
213
  */
180
214
  export const tlsClientChain = (chain: u8[][], serverName: string, nowMs: i64, trust: TlsClientTrust): i32 => {
181
215
  const result: PkiResult = pkiVerifyServer(chain, serverName, nowMs, trust.store)
@@ -193,6 +227,13 @@ export const tlsClientChain = (chain: u8[][], serverName: string, nowMs: i64, tr
193
227
  * CertificateVerify verifies under the leaf's key and the chain leads from
194
228
  * that leaf to a root in `trust` for the configured server name at `nowMs`
195
229
  * (milliseconds since 1970-01-01 UTC), else the alert to refuse with.
230
+ *
231
+ * @param tls The client, stopped at `TLS_CLIENT_STATE_WAIT_AUTH`; it is only read, not advanced.
232
+ * @param trust The roots the server's chain must lead to.
233
+ * @param nowMs The instant to judge validity at, in milliseconds since 1970-01-01 UTC.
234
+ * @returns 0 when the server is authenticated; `TLS_ALERT_BAD_CERTIFICATE` for an empty chain, a leaf that
235
+ * does not parse or whose key is unusable; `TLS_ALERT_DECRYPT_ERROR` when the signature does not verify;
236
+ * else `tlsClientChain`'s answer.
196
237
  */
197
238
  export const tlsClientVerdict = (tls: TlsClient, trust: TlsClientTrust, nowMs: i64): i32 => {
198
239
  const chain: u8[][] = tls.peerCertificates()
@@ -214,6 +255,12 @@ export const tlsClientVerdict = (tls: TlsClient, trust: TlsClientTrust, nowMs: i
214
255
  * `TLS_RECORD_AUTH`: `tlsClientVerdict`, then `client.authenticate(true)` on
215
256
  * 0 or `client.reject(alert)` otherwise, which seals the alert. Answers 0
216
257
  * or the alert the connection failed with.
258
+ *
259
+ * @param client The record-layer client, whose interest says `TLS_RECORD_AUTH`.
260
+ * @param trust The roots the server's chain must lead to.
261
+ * @param nowMs The instant to judge validity at, in milliseconds since 1970-01-01 UTC.
262
+ * @returns 0 when the server was accepted and the handshake went on, else the alert the connection failed
263
+ * with: the verdict's, or one `client.authenticate(true)` reports.
217
264
  */
218
265
  export const tlsClientAuthenticate = (client: TlsRecordClient, trust: TlsClientTrust, nowMs: i64): i32 => {
219
266
  const alert: i32 = tlsClientVerdict(client.tls, trust, nowMs)