@amritk/nish-x86_64-linux 0.34.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +22 -13
- package/bin/nish +0 -0
- package/docs/AI.md +86 -16
- package/package.json +1 -1
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- 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 +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- 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 +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- 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 +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +297 -77
- package/std/net/http1-server.ts +430 -94
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +275 -101
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +801 -112
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- 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 +430 -40
- package/std/sync/shm.ts +222 -15
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
|
@@ -187,62 +187,69 @@ const tlsClientAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]
|
|
|
187
187
|
*/
|
|
188
188
|
export class TlsRecordClient {
|
|
189
189
|
tls: TlsClient
|
|
190
|
+
/** @internal Read by `net/fetch.ts`. */
|
|
190
191
|
reader: TlsRecordReader
|
|
191
192
|
readProtection: TlsRecordProtection
|
|
192
193
|
writeProtection: TlsRecordProtection
|
|
193
194
|
/** Never installed: seals the compatibility record in the clear once the write side is protected. */
|
|
194
|
-
clearProtection: TlsRecordProtection
|
|
195
|
+
private clearProtection: TlsRecordProtection
|
|
195
196
|
/** Bytes to send are `output[outputStart .. outputEnd)`. */
|
|
196
197
|
output: u8[]
|
|
197
198
|
/** Application data the caller has not read is `plain[plainStart .. plainEnd)`. */
|
|
198
|
-
plain: u8[]
|
|
199
|
+
private plain: u8[]
|
|
199
200
|
/** Where a record is opened to. */
|
|
200
|
-
scratch: u8[]
|
|
201
|
+
private scratch: u8[]
|
|
201
202
|
/** The server's and the client's current application traffic secrets, at both hash lengths. */
|
|
202
|
-
readonly readTraffic256: Secret<u8[]>
|
|
203
|
-
readonly readTraffic384: Secret<u8[]>
|
|
204
|
-
readonly writeTraffic256: Secret<u8[]>
|
|
205
|
-
readonly writeTraffic384: Secret<u8[]>
|
|
206
|
-
readHash: i32 = 0
|
|
207
|
-
writeHash: i32 = 0
|
|
203
|
+
private readonly readTraffic256: Secret<u8[]>
|
|
204
|
+
private readonly readTraffic384: Secret<u8[]>
|
|
205
|
+
private readonly writeTraffic256: Secret<u8[]>
|
|
206
|
+
private readonly writeTraffic384: Secret<u8[]>
|
|
207
|
+
private readHash: i32 = 0
|
|
208
|
+
private writeHash: i32 = 0
|
|
208
209
|
/** The header of a post-handshake message the server has sent part of, and a KeyUpdate's body. */
|
|
209
|
-
postHandshake: u8[]
|
|
210
|
+
private postHandshake: u8[]
|
|
210
211
|
state: i32 = 0
|
|
211
212
|
alert: i32 = 0
|
|
212
213
|
peerAlert: i32 = -1
|
|
213
214
|
padding: i32 = 0
|
|
214
215
|
/** The `TLS_LEVEL_*` the server's records are read at. */
|
|
215
|
-
readLevel: i32 = 0
|
|
216
|
+
private readLevel: i32 = 0
|
|
216
217
|
/** The `TLS_LEVEL_*` the client's records are written at. */
|
|
217
|
-
writeLevel: i32 = 0
|
|
218
|
+
private writeLevel: i32 = 0
|
|
218
219
|
outputStart: i32 = 0
|
|
219
220
|
outputEnd: i32 = 0
|
|
221
|
+
/** @internal Read by `net/fetch.ts`. */
|
|
220
222
|
plainStart: i32 = 0
|
|
223
|
+
/** @internal Read by `net/fetch.ts`. */
|
|
221
224
|
plainEnd: i32 = 0
|
|
222
225
|
/** How much of `TlsClient`'s Initial-level output is already in records. */
|
|
223
|
-
pendingInitialAt: i32 = 0
|
|
226
|
+
private pendingInitialAt: i32 = 0
|
|
224
227
|
/** How much of `TlsClient`'s Handshake-level output is already in records. */
|
|
225
|
-
pendingHandshakeAt: i32 = 0
|
|
228
|
+
private pendingHandshakeAt: i32 = 0
|
|
226
229
|
/** How many bytes of the current post-handshake message's header or KeyUpdate are in `postHandshake`. */
|
|
227
|
-
postHandshakeLength: i32 = 0
|
|
230
|
+
private postHandshakeLength: i32 = 0
|
|
228
231
|
/** How many body bytes of a NewSessionTicket are still to come, handed to `TlsClient` as they arrive. */
|
|
229
|
-
ticketLeft: i32 = 0
|
|
230
|
-
idleRecords: i32 = 0
|
|
232
|
+
private ticketLeft: i32 = 0
|
|
233
|
+
private idleRecords: i32 = 0
|
|
231
234
|
keyUpdates: i32 = 0
|
|
232
235
|
peerClosed: boolean = false
|
|
236
|
+
/** @internal Read by `net/fetch.ts`. */
|
|
233
237
|
ended: boolean = false
|
|
234
238
|
/** The server's first handshake record has arrived, which opens the window its `change_cipher_spec` may come in. */
|
|
235
|
-
started: boolean = false
|
|
239
|
+
private started: boolean = false
|
|
236
240
|
/** The client's compatibility `change_cipher_spec` is sent (§D.4). */
|
|
237
|
-
ccsSent: boolean = false
|
|
238
|
-
keyUpdateDue: boolean = false
|
|
239
|
-
closeDue: boolean = false
|
|
241
|
+
private ccsSent: boolean = false
|
|
242
|
+
private keyUpdateDue: boolean = false
|
|
243
|
+
private closeDue: boolean = false
|
|
240
244
|
|
|
241
245
|
/**
|
|
242
246
|
* Wraps `tls`, which should be fresh, and starts it: the ClientHello is
|
|
243
247
|
* sealed into `output` at once. A `tls` that has already failed, or whose
|
|
244
248
|
* ClientHello cannot be written, fails this connection with its alert
|
|
245
249
|
* queued in the clear.
|
|
250
|
+
*
|
|
251
|
+
* @param tls The handshake to carry, normally built and not yet started (a started one is carried from
|
|
252
|
+
* where it is); the connection keeps it as `tls` and wipes it in `wipe`.
|
|
246
253
|
*/
|
|
247
254
|
constructor(tls: TlsClient) {
|
|
248
255
|
this.tls = tls
|
|
@@ -294,7 +301,7 @@ export class TlsRecordClient {
|
|
|
294
301
|
}
|
|
295
302
|
|
|
296
303
|
/** Wipes the server's application traffic secret and the read keys. */
|
|
297
|
-
wipeRead(): void {
|
|
304
|
+
private wipeRead(): void {
|
|
298
305
|
exposeWith(this.readTraffic256, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
|
|
299
306
|
exposeWith(this.readTraffic384, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
|
|
300
307
|
this.readHash = 0
|
|
@@ -302,7 +309,7 @@ export class TlsRecordClient {
|
|
|
302
309
|
}
|
|
303
310
|
|
|
304
311
|
/** Wipes the client's application traffic secret and its write keys. */
|
|
305
|
-
wipeWrite(): void {
|
|
312
|
+
private wipeWrite(): void {
|
|
306
313
|
exposeWith(this.writeTraffic256, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
|
|
307
314
|
exposeWith(this.writeTraffic384, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
|
|
308
315
|
this.writeHash = 0
|
|
@@ -315,7 +322,7 @@ export class TlsRecordClient {
|
|
|
315
322
|
* read keys from it. False when the secret is not known or the keys do not
|
|
316
323
|
* install.
|
|
317
324
|
*/
|
|
318
|
-
keepRead(): boolean {
|
|
325
|
+
private keepRead(): boolean {
|
|
319
326
|
const tls: TlsClient = this.tls
|
|
320
327
|
if (!tls.readKnown(TLS_LEVEL_APPLICATION)) {
|
|
321
328
|
return false
|
|
@@ -336,7 +343,7 @@ export class TlsRecordClient {
|
|
|
336
343
|
}
|
|
337
344
|
|
|
338
345
|
/** `keepRead` for the client's application traffic secret and the write keys. */
|
|
339
|
-
keepWrite(): boolean {
|
|
346
|
+
private keepWrite(): boolean {
|
|
340
347
|
const tls: TlsClient = this.tls
|
|
341
348
|
if (!tls.writeKnown(TLS_LEVEL_APPLICATION)) {
|
|
342
349
|
return false
|
|
@@ -356,7 +363,11 @@ export class TlsRecordClient {
|
|
|
356
363
|
return installed
|
|
357
364
|
}
|
|
358
365
|
|
|
359
|
-
/**
|
|
366
|
+
/**
|
|
367
|
+
* How many bytes `receive` would take now: what the socket should read at most.
|
|
368
|
+
*
|
|
369
|
+
* @returns The free room in the record reader, in bytes: one largest record less what it holds unhandled.
|
|
370
|
+
*/
|
|
360
371
|
space(): i32 {
|
|
361
372
|
return this.reader.space()
|
|
362
373
|
}
|
|
@@ -368,6 +379,11 @@ export class TlsRecordClient {
|
|
|
368
379
|
* outside `data`. Once the connection has failed, the server has sent
|
|
369
380
|
* `close_notify`, or the client closed before the handshake was done, bytes
|
|
370
381
|
* are taken and dropped (§6.1).
|
|
382
|
+
*
|
|
383
|
+
* @param data The buffer the socket was read into; the bytes are copied, not kept.
|
|
384
|
+
* @param off Where in `data` the bytes start.
|
|
385
|
+
* @param len How many bytes, from `off`, the socket gave.
|
|
386
|
+
* @returns How many of the `len` bytes were taken, or `TLS_RECORD_INVALID` (-22).
|
|
371
387
|
*/
|
|
372
388
|
receive(data: u8[], off: i32, len: i32): i32 {
|
|
373
389
|
if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
|
|
@@ -392,7 +408,7 @@ export class TlsRecordClient {
|
|
|
392
408
|
* handled, an end without `close_notify` is truncation: the connection
|
|
393
409
|
* fails with nothing more sent, and every key it holds is wiped.
|
|
394
410
|
*/
|
|
395
|
-
checkTruncation(): void {
|
|
411
|
+
private checkTruncation(): void {
|
|
396
412
|
if (this.ended && !this.peerClosed && this.state !== TLS_RECORD_STATE_FAILED && this.reader.next() <= 0) {
|
|
397
413
|
this.state = TLS_RECORD_STATE_FAILED
|
|
398
414
|
this.dropPending()
|
|
@@ -410,7 +426,7 @@ export class TlsRecordClient {
|
|
|
410
426
|
}
|
|
411
427
|
|
|
412
428
|
/** Forgets what is owed but not yet sealed: the handshake queues, a KeyUpdate and a close. */
|
|
413
|
-
dropPending(): void {
|
|
429
|
+
private dropPending(): void {
|
|
414
430
|
this.tls.clearOutput(TLS_LEVEL_INITIAL)
|
|
415
431
|
this.tls.clearOutput(TLS_LEVEL_HANDSHAKE)
|
|
416
432
|
this.pendingInitialAt = 0
|
|
@@ -425,7 +441,7 @@ export class TlsRecordClient {
|
|
|
425
441
|
* verdict on the server's certificate the records stay where they are, and
|
|
426
442
|
* `authenticate` comes back for them.
|
|
427
443
|
*/
|
|
428
|
-
process(): void {
|
|
444
|
+
private process(): void {
|
|
429
445
|
while (this.state !== TLS_RECORD_STATE_FAILED && !this.peerClosed && !this.abandoned()) {
|
|
430
446
|
if (this.tls.state === TLS_CLIENT_STATE_WAIT_AUTH) {
|
|
431
447
|
break
|
|
@@ -453,7 +469,7 @@ export class TlsRecordClient {
|
|
|
453
469
|
}
|
|
454
470
|
|
|
455
471
|
/** Whether a whole record's content fits beside what the caller has not read, moving that to the front when it would then fit. */
|
|
456
|
-
plainHasRoom(): boolean {
|
|
472
|
+
private plainHasRoom(): boolean {
|
|
457
473
|
const capacity: i32 = TLS_RECORD_CLIENT_PLAIN_SIZE
|
|
458
474
|
if (capacity - (this.plainEnd - this.plainStart) < TLS_MAX_PLAINTEXT) {
|
|
459
475
|
return false
|
|
@@ -470,7 +486,7 @@ export class TlsRecordClient {
|
|
|
470
486
|
}
|
|
471
487
|
|
|
472
488
|
/** Counts a record that carried nothing for the caller: 0, or `unexpected_message` once there are too many in a row. */
|
|
473
|
-
idle(): i32 {
|
|
489
|
+
private idle(): i32 {
|
|
474
490
|
this.idleRecords = this.idleRecords + 1
|
|
475
491
|
return this.idleRecords > TLS_RECORD_IDLE_LIMIT ? TLS_ALERT_UNEXPECTED_MESSAGE : 0
|
|
476
492
|
}
|
|
@@ -481,7 +497,7 @@ export class TlsRecordClient {
|
|
|
481
497
|
* `scratch`, and its content goes to `handleApplicationRecord` once the
|
|
482
498
|
* server's Finished is read, to the handshake before. Answers 0 or the alert.
|
|
483
499
|
*/
|
|
484
|
-
handleRecord(buf: u8[], off: i32, n: i32): i32 {
|
|
500
|
+
private handleRecord(buf: u8[], off: i32, n: i32): i32 {
|
|
485
501
|
if (toI32(buf[off]) === TLS_CONTENT_CHANGE_CIPHER_SPEC) {
|
|
486
502
|
return this.handleChangeCipherSpec(buf, off, n - TLS_RECORD_HEADER_SIZE)
|
|
487
503
|
}
|
|
@@ -511,7 +527,7 @@ export class TlsRecordClient {
|
|
|
511
527
|
* between the records of one handshake message. Dropped; anything else is
|
|
512
528
|
* `unexpected_message`.
|
|
513
529
|
*/
|
|
514
|
-
handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
|
|
530
|
+
private handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
|
|
515
531
|
if (length !== 1 || toI32(buf[off + TLS_RECORD_HEADER_SIZE]) !== 1) {
|
|
516
532
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
517
533
|
}
|
|
@@ -522,7 +538,7 @@ export class TlsRecordClient {
|
|
|
522
538
|
}
|
|
523
539
|
|
|
524
540
|
/** Handshake bytes, `scratch[0 .. length)`, for `TlsClient` at the level they were read at; an empty fragment is `unexpected_message` (§5.1). */
|
|
525
|
-
handleHandshake(length: i32): i32 {
|
|
541
|
+
private handleHandshake(length: i32): i32 {
|
|
526
542
|
if (length === 0) {
|
|
527
543
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
528
544
|
}
|
|
@@ -541,7 +557,7 @@ export class TlsRecordClient {
|
|
|
541
557
|
* content in `scratch`: application data for the caller, an alert, or
|
|
542
558
|
* post-handshake handshake bytes, which nothing else may interrupt (§5.1).
|
|
543
559
|
*/
|
|
544
|
-
handleApplicationRecord(length: i32): i32 {
|
|
560
|
+
private handleApplicationRecord(length: i32): i32 {
|
|
545
561
|
const type: i32 = this.readProtection.contentType
|
|
546
562
|
if ((this.postHandshakeLength > 0 || this.ticketLeft > 0) && type !== TLS_CONTENT_HANDSHAKE) {
|
|
547
563
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
@@ -575,7 +591,7 @@ export class TlsRecordClient {
|
|
|
575
591
|
* connection that never authenticated cannot end like one that did — ends
|
|
576
592
|
* the connection with nothing more sent and every key wiped.
|
|
577
593
|
*/
|
|
578
|
-
handleAlert(length: i32): i32 {
|
|
594
|
+
private handleAlert(length: i32): i32 {
|
|
579
595
|
if (length !== 2) {
|
|
580
596
|
return TLS_ALERT_DECODE_ERROR
|
|
581
597
|
}
|
|
@@ -607,7 +623,7 @@ export class TlsRecordClient {
|
|
|
607
623
|
* other type is `unexpected_message`. Each message counts against the idle
|
|
608
624
|
* limit.
|
|
609
625
|
*/
|
|
610
|
-
handlePostHandshake(length: i32): i32 {
|
|
626
|
+
private handlePostHandshake(length: i32): i32 {
|
|
611
627
|
let k: i32 = 0
|
|
612
628
|
while (k < length) {
|
|
613
629
|
if (this.ticketLeft > 0) {
|
|
@@ -684,7 +700,7 @@ export class TlsRecordClient {
|
|
|
684
700
|
* which `pump` sends. Answers 0, or `internal_error` when the keys do not
|
|
685
701
|
* install.
|
|
686
702
|
*/
|
|
687
|
-
updateReadKeys(requested: boolean): i32 {
|
|
703
|
+
private updateReadKeys(requested: boolean): i32 {
|
|
688
704
|
const advanced: boolean =
|
|
689
705
|
this.readHash === SHA384_SIZE
|
|
690
706
|
? tlsClientAdvanceHeld(this.readProtection, this.readTraffic384)
|
|
@@ -704,7 +720,7 @@ export class TlsRecordClient {
|
|
|
704
720
|
* server's flight, after which its handshake secrets have done their work
|
|
705
721
|
* and are wiped in `TlsClient`.
|
|
706
722
|
*/
|
|
707
|
-
advance(): void {
|
|
723
|
+
private advance(): void {
|
|
708
724
|
const tls: TlsClient = this.tls
|
|
709
725
|
if (this.readLevel === TLS_LEVEL_INITIAL && tls.readKnown(TLS_LEVEL_HANDSHAKE)) {
|
|
710
726
|
if (!tlsClientInstallHeld(this.readProtection, tls.suite, tls.secrets().serverHandshake)) {
|
|
@@ -731,7 +747,7 @@ export class TlsRecordClient {
|
|
|
731
747
|
}
|
|
732
748
|
|
|
733
749
|
/** Whether `need` more bytes fit in `output`, moving what is unsent to the front first. */
|
|
734
|
-
outputHasRoom(need: i32): boolean {
|
|
750
|
+
private outputHasRoom(need: i32): boolean {
|
|
735
751
|
const capacity: i32 = TLS_RECORD_CLIENT_OUTPUT_SIZE
|
|
736
752
|
if (this.outputEnd + need > capacity && this.outputStart > 0) {
|
|
737
753
|
const held: i32 = this.outputEnd - this.outputStart
|
|
@@ -745,17 +761,17 @@ export class TlsRecordClient {
|
|
|
745
761
|
}
|
|
746
762
|
|
|
747
763
|
/** The most content one record may carry under the write protection as it stands (§5.4). */
|
|
748
|
-
maxChunk(): i32 {
|
|
764
|
+
private maxChunk(): i32 {
|
|
749
765
|
return TLS_MAX_PLAINTEXT - this.writePadding()
|
|
750
766
|
}
|
|
751
767
|
|
|
752
768
|
/** The padding the next record carries: `padding` once records are protected, none in the clear. */
|
|
753
|
-
writePadding(): i32 {
|
|
769
|
+
private writePadding(): i32 {
|
|
754
770
|
return this.writeProtection.isProtected() ? this.padding : 0
|
|
755
771
|
}
|
|
756
772
|
|
|
757
773
|
/** The room a record of `len` content bytes takes in `output` under the write protection as it stands. */
|
|
758
|
-
recordRoom(len: i32): i32 {
|
|
774
|
+
private recordRoom(len: i32): i32 {
|
|
759
775
|
if (!this.writeProtection.isProtected()) {
|
|
760
776
|
return TLS_RECORD_HEADER_SIZE + len
|
|
761
777
|
}
|
|
@@ -767,7 +783,7 @@ export class TlsRecordClient {
|
|
|
767
783
|
* under `protection`. False, writing nothing, when it does not fit; a
|
|
768
784
|
* refusal from `seal` itself fails the connection.
|
|
769
785
|
*/
|
|
770
|
-
sealWith(protection: TlsRecordProtection, type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
786
|
+
private sealWith(protection: TlsRecordProtection, type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
771
787
|
const sealed: boolean = protection.isProtected()
|
|
772
788
|
const padding: i32 = sealed ? this.padding : 0
|
|
773
789
|
const room: i32 = sealed
|
|
@@ -786,7 +802,7 @@ export class TlsRecordClient {
|
|
|
786
802
|
}
|
|
787
803
|
|
|
788
804
|
/** `sealWith` under the write protection. */
|
|
789
|
-
sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
805
|
+
private sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
790
806
|
return this.sealWith(this.writeProtection, type, data, off, len)
|
|
791
807
|
}
|
|
792
808
|
|
|
@@ -794,7 +810,7 @@ export class TlsRecordClient {
|
|
|
794
810
|
* The compatibility `change_cipher_spec` (§D.4), in the clear whatever the
|
|
795
811
|
* write side is under, once a connection. False when `output` has no room.
|
|
796
812
|
*/
|
|
797
|
-
sealChangeCipherSpec(): boolean {
|
|
813
|
+
private sealChangeCipherSpec(): boolean {
|
|
798
814
|
if (this.ccsSent) {
|
|
799
815
|
return true
|
|
800
816
|
}
|
|
@@ -815,7 +831,7 @@ export class TlsRecordClient {
|
|
|
815
831
|
* `maxChunk()` bytes, and answers where it stopped. A padding that leaves
|
|
816
832
|
* no room for content fails the connection with `internal_error`.
|
|
817
833
|
*/
|
|
818
|
-
sealQueue(queue: u8[], at: i32, length: i32): i32 {
|
|
834
|
+
private sealQueue(queue: u8[], at: i32, length: i32): i32 {
|
|
819
835
|
let next: i32 = at
|
|
820
836
|
const most: i32 = this.maxChunk()
|
|
821
837
|
if (most < 1 && next < length) {
|
|
@@ -842,7 +858,7 @@ export class TlsRecordClient {
|
|
|
842
858
|
* application secrets wiped in `TlsClient` once they are installed here. A
|
|
843
859
|
* KeyUpdate owed to the server goes last.
|
|
844
860
|
*/
|
|
845
|
-
pump(): void {
|
|
861
|
+
private pump(): void {
|
|
846
862
|
if (this.state === TLS_RECORD_STATE_FAILED) {
|
|
847
863
|
return
|
|
848
864
|
}
|
|
@@ -911,7 +927,7 @@ export class TlsRecordClient {
|
|
|
911
927
|
* to the next secret (§4.6.3, §7.2), the old one wiped. False, sending
|
|
912
928
|
* nothing, when it does not fit in `output` yet.
|
|
913
929
|
*/
|
|
914
|
-
sendKeyUpdate(request: boolean): boolean {
|
|
930
|
+
private sendKeyUpdate(request: boolean): boolean {
|
|
915
931
|
const message: u8[] = [toU8(TLS_HANDSHAKE_KEY_UPDATE), toU8(0), toU8(0), toU8(1), toU8(request ? 1 : 0)]
|
|
916
932
|
if (!this.sealRecord(TLS_CONTENT_HANDSHAKE, message, TLS_RECORD_CLIENT_FROM, TLS_KEY_UPDATE_SIZE)) {
|
|
917
933
|
return false
|
|
@@ -933,6 +949,10 @@ export class TlsRecordClient {
|
|
|
933
949
|
* is under way or `output` is full, `TLS_RECORD_PIPE` once the client has
|
|
934
950
|
* closed or the connection failed, or `TLS_RECORD_RESET` when the next keys
|
|
935
951
|
* do not install.
|
|
952
|
+
*
|
|
953
|
+
* @param requestPeer True to ask the server to update its sending keys as well.
|
|
954
|
+
* @returns 0 once the KeyUpdate is sealed and the new write keys installed, or one of the negative
|
|
955
|
+
* `TLS_RECORD_*` values above.
|
|
936
956
|
*/
|
|
937
957
|
keyUpdate(requestPeer: boolean): i32 {
|
|
938
958
|
if (this.state === TLS_RECORD_STATE_FAILED || this.state === TLS_RECORD_STATE_CLOSED) {
|
|
@@ -953,7 +973,7 @@ export class TlsRecordClient {
|
|
|
953
973
|
* the client writes with now. What was still owed is dropped, and once the
|
|
954
974
|
* alert is sealed every key the connection holds is wiped.
|
|
955
975
|
*/
|
|
956
|
-
fail(alert: i32): void {
|
|
976
|
+
private fail(alert: i32): void {
|
|
957
977
|
if (this.state === TLS_RECORD_STATE_FAILED) {
|
|
958
978
|
return
|
|
959
979
|
}
|
|
@@ -969,13 +989,19 @@ export class TlsRecordClient {
|
|
|
969
989
|
}
|
|
970
990
|
|
|
971
991
|
/** Seals an alert record of `level` and `description`; false, sending nothing, when `output` has no room for it. */
|
|
972
|
-
sendAlert(level: i32, description: i32): boolean {
|
|
992
|
+
private sendAlert(level: i32, description: i32): boolean {
|
|
973
993
|
const body: u8[] = [toU8(level), toU8(description)]
|
|
974
994
|
const two: i32 = 2
|
|
975
995
|
return this.sealRecord(TLS_CONTENT_ALERT, body, TLS_RECORD_CLIENT_FROM, two)
|
|
976
996
|
}
|
|
977
997
|
|
|
978
|
-
/**
|
|
998
|
+
/**
|
|
999
|
+
* Lets the first `n` bytes of `output` go, once the socket has taken them, and seals what was waiting
|
|
1000
|
+
* for the room.
|
|
1001
|
+
*
|
|
1002
|
+
* @param n How many bytes from `outputStart` the socket took; clamped to what `output` holds, and a
|
|
1003
|
+
* negative count lets nothing go.
|
|
1004
|
+
*/
|
|
979
1005
|
consume(n: i32): void {
|
|
980
1006
|
const held: i32 = this.outputEnd - this.outputStart
|
|
981
1007
|
let take: i32 = n < held ? n : held
|
|
@@ -998,6 +1024,13 @@ export class TlsRecordClient {
|
|
|
998
1024
|
* handshake is under way or nothing fits, `TLS_RECORD_PIPE` once the client
|
|
999
1025
|
* has closed or the connection failed, `TLS_RECORD_INVALID` for a window
|
|
1000
1026
|
* outside `data`.
|
|
1027
|
+
*
|
|
1028
|
+
* @param data The bytes to send; only read, never kept.
|
|
1029
|
+
* @param off Where the bytes start in `data`.
|
|
1030
|
+
* @param len How many bytes to send; with `off`, a window that must lie inside `data`.
|
|
1031
|
+
* @returns How many bytes were sealed (0 only for a `len` of 0 on an open connection with Application
|
|
1032
|
+
* keys and no KeyUpdate pending), or a negative `TLS_RECORD_*` value, whatever `len` is:
|
|
1033
|
+
* `TLS_RECORD_RESET` when the connection failed during this call.
|
|
1001
1034
|
*/
|
|
1002
1035
|
write(data: u8[], off: i32, len: i32): i32 {
|
|
1003
1036
|
if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
|
|
@@ -1049,6 +1082,12 @@ export class TlsRecordClient {
|
|
|
1049
1082
|
* 0; `TLS_RECORD_AGAIN` when nothing is waiting; `TLS_RECORD_RESET` once
|
|
1050
1083
|
* the connection has failed; `TLS_RECORD_INVALID` for a window outside
|
|
1051
1084
|
* `buf`.
|
|
1085
|
+
*
|
|
1086
|
+
* @param buf Where the bytes go.
|
|
1087
|
+
* @param off Where in `buf` the first byte goes.
|
|
1088
|
+
* @param len The most bytes to copy; with `off`, a window that must lie inside `buf`.
|
|
1089
|
+
* @returns How many bytes were copied, 0 at the server's `close_notify` or for a `len` of 0, or a
|
|
1090
|
+
* negative `TLS_RECORD_*` value as above.
|
|
1052
1091
|
*/
|
|
1053
1092
|
read(buf: u8[], off: i32, len: i32): i32 {
|
|
1054
1093
|
if (!tlsRecordWindowFits(toI32(buf.length), off, len)) {
|
|
@@ -1104,12 +1143,12 @@ export class TlsRecordClient {
|
|
|
1104
1143
|
}
|
|
1105
1144
|
|
|
1106
1145
|
/** Whether the client closed before its Finished was sealed, so the server's records are not read. */
|
|
1107
|
-
abandoned(): boolean {
|
|
1146
|
+
private abandoned(): boolean {
|
|
1108
1147
|
return this.state === TLS_RECORD_STATE_CLOSED && this.writeLevel !== TLS_LEVEL_APPLICATION
|
|
1109
1148
|
}
|
|
1110
1149
|
|
|
1111
1150
|
/** Seals the `close_notify` `close` owes once `output` has room, then wipes the write direction. */
|
|
1112
|
-
sealClose(): void {
|
|
1151
|
+
private sealClose(): void {
|
|
1113
1152
|
if (this.closeDue && this.sendAlert(TLS_ALERT_LEVEL_WARNING, TLS_ALERT_CLOSE_NOTIFY)) {
|
|
1114
1153
|
this.closeDue = false
|
|
1115
1154
|
this.wipeWrite()
|
|
@@ -1124,6 +1163,10 @@ export class TlsRecordClient {
|
|
|
1124
1163
|
* sent. Answers 0, or a non-zero alert once the connection has failed
|
|
1125
1164
|
* (`failure`) — a verdict given before one is due answers
|
|
1126
1165
|
* `internal_error`.
|
|
1166
|
+
*
|
|
1167
|
+
* @param ok True when the caller accepts the server's certificate chain and CertificateVerify.
|
|
1168
|
+
* @returns 0 when the handshake goes on, or the alert the connection failed with; `internal_error`,
|
|
1169
|
+
* without failing it, on a connection already open or closed.
|
|
1127
1170
|
*/
|
|
1128
1171
|
authenticate(ok: boolean): i32 {
|
|
1129
1172
|
if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
|
|
@@ -1139,7 +1182,15 @@ export class TlsRecordClient {
|
|
|
1139
1182
|
return this.state === TLS_RECORD_STATE_FAILED ? this.failure() : 0
|
|
1140
1183
|
}
|
|
1141
1184
|
|
|
1142
|
-
/**
|
|
1185
|
+
/**
|
|
1186
|
+
* Refuses the server with the caller's `alert` (`TlsClient.reject`), sent; answers the alert.
|
|
1187
|
+
*
|
|
1188
|
+
* @param alert The TLS alert to send, a code from 1 to 255; `TlsClient.reject` takes any other value
|
|
1189
|
+
* as `bad_certificate`.
|
|
1190
|
+
* @returns The alert the connection failed with (see `TlsClient.reject`); when the connection is not
|
|
1191
|
+
* in its handshake, nothing is sent and the answer is `failure` for a failed connection, else
|
|
1192
|
+
* `internal_error`.
|
|
1193
|
+
*/
|
|
1143
1194
|
reject(alert: i32): i32 {
|
|
1144
1195
|
if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
|
|
1145
1196
|
return this.state === TLS_RECORD_STATE_FAILED ? this.failure() : TLS_ALERT_INTERNAL_ERROR
|
|
@@ -1153,6 +1204,8 @@ export class TlsRecordClient {
|
|
|
1153
1204
|
* Why a failed connection failed, never 0: the alert the client sent, else
|
|
1154
1205
|
* the server's error alert, else `internal_error` for a failure with no
|
|
1155
1206
|
* alert of its own (truncation, `abort`, `close_notify` mid-handshake).
|
|
1207
|
+
*
|
|
1208
|
+
* @returns A TLS alert code, never 0.
|
|
1156
1209
|
*/
|
|
1157
1210
|
failure(): i32 {
|
|
1158
1211
|
if (this.alert !== 0) {
|
|
@@ -1165,6 +1218,8 @@ export class TlsRecordClient {
|
|
|
1165
1218
|
* What the connection wants next, as `TLS_RECORD_*` bits: read the socket,
|
|
1166
1219
|
* write it, `read`, the caller's verdict on the server (`TLS_RECORD_AUTH`),
|
|
1167
1220
|
* or nothing more ever (`TLS_RECORD_DONE`).
|
|
1221
|
+
*
|
|
1222
|
+
* @returns The `TLS_RECORD_*` interest bits OR-ed together.
|
|
1168
1223
|
*/
|
|
1169
1224
|
interest(): i32 {
|
|
1170
1225
|
let bits: i32 = 0
|