@amritk/nish-x86_64-linux 0.35.0 → 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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -141,7 +141,12 @@ export const TLS_CLIENT_STATE_START: i32 = 0
141
141
  export const TLS_CLIENT_STATE_WAIT_SERVER_HELLO: i32 = 1
142
142
  /** The handshake secrets are known; EncryptedExtensions comes next, at the Handshake level. */
143
143
  export const TLS_CLIENT_STATE_WAIT_ENCRYPTED_EXTENSIONS: i32 = 2
144
+ /**
145
+ * EncryptedExtensions is read; the server's Certificate comes next, at the Handshake level. A
146
+ * CertificateRequest here is `unexpected_message`: this client does not authenticate itself.
147
+ */
144
148
  export const TLS_CLIENT_STATE_WAIT_CERTIFICATE: i32 = 3
149
+ /** The server's chain is read; its CertificateVerify comes next, at the Handshake level. */
145
150
  export const TLS_CLIENT_STATE_WAIT_CERTIFICATE_VERIFY: i32 = 4
146
151
  /**
147
152
  * The server's chain and signature are read and wait for the caller's
@@ -153,6 +158,7 @@ export const TLS_CLIENT_STATE_WAIT_AUTH: i32 = 5
153
158
  export const TLS_CLIENT_STATE_WAIT_FINISHED: i32 = 6
154
159
  /** Both Finished are done and the application secrets known. */
155
160
  export const TLS_CLIENT_STATE_CONNECTED: i32 = 7
161
+ /** The handshake failed or was wiped: every secret is zeroed, and `alert` says why. No state follows it. */
156
162
  export const TLS_CLIENT_STATE_FAILED: i32 = 8
157
163
 
158
164
  /** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
@@ -181,6 +187,15 @@ export class TlsClientConfig {
181
187
  /** The group of the one key share the first ClientHello carries; one of `groups`. */
182
188
  shareGroup: i32
183
189
 
190
+ /**
191
+ * A configuration for `serverName` offering `alpn`, with every suite this tree negotiates and an x25519
192
+ * share. Nothing is checked here: `TlsClient`'s constructor checks the whole configuration.
193
+ *
194
+ * @param serverName The host to reach: at most 253 ASCII bytes, sent as `server_name` unless it is an
195
+ * IP literal, and the name the caller authenticates the certificate against.
196
+ * @param alpn The ALPN protocols to offer, most preferred first, each 1 to 255 ASCII bytes; empty sends
197
+ * no ALPN. The array is kept, not copied.
198
+ */
184
199
  constructor(serverName: string, alpn: string[]) {
185
200
  this.serverName = serverName
186
201
  this.alpn = alpn
@@ -201,6 +216,10 @@ const tlsClientKnowsGroup = (group: i32): boolean =>
201
216
  * Whether `name` is an IP literal, which RFC 6066 §3 keeps out of
202
217
  * `server_name`: dotted decimal (digits and dots only), or anything with a
203
218
  * colon, which no DNS name has.
219
+ *
220
+ * @param name A host name as the caller gave it, such as `TlsClientConfig.serverName`.
221
+ * @returns True when `name` is non-empty and all digits and dots, or holds a colon anywhere; such a
222
+ * name is not sent as `server_name`.
204
223
  */
205
224
  export const tlsClientIsIpLiteral = (name: string): boolean => {
206
225
  let digits: boolean = true
@@ -264,57 +283,77 @@ const tlsClientConfigFits = (config: TlsClientConfig): boolean => {
264
283
  return toI32(config.serverName.length) <= TLS_CLIENT_SERVER_NAME_MAX && tlsClientIsAscii(config.serverName)
265
284
  }
266
285
 
286
+ /**
287
+ * The client side of one TLS 1.3 handshake, sans-IO: it takes the server's handshake messages through
288
+ * `receive`, writes its own for `takeOutput`, and derives each secret into `secrets()` as it becomes
289
+ * known. It never judges the server's certificate itself: at `TLS_CLIENT_STATE_WAIT_AUTH` it waits for
290
+ * the caller's `authenticate` or `reject`. One instance serves one connection; once it fails or is
291
+ * wiped it stays failed.
292
+ */
267
293
  export class TlsClient {
294
+ /** @internal Read by `net/tls/verify.ts`. */
268
295
  config: TlsClientConfig
269
- clientRandom: u8[]
296
+ private clientRandom: u8[]
270
297
  /** The x25519 private key, copied in by the constructor; wiped once the server picks a group, unless it picked x25519, then once the exchange is done. */
271
- readonly x25519Key: Secret<u8[]>
298
+ private readonly x25519Key: Secret<u8[]>
272
299
  /** The P-256 private key, held the same way. */
273
- readonly p256Key: Secret<u8[]>
300
+ private readonly p256Key: Secret<u8[]>
274
301
  /** The legacy_session_id sent, which the ServerHello must echo. */
275
302
  sessionId: u8[]
276
- transcript: TlsTranscript
303
+ private transcript: TlsTranscript
277
304
  /**
278
305
  * The first ClientHello, kept plain until the ServerHello names the hash:
279
306
  * the transcript feeds one hash only, so it starts from this copy.
280
307
  */
281
- firstHello: u8[]
308
+ private firstHello: u8[]
282
309
  /** The bytes received at the current level and not yet read: `input[0 .. inputLength)`, whole messages and the start of the next. */
283
- input: u8[]
284
- /** What the client has written at the Initial level and not handed on. */
310
+ private input: u8[]
311
+ /**
312
+ * What the client has written at the Initial level and not handed on.
313
+ *
314
+ * @internal Read by `net/tls/record-client.ts`.
315
+ */
285
316
  outputInitial: u8[]
286
- /** What the client has written at the Handshake level and not handed on: its Finished. */
317
+ /**
318
+ * What the client has written at the Handshake level and not handed on: its Finished.
319
+ *
320
+ * @internal Read by `net/tls/record-client.ts`.
321
+ */
287
322
  outputHandshake: u8[]
288
- secrets256: TlsSecretPool
289
- secrets384: TlsSecretPool
323
+ private secrets256: TlsSecretPool
324
+ private secrets384: TlsSecretPool
290
325
  /** The server message last read, as windows into `input`. */
291
- view: TlsServerFlightView
326
+ private view: TlsServerFlightView
292
327
  /** The HelloRetryRequest's cookie, echoed in the second ClientHello; empty if it sent none. */
293
- cookie: u8[]
328
+ private cookie: u8[]
294
329
  /** The server's DER certificates, leaf first, once its Certificate is read. */
295
- certificates: u8[][]
330
+ private certificates: u8[][]
296
331
  /** What the server's CertificateVerify signs (§4.4.3), once its Certificate is read. */
297
- content: u8[]
332
+ private content: u8[]
298
333
  /** The CertificateVerify signature, as it came on the wire. */
299
- signature: u8[]
334
+ private signature: u8[]
300
335
  /** A transcript hash on its way into the schedule: HashLen bytes of it. */
301
- hashes: u8[]
336
+ private hashes: u8[]
302
337
  /** The ALPN protocol the server chose, one of the configuration's, or empty. */
303
338
  alpnChosen: string = ""
304
339
  /** Which of the negotiated pool's secrets are known: bit `1 << TLS_SECRET_*` for each, as `TlsServer.held`. */
305
- held: i32 = 0
340
+ private held: i32 = 0
306
341
  state: i32 = 0
307
342
  /** The alert the client failed with, or 0. */
308
343
  alert: i32 = 0
309
344
  /** The negotiated cipher suite, or 0 before the ServerHello (or HelloRetryRequest) is read. */
310
345
  suite: i32 = 0
346
+ /** @internal Read by `net/tls/record-client.ts`. */
311
347
  hashLength: i32 = 0
312
348
  /** The group of the key share the last ClientHello carried. */
313
349
  shareGroup: i32 = 0
314
350
  /** The CertificateVerify scheme. */
315
351
  scheme: i32 = 0
352
+ /** @internal Read by `net/tls/record-client.ts`. */
316
353
  inputLength: i32 = 0
354
+ /** @internal Read by `net/tls/record-client.ts`. */
317
355
  outputInitialLength: i32 = 0
356
+ /** @internal Read by `net/tls/record-client.ts`. */
318
357
  outputHandshakeLength: i32 = 0
319
358
  /** Whether a HelloRetryRequest was read, so a second is fatal. */
320
359
  retried: boolean = false
@@ -327,6 +366,14 @@ export class TlsClient {
327
366
  * keys are copied in and the caller's arrays zeroed. A random or key of
328
367
  * another length, or a configuration it cannot offer, leaves the client
329
368
  * failed with `internal_error` before it writes anything.
369
+ *
370
+ * @param config What to offer; kept, not copied, so it must not change while the handshake runs.
371
+ * @param clientRandom 32 fresh random bytes for the ClientHello's random; copied, so the array is the
372
+ * caller's again.
373
+ * @param x25519Private 32 fresh random bytes for the x25519 key share; copied into the client's own
374
+ * `Secret`, and this array zeroed whatever its length.
375
+ * @param p256Private 32 fresh random bytes for a P-256 key share, used only if the share or a
376
+ * HelloRetryRequest names secp256r1; copied and zeroed the same way.
330
377
  */
331
378
  constructor(config: TlsClientConfig, clientRandom: u8[], x25519Private: u8[], p256Private: u8[]) {
332
379
  const x25519Length: i32 = toI32(x25519Private.length)
@@ -369,7 +416,13 @@ export class TlsClient {
369
416
  }
370
417
  }
371
418
 
372
- /** Writes the first ClientHello at the Initial level. Answers 0, or the alert the client failed with; a second call is `internal_error`. */
419
+ /**
420
+ * Writes the first ClientHello at the Initial level. Answers 0, or the alert the client failed with;
421
+ * a second call is `internal_error`.
422
+ *
423
+ * @returns 0 once the ClientHello waits in `takeOutput(TLS_LEVEL_INITIAL)`, or the TLS alert code
424
+ * (1 to 255) the client failed with.
425
+ */
373
426
  start(): i32 {
374
427
  if (this.state === TLS_CLIENT_STATE_FAILED) {
375
428
  return this.alert
@@ -394,7 +447,7 @@ export class TlsClient {
394
447
  * pass the 65,535 bytes their length can say (a HelloRetryRequest's cookie
395
448
  * near that size).
396
449
  */
397
- writeHello(): i32 {
450
+ private writeHello(): i32 {
398
451
  const share: u8[] | null =
399
452
  this.shareGroup === TLS_GROUP_X25519 ? x25519Base(this.x25519Key) : p256PublicKey(this.p256Key)
400
453
  if (share === null) {
@@ -441,24 +494,33 @@ export class TlsClient {
441
494
  }
442
495
 
443
496
  /** Fails the handshake with `alert` and answers it, wiping every secret the client holds. */
444
- fail(alert: i32): i32 {
497
+ private fail(alert: i32): i32 {
445
498
  this.wipe()
446
499
  this.state = TLS_CLIENT_STATE_FAILED
447
500
  this.alert = alert
448
501
  return alert
449
502
  }
450
503
 
451
- /** The pool of the negotiated hash: SHA-384's for a SHA-384 suite, SHA-256's otherwise. */
504
+ /**
505
+ * The pool of the negotiated hash: SHA-384's for a SHA-384 suite, SHA-256's otherwise.
506
+ *
507
+ * @returns The client's own pool, one named field per secret; only the secrets the handshake has
508
+ * reached hold a value. It is the client's own, not a copy, and `wipe` zeroes it.
509
+ */
452
510
  secrets(): TlsSecretPool {
453
511
  return this.hashLength === SHA384_SIZE ? this.secrets384 : this.secrets256
454
512
  }
455
513
 
456
514
  /** Whether the handshake has derived secret `index` (`TLS_SECRET_*`). */
457
- known(index: i32): boolean {
515
+ private known(index: i32): boolean {
458
516
  return index >= 0 && index < 31 && (this.held & (1 << index)) !== 0
459
517
  }
460
518
 
461
- /** Whether the secret the client *reads* `level` under — the server's, `serverHandshake` or `serverApplication` — is known yet. */
519
+ /**
520
+ * Whether the secret the client *reads* `level` under — the server's, `serverHandshake` or `serverApplication` — is known yet.
521
+ *
522
+ * @internal Read by `net/tls/record-client.ts`.
523
+ */
462
524
  readKnown(level: i32): boolean {
463
525
  if (level === TLS_LEVEL_HANDSHAKE) {
464
526
  return this.known(TLS_SECRET_SERVER_HANDSHAKE)
@@ -466,7 +528,11 @@ export class TlsClient {
466
528
  return level === TLS_LEVEL_APPLICATION && this.known(TLS_SECRET_SERVER_APPLICATION)
467
529
  }
468
530
 
469
- /** Whether the secret the client *writes* `level` under — `clientHandshake` or `clientApplication` — is known yet. */
531
+ /**
532
+ * Whether the secret the client *writes* `level` under — `clientHandshake` or `clientApplication` — is known yet.
533
+ *
534
+ * @internal Read by `net/tls/record-client.ts`.
535
+ */
470
536
  writeKnown(level: i32): boolean {
471
537
  if (level === TLS_LEVEL_HANDSHAKE) {
472
538
  return this.known(TLS_SECRET_CLIENT_HANDSHAKE)
@@ -474,7 +540,11 @@ export class TlsClient {
474
540
  return level === TLS_LEVEL_APPLICATION && this.known(TLS_SECRET_CLIENT_APPLICATION)
475
541
  }
476
542
 
477
- /** Zeroes secret `index` of `secrets()` once a carrier has taken what it needs from it; it stays known. */
543
+ /**
544
+ * Zeroes secret `index` of `secrets()` once a carrier has taken what it needs from it; it stays known.
545
+ *
546
+ * @internal Read by `net/tls/record-client.ts`.
547
+ */
478
548
  wipeSecret(index: i32): void {
479
549
  this.secrets().zero(index)
480
550
  }
@@ -497,7 +567,7 @@ export class TlsClient {
497
567
  }
498
568
 
499
569
  /** The level the server's next bytes must arrive at, or -1 when the state expects none. */
500
- expectedLevel(): i32 {
570
+ private expectedLevel(): i32 {
501
571
  switch (this.state) {
502
572
  case TLS_CLIENT_STATE_WAIT_SERVER_HELLO:
503
573
  return TLS_LEVEL_INITIAL
@@ -515,7 +585,7 @@ export class TlsClient {
515
585
  }
516
586
 
517
587
  /** Makes `input` hold at least `need` bytes, keeping the ones it holds; it doubles, and never grows past one largest message. */
518
- reserveInput(need: i32): void {
588
+ private reserveInput(need: i32): void {
519
589
  const capacity: i32 = toI32(this.input.length)
520
590
  if (need <= capacity) {
521
591
  return
@@ -533,7 +603,7 @@ export class TlsClient {
533
603
  }
534
604
 
535
605
  /** Makes the Initial output hold at least `need` bytes, keeping the ones it holds. */
536
- reserveOutput(need: i32): void {
606
+ private reserveOutput(need: i32): void {
537
607
  const capacity: i32 = toI32(this.outputInitial.length)
538
608
  if (need <= capacity) {
539
609
  return
@@ -558,6 +628,13 @@ export class TlsClient {
558
628
  * until `authenticate`. Once connected, the Application level takes
559
629
  * NewSessionTickets, which are checked and dropped; anything else there is
560
630
  * `unexpected_message`, KeyUpdate being the record layer's.
631
+ *
632
+ * @param level The level the bytes arrived at: `TLS_LEVEL_INITIAL`, `TLS_LEVEL_HANDSHAKE` or
633
+ * `TLS_LEVEL_APPLICATION`, from `nish/net/tls`.
634
+ * @param data The buffer holding the server's handshake bytes; they are copied, not kept.
635
+ * @param off Where in `data` the bytes start.
636
+ * @param len How many bytes, from `off`, to take; 0 takes nothing.
637
+ * @returns 0, or the TLS alert code (1 to 255) the client failed with.
561
638
  */
562
639
  receive(level: i32, data: u8[], off: i32, len: i32): i32 {
563
640
  if (this.state === TLS_CLIENT_STATE_FAILED) {
@@ -608,7 +685,7 @@ export class TlsClient {
608
685
  * read, until a partial one is left or the state waits on the caller.
609
686
  * Answers 0 or the alert.
610
687
  */
611
- drain(): i32 {
688
+ private drain(): i32 {
612
689
  while (this.state !== TLS_CLIENT_STATE_WAIT_AUTH && this.inputLength >= 4) {
613
690
  const length: i32 = (toI32(this.input[1]) << 16) | (toI32(this.input[2]) << 8) | toI32(this.input[3])
614
691
  if (length > TLS_MAX_HANDSHAKE_MESSAGE) {
@@ -633,7 +710,7 @@ export class TlsClient {
633
710
  }
634
711
 
635
712
  /** Drops the first `count` bytes of `input`, moving the rest to its front. */
636
- consume(count: i32): void {
713
+ private consume(count: i32): void {
637
714
  const rest: i32 = this.inputLength - count
638
715
  for (let k: i32 = 0; k < rest && count + k < toI32(this.input.length); k++) {
639
716
  this.input[k] = this.input[count + k]
@@ -642,7 +719,7 @@ export class TlsClient {
642
719
  }
643
720
 
644
721
  /** Dispatches one whole message, `input[0 .. 4 + length)`, of type `type`, by state. */
645
- handleMessage(type: i32, length: i32): i32 {
722
+ private handleMessage(type: i32, length: i32): i32 {
646
723
  switch (this.state) {
647
724
  case TLS_CLIENT_STATE_WAIT_SERVER_HELLO:
648
725
  return type === TLS_HANDSHAKE_SERVER_HELLO
@@ -678,7 +755,7 @@ export class TlsClient {
678
755
  * extensions, the echoed session id, null compression and an offered
679
756
  * suite. Answers 0 or the alert.
680
757
  */
681
- checkHello(view: TlsServerFlightView): i32 {
758
+ private checkHello(view: TlsServerFlightView): i32 {
682
759
  if (view.alert !== 0) {
683
760
  return view.alert
684
761
  }
@@ -717,7 +794,7 @@ export class TlsClient {
717
794
  }
718
795
 
719
796
  /** A ServerHello or HelloRetryRequest, `input[0 .. 4 + length)`. */
720
- handleServerHello(length: i32): i32 {
797
+ private handleServerHello(length: i32): i32 {
721
798
  const view: TlsServerFlightView = this.view
722
799
  tlsReadServerHello(view, this.input, 4, length)
723
800
  if (view.isRetry && this.retried) {
@@ -764,7 +841,7 @@ export class TlsClient {
764
841
  }
765
842
 
766
843
  /** Starts the transcript over `suite`'s hash, from the first ClientHello. */
767
- startTranscript(suite: i32): void {
844
+ private startTranscript(suite: i32): void {
768
845
  this.suite = suite
769
846
  this.hashLength = tlsSuiteHashLength(suite)
770
847
  this.transcript.reset(this.hashLength)
@@ -779,7 +856,7 @@ export class TlsClient {
779
856
  * the new group and echoes the cookie. The key of the group not picked is
780
857
  * wiped now.
781
858
  */
782
- handleRetry(length: i32): i32 {
859
+ private handleRetry(length: i32): i32 {
783
860
  const view: TlsServerFlightView = this.view
784
861
  if (!view.hasKeyShare && !view.hasCookie) {
785
862
  return TLS_ALERT_ILLEGAL_PARAMETER
@@ -810,13 +887,13 @@ export class TlsClient {
810
887
  }
811
888
 
812
889
  /** Zeroes both private keys, once the exchange is done or cannot be. */
813
- wipeKeys(): void {
890
+ private wipeKeys(): void {
814
891
  exposeWith(this.x25519Key, TLS_CLIENT_FROM, tlsZeroHeld)
815
892
  exposeWith(this.p256Key, TLS_CLIENT_FROM, tlsZeroHeld)
816
893
  }
817
894
 
818
895
  /** Zeroes the private key of the group the client's share is not of. */
819
- wipeUnpicked(): void {
896
+ private wipeUnpicked(): void {
820
897
  if (this.shareGroup === TLS_GROUP_X25519) {
821
898
  exposeWith(this.p256Key, TLS_CLIENT_FROM, tlsZeroHeld)
822
899
  } else {
@@ -832,7 +909,7 @@ export class TlsClient {
832
909
  * a secp256r1 share that is not an uncompressed point on the curve
833
910
  * (§4.2.8.2); its shared secret is the product's x, which may be zero.
834
911
  */
835
- exchangeKeys(): i32 {
912
+ private exchangeKeys(): i32 {
836
913
  const view: TlsServerFlightView = this.view
837
914
  const peer: u8[] = tlsCopyWindow(view.data, view.shareAt, view.shareLength)
838
915
  const isX25519: boolean = this.shareGroup === TLS_GROUP_X25519
@@ -854,7 +931,7 @@ export class TlsClient {
854
931
  }
855
932
 
856
933
  /** EncryptedExtensions (§4.3.1): ALPN, if any, must be one the client offered, and `server_name` acknowledged only if sent. */
857
- handleEncryptedExtensions(length: i32): i32 {
934
+ private handleEncryptedExtensions(length: i32): i32 {
858
935
  const view: TlsServerFlightView = this.view
859
936
  tlsReadEncryptedExtensions(view, this.input, 4, length)
860
937
  if (view.alert !== 0) {
@@ -885,7 +962,7 @@ export class TlsClient {
885
962
  }
886
963
 
887
964
  /** The server's Certificate (§4.4.2): its request context must be empty. The chain is copied out, and what CertificateVerify signs computed. */
888
- handleCertificate(length: i32): i32 {
965
+ private handleCertificate(length: i32): i32 {
889
966
  const view: TlsServerFlightView = this.view
890
967
  tlsReadCertificate(view, this.input, 4, length)
891
968
  if (view.alert !== 0) {
@@ -905,7 +982,7 @@ export class TlsClient {
905
982
  }
906
983
 
907
984
  /** CertificateVerify (§4.4.3): a scheme the client offered. The signature is kept, unchecked, for the caller; the client then waits on it. */
908
- handleCertificateVerify(length: i32): i32 {
985
+ private handleCertificateVerify(length: i32): i32 {
909
986
  const view: TlsServerFlightView = this.view
910
987
  tlsReadCertificateVerify(view, this.input, 4, length)
911
988
  if (view.alert !== 0) {
@@ -927,7 +1004,7 @@ export class TlsClient {
927
1004
  * master secret, the application and exporter secrets, and the client's
928
1005
  * Finished at the Handshake level; the handshake secret is wiped.
929
1006
  */
930
- handleFinished(length: i32): i32 {
1007
+ private handleFinished(length: i32): i32 {
931
1008
  const h: i32 = this.hashLength
932
1009
  if (length !== h) {
933
1010
  return TLS_ALERT_DECODE_ERROR
@@ -979,32 +1056,53 @@ export class TlsClient {
979
1056
  }
980
1057
 
981
1058
  /** A NewSessionTicket (§4.6.1) once connected: checked for its structure and dropped, since this client does not resume. */
982
- handleTicket(length: i32): i32 {
1059
+ private handleTicket(length: i32): i32 {
983
1060
  tlsReadNewSessionTicket(this.view, this.input, 4, length)
984
1061
  return this.view.alert
985
1062
  }
986
1063
 
987
- /** The server's DER certificates, leaf first; empty before its Certificate is read. */
1064
+ /**
1065
+ * The server's DER certificates, leaf first; empty before its Certificate is read.
1066
+ *
1067
+ * @returns The client's own list, not a copy, for the caller to check before `authenticate`.
1068
+ */
988
1069
  peerCertificates(): u8[][] {
989
1070
  return this.certificates
990
1071
  }
991
1072
 
992
- /** The CertificateVerify scheme (`TLS_SIGNATURE_*`); 0 before it is read. */
1073
+ /**
1074
+ * The CertificateVerify scheme (`TLS_SIGNATURE_*`); 0 before it is read.
1075
+ *
1076
+ * @returns The signature scheme code point the server signed with, or 0.
1077
+ */
993
1078
  verifyScheme(): i32 {
994
1079
  return this.scheme
995
1080
  }
996
1081
 
997
- /** What the CertificateVerify signature is over (§4.4.3): 64 spaces, the server's context string, a zero byte and the transcript hash. */
1082
+ /**
1083
+ * What the CertificateVerify signature is over (§4.4.3): 64 spaces, the server's context string, a
1084
+ * zero byte and the transcript hash.
1085
+ *
1086
+ * @returns The client's own bytes, not a copy; empty before the server's Certificate is read.
1087
+ */
998
1088
  verifyContent(): u8[] {
999
1089
  return this.content
1000
1090
  }
1001
1091
 
1002
- /** The CertificateVerify signature as sent: DER for ECDSA, raw for RSA-PSS. */
1092
+ /**
1093
+ * The CertificateVerify signature as sent: DER for ECDSA, raw for RSA-PSS.
1094
+ *
1095
+ * @returns The client's own bytes, not a copy; empty before the CertificateVerify is read.
1096
+ */
1003
1097
  verifySignature(): u8[] {
1004
1098
  return this.signature
1005
1099
  }
1006
1100
 
1007
- /** The ALPN protocol the server chose, or empty when it chose none. */
1101
+ /**
1102
+ * The ALPN protocol the server chose, or empty when it chose none.
1103
+ *
1104
+ * @returns One of the configuration's `alpn` protocols, or the empty string.
1105
+ */
1008
1106
  alpn(): string {
1009
1107
  return this.alpnChosen
1010
1108
  }
@@ -1015,6 +1113,10 @@ export class TlsClient {
1015
1113
  * reading what was held meanwhile; `false` is `reject(bad_certificate)`.
1016
1114
  * Answers 0 or the alert; in any other state the client fails with
1017
1115
  * `internal_error`.
1116
+ *
1117
+ * @param ok True when the caller has checked the chain against its trust and the signature over
1118
+ * `verifyContent()`, and accepts the server.
1119
+ * @returns 0, or the TLS alert code the client failed with.
1018
1120
  */
1019
1121
  authenticate(ok: boolean): i32 {
1020
1122
  if (!ok) {
@@ -1040,6 +1142,10 @@ export class TlsClient {
1040
1142
  * `unknown_ca`, `decrypt_error`…): the client fails with it and answers it.
1041
1143
  * An alert outside 1 to 255 is taken as `bad_certificate`. In any other
1042
1144
  * state the client fails with `internal_error`.
1145
+ *
1146
+ * @param alert The TLS alert code to fail with, 1 to 255 (a `TLS_ALERT_*`).
1147
+ * @returns The alert the client failed with: `alert`, `bad_certificate`, `internal_error`, or the
1148
+ * earlier alert of a client that had already failed.
1043
1149
  */
1044
1150
  reject(alert: i32): i32 {
1045
1151
  if (this.state === TLS_CLIENT_STATE_FAILED) {
@@ -1052,7 +1158,7 @@ export class TlsClient {
1052
1158
  }
1053
1159
 
1054
1160
  /** A message the client wrote at `level`, `output[at .. end)`, goes into the transcript once its hash is known and stays on the output. */
1055
- sent(level: i32, at: i32, end: i32): void {
1161
+ private sent(level: i32, at: i32, end: i32): void {
1056
1162
  if (level === TLS_LEVEL_INITIAL) {
1057
1163
  if (this.hashLength !== 0) {
1058
1164
  this.transcript.update(this.outputInitial, at, end - at)
@@ -1064,7 +1170,12 @@ export class TlsClient {
1064
1170
  }
1065
1171
  }
1066
1172
 
1067
- /** The handshake bytes written at `level` since the last call, in a fresh array, and forgets them. */
1173
+ /**
1174
+ * The handshake bytes written at `level` since the last call, in a fresh array, and forgets them.
1175
+ *
1176
+ * @param level `TLS_LEVEL_INITIAL` for ClientHellos or `TLS_LEVEL_HANDSHAKE` for the Finished.
1177
+ * @returns A copy the caller owns, empty when nothing waits or for any other level.
1178
+ */
1068
1179
  takeOutput(level: i32): u8[] {
1069
1180
  const none: u8[] = []
1070
1181
  switch (level) {
@@ -1083,7 +1194,11 @@ export class TlsClient {
1083
1194
  }
1084
1195
  }
1085
1196
 
1086
- /** Forgets the handshake bytes written at `level`, which a carrier has read out of the buffer itself. */
1197
+ /**
1198
+ * Forgets the handshake bytes written at `level`, which a carrier has read out of the buffer itself.
1199
+ *
1200
+ * @internal Read by `net/tls/record-client.ts`.
1201
+ */
1087
1202
  clearOutput(level: i32): void {
1088
1203
  if (level === TLS_LEVEL_INITIAL) {
1089
1204
  this.outputInitialLength = 0