@amritk/nish-aarch64-linux 0.42.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/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- 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 +163 -39
- 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 +141 -0
- package/std/crypto/sha1.ts +157 -57
- 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/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- 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 +1276 -218
- 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 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- 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 +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- 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 +104 -22
- 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 +324 -121
- package/std/net/quic-listener.ts +206 -91
- 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 +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- 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 +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/tls/record.ts
CHANGED
|
@@ -172,11 +172,14 @@ export const tlsNextTrafficSecret = (hashLength: i32, trafficSecret: Secret<u8[]
|
|
|
172
172
|
* @returns `true` when `off` and `len` are not negative and the window ends at or before `length`.
|
|
173
173
|
*/
|
|
174
174
|
export const tlsRecordWindowFits = (length: i32, off: i32, len: i32): boolean =>
|
|
175
|
-
|
|
175
|
+
// An array's length is never negative, and with it and `len` both at least 0 the subtraction cannot overflow.
|
|
176
|
+
off >= 0 && len >= 0 && length >= 0 && off <= length - len
|
|
176
177
|
|
|
177
178
|
/** The five header bytes of a record of `type` and body length `len`, written at `out[at]`. */
|
|
178
179
|
const tlsWriteRecordHeader = (out: u8[], at: i32, type: i32, len: i32): void => {
|
|
179
|
-
|
|
180
|
+
// Every caller checks the whole header window first (`tlsRecordWindowFits`, or a five-byte `aad` at 0), so
|
|
181
|
+
// this return is never taken; a position past 2^30 - 1 is none any buffer this layer writes reaches.
|
|
182
|
+
if (at < 0 || at > 1073741823 || at + 4 >= toI32(out.length)) {
|
|
180
183
|
return
|
|
181
184
|
}
|
|
182
185
|
out[at] = toU8(type)
|
|
@@ -207,12 +210,18 @@ export const tlsRecordLength = (buf: u8[], off: i32, avail: i32): i32 => {
|
|
|
207
210
|
if (avail < TLS_RECORD_HEADER_SIZE) {
|
|
208
211
|
return 0
|
|
209
212
|
}
|
|
213
|
+
// The window fits and holds the five header bytes, so `off + 4` is inside `buf`; a start past 2^30 - 1 is
|
|
214
|
+
// none any buffer this layer reads reaches.
|
|
215
|
+
if (off < 0 || off > 1073741823 || off + 4 >= toI32(buf.length)) {
|
|
216
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
217
|
+
}
|
|
210
218
|
const type: i32 = toI32(buf[off])
|
|
211
219
|
if (type < TLS_CONTENT_CHANGE_CIPHER_SPEC || type > TLS_CONTENT_APPLICATION_DATA) {
|
|
212
220
|
return -TLS_ALERT_UNEXPECTED_MESSAGE
|
|
213
221
|
}
|
|
214
222
|
const length: i32 = (toI32(buf[off + 3]) << 8) | toI32(buf[off + 4])
|
|
215
|
-
|
|
223
|
+
// 16640 is `TLS_MAX_CIPHERTEXT`, written out so the bound reaches the sum below; two bytes are never negative.
|
|
224
|
+
if (length < 0 || length > 16640) {
|
|
216
225
|
return -TLS_ALERT_RECORD_OVERFLOW
|
|
217
226
|
}
|
|
218
227
|
return TLS_RECORD_HEADER_SIZE + length
|
|
@@ -286,7 +295,14 @@ export class TlsRecordReader {
|
|
|
286
295
|
* @returns `TLS_MAX_RECORD` less the bytes held and not yet consumed.
|
|
287
296
|
*/
|
|
288
297
|
space(): i32 {
|
|
289
|
-
|
|
298
|
+
const start: i32 = this.start
|
|
299
|
+
const end: i32 = this.end
|
|
300
|
+
// Every method keeps `0 <= start <= end <= TLS_MAX_RECORD` (16645); the guard only states it for the
|
|
301
|
+
// subtractions, and a reader outside it, which none is, takes nothing.
|
|
302
|
+
if (start < 0 || start > 16645 || end < 0 || end > 16645 || end < start) {
|
|
303
|
+
return 0
|
|
304
|
+
}
|
|
305
|
+
return TLS_MAX_RECORD - (end - start)
|
|
290
306
|
}
|
|
291
307
|
|
|
292
308
|
/**
|
|
@@ -310,22 +326,46 @@ export class TlsRecordReader {
|
|
|
310
326
|
if (this.buffer.length === 0) {
|
|
311
327
|
this.buffer = this.pool.take()
|
|
312
328
|
}
|
|
313
|
-
const
|
|
329
|
+
const buffer: u8[] = this.buffer
|
|
330
|
+
let start: i32 = this.start
|
|
331
|
+
let end: i32 = this.end
|
|
332
|
+
// Every method keeps `0 <= start <= end <= TLS_MAX_RECORD` (16645), and `off` is inside `data`, so no array
|
|
333
|
+
// reaches 2^30: the guard only states it for the sums below.
|
|
334
|
+
if (start < 0 || start > 16645 || end < 0 || end > 16645 || end < start || off < 0 || off > 1073741823) {
|
|
335
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
336
|
+
}
|
|
337
|
+
const held: i32 = end - start
|
|
314
338
|
// The held bytes move to the front only when the new ones would not fit after them.
|
|
315
|
-
if (
|
|
316
|
-
|
|
317
|
-
|
|
339
|
+
if (start > 0 && TLS_MAX_RECORD - end < len) {
|
|
340
|
+
let k: i32 = 0
|
|
341
|
+
for (let j: i32 = start; j < end && j < toI32(buffer.length) && k < toI32(buffer.length); j++) {
|
|
342
|
+
buffer[k] = buffer[j]
|
|
343
|
+
k++
|
|
318
344
|
}
|
|
319
|
-
|
|
320
|
-
|
|
345
|
+
start = 0
|
|
346
|
+
end = held
|
|
347
|
+
this.start = start
|
|
348
|
+
this.end = end
|
|
321
349
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
this.buffer[this.end + k] = data[off + k]
|
|
350
|
+
// `held` was between 0 and 16645 as `end` was: restated for the arithmetic below.
|
|
351
|
+
if (end < 0 || end > 16645) {
|
|
352
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
326
353
|
}
|
|
327
|
-
|
|
328
|
-
|
|
354
|
+
const room: i32 = TLS_MAX_RECORD - end
|
|
355
|
+
const take: i32 = len < room ? len : room
|
|
356
|
+
// The buffer is `TLS_MAX_RECORD` bytes and the window is inside `data`: neither array bound below binds,
|
|
357
|
+
// so the loop copies all `take` bytes and `k` ends at `end + take`.
|
|
358
|
+
let k: i32 = end
|
|
359
|
+
for (
|
|
360
|
+
let j: i32 = off;
|
|
361
|
+
j - off < take && j < toI32(data.length) && k >= 0 && k < toI32(buffer.length);
|
|
362
|
+
j++
|
|
363
|
+
) {
|
|
364
|
+
buffer[k] = data[j]
|
|
365
|
+
k++
|
|
366
|
+
}
|
|
367
|
+
this.end = k
|
|
368
|
+
return k - end
|
|
329
369
|
}
|
|
330
370
|
|
|
331
371
|
/**
|
|
@@ -337,8 +377,14 @@ export class TlsRecordReader {
|
|
|
337
377
|
* `TLS_ALERT_*` value, the alert to send.
|
|
338
378
|
*/
|
|
339
379
|
next(): i32 {
|
|
340
|
-
const
|
|
341
|
-
const
|
|
380
|
+
const start: i32 = this.start
|
|
381
|
+
const end: i32 = this.end
|
|
382
|
+
// `0 <= start <= end <= TLS_MAX_RECORD` (16645) always holds; the guard states it for the subtraction.
|
|
383
|
+
if (start < 0 || start > 16645 || end < 0 || end > 16645 || end < start) {
|
|
384
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
385
|
+
}
|
|
386
|
+
const avail: i32 = end - start
|
|
387
|
+
const length: i32 = tlsRecordLength(this.buffer, start, avail)
|
|
342
388
|
return length > avail ? 0 : length
|
|
343
389
|
}
|
|
344
390
|
|
|
@@ -350,15 +396,24 @@ export class TlsRecordReader {
|
|
|
350
396
|
* @param n How many bytes to let go, usually what `next` answered. Clamped to 0 and to the bytes held.
|
|
351
397
|
*/
|
|
352
398
|
consume(n: i32): void {
|
|
353
|
-
const
|
|
399
|
+
const start: i32 = this.start
|
|
400
|
+
const end: i32 = this.end
|
|
401
|
+
// `0 <= start <= end <= TLS_MAX_RECORD` (16645) always holds; the guard states it for the arithmetic.
|
|
402
|
+
if (start < 0 || start > 16645 || end < 0 || end > 16645 || end < start) {
|
|
403
|
+
return
|
|
404
|
+
}
|
|
405
|
+
const held: i32 = end - start
|
|
354
406
|
let take: i32 = n < held ? n : held
|
|
355
|
-
if (take < 0) {
|
|
407
|
+
if (take < 0 || take > 16645) {
|
|
408
|
+
// Never above: `take` is at most `held`.
|
|
356
409
|
take = 0
|
|
357
410
|
}
|
|
358
|
-
|
|
359
|
-
if (
|
|
411
|
+
const at: i32 = start + take
|
|
412
|
+
if (at === end) {
|
|
360
413
|
this.start = 0
|
|
361
414
|
this.end = 0
|
|
415
|
+
} else {
|
|
416
|
+
this.start = at
|
|
362
417
|
}
|
|
363
418
|
}
|
|
364
419
|
}
|
|
@@ -651,6 +706,19 @@ export class TlsRecordProtection {
|
|
|
651
706
|
if (!tlsRecordWindowFits(toI32(data.length), off, len) || len > TLS_MAX_PLAINTEXT || padding < 0) {
|
|
652
707
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
653
708
|
}
|
|
709
|
+
// The bounds the sums and copies below need, in literals the prover reads: 16384 is `TLS_MAX_PLAINTEXT`,
|
|
710
|
+
// a padding past it is refused below in either direction, and no buffer this layer writes reaches 2^30.
|
|
711
|
+
if (
|
|
712
|
+
off < 0 ||
|
|
713
|
+
off > 1073741823 ||
|
|
714
|
+
len < 0 ||
|
|
715
|
+
len > 16384 ||
|
|
716
|
+
padding > 16384 ||
|
|
717
|
+
at < 0 ||
|
|
718
|
+
at > 1073741823
|
|
719
|
+
) {
|
|
720
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
721
|
+
}
|
|
654
722
|
if (this.suite === 0) {
|
|
655
723
|
// Application data is never sent in the clear, as `open` never reads
|
|
656
724
|
// it so: a cleartext direction asked for some has lost its keys.
|
|
@@ -662,12 +730,22 @@ export class TlsRecordProtection {
|
|
|
662
730
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
663
731
|
}
|
|
664
732
|
tlsWriteRecordHeader(out, at, type, len)
|
|
665
|
-
|
|
666
|
-
|
|
733
|
+
// Both windows were checked, so neither array bound in the loop binds.
|
|
734
|
+
let j: i32 = off
|
|
735
|
+
for (
|
|
736
|
+
let k: i32 = at + TLS_RECORD_HEADER_SIZE;
|
|
737
|
+
j - off < len && j < toI32(data.length) && k >= 0 && k < toI32(out.length);
|
|
738
|
+
k++
|
|
739
|
+
) {
|
|
740
|
+
out[k] = data[j]
|
|
741
|
+
j++
|
|
667
742
|
}
|
|
668
743
|
return TLS_RECORD_HEADER_SIZE + len
|
|
669
744
|
}
|
|
670
|
-
|
|
745
|
+
// The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule, and it
|
|
746
|
+
// keeps the step at the end from overflowing.
|
|
747
|
+
const sequence: i64 = this.sequence
|
|
748
|
+
if (padding > TLS_MAX_INNER_PLAINTEXT - 1 - len || sequence < 0 || sequence >= 9007199254740991) {
|
|
671
749
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
672
750
|
}
|
|
673
751
|
const inner: i32 = len + 1 + padding
|
|
@@ -675,9 +753,15 @@ export class TlsRecordProtection {
|
|
|
675
753
|
if (!tlsRecordWindowFits(toI32(out.length), at, TLS_RECORD_HEADER_SIZE + body)) {
|
|
676
754
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
677
755
|
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
756
|
+
// `inner` is at most 2^14 + 1, so it is a u16 as it is.
|
|
757
|
+
const plaintext: u8[] = new Array<u8>(toU16(inner))
|
|
758
|
+
if (len >= toI32(plaintext.length)) {
|
|
759
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
760
|
+
}
|
|
761
|
+
let j: i32 = off
|
|
762
|
+
for (let k: i32 = 0; k < len && k < toI32(plaintext.length) && j < toI32(data.length); k++) {
|
|
763
|
+
plaintext[k] = data[j]
|
|
764
|
+
j++
|
|
681
765
|
}
|
|
682
766
|
plaintext[len] = toU8(type)
|
|
683
767
|
const aad: u8[] = new Array<u8>(5)
|
|
@@ -694,11 +778,16 @@ export class TlsRecordProtection {
|
|
|
694
778
|
if (sealed === null || toI32(sealed.length) !== body) {
|
|
695
779
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
696
780
|
}
|
|
781
|
+
const ciphertext: u8[] = sealed
|
|
697
782
|
tlsWriteRecordHeader(out, at, TLS_CONTENT_APPLICATION_DATA, body)
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
783
|
+
// The window at `out[at]` was checked for the whole record, so neither array bound in the loop binds.
|
|
784
|
+
let k: i32 = at + TLS_RECORD_HEADER_SIZE
|
|
785
|
+
for (let i: i32 = 0; i < body && i < toI32(ciphertext.length) && k >= 0 && k < toI32(out.length); i++) {
|
|
786
|
+
out[k] = ciphertext[i]
|
|
787
|
+
k++
|
|
788
|
+
}
|
|
789
|
+
// Unsigned, the step cannot panic; it cannot wrap either, since `sequence` is below 2^53 - 1.
|
|
790
|
+
this.sequence = toI64(toU64(sequence) + toU64(1))
|
|
702
791
|
return TLS_RECORD_HEADER_SIZE + body
|
|
703
792
|
}
|
|
704
793
|
|
|
@@ -741,6 +830,19 @@ export class TlsRecordProtection {
|
|
|
741
830
|
if (declared !== len) {
|
|
742
831
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
743
832
|
}
|
|
833
|
+
// `len` is a whole record's, so at most `TLS_MAX_RECORD` (16645) and with its header inside `record`; no
|
|
834
|
+
// buffer this layer reads or writes reaches 2^30. The guard states that for the sums and copies below.
|
|
835
|
+
if (
|
|
836
|
+
off < 0 ||
|
|
837
|
+
off > 1073741823 ||
|
|
838
|
+
len < 5 ||
|
|
839
|
+
len > 16645 ||
|
|
840
|
+
off + 4 >= toI32(record.length) ||
|
|
841
|
+
at < 0 ||
|
|
842
|
+
at > 1073741823
|
|
843
|
+
) {
|
|
844
|
+
return -TLS_ALERT_INTERNAL_ERROR
|
|
845
|
+
}
|
|
744
846
|
const type: i32 = toI32(record[off])
|
|
745
847
|
const body: i32 = len - TLS_RECORD_HEADER_SIZE
|
|
746
848
|
if (this.suite === 0) {
|
|
@@ -753,8 +855,15 @@ export class TlsRecordProtection {
|
|
|
753
855
|
if (!tlsRecordWindowFits(toI32(out.length), at, body)) {
|
|
754
856
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
755
857
|
}
|
|
756
|
-
|
|
757
|
-
|
|
858
|
+
// Both windows were checked, so neither array bound in the loop binds.
|
|
859
|
+
let j: i32 = off + TLS_RECORD_HEADER_SIZE
|
|
860
|
+
for (
|
|
861
|
+
let k: i32 = at;
|
|
862
|
+
k - at < body && k < toI32(out.length) && j >= 0 && j < toI32(record.length);
|
|
863
|
+
k++
|
|
864
|
+
) {
|
|
865
|
+
out[k] = record[j]
|
|
866
|
+
j++
|
|
758
867
|
}
|
|
759
868
|
this.contentType = type
|
|
760
869
|
return body
|
|
@@ -765,29 +874,43 @@ export class TlsRecordProtection {
|
|
|
765
874
|
if (body < TLS_RECORD_TAG_SIZE + 1) {
|
|
766
875
|
return -TLS_ALERT_BAD_RECORD_MAC
|
|
767
876
|
}
|
|
768
|
-
|
|
877
|
+
// The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule, and it
|
|
878
|
+
// keeps the step below from wrapping.
|
|
879
|
+
const sequence: i64 = this.sequence
|
|
880
|
+
if (sequence < 0 || sequence >= 9007199254740991) {
|
|
769
881
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
770
882
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
883
|
+
// `body` is at most 2^14 + 256, so it is a u16 as it is.
|
|
884
|
+
const sealed: u8[] = new Array<u8>(toU16(body))
|
|
885
|
+
let j: i32 = off + TLS_RECORD_HEADER_SIZE
|
|
886
|
+
for (let k: i32 = 0; k < body && k < toI32(sealed.length) && j >= 0 && j < toI32(record.length); k++) {
|
|
887
|
+
sealed[k] = record[j]
|
|
888
|
+
j++
|
|
774
889
|
}
|
|
775
890
|
const aad: u8[] = new Array<u8>(5)
|
|
776
|
-
|
|
777
|
-
|
|
891
|
+
j = off
|
|
892
|
+
for (
|
|
893
|
+
let k: i32 = 0;
|
|
894
|
+
k < TLS_RECORD_HEADER_SIZE && k < toI32(aad.length) && j >= 0 && j < toI32(record.length);
|
|
895
|
+
k++
|
|
896
|
+
) {
|
|
897
|
+
aad[k] = record[j]
|
|
898
|
+
j++
|
|
778
899
|
}
|
|
779
900
|
const nonce: u8[] = new Array<u8>(12)
|
|
780
901
|
this.nonceInto(nonce)
|
|
781
902
|
const aes: AesKey | null = this.aesForSuite()
|
|
782
|
-
const
|
|
903
|
+
const opened: u8[] | null =
|
|
783
904
|
aes !== null ? aesGcmOpen(aes, nonce, aad, sealed) : chacha20Poly1305Open(this.key, nonce, aad, sealed)
|
|
784
905
|
secureZero(nonce)
|
|
785
|
-
if (
|
|
906
|
+
if (opened === null) {
|
|
786
907
|
return -TLS_ALERT_BAD_RECORD_MAC
|
|
787
908
|
}
|
|
909
|
+
const inner: u8[] = opened
|
|
788
910
|
// The record authenticated, so it is the peer's, and its number is spent
|
|
789
|
-
// whatever its content turns out to be.
|
|
790
|
-
|
|
911
|
+
// whatever its content turns out to be. Unsigned, the step cannot panic, and it cannot wrap from below
|
|
912
|
+
// 2^53 - 1.
|
|
913
|
+
this.sequence = toI64(toU64(sequence) + toU64(1))
|
|
791
914
|
const innerLength: i32 = toI32(inner.length)
|
|
792
915
|
if (innerLength > TLS_MAX_INNER_PLAINTEXT) {
|
|
793
916
|
return -TLS_ALERT_RECORD_OVERFLOW
|
|
@@ -797,7 +920,7 @@ export class TlsRecordProtection {
|
|
|
797
920
|
// addresses read depend on how much of the record was padding.
|
|
798
921
|
let last: i32 = -1
|
|
799
922
|
let found: i32 = 0
|
|
800
|
-
for (let k: i32 = 0; k < innerLength; k++) {
|
|
923
|
+
for (let k: i32 = 0; k < innerLength && k < toI32(inner.length); k++) {
|
|
801
924
|
const b: i32 = toI32(inner[k])
|
|
802
925
|
const keep: i32 = (0 - b) >> 31
|
|
803
926
|
last = (k & keep) | (last & ~keep)
|
|
@@ -809,8 +932,11 @@ export class TlsRecordProtection {
|
|
|
809
932
|
if (!tlsRecordWindowFits(toI32(out.length), at, last)) {
|
|
810
933
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
811
934
|
}
|
|
812
|
-
|
|
813
|
-
|
|
935
|
+
// The window at `out[at]` was checked, so the output bound in the loop never binds.
|
|
936
|
+
let k: i32 = at
|
|
937
|
+
for (let i: i32 = 0; i < last && i < toI32(inner.length) && k >= 0 && k < toI32(out.length); i++) {
|
|
938
|
+
out[k] = inner[i]
|
|
939
|
+
k++
|
|
814
940
|
}
|
|
815
941
|
this.contentType = found
|
|
816
942
|
return last
|
package/std/net/tls/schedule.ts
CHANGED
|
@@ -75,7 +75,7 @@ import {
|
|
|
75
75
|
hmacSha384,
|
|
76
76
|
hmacSha384Secret,
|
|
77
77
|
} from "nish/crypto/hmac"
|
|
78
|
-
import { tlsHexInto } from "nish/net/tls/codec"
|
|
78
|
+
import { tlsCopyWindow, tlsHexInto } from "nish/net/tls/codec"
|
|
79
79
|
import { timingSafeEqualAt } from "nish/crypto/ct"
|
|
80
80
|
import { SHA256_SIZE, Sha256, sha256 } from "nish/crypto/sha256"
|
|
81
81
|
import { SHA384_SIZE, Sha384, sha384 } from "nish/crypto/sha512"
|
|
@@ -205,6 +205,13 @@ export const tlsEmptyHash = (hashLength: i32): u8[] => {
|
|
|
205
205
|
return hashLength === SHA384_SIZE ? sha384(none) : sha256(none)
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
+
/**
|
|
209
|
+
* HashLen zero bytes in a fresh array: 48 for SHA-384 and 32 for any other
|
|
210
|
+
* value, as `tlsEmptyHash` reads a hash length. A `u16` length is one an
|
|
211
|
+
* allocation never refuses.
|
|
212
|
+
*/
|
|
213
|
+
const tlsHashZeros = (hashLength: i32): u8[] => new Array<u8>(toU16(hashLength === 48 ? 48 : 32))
|
|
214
|
+
|
|
208
215
|
/**
|
|
209
216
|
* Derive-Secret (RFC 8446 §7.1): Expand-Label of `prk` with `label` and a transcript hash, HashLen bytes
|
|
210
217
|
* held as a `Secret`.
|
|
@@ -230,8 +237,8 @@ export const tlsDeriveSecret = (
|
|
|
230
237
|
* @returns A fresh `Secret` holding the early secret, which the caller owns and wipes.
|
|
231
238
|
*/
|
|
232
239
|
export const tlsEarlySecret = (hashLength: i32): Secret<u8[]> => {
|
|
233
|
-
const salt: Secret<u8[]> = secret(
|
|
234
|
-
const ikm: Secret<u8[]> = secret(
|
|
240
|
+
const salt: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
241
|
+
const ikm: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
235
242
|
const early: Secret<u8[]> = tlsExtract(hashLength, salt, ikm)
|
|
236
243
|
wipe(salt)
|
|
237
244
|
wipe(ikm)
|
|
@@ -248,7 +255,7 @@ export const tlsEarlySecret = (hashLength: i32): Secret<u8[]> => {
|
|
|
248
255
|
* @returns A fresh `Secret` holding the early secret, which the caller owns and wipes.
|
|
249
256
|
*/
|
|
250
257
|
export const tlsPskEarlySecret = (hashLength: i32, psk: Secret<u8[]>): Secret<u8[]> => {
|
|
251
|
-
const salt: Secret<u8[]> = secret(
|
|
258
|
+
const salt: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
252
259
|
const early: Secret<u8[]> = tlsExtract(hashLength, salt, psk)
|
|
253
260
|
wipe(salt)
|
|
254
261
|
return early
|
|
@@ -327,7 +334,7 @@ export const tlsMasterSecret = (hashLength: i32, handshakeSecret: Secret<u8[]>):
|
|
|
327
334
|
"derived",
|
|
328
335
|
tlsEmptyHash(hashLength)
|
|
329
336
|
)
|
|
330
|
-
const zeros: Secret<u8[]> = secret(
|
|
337
|
+
const zeros: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
331
338
|
const master: Secret<u8[]> = tlsExtract(hashLength, derived, zeros)
|
|
332
339
|
wipe(derived)
|
|
333
340
|
wipe(zeros)
|
|
@@ -479,18 +486,27 @@ export class TlsTranscript {
|
|
|
479
486
|
hmacCopySha256(this.sha256, this.twin256)
|
|
480
487
|
digest = this.twin256.digest()
|
|
481
488
|
}
|
|
482
|
-
|
|
483
|
-
|
|
489
|
+
if (at < 0) {
|
|
490
|
+
return
|
|
491
|
+
}
|
|
492
|
+
// A position past an `i32` wraps negative and is skipped, as one past `out` is.
|
|
493
|
+
for (let k: i32 = 0; k < toI32(digest.length); k++) {
|
|
494
|
+
const to: i32 = toI32(toI64(at) + toI64(k))
|
|
495
|
+
if (to >= 0 && to < toI32(out.length)) {
|
|
496
|
+
out[to] = digest[k]
|
|
497
|
+
}
|
|
484
498
|
}
|
|
485
499
|
}
|
|
486
500
|
|
|
487
501
|
/**
|
|
488
502
|
* The transcript hash of everything absorbed so far, in a fresh array; the transcript goes on.
|
|
489
503
|
*
|
|
490
|
-
* @returns The `hashLength` bytes of the hash so far
|
|
504
|
+
* @returns The `hashLength` bytes of the hash so far, or none while no hash is named (a
|
|
505
|
+
* `hashLength` of 0). It is public, so it is plain.
|
|
491
506
|
*/
|
|
492
507
|
hash(): u8[] {
|
|
493
|
-
const
|
|
508
|
+
const length: i32 = this.hashLength
|
|
509
|
+
const out: u8[] = length === 32 || length === 48 ? tlsHashZeros(length) : []
|
|
494
510
|
this.hashInto(out, TLS_SCHEDULE_FROM)
|
|
495
511
|
return out
|
|
496
512
|
}
|
|
@@ -583,11 +599,11 @@ export const tlsMasterSecretInto = (
|
|
|
583
599
|
handshakeSecret: u8[],
|
|
584
600
|
out: u8[]
|
|
585
601
|
): void => {
|
|
586
|
-
const empty: u8[] =
|
|
602
|
+
const empty: u8[] = tlsHashZeros(hashLength)
|
|
587
603
|
tlsEmptyHashInto(hashLength, empty)
|
|
588
|
-
const salt: u8[] =
|
|
604
|
+
const salt: u8[] = tlsHashZeros(hashLength)
|
|
589
605
|
tlsDeriveSecretInto(kdf, hashLength, handshakeSecret, "derived", empty, TLS_SCHEDULE_FROM, salt)
|
|
590
|
-
hkdfExtractInto(kdf, hashLength, salt,
|
|
606
|
+
hkdfExtractInto(kdf, hashLength, salt, tlsHashZeros(hashLength), out, TLS_SCHEDULE_FROM)
|
|
591
607
|
secureZero(salt)
|
|
592
608
|
}
|
|
593
609
|
|
|
@@ -615,7 +631,7 @@ export const tlsFinishedVerifyDataInto = (
|
|
|
615
631
|
at: i32
|
|
616
632
|
): void => {
|
|
617
633
|
const none: u8[] = []
|
|
618
|
-
const finishedKey: u8[] =
|
|
634
|
+
const finishedKey: u8[] = tlsHashZeros(hashLength)
|
|
619
635
|
hkdfExpandLabelInto(
|
|
620
636
|
kdf,
|
|
621
637
|
hashLength,
|
|
@@ -652,7 +668,7 @@ export const tlsFinishedVerifyDataInto = (
|
|
|
652
668
|
* @param out Where the early secret is written, from index 0; the caller owns and wipes it.
|
|
653
669
|
*/
|
|
654
670
|
export const tlsPskEarlySecretInto = (kdf: HkdfScratch, hashLength: i32, psk: u8[], out: u8[]): void => {
|
|
655
|
-
hkdfExtractInto(kdf, hashLength,
|
|
671
|
+
hkdfExtractInto(kdf, hashLength, tlsHashZeros(hashLength), psk, out, TLS_SCHEDULE_FROM)
|
|
656
672
|
}
|
|
657
673
|
|
|
658
674
|
/**
|
|
@@ -665,7 +681,7 @@ export const tlsPskEarlySecretInto = (kdf: HkdfScratch, hashLength: i32, psk: u8
|
|
|
665
681
|
* @param out Where the binder key is written, from index 0; the caller owns and wipes it.
|
|
666
682
|
*/
|
|
667
683
|
export const tlsBinderKeyInto = (kdf: HkdfScratch, hashLength: i32, earlySecret: u8[], out: u8[]): void => {
|
|
668
|
-
const empty: u8[] =
|
|
684
|
+
const empty: u8[] = tlsHashZeros(hashLength)
|
|
669
685
|
tlsEmptyHashInto(hashLength, empty)
|
|
670
686
|
tlsDeriveSecretInto(kdf, hashLength, earlySecret, "res binder", empty, TLS_SCHEDULE_FROM, out)
|
|
671
687
|
}
|
|
@@ -687,7 +703,7 @@ export const tlsHandshakeSaltInto = (
|
|
|
687
703
|
earlySecret: u8[],
|
|
688
704
|
out: u8[]
|
|
689
705
|
): void => {
|
|
690
|
-
const empty: u8[] =
|
|
706
|
+
const empty: u8[] = tlsHashZeros(hashLength)
|
|
691
707
|
tlsEmptyHashInto(hashLength, empty)
|
|
692
708
|
tlsDeriveSecretInto(kdf, hashLength, earlySecret, "derived", empty, TLS_SCHEDULE_FROM, out)
|
|
693
709
|
}
|
|
@@ -765,7 +781,7 @@ export const tlsBinderVerify = (
|
|
|
765
781
|
if (binderLength !== hashLength) {
|
|
766
782
|
return false
|
|
767
783
|
}
|
|
768
|
-
const expected: u8[] =
|
|
784
|
+
const expected: u8[] = tlsHashZeros(hashLength)
|
|
769
785
|
tlsFinishedVerifyDataInto(kdf, hashLength, binderKey, truncatedHash, hashOff, expected, TLS_SCHEDULE_FROM)
|
|
770
786
|
const verified: boolean = timingSafeEqualAt(binder, binderOff, expected, TLS_SCHEDULE_FROM, hashLength)
|
|
771
787
|
secureZero(expected)
|
|
@@ -844,11 +860,16 @@ const tlsHeldCopy = (held: u8[], from: u8[]): void => {
|
|
|
844
860
|
}
|
|
845
861
|
}
|
|
846
862
|
|
|
847
|
-
/**
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
863
|
+
/**
|
|
864
|
+
* `transcriptHash[hashOff .. hashOff + hashLength)` in a fresh array: the
|
|
865
|
+
* context a held form hands HKDF. Panics naming `caller` when the window is
|
|
866
|
+
* not inside `transcriptHash`, as `tlsHeldCheck` does for a wrong hash length:
|
|
867
|
+
* a shorter context would derive another secret without a word.
|
|
868
|
+
*/
|
|
869
|
+
const tlsHeldWindow = (caller: string, hashLength: i32, transcriptHash: u8[], hashOff: i32): u8[] => {
|
|
870
|
+
const window: u8[] = tlsCopyWindow(transcriptHash, hashOff, hashLength)
|
|
871
|
+
if (toI32(window.length) !== hashLength) {
|
|
872
|
+
panic(`${caller}: a transcript hash at ${hashOff} outside its ${transcriptHash.length} bytes`)
|
|
852
873
|
}
|
|
853
874
|
return window
|
|
854
875
|
}
|
|
@@ -881,8 +902,8 @@ const tlsExtractHeld = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]>,
|
|
|
881
902
|
* `tlsDeriveSecret` into a held `Secret`: Derive-Secret (RFC 8446 §7.1) of
|
|
882
903
|
* `prk` with `label` and the transcript hash `transcriptHash[hashOff ..
|
|
883
904
|
* hashOff + hashLength)`, written over the HashLen bytes `dst` holds. `dst`
|
|
884
|
-
* may be `prk`. Panics unless `hashLength` is 32 or 48
|
|
885
|
-
*
|
|
905
|
+
* may be `prk`. Panics unless `hashLength` is 32 or 48, both hold HashLen
|
|
906
|
+
* bytes and the transcript hash's window is inside `transcriptHash`.
|
|
886
907
|
*
|
|
887
908
|
* @param hashLength The hash length, 32 or 48.
|
|
888
909
|
* @param prk The held stage of the schedule to derive from. It is only overwritten when it is also `dst`.
|
|
@@ -901,7 +922,7 @@ export const tlsDeriveSecretHeld = (
|
|
|
901
922
|
): void => {
|
|
902
923
|
tlsHeldCheck("tlsDeriveSecretHeld", hashLength, prk)
|
|
903
924
|
tlsHeldCheck("tlsDeriveSecretHeld", hashLength, dst)
|
|
904
|
-
const context: u8[] = tlsHeldWindow(hashLength, transcriptHash, hashOff)
|
|
925
|
+
const context: u8[] = tlsHeldWindow("tlsDeriveSecretHeld", hashLength, transcriptHash, hashOff)
|
|
905
926
|
if (hashLength === SHA384_SIZE) {
|
|
906
927
|
hkdfExpandLabelSha384Held(dst, prk, label, context)
|
|
907
928
|
} else {
|
|
@@ -928,15 +949,11 @@ export const tlsHandshakeSecretHeld = (
|
|
|
928
949
|
dst: Secret<u8[]>
|
|
929
950
|
): void => {
|
|
930
951
|
tlsHeldCheck("tlsHandshakeSecretHeld", hashLength, dst)
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
tlsEmptyHash(hashLength),
|
|
937
|
-
TLS_SCHEDULE_FROM,
|
|
938
|
-
derived
|
|
939
|
-
)
|
|
952
|
+
// Sized by the empty hash, not by a second test of `hashLength`, which `-O2`
|
|
953
|
+
// would thread into a copy of the derivation (and its wipes) for each size.
|
|
954
|
+
const empty: u8[] = tlsEmptyHash(hashLength)
|
|
955
|
+
const derived: Secret<u8[]> = secret(new Array<u8>(toU16(empty.length)))
|
|
956
|
+
tlsDeriveSecretHeld(hashLength, earlySecret, "derived", empty, TLS_SCHEDULE_FROM, derived)
|
|
940
957
|
tlsExtractHeld(hashLength, derived, ecdhe, dst)
|
|
941
958
|
wipe(derived)
|
|
942
959
|
}
|
|
@@ -958,7 +975,7 @@ export const tlsMasterSecretHeld = (
|
|
|
958
975
|
dst: Secret<u8[]>
|
|
959
976
|
): void => {
|
|
960
977
|
tlsHeldCheck("tlsMasterSecretHeld", hashLength, dst)
|
|
961
|
-
const derived: Secret<u8[]> = secret(
|
|
978
|
+
const derived: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
962
979
|
tlsDeriveSecretHeld(
|
|
963
980
|
hashLength,
|
|
964
981
|
handshakeSecret,
|
|
@@ -967,7 +984,7 @@ export const tlsMasterSecretHeld = (
|
|
|
967
984
|
TLS_SCHEDULE_FROM,
|
|
968
985
|
derived
|
|
969
986
|
)
|
|
970
|
-
const zeros: Secret<u8[]> = secret(
|
|
987
|
+
const zeros: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
971
988
|
tlsExtractHeld(hashLength, derived, zeros, dst)
|
|
972
989
|
wipe(derived)
|
|
973
990
|
wipe(zeros)
|
|
@@ -979,7 +996,8 @@ export const tlsMasterSecretHeld = (
|
|
|
979
996
|
* `finished_key = Expand-Label(baseKey, "finished", "", HashLen)` (RFC 8446
|
|
980
997
|
* §4.4.4). The finished key is held and wiped before it returns;
|
|
981
998
|
* `verify_data` goes on the wire, so it is plain. Panics unless `hashLength`
|
|
982
|
-
* is 32 or 48
|
|
999
|
+
* is 32 or 48, `baseKey` holds HashLen bytes and the transcript hash's window
|
|
1000
|
+
* is inside `transcriptHash`.
|
|
983
1001
|
*
|
|
984
1002
|
* @param hashLength The hash length, 32 or 48.
|
|
985
1003
|
* @param baseKey The sender's held handshake traffic secret. It stays the caller's: this does not wipe it.
|
|
@@ -994,14 +1012,15 @@ export const tlsFinishedVerifyDataHeld = (
|
|
|
994
1012
|
hashOff: i32
|
|
995
1013
|
): u8[] => {
|
|
996
1014
|
tlsHeldCheck("tlsFinishedVerifyDataHeld", hashLength, baseKey)
|
|
1015
|
+
// Refused before the finished key exists, so a panic leaves no key unwiped.
|
|
1016
|
+
const transcript: u8[] = tlsHeldWindow("tlsFinishedVerifyDataHeld", hashLength, transcriptHash, hashOff)
|
|
997
1017
|
const none: u8[] = []
|
|
998
|
-
const finishedKey: Secret<u8[]> = secret(
|
|
1018
|
+
const finishedKey: Secret<u8[]> = secret(tlsHashZeros(hashLength))
|
|
999
1019
|
if (hashLength === SHA384_SIZE) {
|
|
1000
1020
|
hkdfExpandLabelSha384Held(finishedKey, baseKey, "finished", none)
|
|
1001
1021
|
} else {
|
|
1002
1022
|
hkdfExpandLabelSha256Held(finishedKey, baseKey, "finished", none)
|
|
1003
1023
|
}
|
|
1004
|
-
const transcript: u8[] = tlsHeldWindow(hashLength, transcriptHash, hashOff)
|
|
1005
1024
|
const verifyData: u8[] =
|
|
1006
1025
|
hashLength === SHA384_SIZE
|
|
1007
1026
|
? hmacSha384Secret(finishedKey, transcript)
|