nish 0.44.0 → 0.45.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/README.md +1 -1
- package/docs/AI.md +18 -16
- package/package.json +5 -5
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
|
@@ -147,6 +147,9 @@ const TLS_MESSAGE_HEADER_SIZE: i32 = 4
|
|
|
147
147
|
/** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
|
|
148
148
|
const TLS_RECORD_CLIENT_FROM: i32 = 0
|
|
149
149
|
|
|
150
|
+
/** The alert a negative answer of the record layer stands for: `code` is minus an alert, so the negation never wraps. */
|
|
151
|
+
const tlsClientAlertOf = (code: i32): i32 => toI32(0 - toI64(code))
|
|
152
|
+
|
|
150
153
|
/**
|
|
151
154
|
* Installs `protection` under `suite` from the traffic secret `held`, from a
|
|
152
155
|
* plain copy that dies with the arena block, zeroed; false, as `install`
|
|
@@ -238,6 +241,10 @@ export class TlsRecordClient {
|
|
|
238
241
|
private pendingHandshakeAt: i32 = 0
|
|
239
242
|
/** How many bytes of the current post-handshake message's header or KeyUpdate are in `postHandshake`. */
|
|
240
243
|
private postHandshakeLength: i32 = 0
|
|
244
|
+
/** The type of the post-handshake message in `postHandshake`, kept as its first byte arrives. */
|
|
245
|
+
private postType: i32 = 0
|
|
246
|
+
/** The 24-bit body length of that message, as much of it as has arrived. */
|
|
247
|
+
private postBody: i32 = 0
|
|
241
248
|
/** How many body bytes of a NewSessionTicket are still to come, handed to `TlsClient` as they arrive. */
|
|
242
249
|
private ticketLeft: i32 = 0
|
|
243
250
|
private idleRecords: i32 = 0
|
|
@@ -460,7 +467,7 @@ export class TlsRecordClient {
|
|
|
460
467
|
}
|
|
461
468
|
const n: i32 = this.reader.next()
|
|
462
469
|
if (n < 0) {
|
|
463
|
-
this.fail(
|
|
470
|
+
this.fail(tlsClientAlertOf(n))
|
|
464
471
|
return
|
|
465
472
|
}
|
|
466
473
|
if (n === 0) {
|
|
@@ -483,13 +490,31 @@ export class TlsRecordClient {
|
|
|
483
490
|
/** Whether a whole record's content fits beside what the caller has not read, moving that to the front when it would then fit. */
|
|
484
491
|
private plainHasRoom(): boolean {
|
|
485
492
|
const capacity: i32 = TLS_RECORD_CLIENT_PLAIN_SIZE
|
|
486
|
-
|
|
493
|
+
const start: i32 = this.plainStart
|
|
494
|
+
const end: i32 = this.plainEnd
|
|
495
|
+
// Both ends stay inside `plain`'s 2 * 2^14 bytes: a state no path reaches fails the connection.
|
|
496
|
+
if (start < 0 || start > 32768 || end < 0 || end > 32768 || start > end) {
|
|
497
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
487
498
|
return false
|
|
488
499
|
}
|
|
489
|
-
if (capacity -
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
500
|
+
if (capacity - (end - start) < TLS_MAX_PLAINTEXT) {
|
|
501
|
+
return false
|
|
502
|
+
}
|
|
503
|
+
if (capacity - end < TLS_MAX_PLAINTEXT) {
|
|
504
|
+
const held: i32 = end - start
|
|
505
|
+
const plain: u8[] = this.plain
|
|
506
|
+
// What is held lies inside the buffer it is held in, so the move below
|
|
507
|
+
// copies all of it; the guards inside the loop are the prover's.
|
|
508
|
+
if (end > toI32(plain.length)) {
|
|
509
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
510
|
+
return false
|
|
511
|
+
}
|
|
512
|
+
for (let k: i32 = 0; k < held && k < toI32(plain.length); k++) {
|
|
513
|
+
const from: i32 = start + k
|
|
514
|
+
if (from >= toI32(plain.length)) {
|
|
515
|
+
break
|
|
516
|
+
}
|
|
517
|
+
plain[k] = plain[from]
|
|
493
518
|
}
|
|
494
519
|
this.plainStart = 0
|
|
495
520
|
this.plainEnd = held
|
|
@@ -499,8 +524,13 @@ export class TlsRecordClient {
|
|
|
499
524
|
|
|
500
525
|
/** Counts a record that carried nothing for the caller: 0, or `unexpected_message` once there are too many in a row. */
|
|
501
526
|
private idle(): i32 {
|
|
502
|
-
|
|
503
|
-
|
|
527
|
+
const seen: i32 = this.idleRecords
|
|
528
|
+
// The count ends the connection just past `TLS_RECORD_IDLE_LIMIT`, so it never nears 2^30.
|
|
529
|
+
if (seen > 1073741823) {
|
|
530
|
+
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
531
|
+
}
|
|
532
|
+
this.idleRecords = seen + 1
|
|
533
|
+
return seen + 1 > TLS_RECORD_IDLE_LIMIT ? TLS_ALERT_UNEXPECTED_MESSAGE : 0
|
|
504
534
|
}
|
|
505
535
|
|
|
506
536
|
/**
|
|
@@ -510,12 +540,13 @@ export class TlsRecordClient {
|
|
|
510
540
|
* server's Finished is read, to the handshake before. Answers 0 or the alert.
|
|
511
541
|
*/
|
|
512
542
|
private handleRecord(buf: u8[], off: i32, n: i32): i32 {
|
|
513
|
-
|
|
514
|
-
|
|
543
|
+
// A record outside `buf` is `open`'s to refuse.
|
|
544
|
+
if (off >= 0 && off < toI32(buf.length) && toI32(buf[off]) === TLS_CONTENT_CHANGE_CIPHER_SPEC) {
|
|
545
|
+
return this.handleChangeCipherSpec(buf, off, n)
|
|
515
546
|
}
|
|
516
547
|
const length: i32 = this.readProtection.open(buf, off, n, this.scratch, TLS_RECORD_CLIENT_FROM)
|
|
517
548
|
if (length < 0) {
|
|
518
|
-
return
|
|
549
|
+
return tlsClientAlertOf(length)
|
|
519
550
|
}
|
|
520
551
|
if (this.readLevel === TLS_LEVEL_APPLICATION) {
|
|
521
552
|
return this.handleApplicationRecord(length)
|
|
@@ -537,10 +568,15 @@ export class TlsRecordClient {
|
|
|
537
568
|
* The server's compatibility record (§5, §D.4): one byte of 1, in the
|
|
538
569
|
* clear, after its first handshake record and before its Finished, and not
|
|
539
570
|
* between the records of one handshake message. Dropped; anything else is
|
|
540
|
-
* `unexpected_message`.
|
|
571
|
+
* `unexpected_message`. The record, header and all, is `buf[off .. off + n)`.
|
|
541
572
|
*/
|
|
542
|
-
private handleChangeCipherSpec(buf: u8[], off: i32,
|
|
543
|
-
|
|
573
|
+
private handleChangeCipherSpec(buf: u8[], off: i32, n: i32): i32 {
|
|
574
|
+
// A record starts inside the reader's buffer of fewer than 2^15 bytes: the bound refuses none it hands over.
|
|
575
|
+
if (n !== TLS_RECORD_HEADER_SIZE + 1 || off < 0 || off > 1073741823) {
|
|
576
|
+
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
577
|
+
}
|
|
578
|
+
const at: i32 = off + TLS_RECORD_HEADER_SIZE
|
|
579
|
+
if (at >= toI32(buf.length) || toI32(buf[at]) !== 1) {
|
|
544
580
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
545
581
|
}
|
|
546
582
|
if (!this.started || this.readLevel === TLS_LEVEL_APPLICATION || this.tls.inputLength > 0) {
|
|
@@ -579,10 +615,27 @@ export class TlsRecordClient {
|
|
|
579
615
|
if (length === 0) {
|
|
580
616
|
return this.idle()
|
|
581
617
|
}
|
|
582
|
-
|
|
583
|
-
|
|
618
|
+
const end: i32 = this.plainEnd
|
|
619
|
+
// `plainHasRoom` left a record's room past `end`, and `open` wrote at most 2^14 bytes: the bound
|
|
620
|
+
// refuses only a state no path reaches.
|
|
621
|
+
if (end < 0 || end > 32768 || length < 0 || length > 16384) {
|
|
622
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
623
|
+
}
|
|
624
|
+
const plain: u8[] = this.plain
|
|
625
|
+
const scratch: u8[] = this.scratch
|
|
626
|
+
// The whole record lands, or the connection fails: the guards inside
|
|
627
|
+
// the loop are the prover's, and never drop a byte past this check.
|
|
628
|
+
if (end + length > toI32(plain.length) || length > toI32(scratch.length)) {
|
|
629
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
630
|
+
}
|
|
631
|
+
for (let k: i32 = 0; k < length && k < toI32(scratch.length); k++) {
|
|
632
|
+
const to: i32 = end + k
|
|
633
|
+
if (to >= toI32(plain.length)) {
|
|
634
|
+
break
|
|
635
|
+
}
|
|
636
|
+
plain[to] = scratch[k]
|
|
584
637
|
}
|
|
585
|
-
this.plainEnd =
|
|
638
|
+
this.plainEnd = end + length
|
|
586
639
|
this.idleRecords = 0
|
|
587
640
|
return 0
|
|
588
641
|
}
|
|
@@ -604,10 +657,11 @@ export class TlsRecordClient {
|
|
|
604
657
|
* the connection with nothing more sent and every key wiped.
|
|
605
658
|
*/
|
|
606
659
|
private handleAlert(length: i32): i32 {
|
|
607
|
-
|
|
660
|
+
const scratch: u8[] = this.scratch
|
|
661
|
+
if (length !== 2 || toI32(scratch.length) < 2) {
|
|
608
662
|
return TLS_ALERT_DECODE_ERROR
|
|
609
663
|
}
|
|
610
|
-
const description: i32 = toI32(
|
|
664
|
+
const description: i32 = toI32(scratch[1])
|
|
611
665
|
if (description === TLS_ALERT_CLOSE_NOTIFY && this.state !== TLS_RECORD_STATE_HANDSHAKE) {
|
|
612
666
|
this.peerClosed = true
|
|
613
667
|
this.wipeRead()
|
|
@@ -636,44 +690,70 @@ export class TlsRecordClient {
|
|
|
636
690
|
* limit.
|
|
637
691
|
*/
|
|
638
692
|
private handlePostHandshake(length: i32): i32 {
|
|
693
|
+
const scratch: u8[] = this.scratch
|
|
694
|
+
const message: u8[] = this.postHandshake
|
|
695
|
+
// `open` writes at most 2^14 bytes into `scratch`, and `message` holds a whole KeyUpdate: the bounds
|
|
696
|
+
// refuse only a state no path reaches.
|
|
697
|
+
if (length < 0 || length > 16384 || toI32(message.length) < TLS_KEY_UPDATE_SIZE) {
|
|
698
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
699
|
+
}
|
|
639
700
|
let k: i32 = 0
|
|
640
|
-
|
|
641
|
-
|
|
701
|
+
// `length` is at most 2^14, so the last bound never ends the walk early.
|
|
702
|
+
while (k >= 0 && k < length && k < toI32(scratch.length) && k < 16384) {
|
|
703
|
+
const ticketLeft: i32 = this.ticketLeft
|
|
704
|
+
// A ticket's body length is 24 bits.
|
|
705
|
+
if (ticketLeft < 0 || ticketLeft > 16777215) {
|
|
706
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
707
|
+
}
|
|
708
|
+
if (ticketLeft > 0) {
|
|
642
709
|
const left: i32 = length - k
|
|
643
|
-
|
|
644
|
-
|
|
710
|
+
let take: i32 = ticketLeft
|
|
711
|
+
if (left > 0 && left < take) {
|
|
712
|
+
take = left
|
|
713
|
+
}
|
|
714
|
+
const alert: i32 = this.tls.receive(TLS_LEVEL_APPLICATION, scratch, k, take)
|
|
645
715
|
if (alert !== 0) {
|
|
646
716
|
return alert
|
|
647
717
|
}
|
|
648
|
-
this.ticketLeft =
|
|
718
|
+
this.ticketLeft = ticketLeft - take
|
|
649
719
|
k = k + take
|
|
650
720
|
continue
|
|
651
721
|
}
|
|
652
|
-
|
|
722
|
+
const have: i32 = this.postHandshakeLength
|
|
723
|
+
// A message is handled at its fifth byte, so no more than four are ever held before one.
|
|
724
|
+
if (have < 0 || have > 4) {
|
|
725
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
726
|
+
}
|
|
727
|
+
const byte: u8 = scratch[k]
|
|
728
|
+
if (have === 0) {
|
|
653
729
|
const idle: i32 = this.idle()
|
|
654
730
|
if (idle !== 0) {
|
|
655
731
|
return idle
|
|
656
732
|
}
|
|
657
733
|
}
|
|
658
|
-
|
|
659
|
-
|
|
734
|
+
// Only the 4-byte header is kept; the body's bytes are counted, not stored.
|
|
735
|
+
if (have < toI32(message.length)) {
|
|
736
|
+
message[have] = byte
|
|
660
737
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
738
|
+
const held: i32 = have + 1
|
|
739
|
+
this.postHandshakeLength = held
|
|
740
|
+
if (held === 1) {
|
|
741
|
+
this.postType = toI32(byte)
|
|
742
|
+
this.postBody = 0
|
|
743
|
+
} else if (held <= TLS_MESSAGE_HEADER_SIZE) {
|
|
744
|
+
this.postBody = (this.postBody << 8) | toI32(byte)
|
|
745
|
+
}
|
|
746
|
+
const type: i32 = this.postType
|
|
664
747
|
if (held === 1 && type !== TLS_HANDSHAKE_KEY_UPDATE && type !== TLS_HANDSHAKE_NEW_SESSION_TICKET) {
|
|
665
748
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
666
749
|
}
|
|
667
750
|
if (held === TLS_MESSAGE_HEADER_SIZE) {
|
|
668
|
-
const body: i32 =
|
|
669
|
-
(toI32(this.postHandshake[1]) << 16) |
|
|
670
|
-
(toI32(this.postHandshake[2]) << 8) |
|
|
671
|
-
toI32(this.postHandshake[3])
|
|
751
|
+
const body: i32 = this.postBody
|
|
672
752
|
if (type === TLS_HANDSHAKE_NEW_SESSION_TICKET) {
|
|
673
753
|
// The header goes to `TlsClient` with the body, which it reads whole.
|
|
674
754
|
const alert: i32 = this.tls.receive(
|
|
675
755
|
TLS_LEVEL_APPLICATION,
|
|
676
|
-
|
|
756
|
+
message,
|
|
677
757
|
TLS_RECORD_CLIENT_FROM,
|
|
678
758
|
TLS_MESSAGE_HEADER_SIZE
|
|
679
759
|
)
|
|
@@ -687,7 +767,7 @@ export class TlsRecordClient {
|
|
|
687
767
|
}
|
|
688
768
|
}
|
|
689
769
|
if (held === TLS_KEY_UPDATE_SIZE) {
|
|
690
|
-
const request: i32 = toI32(
|
|
770
|
+
const request: i32 = toI32(byte)
|
|
691
771
|
if (request > 1) {
|
|
692
772
|
return TLS_ALERT_ILLEGAL_PARAMETER
|
|
693
773
|
}
|
|
@@ -695,10 +775,12 @@ export class TlsRecordClient {
|
|
|
695
775
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
696
776
|
}
|
|
697
777
|
this.postHandshakeLength = 0
|
|
698
|
-
|
|
778
|
+
const updates: i32 = this.keyUpdates
|
|
779
|
+
// The count stops at `TLS_RECORD_MAX_KEY_UPDATES`, far below 2^30.
|
|
780
|
+
if (updates >= TLS_RECORD_MAX_KEY_UPDATES || updates < 0 || updates > 1073741823) {
|
|
699
781
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
700
782
|
}
|
|
701
|
-
this.keyUpdates =
|
|
783
|
+
this.keyUpdates = updates + 1
|
|
702
784
|
return this.updateReadKeys(request === 1)
|
|
703
785
|
}
|
|
704
786
|
k = k + 1
|
|
@@ -761,20 +843,58 @@ export class TlsRecordClient {
|
|
|
761
843
|
/** Whether `need` more bytes fit in `output`, moving what is unsent to the front first. */
|
|
762
844
|
private outputHasRoom(need: i32): boolean {
|
|
763
845
|
const capacity: i32 = TLS_RECORD_CLIENT_OUTPUT_SIZE
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
846
|
+
const start: i32 = this.outputStart
|
|
847
|
+
const end: i32 = this.outputEnd
|
|
848
|
+
// Both ends stay inside `output`'s two records, and no record needs 2^30 bytes: a state no path
|
|
849
|
+
// reaches fails the connection.
|
|
850
|
+
if (
|
|
851
|
+
start < 0 ||
|
|
852
|
+
start > 33290 ||
|
|
853
|
+
end < 0 ||
|
|
854
|
+
end > 33290 ||
|
|
855
|
+
start > end ||
|
|
856
|
+
need < 0 ||
|
|
857
|
+
need > 1073741823
|
|
858
|
+
) {
|
|
859
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
860
|
+
return false
|
|
861
|
+
}
|
|
862
|
+
if (end + need <= capacity) {
|
|
863
|
+
return true
|
|
864
|
+
}
|
|
865
|
+
if (start === 0) {
|
|
866
|
+
return false
|
|
867
|
+
}
|
|
868
|
+
const held: i32 = end - start
|
|
869
|
+
const output: u8[] = this.output
|
|
870
|
+
// What is unsent lies inside the buffer it is held in, so the move below
|
|
871
|
+
// copies all of it; the guards inside the loop are the prover's.
|
|
872
|
+
if (end > toI32(output.length)) {
|
|
873
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
874
|
+
return false
|
|
875
|
+
}
|
|
876
|
+
for (let k: i32 = 0; k < held && k < toI32(output.length); k++) {
|
|
877
|
+
const from: i32 = start + k
|
|
878
|
+
if (from >= toI32(output.length)) {
|
|
879
|
+
break
|
|
768
880
|
}
|
|
769
|
-
|
|
770
|
-
this.outputEnd = held
|
|
881
|
+
output[k] = output[from]
|
|
771
882
|
}
|
|
772
|
-
|
|
883
|
+
this.outputStart = 0
|
|
884
|
+
this.outputEnd = held
|
|
885
|
+
return held + need <= capacity
|
|
773
886
|
}
|
|
774
887
|
|
|
775
|
-
/**
|
|
888
|
+
/**
|
|
889
|
+
* The most content one record may carry under the write protection as it stands (§5.4): 0, which
|
|
890
|
+
* the callers refuse with `internal_error`, for a `padding` that leaves no room or is negative.
|
|
891
|
+
*/
|
|
776
892
|
private maxChunk(): i32 {
|
|
777
|
-
|
|
893
|
+
const padding: i32 = this.writePadding()
|
|
894
|
+
if (padding < 0 || padding > 16384) {
|
|
895
|
+
return 0
|
|
896
|
+
}
|
|
897
|
+
return TLS_MAX_PLAINTEXT - padding
|
|
778
898
|
}
|
|
779
899
|
|
|
780
900
|
/** The padding the next record carries: `padding` once records are protected, none in the clear. */
|
|
@@ -782,34 +902,52 @@ export class TlsRecordClient {
|
|
|
782
902
|
return this.writeProtection.isProtected() ? this.padding : 0
|
|
783
903
|
}
|
|
784
904
|
|
|
785
|
-
/**
|
|
786
|
-
|
|
905
|
+
/**
|
|
906
|
+
* The room a record takes in `output` past its content under the write protection as it stands: -1
|
|
907
|
+
* for a `padding` `seal` refuses.
|
|
908
|
+
*/
|
|
909
|
+
private recordOverhead(): i32 {
|
|
787
910
|
if (!this.writeProtection.isProtected()) {
|
|
788
|
-
return TLS_RECORD_HEADER_SIZE
|
|
911
|
+
return TLS_RECORD_HEADER_SIZE
|
|
789
912
|
}
|
|
790
|
-
|
|
913
|
+
const padding: i32 = this.padding
|
|
914
|
+
if (padding < 0 || padding > 16384) {
|
|
915
|
+
return -1
|
|
916
|
+
}
|
|
917
|
+
return TLS_RECORD_HEADER_SIZE + 1 + padding + TLS_RECORD_TAG_SIZE
|
|
791
918
|
}
|
|
792
919
|
|
|
793
920
|
/**
|
|
794
921
|
* Seals one record of `type` from `data[off .. off + len)` onto `output`
|
|
795
922
|
* under `protection`. False, writing nothing, when it does not fit; a
|
|
796
|
-
* refusal from `seal` itself fails the connection
|
|
923
|
+
* refusal from `seal` itself fails the connection, as does a `len` or
|
|
924
|
+
* `padding` outside 0 to 2^14, which `seal` refuses.
|
|
797
925
|
*/
|
|
798
926
|
private sealWith(protection: TlsRecordProtection, type: i32, data: u8[], off: i32, len: i32): boolean {
|
|
799
927
|
const sealed: boolean = protection.isProtected()
|
|
800
928
|
const padding: i32 = sealed ? this.padding : 0
|
|
929
|
+
if (len < 0 || len > 16384 || padding < 0 || padding > 16384) {
|
|
930
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
931
|
+
return false
|
|
932
|
+
}
|
|
801
933
|
const room: i32 = sealed
|
|
802
934
|
? TLS_RECORD_HEADER_SIZE + len + 1 + padding + TLS_RECORD_TAG_SIZE
|
|
803
935
|
: TLS_RECORD_HEADER_SIZE + len
|
|
804
936
|
if (!this.outputHasRoom(room)) {
|
|
805
937
|
return false
|
|
806
938
|
}
|
|
807
|
-
const
|
|
939
|
+
const end: i32 = this.outputEnd
|
|
940
|
+
const n: i32 = protection.seal(type, data, off, len, padding, this.output, end)
|
|
808
941
|
if (n < 0) {
|
|
809
|
-
this.fail(
|
|
942
|
+
this.fail(tlsClientAlertOf(n))
|
|
810
943
|
return false
|
|
811
944
|
}
|
|
812
|
-
|
|
945
|
+
// `seal` writes inside `output`, whose ends `outputHasRoom` keeps below 2^30.
|
|
946
|
+
if (end < 0 || end > 1073741823 || n > 1073741823) {
|
|
947
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
948
|
+
return false
|
|
949
|
+
}
|
|
950
|
+
this.outputEnd = end + n
|
|
813
951
|
return true
|
|
814
952
|
}
|
|
815
953
|
|
|
@@ -828,7 +966,6 @@ export class TlsRecordClient {
|
|
|
828
966
|
}
|
|
829
967
|
const ccs: u8[] = [toU8(1)]
|
|
830
968
|
const one: i32 = 1
|
|
831
|
-
// `recordRoom` counts the protected size, which is more than the clear record needs.
|
|
832
969
|
if (
|
|
833
970
|
!this.sealWith(this.clearProtection, TLS_CONTENT_CHANGE_CIPHER_SPEC, ccs, TLS_RECORD_CLIENT_FROM, one)
|
|
834
971
|
) {
|
|
@@ -846,13 +983,20 @@ export class TlsRecordClient {
|
|
|
846
983
|
private sealQueue(queue: u8[], at: i32, length: i32): i32 {
|
|
847
984
|
let next: i32 = at
|
|
848
985
|
const most: i32 = this.maxChunk()
|
|
849
|
-
if (
|
|
986
|
+
if (next >= length) {
|
|
987
|
+
return next
|
|
988
|
+
}
|
|
989
|
+
// `TlsClient`'s queues are far below 2^30 bytes, and a position in one is never negative.
|
|
990
|
+
if (most < 1 || most > 16384 || at < 0 || length < 0 || length > 1073741823) {
|
|
850
991
|
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
851
992
|
return next
|
|
852
993
|
}
|
|
853
|
-
while (next < length && this.state !== TLS_RECORD_STATE_FAILED) {
|
|
994
|
+
while (next >= 0 && next < length && this.state !== TLS_RECORD_STATE_FAILED) {
|
|
854
995
|
const left: i32 = length - next
|
|
855
|
-
|
|
996
|
+
let chunk: i32 = most
|
|
997
|
+
if (left < chunk) {
|
|
998
|
+
chunk = left
|
|
999
|
+
}
|
|
856
1000
|
if (!this.sealRecord(TLS_CONTENT_HANDSHAKE, queue, next, chunk)) {
|
|
857
1001
|
return next
|
|
858
1002
|
}
|
|
@@ -1015,13 +1159,22 @@ export class TlsRecordClient {
|
|
|
1015
1159
|
* negative count lets nothing go.
|
|
1016
1160
|
*/
|
|
1017
1161
|
consume(n: i32): void {
|
|
1018
|
-
const
|
|
1162
|
+
const start: i32 = this.outputStart
|
|
1163
|
+
const end: i32 = this.outputEnd
|
|
1164
|
+
// Both ends stay inside `output`'s two records: the bound refuses only a state no path reaches.
|
|
1165
|
+
if (start < 0 || start > 33290 || end < 0 || end > 33290) {
|
|
1166
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1167
|
+
return
|
|
1168
|
+
}
|
|
1169
|
+
const held: i32 = end - start
|
|
1019
1170
|
let take: i32 = n < held ? n : held
|
|
1020
1171
|
if (take < 0) {
|
|
1021
1172
|
take = 0
|
|
1022
1173
|
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1174
|
+
// `take` is 0 .. held, so the sum stays inside `output` and never wraps.
|
|
1175
|
+
const at: i32 = toI32(toI64(start) + toI64(take))
|
|
1176
|
+
this.outputStart = at
|
|
1177
|
+
if (at === end) {
|
|
1025
1178
|
this.outputStart = 0
|
|
1026
1179
|
this.outputEnd = 0
|
|
1027
1180
|
}
|
|
@@ -1060,27 +1213,43 @@ export class TlsRecordClient {
|
|
|
1060
1213
|
return this.state === TLS_RECORD_STATE_FAILED ? TLS_RECORD_RESET : TLS_RECORD_AGAIN
|
|
1061
1214
|
}
|
|
1062
1215
|
let done: i32 = 0
|
|
1063
|
-
|
|
1216
|
+
// `tlsRecordWindowFits` refused a negative `off` or `len`, so `done` never leaves 0 .. len.
|
|
1217
|
+
while (done >= 0 && done < len && len >= 0 && this.state === TLS_RECORD_STATE_OPEN) {
|
|
1064
1218
|
if (this.writeProtection.sequence >= this.writeProtection.recordLimit && !this.sendKeyUpdate(false)) {
|
|
1065
1219
|
break
|
|
1066
1220
|
}
|
|
1067
1221
|
const most: i32 = this.maxChunk()
|
|
1068
|
-
|
|
1222
|
+
const overhead: i32 = this.recordOverhead()
|
|
1223
|
+
// A record carries at most 2^14 bytes of content and 2^14 + 22 past it.
|
|
1224
|
+
if (most < 1 || most > 16384 || overhead < 0 || overhead > 16406) {
|
|
1069
1225
|
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1070
1226
|
break
|
|
1071
1227
|
}
|
|
1072
1228
|
const left: i32 = len - done
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1229
|
+
// `most` is at most 2^14, so a longer `left` takes `most`.
|
|
1230
|
+
let chunk: i32 = most
|
|
1231
|
+
if (left < 16384 && left < chunk) {
|
|
1232
|
+
chunk = left
|
|
1233
|
+
}
|
|
1234
|
+
this.outputHasRoom(overhead + chunk)
|
|
1235
|
+
const end: i32 = this.outputEnd
|
|
1236
|
+
// `outputHasRoom` keeps the end inside `output`'s two records.
|
|
1237
|
+
if (end < 0 || end > 33290) {
|
|
1238
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1239
|
+
break
|
|
1240
|
+
}
|
|
1241
|
+
const room: i32 = TLS_RECORD_CLIENT_OUTPUT_SIZE - end - overhead
|
|
1077
1242
|
if (room < chunk) {
|
|
1078
1243
|
chunk = room
|
|
1079
1244
|
}
|
|
1080
|
-
|
|
1245
|
+
// The window fits in `data`, so neither sum below wraps.
|
|
1246
|
+
if (
|
|
1247
|
+
chunk <= 0 ||
|
|
1248
|
+
!this.sealRecord(TLS_CONTENT_APPLICATION_DATA, data, toI32(toI64(off) + toI64(done)), chunk)
|
|
1249
|
+
) {
|
|
1081
1250
|
break
|
|
1082
1251
|
}
|
|
1083
|
-
done = done + chunk
|
|
1252
|
+
done = toI32(toI64(done) + toI64(chunk))
|
|
1084
1253
|
}
|
|
1085
1254
|
if (done === 0 && len > 0) {
|
|
1086
1255
|
return this.state === TLS_RECORD_STATE_FAILED ? TLS_RECORD_RESET : TLS_RECORD_AGAIN
|
|
@@ -1108,14 +1277,37 @@ export class TlsRecordClient {
|
|
|
1108
1277
|
if (len === 0) {
|
|
1109
1278
|
return 0
|
|
1110
1279
|
}
|
|
1111
|
-
const
|
|
1280
|
+
const start: i32 = this.plainStart
|
|
1281
|
+
const end: i32 = this.plainEnd
|
|
1282
|
+
// Both ends stay inside `plain`'s 2 * 2^14 bytes: a state no path reaches fails the connection.
|
|
1283
|
+
if (start < 0 || start > 32768 || end < 0 || end > 32768) {
|
|
1284
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1285
|
+
return TLS_RECORD_RESET
|
|
1286
|
+
}
|
|
1287
|
+
const avail: i32 = end - start
|
|
1112
1288
|
if (avail > 0) {
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1289
|
+
let n: i32 = avail
|
|
1290
|
+
if (len < n) {
|
|
1291
|
+
n = len
|
|
1292
|
+
}
|
|
1293
|
+
const plain: u8[] = this.plain
|
|
1294
|
+
// All `n` bytes are copied or none: the guards inside the loop are the
|
|
1295
|
+
// prover's, and never skip a byte past this check.
|
|
1296
|
+
if (end > toI32(plain.length) || toI64(off) + toI64(n) > toI64(buf.length)) {
|
|
1297
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1298
|
+
return TLS_RECORD_RESET
|
|
1299
|
+
}
|
|
1300
|
+
for (let k: i32 = 0; k < n; k++) {
|
|
1301
|
+
// The window fits in `buf`, so the sum never wraps.
|
|
1302
|
+
const at: i32 = toI32(toI64(off) + toI64(k))
|
|
1303
|
+
const from: i32 = start + k
|
|
1304
|
+
if (at < 0 || at >= toI32(buf.length) || from >= toI32(plain.length)) {
|
|
1305
|
+
break
|
|
1306
|
+
}
|
|
1307
|
+
buf[at] = plain[from]
|
|
1116
1308
|
}
|
|
1117
|
-
this.plainStart =
|
|
1118
|
-
if (
|
|
1309
|
+
this.plainStart = start + n
|
|
1310
|
+
if (start + n === end) {
|
|
1119
1311
|
this.plainStart = 0
|
|
1120
1312
|
this.plainEnd = 0
|
|
1121
1313
|
}
|