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