@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
@@ -2,30 +2,48 @@
2
2
  * `nish/net/tls/codec` — the TLS 1.3 handshake messages a server and a
3
3
  * client read and write (RFC 8446 §4), as bytes.
4
4
  *
5
- * One reader and eight writers. `tlsParseClientHello` reads a ClientHello body
6
- * into a `TlsClientHello`, checking its structure: every length against the
7
- * bytes it claims, every vector against its RFC 8446 bounds, and no extension
8
- * twice (§4.2). A message that breaks one answers the alert a server sends for
9
- * it in the hello's `alert` field (`decode_error` for a malformed or truncated
10
- * one, `illegal_parameter` for a duplicate), never a panic, since every byte
11
- * of it is the peer's. What the fields *mean* — which version, which suite,
12
- * whether a share is missing — is the server's to decide, in `nish/net/tls`.
5
+ * import {
6
+ * TLS_VERSION_13, TlsClientHelloView, tlsCopyWindow, tlsEncodeServerHello,
7
+ * tlsReadClientHello, tlsWindowHasU16
8
+ * } from "nish/net/tls/codec";
13
9
  *
14
- * `tlsReadClientHello` is the same reader for a server that keeps its state
15
- * in a slot: it reads into a `TlsClientHelloView` made once, whose fields are
16
- * windows into the message rather than copies of it, so a ClientHello read
17
- * allocates nothing, and `tlsParseClientHello` copies a view's windows out.
10
+ * // `body` is a ClientHello without its four-byte handshake header;
11
+ * // `random`, `suite` and `x25519Public` are the server's own.
12
+ * const hello = new TlsClientHelloView()
13
+ * tlsReadClientHello(hello, body, 0, toI32(body.length))
14
+ * if (hello.alert !== 0) {
15
+ * // send hello.alert, such as TLS_ALERT_DECODE_ERROR (50), and close
16
+ * } else if (tlsWindowHasU16(hello.data, hello.versionsAt, hello.versionsLength, TLS_VERSION_13)) {
17
+ * const sessionId: u8[] = tlsCopyWindow(hello.data, hello.sessionIdAt, hello.sessionIdLength)
18
+ * const reply: u8[] = tlsEncodeServerHello(random, sessionId, suite, x25519Public)
19
+ * }
18
20
  *
19
- * The writers each answer one whole handshake message, header included:
20
- * ServerHello, a resumed ServerHello that names the PSK it accepts,
21
- * HelloRetryRequest, EncryptedExtensions, Certificate, CertificateVerify,
22
- * Finished and NewSessionTicket. Their inputs come from the server and the
23
- * caller's configuration rather than from the peer, so they check nothing.
24
- * Each is a `tlsWrite*` function that writes into the caller's array at an
25
- * offset — run over an empty array, it measures — and a `tlsEncode*` one
26
- * that answers the message in a fresh array.
21
+ * **One reader and eight writers.** `tlsParseClientHello` reads a ClientHello
22
+ * body into a `TlsClientHello`, checking its structure: every length against
23
+ * the bytes it claims, every vector against its RFC 8446 bounds, and no
24
+ * extension twice (§4.2). A message that breaks one answers the alert a server
25
+ * sends for it in the hello's `alert` field (`decode_error` for a malformed or
26
+ * truncated one, `illegal_parameter` for a duplicate), never a panic, since
27
+ * every byte of it is the peer's. What the fields *mean* — which version,
28
+ * which suite, whether a share is missing — is the server's to decide, in
29
+ * `nish/net/tls`.
27
30
  *
28
- * The client's side is the mirror: `tlsWriteClientHello` and
31
+ * **Reading without copying.** `tlsReadClientHello` is the same reader for a
32
+ * server that keeps its state in a slot: it reads into a `TlsClientHelloView`
33
+ * made once, whose fields are windows into the message rather than copies of
34
+ * it, so a ClientHello read allocates nothing, and `tlsParseClientHello`
35
+ * copies a view's windows out.
36
+ *
37
+ * **The writers trust their inputs.** Each answers one whole handshake
38
+ * message, header included: ServerHello, a resumed ServerHello that names the
39
+ * PSK it accepts, HelloRetryRequest, EncryptedExtensions, Certificate,
40
+ * CertificateVerify, Finished and NewSessionTicket. Their inputs come from the
41
+ * server and the caller's configuration rather than from the peer, so they
42
+ * check nothing. Each is a `tlsWrite*` function that writes into the caller's
43
+ * array at an offset — run over an empty array, it measures — and a
44
+ * `tlsEncode*` one that answers the message in a fresh array.
45
+ *
46
+ * **The client's side is the mirror**: `tlsWriteClientHello` and
29
47
  * `tlsEncodeClientHello` write the hello `nish/net/tls/client` sends, and five
30
48
  * readers — `tlsReadServerHello` (a HelloRetryRequest too),
31
49
  * `tlsReadEncryptedExtensions`, `tlsReadCertificate`,
@@ -35,14 +53,15 @@
35
53
  * are the peer's just as a client's are, so these readers never panic either
36
54
  * (the `tls-server-flight` fuzz target).
37
55
  *
38
- * The extensions read are `server_name` (RFC 6066 §3), `supported_groups`,
39
- * `signature_algorithms`, `supported_versions` and `key_share` (RFC 8446
40
- * §4.2), `application_layer_protocol_negotiation` (RFC 7301 §3.1) and
41
- * `quic_transport_parameters` (RFC 9001 §8.2), and `pre_shared_key` and
42
- * `psk_key_exchange_modes` (RFC 8446 §4.2.11, §4.2.9), whose identities and
43
- * binders the reader checks against their syntax but does not open: a ticket
44
- * and its binder are the server's to judge. Every other extension is skipped,
45
- * which is what RFC 8446 §4.2 asks of a server that does not know it.
56
+ * **Extensions.** The ones read are `server_name` (RFC 6066 §3),
57
+ * `supported_groups`, `signature_algorithms`, `supported_versions` and
58
+ * `key_share` (RFC 8446 §4.2), `application_layer_protocol_negotiation`
59
+ * (RFC 7301 §3.1) and `quic_transport_parameters` (RFC 9001 §8.2), and
60
+ * `pre_shared_key` and `psk_key_exchange_modes` (RFC 8446 §4.2.11, §4.2.9),
61
+ * whose identities and binders the reader checks against their syntax but
62
+ * does not open: a ticket and its binder are the server's to judge. Every
63
+ * other extension is skipped, which is what RFC 8446 §4.2 asks of a server
64
+ * that does not know it.
46
65
  *
47
66
  * Written from the RFCs, not ported from another implementation. Private
48
67
  * names carry the `tls` prefix because a `std/` module's private functions
@@ -52,18 +71,29 @@
52
71
 
53
72
  // ---- Handshake message types (RFC 8446 §4) --------------------------------
54
73
 
74
+ /**
75
+ * `client_hello` (§4.1.2): the first message of a handshake, and the client's second after a
76
+ * HelloRetryRequest.
77
+ */
55
78
  export const TLS_HANDSHAKE_CLIENT_HELLO: i32 = 1
79
+ /** `server_hello` (§4.1.3), which a HelloRetryRequest shares, told apart by its `random`. */
56
80
  export const TLS_HANDSHAKE_SERVER_HELLO: i32 = 2
81
+ /** `new_session_ticket` (§4.6.1): a ticket the server sends after the handshake, for resumption. */
57
82
  export const TLS_HANDSHAKE_NEW_SESSION_TICKET: i32 = 4
83
+ /** `encrypted_extensions` (§4.3.1): the server's extensions that do not belong in its ServerHello. */
58
84
  export const TLS_HANDSHAKE_ENCRYPTED_EXTENSIONS: i32 = 8
85
+ /** `certificate` (§4.4.2): the server's chain, leaf first. */
59
86
  export const TLS_HANDSHAKE_CERTIFICATE: i32 = 11
87
+ /** `certificate_verify` (§4.4.3): a signature over the transcript under the leaf's key. */
60
88
  export const TLS_HANDSHAKE_CERTIFICATE_VERIFY: i32 = 15
89
+ /** `finished` (§4.4.4): the MAC over the transcript that closes a side's flight. */
61
90
  export const TLS_HANDSHAKE_FINISHED: i32 = 20
62
91
  /** The synthetic message a HelloRetryRequest puts in the transcript in place of the first ClientHello (§4.4.1). */
63
92
  export const TLS_HANDSHAKE_MESSAGE_HASH: i32 = 254
64
93
 
65
94
  // ---- Extension types (RFC 8446 §4.2, RFC 7301, RFC 9001) ------------------
66
95
 
96
+ /** `server_name` (RFC 6066 §3): the host name the client asks for, or the server's empty acknowledgement. */
67
97
  export const TLS_EXT_SERVER_NAME: i32 = 0
68
98
  /** `pre_shared_key` (§4.2.11): the client's identities and binders, which must be the last extension. */
69
99
  export const TLS_EXT_PRE_SHARED_KEY: i32 = 41
@@ -71,11 +101,17 @@ export const TLS_EXT_PRE_SHARED_KEY: i32 = 41
71
101
  export const TLS_EXT_EARLY_DATA: i32 = 42
72
102
  /** `psk_key_exchange_modes` (§4.2.9): which modes the client will resume in. */
73
103
  export const TLS_EXT_PSK_KEY_EXCHANGE_MODES: i32 = 45
104
+ /** `supported_groups` (§4.2.7): the key-exchange groups the client offers. */
74
105
  export const TLS_EXT_SUPPORTED_GROUPS: i32 = 10
106
+ /** `signature_algorithms` (§4.2.3): the schemes a CertificateVerify may be signed with. */
75
107
  export const TLS_EXT_SIGNATURE_ALGORITHMS: i32 = 13
108
+ /** `application_layer_protocol_negotiation` (RFC 7301 §3.1): the protocols offered, or the one chosen. */
76
109
  export const TLS_EXT_ALPN: i32 = 16
110
+ /** `supported_versions` (§4.2.1): where TLS 1.3 is offered and chosen, since `legacy_version` stays 1.2. */
77
111
  export const TLS_EXT_SUPPORTED_VERSIONS: i32 = 43
112
+ /** `key_share` (§4.2.8): the (EC)DHE shares, or a HelloRetryRequest's `selected_group`. */
78
113
  export const TLS_EXT_KEY_SHARE: i32 = 51
114
+ /** `quic_transport_parameters` (RFC 9001 §8.2): QUIC's own parameters, opaque to TLS. */
79
115
  export const TLS_EXT_QUIC_TRANSPORT_PARAMETERS: i32 = 57
80
116
  /** `cookie` (§4.2.2): what a HelloRetryRequest may carry and the client's second ClientHello echoes. */
81
117
  export const TLS_EXT_COOKIE: i32 = 44
@@ -154,9 +190,13 @@ const TLS_CODEC_FROM: i32 = 0
154
190
  * reader was opened on.
155
191
  */
156
192
  class TlsReader {
193
+ /** @internal Read by the free functions of this module. */
157
194
  data: u8[]
195
+ /** @internal Read by the free functions of this module. */
158
196
  at: i32
197
+ /** @internal Read by the free functions of this module. */
159
198
  end: i32
199
+ /** @internal Read by the free functions of this module. */
160
200
  failed: boolean = false
161
201
 
162
202
  constructor(data: u8[], at: i32, end: i32) {
@@ -233,13 +273,29 @@ const tlsReadSkipTo = (r: TlsReader, to: i32): void => {
233
273
  * in a slot reads its ClientHellos without moving the arena.
234
274
  */
235
275
  export class TlsClientHelloView {
236
- /** The buffer the windows point into: the one the last hello was read from. */
276
+ /**
277
+ * The buffer the windows point into: the one the last hello was read from.
278
+ *
279
+ * @internal Read by `net/tls.ts`.
280
+ */
237
281
  data: u8[]
238
- /** One bit per extension type read, for the duplicate check (§4.2); cleared by each read. */
282
+ /**
283
+ * One bit per extension type read, for the duplicate check (§4.2); cleared by each read.
284
+ *
285
+ * @internal Read by the free functions of this module.
286
+ */
239
287
  extensionSeen: u32[]
240
- /** One bit per group a share was offered for, for the duplicate check (§4.2.8). */
288
+ /**
289
+ * One bit per group a share was offered for, for the duplicate check (§4.2.8).
290
+ *
291
+ * @internal Read by the free functions of this module.
292
+ */
241
293
  shareSeen: u32[]
242
- /** The reader each read runs, kept so that a read allocates none. */
294
+ /**
295
+ * The reader each read runs, kept so that a read allocates none.
296
+ *
297
+ * @internal Read by the free functions of this module.
298
+ */
243
299
  reader: TlsReader
244
300
  /** 0 when the message parsed, or the alert it earns: `decode_error` or `illegal_parameter`. */
245
301
  alert: i32 = 0
@@ -317,6 +373,10 @@ export class TlsClientHelloView {
317
373
  hasPreSharedKey: boolean = false
318
374
  hasPskModes: boolean = false
319
375
 
376
+ /**
377
+ * Makes an empty view, every window empty and every flag false, for
378
+ * `tlsReadClientHello` to fill.
379
+ */
320
380
  constructor() {
321
381
  this.data = []
322
382
  this.extensionSeen = new Array<u32>(TLS_U16_BITMAP_WORDS)
@@ -324,7 +384,11 @@ export class TlsClientHelloView {
324
384
  this.reader = new TlsReader(this.data, TLS_CODEC_FROM, TLS_CODEC_FROM)
325
385
  }
326
386
 
327
- /** Forgets the last hello: every window empty, every flag down, both bitmaps clear. */
387
+ /**
388
+ * Forgets the last hello: every window empty, every flag down, both bitmaps clear.
389
+ *
390
+ * @internal Read by the free functions of this module.
391
+ */
328
392
  clear(): void {
329
393
  this.alert = 0
330
394
  this.legacyVersion = 0
@@ -423,10 +487,27 @@ const tlsReadU16Vector = (r: TlsReader, width: i32, min: i32, max: i32): i32 =>
423
487
  return end
424
488
  }
425
489
 
426
- /** Whether `list` holds `value`. */
490
+ /**
491
+ * Whether `list` holds `value`.
492
+ *
493
+ * @param list The values to look in, such as a `TlsClientHello`'s `cipherSuites` or `groups`.
494
+ * @param value The code point to look for, such as a suite or a group.
495
+ * @returns true when `value` is somewhere in `list`.
496
+ */
427
497
  export const tlsListHas = (list: i32[], value: i32): boolean => list.indexOf(value) >= 0
428
498
 
429
- /** Whether the 16-bit values `data[at .. at + length)` hold `value`. */
499
+ /**
500
+ * Whether the 16-bit values `data[at .. at + length)` hold `value`: how a
501
+ * server asks a `TlsClientHelloView`'s windows what it offers without copying
502
+ * them. The values are big-endian pairs; a last odd byte, and any part of the
503
+ * window outside `data`, is not read.
504
+ *
505
+ * @param data The buffer the window points into, such as a view's `data`.
506
+ * @param at Where the window starts in `data`, in bytes.
507
+ * @param length How many bytes the window holds, two a value.
508
+ * @param value The 16-bit code point to look for, such as `TLS_VERSION_13`.
509
+ * @returns true when one of the window's values is `value`.
510
+ */
430
511
  export const tlsWindowHasU16 = (data: u8[], at: i32, length: i32, value: i32): boolean => {
431
512
  for (
432
513
  let k: i32 = at;
@@ -640,6 +721,11 @@ const tlsReadExtension = (r: TlsReader, type: i32, end: i32, hello: TlsClientHel
640
721
  * `psk_key_exchange_modes` is `missing_extension` (§4.2.9): rows 10 to 13 of
641
722
  * the fallback table in `docs/security/tls.md`, "Resumption". Nothing is copied: the windows
642
723
  * point into `data`, which must outlive the use made of them.
724
+ *
725
+ * @param hello The view to read into, cleared first; its `alert` is the verdict.
726
+ * @param data The buffer that holds the message, which the view's windows then point into.
727
+ * @param off Where the body starts in `data`, in bytes, just past the handshake header.
728
+ * @param len How many bytes the body is, from `off`.
643
729
  */
644
730
  export const tlsReadClientHello = (hello: TlsClientHelloView, data: u8[], off: i32, len: i32): void => {
645
731
  hello.clear()
@@ -718,6 +804,10 @@ export const tlsReadClientHello = (hello: TlsClientHelloView, data: u8[], off: i
718
804
  * answers whether there is one: false for an index out of range, or a hello
719
805
  * that offered no `pre_shared_key` or did not parse. It walks the lists
720
806
  * `tlsReadClientHello` checked, and allocates nothing.
807
+ *
808
+ * @param hello A view `tlsReadClientHello` has read into; its PSK windows are cleared first either way.
809
+ * @param index Which identity to choose, from 0, in the client's order.
810
+ * @returns true when the identity and its binder were found and the windows point at them.
721
811
  */
722
812
  export const tlsSelectPskIdentity = (hello: TlsClientHelloView, index: i32): boolean => {
723
813
  hello.pskIdentityAt = 0
@@ -827,6 +917,10 @@ export class TlsClientHello {
827
917
  /** Whether `psk_key_exchange_modes` lists `psk_dhe_ke`. */
828
918
  pskDheKe: boolean = false
829
919
 
920
+ /**
921
+ * Makes an empty hello, every field empty and every flag false, for
922
+ * `tlsParseClientHello` to fill.
923
+ */
830
924
  constructor() {
831
925
  this.random = []
832
926
  this.sessionId = []
@@ -846,7 +940,15 @@ export class TlsClientHello {
846
940
  }
847
941
  }
848
942
 
849
- /** A copy of `data[at .. at + length)`. */
943
+ /**
944
+ * A copy of `data[at .. at + length)`, which is how a window of a view becomes
945
+ * an array of its own that outlives the buffer it was read from.
946
+ *
947
+ * @param data The buffer the window points into.
948
+ * @param at Where the window starts in `data`, in bytes.
949
+ * @param length How many bytes to copy; a negative one copies none.
950
+ * @returns A fresh array of `length` bytes; nothing from outside `data` is copied into it.
951
+ */
850
952
  export const tlsCopyWindow = (data: u8[], at: i32, length: i32): u8[] => {
851
953
  const out: u8[] = new Array<u8>(length < 0 ? TLS_CODEC_FROM : length)
852
954
  for (let k: i32 = 0; k < toI32(out.length) && at + k >= 0 && at + k < toI32(data.length); k++) {
@@ -868,7 +970,15 @@ const tlsCopyU16Window = (data: u8[], at: i32, length: i32): i32[] => {
868
970
  return out
869
971
  }
870
972
 
871
- /** The bytes of `data[at .. at + length)` as a string of one character a byte: ASCII here, a host name. */
973
+ /**
974
+ * The bytes of `data[at .. at + length)` as a string of one character a byte:
975
+ * ASCII here, a host name.
976
+ *
977
+ * @param data The buffer the window points into.
978
+ * @param at Where the window starts in `data`, in bytes.
979
+ * @param length How many bytes the window holds; any part outside `data` is left out.
980
+ * @returns A fresh string, one character for each byte read.
981
+ */
872
982
  export const tlsWindowString = (data: u8[], at: i32, length: i32): string => {
873
983
  const parts: string[] = []
874
984
  for (let k: i32 = at; k < at + length && k >= 0 && k < toI32(data.length); k++) {
@@ -881,6 +991,11 @@ export const tlsWindowString = (data: u8[], at: i32, length: i32): string => {
881
991
  * Reads the ClientHello body `data[off .. off + len)` — the message without
882
992
  * its four-byte handshake header — into a fresh `TlsClientHello`, with
883
993
  * `tlsReadClientHello`'s checks and its `alert`, every field copied out.
994
+ *
995
+ * @param data The buffer that holds the message; nothing in the answer points into it.
996
+ * @param off Where the body starts in `data`, in bytes, just past the handshake header.
997
+ * @param len How many bytes the body is, from `off`.
998
+ * @returns A fresh hello whose `alert` is 0 when the body parsed, or the alert it earns.
884
999
  */
885
1000
  export const tlsParseClientHello = (data: u8[], off: i32, len: i32): TlsClientHello => {
886
1001
  const view = new TlsClientHelloView()
@@ -1036,6 +1151,11 @@ const tlsHexNibble = (c: i32): i32 => (c <= 57 ? c - 48 : c - 87)
1036
1151
  * Writes the bytes the lowercase hex constant `text` spells at `out[at]` and
1037
1152
  * answers where they end: how this stack's fixed values, written out where
1038
1153
  * they are defined, become bytes without an array of their own.
1154
+ *
1155
+ * @param text Lowercase hex digits, two a byte; a last odd digit is ignored, and nothing else is checked.
1156
+ * @param out The array to write into; a byte that would land outside it is dropped.
1157
+ * @param at Where the first byte goes in `out`.
1158
+ * @returns `at` plus the number of bytes `text` spells, written or not.
1039
1159
  */
1040
1160
  export const tlsHexInto = (text: string, out: u8[], at: i32): i32 => {
1041
1161
  const length: i32 = toI32(text.length) >> 1
@@ -1116,6 +1236,16 @@ const tlsPutServerHello = (
1116
1236
  * `sessionId[sessionIdOff .. sessionIdOff + sessionIdLength)`, and carrying
1117
1237
  * the server's x25519 share. The extensions are `key_share` then
1118
1238
  * `supported_versions`, the order RFC 8448 §3 shows.
1239
+ *
1240
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1241
+ * @param at Where the message, handshake header first, starts in `out`.
1242
+ * @param random The server's 32-byte `random` (§4.1.3), written as it is.
1243
+ * @param sessionId The buffer that holds the client's `legacy_session_id`, such as the hello's `data`.
1244
+ * @param sessionIdOff Where the session id starts in `sessionId`.
1245
+ * @param sessionIdLength How many bytes the session id is, 0 to 32.
1246
+ * @param suite The cipher suite the server chose, such as `nish/net/tls/schedule`'s `TLS_AES_128_GCM_SHA256`.
1247
+ * @param x25519Public The server's x25519 public key, the share's `key_exchange`.
1248
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1119
1249
  */
1120
1250
  export const tlsWriteServerHello = (
1121
1251
  out: u8[],
@@ -1135,6 +1265,18 @@ export const tlsWriteServerHello = (
1135
1265
  * `pre_shared_key` that names the client's identity number
1136
1266
  * `selectedIdentity` (a `uint16`) ahead of `key_share`, since the server
1137
1267
  * resumes only in `psk_dhe_ke` and so always sends its share.
1268
+ *
1269
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1270
+ * @param at Where the message, handshake header first, starts in `out`.
1271
+ * @param random The server's 32-byte `random` (§4.1.3), written as it is.
1272
+ * @param sessionId The buffer that holds the client's `legacy_session_id`, such as the hello's `data`.
1273
+ * @param sessionIdOff Where the session id starts in `sessionId`.
1274
+ * @param sessionIdLength How many bytes the session id is, 0 to 32.
1275
+ * @param suite The cipher suite the server chose.
1276
+ * @param x25519Public The server's x25519 public key, the share's `key_exchange`.
1277
+ * @param selectedIdentity The index, from 0, of the client's PSK identity the server accepts; only its low
1278
+ * 16 bits are written.
1279
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1138
1280
  */
1139
1281
  export const tlsWriteServerHelloPsk = (
1140
1282
  out: u8[],
@@ -1164,6 +1306,15 @@ export const tlsWriteServerHelloPsk = (
1164
1306
  * a ServerHello whose `random` is the fixed HelloRetryRequest value, choosing
1165
1307
  * `suite`, echoing the session id, and asking for a share of `group` in
1166
1308
  * `key_share`. No cookie is sent.
1309
+ *
1310
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1311
+ * @param at Where the message, handshake header first, starts in `out`.
1312
+ * @param sessionId The buffer that holds the client's `legacy_session_id`, such as the hello's `data`.
1313
+ * @param sessionIdOff Where the session id starts in `sessionId`.
1314
+ * @param sessionIdLength How many bytes the session id is, 0 to 32.
1315
+ * @param suite The cipher suite the server chose, which the second ServerHello must choose again.
1316
+ * @param group The group the client is asked to send a share for, such as `TLS_GROUP_X25519`.
1317
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1167
1318
  */
1168
1319
  export const tlsWriteHelloRetryRequest = (
1169
1320
  out: u8[],
@@ -1193,6 +1344,14 @@ export const tlsWriteHelloRetryRequest = (
1193
1344
  * a list of exactly one), an empty `server_name` when `acknowledgeServerName`
1194
1345
  * (RFC 6066 §3's acknowledgement), and the server's
1195
1346
  * `quic_transport_parameters` when `quicTransportParameters` is not `null`.
1347
+ *
1348
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1349
+ * @param at Where the message, handshake header first, starts in `out`.
1350
+ * @param extra Whole extensions, type and length included, written first and unchecked; empty for none.
1351
+ * @param alpn The protocol the server chose, one character a byte, at most 255; empty to send no ALPN.
1352
+ * @param acknowledgeServerName Whether to send the empty `server_name` that says the server used the name.
1353
+ * @param quicTransportParameters The server's QUIC transport parameters, opaque; `null` to send none.
1354
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1196
1355
  */
1197
1356
  export const tlsWriteEncryptedExtensions = (
1198
1357
  out: u8[],
@@ -1232,6 +1391,11 @@ export const tlsWriteEncryptedExtensions = (
1232
1391
  * Writes a server's Certificate (§4.4.2) at `out[at]` and answers where it
1233
1392
  * ends: an empty request context, then each DER certificate of `chain`, leaf
1234
1393
  * first, with no extensions of its own.
1394
+ *
1395
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1396
+ * @param at Where the message, handshake header first, starts in `out`.
1397
+ * @param chain The DER certificates, leaf first, each written as it is.
1398
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1235
1399
  */
1236
1400
  export const tlsWriteCertificate = (out: u8[], at: i32, chain: u8[][]): i32 => {
1237
1401
  const list: i32 = tlsPutU8(out, at + 4, 0) + 3
@@ -1247,7 +1411,16 @@ export const tlsWriteCertificate = (out: u8[], at: i32, chain: u8[][]): i32 => {
1247
1411
  return p
1248
1412
  }
1249
1413
 
1250
- /** Writes a CertificateVerify (§4.4.3) at `out[at]`: the signature scheme and the signature as it goes on the wire. */
1414
+ /**
1415
+ * Writes a CertificateVerify (§4.4.3) at `out[at]`: the signature scheme and
1416
+ * the signature as it goes on the wire.
1417
+ *
1418
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1419
+ * @param at Where the message, handshake header first, starts in `out`.
1420
+ * @param scheme The signature scheme, such as `TLS_SIGNATURE_ECDSA_SECP256R1_SHA256`.
1421
+ * @param signature The signature over `tlsCertificateVerifyContent`, in the scheme's wire encoding.
1422
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1423
+ */
1251
1424
  export const tlsWriteCertificateVerify = (out: u8[], at: i32, scheme: i32, signature: u8[]): i32 => {
1252
1425
  const body: i32 = tlsPutU16(out, at + 4, scheme) + 2
1253
1426
  const p: i32 = tlsPutBytes(out, body, signature)
@@ -1256,7 +1429,16 @@ export const tlsWriteCertificateVerify = (out: u8[], at: i32, scheme: i32, signa
1256
1429
  return p
1257
1430
  }
1258
1431
 
1259
- /** Writes a Finished (§4.4.4) at `out[at]`, carrying `verifyData[off .. off + len)`. */
1432
+ /**
1433
+ * Writes a Finished (§4.4.4) at `out[at]`, carrying `verifyData[off .. off + len)`.
1434
+ *
1435
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1436
+ * @param at Where the message, handshake header first, starts in `out`.
1437
+ * @param verifyData The buffer that holds the `verify_data`, the HMAC the key schedule computed.
1438
+ * @param off Where the `verify_data` starts in `verifyData`.
1439
+ * @param len How many bytes it is: the suite's hash length, 32 or 48.
1440
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1441
+ */
1260
1442
  export const tlsWriteFinished = (out: u8[], at: i32, verifyData: u8[], off: i32, len: i32): i32 => {
1261
1443
  const p: i32 = tlsPutWindow(out, at + 4, verifyData, off, len)
1262
1444
  tlsPutHeader(out, at, TLS_HANDSHAKE_FINISHED, p)
@@ -1270,6 +1452,19 @@ export const tlsWriteFinished = (out: u8[], at: i32, verifyData: u8[], off: i32,
1270
1452
  * `ticket[ticketOff .. ticketOff + ticketLength)`, and `extra` as the
1271
1453
  * extensions, encoded by the caller and written as they are. The server sends
1272
1454
  * none; RFC 8448 §3's ticket carries `early_data`.
1455
+ *
1456
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1457
+ * @param at Where the message, handshake header first, starts in `out`.
1458
+ * @param lifetime How long the ticket may be used, in seconds; RFC 8446 §4.6.1 caps it at seven days.
1459
+ * @param ageAdd The `ticket_age_add` that obfuscates the ticket age, which §4.6.1 wants fresh and random.
1460
+ * @param nonce The buffer that holds the `ticket_nonce`.
1461
+ * @param nonceOff Where the nonce starts in `nonce`.
1462
+ * @param nonceLength How many bytes the nonce is, 0 to 255.
1463
+ * @param ticket The buffer that holds the ticket the client will send back.
1464
+ * @param ticketOff Where the ticket starts in `ticket`.
1465
+ * @param ticketLength How many bytes the ticket is: §4.6.1 allows 1 to 65535, not checked here.
1466
+ * @param extra Whole extensions, type and length included, written unchecked; empty for none.
1467
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1273
1468
  */
1274
1469
  export const tlsWriteNewSessionTicket = (
1275
1470
  out: u8[],
@@ -1301,6 +1496,13 @@ export const tlsWriteNewSessionTicket = (
1301
1496
  * spaces, the context string "TLS 1.3, server CertificateVerify", a zero byte,
1302
1497
  * and the transcript hash `transcriptHash[off .. off + len)` through
1303
1498
  * Certificate.
1499
+ *
1500
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1501
+ * @param at Where the content starts in `out`. It has no handshake header: it is signed, not sent.
1502
+ * @param transcriptHash The buffer that holds the transcript hash through the server's Certificate.
1503
+ * @param off Where the hash starts in `transcriptHash`.
1504
+ * @param len How many bytes the hash is: the suite's hash length.
1505
+ * @returns Where the content ends in `out`: `at` plus 98 plus `len`, written or not.
1304
1506
  */
1305
1507
  export const tlsWriteCertificateVerifyContent = (
1306
1508
  out: u8[],
@@ -1325,6 +1527,12 @@ const tlsBytesString = (name: u8[]): string => tlsWindowString(name, TLS_CODEC_F
1325
1527
  * A ServerHello (§4.1.3) choosing `suite` and TLS 1.3, echoing the client's
1326
1528
  * `sessionId`, and carrying the server's x25519 share, in a fresh array
1327
1529
  * (`tlsWriteServerHello`).
1530
+ *
1531
+ * @param random The server's 32-byte `random` (§4.1.3), written as it is.
1532
+ * @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
1533
+ * @param suite The cipher suite the server chose.
1534
+ * @param x25519Public The server's x25519 public key, the share's `key_exchange`.
1535
+ * @returns The whole message, handshake header included.
1328
1536
  */
1329
1537
  export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32, x25519Public: u8[]): u8[] => {
1330
1538
  const none: u8[] = []
@@ -1354,7 +1562,18 @@ export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32,
1354
1562
  return out
1355
1563
  }
1356
1564
 
1357
- /** A resumed handshake's ServerHello naming `selectedIdentity`, in a fresh array (`tlsWriteServerHelloPsk`). */
1565
+ /**
1566
+ * A resumed handshake's ServerHello naming `selectedIdentity`, in a fresh
1567
+ * array (`tlsWriteServerHelloPsk`).
1568
+ *
1569
+ * @param random The server's 32-byte `random` (§4.1.3), written as it is.
1570
+ * @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
1571
+ * @param suite The cipher suite the server chose.
1572
+ * @param x25519Public The server's x25519 public key, the share's `key_exchange`.
1573
+ * @param selectedIdentity The index, from 0, of the client's PSK identity the server accepts; only its low
1574
+ * 16 bits are written.
1575
+ * @returns The whole message, handshake header included.
1576
+ */
1358
1577
  export const tlsEncodeServerHelloPsk = (
1359
1578
  random: u8[],
1360
1579
  sessionId: u8[],
@@ -1391,7 +1610,15 @@ export const tlsEncodeServerHelloPsk = (
1391
1610
  return out
1392
1611
  }
1393
1612
 
1394
- /** A HelloRetryRequest (§4.1.4) choosing `suite`, echoing `sessionId` and asking for `group`, in a fresh array (`tlsWriteHelloRetryRequest`). */
1613
+ /**
1614
+ * A HelloRetryRequest (§4.1.4) choosing `suite`, echoing `sessionId` and
1615
+ * asking for `group`, in a fresh array (`tlsWriteHelloRetryRequest`).
1616
+ *
1617
+ * @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
1618
+ * @param suite The cipher suite the server chose, which the second ServerHello must choose again.
1619
+ * @param group The group the client is asked to send a share for, such as `TLS_GROUP_X25519`.
1620
+ * @returns The whole message, handshake header included.
1621
+ */
1395
1622
  export const tlsEncodeHelloRetryRequest = (sessionId: u8[], suite: i32, group: i32): u8[] => {
1396
1623
  const none: u8[] = []
1397
1624
  const sessionIdLength: i32 = toI32(sessionId.length)
@@ -1405,6 +1632,12 @@ export const tlsEncodeHelloRetryRequest = (sessionId: u8[], suite: i32, group: i
1405
1632
  /**
1406
1633
  * EncryptedExtensions (§4.3.1) in a fresh array (`tlsWriteEncryptedExtensions`),
1407
1634
  * the ALPN protocol given as its bytes.
1635
+ *
1636
+ * @param extra Whole extensions, type and length included, written first and unchecked; empty for none.
1637
+ * @param alpn The protocol the server chose, as its bytes, at most 255; empty to send no ALPN.
1638
+ * @param acknowledgeServerName Whether to send the empty `server_name` that says the server used the name.
1639
+ * @param quicTransportParameters The server's QUIC transport parameters, opaque; `null` to send none.
1640
+ * @returns The whole message, handshake header included.
1408
1641
  */
1409
1642
  export const tlsEncodeEncryptedExtensions = (
1410
1643
  extra: u8[],
@@ -1435,7 +1668,12 @@ export const tlsEncodeEncryptedExtensions = (
1435
1668
  return out
1436
1669
  }
1437
1670
 
1438
- /** A server's Certificate (§4.4.2) in a fresh array (`tlsWriteCertificate`). */
1671
+ /**
1672
+ * A server's Certificate (§4.4.2) in a fresh array (`tlsWriteCertificate`).
1673
+ *
1674
+ * @param chain The DER certificates, leaf first, each written as it is.
1675
+ * @returns The whole message, handshake header included.
1676
+ */
1439
1677
  export const tlsEncodeCertificate = (chain: u8[][]): u8[] => {
1440
1678
  const none: u8[] = []
1441
1679
  const out: u8[] = new Array<u8>(tlsWriteCertificate(none, TLS_CODEC_FROM, chain))
@@ -1443,7 +1681,13 @@ export const tlsEncodeCertificate = (chain: u8[][]): u8[] => {
1443
1681
  return out
1444
1682
  }
1445
1683
 
1446
- /** A CertificateVerify (§4.4.3) in a fresh array (`tlsWriteCertificateVerify`). */
1684
+ /**
1685
+ * A CertificateVerify (§4.4.3) in a fresh array (`tlsWriteCertificateVerify`).
1686
+ *
1687
+ * @param scheme The signature scheme, such as `TLS_SIGNATURE_ECDSA_SECP256R1_SHA256`.
1688
+ * @param signature The signature over `tlsCertificateVerifyContent`, in the scheme's wire encoding.
1689
+ * @returns The whole message, handshake header included.
1690
+ */
1447
1691
  export const tlsEncodeCertificateVerify = (scheme: i32, signature: u8[]): u8[] => {
1448
1692
  const none: u8[] = []
1449
1693
  const out: u8[] = new Array<u8>(tlsWriteCertificateVerify(none, TLS_CODEC_FROM, scheme, signature))
@@ -1451,7 +1695,12 @@ export const tlsEncodeCertificateVerify = (scheme: i32, signature: u8[]): u8[] =
1451
1695
  return out
1452
1696
  }
1453
1697
 
1454
- /** A Finished (§4.4.4) carrying `verifyData`, in a fresh array (`tlsWriteFinished`). */
1698
+ /**
1699
+ * A Finished (§4.4.4) carrying `verifyData`, in a fresh array (`tlsWriteFinished`).
1700
+ *
1701
+ * @param verifyData The whole `verify_data`, the HMAC the key schedule computed: 32 or 48 bytes.
1702
+ * @returns The whole message, handshake header included.
1703
+ */
1455
1704
  export const tlsEncodeFinished = (verifyData: u8[]): u8[] => {
1456
1705
  const none: u8[] = []
1457
1706
  const length: i32 = toI32(verifyData.length)
@@ -1460,7 +1709,16 @@ export const tlsEncodeFinished = (verifyData: u8[]): u8[] => {
1460
1709
  return out
1461
1710
  }
1462
1711
 
1463
- /** A NewSessionTicket (§4.6.1) in a fresh array (`tlsWriteNewSessionTicket`). */
1712
+ /**
1713
+ * A NewSessionTicket (§4.6.1) in a fresh array (`tlsWriteNewSessionTicket`).
1714
+ *
1715
+ * @param lifetime How long the ticket may be used, in seconds; RFC 8446 §4.6.1 caps it at seven days.
1716
+ * @param ageAdd The `ticket_age_add` that obfuscates the ticket age, which §4.6.1 wants fresh and random.
1717
+ * @param nonce The whole `ticket_nonce`, 0 to 255 bytes.
1718
+ * @param ticket The whole ticket the client will send back: §4.6.1 allows 1 to 65535 bytes, not checked here.
1719
+ * @param extra Whole extensions, type and length included, written unchecked; empty for none.
1720
+ * @returns The whole message, handshake header included.
1721
+ */
1464
1722
  export const tlsEncodeNewSessionTicket = (
1465
1723
  lifetime: u32,
1466
1724
  ageAdd: u32,
@@ -1502,7 +1760,13 @@ export const tlsEncodeNewSessionTicket = (
1502
1760
  return out
1503
1761
  }
1504
1762
 
1505
- /** What a server's CertificateVerify signs (§4.4.3), in a fresh array (`tlsWriteCertificateVerifyContent`). */
1763
+ /**
1764
+ * What a server's CertificateVerify signs (§4.4.3), in a fresh array
1765
+ * (`tlsWriteCertificateVerifyContent`).
1766
+ *
1767
+ * @param transcriptHash The whole transcript hash through the server's Certificate.
1768
+ * @returns The content to sign: 98 bytes of prefix, then the hash. It has no handshake header.
1769
+ */
1506
1770
  export const tlsCertificateVerifyContent = (transcriptHash: u8[]): u8[] => {
1507
1771
  const none: u8[] = []
1508
1772
  const length: i32 = toI32(transcriptHash.length)
@@ -1526,7 +1790,13 @@ const tlsPutClientSchemes = (out: u8[], at: i32): i32 => {
1526
1790
  return tlsPutU16(out, p, TLS_SIGNATURE_RSA_PSS_RSAE_SHA512)
1527
1791
  }
1528
1792
 
1529
- /** Whether `scheme` is one the client's `signature_algorithms` offers, the only ones a server's CertificateVerify may use (§4.4.3). */
1793
+ /**
1794
+ * Whether `scheme` is one the client's `signature_algorithms` offers, the
1795
+ * only ones a server's CertificateVerify may use (§4.4.3).
1796
+ *
1797
+ * @param scheme The scheme a server's CertificateVerify names.
1798
+ * @returns true for ECDSA P-256 or P-384, or RSA-PSS (rsae) with SHA-256, SHA-384 or SHA-512.
1799
+ */
1530
1800
  export const tlsClientOffersScheme = (scheme: i32): boolean =>
1531
1801
  scheme === TLS_SIGNATURE_ECDSA_SECP256R1_SHA256 ||
1532
1802
  scheme === TLS_SIGNATURE_ECDSA_SECP384R1_SHA384 ||
@@ -1543,6 +1813,19 @@ export const tlsClientOffersScheme = (scheme: i32): boolean =>
1543
1813
  * `supported_versions` (TLS 1.3 alone), a `key_share` with the one entry
1544
1814
  * `shareGroup`/`sharePublic`, and `cookie` when `cookie` is not empty (the
1545
1815
  * echo of a HelloRetryRequest's, §4.2.2).
1816
+ *
1817
+ * @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
1818
+ * @param at Where the message, handshake header first, starts in `out`.
1819
+ * @param random The client's 32-byte `random` (§4.1.2), written as it is.
1820
+ * @param sessionId The `legacy_session_id`, 0 to 32 bytes; the client sends 32 for middlebox compatibility.
1821
+ * @param suites The cipher suites offered, in order of preference.
1822
+ * @param groups The groups `supported_groups` offers, in order of preference.
1823
+ * @param shareGroup The group of the one key share sent, such as `TLS_GROUP_X25519`.
1824
+ * @param sharePublic That share's public key, its `key_exchange`.
1825
+ * @param serverName The host name for `server_name`, ASCII; empty to leave the extension out.
1826
+ * @param alpn The ALPN protocols offered, one character a byte, in order; empty to leave ALPN out.
1827
+ * @param cookie A HelloRetryRequest's `cookie` to echo; empty to send none.
1828
+ * @returns Where the message ends in `out`: `at` plus its length, written or not.
1546
1829
  */
1547
1830
  export const tlsWriteClientHello = (
1548
1831
  out: u8[],
@@ -1631,7 +1914,20 @@ export const tlsWriteClientHello = (
1631
1914
  return p
1632
1915
  }
1633
1916
 
1634
- /** A ClientHello (§4.1.2) in a fresh array (`tlsWriteClientHello`). */
1917
+ /**
1918
+ * A ClientHello (§4.1.2) in a fresh array (`tlsWriteClientHello`).
1919
+ *
1920
+ * @param random The client's 32-byte `random` (§4.1.2), written as it is.
1921
+ * @param sessionId The `legacy_session_id`, 0 to 32 bytes; the client sends 32 for middlebox compatibility.
1922
+ * @param suites The cipher suites offered, in order of preference.
1923
+ * @param groups The groups `supported_groups` offers, in order of preference.
1924
+ * @param shareGroup The group of the one key share sent, such as `TLS_GROUP_X25519`.
1925
+ * @param sharePublic That share's public key, its `key_exchange`.
1926
+ * @param serverName The host name for `server_name`, ASCII; empty to leave the extension out.
1927
+ * @param alpn The ALPN protocols offered, one character a byte, in order; empty to leave ALPN out.
1928
+ * @param cookie A HelloRetryRequest's `cookie` to echo; empty to send none.
1929
+ * @returns The whole message, handshake header included.
1930
+ */
1635
1931
  export const tlsEncodeClientHello = (
1636
1932
  random: u8[],
1637
1933
  sessionId: u8[],
@@ -1693,11 +1989,23 @@ export const tlsEncodeClientHello = (
1693
1989
  * TLS 1.2 server sends.
1694
1990
  */
1695
1991
  export class TlsServerFlightView {
1696
- /** The buffer the windows point into: the one the last message was read from. */
1992
+ /**
1993
+ * The buffer the windows point into: the one the last message was read from.
1994
+ *
1995
+ * @internal Read by `net/tls/client.ts`.
1996
+ */
1697
1997
  data: u8[]
1698
- /** One bit per extension type read, for the duplicate check (§4.2). */
1998
+ /**
1999
+ * One bit per extension type read, for the duplicate check (§4.2).
2000
+ *
2001
+ * @internal Read by the free functions of this module.
2002
+ */
1699
2003
  extensionSeen: u32[]
1700
- /** The reader each read runs, kept so that a read allocates none. */
2004
+ /**
2005
+ * The reader each read runs, kept so that a read allocates none.
2006
+ *
2007
+ * @internal Read by the free functions of this module.
2008
+ */
1701
2009
  reader: TlsReader
1702
2010
  /** 0 when the message parsed, else `decode_error`. */
1703
2011
  alert: i32 = 0
@@ -1739,13 +2047,21 @@ export class TlsServerFlightView {
1739
2047
  /** EncryptedExtensions' empty `server_name`, the acknowledgement of RFC 6066 §3. */
1740
2048
  serverNameAcknowledged: boolean = false
1741
2049
 
2050
+ /**
2051
+ * Makes an empty view, every window empty and every flag false, for the
2052
+ * `tlsRead*` functions to fill.
2053
+ */
1742
2054
  constructor() {
1743
2055
  this.data = []
1744
2056
  this.extensionSeen = new Array<u32>(TLS_U16_BITMAP_WORDS)
1745
2057
  this.reader = new TlsReader(this.data, TLS_CODEC_FROM, TLS_CODEC_FROM)
1746
2058
  }
1747
2059
 
1748
- /** Forgets the last message: every window empty, every flag down, the bitmap clear. */
2060
+ /**
2061
+ * Forgets the last message: every window empty, every flag down, the bitmap clear.
2062
+ *
2063
+ * @internal Read by the free functions of this module.
2064
+ */
1749
2065
  clear(): void {
1750
2066
  this.alert = 0
1751
2067
  this.extensionAlert = 0
@@ -1938,6 +2254,11 @@ const tlsWindowIsHex = (data: u8[], at: i32, text: string): boolean => {
1938
2254
  * header — into `view`, which tells a HelloRetryRequest from it by its
1939
2255
  * `random` (§4.1.3). An absent extensions block parses: it is how a TLS 1.2
1940
2256
  * or older server answers, which the client refuses for its version.
2257
+ *
2258
+ * @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
2259
+ * @param data The buffer that holds the message, which the view's windows then point into.
2260
+ * @param off Where the ServerHello body starts in `data`, in bytes, just past the handshake header.
2261
+ * @param len How many bytes the body is, from `off`.
1941
2262
  */
1942
2263
  export const tlsReadServerHello = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
1943
2264
  const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
@@ -1968,13 +2289,25 @@ const TLS_DOWNGRADE_11: string = "444f574e47524400"
1968
2289
  * Whether the ServerHello in `view` carries a downgrade sentinel in the last
1969
2290
  * 8 bytes of its random (§4.1.3), which a TLS 1.3 client must refuse with
1970
2291
  * `illegal_parameter`.
2292
+ *
2293
+ * @param view A view `tlsReadServerHello` has just read into.
2294
+ * @returns true when the random ends in the TLS 1.2 or the TLS 1.1 sentinel.
1971
2295
  */
1972
2296
  export const tlsServerHelloDowngrade = (view: TlsServerFlightView): boolean => {
1973
2297
  const at: i32 = view.randomAt + TLS_RANDOM_SIZE - 8
1974
2298
  return tlsWindowIsHex(view.data, at, TLS_DOWNGRADE_12) || tlsWindowIsHex(view.data, at, TLS_DOWNGRADE_11)
1975
2299
  }
1976
2300
 
1977
- /** Reads an EncryptedExtensions body (§4.3.1) into `view`. */
2301
+ /**
2302
+ * Reads an EncryptedExtensions body (§4.3.1) into `view`: ALPN, an empty
2303
+ * `server_name` and `supported_groups` may be there, and any other extension
2304
+ * sets `extensionAlert`.
2305
+ *
2306
+ * @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
2307
+ * @param data The buffer that holds the message, which the view's windows then point into.
2308
+ * @param off Where the EncryptedExtensions body starts in `data`, in bytes, just past the handshake header.
2309
+ * @param len How many bytes the body is, from `off`.
2310
+ */
1978
2311
  export const tlsReadEncryptedExtensions = (
1979
2312
  view: TlsServerFlightView,
1980
2313
  data: u8[],
@@ -1995,6 +2328,11 @@ export const tlsReadEncryptedExtensions = (
1995
2328
  * entries. An empty list is `decode_error`, as §4.4.2.4 has a client answer
1996
2329
  * it; an entry with extensions is `unsupported_extension`, because the
1997
2330
  * client offers none that a CertificateEntry may carry.
2331
+ *
2332
+ * @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
2333
+ * @param data The buffer that holds the message, which the view's windows then point into.
2334
+ * @param off Where the Certificate body starts in `data`, in bytes, just past the handshake header.
2335
+ * @param len How many bytes the body is, from `off`.
1998
2336
  */
1999
2337
  export const tlsReadCertificate = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
2000
2338
  const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
@@ -2021,7 +2359,15 @@ export const tlsReadCertificate = (view: TlsServerFlightView, data: u8[], off: i
2021
2359
  tlsFlightClose(view, r, off + len)
2022
2360
  }
2023
2361
 
2024
- /** Reads a CertificateVerify body (§4.4.3) into `view`: the scheme and the signature. */
2362
+ /**
2363
+ * Reads a CertificateVerify body (§4.4.3) into `view`: the scheme and the
2364
+ * signature, neither of them checked here.
2365
+ *
2366
+ * @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
2367
+ * @param data The buffer that holds the message, which the view's windows then point into.
2368
+ * @param off Where the CertificateVerify body starts in `data`, in bytes, just past the handshake header.
2369
+ * @param len How many bytes the body is, from `off`.
2370
+ */
2025
2371
  export const tlsReadCertificateVerify = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
2026
2372
  const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
2027
2373
  if (r === null) {
@@ -2039,6 +2385,11 @@ export const tlsReadCertificateVerify = (view: TlsServerFlightView, data: u8[],
2039
2385
  * Checks a NewSessionTicket body's structure (§4.6.1) into `view`'s `alert`:
2040
2386
  * the client keeps no tickets, so it reads one only to discard it, but a
2041
2387
  * malformed one is still `decode_error`.
2388
+ *
2389
+ * @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
2390
+ * @param data The buffer that holds the message, which the view's windows then point into.
2391
+ * @param off Where the NewSessionTicket body starts in `data`, in bytes, just past the handshake header.
2392
+ * @param len How many bytes the body is, from `off`.
2042
2393
  */
2043
2394
  export const tlsReadNewSessionTicket = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
2044
2395
  const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
@@ -2052,7 +2403,13 @@ export const tlsReadNewSessionTicket = (view: TlsServerFlightView, data: u8[], o
2052
2403
  tlsFlightClose(view, r, off + len)
2053
2404
  }
2054
2405
 
2055
- /** The DER certificates of the Certificate `tlsReadCertificate` last read into `view`, leaf first, each a fresh array. */
2406
+ /**
2407
+ * The DER certificates of the Certificate `tlsReadCertificate` last read into
2408
+ * `view`, leaf first, each a fresh array.
2409
+ *
2410
+ * @param view A view `tlsReadCertificate` has just read into.
2411
+ * @returns The chain, copied out of `view.data`; empty if the read failed or another message was read since.
2412
+ */
2056
2413
  export const tlsCopyCertificates = (view: TlsServerFlightView): u8[][] => {
2057
2414
  const chain: u8[][] = []
2058
2415
  const r: TlsReader = new TlsReader(