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
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 —
@@ -335,6 +337,9 @@ const tlsEcdsaDerInto = (der: u8[], rs: u8[]): void => {
335
337
  * An ECDSA signature `r || s` (32 bytes each, as `nish/crypto/p256` answers
336
338
  * it) as the DER `ECDSA-Sig-Value` a CertificateVerify carries (RFC 8446
337
339
  * §4.2.3), or `null` when `rs` is not 64 bytes.
340
+ *
341
+ * @param rs The signature's `r` then `s`, 32 big-endian bytes each.
342
+ * @returns A fresh array of 8 to 72 bytes holding the DER encoding, or `null`.
338
343
  */
339
344
  export const tlsEcdsaDerSignature = (rs: u8[]): u8[] | null => {
340
345
  if (toI32(rs.length) !== 64) {
@@ -351,6 +356,10 @@ export const tlsEcdsaDerSignature = (rs: u8[]): u8[] | null => {
351
356
  * encoded. `null` for a malformed key. The answer is what `TlsServer.sign`
352
357
  * takes. The key is borrowed, as every `Secret` parameter is: the caller made
353
358
  * it, and wipes it.
359
+ *
360
+ * @param priv The server's P-256 private key, borrowed.
361
+ * @param content The CertificateVerify input, as `TlsServer.signatureInput` answers it.
362
+ * @returns A fresh array holding the DER signature, or `null` for a malformed key.
354
363
  */
355
364
  export const tlsSignEcdsaP256 = (priv: Secret<u8[]>, content: u8[]): u8[] | null => {
356
365
  const rs: u8[] | null = p256SignSha256(priv, content)
@@ -372,12 +381,17 @@ export const tlsSignEcdsaP256 = (priv: Secret<u8[]>, content: u8[]): u8[] | null
372
381
  */
373
382
  export class TlsP256Signer {
374
383
  /** The P-256 signer's scratch. */
375
- scratch: P256SignScratch
384
+ private scratch: P256SignScratch
376
385
  /** `r || s` as `p256SignSha256Into` writes it. */
377
- rs: u8[]
386
+ private rs: u8[]
378
387
  /** The DER signatures, index `length - TLS_P256_DER_MIN`. */
379
- ders: u8[][]
388
+ private ders: u8[][]
380
389
 
390
+ /**
391
+ * Makes the signer's scratch and one signature array per DER length, so a
392
+ * signature keeps nothing: the P-256 step releases what it allocates per
393
+ * call, and the DER is written into one of these arrays.
394
+ */
381
395
  constructor() {
382
396
  this.scratch = new P256SignScratch()
383
397
  this.rs = new Array<u8>(P256_SIGNATURE_SIZE)
@@ -392,6 +406,10 @@ export class TlsP256Signer {
392
406
  * signer's that has its length; `null` for a malformed key. It keeps
393
407
  * nothing: the P-256 signature releases what it allocated, and the DER is
394
408
  * written in place. The key is borrowed.
409
+ *
410
+ * @param priv The server's P-256 private key, borrowed.
411
+ * @param content The CertificateVerify input, as `TlsServer.signatureInput` answers it.
412
+ * @returns One of this signer's own arrays, which a later `sign` of the same length overwrites, or `null`.
395
413
  */
396
414
  sign(priv: Secret<u8[]>, content: u8[]): u8[] | null {
397
415
  if (
@@ -409,7 +427,12 @@ export class TlsP256Signer {
409
427
  }
410
428
  }
411
429
 
412
- /** Whether an x25519 shared secret is all zeros, reading every byte (run inside `expose`). */
430
+ /**
431
+ * Whether an x25519 shared secret is all zeros, reading every byte (run inside `expose`).
432
+ *
433
+ * @param shared The exposed shared secret, `X25519_SIZE` bytes.
434
+ * @returns True when every byte is zero (a low-order peer share, RFC 7748 §6.1), which the handshake refuses.
435
+ */
413
436
  export const tlsIsZeroSecret = (shared: u8[]): boolean => timingSafeEqual(shared, new Array<u8>(X25519_SIZE))
414
437
 
415
438
  /**
@@ -437,13 +460,26 @@ const tlsHandshakeFromEcdhe256 = (ecdhe: u8[], salt: u8[]): u8[] => hmacSha256(s
437
460
  /** The SHA-384 handshake secret: HKDF-Extract of the ECDHE secret under `salt`, as the SHA-256 one is. */
438
461
  const tlsHandshakeFromEcdhe384 = (ecdhe: u8[], salt: u8[]): u8[] => hmacSha384(salt, ecdhe)
439
462
 
440
- /** Wipes the bytes a `Secret` holds, for `exposeWith` to run on one secret of a holder outside its `wipe()`; answers `answer`. */
463
+ /**
464
+ * Wipes the bytes a `Secret` holds, for `exposeWith` to run on one secret of a holder outside its `wipe()`.
465
+ *
466
+ * @param held The exposed bytes, zeroed in place.
467
+ * @param answer The value to hand back, so a caller can wipe and answer in one expression.
468
+ * @returns `answer`, unchanged.
469
+ */
441
470
  export const tlsZeroHeld = (held: u8[], answer: i32): i32 => {
442
471
  wipe(held)
443
472
  return answer
444
473
  }
445
474
 
446
- /** Overwrites the bytes a `Secret` holds with `from`'s, for `exposeWith` to run on the destination; answers 0. */
475
+ /**
476
+ * Overwrites the bytes a `Secret` holds with `from`'s, for `exposeWith` to run on the destination.
477
+ * Only as many bytes as the shorter of the two holds are copied; the rest of `held` is left as it was.
478
+ *
479
+ * @param held The destination's exposed bytes, written in place.
480
+ * @param from The bytes to copy in, read from index 0.
481
+ * @returns 0.
482
+ */
447
483
  export const tlsHeldCopyIn = (held: u8[], from: u8[]): i32 => {
448
484
  for (let k: i32 = 0; k < toI32(held.length) && k < toI32(from.length); k++) {
449
485
  held[k] = from[k]
@@ -455,6 +491,9 @@ export const tlsHeldCopyIn = (held: u8[], from: u8[]): i32 => {
455
491
  * A plain copy of a held secret, for a derivation that runs on its bytes
456
492
  * inside a `using arena()` block: the copy dies with the block, and its
457
493
  * caller `secureZero`s it before the block ends.
494
+ *
495
+ * @param held The exposed bytes to copy.
496
+ * @returns A fresh plain array of the same length, which the caller must wipe.
458
497
  */
459
498
  export const tlsHeldClone = (held: u8[]): u8[] => {
460
499
  const copy: u8[] = new Array<u8>(held.length)
@@ -468,6 +507,10 @@ export const tlsHeldClone = (held: u8[]): u8[] => {
468
507
  * Copies the bytes one `Secret` holds over another's, for `exposeWith` to
469
508
  * run on the source with the destination as its argument: no plain copy is
470
509
  * made on the way. Answers 0.
510
+ *
511
+ * @param from The source's exposed bytes.
512
+ * @param to The destination secret, borrowed; as many bytes as the shorter of the two holds are overwritten.
513
+ * @returns 0.
471
514
  */
472
515
  export const tlsHeldCopyOver = (from: u8[], to: Secret<u8[]>): i32 => exposeWith(to, from, tlsHeldCopyIn)
473
516
 
@@ -517,12 +560,19 @@ class TlsExchangeWords {
517
560
  * The salt the handshake secret is extracted under, `hashLength` bytes of
518
561
  * it: the `TLS_DERIVED_SALT_*` constant on a full handshake, the salt
519
562
  * derived from the PSK early secret on a resumed one.
563
+ *
564
+ * @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it.
520
565
  */
521
566
  salt0: u64 = 0
567
+ /** @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it. */
522
568
  salt1: u64 = 0
569
+ /** @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it. */
523
570
  salt2: u64 = 0
571
+ /** @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it. */
524
572
  salt3: u64 = 0
573
+ /** @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it. */
525
574
  salt4: u64 = 0
575
+ /** @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it. */
526
576
  salt5: u64 = 0
527
577
  /** 32 or 48: which handshake secret to extract. */
528
578
  hashLength: i32 = 0
@@ -643,14 +693,21 @@ const TLS_ECDSA_SIGNATURE_MAX: i32 = 72
643
693
  * that says the secret is derived (`1 << index`).
644
694
  */
645
695
  export const TLS_SECRET_HANDSHAKE: i32 = 0
696
+ /** `client_handshake_traffic_secret` (RFC 8446 §7.1): the client's Handshake-level keys. */
646
697
  export const TLS_SECRET_CLIENT_HANDSHAKE: i32 = 1
698
+ /** `server_handshake_traffic_secret` (RFC 8446 §7.1): the server's Handshake-level keys. */
647
699
  export const TLS_SECRET_SERVER_HANDSHAKE: i32 = 2
700
+ /** `client_application_traffic_secret_0` (RFC 8446 §7.1): the client's first application keys. */
648
701
  export const TLS_SECRET_CLIENT_APPLICATION: i32 = 3
702
+ /** `server_application_traffic_secret_0` (RFC 8446 §7.1): the server's first application keys. */
649
703
  export const TLS_SECRET_SERVER_APPLICATION: i32 = 4
704
+ /** `exporter_master_secret` (RFC 8446 §7.5), for a caller's own exporters. */
650
705
  export const TLS_SECRET_EXPORTER: i32 = 5
706
+ /** The client Finished's expected `verify_data` (RFC 8446 §4.4.4), plain, since the wire carries it. */
651
707
  export const TLS_SECRET_CLIENT_FINISHED: i32 = 6
652
708
  /** The CertificateVerify input sits beside them: 98 bytes and the transcript hash. */
653
709
  export const TLS_SECRET_TO_BE_SIGNED: i32 = 7
710
+ /** `resumption_master_secret` (RFC 8446 §7.1), what a NewSessionTicket's PSK is derived from. */
654
711
  export const TLS_SECRET_RESUMPTION: i32 = 8
655
712
  /** The PSK a ticket opened to, held from the binder's check to the early secret's extract. */
656
713
  export const TLS_SECRET_PSK: i32 = 9
@@ -672,10 +729,15 @@ const TLS_CERTIFICATE_VERIFY_PREFIX: i32 = 98
672
729
  * `wipe` zeroes them too (TLS-1).
673
730
  */
674
731
  export class TlsSecretPool {
732
+ /** `handshake_secret` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
675
733
  readonly handshake: Secret<u8[]>
734
+ /** `client_handshake_traffic_secret` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
676
735
  readonly clientHandshake: Secret<u8[]>
736
+ /** `server_handshake_traffic_secret` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
677
737
  readonly serverHandshake: Secret<u8[]>
738
+ /** `client_application_traffic_secret_0` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
678
739
  readonly clientApplication: Secret<u8[]>
740
+ /** `server_application_traffic_secret_0` (RFC 8446 §7.1), hash-length bytes; read it inside `expose`. */
679
741
  readonly serverApplication: Secret<u8[]>
680
742
  /** `exporter_master_secret` (RFC 8446 §7.5), for a caller's own exporters. */
681
743
  readonly exporter: Secret<u8[]>
@@ -696,6 +758,11 @@ export class TlsSecretPool {
696
758
  /** The CertificateVerify input, 98 bytes longer than the rest. */
697
759
  toBeSigned: u8[]
698
760
 
761
+ /**
762
+ * Allocates every array of the pool once, all zero, so a connection never allocates one for a secret.
763
+ *
764
+ * @param hashLength The suite's hash length in bytes: 32 for SHA-256, 48 for SHA-384.
765
+ */
699
766
  constructor(hashLength: i32) {
700
767
  this.handshake = secret(new Array<u8>(hashLength))
701
768
  this.clientHandshake = secret(new Array<u8>(hashLength))
@@ -711,7 +778,11 @@ export class TlsSecretPool {
711
778
  this.toBeSigned = new Array<u8>(hashLength + TLS_CERTIFICATE_VERIFY_PREFIX)
712
779
  }
713
780
 
714
- /** Wipes the one secret `index` names (`TLS_SECRET_*`), leaving the rest; any other index wipes nothing. */
781
+ /**
782
+ * Wipes the one secret `index` names (`TLS_SECRET_*`), leaving the rest; any other index wipes nothing.
783
+ *
784
+ * @internal Read by `net/tls/client.ts` and `TlsServer`.
785
+ */
715
786
  zero(index: i32): void {
716
787
  if (index === TLS_SECRET_HANDSHAKE) {
717
788
  exposeWith(this.handshake, TLS_FROM, tlsZeroHeld)
@@ -740,7 +811,11 @@ export class TlsSecretPool {
740
811
  }
741
812
  }
742
813
 
743
- /** Wipes every secret in the pool. */
814
+ /**
815
+ * Wipes every secret in the pool.
816
+ *
817
+ * @internal Read by `net/tls/client.ts` and `TlsServer`.
818
+ */
744
819
  wipe(): void {
745
820
  wipe(this.handshake)
746
821
  wipe(this.clientHandshake)
@@ -865,22 +940,24 @@ const tlsServerTicketCapacity = (state: TlsTicketState): i32 => {
865
940
  *
866
941
  * **What it allocates.** Everything it keeps is allocated by the constructor
867
942
  * and reused by `restart`: the input and output buffers, the transcript's
868
- * hashers, the HKDF scratch, the ephemeral key's 32 bytes, and HashLen bytes for every secret
869
- * under each hash, which each derivation writes over in place and `wipe`
870
- * zeroes before the next connection starts. `receive` and
871
- * `sign` do their work inside `using a = arena()` blocks, so the compiler has
872
- * proved that what they allocate on the way dies before they return. Four
873
- * things grow or are made outside those blocks: the input buffer, doubled up
874
- * to the largest message when a ClientHello does not fit (once per slot);
875
- * the Handshake-level output, when a signature is longer than an ECDSA one;
876
- * the `serverName` string, made when the client's host name differs from the
877
- * last one this server held; and, over QUIC only, the array the client's
878
- * transport parameters are copied into, which grows only when a client's
879
- * 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).
880
956
  */
881
957
  export class TlsServer {
958
+ /** The configuration this server was built with: its certificate chain, ALPN list and QUIC transport parameters. */
882
959
  config: TlsServerConfig
883
- serverRandom: u8[]
960
+ private serverRandom: u8[]
884
961
  /** The connection's x25519 private key, copied in by `restart` and wiped by the record layer once the ServerHello is written. */
885
962
  readonly ephemeral: Secret<u8[]>
886
963
  /** The client's `host_name`, or empty. */
@@ -888,33 +965,42 @@ export class TlsServer {
888
965
  /** The ALPN protocol chosen, one of the configuration's, or empty. */
889
966
  alpn: string = ""
890
967
  /** The last host name a client of this server sent, kept across `restart` so that the next one naming it reuses the string. */
891
- lastServerName: string = ""
968
+ private lastServerName: string = ""
892
969
  /** The client's `quic_transport_parameters`, opaque; empty unless the carrier is QUIC. */
893
970
  clientTransportParameters: u8[]
894
971
  /** The array `clientTransportParameters` points at once read, kept across `restart` and refilled in the room it has. */
895
- clientParameters: u8[]
896
- transcript: TlsTranscript
972
+ private clientParameters: u8[]
973
+ private transcript: TlsTranscript
897
974
  /** The bytes received at the current level and not yet a whole message: `input[0 .. inputLength)`. */
898
975
  input: u8[]
899
- /** What the server has written at the Initial level and not handed on: `outputInitial[0 .. outputInitialLength)`. */
976
+ /**
977
+ * What the server has written at the Initial level and not handed on: `outputInitial[0 .. outputInitialLength)`.
978
+ *
979
+ * @internal Read by `net/tls/record-server.ts` and `net/quic.ts`.
980
+ */
900
981
  outputInitial: u8[]
901
- /** What the server has written at the Handshake level and not handed on: `outputHandshake[0 .. outputHandshakeLength)`. */
982
+ /**
983
+ * What the server has written at the Handshake level and not handed on: `outputHandshake[0 .. outputHandshakeLength)`.
984
+ *
985
+ * @internal Read by `net/tls/record-server.ts` and `net/quic.ts`.
986
+ */
902
987
  outputHandshake: u8[]
903
988
  /** An empty array: what `clientTransportParameters` holds until a client's are read. */
904
- none: u8[]
989
+ private none: u8[]
905
990
  /** The secrets under each hash (`TlsSecretPool`); `secrets()` answers the negotiated hash's. */
906
991
  secrets256: TlsSecretPool
992
+ /** The secrets under SHA-384, for TLS_AES_256_GCM_SHA384. */
907
993
  secrets384: TlsSecretPool
908
994
  /** The ClientHello last read, as windows into `input`. */
909
- hello: TlsClientHelloView
995
+ private hello: TlsClientHelloView
910
996
  /** Where every HKDF and HMAC of the schedule runs. */
911
- kdf: HkdfScratch
997
+ private kdf: HkdfScratch
912
998
  /** The exchange's words (`tlsExchange`). */
913
999
  exchange: TlsExchangeWords
914
1000
  /** The server's x25519 public key, unpacked from the exchange's words for the ServerHello. */
915
- serverPublic: u8[]
1001
+ private serverPublic: u8[]
916
1002
  /** A transcript hash on its way into the schedule: HashLen bytes of it. */
917
- hashes: u8[]
1003
+ private hashes: u8[]
918
1004
  /**
919
1005
  * The salt the handshake secret is extracted under, HashLen bytes of it:
920
1006
  * the `TLS_DERIVED_SALT_*` constant on a full handshake, derived from the
@@ -923,48 +1009,64 @@ export class TlsServer {
923
1009
  */
924
1010
  salt: u8[]
925
1011
  /** The transcript hash through the ClientHello's binders prefix, which a binder covers (§4.2.11.2). */
926
- binderHash: u8[]
1012
+ private binderHash: u8[]
927
1013
  /** What the ticket `resume` is reading opened to; its PSK is zeroed once read. */
928
1014
  opened: TlsTicketState
929
1015
  /** What `issue` seals; its PSK is zeroed once sealed. */
930
1016
  issuing: TlsTicketState
931
1017
  /** The sealed ticket, sized once for the largest. */
932
- ticketScratch: u8[]
933
- /** What the server has written at the Application level and not handed on: one NewSessionTicket. */
1018
+ private ticketScratch: u8[]
1019
+ /**
1020
+ * What the server has written at the Application level and not handed on: one NewSessionTicket.
1021
+ *
1022
+ * @internal Read by `net/tls/record-server.ts`.
1023
+ */
934
1024
  outputApplication: u8[]
935
1025
  /** The ticket nonce: one ticket a connection, so one byte, the same each time, keeps each PSK distinct (§4.6.1). */
936
- ticketNonce: u8[]
1026
+ private ticketNonce: u8[]
937
1027
  /** The ticket's `ticket_age_add`, drawn fresh for each ticket (§4.6.1). */
938
- ageAddBytes: u8[]
1028
+ private ageAddBytes: u8[]
939
1029
  /**
940
1030
  * Which of the negotiated pool's secrets the handshake has derived: bit
941
1031
  * `1 << TLS_SECRET_*` for each. A secret wiped by `wipeSecret` keeps its
942
1032
  * bit, since its place is still the one it was derived into.
943
1033
  */
944
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. */
945
1036
  state: i32 = 0
946
1037
  /** The alert the server failed with, or 0. */
947
1038
  alert: i32 = 0
948
1039
  /** The negotiated cipher suite, or 0 before the first ClientHello is read. */
949
1040
  suite: i32 = 0
1041
+ /** @internal Read by `net/tls/record-server.ts`. */
950
1042
  hashLength: i32 = 0
1043
+ /** @internal Read by `net/tls/record-server.ts`. */
951
1044
  inputLength: i32 = 0
1045
+ /** @internal Read by `net/tls/record-server.ts` and `net/quic.ts`. */
952
1046
  outputInitialLength: i32 = 0
1047
+ /** @internal Read by `net/tls/record-server.ts` and `net/quic.ts`. */
953
1048
  outputHandshakeLength: i32 = 0
1049
+ /** @internal Read by `net/tls/record-server.ts`. */
954
1050
  outputApplicationLength: i32 = 0
955
1051
  /** Whether a HelloRetryRequest was sent, so a second miss is fatal. */
956
1052
  retried: boolean = false
957
1053
  /** Whether `config` passed `tlsConfigFits`, decided once by the constructor. */
958
- configFits: boolean = false
1054
+ private configFits: boolean = false
959
1055
  /** Whether the carrier turned resumption on with `enableTickets`; kept across `restart`. */
960
1056
  tickets: boolean = false
961
1057
  /**
962
1058
  * Whether this connection may be issued a ticket: tickets are on, the
963
1059
  * client listed `psk_dhe_ke`, the only mode this server resumes in, and its
964
1060
  * server name, if any, fits a ticket.
1061
+ *
1062
+ * @internal Read by `net/tls/record-server.ts`.
965
1063
  */
966
1064
  ticketWanted: boolean = false
967
- /** Whether `issue` has run for this connection. */
1065
+ /**
1066
+ * Whether `issue` has run for this connection.
1067
+ *
1068
+ * @internal Read by `net/tls/record-server.ts`.
1069
+ */
968
1070
  issued: boolean = false
969
1071
  /** Whether this connection resumed a ticket (row 1 of the record's table). */
970
1072
  resumed: boolean = false
@@ -974,6 +1076,12 @@ export class TlsServer {
974
1076
  * random and the 32-byte x25519 private key drawn for it. A random or key of
975
1077
  * another length, or a configuration `tlsConfigFits` refuses, leaves the
976
1078
  * server failed with `internal_error` before it reads anything.
1079
+ *
1080
+ * @param config What the server offers and answers with; held, not copied, and checked by `tlsConfigFits`.
1081
+ * @param serverRandom The 32 random bytes of the ServerHello's `random`; the array itself is kept, so the
1082
+ * caller hands over a fresh one.
1083
+ * @param ephemeralPrivate The 32-byte x25519 private key; copied into the server's `Secret` and then zeroed
1084
+ * in the caller's array, as `restart` does.
977
1085
  */
978
1086
  constructor(config: TlsServerConfig, serverRandom: u8[], ephemeralPrivate: u8[]) {
979
1087
  this.config = config
@@ -1014,6 +1122,9 @@ export class TlsServer {
1014
1122
  * only copy left is the one the server wipes. A random or key of another
1015
1123
  * length, or a configuration `tlsConfigFits` refused, leaves the server
1016
1124
  * failed with `internal_error`.
1125
+ *
1126
+ * @param serverRandom The next connection's 32 random bytes; the array itself is kept.
1127
+ * @param ephemeralPrivate The next connection's 32-byte x25519 private key; zeroed once copied in.
1017
1128
  */
1018
1129
  restart(serverRandom: u8[], ephemeralPrivate: u8[]): void {
1019
1130
  const keyLength: i32 = toI32(ephemeralPrivate.length)
@@ -1051,7 +1162,7 @@ export class TlsServer {
1051
1162
  * master secret `sign` derived ahead and what a ticket being read left: its
1052
1163
  * PSK, early secret and binder key, and the salt.
1053
1164
  */
1054
- fail(alert: i32): i32 {
1165
+ private fail(alert: i32): i32 {
1055
1166
  this.wipeResumption()
1056
1167
  const pool: TlsSecretPool = this.secrets()
1057
1168
  pool.zero(TLS_SECRET_PSK)
@@ -1073,12 +1184,23 @@ export class TlsServer {
1073
1184
  this.secrets().zero(TLS_SECRET_RESUMPTION)
1074
1185
  }
1075
1186
 
1076
- /** The pool of the negotiated hash: SHA-384's for a SHA-384 suite, SHA-256's otherwise. */
1187
+ /**
1188
+ * The pool of the negotiated hash: SHA-384's for a SHA-384 suite, SHA-256's otherwise.
1189
+ *
1190
+ * @returns One of the server's two pools, which the server keeps and reuses for the next connection.
1191
+ */
1077
1192
  secrets(): TlsSecretPool {
1078
1193
  return this.hashLength === SHA384_SIZE ? this.secrets384 : this.secrets256
1079
1194
  }
1080
1195
 
1081
- /** Whether the handshake has derived secret `index` (`TLS_SECRET_*`) for this connection. */
1196
+ /**
1197
+ * Whether the handshake has derived secret `index` (`TLS_SECRET_*`) for this connection.
1198
+ *
1199
+ * @param index A `TLS_SECRET_*` index; any index outside 0 to 30 is never known.
1200
+ * @returns True once the secret is derived; it stays true after `wipeSecret`, until `restart`,
1201
+ * except that `sign` clears `TLS_SECRET_TO_BE_SIGNED` and the client's Finished clears
1202
+ * `TLS_SECRET_HANDSHAKE`.
1203
+ */
1082
1204
  known(index: i32): boolean {
1083
1205
  return index >= 0 && index < 31 && (this.held & (1 << index)) !== 0
1084
1206
  }
@@ -1088,6 +1210,9 @@ export class TlsServer {
1088
1210
  * traffic secret, `clientHandshake` or `clientApplication` of `secrets()` —
1089
1211
  * is known yet. The Initial level has none: it is cleartext in TLS, and
1090
1212
  * QUIC derives its own from the connection id.
1213
+ *
1214
+ * @param level A `TLS_LEVEL_*` level.
1215
+ * @returns True when the client's secret for `level` is derived; always false for Initial.
1091
1216
  */
1092
1217
  readKnown(level: i32): boolean {
1093
1218
  if (level === TLS_LEVEL_HANDSHAKE) {
@@ -1096,7 +1221,13 @@ export class TlsServer {
1096
1221
  return level === TLS_LEVEL_APPLICATION && this.known(TLS_SECRET_CLIENT_APPLICATION)
1097
1222
  }
1098
1223
 
1099
- /** Whether the secret the server *writes* `level` under — `serverHandshake` or `serverApplication` — is known yet. */
1224
+ /**
1225
+ * Whether the secret the server *writes* `level` under — `serverHandshake` or `serverApplication` — is
1226
+ * known yet.
1227
+ *
1228
+ * @param level A `TLS_LEVEL_*` level.
1229
+ * @returns True when the server's secret for `level` is derived; always false for Initial.
1230
+ */
1100
1231
  writeKnown(level: i32): boolean {
1101
1232
  if (level === TLS_LEVEL_HANDSHAKE) {
1102
1233
  return this.known(TLS_SECRET_SERVER_HANDSHAKE)
@@ -1104,12 +1235,18 @@ export class TlsServer {
1104
1235
  return level === TLS_LEVEL_APPLICATION && this.known(TLS_SECRET_SERVER_APPLICATION)
1105
1236
  }
1106
1237
 
1107
- /** Zeroes secret `index` of `secrets()` once a carrier has taken what it needs from it; it stays known. */
1238
+ /**
1239
+ * Zeroes secret `index` of `secrets()` once a carrier has taken what it needs from it; it stays known.
1240
+ *
1241
+ * @param index A `TLS_SECRET_*` index; any other index wipes nothing.
1242
+ */
1108
1243
  wipeSecret(index: i32): void {
1109
1244
  this.secrets().zero(index)
1110
1245
  }
1111
1246
 
1112
- /** Zeroes the ephemeral key, which the record layer does once the ServerHello is written: not while a hello that offers a ticket waits for `resume`, whose exchange still needs it. */
1247
+ /**
1248
+ * Zeroes the ephemeral key, which the record layer does once the ServerHello is written: not while a hello that offers a ticket waits for `resume`, whose exchange still needs it.
1249
+ */
1113
1250
  wipeEphemeral(): void {
1114
1251
  exposeWith(this.ephemeral, TLS_FROM, tlsZeroHeld)
1115
1252
  }
@@ -1122,7 +1259,7 @@ export class TlsServer {
1122
1259
  }
1123
1260
 
1124
1261
  /** The level the client's next bytes must arrive at, or -1 when the state expects none. */
1125
- expectedLevel(): i32 {
1262
+ private expectedLevel(): i32 {
1126
1263
  switch (this.state) {
1127
1264
  case TLS_STATE_WAIT_CLIENT_HELLO:
1128
1265
  return TLS_LEVEL_INITIAL
@@ -1139,7 +1276,7 @@ export class TlsServer {
1139
1276
  * clients cut their messages, and never past the largest message `receive`
1140
1277
  * takes.
1141
1278
  */
1142
- reserveInput(need: i32): void {
1279
+ private reserveInput(need: i32): void {
1143
1280
  const capacity: i32 = toI32(this.input.length)
1144
1281
  if (need <= capacity) {
1145
1282
  return
@@ -1162,7 +1299,7 @@ export class TlsServer {
1162
1299
  * writes with an ECDSA signature, so only a longer signature grows the
1163
1300
  * Handshake level's, once per slot.
1164
1301
  */
1165
- reserveOutput(level: i32, need: i32): void {
1302
+ private reserveOutput(level: i32, need: i32): void {
1166
1303
  const initial: boolean = level === TLS_LEVEL_INITIAL
1167
1304
  const buffer: u8[] = initial ? this.outputInitial : this.outputHandshake
1168
1305
  const held: i32 = initial ? this.outputInitialLength : this.outputHandshakeLength
@@ -1194,6 +1331,13 @@ export class TlsServer {
1194
1331
  * fault and not the peer's. Once the handshake is done, any further bytes
1195
1332
  * are `unexpected_message` too: the post-handshake messages a client may
1196
1333
  * send (KeyUpdate) belong to the record layer, which handles them itself.
1334
+ *
1335
+ * @param level The `TLS_LEVEL_*` level the bytes arrived at.
1336
+ * @param data The array holding the bytes; only read, and copied into the server's own buffer.
1337
+ * @param off Where in `data` the bytes start.
1338
+ * @param len How many bytes, from `off`, to take; 0 takes nothing and answers 0, once the window is
1339
+ * checked and unless the server has already failed.
1340
+ * @returns 0, or the `TLS_ALERT_*` the server failed with (the same one again on every later call).
1197
1341
  */
1198
1342
  receive(level: i32, data: u8[], off: i32, len: i32): i32 {
1199
1343
  if (this.state === TLS_STATE_FAILED) {
@@ -1253,7 +1397,7 @@ export class TlsServer {
1253
1397
  * admits bytes only in the two states that expect them, so the state is
1254
1398
  * one of those two here.
1255
1399
  */
1256
- handleMessage(length: i32): i32 {
1400
+ private handleMessage(length: i32): i32 {
1257
1401
  const type: i32 = toI32(this.input[0])
1258
1402
  if (this.state === TLS_STATE_WAIT_CLIENT_HELLO) {
1259
1403
  return type === TLS_HANDSHAKE_CLIENT_HELLO
@@ -1268,7 +1412,7 @@ export class TlsServer {
1268
1412
  * (§4.1.1: the server picks; following the client's order is the choice
1269
1413
  * RFC 8448's server makes).
1270
1414
  */
1271
- chooseSuite(hello: TlsClientHelloView): i32 {
1415
+ private chooseSuite(hello: TlsClientHelloView): i32 {
1272
1416
  const data: u8[] = hello.data
1273
1417
  const end: i32 = hello.suitesAt + hello.suitesLength
1274
1418
  for (
@@ -1289,7 +1433,7 @@ export class TlsServer {
1289
1433
  * Answers 0 with `alpn` set, or empty when nothing is negotiated, or
1290
1434
  * `no_application_protocol`.
1291
1435
  */
1292
- chooseAlpn(hello: TlsClientHelloView): i32 {
1436
+ private chooseAlpn(hello: TlsClientHelloView): i32 {
1293
1437
  this.alpn = ""
1294
1438
  if (hello.hasAlpn && toI32(this.config.alpn.length) > 0) {
1295
1439
  const data: u8[] = hello.data
@@ -1315,7 +1459,7 @@ export class TlsServer {
1315
1459
  * already spells those bytes, so a slot whose clients name one host makes
1316
1460
  * the string once, and made again only when the name changes.
1317
1461
  */
1318
- takeServerName(hello: TlsClientHelloView): void {
1462
+ private takeServerName(hello: TlsClientHelloView): void {
1319
1463
  if (!hello.hasServerName) {
1320
1464
  this.serverName = ""
1321
1465
  return
@@ -1334,7 +1478,7 @@ export class TlsServer {
1334
1478
  * the suite, the extensions §9.2 requires, the group, the signature scheme,
1335
1479
  * the share itself, and the application protocol.
1336
1480
  */
1337
- handleClientHello(length: i32): i32 {
1481
+ private handleClientHello(length: i32): i32 {
1338
1482
  const hello: TlsClientHelloView = this.hello
1339
1483
  const data: u8[] = this.input
1340
1484
  tlsReadClientHello(hello, data, 4, length)
@@ -1495,7 +1639,7 @@ export class TlsServer {
1495
1639
  * does not resume, the checks a certificate needs and the full flight.
1496
1640
  * Answers 0 or the alert.
1497
1641
  */
1498
- answerHello(index: i32): i32 {
1642
+ private answerHello(index: i32): i32 {
1499
1643
  const hello: TlsClientHelloView = this.hello
1500
1644
  if (!this.resumed) {
1501
1645
  if (!hello.hasSignatureAlgorithms) {
@@ -1538,7 +1682,7 @@ export class TlsServer {
1538
1682
  * the negotiated one, its PSK is that long, and it names the server name
1539
1683
  * this hello names, or none when the hello names none (rows 4 and 5).
1540
1684
  */
1541
- ticketFits(): boolean {
1685
+ private ticketFits(): boolean {
1542
1686
  const hello: TlsClientHelloView = this.hello
1543
1687
  const opened: TlsTicketState = this.opened
1544
1688
  const h: i32 = this.hashLength
@@ -1562,7 +1706,7 @@ export class TlsServer {
1562
1706
  * counted with `take`, so that a full register fails closed into a full
1563
1707
  * handshake (row 7).
1564
1708
  */
1565
- selectTicket(
1709
+ private selectTicket(
1566
1710
  current: Secret<u8[]>,
1567
1711
  currentInfo: TlsTicketKeyInfo,
1568
1712
  previous: Secret<u8[]>,
@@ -1617,7 +1761,7 @@ export class TlsServer {
1617
1761
  * hello's binder for it verifies (§4.2.11.2). The PSK and binder key are
1618
1762
  * zeroed before it returns; the early secret stays for `saltFromEarly`.
1619
1763
  */
1620
- bindTicket(): boolean {
1764
+ private bindTicket(): boolean {
1621
1765
  const hello: TlsClientHelloView = this.hello
1622
1766
  const pool: TlsSecretPool = this.secrets()
1623
1767
  const h: i32 = this.hashLength
@@ -1650,7 +1794,7 @@ export class TlsServer {
1650
1794
  }
1651
1795
 
1652
1796
  /** The salt a resumed handshake secret is extracted under, `Derive-Secret(early, "derived", "")`, into `salt`. */
1653
- saltFromEarly(): void {
1797
+ private saltFromEarly(): void {
1654
1798
  const pool: TlsSecretPool = this.secrets()
1655
1799
  using _scope = arena()
1656
1800
  const early: u8[] = expose(pool.early, tlsHeldClone)
@@ -1666,6 +1810,16 @@ export class TlsServer {
1666
1810
  * table of "Resumption" in `docs/security/tls.md` says. A selected ticket
1667
1811
  * whose binder fails is `decrypt_error`, with nothing recorded (row 9).
1668
1812
  * Answers 0, or the alert the server fails with.
1813
+ *
1814
+ * @param current The newest ticket key, lent: read only inside `exposeWith`, and the carrier's to wipe.
1815
+ * @param currentInfo The current key's record.
1816
+ * @param previous The key before it, lent as `current` is.
1817
+ * @param previousInfo The previous key's record.
1818
+ * @param strikes The register a resumed ticket's id is recorded in, so that it resumes only once.
1819
+ * @param now The time to judge the tickets at, in `monotonicNanos()`.
1820
+ * @returns 0 when the hello was answered, resumed or not, or the `TLS_ALERT_*` the server failed with:
1821
+ * the recorded alert in `TLS_STATE_FAILED`, `internal_error` in any other state but
1822
+ * `TLS_STATE_WAIT_TICKET`.
1669
1823
  */
1670
1824
  resume(
1671
1825
  current: Secret<u8[]>,
@@ -1707,7 +1861,7 @@ export class TlsServer {
1707
1861
  * handshake traffic secrets, EncryptedExtensions and the server's
1708
1862
  * Finished, after which the server waits for the client's.
1709
1863
  */
1710
- writeResumedFlight(index: i32): void {
1864
+ private writeResumedFlight(index: i32): void {
1711
1865
  const hello: TlsClientHelloView = this.hello
1712
1866
  const none: u8[] = []
1713
1867
  this.reserveOutput(
@@ -1741,7 +1895,7 @@ export class TlsServer {
1741
1895
  }
1742
1896
 
1743
1897
  /** The resumed ServerHello, the handshake traffic secrets and EncryptedExtensions, in the room `writeResumedFlight` reserved. */
1744
- writeResumedHello(pool: TlsSecretPool, index: i32): void {
1898
+ private writeResumedHello(pool: TlsSecretPool, index: i32): void {
1745
1899
  const hello: TlsClientHelloView = this.hello
1746
1900
  using _scope = arena()
1747
1901
  const helloAt: i32 = this.outputInitialLength
@@ -1786,7 +1940,7 @@ export class TlsServer {
1786
1940
  * `Secret` field a word at a time; the record layer wipes it once the
1787
1941
  * ServerHello is written (TLS-1).
1788
1942
  */
1789
- exchangeKeys(): i32 {
1943
+ private exchangeKeys(): i32 {
1790
1944
  const w: TlsExchangeWords = this.exchange
1791
1945
  const share: u8[] = this.input
1792
1946
  const at: i32 = this.hello.x25519ShareAt
@@ -1825,7 +1979,7 @@ export class TlsServer {
1825
1979
  * EncryptedExtensions and Certificate at the Handshake level, then the
1826
1980
  * CertificateVerify input: all in place, inside one arena block.
1827
1981
  */
1828
- writeServerFlight(): void {
1982
+ private writeServerFlight(): void {
1829
1983
  const hello: TlsClientHelloView = this.hello
1830
1984
  const h: i32 = this.hashLength
1831
1985
  // Room for the messages first, outside the block, measured by writing
@@ -1908,7 +2062,7 @@ export class TlsServer {
1908
2062
  * held secret `dst` (RFC 8446 §7.1): derived into a plain array that dies
1909
2063
  * with the caller's arena block, copied in, and zeroed.
1910
2064
  */
1911
- deriveInto(prk: u8[], label: string, dst: Secret<u8[]>): void {
2065
+ private deriveInto(prk: u8[], label: string, dst: Secret<u8[]>): void {
1912
2066
  const out: u8[] = new Array<u8>(this.hashLength)
1913
2067
  tlsDeriveSecretInto(this.kdf, this.hashLength, prk, label, this.hashes, TLS_FROM, out)
1914
2068
  exposeWith(dst, out, tlsHeldCopyIn)
@@ -1918,6 +2072,9 @@ export class TlsServer {
1918
2072
  /**
1919
2073
  * What CertificateVerify signs (RFC 8446 §4.4.3), while the state is
1920
2074
  * `TLS_STATE_WAIT_SIGNATURE`; `null` at any other time.
2075
+ *
2076
+ * @returns The pool's own `toBeSigned` array, not a copy: 98 bytes and the transcript hash, which the
2077
+ * caller signs and does not change. `null` outside `TLS_STATE_WAIT_SIGNATURE`.
1921
2078
  */
1922
2079
  signatureInput(): u8[] | null {
1923
2080
  if (this.state !== TLS_STATE_WAIT_SIGNATURE) {
@@ -1933,6 +2090,9 @@ export class TlsServer {
1933
2090
  * when called in another state or with an empty or over-long signature.
1934
2091
  * The signature is not checked here: a wrong one is the caller's bug, and
1935
2092
  * the client's verification is what catches it.
2093
+ *
2094
+ * @param signature The signature bytes, 1 to 65,535 of them, such as `tlsSignEcdsaP256` answers.
2095
+ * @returns 0, or the `TLS_ALERT_*` the server failed with.
1936
2096
  */
1937
2097
  sign(signature: u8[]): i32 {
1938
2098
  if (this.state === TLS_STATE_FAILED) {
@@ -1954,7 +2114,7 @@ export class TlsServer {
1954
2114
  }
1955
2115
 
1956
2116
  /** CertificateVerify, carrying `signature`, at the Handshake level, in the room `sign` reserved. */
1957
- writeCertificateVerify(signature: u8[]): void {
2117
+ private writeCertificateVerify(signature: u8[]): void {
1958
2118
  using _scope = arena()
1959
2119
  const verifyAt: i32 = this.outputHandshakeLength
1960
2120
  const verifyEnd: i32 = tlsWriteCertificateVerify(
@@ -1971,7 +2131,7 @@ export class TlsServer {
1971
2131
  * it: its `verify_data`, under the server's handshake traffic secret, goes
1972
2132
  * straight into its place in the flight.
1973
2133
  */
1974
- writeServerFinished(pool: TlsSecretPool): void {
2134
+ private writeServerFinished(pool: TlsSecretPool): void {
1975
2135
  const h: i32 = this.hashLength
1976
2136
  using _scope = arena()
1977
2137
  const at: i32 = this.outputHandshakeLength
@@ -1997,7 +2157,7 @@ export class TlsServer {
1997
2157
  * Finished the server expects, and the resumption master secret, after
1998
2158
  * which the server waits for that Finished.
1999
2159
  */
2000
- masterSecretStep(pool: TlsSecretPool): void {
2160
+ private masterSecretStep(pool: TlsSecretPool): void {
2001
2161
  const h: i32 = this.hashLength
2002
2162
  using _scope = arena()
2003
2163
  this.transcript.hashInto(this.hashes, TLS_FROM)
@@ -2031,17 +2191,23 @@ export class TlsServer {
2031
2191
  }
2032
2192
 
2033
2193
  /**
2034
- * Once the handshake is done, at most once a connection: seals a ticket
2035
- * for it under `key`, which `info` describes, and writes the
2036
- * NewSessionTicket carrying it at the Application level, for the carrier
2037
- * to send under the server's application keys. Answers whether one was
2038
- * written: none is when tickets are off, the client did not list
2039
- * `psk_dhe_ke`, or the key does not seal. On a connected connection's
2040
- * first call the resumption master secret is wiped before it returns,
2041
- * written or not, so a carrier that issues no ticket still calls this, or
2042
- * `wipeResumption`. Before the handshake is done, or on a second call, it
2043
- * answers false and touches nothing: a secret wiped then would leave no
2044
- * 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.
2206
+ *
2207
+ * @param key The current ticket key, lent: read only inside `exposeWith`, and still the carrier's to wipe.
2208
+ * @param info The key's record, which counts the ticket when one is sealed.
2209
+ * @param now The issue time, in `monotonicNanos()`.
2210
+ * @returns True when a NewSessionTicket was written for `takeOutput(TLS_LEVEL_APPLICATION)`.
2045
2211
  */
2046
2212
  issue(key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): boolean {
2047
2213
  if (this.state !== TLS_STATE_CONNECTED || this.issued) {
@@ -2054,7 +2220,7 @@ export class TlsServer {
2054
2220
  }
2055
2221
 
2056
2222
  /** The resumption PSK for nonce `ticketNonce`, into `issuing.psk` (§4.6.1). */
2057
- resumptionPsk(): void {
2223
+ private resumptionPsk(): void {
2058
2224
  const pool: TlsSecretPool = this.secrets()
2059
2225
  using _scope = arena()
2060
2226
  const resumption: u8[] = expose(pool.resumption, tlsHeldClone)
@@ -2071,7 +2237,7 @@ export class TlsServer {
2071
2237
  }
2072
2238
 
2073
2239
  /** `issue`'s ticket: sealed into `ticketScratch`, its PSK zeroed, and the NewSessionTicket written. */
2074
- writeTicket(key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): boolean {
2240
+ private writeTicket(key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): boolean {
2075
2241
  const issuing: TlsTicketState = this.issuing
2076
2242
  issuing.suite = this.suite
2077
2243
  issuing.lifetime = TLS_TICKET_LIFETIME_SECONDS
@@ -2114,7 +2280,7 @@ export class TlsServer {
2114
2280
  * secret `sign` derived is the connection's, and known. The transcript already holds this Finished: `sign` absorbed the
2115
2281
  * one it expected, and these bytes are equal to it.
2116
2282
  */
2117
- handleFinished(length: i32): i32 {
2283
+ private handleFinished(length: i32): i32 {
2118
2284
  if (length !== this.hashLength) {
2119
2285
  return TLS_ALERT_DECODE_ERROR
2120
2286
  }
@@ -2133,7 +2299,7 @@ export class TlsServer {
2133
2299
  * A message the server wrote at `level`, `output[at .. end)` of that
2134
2300
  * level's buffer, goes into the transcript and stays on the output.
2135
2301
  */
2136
- sent(level: i32, at: i32, end: i32): void {
2302
+ private sent(level: i32, at: i32, end: i32): void {
2137
2303
  if (level === TLS_LEVEL_INITIAL) {
2138
2304
  this.transcript.update(this.outputInitial, at, end - at)
2139
2305
  this.outputInitialLength = end
@@ -2148,7 +2314,11 @@ export class TlsServer {
2148
2314
  * array, and forgets them. Bytes are only ever written at a level whose
2149
2315
  * secrets are already known, so a carrier that installs keys as they appear
2150
2316
  * can always protect what this answers. A carrier that reads the output
2151
- * buffers in place calls `clearOutput` instead, and allocates nothing.
2317
+ * buffers in place (the record server and QUIC, inside the standard
2318
+ * library) forgets them with `clearOutput` instead, and allocates nothing.
2319
+ *
2320
+ * @param level The `TLS_LEVEL_*` level to take the bytes of.
2321
+ * @returns The pending bytes, owned by the caller; empty when there are none or `level` is not a level.
2152
2322
  */
2153
2323
  takeOutput(level: i32): u8[] {
2154
2324
  const none: u8[] = []
@@ -2182,7 +2352,11 @@ export class TlsServer {
2182
2352
  }
2183
2353
  }
2184
2354
 
2185
- /** Forgets the handshake bytes written at `level`, which a carrier has read out of the buffer itself. */
2355
+ /**
2356
+ * Forgets the handshake bytes written at `level`, which a carrier has read out of the buffer itself.
2357
+ *
2358
+ * @internal Read by `net/tls/record-server.ts` and `net/quic.ts`.
2359
+ */
2186
2360
  clearOutput(level: i32): void {
2187
2361
  if (level === TLS_LEVEL_INITIAL) {
2188
2362
  this.outputInitialLength = 0