@amritk/nish-aarch64-linux 0.35.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
|
@@ -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
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* server
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* the
|
|
32
|
-
*
|
|
33
|
-
*
|
|
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
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* `BufferPool.giveSecret` when it ends (`start`, `wipe`, `release`), so a
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* `TlsServer`
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
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).
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* the
|
|
69
|
-
*
|
|
70
|
-
*
|
|
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.
|
|
@@ -169,9 +176,10 @@ const TLS_RECORD_PLAIN_SIZE: i32 = 2 * TLS_MAX_PLAINTEXT
|
|
|
169
176
|
/** Where a record's content is opened to before it is handed on: the most content a record carries. */
|
|
170
177
|
const TLS_RECORD_SCRATCH_SIZE: i32 = TLS_MAX_PLAINTEXT
|
|
171
178
|
/**
|
|
172
|
-
* The four pools a connection's record buffers are borrowed from: `output
|
|
173
|
-
*
|
|
174
|
-
*
|
|
179
|
+
* The four pools a connection's record buffers are borrowed from: `output`
|
|
180
|
+
* and `plain` for `TlsRecordServer`'s fields of those names, `scratch` for the
|
|
181
|
+
* buffer it opens a record's content into, and `reader` for its record
|
|
182
|
+
* reader's buffer. A carrier with many slots makes one set
|
|
175
183
|
* with a ceiling of its slot count and points every slot at it with
|
|
176
184
|
* `TlsRecordServer.borrowFrom`, so a full server holds what preallocating
|
|
177
185
|
* every slot would have held and an idle one holds only what earlier
|
|
@@ -179,12 +187,20 @@ const TLS_RECORD_SCRATCH_SIZE: i32 = TLS_MAX_PLAINTEXT
|
|
|
179
187
|
* (`BufferPool.giveSecret`): it held plaintext, or handshake bytes.
|
|
180
188
|
*/
|
|
181
189
|
export class TlsRecordPools {
|
|
190
|
+
/** Lends each slot its `output` buffer, where sealed records wait to be sent. */
|
|
182
191
|
output: BufferPool
|
|
192
|
+
/** Lends each slot its `plain` buffer, where opened application data waits to be read. */
|
|
183
193
|
plain: BufferPool
|
|
194
|
+
/** Lends each slot the buffer a record's content is opened into. */
|
|
184
195
|
scratch: BufferPool
|
|
196
|
+
/** Lends each slot its record reader's buffer. */
|
|
185
197
|
reader: BufferPool
|
|
186
198
|
|
|
187
|
-
/**
|
|
199
|
+
/**
|
|
200
|
+
* Four empty pools of `ceiling` buffers each; nothing is made until a slot needs it.
|
|
201
|
+
*
|
|
202
|
+
* @param ceiling How many buffers each pool may lend at once: the carrier's slot count.
|
|
203
|
+
*/
|
|
188
204
|
constructor(ceiling: i32) {
|
|
189
205
|
this.output = new BufferPool("TlsRecordServer.output", TLS_RECORD_OUTPUT_SIZE, ceiling)
|
|
190
206
|
this.plain = new BufferPool("TlsRecordServer.plain", TLS_RECORD_PLAIN_SIZE, ceiling)
|
|
@@ -192,12 +208,20 @@ export class TlsRecordPools {
|
|
|
192
208
|
this.reader = new BufferPool("TlsRecordReader.buffer", TLS_MAX_RECORD, ceiling)
|
|
193
209
|
}
|
|
194
210
|
|
|
195
|
-
/**
|
|
211
|
+
/**
|
|
212
|
+
* How many buffers of the four pools are lent now: 0 once every connection has given its back.
|
|
213
|
+
*
|
|
214
|
+
* @returns The buffers lent and not yet given back, summed over the four pools.
|
|
215
|
+
*/
|
|
196
216
|
lent(): i32 {
|
|
197
217
|
return this.output.lent() + this.plain.lent() + this.scratch.lent() + this.reader.lent()
|
|
198
218
|
}
|
|
199
219
|
|
|
200
|
-
/**
|
|
220
|
+
/**
|
|
221
|
+
* How many buffers the four pools have made: their high-water mark together.
|
|
222
|
+
*
|
|
223
|
+
* @returns The buffers ever made, summed over the four pools.
|
|
224
|
+
*/
|
|
201
225
|
made(): i32 {
|
|
202
226
|
return this.output.made + this.plain.made + this.scratch.made + this.reader.made
|
|
203
227
|
}
|
|
@@ -286,76 +310,104 @@ const tlsAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]>): bo
|
|
|
286
310
|
* caller sets it.
|
|
287
311
|
*/
|
|
288
312
|
export class TlsRecordServer {
|
|
313
|
+
/** The handshake underneath: read it for the suite, ALPN, server name and exporter secret. */
|
|
289
314
|
tls: TlsServer
|
|
290
|
-
reader: TlsRecordReader
|
|
315
|
+
private reader: TlsRecordReader
|
|
316
|
+
/** Opens the client's records; its sequence number counts the records read at the current keys. */
|
|
291
317
|
readProtection: TlsRecordProtection
|
|
318
|
+
/** Seals the server's records; its sequence number counts the records written at the current keys. */
|
|
292
319
|
writeProtection: TlsRecordProtection
|
|
293
320
|
/** Bytes to send are `output[outputStart .. outputEnd)`; `empty` until a record is sealed. */
|
|
294
321
|
output: u8[]
|
|
295
322
|
/** Application data the caller has not read is `plain[plainStart .. plainEnd)`; `empty` until a record brings some. */
|
|
296
323
|
plain: u8[]
|
|
297
324
|
/** Where a record is opened to; `empty` until the first protected record. */
|
|
298
|
-
scratch: u8[]
|
|
325
|
+
private scratch: u8[]
|
|
299
326
|
/** The zero-length array a buffer field is while it holds no borrowed buffer. */
|
|
300
|
-
empty: u8[]
|
|
327
|
+
private empty: u8[]
|
|
301
328
|
/** Where `output`, `plain`, `scratch` and the reader's buffer are borrowed from. */
|
|
302
|
-
pools: TlsRecordPools
|
|
329
|
+
private pools: TlsRecordPools
|
|
303
330
|
/**
|
|
304
331
|
* The client's and the server's current application traffic secrets, for
|
|
305
332
|
* KeyUpdate, each held at both hash lengths: `readHash` and `writeHash`
|
|
306
333
|
* say which of a pair holds the secret, and are 0 before it is known.
|
|
307
334
|
*/
|
|
308
335
|
readonly readTraffic256: Secret<u8[]>
|
|
336
|
+
/** The client's current application traffic secret when the hash is SHA-384, 48 bytes. */
|
|
309
337
|
readonly readTraffic384: Secret<u8[]>
|
|
338
|
+
/** The server's current application traffic secret when the hash is SHA-256, 32 bytes. */
|
|
310
339
|
readonly writeTraffic256: Secret<u8[]>
|
|
340
|
+
/** The server's current application traffic secret when the hash is SHA-384, 48 bytes. */
|
|
311
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. */
|
|
312
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. */
|
|
313
345
|
writeHash: i32 = 0
|
|
314
346
|
/** A post-handshake message the client has sent part of. */
|
|
315
|
-
postHandshake: u8[]
|
|
347
|
+
private postHandshake: u8[]
|
|
348
|
+
/** Where the connection stands: one of the `TLS_RECORD_STATE_*` constants. */
|
|
316
349
|
state: i32 = 0
|
|
350
|
+
/** The alert this server sent, or 0. */
|
|
317
351
|
alert: i32 = 0
|
|
352
|
+
/** The error alert the client sent, or -1 when it sent none. */
|
|
318
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. */
|
|
319
355
|
padding: i32 = 0
|
|
320
356
|
/** The `TLS_LEVEL_*` the client's records are read at: Initial in the clear, then Handshake, then Application. */
|
|
321
|
-
readLevel: i32 = 0
|
|
357
|
+
private readLevel: i32 = 0
|
|
322
358
|
/** The `TLS_LEVEL_*` the server's records are written at. */
|
|
323
359
|
writeLevel: i32 = 0
|
|
360
|
+
/** The index in `output` of the first byte still to send; `consume(sent)` moves it on. */
|
|
324
361
|
outputStart: i32 = 0
|
|
362
|
+
/** One past the last byte still to send in `output`; `outputStart` when nothing is pending. */
|
|
325
363
|
outputEnd: i32 = 0
|
|
326
364
|
/** How far into `output` this borrowing has written, which says whether its give-back zeroes. */
|
|
327
|
-
outputUsed: i32 = 0
|
|
365
|
+
private outputUsed: i32 = 0
|
|
366
|
+
/** The index in `plain` of the first byte of application data the caller has not read. */
|
|
328
367
|
plainStart: i32 = 0
|
|
368
|
+
/** One past the last unread byte in `plain`; `plainStart` when none is pending. */
|
|
329
369
|
plainEnd: i32 = 0
|
|
330
370
|
/** How far into `plain` this borrowing has written. */
|
|
331
|
-
plainUsed: i32 = 0
|
|
371
|
+
private plainUsed: i32 = 0
|
|
332
372
|
/** How much of `TlsServer`'s Initial-level output is already in records. */
|
|
333
|
-
pendingInitialAt: i32 = 0
|
|
373
|
+
private pendingInitialAt: i32 = 0
|
|
334
374
|
/** How much of `TlsServer`'s Handshake-level output is already in records. */
|
|
335
|
-
pendingHandshakeAt: i32 = 0
|
|
375
|
+
private pendingHandshakeAt: i32 = 0
|
|
336
376
|
/** How much of `TlsServer`'s Application-level output, a NewSessionTicket, is already in records. */
|
|
337
|
-
pendingApplicationAt: i32 = 0
|
|
338
|
-
postHandshakeLength: i32 = 0
|
|
377
|
+
private pendingApplicationAt: i32 = 0
|
|
378
|
+
private postHandshakeLength: i32 = 0
|
|
339
379
|
/** Records in a row that carried nothing for the caller, against `TLS_RECORD_IDLE_LIMIT`. */
|
|
340
|
-
idleRecords: i32 = 0
|
|
380
|
+
private idleRecords: i32 = 0
|
|
341
381
|
/** KeyUpdates taken from the client, against `TLS_RECORD_MAX_KEY_UPDATES`. */
|
|
342
382
|
keyUpdates: i32 = 0
|
|
343
383
|
/** The server's flight is signed and finished, so its Handshake-level output may be sealed. */
|
|
344
|
-
flightReady: boolean = false
|
|
384
|
+
private flightReady: boolean = false
|
|
385
|
+
/** True once the client's `close_notify` has arrived. */
|
|
345
386
|
peerClosed: boolean = false
|
|
346
|
-
/**
|
|
387
|
+
/**
|
|
388
|
+
* The client's TCP stream ended. Without a `close_notify` before it, that is truncation and the connection failed.
|
|
389
|
+
*
|
|
390
|
+
* @internal Read by `net/tls-tcp.ts`.
|
|
391
|
+
*/
|
|
347
392
|
ended: boolean = false
|
|
348
393
|
/** Whether the server's first handshake message has been seen, which decides the compatibility record. */
|
|
349
|
-
firstMessageSeen: boolean = false
|
|
394
|
+
private firstMessageSeen: boolean = false
|
|
350
395
|
/** The compatibility `change_cipher_spec` is owed, after the first handshake message (§D.4). */
|
|
351
|
-
ccsDue: boolean = false
|
|
396
|
+
private ccsDue: boolean = false
|
|
352
397
|
/** The client's first handshake byte has arrived, which opens the window its `change_cipher_spec` may come in. */
|
|
353
|
-
started: boolean = false
|
|
398
|
+
private started: boolean = false
|
|
354
399
|
/** The server owes a KeyUpdate before its next application data (§4.6.3). */
|
|
355
|
-
keyUpdateDue: boolean = false
|
|
400
|
+
private keyUpdateDue: boolean = false
|
|
356
401
|
/** The server has closed and its `close_notify` waits for room in `output`. */
|
|
357
402
|
closeDue: boolean = false
|
|
358
403
|
|
|
404
|
+
/**
|
|
405
|
+
* Makes a slot for `tls` with a pool set of its own, one buffer of each
|
|
406
|
+
* kind, and starts the connection as `start` does. A carrier that shares
|
|
407
|
+
* its pools calls `borrowFrom` right after.
|
|
408
|
+
*
|
|
409
|
+
* @param tls The handshake this slot carries, fresh or just restarted.
|
|
410
|
+
*/
|
|
359
411
|
constructor(tls: TlsServer) {
|
|
360
412
|
this.tls = tls
|
|
361
413
|
this.reader = new TlsRecordReader()
|
|
@@ -384,6 +436,8 @@ export class TlsRecordServer {
|
|
|
384
436
|
* `tls` that has already failed — a configuration
|
|
385
437
|
* or randomness `TlsServer` refused — fails this connection at once, with
|
|
386
438
|
* its alert queued in the clear.
|
|
439
|
+
*
|
|
440
|
+
* @param tls The handshake the new connection runs, fresh or just restarted.
|
|
387
441
|
*/
|
|
388
442
|
start(tls: TlsServer): void {
|
|
389
443
|
// The last connection's traffic keys. Its ephemeral key was wiped when it
|
|
@@ -417,6 +471,8 @@ export class TlsRecordServer {
|
|
|
417
471
|
* slots share. Whatever is held from the old pools is given back first,
|
|
418
472
|
* zeroed, and dropped with it, so call it on a slot with nothing in flight:
|
|
419
473
|
* the carrier does, right after the constructor.
|
|
474
|
+
*
|
|
475
|
+
* @param pools The carrier's pool set, shared by all of its slots.
|
|
420
476
|
*/
|
|
421
477
|
borrowFrom(pools: TlsRecordPools): void {
|
|
422
478
|
this.release()
|
|
@@ -446,7 +502,7 @@ export class TlsRecordServer {
|
|
|
446
502
|
* reads zeros. Not called between records, since `giveSecret` zeroes the
|
|
447
503
|
* whole buffer however little of it a record used.
|
|
448
504
|
*/
|
|
449
|
-
giveBackIdle(): void {
|
|
505
|
+
private giveBackIdle(): void {
|
|
450
506
|
if (this.output.length > 0 && this.outputStart === this.outputEnd) {
|
|
451
507
|
this.pools.output.giveSecret(this.output, this.outputUsed)
|
|
452
508
|
this.output = this.empty
|
|
@@ -465,7 +521,7 @@ export class TlsRecordServer {
|
|
|
465
521
|
}
|
|
466
522
|
|
|
467
523
|
/** Forgets the application data the caller has not read. */
|
|
468
|
-
dropPlain(): void {
|
|
524
|
+
private dropPlain(): void {
|
|
469
525
|
this.plainStart = 0
|
|
470
526
|
this.plainEnd = 0
|
|
471
527
|
}
|
|
@@ -501,13 +557,13 @@ export class TlsRecordServer {
|
|
|
501
557
|
}
|
|
502
558
|
|
|
503
559
|
/** Wipes the two application traffic secrets and both directions' keys. */
|
|
504
|
-
wipeTraffic(): void {
|
|
560
|
+
private wipeTraffic(): void {
|
|
505
561
|
this.wipeRead()
|
|
506
562
|
this.wipeWrite()
|
|
507
563
|
}
|
|
508
564
|
|
|
509
565
|
/** Wipes the client's application traffic secret and the read keys. */
|
|
510
|
-
wipeRead(): void {
|
|
566
|
+
private wipeRead(): void {
|
|
511
567
|
// Whichever of the pair held the secret, both are zeroed.
|
|
512
568
|
this.wipeReadSecret()
|
|
513
569
|
this.readHash = 0
|
|
@@ -515,20 +571,20 @@ export class TlsRecordServer {
|
|
|
515
571
|
}
|
|
516
572
|
|
|
517
573
|
/** Wipes the server's application traffic secret and its write keys. */
|
|
518
|
-
wipeWrite(): void {
|
|
574
|
+
private wipeWrite(): void {
|
|
519
575
|
this.wipeWriteSecret()
|
|
520
576
|
this.writeHash = 0
|
|
521
577
|
this.writeProtection.clear()
|
|
522
578
|
}
|
|
523
579
|
|
|
524
580
|
/** Zeroes both of the client's traffic secret fields. */
|
|
525
|
-
wipeReadSecret(): void {
|
|
581
|
+
private wipeReadSecret(): void {
|
|
526
582
|
exposeWith(this.readTraffic256, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
|
|
527
583
|
exposeWith(this.readTraffic384, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
|
|
528
584
|
}
|
|
529
585
|
|
|
530
586
|
/** Zeroes both of the server's traffic secret fields. */
|
|
531
|
-
wipeWriteSecret(): void {
|
|
587
|
+
private wipeWriteSecret(): void {
|
|
532
588
|
exposeWith(this.writeTraffic256, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
|
|
533
589
|
exposeWith(this.writeTraffic384, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
|
|
534
590
|
}
|
|
@@ -539,7 +595,7 @@ export class TlsRecordServer {
|
|
|
539
595
|
* read keys from it. False when the secret is not known or the keys do not
|
|
540
596
|
* install.
|
|
541
597
|
*/
|
|
542
|
-
keepRead(): boolean {
|
|
598
|
+
private keepRead(): boolean {
|
|
543
599
|
const tls: TlsServer = this.tls
|
|
544
600
|
if (!tls.readKnown(TLS_LEVEL_APPLICATION)) {
|
|
545
601
|
return false
|
|
@@ -560,7 +616,7 @@ export class TlsRecordServer {
|
|
|
560
616
|
}
|
|
561
617
|
|
|
562
618
|
/** `keepRead` for the server's application traffic secret and the write keys. */
|
|
563
|
-
keepWrite(): boolean {
|
|
619
|
+
private keepWrite(): boolean {
|
|
564
620
|
const tls: TlsServer = this.tls
|
|
565
621
|
if (!tls.writeKnown(TLS_LEVEL_APPLICATION)) {
|
|
566
622
|
return false
|
|
@@ -580,7 +636,11 @@ export class TlsRecordServer {
|
|
|
580
636
|
return installed
|
|
581
637
|
}
|
|
582
638
|
|
|
583
|
-
/**
|
|
639
|
+
/**
|
|
640
|
+
* How many bytes `receive` would take now: what the socket should read at most.
|
|
641
|
+
*
|
|
642
|
+
* @returns The room left in the record reader's buffer, in bytes.
|
|
643
|
+
*/
|
|
584
644
|
space(): i32 {
|
|
585
645
|
return this.reader.space()
|
|
586
646
|
}
|
|
@@ -592,6 +652,12 @@ export class TlsRecordServer {
|
|
|
592
652
|
* socket again), or `TLS_RECORD_INVALID` for a window outside `data`. Once
|
|
593
653
|
* the connection has failed, or the client has sent `close_notify`, bytes
|
|
594
654
|
* are taken and dropped (§6.1).
|
|
655
|
+
*
|
|
656
|
+
* @param data The array that holds the bytes read from the socket.
|
|
657
|
+
* @param off Where in `data` the bytes start.
|
|
658
|
+
* @param len How many bytes, from `off`, to offer.
|
|
659
|
+
* @returns How many bytes were taken, from 0 up to `len` (all of them once the connection has failed or
|
|
660
|
+
* the client has closed); `TLS_RECORD_INVALID` for a window outside `data`.
|
|
595
661
|
*/
|
|
596
662
|
receive(data: u8[], off: i32, len: i32): i32 {
|
|
597
663
|
if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
|
|
@@ -624,7 +690,7 @@ export class TlsRecordServer {
|
|
|
624
690
|
* signature, are handled first, so a `close_notify` among them counts.
|
|
625
691
|
* Every key the connection holds is wiped, as `fail` wipes them.
|
|
626
692
|
*/
|
|
627
|
-
checkTruncation(): void {
|
|
693
|
+
private checkTruncation(): void {
|
|
628
694
|
if (this.ended && !this.peerClosed && this.state !== TLS_RECORD_STATE_FAILED && this.reader.next() <= 0) {
|
|
629
695
|
this.state = TLS_RECORD_STATE_FAILED
|
|
630
696
|
this.dropPending()
|
|
@@ -640,7 +706,7 @@ export class TlsRecordServer {
|
|
|
640
706
|
}
|
|
641
707
|
|
|
642
708
|
/** Forgets everything waiting to be sent. */
|
|
643
|
-
dropOutput(): void {
|
|
709
|
+
private dropOutput(): void {
|
|
644
710
|
this.outputStart = 0
|
|
645
711
|
this.outputEnd = 0
|
|
646
712
|
this.dropPending()
|
|
@@ -650,7 +716,7 @@ export class TlsRecordServer {
|
|
|
650
716
|
* Forgets what is owed but not yet sealed: the handshake queues, a
|
|
651
717
|
* NewSessionTicket, the compatibility record, a KeyUpdate and a close.
|
|
652
718
|
*/
|
|
653
|
-
dropPending(): void {
|
|
719
|
+
private dropPending(): void {
|
|
654
720
|
this.tls.clearOutput(TLS_LEVEL_INITIAL)
|
|
655
721
|
this.tls.clearOutput(TLS_LEVEL_HANDSHAKE)
|
|
656
722
|
this.tls.clearOutput(TLS_LEVEL_APPLICATION)
|
|
@@ -670,7 +736,7 @@ export class TlsRecordServer {
|
|
|
670
736
|
* nothing to send then but a `change_cipher_spec`, and holding the rest
|
|
671
737
|
* means bytes mean the same thing however the stream was cut.
|
|
672
738
|
*/
|
|
673
|
-
process(): void {
|
|
739
|
+
private process(): void {
|
|
674
740
|
while (this.state !== TLS_RECORD_STATE_FAILED && !this.peerClosed) {
|
|
675
741
|
if (this.tls.state === TLS_STATE_WAIT_SIGNATURE || this.tls.state === TLS_STATE_WAIT_TICKET) {
|
|
676
742
|
break
|
|
@@ -702,7 +768,7 @@ export class TlsRecordServer {
|
|
|
702
768
|
* read, moving that to the front when it would then fit and only then, so
|
|
703
769
|
* a caller reading a byte at a time does not move the buffer each time.
|
|
704
770
|
*/
|
|
705
|
-
plainHasRoom(): boolean {
|
|
771
|
+
private plainHasRoom(): boolean {
|
|
706
772
|
const capacity: i32 = TLS_RECORD_PLAIN_SIZE
|
|
707
773
|
if (capacity - (this.plainEnd - this.plainStart) < TLS_MAX_PLAINTEXT) {
|
|
708
774
|
return false
|
|
@@ -722,7 +788,7 @@ export class TlsRecordServer {
|
|
|
722
788
|
}
|
|
723
789
|
|
|
724
790
|
/** Counts a record that carried nothing for the caller: 0, or `unexpected_message` once there are too many in a row. */
|
|
725
|
-
idle(): i32 {
|
|
791
|
+
private idle(): i32 {
|
|
726
792
|
this.idleRecords = this.idleRecords + 1
|
|
727
793
|
return this.idleRecords > TLS_RECORD_IDLE_LIMIT ? TLS_ALERT_UNEXPECTED_MESSAGE : 0
|
|
728
794
|
}
|
|
@@ -733,7 +799,7 @@ export class TlsRecordServer {
|
|
|
733
799
|
* into `scratch`, and its content goes to `handleApplicationRecord` once the
|
|
734
800
|
* handshake is done, to the handshake before. Answers 0 or the alert.
|
|
735
801
|
*/
|
|
736
|
-
handleRecord(buf: u8[], off: i32, n: i32): i32 {
|
|
802
|
+
private handleRecord(buf: u8[], off: i32, n: i32): i32 {
|
|
737
803
|
if (toI32(buf[off]) === TLS_CONTENT_CHANGE_CIPHER_SPEC) {
|
|
738
804
|
return this.handleChangeCipherSpec(buf, off, n - TLS_RECORD_HEADER_SIZE)
|
|
739
805
|
}
|
|
@@ -766,7 +832,7 @@ export class TlsRecordServer {
|
|
|
766
832
|
* records of one handshake message. It is dropped; anything else is
|
|
767
833
|
* `unexpected_message`.
|
|
768
834
|
*/
|
|
769
|
-
handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
|
|
835
|
+
private handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
|
|
770
836
|
if (length !== 1 || toI32(buf[off + TLS_RECORD_HEADER_SIZE]) !== 1) {
|
|
771
837
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
772
838
|
}
|
|
@@ -781,7 +847,7 @@ export class TlsRecordServer {
|
|
|
781
847
|
* they were read at. An empty fragment is `unexpected_message` (§5.1 forbids
|
|
782
848
|
* sending one); `TlsServer`'s own answer otherwise.
|
|
783
849
|
*/
|
|
784
|
-
handleHandshake(length: i32): i32 {
|
|
850
|
+
private handleHandshake(length: i32): i32 {
|
|
785
851
|
if (length === 0) {
|
|
786
852
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
787
853
|
}
|
|
@@ -802,7 +868,7 @@ export class TlsRecordServer {
|
|
|
802
868
|
* post-handshake message are handled here; and a post-handshake message the
|
|
803
869
|
* client has sent part of may not be interrupted by anything else (§5.1).
|
|
804
870
|
*/
|
|
805
|
-
handleApplicationRecord(length: i32): i32 {
|
|
871
|
+
private handleApplicationRecord(length: i32): i32 {
|
|
806
872
|
const type: i32 = this.readProtection.contentType
|
|
807
873
|
if (this.postHandshakeLength > 0 && type !== TLS_CONTENT_HANDSHAKE) {
|
|
808
874
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
@@ -838,7 +904,7 @@ export class TlsRecordServer {
|
|
|
838
904
|
* this module does not know included (§6.2) — ends the connection with
|
|
839
905
|
* nothing more sent.
|
|
840
906
|
*/
|
|
841
|
-
handleAlert(length: i32): i32 {
|
|
907
|
+
private handleAlert(length: i32): i32 {
|
|
842
908
|
if (length !== 2) {
|
|
843
909
|
return TLS_ALERT_DECODE_ERROR
|
|
844
910
|
}
|
|
@@ -871,7 +937,7 @@ export class TlsRecordServer {
|
|
|
871
937
|
* KeyUpdate that does not end its record `unexpected_message`, since the
|
|
872
938
|
* keys change after it (§5.1). The message may start in an earlier record.
|
|
873
939
|
*/
|
|
874
|
-
handlePostHandshake(length: i32): i32 {
|
|
940
|
+
private handlePostHandshake(length: i32): i32 {
|
|
875
941
|
const idle: i32 = this.idle()
|
|
876
942
|
if (idle !== 0) {
|
|
877
943
|
return idle
|
|
@@ -920,7 +986,7 @@ export class TlsRecordServer {
|
|
|
920
986
|
* Answers 0, or `internal_error` when no read secret is kept (`readHash`
|
|
921
987
|
* 0) or the keys do not install.
|
|
922
988
|
*/
|
|
923
|
-
updateReadKeys(requested: boolean): i32 {
|
|
989
|
+
private updateReadKeys(requested: boolean): i32 {
|
|
924
990
|
const advanced: boolean =
|
|
925
991
|
this.readHash === SHA384_SIZE
|
|
926
992
|
? tlsAdvanceHeld(this.readProtection, this.readTraffic384)
|
|
@@ -941,7 +1007,7 @@ export class TlsRecordServer {
|
|
|
941
1007
|
* hands over are copied or installed and then wiped in `TlsServer`, so
|
|
942
1008
|
* that only this connection holds them.
|
|
943
1009
|
*/
|
|
944
|
-
advance(): void {
|
|
1010
|
+
private advance(): void {
|
|
945
1011
|
const tls: TlsServer = this.tls
|
|
946
1012
|
if (tls.outputInitialLength > 0 && !this.firstMessageSeen) {
|
|
947
1013
|
// §D.4: in compatibility mode, which a client asks for with a session
|
|
@@ -997,7 +1063,7 @@ export class TlsRecordServer {
|
|
|
997
1063
|
* front first. Borrows `output` when none is held, since a record is about
|
|
998
1064
|
* to be sealed into it.
|
|
999
1065
|
*/
|
|
1000
|
-
outputHasRoom(need: i32): boolean {
|
|
1066
|
+
private outputHasRoom(need: i32): boolean {
|
|
1001
1067
|
if (this.output.length === 0) {
|
|
1002
1068
|
this.output = this.pools.output.take()
|
|
1003
1069
|
}
|
|
@@ -1019,17 +1085,17 @@ export class TlsRecordServer {
|
|
|
1019
1085
|
* the content, its type and the padding together may not pass 2^14 + 1
|
|
1020
1086
|
* (§5.4). Below 1 when the padding leaves no room at all.
|
|
1021
1087
|
*/
|
|
1022
|
-
maxChunk(): i32 {
|
|
1088
|
+
private maxChunk(): i32 {
|
|
1023
1089
|
return TLS_MAX_PLAINTEXT - this.writePadding()
|
|
1024
1090
|
}
|
|
1025
1091
|
|
|
1026
1092
|
/** The padding the next record carries: `padding` once records are protected, none in the clear. */
|
|
1027
|
-
writePadding(): i32 {
|
|
1093
|
+
private writePadding(): i32 {
|
|
1028
1094
|
return this.writeProtection.isProtected() ? this.padding : 0
|
|
1029
1095
|
}
|
|
1030
1096
|
|
|
1031
1097
|
/** The room a record of `len` content bytes takes in `output` under the write protection as it stands. */
|
|
1032
|
-
recordRoom(len: i32): i32 {
|
|
1098
|
+
private recordRoom(len: i32): i32 {
|
|
1033
1099
|
if (!this.writeProtection.isProtected()) {
|
|
1034
1100
|
return TLS_RECORD_HEADER_SIZE + len
|
|
1035
1101
|
}
|
|
@@ -1041,6 +1107,12 @@ export class TlsRecordServer {
|
|
|
1041
1107
|
* under the write protection, padded when it is protected. False, writing
|
|
1042
1108
|
* nothing, when it does not fit; a refusal from `seal` itself, which only a
|
|
1043
1109
|
* mistake here could cause, fails the connection.
|
|
1110
|
+
*
|
|
1111
|
+
* @param type The content type to send, such as `TLS_CONTENT_HANDSHAKE`.
|
|
1112
|
+
* @param data The array that holds the content.
|
|
1113
|
+
* @param off Where in `data` the content starts.
|
|
1114
|
+
* @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.
|
|
1044
1116
|
*/
|
|
1045
1117
|
sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
1046
1118
|
const room: i32 = this.recordRoom(len)
|
|
@@ -1073,7 +1145,7 @@ export class TlsRecordServer {
|
|
|
1073
1145
|
* `maxChunk()` bytes, and answers where it stopped. A padding that leaves
|
|
1074
1146
|
* no room for content fails the connection with `internal_error`.
|
|
1075
1147
|
*/
|
|
1076
|
-
sealQueue(queue: u8[], at: i32, length: i32): i32 {
|
|
1148
|
+
private sealQueue(queue: u8[], at: i32, length: i32): i32 {
|
|
1077
1149
|
let next: i32 = at
|
|
1078
1150
|
const most: i32 = this.maxChunk()
|
|
1079
1151
|
if (most < 1 && next < length) {
|
|
@@ -1099,7 +1171,7 @@ export class TlsRecordServer {
|
|
|
1099
1171
|
* application secrets wiped in `TlsServer` once they are installed here. A
|
|
1100
1172
|
* KeyUpdate owed to the client goes last.
|
|
1101
1173
|
*/
|
|
1102
|
-
pump(): void {
|
|
1174
|
+
private pump(): void {
|
|
1103
1175
|
if (this.state === TLS_RECORD_STATE_FAILED) {
|
|
1104
1176
|
return
|
|
1105
1177
|
}
|
|
@@ -1191,7 +1263,7 @@ export class TlsRecordServer {
|
|
|
1191
1263
|
* to the next secret (§4.6.3, §7.2), the old one wiped. False, sending
|
|
1192
1264
|
* nothing, when it does not fit in `output` yet.
|
|
1193
1265
|
*/
|
|
1194
|
-
sendKeyUpdate(request: boolean): boolean {
|
|
1266
|
+
private sendKeyUpdate(request: boolean): boolean {
|
|
1195
1267
|
const message: u8[] = [toU8(TLS_HANDSHAKE_KEY_UPDATE), toU8(0), toU8(0), toU8(1), toU8(request ? 1 : 0)]
|
|
1196
1268
|
if (!this.sealRecord(TLS_CONTENT_HANDSHAKE, message, TLS_RECORD_SERVER_FROM, TLS_KEY_UPDATE_SIZE)) {
|
|
1197
1269
|
return false
|
|
@@ -1213,6 +1285,10 @@ export class TlsRecordServer {
|
|
|
1213
1285
|
* is under way or `output` is full, `TLS_RECORD_PIPE` once the server has
|
|
1214
1286
|
* closed or the connection failed, or `TLS_RECORD_RESET` when the next keys
|
|
1215
1287
|
* do not install and the connection fails on it.
|
|
1288
|
+
*
|
|
1289
|
+
* @param requestPeer `true` to set `request_update`, so that the client sends a KeyUpdate of its own.
|
|
1290
|
+
* @returns 0 when the KeyUpdate was sealed and the write keys moved on, or one of the negative
|
|
1291
|
+
* `TLS_RECORD_*` codes above.
|
|
1216
1292
|
*/
|
|
1217
1293
|
keyUpdate(requestPeer: boolean): i32 {
|
|
1218
1294
|
if (this.state === TLS_RECORD_STATE_FAILED || this.state === TLS_RECORD_STATE_CLOSED) {
|
|
@@ -1234,6 +1310,10 @@ export class TlsRecordServer {
|
|
|
1234
1310
|
* was still owed is dropped: the alert is the last thing the server sends,
|
|
1235
1311
|
* so once it is sealed every key the connection holds is wiped, the
|
|
1236
1312
|
* traffic keys and secrets as well as what `TlsServer` holds.
|
|
1313
|
+
*
|
|
1314
|
+
* @param alert The `TLS_ALERT_*` description to send, such as `TLS_ALERT_INTERNAL_ERROR`. It is not sent
|
|
1315
|
+
* once the connection is `TLS_RECORD_STATE_CLOSED` (set when `close` is called, before its
|
|
1316
|
+
* `close_notify` is sealed), and a second call does nothing.
|
|
1237
1317
|
*/
|
|
1238
1318
|
fail(alert: i32): void {
|
|
1239
1319
|
if (this.state === TLS_RECORD_STATE_FAILED) {
|
|
@@ -1254,7 +1334,7 @@ export class TlsRecordServer {
|
|
|
1254
1334
|
}
|
|
1255
1335
|
|
|
1256
1336
|
/** Seals an alert record of `level` and `description`; false, sending nothing, when `output` has no room for it. */
|
|
1257
|
-
sendAlert(level: i32, description: i32): boolean {
|
|
1337
|
+
private sendAlert(level: i32, description: i32): boolean {
|
|
1258
1338
|
const body: u8[] = [toU8(level), toU8(description)]
|
|
1259
1339
|
const two: i32 = 2
|
|
1260
1340
|
return this.sealRecord(TLS_CONTENT_ALERT, body, TLS_RECORD_SERVER_FROM, two)
|
|
@@ -1263,6 +1343,8 @@ export class TlsRecordServer {
|
|
|
1263
1343
|
/**
|
|
1264
1344
|
* Lets the first `n` bytes of `output` go, once the socket has taken them,
|
|
1265
1345
|
* and seals what was waiting for the room.
|
|
1346
|
+
*
|
|
1347
|
+
* @param n How many bytes the socket wrote. It is clamped to 0 and to the bytes `output` holds.
|
|
1266
1348
|
*/
|
|
1267
1349
|
consume(n: i32): void {
|
|
1268
1350
|
const held: i32 = this.outputEnd - this.outputStart
|
|
@@ -1286,6 +1368,13 @@ export class TlsRecordServer {
|
|
|
1286
1368
|
* `TLS_RECORD_AGAIN` while the handshake is under way or nothing fits,
|
|
1287
1369
|
* `TLS_RECORD_PIPE` once the server has closed or the connection failed,
|
|
1288
1370
|
* and `TLS_RECORD_INVALID` for a window outside `data`.
|
|
1371
|
+
*
|
|
1372
|
+
* @param data The array that holds the application data.
|
|
1373
|
+
* @param off Where in `data` the bytes start.
|
|
1374
|
+
* @param len How many bytes, from `off`, to send.
|
|
1375
|
+
* @returns How many bytes were sealed, from 1 up to `len` (0 for a `len` of 0 on an open connection
|
|
1376
|
+
* with Application keys and no KeyUpdate pending); `TLS_RECORD_RESET` when nothing was sealed
|
|
1377
|
+
* because the connection failed; or one of the negative codes above, whatever `len` is.
|
|
1289
1378
|
*/
|
|
1290
1379
|
write(data: u8[], off: i32, len: i32): i32 {
|
|
1291
1380
|
if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
|
|
@@ -1338,6 +1427,12 @@ export class TlsRecordServer {
|
|
|
1338
1427
|
* waiting; `TLS_RECORD_RESET` once the connection has failed; and
|
|
1339
1428
|
* `TLS_RECORD_INVALID` for a window outside `buf`. Reading makes room, so
|
|
1340
1429
|
* records the buffer was full for are handled before it answers.
|
|
1430
|
+
*
|
|
1431
|
+
* @param buf Where the application data is written.
|
|
1432
|
+
* @param off Where in `buf` to start writing.
|
|
1433
|
+
* @param len How many bytes, at most, to read.
|
|
1434
|
+
* @returns How many bytes were written, which may be fewer than `len`, or 0 or a negative
|
|
1435
|
+
* `TLS_RECORD_*` code as above.
|
|
1341
1436
|
*/
|
|
1342
1437
|
read(buf: u8[], off: i32, len: i32): i32 {
|
|
1343
1438
|
if (!tlsRecordWindowFits(toI32(buf.length), off, len)) {
|
|
@@ -1392,14 +1487,20 @@ export class TlsRecordServer {
|
|
|
1392
1487
|
* server's traffic secret are wiped as soon as it is sealed. The read keys
|
|
1393
1488
|
* stay: the client may still send until its own `close_notify`.
|
|
1394
1489
|
*/
|
|
1395
|
-
sealClose(): void {
|
|
1490
|
+
private sealClose(): void {
|
|
1396
1491
|
if (this.closeDue && this.sendAlert(TLS_ALERT_LEVEL_WARNING, TLS_ALERT_CLOSE_NOTIFY)) {
|
|
1397
1492
|
this.closeDue = false
|
|
1398
1493
|
this.wipeWrite()
|
|
1399
1494
|
}
|
|
1400
1495
|
}
|
|
1401
1496
|
|
|
1402
|
-
/**
|
|
1497
|
+
/**
|
|
1498
|
+
* What the CertificateVerify signs, while `interest` says `TLS_RECORD_SIGN`; `null` otherwise.
|
|
1499
|
+
*
|
|
1500
|
+
* @returns The bytes to sign, the handshake's own array (read it, do not keep it), as
|
|
1501
|
+
* `TlsServer.signatureInput` answers them; `null` whenever the handshake is not in
|
|
1502
|
+
* `TLS_STATE_WAIT_SIGNATURE`, the connection being out of its handshake included.
|
|
1503
|
+
*/
|
|
1403
1504
|
signatureInput(): u8[] | null {
|
|
1404
1505
|
if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
|
|
1405
1506
|
return null
|
|
@@ -1412,6 +1513,12 @@ export class TlsRecordServer {
|
|
|
1412
1513
|
* `tlsSignEcdsaP256` answers it) and seals the rest of the flight. Answers
|
|
1413
1514
|
* 0, or the alert the connection failed with — `internal_error` when no
|
|
1414
1515
|
* signature is due.
|
|
1516
|
+
*
|
|
1517
|
+
* @param signature The signature over `signatureInput()`, made with the certificate's private key.
|
|
1518
|
+
* @returns 0 when the flight is sealed, or the `TLS_ALERT_*` value the connection failed with;
|
|
1519
|
+
* `internal_error`, without failing it, on a connection already open or closed. A connection
|
|
1520
|
+
* that failed without an alert (a truncated stream, the peer's alert, `abort`) also answers 0:
|
|
1521
|
+
* `state` tells it from a sealed flight.
|
|
1415
1522
|
*/
|
|
1416
1523
|
sign(signature: u8[]): i32 {
|
|
1417
1524
|
if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
|
|
@@ -1433,6 +1540,18 @@ export class TlsRecordServer {
|
|
|
1433
1540
|
* and seals the flight that answers it, resumed or in full. Answers 0, or
|
|
1434
1541
|
* the alert the connection failed with — `internal_error` when no ticket
|
|
1435
1542
|
* waits.
|
|
1543
|
+
*
|
|
1544
|
+
* @param current The carrier's current ticket key. It is read, not kept, and stays the carrier's to wipe.
|
|
1545
|
+
* @param currentInfo What the carrier knows of `current`: its id, when it was drawn, how many it sealed.
|
|
1546
|
+
* @param previous The carrier's previous ticket key, or `current` again when it has only one. It stays
|
|
1547
|
+
* the carrier's to wipe.
|
|
1548
|
+
* @param previousInfo What the carrier knows of `previous`.
|
|
1549
|
+
* @param strikes The carrier's record of tickets already resumed, where the one resumed now is recorded.
|
|
1550
|
+
* @param now The time now, in `monotonicNanos()`.
|
|
1551
|
+
* @returns 0 when the answering flight is sealed, or the `TLS_ALERT_*` value the connection failed with;
|
|
1552
|
+
* `internal_error`, without failing it, on a connection already open or closed. A connection
|
|
1553
|
+
* that failed without an alert (a truncated stream, the peer's alert, `abort`) also answers 0:
|
|
1554
|
+
* `state` tells it from a sealed flight.
|
|
1436
1555
|
*/
|
|
1437
1556
|
resume(
|
|
1438
1557
|
current: Secret<u8[]>,
|
|
@@ -1460,6 +1579,12 @@ export class TlsRecordServer {
|
|
|
1460
1579
|
* ticket under `key` (`TlsServer.issue`) and queues its NewSessionTicket.
|
|
1461
1580
|
* Whether one was queued; either way the resumption master secret is
|
|
1462
1581
|
* wiped, and no second ticket is issued.
|
|
1582
|
+
*
|
|
1583
|
+
* @param key The carrier's current ticket key. It is read, not kept, and stays the carrier's to wipe.
|
|
1584
|
+
* @param info What the carrier knows of `key`: its id, when it was drawn, how many it sealed.
|
|
1585
|
+
* @param now The time now, in `monotonicNanos()`.
|
|
1586
|
+
* @returns `true` when a NewSessionTicket was queued; `false` when none was, including on a connection
|
|
1587
|
+
* that is not open.
|
|
1463
1588
|
*/
|
|
1464
1589
|
issue(key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): boolean {
|
|
1465
1590
|
if (this.state !== TLS_RECORD_STATE_OPEN) {
|
|
@@ -1476,6 +1601,9 @@ export class TlsRecordServer {
|
|
|
1476
1601
|
* bytes), `read` (application data, or the end of the client's stream),
|
|
1477
1602
|
* sign, handle a ticket, or nothing more ever (`TLS_RECORD_DONE`: failed
|
|
1478
1603
|
* or closed, and everything is sent).
|
|
1604
|
+
*
|
|
1605
|
+
* @returns `TLS_RECORD_WANT_READ`, `TLS_RECORD_WANT_WRITE`, `TLS_RECORD_DATA`, `TLS_RECORD_SIGN`,
|
|
1606
|
+
* `TLS_RECORD_TICKET` and `TLS_RECORD_DONE` ORed together; 0 when there is nothing to do yet.
|
|
1479
1607
|
*/
|
|
1480
1608
|
interest(): i32 {
|
|
1481
1609
|
let bits: i32 = 0
|