@amritk/nish-x86_64-linux 0.38.0 → 0.40.1
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.
- package/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/net/tls/codec.ts
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
* } from "nish/net/tls/codec";
|
|
9
9
|
*
|
|
10
10
|
* // `body` is a ClientHello without its four-byte handshake header;
|
|
11
|
-
* // `random`, `suite` and `
|
|
11
|
+
* // `random`, `suite` and `serverShare` are the server's own.
|
|
12
12
|
* const hello = new TlsClientHelloView()
|
|
13
13
|
* tlsReadClientHello(hello, body, 0, toI32(body.length))
|
|
14
14
|
* if (hello.alert !== 0) {
|
|
15
15
|
* // send hello.alert, such as TLS_ALERT_DECODE_ERROR (50), and close
|
|
16
16
|
* } else if (tlsWindowHasU16(hello.data, hello.versionsAt, hello.versionsLength, TLS_VERSION_13)) {
|
|
17
17
|
* const sessionId: u8[] = tlsCopyWindow(hello.data, hello.sessionIdAt, hello.sessionIdLength)
|
|
18
|
-
* const reply: u8[] = tlsEncodeServerHello(random, sessionId, suite,
|
|
18
|
+
* const reply: u8[] = tlsEncodeServerHello(random, sessionId, suite, serverShare)
|
|
19
19
|
* }
|
|
20
20
|
*
|
|
21
21
|
* **One reader and eight writers.** `tlsParseClientHello` reads a ClientHello
|
|
@@ -56,8 +56,9 @@
|
|
|
56
56
|
* **Extensions.** The ones read are `server_name` (RFC 6066 §3),
|
|
57
57
|
* `supported_groups`, `signature_algorithms`, `supported_versions` and
|
|
58
58
|
* `key_share` (RFC 8446 §4.2), `application_layer_protocol_negotiation`
|
|
59
|
-
* (RFC 7301 §3.1)
|
|
60
|
-
* `
|
|
59
|
+
* (RFC 7301 §3.1), `quic_transport_parameters` (RFC 9001 §8.2) and
|
|
60
|
+
* `record_size_limit` (RFC 8449 §4), and `pre_shared_key` and
|
|
61
|
+
* `psk_key_exchange_modes` (RFC 8446 §4.2.11, §4.2.9),
|
|
61
62
|
* whose identities and binders the reader checks against their syntax but
|
|
62
63
|
* does not open: a ticket and its binder are the server's to judge. Every
|
|
63
64
|
* other extension is skipped, which is what RFC 8446 §4.2 asks of a server
|
|
@@ -113,6 +114,15 @@ export const TLS_EXT_SUPPORTED_VERSIONS: i32 = 43
|
|
|
113
114
|
export const TLS_EXT_KEY_SHARE: i32 = 51
|
|
114
115
|
/** `quic_transport_parameters` (RFC 9001 §8.2): QUIC's own parameters, opaque to TLS. */
|
|
115
116
|
export const TLS_EXT_QUIC_TRANSPORT_PARAMETERS: i32 = 57
|
|
117
|
+
/** `record_size_limit` (RFC 8449 §4): the largest protected record plaintext the sender will take, content type included. */
|
|
118
|
+
export const TLS_EXT_RECORD_SIZE_LIMIT: i32 = 28
|
|
119
|
+
/** The smallest `record_size_limit` a peer may send (RFC 8449 §4); below it is `illegal_parameter`. */
|
|
120
|
+
export const TLS_RECORD_SIZE_LIMIT_MIN: i32 = 64
|
|
121
|
+
/**
|
|
122
|
+
* The largest `record_size_limit` TLS 1.3 can use (RFC 8449 §4): 2^14 bytes
|
|
123
|
+
* of content and the content-type byte. A larger value means this.
|
|
124
|
+
*/
|
|
125
|
+
export const TLS_RECORD_SIZE_LIMIT_MAX: i32 = 16385
|
|
116
126
|
/** `cookie` (§4.2.2): what a HelloRetryRequest may carry and the client's second ClientHello echoes. */
|
|
117
127
|
export const TLS_EXT_COOKIE: i32 = 44
|
|
118
128
|
|
|
@@ -122,7 +132,7 @@ export const TLS_EXT_COOKIE: i32 = 44
|
|
|
122
132
|
export const TLS_LEGACY_VERSION: i32 = 0x0303
|
|
123
133
|
/** TLS 1.3 in `supported_versions` (§4.2.1). */
|
|
124
134
|
export const TLS_VERSION_13: i32 = 0x0304
|
|
125
|
-
/** x25519 (§4.2.7), the
|
|
135
|
+
/** x25519 (§4.2.7), the group `nish/net/tls` prefers. */
|
|
126
136
|
export const TLS_GROUP_X25519: i32 = 0x001d
|
|
127
137
|
/** The length of an x25519 key share (RFC 8446 §4.2.8.2). */
|
|
128
138
|
export const TLS_X25519_SHARE_SIZE: i32 = 32
|
|
@@ -136,7 +146,7 @@ export const TLS_SIGNATURE_ECDSA_SECP384R1_SHA384: i32 = 0x0503
|
|
|
136
146
|
export const TLS_SIGNATURE_RSA_PSS_RSAE_SHA384: i32 = 0x0805
|
|
137
147
|
/** `rsa_pss_rsae_sha512` (§4.2.3), offered by the client. */
|
|
138
148
|
export const TLS_SIGNATURE_RSA_PSS_RSAE_SHA512: i32 = 0x0806
|
|
139
|
-
/** secp256r1 (§4.2.7), the second group the client offers. */
|
|
149
|
+
/** secp256r1 (§4.2.7), the second group the client offers and the server agrees to. */
|
|
140
150
|
export const TLS_GROUP_SECP256R1: i32 = 0x0017
|
|
141
151
|
/** The length of a secp256r1 key share: an uncompressed point, 0x04 then X and Y (§4.2.8.2). */
|
|
142
152
|
export const TLS_P256_SHARE_SIZE: i32 = 65
|
|
@@ -331,6 +341,10 @@ export class TlsClientHelloView {
|
|
|
331
341
|
x25519ShareAt: i32 = 0
|
|
332
342
|
/** The x25519 share's length in bytes as sent, 0 when none was offered; a usable share is 32. */
|
|
333
343
|
x25519ShareLength: i32 = 0
|
|
344
|
+
/** The secp256r1 share's key_exchange, as sent; its length is the server's to check. */
|
|
345
|
+
p256ShareAt: i32 = 0
|
|
346
|
+
/** The secp256r1 share's length in bytes as sent, 0 when none was offered; a usable share is 65. */
|
|
347
|
+
p256ShareLength: i32 = 0
|
|
334
348
|
/** The `host_name` of `server_name`, or empty. */
|
|
335
349
|
serverNameAt: i32 = 0
|
|
336
350
|
/** The `host_name`'s length in bytes, 0 when `server_name` was absent. */
|
|
@@ -394,6 +408,8 @@ export class TlsClientHelloView {
|
|
|
394
408
|
hasKeyShare: boolean = false
|
|
395
409
|
/** Whether `key_share` offered an x25519 share, so `x25519ShareAt` names one. */
|
|
396
410
|
hasX25519Share: boolean = false
|
|
411
|
+
/** Whether `key_share` offered a secp256r1 share, so `p256ShareAt` names one. */
|
|
412
|
+
hasP256Share: boolean = false
|
|
397
413
|
/** Whether the hello carried `signature_algorithms`. */
|
|
398
414
|
hasSignatureAlgorithms: boolean = false
|
|
399
415
|
/** Whether the hello carried `server_name` with a `host_name` in it. */
|
|
@@ -402,6 +418,14 @@ export class TlsClientHelloView {
|
|
|
402
418
|
hasAlpn: boolean = false
|
|
403
419
|
/** Whether the hello carried `quic_transport_parameters`, which a QUIC handshake requires (RFC 9001 §8.2). */
|
|
404
420
|
hasQuicTransportParameters: boolean = false
|
|
421
|
+
/** Whether the hello carried `record_size_limit` (RFC 8449). */
|
|
422
|
+
hasRecordSizeLimit: boolean = false
|
|
423
|
+
/**
|
|
424
|
+
* The client's `record_size_limit`, at least `TLS_RECORD_SIZE_LIMIT_MIN`
|
|
425
|
+
* and at most `TLS_RECORD_SIZE_LIMIT_MAX`, a larger value lowered to it;
|
|
426
|
+
* 0 when the hello carried none.
|
|
427
|
+
*/
|
|
428
|
+
recordSizeLimit: i32 = 0
|
|
405
429
|
/** Whether the hello carried `pre_shared_key`, which must be its last extension (§4.2.11). */
|
|
406
430
|
hasPreSharedKey: boolean = false
|
|
407
431
|
/** Whether the hello carried `psk_key_exchange_modes`, without which a server must not resume (§4.2.9). */
|
|
@@ -441,6 +465,8 @@ export class TlsClientHelloView {
|
|
|
441
465
|
this.sharesLength = 0
|
|
442
466
|
this.x25519ShareAt = 0
|
|
443
467
|
this.x25519ShareLength = 0
|
|
468
|
+
this.p256ShareAt = 0
|
|
469
|
+
this.p256ShareLength = 0
|
|
444
470
|
this.serverNameAt = 0
|
|
445
471
|
this.serverNameLength = 0
|
|
446
472
|
this.alpnAt = 0
|
|
@@ -469,10 +495,13 @@ export class TlsClientHelloView {
|
|
|
469
495
|
this.hasSupportedGroups = false
|
|
470
496
|
this.hasKeyShare = false
|
|
471
497
|
this.hasX25519Share = false
|
|
498
|
+
this.hasP256Share = false
|
|
472
499
|
this.hasSignatureAlgorithms = false
|
|
473
500
|
this.hasServerName = false
|
|
474
501
|
this.hasAlpn = false
|
|
475
502
|
this.hasQuicTransportParameters = false
|
|
503
|
+
this.hasRecordSizeLimit = false
|
|
504
|
+
this.recordSizeLimit = 0
|
|
476
505
|
this.hasPreSharedKey = false
|
|
477
506
|
this.hasPskModes = false
|
|
478
507
|
for (let k: i32 = 0; k < toI32(this.extensionSeen.length); k++) {
|
|
@@ -612,6 +641,11 @@ const tlsReadKeyShare = (r: TlsReader, hello: TlsClientHelloView): void => {
|
|
|
612
641
|
hello.x25519ShareAt = r.at
|
|
613
642
|
hello.x25519ShareLength = shareEnd - r.at
|
|
614
643
|
}
|
|
644
|
+
if (group === TLS_GROUP_SECP256R1 && !r.failed) {
|
|
645
|
+
hello.hasP256Share = true
|
|
646
|
+
hello.p256ShareAt = r.at
|
|
647
|
+
hello.p256ShareLength = shareEnd - r.at
|
|
648
|
+
}
|
|
615
649
|
tlsReadSkipTo(r, shareEnd)
|
|
616
650
|
}
|
|
617
651
|
r.end = outerEnd
|
|
@@ -689,6 +723,26 @@ const tlsReadPskModes = (r: TlsReader, hello: TlsClientHelloView): void => {
|
|
|
689
723
|
}
|
|
690
724
|
}
|
|
691
725
|
|
|
726
|
+
/**
|
|
727
|
+
* `record_size_limit` (RFC 8449 §4), the body `r.at .. end`: a bare
|
|
728
|
+
* `uint16`. A body of another length fails the reader, `decode_error`, and
|
|
729
|
+
* is checked before the value so that a short value with trailing bytes is
|
|
730
|
+
* not reported as `illegal_parameter`; a value below 64 is
|
|
731
|
+
* `illegal_parameter`; one above the TLS 1.3 maximum is read as that maximum.
|
|
732
|
+
*/
|
|
733
|
+
const tlsReadRecordSizeLimit = (r: TlsReader, end: i32, hello: TlsClientHelloView): void => {
|
|
734
|
+
if (end - r.at !== 2) {
|
|
735
|
+
r.failed = true
|
|
736
|
+
return
|
|
737
|
+
}
|
|
738
|
+
const limit: i32 = tlsReadU16(r)
|
|
739
|
+
hello.hasRecordSizeLimit = true
|
|
740
|
+
if (limit < TLS_RECORD_SIZE_LIMIT_MIN) {
|
|
741
|
+
tlsHelloRefuse(hello, TLS_ALERT_ILLEGAL_PARAMETER)
|
|
742
|
+
}
|
|
743
|
+
hello.recordSizeLimit = limit > TLS_RECORD_SIZE_LIMIT_MAX ? TLS_RECORD_SIZE_LIMIT_MAX : limit
|
|
744
|
+
}
|
|
745
|
+
|
|
692
746
|
/** Reads one extension body, `r.at .. end`, of type `type`; an unknown type is skipped. */
|
|
693
747
|
const tlsReadExtension = (r: TlsReader, type: i32, end: i32, hello: TlsClientHelloView): void => {
|
|
694
748
|
switch (type) {
|
|
@@ -728,6 +782,9 @@ const tlsReadExtension = (r: TlsReader, type: i32, end: i32, hello: TlsClientHel
|
|
|
728
782
|
hello.quicLength = end - r.at
|
|
729
783
|
tlsReadSkipTo(r, end)
|
|
730
784
|
break
|
|
785
|
+
case TLS_EXT_RECORD_SIZE_LIMIT:
|
|
786
|
+
tlsReadRecordSizeLimit(r, end, hello)
|
|
787
|
+
break
|
|
731
788
|
default:
|
|
732
789
|
tlsReadSkipTo(r, end)
|
|
733
790
|
break
|
|
@@ -960,6 +1017,8 @@ export class TlsClientHello {
|
|
|
960
1017
|
hasAlpn: boolean = false
|
|
961
1018
|
/** Whether the hello carried `quic_transport_parameters`, which a QUIC handshake requires (RFC 9001 §8.2). */
|
|
962
1019
|
hasQuicTransportParameters: boolean = false
|
|
1020
|
+
/** The client's `record_size_limit` (RFC 8449), lowered to the TLS 1.3 maximum, or 0 when it sent none. */
|
|
1021
|
+
recordSizeLimit: i32 = 0
|
|
963
1022
|
/** Whether the hello carried `pre_shared_key`, which must be its last extension (§4.2.11). */
|
|
964
1023
|
hasPreSharedKey: boolean = false
|
|
965
1024
|
/** Whether the hello carried `psk_key_exchange_modes`, without which a server must not resume (§4.2.9). */
|
|
@@ -1109,6 +1168,7 @@ export const tlsParseClientHello = (data: u8[], off: i32, len: i32): TlsClientHe
|
|
|
1109
1168
|
hello.hasServerName = view.hasServerName
|
|
1110
1169
|
hello.hasAlpn = view.hasAlpn
|
|
1111
1170
|
hello.hasQuicTransportParameters = view.hasQuicTransportParameters
|
|
1171
|
+
hello.recordSizeLimit = view.recordSizeLimit
|
|
1112
1172
|
hello.hasPreSharedKey = view.hasPreSharedKey
|
|
1113
1173
|
hello.hasPskModes = view.hasPskModes
|
|
1114
1174
|
hello.pskDheKe = view.pskDheKe
|
|
@@ -1248,7 +1308,9 @@ const tlsPutHelloHead = (
|
|
|
1248
1308
|
* A ServerHello (§4.1.3), as `tlsWriteServerHello` and
|
|
1249
1309
|
* `tlsWriteServerHelloPsk` write it: with a `pre_shared_key` naming
|
|
1250
1310
|
* `selectedIdentity` first when that is not negative, the order RFC 8448 §4
|
|
1251
|
-
* shows, then `key_share` and `supported_versions`.
|
|
1311
|
+
* shows, then `key_share` and `supported_versions`. The share's group follows
|
|
1312
|
+
* from `publicKey`'s length: 65 bytes is a secp256r1 point, anything else
|
|
1313
|
+
* an x25519 key.
|
|
1252
1314
|
*/
|
|
1253
1315
|
const tlsPutServerHello = (
|
|
1254
1316
|
out: u8[],
|
|
@@ -1258,7 +1320,7 @@ const tlsPutServerHello = (
|
|
|
1258
1320
|
sessionIdOff: i32,
|
|
1259
1321
|
sessionIdLength: i32,
|
|
1260
1322
|
suite: i32,
|
|
1261
|
-
|
|
1323
|
+
publicKey: u8[],
|
|
1262
1324
|
selectedIdentity: i32
|
|
1263
1325
|
): i32 => {
|
|
1264
1326
|
let p: i32 = tlsPutHelloHead(out, at + 4, random, false, sessionId, sessionIdOff, sessionIdLength, suite)
|
|
@@ -1269,9 +1331,10 @@ const tlsPutServerHello = (
|
|
|
1269
1331
|
}
|
|
1270
1332
|
p = tlsPutU16(out, p, TLS_EXT_KEY_SHARE)
|
|
1271
1333
|
const share: i32 = p + 2
|
|
1272
|
-
|
|
1334
|
+
const group: i32 = toI32(publicKey.length) === TLS_P256_SHARE_SIZE ? TLS_GROUP_SECP256R1 : TLS_GROUP_X25519
|
|
1335
|
+
p = tlsPutU16(out, share, group)
|
|
1273
1336
|
const key: i32 = p + 2
|
|
1274
|
-
p = tlsPutBytes(out, key,
|
|
1337
|
+
p = tlsPutBytes(out, key, publicKey)
|
|
1275
1338
|
tlsPutLength(out, key, p, 2)
|
|
1276
1339
|
tlsPutLength(out, share, p, 2)
|
|
1277
1340
|
p = tlsPutSelectedVersion(out, p)
|
|
@@ -1284,7 +1347,8 @@ const tlsPutServerHello = (
|
|
|
1284
1347
|
* Writes a ServerHello (§4.1.3) at `out[at]` and answers where it ends:
|
|
1285
1348
|
* choosing `suite` and TLS 1.3, echoing the session id
|
|
1286
1349
|
* `sessionId[sessionIdOff .. sessionIdOff + sessionIdLength)`, and carrying
|
|
1287
|
-
* the server's
|
|
1350
|
+
* the server's key share, secp256r1 when `serverShare` is a 65-byte point
|
|
1351
|
+
* and x25519 otherwise. The extensions are `key_share` then
|
|
1288
1352
|
* `supported_versions`, the order RFC 8448 §3 shows.
|
|
1289
1353
|
*
|
|
1290
1354
|
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
@@ -1294,7 +1358,8 @@ const tlsPutServerHello = (
|
|
|
1294
1358
|
* @param sessionIdOff Where the session id starts in `sessionId`.
|
|
1295
1359
|
* @param sessionIdLength How many bytes the session id is, 0 to 32.
|
|
1296
1360
|
* @param suite The cipher suite the server chose, such as `nish/net/tls/schedule`'s `TLS_AES_128_GCM_SHA256`.
|
|
1297
|
-
* @param
|
|
1361
|
+
* @param serverShare The server's key share, its `key_exchange`: 32 bytes of x25519, or a
|
|
1362
|
+
* 65-byte uncompressed secp256r1 point.
|
|
1298
1363
|
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1299
1364
|
*/
|
|
1300
1365
|
export const tlsWriteServerHello = (
|
|
@@ -1305,9 +1370,8 @@ export const tlsWriteServerHello = (
|
|
|
1305
1370
|
sessionIdOff: i32,
|
|
1306
1371
|
sessionIdLength: i32,
|
|
1307
1372
|
suite: i32,
|
|
1308
|
-
|
|
1309
|
-
): i32 =>
|
|
1310
|
-
tlsPutServerHello(out, at, random, sessionId, sessionIdOff, sessionIdLength, suite, x25519Public, -1)
|
|
1373
|
+
serverShare: u8[]
|
|
1374
|
+
): i32 => tlsPutServerHello(out, at, random, sessionId, sessionIdOff, sessionIdLength, suite, serverShare, -1)
|
|
1311
1375
|
|
|
1312
1376
|
/**
|
|
1313
1377
|
* Writes the ServerHello of a resumed handshake (§4.1.3, §4.2.11) at
|
|
@@ -1323,7 +1387,8 @@ export const tlsWriteServerHello = (
|
|
|
1323
1387
|
* @param sessionIdOff Where the session id starts in `sessionId`.
|
|
1324
1388
|
* @param sessionIdLength How many bytes the session id is, 0 to 32.
|
|
1325
1389
|
* @param suite The cipher suite the server chose.
|
|
1326
|
-
* @param
|
|
1390
|
+
* @param serverShare The server's key share, its `key_exchange`: 32 bytes of x25519, or a
|
|
1391
|
+
* 65-byte uncompressed secp256r1 point.
|
|
1327
1392
|
* @param selectedIdentity The index, from 0, of the client's PSK identity the server accepts; only its low
|
|
1328
1393
|
* 16 bits are written.
|
|
1329
1394
|
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
@@ -1336,7 +1401,7 @@ export const tlsWriteServerHelloPsk = (
|
|
|
1336
1401
|
sessionIdOff: i32,
|
|
1337
1402
|
sessionIdLength: i32,
|
|
1338
1403
|
suite: i32,
|
|
1339
|
-
|
|
1404
|
+
serverShare: u8[],
|
|
1340
1405
|
selectedIdentity: i32
|
|
1341
1406
|
): i32 =>
|
|
1342
1407
|
tlsPutServerHello(
|
|
@@ -1347,7 +1412,7 @@ export const tlsWriteServerHelloPsk = (
|
|
|
1347
1412
|
sessionIdOff,
|
|
1348
1413
|
sessionIdLength,
|
|
1349
1414
|
suite,
|
|
1350
|
-
|
|
1415
|
+
serverShare,
|
|
1351
1416
|
selectedIdentity & 0xffff
|
|
1352
1417
|
)
|
|
1353
1418
|
|
|
@@ -1392,8 +1457,10 @@ export const tlsWriteHelloRetryRequest = (
|
|
|
1392
1457
|
* `extra` is extensions the caller encodes itself, written first and as they
|
|
1393
1458
|
* are; then the chosen ALPN protocol when `alpn` is not empty (RFC 7301 §3.1,
|
|
1394
1459
|
* a list of exactly one), an empty `server_name` when `acknowledgeServerName`
|
|
1395
|
-
* (RFC 6066 §3's acknowledgement),
|
|
1396
|
-
* `quic_transport_parameters` when `quicTransportParameters` is not `null
|
|
1460
|
+
* (RFC 6066 §3's acknowledgement), the server's
|
|
1461
|
+
* `quic_transport_parameters` when `quicTransportParameters` is not `null`,
|
|
1462
|
+
* and the server's own `record_size_limit` (RFC 8449 §4) when
|
|
1463
|
+
* `recordSizeLimit` is not 0.
|
|
1397
1464
|
*
|
|
1398
1465
|
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1399
1466
|
* @param at Where the message, handshake header first, starts in `out`.
|
|
@@ -1401,6 +1468,7 @@ export const tlsWriteHelloRetryRequest = (
|
|
|
1401
1468
|
* @param alpn The protocol the server chose, one character a byte, at most 255; empty to send no ALPN.
|
|
1402
1469
|
* @param acknowledgeServerName Whether to send the empty `server_name` that says the server used the name.
|
|
1403
1470
|
* @param quicTransportParameters The server's QUIC transport parameters, opaque; `null` to send none.
|
|
1471
|
+
* @param recordSizeLimit The largest record plaintext the server takes, content type included, written as a `uint16`; 0 to send none.
|
|
1404
1472
|
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1405
1473
|
*/
|
|
1406
1474
|
export const tlsWriteEncryptedExtensions = (
|
|
@@ -1409,7 +1477,8 @@ export const tlsWriteEncryptedExtensions = (
|
|
|
1409
1477
|
extra: u8[],
|
|
1410
1478
|
alpn: string,
|
|
1411
1479
|
acknowledgeServerName: boolean,
|
|
1412
|
-
quicTransportParameters: u8[] | null
|
|
1480
|
+
quicTransportParameters: u8[] | null,
|
|
1481
|
+
recordSizeLimit: i32
|
|
1413
1482
|
): i32 => {
|
|
1414
1483
|
const extensions: i32 = at + 6
|
|
1415
1484
|
let p: i32 = tlsPutBytes(out, extensions, extra)
|
|
@@ -1432,6 +1501,9 @@ export const tlsWriteEncryptedExtensions = (
|
|
|
1432
1501
|
p = tlsPutBytes(out, body, quicTransportParameters)
|
|
1433
1502
|
tlsPutLength(out, body, p, 2)
|
|
1434
1503
|
}
|
|
1504
|
+
if (recordSizeLimit !== 0) {
|
|
1505
|
+
p = tlsPutU16(out, tlsPutU16(out, tlsPutU16(out, p, TLS_EXT_RECORD_SIZE_LIMIT), 2), recordSizeLimit)
|
|
1506
|
+
}
|
|
1435
1507
|
tlsPutLength(out, extensions, p, 2)
|
|
1436
1508
|
tlsPutHeader(out, at, TLS_HANDSHAKE_ENCRYPTED_EXTENSIONS, p)
|
|
1437
1509
|
return p
|
|
@@ -1581,10 +1653,11 @@ const tlsBytesString = (name: u8[]): string => tlsWindowString(name, TLS_CODEC_F
|
|
|
1581
1653
|
* @param random The server's 32-byte `random` (§4.1.3), written as it is.
|
|
1582
1654
|
* @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
|
|
1583
1655
|
* @param suite The cipher suite the server chose.
|
|
1584
|
-
* @param
|
|
1656
|
+
* @param serverShare The server's key share, its `key_exchange`: 32 bytes of x25519, or a
|
|
1657
|
+
* 65-byte uncompressed secp256r1 point.
|
|
1585
1658
|
* @returns The whole message, handshake header included.
|
|
1586
1659
|
*/
|
|
1587
|
-
export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32,
|
|
1660
|
+
export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32, serverShare: u8[]): u8[] => {
|
|
1588
1661
|
const none: u8[] = []
|
|
1589
1662
|
const sessionIdLength: i32 = toI32(sessionId.length)
|
|
1590
1663
|
const out: u8[] = new Array<u8>(
|
|
@@ -1596,7 +1669,7 @@ export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32,
|
|
|
1596
1669
|
TLS_CODEC_FROM,
|
|
1597
1670
|
sessionIdLength,
|
|
1598
1671
|
suite,
|
|
1599
|
-
|
|
1672
|
+
serverShare
|
|
1600
1673
|
)
|
|
1601
1674
|
)
|
|
1602
1675
|
tlsWriteServerHello(
|
|
@@ -1607,7 +1680,7 @@ export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32,
|
|
|
1607
1680
|
TLS_CODEC_FROM,
|
|
1608
1681
|
sessionIdLength,
|
|
1609
1682
|
suite,
|
|
1610
|
-
|
|
1683
|
+
serverShare
|
|
1611
1684
|
)
|
|
1612
1685
|
return out
|
|
1613
1686
|
}
|
|
@@ -1619,7 +1692,8 @@ export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32,
|
|
|
1619
1692
|
* @param random The server's 32-byte `random` (§4.1.3), written as it is.
|
|
1620
1693
|
* @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
|
|
1621
1694
|
* @param suite The cipher suite the server chose.
|
|
1622
|
-
* @param
|
|
1695
|
+
* @param serverShare The server's key share, its `key_exchange`: 32 bytes of x25519, or a
|
|
1696
|
+
* 65-byte uncompressed secp256r1 point.
|
|
1623
1697
|
* @param selectedIdentity The index, from 0, of the client's PSK identity the server accepts; only its low
|
|
1624
1698
|
* 16 bits are written.
|
|
1625
1699
|
* @returns The whole message, handshake header included.
|
|
@@ -1628,7 +1702,7 @@ export const tlsEncodeServerHelloPsk = (
|
|
|
1628
1702
|
random: u8[],
|
|
1629
1703
|
sessionId: u8[],
|
|
1630
1704
|
suite: i32,
|
|
1631
|
-
|
|
1705
|
+
serverShare: u8[],
|
|
1632
1706
|
selectedIdentity: i32
|
|
1633
1707
|
): u8[] => {
|
|
1634
1708
|
const none: u8[] = []
|
|
@@ -1642,7 +1716,7 @@ export const tlsEncodeServerHelloPsk = (
|
|
|
1642
1716
|
TLS_CODEC_FROM,
|
|
1643
1717
|
sessionIdLength,
|
|
1644
1718
|
suite,
|
|
1645
|
-
|
|
1719
|
+
serverShare,
|
|
1646
1720
|
selectedIdentity
|
|
1647
1721
|
)
|
|
1648
1722
|
)
|
|
@@ -1654,7 +1728,7 @@ export const tlsEncodeServerHelloPsk = (
|
|
|
1654
1728
|
TLS_CODEC_FROM,
|
|
1655
1729
|
sessionIdLength,
|
|
1656
1730
|
suite,
|
|
1657
|
-
|
|
1731
|
+
serverShare,
|
|
1658
1732
|
selectedIdentity
|
|
1659
1733
|
)
|
|
1660
1734
|
return out
|
|
@@ -1687,13 +1761,15 @@ export const tlsEncodeHelloRetryRequest = (sessionId: u8[], suite: i32, group: i
|
|
|
1687
1761
|
* @param alpn The protocol the server chose, as its bytes, at most 255; empty to send no ALPN.
|
|
1688
1762
|
* @param acknowledgeServerName Whether to send the empty `server_name` that says the server used the name.
|
|
1689
1763
|
* @param quicTransportParameters The server's QUIC transport parameters, opaque; `null` to send none.
|
|
1764
|
+
* @param recordSizeLimit The server's own `record_size_limit` (RFC 8449 §4); 0 to send none.
|
|
1690
1765
|
* @returns The whole message, handshake header included.
|
|
1691
1766
|
*/
|
|
1692
1767
|
export const tlsEncodeEncryptedExtensions = (
|
|
1693
1768
|
extra: u8[],
|
|
1694
1769
|
alpn: u8[],
|
|
1695
1770
|
acknowledgeServerName: boolean,
|
|
1696
|
-
quicTransportParameters: u8[] | null
|
|
1771
|
+
quicTransportParameters: u8[] | null,
|
|
1772
|
+
recordSizeLimit: i32
|
|
1697
1773
|
): u8[] => {
|
|
1698
1774
|
const none: u8[] = []
|
|
1699
1775
|
const protocol: string = tlsBytesString(alpn)
|
|
@@ -1704,7 +1780,8 @@ export const tlsEncodeEncryptedExtensions = (
|
|
|
1704
1780
|
extra,
|
|
1705
1781
|
protocol,
|
|
1706
1782
|
acknowledgeServerName,
|
|
1707
|
-
quicTransportParameters
|
|
1783
|
+
quicTransportParameters,
|
|
1784
|
+
recordSizeLimit
|
|
1708
1785
|
)
|
|
1709
1786
|
)
|
|
1710
1787
|
tlsWriteEncryptedExtensions(
|
|
@@ -1713,7 +1790,8 @@ export const tlsEncodeEncryptedExtensions = (
|
|
|
1713
1790
|
extra,
|
|
1714
1791
|
protocol,
|
|
1715
1792
|
acknowledgeServerName,
|
|
1716
|
-
quicTransportParameters
|
|
1793
|
+
quicTransportParameters,
|
|
1794
|
+
recordSizeLimit
|
|
1717
1795
|
)
|
|
1718
1796
|
return out
|
|
1719
1797
|
}
|
|
@@ -308,6 +308,13 @@ const tlsAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]>): bo
|
|
|
308
308
|
* sequence numbers. `padding` is how many zero bytes each protected record
|
|
309
309
|
* the server writes carries after its content type (§5.4), 0 unless the
|
|
310
310
|
* caller sets it.
|
|
311
|
+
*
|
|
312
|
+
* A client that sent `record_size_limit` (RFC 8449) gets no protected record
|
|
313
|
+
* whose plaintext, content type and padding included, passes it: `write` and
|
|
314
|
+
* the handshake flight are cut into records of at most the limit less one
|
|
315
|
+
* less `padding`, and a padding that leaves no room fails the connection with
|
|
316
|
+
* `internal_error` as it does under 2^14. Records in the clear (ServerHello,
|
|
317
|
+
* HelloRetryRequest, the compatibility `change_cipher_spec`) are not limited.
|
|
311
318
|
*/
|
|
312
319
|
export class TlsRecordServer {
|
|
313
320
|
/** The handshake underneath: read it for the suite, ALPN, server name and exporter secret. */
|
|
@@ -1079,14 +1086,29 @@ export class TlsRecordServer {
|
|
|
1079
1086
|
return this.outputEnd + need <= capacity
|
|
1080
1087
|
}
|
|
1081
1088
|
|
|
1089
|
+
/**
|
|
1090
|
+
* The most content and padding together one record may carry under the
|
|
1091
|
+
* write protection as it stands: 2^14 bytes (§5.4), and under protection
|
|
1092
|
+
* one less than the client's `record_size_limit` when it sent one, since
|
|
1093
|
+
* that limit counts the content-type byte too (RFC 8449 §4). Records in
|
|
1094
|
+
* the clear are not limited by it.
|
|
1095
|
+
*/
|
|
1096
|
+
private writeCap(): i32 {
|
|
1097
|
+
const limit: i32 = this.tls.peerRecordSizeLimit
|
|
1098
|
+
if (limit === 0 || !this.writeProtection.isProtected() || limit - 1 > TLS_MAX_PLAINTEXT) {
|
|
1099
|
+
return TLS_MAX_PLAINTEXT
|
|
1100
|
+
}
|
|
1101
|
+
return limit - 1
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1082
1104
|
/**
|
|
1083
1105
|
* The most content one record may carry under the write protection as it
|
|
1084
|
-
* stands:
|
|
1085
|
-
* the content, its type and the padding together may not pass
|
|
1086
|
-
*
|
|
1106
|
+
* stands: `writeCap()`, less the padding each protected record carries,
|
|
1107
|
+
* since the content, its type and the padding together may not pass it.
|
|
1108
|
+
* Below 1 when the padding leaves no room at all.
|
|
1087
1109
|
*/
|
|
1088
1110
|
private maxChunk(): i32 {
|
|
1089
|
-
return
|
|
1111
|
+
return this.writeCap() - this.writePadding()
|
|
1090
1112
|
}
|
|
1091
1113
|
|
|
1092
1114
|
/** The padding the next record carries: `padding` once records are protected, none in the clear. */
|
|
@@ -1105,16 +1127,24 @@ export class TlsRecordServer {
|
|
|
1105
1127
|
/**
|
|
1106
1128
|
* Seals one record of `type` from `data[off .. off + len)` onto `output`
|
|
1107
1129
|
* under the write protection, padded when it is protected. False, writing
|
|
1108
|
-
* nothing, when it does not fit;
|
|
1109
|
-
*
|
|
1130
|
+
* nothing, when it does not fit; content and padding past `writeCap()` (the
|
|
1131
|
+
* client's `record_size_limit`, or 2^14) and a refusal from `seal` itself
|
|
1132
|
+
* fail the connection with `internal_error`.
|
|
1110
1133
|
*
|
|
1111
1134
|
* @param type The content type to send, such as `TLS_CONTENT_HANDSHAKE`.
|
|
1112
1135
|
* @param data The array that holds the content.
|
|
1113
1136
|
* @param off Where in `data` the content starts.
|
|
1114
1137
|
* @param len How many bytes of content, from `off`, to seal into this one record.
|
|
1115
|
-
* @returns `true` when the record was added to `output`; `false` when it did not fit or `seal` refused it.
|
|
1138
|
+
* @returns `true` when the record was added to `output`; `false` when it did not fit, passed the cap or `seal` refused it.
|
|
1116
1139
|
*/
|
|
1117
1140
|
sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
1141
|
+
// A record the cap cannot carry, such as an alert under a padding that
|
|
1142
|
+
// fills it, fails the connection rather than pass the client's limit; the
|
|
1143
|
+
// alert that failure seals is refused here in turn, and not sent.
|
|
1144
|
+
if (len + this.writePadding() > this.writeCap()) {
|
|
1145
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1146
|
+
return false
|
|
1147
|
+
}
|
|
1118
1148
|
const room: i32 = this.recordRoom(len)
|
|
1119
1149
|
if (!this.outputHasRoom(room)) {
|
|
1120
1150
|
return false
|
package/std/net/tls/verify.ts
CHANGED
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
* serverAuth, name constraints — and that the leaf's subjectAltName names
|
|
28
28
|
* the host dialled. Its refusal becomes the alert: `certificate_expired` for
|
|
29
29
|
* a certificate outside its validity, `unknown_ca` when no path reaches a
|
|
30
|
-
* root of the store, and `bad_certificate` for every other reason
|
|
30
|
+
* root of the store, and `bad_certificate` for every other reason, a key
|
|
31
|
+
* distrusted with `tlsClientDistrustSpki` among them. There is
|
|
31
32
|
* no switch that skips the step and none is to be added: a client that is to
|
|
32
33
|
* talk to a server it cannot authenticate is given a store holding its
|
|
33
34
|
* issuing root, never a way around the check (`docs/security/tls.md`, "The
|
|
@@ -47,6 +48,7 @@ import {
|
|
|
47
48
|
PKI_OK,
|
|
48
49
|
PkiResult,
|
|
49
50
|
PkiTrustStore,
|
|
51
|
+
pkiDistrustSpki,
|
|
50
52
|
pkiTrustStore,
|
|
51
53
|
pkiVerifyServer,
|
|
52
54
|
} from "nish/crypto/pki"
|
|
@@ -135,6 +137,22 @@ export const tlsClientTrust = (roots: PkiRoots): TlsClientTrust => new TlsClient
|
|
|
135
137
|
*/
|
|
136
138
|
export const tlsClientSystemTrust = (): TlsClientTrust => tlsClientTrust(pkiLoadSystemRoots())
|
|
137
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Distrusts a key in `trust`: every connection made with it from now on
|
|
142
|
+
* refuses, with `bad_certificate`, a server whose path holds a certificate
|
|
143
|
+
* (leaf, intermediate or root) whose subjectPublicKeyInfo DER hashes to
|
|
144
|
+
* `spkiSha256` under SHA-256 (`pkiDistrustSpki`). It is how a program stops
|
|
145
|
+
* trusting a CA its roots still hold, or a key known to be compromised,
|
|
146
|
+
* before the system bundle drops it. A trust distrusts nothing until this is
|
|
147
|
+
* called.
|
|
148
|
+
*
|
|
149
|
+
* @param trust The trust to change; every connection that shares it sees the change.
|
|
150
|
+
* @param spkiSha256 The SHA-256 of the subjectPublicKeyInfo DER, 32 bytes.
|
|
151
|
+
* @returns Whether the hash was added: false, and nothing added, unless it is 32 bytes long.
|
|
152
|
+
*/
|
|
153
|
+
export const tlsClientDistrustSpki = (trust: TlsClientTrust, spkiSha256: u8[]): boolean =>
|
|
154
|
+
pkiDistrustSpki(trust.store, spkiSha256)
|
|
155
|
+
|
|
138
156
|
/**
|
|
139
157
|
* Whether `signature`, as a CertificateVerify carries it under `scheme`,
|
|
140
158
|
* verifies over `content` under `leaf`'s key. `false` for a scheme that
|
package/std/net/tls-tcp.ts
CHANGED
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
* A connection that failed, whose client sent its own `close_notify` (it writes
|
|
97
97
|
* nothing after it, RFC 8446 §6.1) or whose client's stream had already ended,
|
|
98
98
|
* is done at once. A client that never hangs up keeps its slot, as one that
|
|
99
|
-
* never sends does
|
|
99
|
+
* never sends does, until `expire` finds it idle past `idleTimeout`
|
|
100
100
|
* (`docs/security/tls.md`, TLS-6 and TLS-4).
|
|
101
101
|
*
|
|
102
102
|
* Written from RFC 8446 and docs/LANGUAGE.md's `nish:net`, not ported from
|
|
@@ -208,6 +208,12 @@ export class TlsTcpServer {
|
|
|
208
208
|
* client's end of stream, then `TLS_TCP_LINGERED` once it is done.
|
|
209
209
|
*/
|
|
210
210
|
private lingered: i32[]
|
|
211
|
+
/**
|
|
212
|
+
* Each slot's last activity beside `fds`, in `monotonicNanos()`: its
|
|
213
|
+
* accept, and each time its socket was readable or writable while it was
|
|
214
|
+
* not lingering. `expire` judges `idleTimeout` against it.
|
|
215
|
+
*/
|
|
216
|
+
private lastActive: i64[]
|
|
211
217
|
/**
|
|
212
218
|
* The slots closed since they were used, a stack whose top `accept` takes
|
|
213
219
|
* before it builds a slot past the high-water mark, so finding one costs
|
|
@@ -249,6 +255,15 @@ export class TlsTcpServer {
|
|
|
249
255
|
pools: TlsRecordPools
|
|
250
256
|
/** The listening socket, which the program made with `tcpListen` and still owns. */
|
|
251
257
|
private listener: i32 = -1
|
|
258
|
+
/**
|
|
259
|
+
* How long, in milliseconds, a slot may go without activity before
|
|
260
|
+
* `expire` closes it: a client that opens a connection and sends nothing,
|
|
261
|
+
* stalls its handshake, or never hangs up after the close holds its slot
|
|
262
|
+
* this long and no longer (`docs/security/tls.md`, TLS-4). 60 seconds by
|
|
263
|
+
* default, as `nish/net/http1-server`'s `idleTimeout`; 0 or less turns the
|
|
264
|
+
* timeout off.
|
|
265
|
+
*/
|
|
266
|
+
idleTimeout: i32 = 60000
|
|
252
267
|
|
|
253
268
|
/**
|
|
254
269
|
* A pool of `size` slots, at least one, serving `config` on `listener`, a
|
|
@@ -268,6 +283,7 @@ export class TlsTcpServer {
|
|
|
268
283
|
this.connections = []
|
|
269
284
|
this.fds = []
|
|
270
285
|
this.lingered = []
|
|
286
|
+
this.lastActive = []
|
|
271
287
|
this.free = []
|
|
272
288
|
this.randoms = []
|
|
273
289
|
this.slots = size < 1 ? 1 : size
|
|
@@ -420,6 +436,7 @@ export class TlsTcpServer {
|
|
|
420
436
|
slot = toI32(this.fds.length)
|
|
421
437
|
this.fds.push(TLS_TCP_FREE)
|
|
422
438
|
this.lingered.push(TLS_TCP_LIVE)
|
|
439
|
+
this.lastActive.push(toI64(0))
|
|
423
440
|
}
|
|
424
441
|
if (slot < 0 || slot >= toI32(this.fds.length)) {
|
|
425
442
|
netClose(fd)
|
|
@@ -429,6 +446,7 @@ export class TlsTcpServer {
|
|
|
429
446
|
slot < toI32(this.connections.length) ? this.connections[slot] : this.build()
|
|
430
447
|
this.fds[slot] = fd
|
|
431
448
|
this.lingered[slot] = TLS_TCP_LIVE
|
|
449
|
+
this.lastActive[slot] = monotonicNanos()
|
|
432
450
|
const random: u8[] = this.randoms[slot]
|
|
433
451
|
for (
|
|
434
452
|
let k: i32 = 0;
|
|
@@ -460,6 +478,7 @@ export class TlsTcpServer {
|
|
|
460
478
|
if (this.lingered[slot] !== TLS_TCP_LIVE) {
|
|
461
479
|
return this.drain(slot)
|
|
462
480
|
}
|
|
481
|
+
this.lastActive[slot] = monotonicNanos()
|
|
463
482
|
const fd: i32 = this.fds[slot]
|
|
464
483
|
const conn: TlsRecordServer = this.connections[slot]
|
|
465
484
|
while (true) {
|
|
@@ -492,6 +511,11 @@ export class TlsTcpServer {
|
|
|
492
511
|
if (!this.holds(slot)) {
|
|
493
512
|
return TLS_RECORD_DONE
|
|
494
513
|
}
|
|
514
|
+
// What a lingering slot drains is not activity, so `idleTimeout` bounds
|
|
515
|
+
// how long a client that never hangs up can hold it.
|
|
516
|
+
if (this.lingered[slot] === TLS_TCP_LIVE) {
|
|
517
|
+
this.lastActive[slot] = monotonicNanos()
|
|
518
|
+
}
|
|
495
519
|
return this.flush(slot)
|
|
496
520
|
}
|
|
497
521
|
|
|
@@ -831,10 +855,41 @@ export class TlsTcpServer {
|
|
|
831
855
|
this.fds[slot] = TLS_TCP_FREE
|
|
832
856
|
this.lingered[slot] = TLS_TCP_LIVE
|
|
833
857
|
this.free.push(slot)
|
|
858
|
+
this.lastActive[slot] = toI64(0)
|
|
834
859
|
conn.wipeKeys()
|
|
835
860
|
// Whatever the last flush could not send is dropped with its buffer.
|
|
836
861
|
conn.release()
|
|
837
862
|
}
|
|
863
|
+
|
|
864
|
+
/**
|
|
865
|
+
* Closes, as `close` does, every slot that has had no activity for longer
|
|
866
|
+
* than `idleTimeout` at `now`, and answers how many it closed. The pool
|
|
867
|
+
* reads no clock of its own for this: a program calls it from its loop,
|
|
868
|
+
* between `pollWait`s whose timeout is at most `idleTimeout`, so a slot
|
|
869
|
+
* idles out within about twice the timeout. Call it once a batch of events
|
|
870
|
+
* from `pollWait` has been dispatched, not in the middle of one: an event
|
|
871
|
+
* already taken for a slot this closes would otherwise be read as the next
|
|
872
|
+
* connection's, once `accept` reuses the slot. It is an idle limit, not a
|
|
873
|
+
* handshake deadline: a client that sends a byte inside every
|
|
874
|
+
* `idleTimeout` keeps its slot.
|
|
875
|
+
*
|
|
876
|
+
* @param now The time now, in `monotonicNanos()`, which each slot's last activity is judged against.
|
|
877
|
+
* @returns How many slots this call closed; 0 while `idleTimeout` is 0 or less.
|
|
878
|
+
*/
|
|
879
|
+
expire(now: i64): i32 {
|
|
880
|
+
if (this.idleTimeout <= 0) {
|
|
881
|
+
return 0
|
|
882
|
+
}
|
|
883
|
+
const limit: i64 = toI64(this.idleTimeout) * TLS_TCP_NANOS_PER_MS
|
|
884
|
+
let n: i32 = 0
|
|
885
|
+
for (let k: i32 = 0; k < toI32(this.fds.length); k++) {
|
|
886
|
+
if (this.holds(k) && now - this.lastActive[k] > limit) {
|
|
887
|
+
this.close(k)
|
|
888
|
+
n = n + 1
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
return n
|
|
892
|
+
}
|
|
838
893
|
}
|
|
839
894
|
|
|
840
895
|
/**
|