@amritk/nish-aarch64-linux 0.36.0 → 0.38.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 (103) hide show
  1. package/INSTALL.md +92 -71
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +75 -14
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/std/LICENSE-fdlibm +8 -0
  22. package/std/README.md +109 -82
  23. package/std/array.ts +266 -0
  24. package/std/bytes.ts +23 -19
  25. package/std/compress/checksum.ts +1 -1
  26. package/std/crypto/aes.ts +48 -43
  27. package/std/crypto/base64url.ts +7 -5
  28. package/std/crypto/bcrypt-batch.ts +13 -13
  29. package/std/crypto/bcrypt.ts +33 -27
  30. package/std/crypto/chacha20poly1305.ts +16 -14
  31. package/std/crypto/ed25519.ts +19 -16
  32. package/std/crypto/hmac.ts +7 -6
  33. package/std/crypto/jwt.ts +23 -20
  34. package/std/crypto/p256.ts +63 -56
  35. package/std/crypto/p384.ts +23 -20
  36. package/std/crypto/pki.ts +18 -15
  37. package/std/crypto/rsa.ts +23 -19
  38. package/std/crypto/x25519.ts +11 -10
  39. package/std/crypto/x509.ts +69 -62
  40. package/std/host/ring.ts +124 -0
  41. package/std/host/text.ts +86 -0
  42. package/std/json-equal.ts +12 -11
  43. package/std/json-number.ts +12 -12
  44. package/std/json-tokenizer.ts +26 -22
  45. package/std/json-typed.ts +92 -78
  46. package/std/json-value.ts +38 -33
  47. package/std/json.ts +32 -28
  48. package/std/math.ts +743 -41
  49. package/std/net/dns-resolver.ts +46 -6
  50. package/std/net/dns.ts +50 -13
  51. package/std/net/fetch.ts +1744 -295
  52. package/std/net/handler.ts +30 -0
  53. package/std/net/hpack.ts +29 -26
  54. package/std/net/http-compress.ts +42 -28
  55. package/std/net/http-fields.ts +19 -4
  56. package/std/net/http1-client-codec.ts +29 -25
  57. package/std/net/http1-codec.ts +62 -91
  58. package/std/net/http1-server.ts +79 -57
  59. package/std/net/http2-frame.ts +17 -16
  60. package/std/net/http2-tls.ts +17 -15
  61. package/std/net/http2.ts +67 -54
  62. package/std/net/http3-frame.ts +11 -10
  63. package/std/net/http3-server.ts +36 -35
  64. package/std/net/http3.ts +70 -58
  65. package/std/net/postgres-async.ts +1005 -0
  66. package/std/net/postgres.ts +185 -164
  67. package/std/net/prefork.ts +38 -34
  68. package/std/net/qpack.ts +15 -14
  69. package/std/net/quic-conn-ack.ts +1 -0
  70. package/std/net/quic-conn-cid.ts +11 -0
  71. package/std/net/quic-conn-params.ts +41 -14
  72. package/std/net/quic-datagram.ts +7 -0
  73. package/std/net/quic-frame.ts +48 -12
  74. package/std/net/quic-listener.ts +61 -44
  75. package/std/net/quic-packet.ts +71 -52
  76. package/std/net/quic-recovery.ts +59 -21
  77. package/std/net/quic-stream.ts +83 -55
  78. package/std/net/quic.ts +204 -152
  79. package/std/net/serve.ts +763 -0
  80. package/std/net/tls/client.ts +1 -0
  81. package/std/net/tls/codec.ts +81 -12
  82. package/std/net/tls/record-client.ts +30 -18
  83. package/std/net/tls/record-server.ts +74 -46
  84. package/std/net/tls/record.ts +33 -24
  85. package/std/net/tls/ticket.ts +2 -0
  86. package/std/net/tls/verify.ts +8 -7
  87. package/std/net/tls-tcp.ts +58 -50
  88. package/std/net/tls.ts +55 -43
  89. package/std/net/url.ts +12 -9
  90. package/std/net/websocket.ts +23 -19
  91. package/std/net/webtransport.ts +24 -18
  92. package/std/random.ts +8 -6
  93. package/std/regex.ts +70 -59
  94. package/std/sync/ring.ts +24 -21
  95. package/std/sync/shm.ts +9 -7
  96. package/std/term-io.ts +17 -14
  97. package/std/term-probe.ts +11 -10
  98. package/std/term-rate.ts +71 -61
  99. package/std/term.ts +73 -59
  100. package/std/text.ts +242 -15
  101. package/std/threads.ts +19 -15
  102. package/std/yaml-json.ts +17 -15
  103. package/std/yaml-write.ts +21 -14
@@ -338,6 +338,7 @@ export class TlsClient {
338
338
  alpnChosen: string = ""
339
339
  /** Which of the negotiated pool's secrets are known: bit `1 << TLS_SECRET_*` for each, as `TlsServer.held`. */
340
340
  private held: i32 = 0
341
+ /** Where the handshake is: one of the `TLS_CLIENT_STATE_*` values, `TLS_CLIENT_STATE_CONNECTED` once it has finished and `TLS_CLIENT_STATE_FAILED` once `alert` is set. */
341
342
  state: i32 = 0
342
343
  /** The alert the client failed with, or 0. */
343
344
  alert: i32 = 0
@@ -299,44 +299,59 @@ export class TlsClientHelloView {
299
299
  reader: TlsReader
300
300
  /** 0 when the message parsed, or the alert it earns: `decode_error` or `illegal_parameter`. */
301
301
  alert: i32 = 0
302
+ /** `legacy_version` as sent; TLS 1.3 fixes it at 0x0303 and negotiates the real version in `supported_versions` (§4.1.2). */
302
303
  legacyVersion: i32 = 0
304
+ /** Where the client's 32-byte `random` starts in `data`; its length is always 32. */
303
305
  randomAt: i32 = 0
304
306
  /** `legacy_session_id`, which a server echoes (§4.1.3). */
305
307
  sessionIdAt: i32 = 0
308
+ /** The `legacy_session_id`'s length in bytes, 0 to 32. */
306
309
  sessionIdLength: i32 = 0
307
310
  /** `cipher_suites`, 16-bit values in the client's order. */
308
311
  suitesAt: i32 = 0
312
+ /** The `cipher_suites` window's length in bytes, two a suite. */
309
313
  suitesLength: i32 = 0
310
314
  /** `supported_versions`, 16-bit values. */
311
315
  versionsAt: i32 = 0
316
+ /** The `supported_versions` window's length in bytes, two a version; 0 when the extension was absent. */
312
317
  versionsLength: i32 = 0
313
318
  /** `supported_groups`, 16-bit values. */
314
319
  groupsAt: i32 = 0
320
+ /** The `supported_groups` window's length in bytes, two a group; 0 when the extension was absent. */
315
321
  groupsLength: i32 = 0
316
322
  /** `signature_algorithms`, 16-bit values. */
317
323
  schemesAt: i32 = 0
324
+ /** The `signature_algorithms` window's length in bytes, two a scheme; 0 when the extension was absent. */
318
325
  schemesLength: i32 = 0
319
326
  /** The `client_shares` list of `key_share`, every entry in it. */
320
327
  sharesAt: i32 = 0
328
+ /** The `client_shares` window's length in bytes; 0 when `key_share` was absent. */
321
329
  sharesLength: i32 = 0
322
330
  /** The x25519 share's key_exchange, as sent; its length is the server's to check. */
323
331
  x25519ShareAt: i32 = 0
332
+ /** The x25519 share's length in bytes as sent, 0 when none was offered; a usable share is 32. */
324
333
  x25519ShareLength: i32 = 0
325
334
  /** The `host_name` of `server_name`, or empty. */
326
335
  serverNameAt: i32 = 0
336
+ /** The `host_name`'s length in bytes, 0 when `server_name` was absent. */
327
337
  serverNameLength: i32 = 0
328
338
  /** ALPN's `protocol_name_list`, each name a length byte and its bytes (RFC 7301 §3.1). */
329
339
  alpnAt: i32 = 0
340
+ /** The ALPN list's length in bytes, its length bytes included; 0 when ALPN was absent. */
330
341
  alpnLength: i32 = 0
331
342
  /** The client's `quic_transport_parameters`, opaque (RFC 9001 §8.2). */
332
343
  quicAt: i32 = 0
344
+ /** The QUIC transport parameters' length in bytes, 0 when the extension was absent. */
333
345
  quicLength: i32 = 0
334
346
  /** The extensions block, every extension in it. */
335
347
  extensionsAt: i32 = 0
348
+ /** The extensions block's length in bytes, 0 for a hello with none. */
336
349
  extensionsLength: i32 = 0
337
350
  /** `pre_shared_key`'s `identities` list (§4.2.11): each an identity<1..2^16-1> and a 32-bit obfuscated age. */
338
351
  pskIdentitiesAt: i32 = 0
352
+ /** The `identities` window's length in bytes, 0 when `pre_shared_key` was absent. */
339
353
  pskIdentitiesLength: i32 = 0
354
+ /** How many identities `identities` holds; the binder count must match it (§4.2.11). */
340
355
  pskIdentityCount: i32 = 0
341
356
  /**
342
357
  * Where `binders`' two-byte length starts: the ClientHello up to here, its
@@ -346,31 +361,50 @@ export class TlsClientHelloView {
346
361
  pskBindersPrefixAt: i32 = 0
347
362
  /** `pre_shared_key`'s `binders` list: each a PskBinderEntry<32..255>. */
348
363
  pskBindersAt: i32 = 0
364
+ /** The `binders` window's length in bytes, 0 when `pre_shared_key` was absent. */
349
365
  pskBindersLength: i32 = 0
366
+ /** How many binders `binders` holds, one an identity. */
350
367
  pskBinderCount: i32 = 0
351
368
  /** `psk_key_exchange_modes`' `ke_modes`, one byte a mode (§4.2.9). */
352
369
  pskModesAt: i32 = 0
370
+ /** The `ke_modes` window's length in bytes, one a mode; 0 when the extension was absent. */
353
371
  pskModesLength: i32 = 0
354
372
  /** The identity `tlsSelectPskIdentity` last chose: its bytes and its obfuscated ticket age. */
355
373
  pskIdentityAt: i32 = 0
374
+ /** The chosen identity's length in bytes, 0 until `tlsSelectPskIdentity` chooses one. */
356
375
  pskIdentityLength: i32 = 0
376
+ /** The chosen identity's `obfuscated_ticket_age`, as sent: the ticket's age in milliseconds plus its `ticket_age_add`, modulo 2^32 (§4.2.11.1). */
357
377
  pskTicketAge: u32 = 0
358
378
  /** That identity's binder, the entry of the same index in `binders`. */
359
379
  pskBinderAt: i32 = 0
380
+ /**
381
+ * The chosen binder's length in bytes, 32 to 255: `tlsReadPreSharedKey` refuses a shorter one
382
+ * when it parses the extension. It must equal the suite's hash length.
383
+ */
360
384
  pskBinderLength: i32 = 0
361
385
  /** Whether `ke_modes` lists `psk_dhe_ke`, the one mode the server resumes in. */
362
386
  pskDheKe: boolean = false
363
387
  /** Whether `legacy_compression_methods` is the single null method TLS 1.3 requires (§4.1.2). */
364
388
  nullCompressionOnly: boolean = false
389
+ /** Whether the hello carried `supported_versions`: without it the client does not offer TLS 1.3 (§4.2.1). */
365
390
  hasSupportedVersions: boolean = false
391
+ /** Whether the hello carried `supported_groups`. */
366
392
  hasSupportedGroups: boolean = false
393
+ /** Whether the hello carried `key_share`, even an empty one. */
367
394
  hasKeyShare: boolean = false
395
+ /** Whether `key_share` offered an x25519 share, so `x25519ShareAt` names one. */
368
396
  hasX25519Share: boolean = false
397
+ /** Whether the hello carried `signature_algorithms`. */
369
398
  hasSignatureAlgorithms: boolean = false
399
+ /** Whether the hello carried `server_name` with a `host_name` in it. */
370
400
  hasServerName: boolean = false
401
+ /** Whether the hello carried ALPN. */
371
402
  hasAlpn: boolean = false
403
+ /** Whether the hello carried `quic_transport_parameters`, which a QUIC handshake requires (RFC 9001 §8.2). */
372
404
  hasQuicTransportParameters: boolean = false
405
+ /** Whether the hello carried `pre_shared_key`, which must be its last extension (§4.2.11). */
373
406
  hasPreSharedKey: boolean = false
407
+ /** Whether the hello carried `psk_key_exchange_modes`, without which a server must not resume (§4.2.9). */
374
408
  hasPskModes: boolean = false
375
409
 
376
410
  /**
@@ -708,19 +742,21 @@ const tlsReadExtension = (r: TlsReader, type: i32, end: i32, hello: TlsClientHel
708
742
  }
709
743
 
710
744
  /**
711
- * Reads the ClientHello body `data[off .. off + len)` — the message without
712
- * its four-byte handshake header — into `hello`, which forgets the last one
713
- * first. Its `alert` is 0 when the body parsed; otherwise the first fault
714
- * decides it: `decode_error` for a length past its bytes, a vector out of its
715
- * bounds or bytes left over, and `illegal_parameter` for an extension sent
716
- * twice (§4.2), a `pre_shared_key` that is not the last extension (§4.2.11),
717
- * two shares for one group, or a bad `server_name`. A window outside `data`
718
- * is `decode_error` too, rather than a panic. A hello that parses but offers
719
- * `pre_shared_key` with a different number of identities and binders is
720
- * `illegal_parameter`, and one that offers it without
745
+ * Reads the ClientHello body `data[off .. off + len)` — the message without its
746
+ * four-byte handshake header — into `hello`, which forgets the last one first.
747
+ * Its `alert` is 0 when the body parsed; otherwise the first fault decides it:
748
+ * `decode_error` for a length past its bytes, a vector out of its bounds or
749
+ * bytes left over, and `illegal_parameter` for an extension sent twice (§4.2),
750
+ * a `pre_shared_key` that is not the last extension (§4.2.11), two shares for
751
+ * one group, or a bad `server_name`. A window outside `data` is `decode_error`
752
+ * too, rather than a panic.
753
+ *
754
+ * A hello that parses but offers `pre_shared_key` with a different number of
755
+ * identities and binders is `illegal_parameter`, and one that offers it without
721
756
  * `psk_key_exchange_modes` is `missing_extension` (§4.2.9): rows 10 to 13 of
722
- * the fallback table in `docs/security/tls.md`, "Resumption". Nothing is copied: the windows
723
- * point into `data`, which must outlive the use made of them.
757
+ * the fallback table in `docs/security/tls.md`, "Resumption". Nothing is
758
+ * copied: the windows point into `data`, which must outlive the use made of
759
+ * them.
724
760
  *
725
761
  * @param hello The view to read into, cleared first; its `alert` is the verdict.
726
762
  * @param data The buffer that holds the message, which the view's windows then point into.
@@ -864,9 +900,11 @@ export const tlsSelectPskIdentity = (hello: TlsClientHelloView, index: i32): boo
864
900
  * says so.
865
901
  */
866
902
  export class TlsClientHello {
903
+ /** The client's 32-byte `random`. */
867
904
  random: u8[]
868
905
  /** `legacy_session_id`, which a server echoes (§4.1.3). */
869
906
  sessionId: u8[]
907
+ /** `cipher_suites`, 16-bit values in the client's order. */
870
908
  cipherSuites: i32[]
871
909
  /** `supported_versions`, in the client's order. */
872
910
  versions: i32[]
@@ -888,6 +926,7 @@ export class TlsClientHello {
888
926
  extensionTypes: i32[]
889
927
  /** `pre_shared_key`'s identities, in the client's order, and each one's obfuscated ticket age (§4.2.11). */
890
928
  pskIdentities: u8[][]
929
+ /** Each identity's `obfuscated_ticket_age`, as sent: entry `i` belongs to `pskIdentities[i]`. */
891
930
  pskTicketAges: u32[]
892
931
  /** `pre_shared_key`'s binders, one an identity. */
893
932
  pskBinders: u8[][]
@@ -901,18 +940,29 @@ export class TlsClientHello {
901
940
  pskBindersPrefix: i32 = 0
902
941
  /** 0 when the message parsed, or the alert it earns: `decode_error` or `illegal_parameter`. */
903
942
  alert: i32 = 0
943
+ /** `legacy_version` as sent; TLS 1.3 fixes it at 0x0303 and negotiates the real version in `supported_versions` (§4.1.2). */
904
944
  legacyVersion: i32 = 0
905
945
  /** Whether `legacy_compression_methods` is the single null method TLS 1.3 requires (§4.1.2). */
906
946
  nullCompressionOnly: boolean = false
947
+ /** Whether the hello carried `supported_versions`: without it the client does not offer TLS 1.3 (§4.2.1). */
907
948
  hasSupportedVersions: boolean = false
949
+ /** Whether the hello carried `supported_groups`. */
908
950
  hasSupportedGroups: boolean = false
951
+ /** Whether the hello carried `key_share`, even an empty one. */
909
952
  hasKeyShare: boolean = false
953
+ /** Whether `key_share` offered an x25519 share, so `x25519Share` holds one. */
910
954
  hasX25519Share: boolean = false
955
+ /** Whether the hello carried `signature_algorithms`. */
911
956
  hasSignatureAlgorithms: boolean = false
957
+ /** Whether the hello carried `server_name` with a `host_name` in it. */
912
958
  hasServerName: boolean = false
959
+ /** Whether the hello carried ALPN. */
913
960
  hasAlpn: boolean = false
961
+ /** Whether the hello carried `quic_transport_parameters`, which a QUIC handshake requires (RFC 9001 §8.2). */
914
962
  hasQuicTransportParameters: boolean = false
963
+ /** Whether the hello carried `pre_shared_key`, which must be its last extension (§4.2.11). */
915
964
  hasPreSharedKey: boolean = false
965
+ /** Whether the hello carried `psk_key_exchange_modes`, without which a server must not resume (§4.2.9). */
916
966
  hasPskModes: boolean = false
917
967
  /** Whether `psk_key_exchange_modes` lists `psk_dhe_ke`. */
918
968
  pskDheKe: boolean = false
@@ -2011,38 +2061,57 @@ export class TlsServerFlightView {
2011
2061
  alert: i32 = 0
2012
2062
  /** 0 when the extensions are ones this message may carry, else `illegal_parameter` or `unsupported_extension`. */
2013
2063
  extensionAlert: i32 = 0
2064
+ /** ServerHello's `legacy_version` as sent; a TLS 1.3 server sends 0x0303 and names 1.3 in `supported_versions` (§4.1.3). */
2014
2065
  legacyVersion: i32 = 0
2066
+ /** Where the server's 32-byte `random` starts in `data`; the client checks it for the downgrade sentinel (§4.1.3). */
2015
2067
  randomAt: i32 = 0
2068
+ /** `legacy_session_id_echo`, which must equal the session id the client sent (§4.1.3). */
2016
2069
  sessionIdAt: i32 = 0
2070
+ /** The echoed session id's length in bytes, 0 to 32. */
2017
2071
  sessionIdLength: i32 = 0
2072
+ /** The cipher suite the server chose, a 16-bit value. */
2018
2073
  suite: i32 = 0
2074
+ /** `legacy_compression_method`, which must be 0 (§4.1.3). */
2019
2075
  compression: i32 = 0
2076
+ /** `supported_versions`' `selected_version`, 0 when the extension was absent; 0x0304 for TLS 1.3. */
2020
2077
  selectedVersion: i32 = 0
2021
2078
  /** `key_share`: a ServerHello's entry, or a HelloRetryRequest's `selected_group` with an empty share. */
2022
2079
  shareGroup: i32 = 0
2080
+ /** Where the ServerHello's key_exchange starts in `data`. */
2023
2081
  shareAt: i32 = 0
2082
+ /** The key_exchange's length in bytes as sent, 0 for a HelloRetryRequest; an x25519 share is 32. */
2024
2083
  shareLength: i32 = 0
2025
2084
  /** A HelloRetryRequest's `cookie`, which the second ClientHello echoes (§4.2.2). */
2026
2085
  cookieAt: i32 = 0
2086
+ /** The cookie's length in bytes, 0 when none was sent. */
2027
2087
  cookieLength: i32 = 0
2028
2088
  /** EncryptedExtensions' ALPN: the one protocol the server chose (RFC 7301 §3.1). */
2029
2089
  alpnAt: i32 = 0
2090
+ /** The chosen protocol name's length in bytes, 0 when ALPN was absent. */
2030
2091
  alpnLength: i32 = 0
2031
2092
  /** Certificate's `certificate_request_context`, which must be empty for a server's (§4.4.2). */
2032
2093
  contextLength: i32 = 0
2033
2094
  /** Certificate's `certificate_list`, every entry in it, and how many there are. */
2034
2095
  certificatesAt: i32 = 0
2096
+ /** The `certificate_list` window's length in bytes. */
2035
2097
  certificatesLength: i32 = 0
2098
+ /** How many entries `certificate_list` holds; the first is the server's own certificate. */
2036
2099
  certificateCount: i32 = 0
2037
2100
  /** CertificateVerify's scheme and signature (§4.4.3). */
2038
2101
  scheme: i32 = 0
2102
+ /** Where CertificateVerify's signature starts in `data`. */
2039
2103
  signatureAt: i32 = 0
2104
+ /** The signature's length in bytes. */
2040
2105
  signatureLength: i32 = 0
2041
2106
  /** A ServerHello whose `random` is the HelloRetryRequest value (§4.1.3). */
2042
2107
  isRetry: boolean = false
2108
+ /** Whether the message carried `supported_versions`: a TLS 1.3 ServerHello must (§4.2.1). */
2043
2109
  hasSupportedVersions: boolean = false
2110
+ /** Whether the message carried `key_share`. */
2044
2111
  hasKeyShare: boolean = false
2112
+ /** Whether a HelloRetryRequest carried `cookie`. */
2045
2113
  hasCookie: boolean = false
2114
+ /** Whether EncryptedExtensions carried ALPN. */
2046
2115
  hasAlpn: boolean = false
2047
2116
  /** EncryptedExtensions' empty `server_name`, the acknowledgement of RFC 6066 §3. */
2048
2117
  serverNameAcknowledged: boolean = false
@@ -14,24 +14,25 @@
14
14
  * }
15
15
  * const got: i32 = conn.read(buf, 0, buf.length); // application data
16
16
  *
17
- * **What it adds to `TlsClient`.** Records around the handshake messages:
18
- * the ClientHello in the clear, then the client's Finished under the
19
- * handshake keys, and every key installed at the record boundary RFC 8446
20
- * §5.1 puts it on. The middlebox-compatibility `change_cipher_spec` of §D.4,
21
- * which a client that sends a session id (this one always does) owes: one,
22
- * in the clear, before its second ClientHello after a HelloRetryRequest, or
23
- * else before its first protected record; the server's is dropped from its
24
- * first handshake record until its Finished. Alerts (§6) both ways, as the
25
- * server has them: `close_notify` ends the server's stream, `user_canceled`
26
- * is passed over, any other alert ends the connection, and a refusal of this
27
- * module's or of `TlsClient`'s is sent as a fatal alert under whatever keys
28
- * the client writes with. KeyUpdate (§4.6.3) both ways, with the server's
29
- * limits: `TLS_RECORD_IDLE_LIMIT` records in a row that carry nothing, and
30
- * `TLS_RECORD_MAX_KEY_UPDATES` KeyUpdates. A NewSessionTicket is handed to
31
- * `TlsClient`, which checks and drops it; any other post-handshake message is
32
- * `unexpected_message` (no client certificate is offered, so no
33
- * CertificateRequest). A stream that ends without `close_notify` is
34
- * truncation.
17
+ * **What it adds to `TlsClient`.** Records around the handshake messages: the
18
+ * ClientHello in the clear, then the client's Finished under the handshake
19
+ * keys, and every key installed at the record boundary RFC 8446 §5.1 puts it
20
+ * on. The middlebox-compatibility `change_cipher_spec` of §D.4, which a client
21
+ * that sends a session id (this one always does) owes: one, in the clear,
22
+ * before its second ClientHello after a HelloRetryRequest, or else before its
23
+ * first protected record; the server's is dropped from its first handshake
24
+ * record until its Finished.
25
+ *
26
+ * Alerts (§6) both ways, as the server has them: `close_notify` ends the
27
+ * server's stream, `user_canceled` is passed over, any other alert ends the
28
+ * connection, and a refusal of this module's or of `TlsClient`'s is sent as a
29
+ * fatal alert under whatever keys the client writes with. KeyUpdate (§4.6.3)
30
+ * both ways, with the server's limits: `TLS_RECORD_IDLE_LIMIT` records in a row
31
+ * that carry nothing, and `TLS_RECORD_MAX_KEY_UPDATES` KeyUpdates. A
32
+ * NewSessionTicket is handed to `TlsClient`, which checks and drops it; any
33
+ * other post-handshake message is `unexpected_message` (no client certificate
34
+ * is offered, so no CertificateRequest). A stream that ends without
35
+ * `close_notify` is truncation.
35
36
  *
36
37
  * **Authentication is the caller's.** At `TlsClient`'s `WAIT_AUTH` the
37
38
  * records after the server's CertificateVerify stay in the reader and
@@ -186,10 +187,13 @@ const tlsClientAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]
186
187
  * record the client writes carries after its content type (§5.4).
187
188
  */
188
189
  export class TlsRecordClient {
190
+ /** The handshake underneath: read it for the suite, ALPN, the server's certificates and the exporter secret. */
189
191
  tls: TlsClient
190
192
  /** @internal Read by `net/fetch.ts`. */
191
193
  reader: TlsRecordReader
194
+ /** Opens the server's records; its sequence number counts the records read at the current keys. */
192
195
  readProtection: TlsRecordProtection
196
+ /** Seals the client's records; its sequence number counts the records written at the current keys. */
193
197
  writeProtection: TlsRecordProtection
194
198
  /** Never installed: seals the compatibility record in the clear once the write side is protected. */
195
199
  private clearProtection: TlsRecordProtection
@@ -208,15 +212,21 @@ export class TlsRecordClient {
208
212
  private writeHash: i32 = 0
209
213
  /** The header of a post-handshake message the server has sent part of, and a KeyUpdate's body. */
210
214
  private postHandshake: u8[]
215
+ /** Where the connection stands: one of the `TLS_RECORD_STATE_*` constants. */
211
216
  state: i32 = 0
217
+ /** The alert this client sent, or 0. */
212
218
  alert: i32 = 0
219
+ /** The error alert the server sent, or -1 when it sent none. */
213
220
  peerAlert: i32 = -1
221
+ /** How many zero bytes each protected record the client writes carries after its content type (§5.4). */
214
222
  padding: i32 = 0
215
223
  /** The `TLS_LEVEL_*` the server's records are read at. */
216
224
  private readLevel: i32 = 0
217
225
  /** The `TLS_LEVEL_*` the client's records are written at. */
218
226
  private writeLevel: i32 = 0
227
+ /** The index in `output` of the first byte still to send; `consume(sent)` moves it on. */
219
228
  outputStart: i32 = 0
229
+ /** One past the last byte still to send in `output`; `outputStart` when nothing is pending. */
220
230
  outputEnd: i32 = 0
221
231
  /** @internal Read by `net/fetch.ts`. */
222
232
  plainStart: i32 = 0
@@ -231,7 +241,9 @@ export class TlsRecordClient {
231
241
  /** How many body bytes of a NewSessionTicket are still to come, handed to `TlsClient` as they arrive. */
232
242
  private ticketLeft: i32 = 0
233
243
  private idleRecords: i32 = 0
244
+ /** How many KeyUpdates from the server this client has taken, up to `TLS_RECORD_MAX_KEY_UPDATES`. */
234
245
  keyUpdates: i32 = 0
246
+ /** True once the server's `close_notify` has arrived. */
235
247
  peerClosed: boolean = false
236
248
  /** @internal Read by `net/fetch.ts`. */
237
249
  ended: boolean = false
@@ -15,59 +15,66 @@
15
15
  * **What it adds to `TlsServer`.** Records around the handshake messages:
16
16
  * ServerHello (or HelloRetryRequest) in the clear, the rest of the server's
17
17
  * flight — EncryptedExtensions through Finished — in as few records as fit
18
- * under the handshake keys once it is signed, which is one record for RFC
19
- * 8448 §3's trace, and every key installed at the record boundary RFC 8446
20
- * §5.1 puts it on. The middlebox-compatibility `change_cipher_spec` of §D.4:
21
- * the server sends one right after its first handshake message when the
22
- * client sent a session id, and drops the client's from its first ClientHello
23
- * until its Finished. Alerts (§6) both ways: `close_notify` ends the client's
24
- * stream, `user_canceled` is passed over, any other alert ends the
25
- * connection, and a refusal of this module's or of `TlsServer`'s is sent as a
26
- * fatal alert under whatever keys the server writes with. KeyUpdate (§4.6.3)
27
- * both ways: the client's moves the read keys on and, when it asks, the
28
- * server answers with its own; `keyUpdate` sends one, and the writer sends
29
- * one by itself before a key has protected `TLS_KEY_UPDATE_RECORDS` records.
30
- * More than `TLS_RECORD_IDLE_LIMIT` records in a row that carry nothing for
31
- * the caller — KeyUpdates among them, which each cost two key derivations —
32
- * are refused. A stream that ends without `close_notify` is truncation, but
33
- * only once the records still waiting for the caller have been read.
18
+ * under the handshake keys once it is signed, which is one record for RFC 8448
19
+ * §3's trace, and every key installed at the record boundary RFC 8446 §5.1 puts
20
+ * it on.
21
+ *
22
+ * The middlebox-compatibility `change_cipher_spec` of §D.4: the server sends
23
+ * one right after its first handshake message when the client sent a session
24
+ * id, and drops the client's from its first ClientHello until its Finished.
25
+ * Alerts (§6) both ways: `close_notify` ends the client's stream,
26
+ * `user_canceled` is passed over, any other alert ends the connection, and a
27
+ * refusal of this module's or of `TlsServer`'s is sent as a fatal alert under
28
+ * whatever keys the server writes with.
29
+ *
30
+ * KeyUpdate (§4.6.3) both ways: the client's moves the read keys on and, when
31
+ * it asks, the server answers with its own; `keyUpdate` sends one, and the
32
+ * writer sends one by itself before a key has protected
33
+ * `TLS_KEY_UPDATE_RECORDS` records. More than `TLS_RECORD_IDLE_LIMIT` records
34
+ * in a row that carry nothing for the caller — KeyUpdates among them, which
35
+ * each cost two key derivations — are refused. A stream that ends without
36
+ * `close_notify` is truncation, but only once the records still waiting for the
37
+ * caller have been read.
34
38
  *
35
39
  * **Borrowed buffers.** The record being read, the record being opened, the
36
- * bytes to send and the application data waiting for the caller each live in
37
- * an array borrowed from a `TlsRecordPools` when the connection first needs
38
- * it, kept while the connection lasts, and given back zeroed with
39
- * `BufferPool.giveSecret` when it ends (`start`, `wipe`, `release`), so a
40
- * slot with no connection holds none of them, a connection that has finished
41
- * its handshake allocates nothing that outlives a record, and a slot can be
42
- * handed the next connection with `start`. Each give-back zeroes the whole
43
- * buffer, so it happens once a connection rather than once a record. The handshake's messages are
44
- * sealed straight out of `TlsServer`'s own buffers, and the application
45
- * secrets are copied into this slot's fields, so a slot that hands its
46
- * `TlsServer` the next connection with `restart` runs every handshake after
47
- * the first without moving the arena, whichever suite it negotiates; a
48
- * KeyUpdate's key install leaves nothing either (TLS-3 in
49
- * `docs/security/tls.md`). When the caller leaves application data unread
50
- * for long enough to fill its buffer, records stay in the reader and
51
- * `interest` stops asking for reads, which is how the peer is made to wait.
40
+ * bytes to send and the application data waiting for the caller each live in an
41
+ * array borrowed from a `TlsRecordPools` when the connection first needs it,
42
+ * kept while the connection lasts, and given back zeroed with
43
+ * `BufferPool.giveSecret` when it ends (`start`, `wipe`, `release`), so a slot
44
+ * with no connection holds none of them, a connection that has finished its
45
+ * handshake allocates nothing that outlives a record, and a slot can be handed
46
+ * the next connection with `start`.
47
+ *
48
+ * Each give-back zeroes the whole buffer, so it happens once a connection
49
+ * rather than once a record. The handshake's messages are sealed straight out
50
+ * of `TlsServer`'s own buffers, and the application secrets are copied into
51
+ * this slot's fields, so a slot that hands its `TlsServer` the next connection
52
+ * with `restart` runs every handshake after the first without moving the arena,
53
+ * whichever suite it negotiates; a KeyUpdate's key install leaves nothing
54
+ * either (TLS-3 in `docs/security/tls.md`). When the caller leaves application
55
+ * data unread for long enough to fill its buffer, records stay in the reader
56
+ * and `interest` stops asking for reads, which is how the peer is made to wait.
52
57
  *
53
58
  * **Secrets.** Each traffic secret is a `Secret<u8[]>` (`nish:secret`):
54
59
  * `TlsServer` holds the handshake's in its `TlsSecretPool`s, and this slot
55
60
  * holds the two application traffic secrets in four readonly fields, one per
56
61
  * direction and hash length, with `readHash` and `writeHash` saying which of
57
- * each pair is live (0 when neither is). A secret is copied out of
58
- * `TlsServer` as it is installed and the server's copy is zeroed, as are the
59
- * handshake secret once the flight is signed and the ephemeral key once the
60
- * ServerHello is written, or the handshake fails; a KeyUpdate overwrites the
61
- * live field in place with the next secret. Every way the connection ends
62
- * wipes what it holds: a failure of any kind (`fail`, the client's error
63
- * alert, a truncated stream, `abort`) wipes both directions' keys, both
64
- * secrets and everything `TlsServer` still holds once the last alert is
65
- * sealed; the server's `close_notify`, once sealed, wipes the write
66
- * direction, the client's the read direction, and `close` the resumption
67
- * master secret; and `wipe` (which `wipeKeys` calls when the carrier closes
68
- * the slot) and `start` wipe the rest. No secret leaves either object but
69
- * through `expose`, so a secret cannot be logged or kept outside
70
- * this module and `nish/net/tls` (TLS-1 in `docs/security/tls.md`).
62
+ * each pair is live (0 when neither is).
63
+ *
64
+ * A secret is copied out of `TlsServer` as it is installed and the server's
65
+ * copy is zeroed, as are the handshake secret once the flight is signed and the
66
+ * ephemeral key once the ServerHello is written, or the handshake fails; a
67
+ * KeyUpdate overwrites the live field in place with the next secret.
68
+ *
69
+ * Every way the connection ends wipes what it holds: a failure of any kind
70
+ * (`fail`, the client's error alert, a truncated stream, `abort`) wipes both
71
+ * directions' keys, both secrets and everything `TlsServer` still holds once
72
+ * the last alert is sealed; the server's `close_notify`, once sealed, wipes the
73
+ * write direction, the client's the read direction, and `close` the resumption
74
+ * master secret; and `wipe` (which `wipeKeys` calls when the carrier closes the
75
+ * slot) and `start` wipe the rest. No secret leaves either object but through
76
+ * `expose`, so a secret cannot be logged or kept outside this module and
77
+ * `nish/net/tls` (TLS-1 in `docs/security/tls.md`).
71
78
  *
72
79
  * Refusals are alerts, never panics. Written from RFC 8446 §4.6.3, §5, §6
73
80
  * and §D.4, not ported from another implementation.
@@ -180,9 +187,13 @@ const TLS_RECORD_SCRATCH_SIZE: i32 = TLS_MAX_PLAINTEXT
180
187
  * (`BufferPool.giveSecret`): it held plaintext, or handshake bytes.
181
188
  */
182
189
  export class TlsRecordPools {
190
+ /** Lends each slot its `output` buffer, where sealed records wait to be sent. */
183
191
  output: BufferPool
192
+ /** Lends each slot its `plain` buffer, where opened application data waits to be read. */
184
193
  plain: BufferPool
194
+ /** Lends each slot the buffer a record's content is opened into. */
185
195
  scratch: BufferPool
196
+ /** Lends each slot its record reader's buffer. */
186
197
  reader: BufferPool
187
198
 
188
199
  /**
@@ -299,9 +310,12 @@ const tlsAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]>): bo
299
310
  * caller sets it.
300
311
  */
301
312
  export class TlsRecordServer {
313
+ /** The handshake underneath: read it for the suite, ALPN, server name and exporter secret. */
302
314
  tls: TlsServer
303
315
  private reader: TlsRecordReader
316
+ /** Opens the client's records; its sequence number counts the records read at the current keys. */
304
317
  readProtection: TlsRecordProtection
318
+ /** Seals the server's records; its sequence number counts the records written at the current keys. */
305
319
  writeProtection: TlsRecordProtection
306
320
  /** Bytes to send are `output[outputStart .. outputEnd)`; `empty` until a record is sealed. */
307
321
  output: u8[]
@@ -319,26 +333,39 @@ export class TlsRecordServer {
319
333
  * say which of a pair holds the secret, and are 0 before it is known.
320
334
  */
321
335
  readonly readTraffic256: Secret<u8[]>
336
+ /** The client's current application traffic secret when the hash is SHA-384, 48 bytes. */
322
337
  readonly readTraffic384: Secret<u8[]>
338
+ /** The server's current application traffic secret when the hash is SHA-256, 32 bytes. */
323
339
  readonly writeTraffic256: Secret<u8[]>
340
+ /** The server's current application traffic secret when the hash is SHA-384, 48 bytes. */
324
341
  readonly writeTraffic384: Secret<u8[]>
342
+ /** The hash length of the client's traffic secret in bytes (32 or 48), or 0 before it is known. */
325
343
  readHash: i32 = 0
344
+ /** The hash length of the server's traffic secret in bytes (32 or 48), or 0 before it is known. */
326
345
  writeHash: i32 = 0
327
346
  /** A post-handshake message the client has sent part of. */
328
347
  private postHandshake: u8[]
348
+ /** Where the connection stands: one of the `TLS_RECORD_STATE_*` constants. */
329
349
  state: i32 = 0
350
+ /** The alert this server sent, or 0. */
330
351
  alert: i32 = 0
352
+ /** The error alert the client sent, or -1 when it sent none. */
331
353
  peerAlert: i32 = -1
354
+ /** How many zero bytes each protected record the server writes carries after its content type (§5.4); 0 unless the caller sets it. */
332
355
  padding: i32 = 0
333
356
  /** The `TLS_LEVEL_*` the client's records are read at: Initial in the clear, then Handshake, then Application. */
334
357
  private readLevel: i32 = 0
335
358
  /** The `TLS_LEVEL_*` the server's records are written at. */
336
359
  writeLevel: i32 = 0
360
+ /** The index in `output` of the first byte still to send; `consume(sent)` moves it on. */
337
361
  outputStart: i32 = 0
362
+ /** One past the last byte still to send in `output`; `outputStart` when nothing is pending. */
338
363
  outputEnd: i32 = 0
339
364
  /** How far into `output` this borrowing has written, which says whether its give-back zeroes. */
340
365
  private outputUsed: i32 = 0
366
+ /** The index in `plain` of the first byte of application data the caller has not read. */
341
367
  plainStart: i32 = 0
368
+ /** One past the last unread byte in `plain`; `plainStart` when none is pending. */
342
369
  plainEnd: i32 = 0
343
370
  /** How far into `plain` this borrowing has written. */
344
371
  private plainUsed: i32 = 0
@@ -355,6 +382,7 @@ export class TlsRecordServer {
355
382
  keyUpdates: i32 = 0
356
383
  /** The server's flight is signed and finished, so its Handshake-level output may be sealed. */
357
384
  private flightReady: boolean = false
385
+ /** True once the client's `close_notify` has arrived. */
358
386
  peerClosed: boolean = false
359
387
  /**
360
388
  * The client's TCP stream ended. Without a `close_notify` before it, that is truncation and the connection failed.