@amritk/nish-aarch64-linux 0.44.0 → 0.46.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 +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- 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 +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- 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 +1315 -222
- 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/sqlite.ts +171 -0
- 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
|
@@ -214,7 +214,14 @@ export class TlsRecordPools {
|
|
|
214
214
|
* @returns The buffers lent and not yet given back, summed over the four pools.
|
|
215
215
|
*/
|
|
216
216
|
lent(): i32 {
|
|
217
|
-
|
|
217
|
+
// Each pool lends at most its ceiling of buffers of 16 KiB or more, so
|
|
218
|
+
// four counts together never come near 2^31 and the sum never wraps.
|
|
219
|
+
return toI32(
|
|
220
|
+
toI64(this.output.lent()) +
|
|
221
|
+
toI64(this.plain.lent()) +
|
|
222
|
+
toI64(this.scratch.lent()) +
|
|
223
|
+
toI64(this.reader.lent())
|
|
224
|
+
)
|
|
218
225
|
}
|
|
219
226
|
|
|
220
227
|
/**
|
|
@@ -223,7 +230,10 @@ export class TlsRecordPools {
|
|
|
223
230
|
* @returns The buffers ever made, summed over the four pools.
|
|
224
231
|
*/
|
|
225
232
|
made(): i32 {
|
|
226
|
-
|
|
233
|
+
// As in `lent`: buffers of 16 KiB or more, so the sum never wraps.
|
|
234
|
+
return toI32(
|
|
235
|
+
toI64(this.output.made) + toI64(this.plain.made) + toI64(this.scratch.made) + toI64(this.reader.made)
|
|
236
|
+
)
|
|
227
237
|
}
|
|
228
238
|
}
|
|
229
239
|
|
|
@@ -258,6 +268,9 @@ export const TLS_RECORD_MAX_KEY_UPDATES: i32 = 64
|
|
|
258
268
|
/** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
|
|
259
269
|
const TLS_RECORD_SERVER_FROM: i32 = 0
|
|
260
270
|
|
|
271
|
+
/** The alert a negative answer of the record layer's stands for: `code` is minus an alert, so the negation never wraps. */
|
|
272
|
+
const tlsAlertOf = (code: i32): i32 => toI32(0 - toI64(code))
|
|
273
|
+
|
|
261
274
|
/** Zeroes the bytes a traffic secret field holds, for `exposeWith`; answers 0. */
|
|
262
275
|
const tlsTrafficZero = (held: u8[], answer: i32): i32 => {
|
|
263
276
|
wipe(held)
|
|
@@ -750,7 +763,7 @@ export class TlsRecordServer {
|
|
|
750
763
|
}
|
|
751
764
|
const n: i32 = this.reader.next()
|
|
752
765
|
if (n < 0) {
|
|
753
|
-
this.fail(
|
|
766
|
+
this.fail(tlsAlertOf(n))
|
|
754
767
|
return
|
|
755
768
|
}
|
|
756
769
|
if (n === 0) {
|
|
@@ -777,13 +790,31 @@ export class TlsRecordServer {
|
|
|
777
790
|
*/
|
|
778
791
|
private plainHasRoom(): boolean {
|
|
779
792
|
const capacity: i32 = TLS_RECORD_PLAIN_SIZE
|
|
780
|
-
|
|
793
|
+
const start: i32 = this.plainStart
|
|
794
|
+
const end: i32 = this.plainEnd
|
|
795
|
+
// Both stay inside the 2^15-byte buffer: a record is copied in only
|
|
796
|
+
// after this made room for it, and `read` moves `start` up to `end`.
|
|
797
|
+
if (start < 0 || start > 32768 || end < 0 || end > 32768 || start > end) {
|
|
798
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
781
799
|
return false
|
|
782
800
|
}
|
|
783
|
-
if (capacity -
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
801
|
+
if (capacity - (end - start) < TLS_MAX_PLAINTEXT) {
|
|
802
|
+
return false
|
|
803
|
+
}
|
|
804
|
+
if (capacity - end < TLS_MAX_PLAINTEXT) {
|
|
805
|
+
const held: i32 = end - start
|
|
806
|
+
const plain: u8[] = this.plain
|
|
807
|
+
// What is held lies inside the buffer it is held in, so the move below
|
|
808
|
+
// copies all of it; the guards inside the loop are the prover's.
|
|
809
|
+
if (end > toI32(plain.length)) {
|
|
810
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
811
|
+
return false
|
|
812
|
+
}
|
|
813
|
+
for (let k: i32 = 0; k < held && k < toI32(plain.length); k++) {
|
|
814
|
+
const from: i32 = start + k
|
|
815
|
+
if (from < toI32(plain.length)) {
|
|
816
|
+
plain[k] = plain[from]
|
|
817
|
+
}
|
|
787
818
|
}
|
|
788
819
|
this.plainStart = 0
|
|
789
820
|
this.plainEnd = held
|
|
@@ -796,8 +827,14 @@ export class TlsRecordServer {
|
|
|
796
827
|
|
|
797
828
|
/** Counts a record that carried nothing for the caller: 0, or `unexpected_message` once there are too many in a row. */
|
|
798
829
|
private idle(): i32 {
|
|
799
|
-
|
|
800
|
-
|
|
830
|
+
const seen: i32 = this.idleRecords
|
|
831
|
+
// The record past `TLS_RECORD_IDLE_LIMIT` fails the connection, so the
|
|
832
|
+
// count never passes it; the literal bound is the prover's.
|
|
833
|
+
if (seen < 0 || seen >= TLS_RECORD_IDLE_LIMIT || seen > 1073741823) {
|
|
834
|
+
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
835
|
+
}
|
|
836
|
+
this.idleRecords = seen + 1
|
|
837
|
+
return 0
|
|
801
838
|
}
|
|
802
839
|
|
|
803
840
|
/**
|
|
@@ -807,6 +844,10 @@ export class TlsRecordServer {
|
|
|
807
844
|
* handshake is done, to the handshake before. Answers 0 or the alert.
|
|
808
845
|
*/
|
|
809
846
|
private handleRecord(buf: u8[], off: i32, n: i32): i32 {
|
|
847
|
+
// The reader hands over a whole record inside its buffer, header and all.
|
|
848
|
+
if (off < 0 || off >= toI32(buf.length) || n < 5 || n > 16645) {
|
|
849
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
850
|
+
}
|
|
810
851
|
if (toI32(buf[off]) === TLS_CONTENT_CHANGE_CIPHER_SPEC) {
|
|
811
852
|
return this.handleChangeCipherSpec(buf, off, n - TLS_RECORD_HEADER_SIZE)
|
|
812
853
|
}
|
|
@@ -815,7 +856,7 @@ export class TlsRecordServer {
|
|
|
815
856
|
}
|
|
816
857
|
const length: i32 = this.readProtection.open(buf, off, n, this.scratch, TLS_RECORD_SERVER_FROM)
|
|
817
858
|
if (length < 0) {
|
|
818
|
-
return
|
|
859
|
+
return tlsAlertOf(length)
|
|
819
860
|
}
|
|
820
861
|
if (this.readLevel === TLS_LEVEL_APPLICATION) {
|
|
821
862
|
return this.handleApplicationRecord(length)
|
|
@@ -840,7 +881,12 @@ export class TlsRecordServer {
|
|
|
840
881
|
* `unexpected_message`.
|
|
841
882
|
*/
|
|
842
883
|
private handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
|
|
843
|
-
|
|
884
|
+
// A record's offset in the reader's buffer is far below 2^30.
|
|
885
|
+
if (length !== 1 || off < 0 || off > 1073741823) {
|
|
886
|
+
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
887
|
+
}
|
|
888
|
+
const at: i32 = off + TLS_RECORD_HEADER_SIZE
|
|
889
|
+
if (at >= toI32(buf.length) || toI32(buf[at]) !== 1) {
|
|
844
890
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
845
891
|
}
|
|
846
892
|
if (!this.started || this.readLevel === TLS_LEVEL_APPLICATION || this.tls.inputLength > 0) {
|
|
@@ -885,12 +931,29 @@ export class TlsRecordServer {
|
|
|
885
931
|
if (length === 0) {
|
|
886
932
|
return this.idle()
|
|
887
933
|
}
|
|
888
|
-
|
|
889
|
-
|
|
934
|
+
const end: i32 = this.plainEnd
|
|
935
|
+
// `process` made room for a whole record past `end`, and `open`
|
|
936
|
+
// answers at most 2^14 bytes of content.
|
|
937
|
+
if (end < 0 || end > 32768 || length < 0 || length > 16384) {
|
|
938
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
890
939
|
}
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
940
|
+
const plain: u8[] = this.plain
|
|
941
|
+
const scratch: u8[] = this.scratch
|
|
942
|
+
// The whole record lands, or the connection fails: the guards inside
|
|
943
|
+
// the loop are the prover's, and never drop a byte past this check.
|
|
944
|
+
if (end + length > toI32(plain.length) || length > toI32(scratch.length)) {
|
|
945
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
946
|
+
}
|
|
947
|
+
for (let k: i32 = 0; k < length && k < toI32(scratch.length); k++) {
|
|
948
|
+
const to: i32 = end + k
|
|
949
|
+
if (to < toI32(plain.length)) {
|
|
950
|
+
plain[to] = scratch[k]
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
const next: i32 = end + length
|
|
954
|
+
this.plainEnd = next
|
|
955
|
+
if (next > this.plainUsed) {
|
|
956
|
+
this.plainUsed = next
|
|
894
957
|
}
|
|
895
958
|
this.idleRecords = 0
|
|
896
959
|
return 0
|
|
@@ -912,10 +975,11 @@ export class TlsRecordServer {
|
|
|
912
975
|
* nothing more sent.
|
|
913
976
|
*/
|
|
914
977
|
private handleAlert(length: i32): i32 {
|
|
915
|
-
|
|
978
|
+
const scratch: u8[] = this.scratch
|
|
979
|
+
if (length !== 2 || toI32(scratch.length) < 2) {
|
|
916
980
|
return TLS_ALERT_DECODE_ERROR
|
|
917
981
|
}
|
|
918
|
-
const description: i32 = toI32(
|
|
982
|
+
const description: i32 = toI32(scratch[1])
|
|
919
983
|
if (description === TLS_ALERT_CLOSE_NOTIFY) {
|
|
920
984
|
// The client writes nothing after it (§6.1), and nothing after it is
|
|
921
985
|
// read, so the read keys have opened their last record.
|
|
@@ -949,37 +1013,47 @@ export class TlsRecordServer {
|
|
|
949
1013
|
if (idle !== 0) {
|
|
950
1014
|
return idle
|
|
951
1015
|
}
|
|
1016
|
+
const message: u8[] = this.postHandshake
|
|
1017
|
+
const scratch: u8[] = this.scratch
|
|
952
1018
|
// The buffer never holds a whole message: the fifth byte either ends it
|
|
953
|
-
// or is refused, so `postHandshakeLength` stays below its length
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1019
|
+
// or is refused, so `postHandshakeLength` stays below its length of 5,
|
|
1020
|
+
// and the length tests below on reading it always pass.
|
|
1021
|
+
let held: i32 = this.postHandshakeLength
|
|
1022
|
+
if (toI32(message.length) < 5 || held < 0 || held > 4) {
|
|
1023
|
+
return TLS_ALERT_INTERNAL_ERROR
|
|
1024
|
+
}
|
|
1025
|
+
for (
|
|
1026
|
+
let k: i32 = 0;
|
|
1027
|
+
k < length && k < toI32(scratch.length) && held >= 0 && held < toI32(message.length);
|
|
1028
|
+
k++
|
|
1029
|
+
) {
|
|
1030
|
+
message[held] = scratch[k]
|
|
1031
|
+
held = held + 1
|
|
1032
|
+
this.postHandshakeLength = held
|
|
1033
|
+
if (held === 1 && toI32(message.length) > 0 && toI32(message[0]) !== TLS_HANDSHAKE_KEY_UPDATE) {
|
|
959
1034
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
960
1035
|
}
|
|
961
|
-
if (held === 4) {
|
|
962
|
-
const body: i32 =
|
|
963
|
-
(toI32(this.postHandshake[1]) << 16) |
|
|
964
|
-
(toI32(this.postHandshake[2]) << 8) |
|
|
965
|
-
toI32(this.postHandshake[3])
|
|
1036
|
+
if (held === 4 && toI32(message.length) > 3) {
|
|
1037
|
+
const body: i32 = (toI32(message[1]) << 16) | (toI32(message[2]) << 8) | toI32(message[3])
|
|
966
1038
|
if (body !== 1) {
|
|
967
1039
|
return TLS_ALERT_DECODE_ERROR
|
|
968
1040
|
}
|
|
969
1041
|
}
|
|
970
|
-
if (held === TLS_KEY_UPDATE_SIZE) {
|
|
971
|
-
const request: i32 = toI32(
|
|
1042
|
+
if (held === TLS_KEY_UPDATE_SIZE && toI32(message.length) > 4) {
|
|
1043
|
+
const request: i32 = toI32(message[4])
|
|
972
1044
|
if (request > 1) {
|
|
973
1045
|
return TLS_ALERT_ILLEGAL_PARAMETER
|
|
974
1046
|
}
|
|
975
|
-
if (k !== length
|
|
1047
|
+
if (k + 1 !== length) {
|
|
976
1048
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
977
1049
|
}
|
|
978
1050
|
this.postHandshakeLength = 0
|
|
979
|
-
|
|
1051
|
+
const updates: i32 = this.keyUpdates
|
|
1052
|
+
// The count never passes `TLS_RECORD_MAX_KEY_UPDATES`.
|
|
1053
|
+
if (updates < 0 || updates >= TLS_RECORD_MAX_KEY_UPDATES) {
|
|
980
1054
|
return TLS_ALERT_UNEXPECTED_MESSAGE
|
|
981
1055
|
}
|
|
982
|
-
this.keyUpdates =
|
|
1056
|
+
this.keyUpdates = updates + 1
|
|
983
1057
|
return this.updateReadKeys(request === 1)
|
|
984
1058
|
}
|
|
985
1059
|
}
|
|
@@ -1021,10 +1095,12 @@ export class TlsRecordServer {
|
|
|
1021
1095
|
// id, the server's first handshake message is followed by a
|
|
1022
1096
|
// change_cipher_spec. The server echoes the id, so its own message says.
|
|
1023
1097
|
this.firstMessageSeen = true
|
|
1098
|
+
const initial: u8[] = tls.outputInitial
|
|
1099
|
+
// 38 is `TLS_SESSION_ID_ECHO_AT`, spelled out for the index's bound.
|
|
1024
1100
|
this.ccsDue =
|
|
1025
1101
|
tls.outputInitialLength > TLS_SESSION_ID_ECHO_AT &&
|
|
1026
|
-
|
|
1027
|
-
toI32(
|
|
1102
|
+
toI32(initial.length) > 38 &&
|
|
1103
|
+
toI32(initial[38]) > 0
|
|
1028
1104
|
}
|
|
1029
1105
|
if (this.readLevel === TLS_LEVEL_INITIAL) {
|
|
1030
1106
|
if (tls.readKnown(TLS_LEVEL_HANDSHAKE)) {
|
|
@@ -1075,15 +1151,34 @@ export class TlsRecordServer {
|
|
|
1075
1151
|
this.output = this.pools.output.take()
|
|
1076
1152
|
}
|
|
1077
1153
|
const capacity: i32 = TLS_RECORD_OUTPUT_SIZE
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1154
|
+
const start: i32 = this.outputStart
|
|
1155
|
+
const end: i32 = this.outputEnd
|
|
1156
|
+
// Both stay inside the buffer of 2 * 16645 bytes: a record is sealed only
|
|
1157
|
+
// where this found room, and `consume` moves `start` up to `end`. A need
|
|
1158
|
+
// past the whole buffer, or below 0, is never fitted.
|
|
1159
|
+
if (start < 0 || start > 33290 || end < 0 || end > 33290 || need < 0 || need > 33290) {
|
|
1160
|
+
return false
|
|
1161
|
+
}
|
|
1162
|
+
if (end + need > capacity && start > 0) {
|
|
1163
|
+
const held: i32 = end - start
|
|
1164
|
+
const output: u8[] = this.output
|
|
1165
|
+
// What is unsent lies inside the buffer it is held in, so the move
|
|
1166
|
+
// below copies all of it; the guards inside the loop are the prover's.
|
|
1167
|
+
if (held < 0 || end > toI32(output.length)) {
|
|
1168
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1169
|
+
return false
|
|
1170
|
+
}
|
|
1171
|
+
for (let k: i32 = 0; k < held && k < toI32(output.length); k++) {
|
|
1172
|
+
const from: i32 = start + k
|
|
1173
|
+
if (from < toI32(output.length)) {
|
|
1174
|
+
output[k] = output[from]
|
|
1175
|
+
}
|
|
1082
1176
|
}
|
|
1083
1177
|
this.outputStart = 0
|
|
1084
1178
|
this.outputEnd = held
|
|
1179
|
+
return held + need <= capacity
|
|
1085
1180
|
}
|
|
1086
|
-
return
|
|
1181
|
+
return end + need <= capacity
|
|
1087
1182
|
}
|
|
1088
1183
|
|
|
1089
1184
|
/**
|
|
@@ -1095,7 +1190,9 @@ export class TlsRecordServer {
|
|
|
1095
1190
|
*/
|
|
1096
1191
|
private writeCap(): i32 {
|
|
1097
1192
|
const limit: i32 = this.tls.peerRecordSizeLimit
|
|
1098
|
-
|
|
1193
|
+
// 0 when the client sent none; RFC 8449 §4 refuses one below 64, and one
|
|
1194
|
+
// past 2^14 + 1 limits nothing §5.4 does not.
|
|
1195
|
+
if (limit < 1 || limit > 16385 || !this.writeProtection.isProtected()) {
|
|
1099
1196
|
return TLS_MAX_PLAINTEXT
|
|
1100
1197
|
}
|
|
1101
1198
|
return limit - 1
|
|
@@ -1108,7 +1205,13 @@ export class TlsRecordServer {
|
|
|
1108
1205
|
* Below 1 when the padding leaves no room at all.
|
|
1109
1206
|
*/
|
|
1110
1207
|
private maxChunk(): i32 {
|
|
1111
|
-
|
|
1208
|
+
const cap: i32 = this.writeCap()
|
|
1209
|
+
const padding: i32 = this.writePadding()
|
|
1210
|
+
// `writeCap` answers at most 2^14; a padding outside 0 .. 2^14 leaves no room.
|
|
1211
|
+
if (cap < 0 || cap > 16384 || padding < 0 || padding > 16384) {
|
|
1212
|
+
return 0
|
|
1213
|
+
}
|
|
1214
|
+
return cap - padding
|
|
1112
1215
|
}
|
|
1113
1216
|
|
|
1114
1217
|
/** The padding the next record carries: `padding` once records are protected, none in the clear. */
|
|
@@ -1116,20 +1219,28 @@ export class TlsRecordServer {
|
|
|
1116
1219
|
return this.writeProtection.isProtected() ? this.padding : 0
|
|
1117
1220
|
}
|
|
1118
1221
|
|
|
1119
|
-
/**
|
|
1222
|
+
/**
|
|
1223
|
+
* The room a record of `len` content bytes takes in `output` under the
|
|
1224
|
+
* write protection as it stands; -1, which no room fits, for a `len` or a
|
|
1225
|
+
* padding outside 0 .. 2^14, which no record carries.
|
|
1226
|
+
*/
|
|
1120
1227
|
private recordRoom(len: i32): i32 {
|
|
1228
|
+
const padding: i32 = this.writePadding()
|
|
1229
|
+
if (len < 0 || len > 16384 || padding < 0 || padding > 16384) {
|
|
1230
|
+
return -1
|
|
1231
|
+
}
|
|
1121
1232
|
if (!this.writeProtection.isProtected()) {
|
|
1122
1233
|
return TLS_RECORD_HEADER_SIZE + len
|
|
1123
1234
|
}
|
|
1124
|
-
return TLS_RECORD_HEADER_SIZE + len + 1 +
|
|
1235
|
+
return TLS_RECORD_HEADER_SIZE + len + 1 + padding + TLS_RECORD_TAG_SIZE
|
|
1125
1236
|
}
|
|
1126
1237
|
|
|
1127
1238
|
/**
|
|
1128
1239
|
* Seals one record of `type` from `data[off .. off + len)` onto `output`
|
|
1129
1240
|
* under the write protection, padded when it is protected. False, writing
|
|
1130
1241
|
* nothing, when it does not fit; content and padding past `writeCap()` (the
|
|
1131
|
-
* client's `record_size_limit`, or 2^14)
|
|
1132
|
-
* fail the connection with `internal_error`.
|
|
1242
|
+
* client's `record_size_limit`, or 2^14), a negative `len` or padding, and a
|
|
1243
|
+
* refusal from `seal` itself fail the connection with `internal_error`.
|
|
1133
1244
|
*
|
|
1134
1245
|
* @param type The content type to send, such as `TLS_CONTENT_HANDSHAKE`.
|
|
1135
1246
|
* @param data The array that holds the content.
|
|
@@ -1141,7 +1252,9 @@ export class TlsRecordServer {
|
|
|
1141
1252
|
// A record the cap cannot carry, such as an alert under a padding that
|
|
1142
1253
|
// fills it, fails the connection rather than pass the client's limit; the
|
|
1143
1254
|
// alert that failure seals is refused here in turn, and not sent.
|
|
1144
|
-
|
|
1255
|
+
const padding: i32 = this.writePadding()
|
|
1256
|
+
// Content or padding past 2^14 is past every cap too.
|
|
1257
|
+
if (len < 0 || len > 16384 || padding < 0 || padding > 16384 || len + padding > this.writeCap()) {
|
|
1145
1258
|
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1146
1259
|
return false
|
|
1147
1260
|
}
|
|
@@ -1149,24 +1262,27 @@ export class TlsRecordServer {
|
|
|
1149
1262
|
if (!this.outputHasRoom(room)) {
|
|
1150
1263
|
return false
|
|
1151
1264
|
}
|
|
1265
|
+
const end: i32 = this.outputEnd
|
|
1266
|
+
// `outputHasRoom` found room for the record past `end`, inside the buffer.
|
|
1267
|
+
if (end < 0 || end > 33290 || room < 0 || room > 33290) {
|
|
1268
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1269
|
+
return false
|
|
1270
|
+
}
|
|
1152
1271
|
// Counted before the seal, so a refused seal's partial write is wiped when the buffer goes back.
|
|
1153
|
-
if (
|
|
1154
|
-
this.outputUsed =
|
|
1155
|
-
}
|
|
1156
|
-
const n: i32 = this.writeProtection.seal(
|
|
1157
|
-
type,
|
|
1158
|
-
data,
|
|
1159
|
-
off,
|
|
1160
|
-
len,
|
|
1161
|
-
this.writePadding(),
|
|
1162
|
-
this.output,
|
|
1163
|
-
this.outputEnd
|
|
1164
|
-
)
|
|
1272
|
+
if (end + room > this.outputUsed) {
|
|
1273
|
+
this.outputUsed = end + room
|
|
1274
|
+
}
|
|
1275
|
+
const n: i32 = this.writeProtection.seal(type, data, off, len, this.writePadding(), this.output, end)
|
|
1165
1276
|
if (n < 0) {
|
|
1166
|
-
this.fail(
|
|
1277
|
+
this.fail(tlsAlertOf(n))
|
|
1167
1278
|
return false
|
|
1168
1279
|
}
|
|
1169
|
-
|
|
1280
|
+
// `seal` writes no more than the room counted above.
|
|
1281
|
+
if (n > 33290) {
|
|
1282
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1283
|
+
return false
|
|
1284
|
+
}
|
|
1285
|
+
this.outputEnd = end + n
|
|
1170
1286
|
return true
|
|
1171
1287
|
}
|
|
1172
1288
|
|
|
@@ -1178,17 +1294,20 @@ export class TlsRecordServer {
|
|
|
1178
1294
|
private sealQueue(queue: u8[], at: i32, length: i32): i32 {
|
|
1179
1295
|
let next: i32 = at
|
|
1180
1296
|
const most: i32 = this.maxChunk()
|
|
1181
|
-
|
|
1297
|
+
// `TlsServer`'s output buffers are far below 2^30 bytes, and `at` is where
|
|
1298
|
+
// this stopped in one of them.
|
|
1299
|
+
if ((most < 1 && next < length) || next < 0 || next > 1073741823 || length < 0 || length > 1073741823) {
|
|
1182
1300
|
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1183
1301
|
return next
|
|
1184
1302
|
}
|
|
1185
|
-
while (next < length && this.state !== TLS_RECORD_STATE_FAILED) {
|
|
1303
|
+
while (next >= 0 && next < length && this.state !== TLS_RECORD_STATE_FAILED) {
|
|
1186
1304
|
const left: i32 = length - next
|
|
1187
1305
|
const chunk: i32 = left < most ? left : most
|
|
1188
1306
|
if (!this.sealRecord(TLS_CONTENT_HANDSHAKE, queue, next, chunk)) {
|
|
1189
1307
|
return next
|
|
1190
1308
|
}
|
|
1191
|
-
|
|
1309
|
+
// `chunk` is at most `left`, so `next` stays at most `length`.
|
|
1310
|
+
next = toI32(toI64(next) + toI64(chunk))
|
|
1192
1311
|
}
|
|
1193
1312
|
return next
|
|
1194
1313
|
}
|
|
@@ -1377,13 +1496,20 @@ export class TlsRecordServer {
|
|
|
1377
1496
|
* @param n How many bytes the socket wrote. It is clamped to 0 and to the bytes `output` holds.
|
|
1378
1497
|
*/
|
|
1379
1498
|
consume(n: i32): void {
|
|
1380
|
-
const
|
|
1499
|
+
const start: i32 = this.outputStart
|
|
1500
|
+
const end: i32 = this.outputEnd
|
|
1501
|
+
// Both stay inside the buffer of 2 * 16645 bytes, `start` at most `end`.
|
|
1502
|
+
if (start < 0 || start > 33290 || end < 0 || end > 33290) {
|
|
1503
|
+
return
|
|
1504
|
+
}
|
|
1505
|
+
const held: i32 = end - start
|
|
1381
1506
|
let take: i32 = n < held ? n : held
|
|
1382
1507
|
if (take < 0) {
|
|
1383
1508
|
take = 0
|
|
1384
1509
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1510
|
+
// `take` is at most `held`, so this is at most `end` and never wraps.
|
|
1511
|
+
this.outputStart = toI32(toI64(start) + toI64(take))
|
|
1512
|
+
if (this.outputStart === end) {
|
|
1387
1513
|
this.outputStart = 0
|
|
1388
1514
|
this.outputEnd = 0
|
|
1389
1515
|
}
|
|
@@ -1407,7 +1533,7 @@ export class TlsRecordServer {
|
|
|
1407
1533
|
* because the connection failed; or one of the negative codes above, whatever `len` is.
|
|
1408
1534
|
*/
|
|
1409
1535
|
write(data: u8[], off: i32, len: i32): i32 {
|
|
1410
|
-
if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
|
|
1536
|
+
if (off < 0 || len < 0 || !tlsRecordWindowFits(toI32(data.length), off, len)) {
|
|
1411
1537
|
return TLS_RECORD_INVALID
|
|
1412
1538
|
}
|
|
1413
1539
|
if (this.state === TLS_RECORD_STATE_FAILED || this.state === TLS_RECORD_STATE_CLOSED) {
|
|
@@ -1422,7 +1548,7 @@ export class TlsRecordServer {
|
|
|
1422
1548
|
return TLS_RECORD_AGAIN
|
|
1423
1549
|
}
|
|
1424
1550
|
let done: i32 = 0
|
|
1425
|
-
while (done < len && this.state === TLS_RECORD_STATE_OPEN) {
|
|
1551
|
+
while (done >= 0 && done < len && this.state === TLS_RECORD_STATE_OPEN) {
|
|
1426
1552
|
if (this.writeProtection.sequence >= this.writeProtection.recordLimit && !this.sendKeyUpdate(false)) {
|
|
1427
1553
|
break
|
|
1428
1554
|
}
|
|
@@ -1435,14 +1561,26 @@ export class TlsRecordServer {
|
|
|
1435
1561
|
let chunk: i32 = left < most ? left : most
|
|
1436
1562
|
// A record shortened to the room there is, rather than none at all.
|
|
1437
1563
|
this.outputHasRoom(this.recordRoom(chunk))
|
|
1438
|
-
const
|
|
1564
|
+
const end: i32 = this.outputEnd
|
|
1565
|
+
const overhead: i32 = this.recordRoom(TLS_RECORD_SERVER_FROM)
|
|
1566
|
+
// `end` is inside the buffer of 2 * 16645 bytes; `overhead` is a record
|
|
1567
|
+
// of no content, -1 under a padding no record carries.
|
|
1568
|
+
if (end < 0 || end > 33290 || overhead < 0 || overhead > 33290) {
|
|
1569
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1570
|
+
break
|
|
1571
|
+
}
|
|
1572
|
+
const room: i32 = TLS_RECORD_OUTPUT_SIZE - end - overhead
|
|
1439
1573
|
if (room < chunk) {
|
|
1440
1574
|
chunk = room
|
|
1441
1575
|
}
|
|
1442
|
-
|
|
1576
|
+
// `off + len` fits in `data`, and `done + chunk` is at most `len`.
|
|
1577
|
+
if (
|
|
1578
|
+
chunk <= 0 ||
|
|
1579
|
+
!this.sealRecord(TLS_CONTENT_APPLICATION_DATA, data, toI32(toI64(off) + toI64(done)), chunk)
|
|
1580
|
+
) {
|
|
1443
1581
|
break
|
|
1444
1582
|
}
|
|
1445
|
-
done = done + chunk
|
|
1583
|
+
done = toI32(toI64(done) + toI64(chunk))
|
|
1446
1584
|
}
|
|
1447
1585
|
if (done === 0 && len > 0) {
|
|
1448
1586
|
return this.state === TLS_RECORD_STATE_FAILED ? TLS_RECORD_RESET : TLS_RECORD_AGAIN
|
|
@@ -1471,14 +1609,33 @@ export class TlsRecordServer {
|
|
|
1471
1609
|
if (len === 0) {
|
|
1472
1610
|
return 0
|
|
1473
1611
|
}
|
|
1474
|
-
const
|
|
1612
|
+
const start: i32 = this.plainStart
|
|
1613
|
+
const end: i32 = this.plainEnd
|
|
1614
|
+
// Both stay inside the 2^15-byte buffer, `start` at most `end`; the
|
|
1615
|
+
// window check refused a negative `off` or `len` already.
|
|
1616
|
+
if (start < 0 || start > 32768 || end < 0 || end > 32768 || off < 0 || len < 0) {
|
|
1617
|
+
return TLS_RECORD_INVALID
|
|
1618
|
+
}
|
|
1619
|
+
const avail: i32 = end - start
|
|
1475
1620
|
if (avail > 0) {
|
|
1476
1621
|
const n: i32 = avail < len ? avail : len
|
|
1477
|
-
|
|
1478
|
-
|
|
1622
|
+
const plain: u8[] = this.plain
|
|
1623
|
+
// All `n` bytes are copied or none: the guards inside the loop are the
|
|
1624
|
+
// prover's, and never skip a byte past this check.
|
|
1625
|
+
if (end > toI32(plain.length) || toI64(off) + toI64(n) > toI64(buf.length)) {
|
|
1626
|
+
this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
1627
|
+
return TLS_RECORD_RESET
|
|
1628
|
+
}
|
|
1629
|
+
for (let j: i32 = off; j < toI32(buf.length) && j - off < n; j++) {
|
|
1630
|
+
// `j - off` is below `n`, so this never wraps.
|
|
1631
|
+
const from: i32 = toI32(toI64(start) + toI64(j - off))
|
|
1632
|
+
if (from >= 0 && from < toI32(plain.length)) {
|
|
1633
|
+
buf[j] = plain[from]
|
|
1634
|
+
}
|
|
1479
1635
|
}
|
|
1480
|
-
|
|
1481
|
-
|
|
1636
|
+
// `n` is at most `avail`, so this is at most `end` and never wraps.
|
|
1637
|
+
this.plainStart = toI32(toI64(start) + toI64(n))
|
|
1638
|
+
if (this.plainStart === end) {
|
|
1482
1639
|
this.plainStart = 0
|
|
1483
1640
|
this.plainEnd = 0
|
|
1484
1641
|
}
|