@amritk/nish-x86_64-linux 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.
Files changed (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
package/std/net/tls.ts CHANGED
@@ -252,28 +252,32 @@ const tlsConfigFits = (config: TlsServerConfig): boolean => {
252
252
  return false
253
253
  }
254
254
  // The request context (1) and the list's length (3), then per certificate
255
- // its length (3), its bytes and its empty extensions (2).
256
- let certificate: i32 = 4
255
+ // its length (3), its bytes and its empty extensions (2). Unsigned, so the
256
+ // sums are no overflow site: the total stays at most 2^24 - 1 and each `n`
257
+ // below 2^31, so none wraps.
258
+ let certificate: u64 = 4
257
259
  for (const der of config.certificateChain) {
258
- const n: i32 = toI32(der.length)
259
- if (n < 1 || n > TLS_MAX_U24 - certificate - 5) {
260
+ const n: u64 = toU64(toI32(der.length))
261
+ if (n < toU64(1) || certificate + toU64(5) + n > toU64(TLS_MAX_U24)) {
260
262
  return false
261
263
  }
262
- certificate = certificate + 5 + n
264
+ certificate = certificate + toU64(5) + n
263
265
  }
264
266
  // ALPN's type, lengths and the one name (at most 4 + 2 + 1 + 255), the
265
267
  // server_name acknowledgement (4), the transport parameters' type and
266
268
  // length (4), and `record_size_limit` (6), beside what the caller passes
267
- // through.
268
- const extensions: i32 =
269
- 272 + toI32(config.extraExtensions.length) + toI32(config.quicTransportParameters.length)
269
+ // through. Two lengths and 272 cannot overflow an i64.
270
+ const extensions: i64 =
271
+ toI64(272) +
272
+ toI64(toI32(config.extraExtensions.length)) +
273
+ toI64(toI32(config.quicTransportParameters.length))
270
274
  for (const protocol of config.alpn) {
271
275
  const n: i32 = toI32(protocol.length)
272
276
  if (n < 1 || n > 255) {
273
277
  return false
274
278
  }
275
279
  }
276
- return extensions <= 65535
280
+ return extensions <= toI64(65535)
277
281
  }
278
282
 
279
283
  /**
@@ -284,11 +288,16 @@ const tlsConfigFits = (config: TlsServerConfig): boolean => {
284
288
  const tlsExtensionsHave = (extensions: u8[], type: i32): boolean => {
285
289
  const end: i32 = toI32(extensions.length)
286
290
  let at: i32 = 0
287
- while (at >= 0 && at + 3 < end) {
291
+ while (at >= 0 && at <= end - 4) {
288
292
  if (((toI32(extensions[at]) << 8) | toI32(extensions[at + 1])) === type) {
289
293
  return true
290
294
  }
291
- at = at + 4 + ((toI32(extensions[at + 2]) << 8) | toI32(extensions[at + 3]))
295
+ const length: i32 = (toI32(extensions[at + 2]) << 8) | toI32(extensions[at + 3])
296
+ if (length > end - at - 4) {
297
+ return false
298
+ }
299
+ // At most `end`, an array length, so the unsigned sum cannot wrap.
300
+ at = toI32(toU32(at) + toU32(4) + toU32(length))
292
301
  }
293
302
  return false
294
303
  }
@@ -302,14 +311,13 @@ const tlsBytesSpell = (data: u8[], at: i32, length: i32, text: string): boolean
302
311
  if (toI32(text.length) !== length) {
303
312
  return false
304
313
  }
305
- for (
306
- let k: i32 = 0;
307
- k < length && k < toI32(text.length) && at + k >= 0 && at + k < toI32(data.length);
308
- k++
309
- ) {
310
- if (toI32(data[at + k]) !== toI32(text.charCodeAt(k))) {
314
+ // `j` is `at + k`, stepped beside `k` so the index is the one the loop tests.
315
+ let j: i32 = at
316
+ for (let k: i32 = 0; k < length && k < toI32(text.length) && j >= 0 && j < toI32(data.length); k++) {
317
+ if (toI32(data[j]) !== toI32(text.charCodeAt(k))) {
311
318
  return false
312
319
  }
320
+ j++
313
321
  }
314
322
  return true
315
323
  }
@@ -324,37 +332,63 @@ const TLS_P256_DER_MAX: i32 = 72
324
332
  * bit is set, and zero one zero octet rather than an empty integer (X.690
325
333
  * §8.3.1).
326
334
  */
327
- const tlsDerIntegerLength = (rs: u8[], at: i32): i32 => {
328
- let first: i32 = 0
329
- while (first < 31 && at + first >= 0 && at + first < toI32(rs.length) && toI32(rs[at + first]) === 0) {
335
+ const tlsDerIntegerLength = (rs: u8[], at: i32): u8 => {
336
+ // Unsigned and at most 31, so `33 - first` and `32 - first` cannot wrap;
337
+ // `j` is `at + first`, stepped beside it.
338
+ let first: u32 = 0
339
+ let j: i32 = at
340
+ while (first < toU32(31) && j >= 0 && j < toI32(rs.length) && toI32(rs[j]) === 0) {
330
341
  first++
342
+ j++
331
343
  }
332
- const top: boolean = at + first >= 0 && at + first < toI32(rs.length) && toI32(rs[at + first]) >= 0x80
333
- return 32 - first + (top ? 1 : 0)
344
+ const top: boolean = j >= 0 && j < toI32(rs.length) && toI32(rs[j]) >= 0x80
345
+ return toU8((top ? toU32(33) : toU32(32)) - first)
334
346
  }
335
347
 
336
- /** That INTEGER, of body length `length`, written at `out[to ..)`; answers where it ends. */
348
+ /**
349
+ * That INTEGER, of body length `length` (1 to 33), written at `out[to ..)`;
350
+ * answers where it ends, or -1, writing nothing, when it does not fit in
351
+ * `out` or `rs[at .. at + 32)` is not inside `rs`.
352
+ */
337
353
  const tlsDerIntegerInto = (out: u8[], to: i32, rs: u8[], at: i32, length: i32): i32 => {
338
- const end: i32 = to + 2 + length
339
- if (to < 0 || end > toI32(out.length)) {
340
- return end
354
+ // The second test is implied by the first; it is the shape the bounds
355
+ // proof reads for the two header bytes.
356
+ if (
357
+ length < 1 ||
358
+ length > 33 ||
359
+ to < 0 ||
360
+ to > toI32(out.length) - 2 - length ||
361
+ to > toI32(out.length) - 3
362
+ ) {
363
+ return -1
364
+ }
365
+ if (at < 0 || at > toI32(rs.length) - 32) {
366
+ return -1
341
367
  }
342
368
  out[to] = toU8(0x02)
343
369
  out[to + 1] = toU8(length)
344
- for (let k: i32 = 0; k < length; k++) {
345
- // The body's last `length` bytes are the magnitude's, a zero first when it is 33.
346
- const from: i32 = at + 32 - length + k
347
- const j: i32 = to + 2 + k
348
- if (j >= 0 && j < toI32(out.length)) {
349
- out[j] = from >= at && from >= 0 && from < toI32(rs.length) ? rs[from] : toU8(0)
350
- }
351
- }
352
- return end
370
+ // The body's last `length` bytes are the magnitude's, a zero first when it
371
+ // is 33. `j` walks the body and `from` the magnitude, beside `k`; the
372
+ // checks above keep both inside their arrays (`from` one below `at` for a
373
+ // 33-byte body), which the loop tests again.
374
+ let j: i32 = to + 2
375
+ let from: i32 = at + (32 - length)
376
+ for (let k: i32 = 0; k < length && j >= 0 && j < toI32(out.length) && from < toI32(rs.length); k++) {
377
+ out[j] = from >= at && from >= 0 ? rs[from] : toU8(0)
378
+ j++
379
+ from++
380
+ }
381
+ // Inside `out` by the check above, so the unsigned sum cannot wrap.
382
+ return toI32(toU32(to) + toU32(2) + toU32(length))
353
383
  }
354
384
 
355
- /** The length of `rs`'s DER `ECDSA-Sig-Value`: the SEQUENCE's two bytes and two INTEGERs. */
356
- const tlsEcdsaDerLength = (rs: u8[]): i32 =>
357
- 6 + tlsDerIntegerLength(rs, TLS_FROM) + tlsDerIntegerLength(rs, 32)
385
+ /**
386
+ * The length of `rs`'s DER `ECDSA-Sig-Value`: the SEQUENCE's two bytes and two
387
+ * INTEGERs. A `u8`: each INTEGER is at most 35 bytes, so the sum is at most 72
388
+ * and cannot wrap.
389
+ */
390
+ const tlsEcdsaDerLength = (rs: u8[]): u8 =>
391
+ toU8(6) + tlsDerIntegerLength(rs, TLS_FROM) + tlsDerIntegerLength(rs, 32)
358
392
 
359
393
  /** `rs`'s DER `ECDSA-Sig-Value` into all of `der`, which is `tlsEcdsaDerLength(rs)` bytes. At most 72, so the length is one short-form byte. */
360
394
  const tlsEcdsaDerInto = (der: u8[], rs: u8[]): void => {
@@ -363,8 +397,14 @@ const tlsEcdsaDerInto = (der: u8[], rs: u8[]): void => {
363
397
  }
364
398
  der[0] = toU8(0x30)
365
399
  der[1] = toU8(toI32(der.length) - 2)
366
- const r: i32 = tlsDerIntegerLength(rs, TLS_FROM)
367
- tlsDerIntegerInto(der, tlsDerIntegerInto(der, 2, rs, TLS_FROM, r), rs, 32, tlsDerIntegerLength(rs, 32))
400
+ const r: i32 = toI32(tlsDerIntegerLength(rs, TLS_FROM))
401
+ tlsDerIntegerInto(
402
+ der,
403
+ tlsDerIntegerInto(der, 2, rs, TLS_FROM, r),
404
+ rs,
405
+ 32,
406
+ toI32(tlsDerIntegerLength(rs, 32))
407
+ )
368
408
  }
369
409
 
370
410
  /**
@@ -430,7 +470,8 @@ export class TlsP256Signer {
430
470
  this.scratch = new P256SignScratch()
431
471
  this.rs = new Array<u8>(P256_SIGNATURE_SIZE)
432
472
  this.ders = []
433
- for (let length: i32 = TLS_P256_DER_MIN; length <= TLS_P256_DER_MAX; length++) {
473
+ // A `u8` from 8 to 72, which `++` cannot wrap.
474
+ for (let length: u8 = toU8(TLS_P256_DER_MIN); toI32(length) <= TLS_P256_DER_MAX; length++) {
434
475
  this.ders.push(new Array<u8>(length))
435
476
  }
436
477
  }
@@ -451,7 +492,7 @@ export class TlsP256Signer {
451
492
  ) {
452
493
  return null
453
494
  }
454
- const at: i32 = tlsEcdsaDerLength(this.rs) - TLS_P256_DER_MIN
495
+ const at: i32 = toI32(tlsEcdsaDerLength(this.rs)) - TLS_P256_DER_MIN
455
496
  if (at < 0 || at >= toI32(this.ders.length)) {
456
497
  return null
457
498
  }
@@ -530,7 +571,9 @@ export const tlsHeldCopyIn = (held: u8[], from: u8[]): i32 => {
530
571
  * @returns A fresh plain array of the same length, which the caller must wipe.
531
572
  */
532
573
  export const tlsHeldClone = (held: u8[]): u8[] => {
533
- const copy: u8[] = new Array<u8>(held.length)
574
+ // Sized before a secret byte goes in, so no grow leaves a copied prefix
575
+ // behind in the arena: `tlsZeros` takes a held secret's `i32` length.
576
+ const copy: u8[] = tlsZeros(toI32(held.length))
534
577
  for (let k: i32 = 0; k < toI32(copy.length) && k < toI32(held.length); k++) {
535
578
  copy[k] = held[k]
536
579
  }
@@ -626,23 +669,53 @@ class TlsExchangeWords {
626
669
  hashLength: i32 = 0
627
670
  }
628
671
 
672
+ /**
673
+ * `n` zero bytes; none for a negative `n`. A length a `u16` holds is one
674
+ * allocation; a longer one is pushed a byte at a time.
675
+ */
676
+ const tlsZeros = (n: i32): u8[] => {
677
+ if (n >= 0 && n <= 65535) {
678
+ return new Array<u8>(toU16(n))
679
+ }
680
+ const out: u8[] = []
681
+ for (let k: i32 = 0; k < n; k++) {
682
+ out.push(toU8(0))
683
+ }
684
+ return out
685
+ }
686
+
629
687
  /** The eight bytes `b[at .. at + 8)` as a big-endian word; a byte outside `b` reads as zero. */
630
688
  const tlsWordAt = (b: u8[], at: i32): u64 => {
631
689
  let v: u64 = 0
690
+ // `j` walks `at .. at + 8` in unsigned steps; it is read only inside `b`.
691
+ let j: u32 = toU32(at)
632
692
  for (let k: i32 = 0; k < 8; k++) {
633
- const byte: u64 = at + k >= 0 && at + k < toI32(b.length) ? toU64(b[at + k]) : toU64(0)
693
+ const i: i32 = toI32(j)
694
+ const byte: u64 = at >= 0 && i >= 0 && i < toI32(b.length) ? toU64(b[i]) : toU64(0)
634
695
  v = (v << toU64(8)) | byte
696
+ j = j + toU32(1)
635
697
  }
636
698
  return v
637
699
  }
638
700
 
701
+ /**
702
+ * `tlsWordAt(b, at + skip)` for a share's later words. The offset is summed
703
+ * in u32, which is the i32 sum wherever that does not wrap; where it would,
704
+ * the offset lands below zero and the word reads as zero, outside `b`.
705
+ */
706
+ const tlsWordAfter = (b: u8[], at: i32, skip: i32): u64 => tlsWordAt(b, toI32(toU32(at) + toU32(skip)))
707
+
639
708
  /** `v` big-endian into `b[at .. at + 8)`; a byte outside `b` is dropped. */
640
709
  const tlsWordInto = (b: u8[], at: i32, v: u64): void => {
710
+ // `j` walks `at .. at + 8` in unsigned steps; it is written only inside `b`.
711
+ let j: u32 = toU32(at)
641
712
  for (let k: i32 = 0; k < 8; k++) {
642
713
  const shift: i32 = (7 - k) << 3
643
- if (at + k >= 0 && at + k < toI32(b.length)) {
644
- b[at + k] = toU8(v >> toU64(shift))
714
+ const i: i32 = toI32(j)
715
+ if (at >= 0 && i >= 0 && i < toI32(b.length)) {
716
+ b[i] = toU8(v >> toU64(shift))
645
717
  }
718
+ j = j + toU32(1)
646
719
  }
647
720
  }
648
721
 
@@ -673,7 +746,7 @@ const tlsHandshakeFromWords = (held: u8[], w: TlsExchangeWords): i32 => {
673
746
  * returns.
674
747
  */
675
748
  const tlsExchange = (w: TlsExchangeWords): i32 => {
676
- const salt: u8[] = new Array<u8>(w.hashLength === SHA384_SIZE ? SHA384_SIZE : SHA256_SIZE)
749
+ const salt: u8[] = new Array<u8>(toU8(w.hashLength === SHA384_SIZE ? SHA384_SIZE : SHA256_SIZE))
677
750
  tlsWordInto(salt, 0, w.salt0)
678
751
  tlsWordInto(salt, 8, w.salt1)
679
752
  tlsWordInto(salt, 16, w.salt2)
@@ -686,11 +759,12 @@ const tlsExchange = (w: TlsExchangeWords): i32 => {
686
759
  tlsWordInto(raw, 8, w.key1)
687
760
  tlsWordInto(raw, 16, w.key2)
688
761
  tlsWordInto(raw, 24, w.key3)
689
- if (p256) {
690
- // Below 2^255, so below the order n: only all zeros is refused.
762
+ if (p256 && raw.length > 0) {
763
+ // Below 2^255, so below the order n: only all zeros is refused. (`raw`
764
+ // is never empty; the length test is the shape the bounds proof reads.)
691
765
  raw[0] = raw[0] & toU8(0x7f)
692
766
  }
693
- const share: u8[] = new Array<u8>(p256 ? TLS_P256_SHARE_SIZE : X25519_SIZE)
767
+ const share: u8[] = new Array<u8>(toU8(p256 ? TLS_P256_SHARE_SIZE : X25519_SIZE))
694
768
  tlsWordInto(share, 0, w.share0)
695
769
  tlsWordInto(share, 8, w.share1)
696
770
  tlsWordInto(share, 16, w.share2)
@@ -830,21 +904,24 @@ export class TlsSecretPool {
830
904
  /**
831
905
  * Allocates every array of the pool once, all zero, so a connection never allocates one for a secret.
832
906
  *
833
- * @param hashLength The suite's hash length in bytes: 32 for SHA-256, 48 for SHA-384.
907
+ * @param hashLength The suite's hash length in bytes: 32 for SHA-256, 48 for SHA-384. A
908
+ * negative one makes every array empty.
834
909
  */
835
910
  constructor(hashLength: i32) {
836
- this.handshake = secret(new Array<u8>(hashLength))
837
- this.clientHandshake = secret(new Array<u8>(hashLength))
838
- this.serverHandshake = secret(new Array<u8>(hashLength))
839
- this.clientApplication = secret(new Array<u8>(hashLength))
840
- this.serverApplication = secret(new Array<u8>(hashLength))
841
- this.exporter = secret(new Array<u8>(hashLength))
842
- this.resumption = secret(new Array<u8>(hashLength))
843
- this.psk = secret(new Array<u8>(hashLength))
844
- this.early = secret(new Array<u8>(hashLength))
845
- this.binderKey = secret(new Array<u8>(hashLength))
846
- this.clientFinished = new Array<u8>(hashLength)
847
- this.toBeSigned = new Array<u8>(hashLength + TLS_CERTIFICATE_VERIFY_PREFIX)
911
+ this.handshake = secret(tlsZeros(hashLength))
912
+ this.clientHandshake = secret(tlsZeros(hashLength))
913
+ this.serverHandshake = secret(tlsZeros(hashLength))
914
+ this.clientApplication = secret(tlsZeros(hashLength))
915
+ this.serverApplication = secret(tlsZeros(hashLength))
916
+ this.exporter = secret(tlsZeros(hashLength))
917
+ this.resumption = secret(tlsZeros(hashLength))
918
+ this.psk = secret(tlsZeros(hashLength))
919
+ this.early = secret(tlsZeros(hashLength))
920
+ this.binderKey = secret(tlsZeros(hashLength))
921
+ this.clientFinished = tlsZeros(hashLength)
922
+ // A u64 sum, which cannot wrap; past `i32` it is no length `tlsZeros` makes.
923
+ const signed: u64 = toU64(hashLength) + toU64(TLS_CERTIFICATE_VERIFY_PREFIX)
924
+ this.toBeSigned = tlsZeros(hashLength >= 0 && signed <= toU64(2147483647) ? toI32(signed) : -1)
848
925
  }
849
926
 
850
927
  /**
@@ -925,7 +1002,8 @@ const tlsFlightCapacity = (config: TlsServerConfig): i32 => {
925
1002
  config.quic ? 0 : TLS_RECORD_SIZE_LIMIT_MAX
926
1003
  )
927
1004
  const certificate: i32 = tlsWriteCertificate(none, TLS_FROM, config.certificateChain)
928
- return extensions + certificate + 8 + TLS_ECDSA_SIGNATURE_MAX + 4 + SHA384_SIZE
1005
+ // Two non-negative i32s and a constant: the u32 sum cannot wrap, and `tlsConfigFits` keeps it far below 2^31.
1006
+ return toI32(toU32(extensions) + toU32(certificate) + toU32(8 + TLS_ECDSA_SIGNATURE_MAX + 4 + SHA384_SIZE))
929
1007
  }
930
1008
 
931
1009
  // ---- Resumption ------------------------------------------------------------------
@@ -949,8 +1027,17 @@ const tlsServerStrikeProbe = (strikes: TlsTicketStrikes, id: u8[], now: i64): i3
949
1027
  const capacity: i32 = strikes.capacity
950
1028
  const home: i32 = strikes.home(id)
951
1029
  let free: i32 = -1
1030
+ // `home` answers a slot below `capacity`; any other fails closed.
1031
+ if (capacity < 1 || home < 0 || home >= capacity) {
1032
+ return TLS_SERVER_STRIKE_FULL
1033
+ }
1034
+ // `(home + n) % capacity`, stepped: it wraps to 0 past the last slot.
1035
+ let slot: i32 = home
952
1036
  for (let n: i32 = 0; n < capacity; n++) {
953
- const slot: i32 = (home + n) % capacity
1037
+ // The table's arrays are `capacity` long; a slot outside them fails closed.
1038
+ if (slot < 0 || slot >= toI32(strikes.used.length) || slot >= toI32(strikes.expires.length)) {
1039
+ return TLS_SERVER_STRIKE_FULL
1040
+ }
954
1041
  if (strikes.used[slot] === 0) {
955
1042
  if (free < 0) {
956
1043
  free = slot
@@ -967,6 +1054,7 @@ const tlsServerStrikeProbe = (strikes: TlsTicketStrikes, id: u8[], now: i64): i3
967
1054
  if (free >= 0 && n >= strikes.longest) {
968
1055
  break
969
1056
  }
1057
+ slot = slot < 0 || slot >= capacity - 1 ? 0 : slot + 1
970
1058
  }
971
1059
  return free < 0 ? TLS_SERVER_STRIKE_FULL : TLS_SERVER_STRIKE_FREE
972
1060
  }
@@ -1186,7 +1274,7 @@ export class TlsServer {
1186
1274
  this.extraRecordSizeLimit = tlsExtensionsHave(config.extraExtensions, TLS_EXT_RECORD_SIZE_LIMIT)
1187
1275
  this.input = new Array<u8>(TLS_INPUT_START)
1188
1276
  this.outputInitial = new Array<u8>(TLS_INITIAL_OUTPUT_SIZE)
1189
- this.outputHandshake = new Array<u8>(this.configFits ? tlsFlightCapacity(config) : TLS_FROM)
1277
+ this.outputHandshake = tlsZeros(this.configFits ? tlsFlightCapacity(config) : TLS_FROM)
1190
1278
  this.secrets256 = new TlsSecretPool(SHA256_SIZE)
1191
1279
  this.secrets384 = new TlsSecretPool(SHA384_SIZE)
1192
1280
  this.hello = new TlsClientHelloView()
@@ -1200,8 +1288,11 @@ export class TlsServer {
1200
1288
  this.binderHash = new Array<u8>(SHA384_SIZE)
1201
1289
  this.opened = new TlsTicketState()
1202
1290
  this.issuing = new TlsTicketState()
1203
- this.ticketScratch = new Array<u8>(tlsServerTicketCapacity(this.issuing))
1204
- this.outputApplication = new Array<u8>(toI32(this.ticketScratch.length) + TLS_SERVER_TICKET_OVERHEAD)
1291
+ this.ticketScratch = tlsZeros(tlsServerTicketCapacity(this.issuing))
1292
+ // A few hundred bytes and a constant: the u32 sum cannot wrap.
1293
+ this.outputApplication = tlsZeros(
1294
+ toI32(toU32(toI32(this.ticketScratch.length)) + toU32(TLS_SERVER_TICKET_OVERHEAD))
1295
+ )
1205
1296
  this.ticketNonce = new Array<u8>(1)
1206
1297
  this.ageAddBytes = new Array<u8>(4)
1207
1298
  this.restart(serverRandom, ephemeralPrivate)
@@ -1377,13 +1468,18 @@ export class TlsServer {
1377
1468
  if (need <= capacity) {
1378
1469
  return
1379
1470
  }
1471
+ const most: i32 = TLS_MAX_HANDSHAKE_MESSAGE + 4
1380
1472
  let size: i32 = capacity
1381
- while (size < need) {
1382
- size = size * 2
1473
+ // Stopping at `most` gives the same clamped size; below it the u32 doubling cannot wrap.
1474
+ while (size < need && size < most) {
1475
+ size = toI32(toU32(size) * toU32(2))
1383
1476
  }
1384
- const most: i32 = TLS_MAX_HANDSHAKE_MESSAGE + 4
1385
- const grown: u8[] = new Array<u8>(size > most ? most : size)
1386
- for (let k: i32 = 0; k < this.inputLength && k < toI32(grown.length) && k < capacity; k++) {
1477
+ const grown: u8[] = tlsZeros(size > most ? most : size)
1478
+ for (
1479
+ let k: i32 = 0;
1480
+ k < this.inputLength && k < toI32(grown.length) && k < toI32(this.input.length);
1481
+ k++
1482
+ ) {
1387
1483
  grown[k] = this.input[k]
1388
1484
  }
1389
1485
  this.input = grown
@@ -1403,7 +1499,7 @@ export class TlsServer {
1403
1499
  if (need <= capacity) {
1404
1500
  return
1405
1501
  }
1406
- const grown: u8[] = new Array<u8>(need)
1502
+ const grown: u8[] = tlsZeros(need)
1407
1503
  for (let k: i32 = 0; k < held && k < toI32(buffer.length) && k < toI32(grown.length); k++) {
1408
1504
  grown[k] = buffer[k]
1409
1505
  }
@@ -1439,7 +1535,7 @@ export class TlsServer {
1439
1535
  if (this.state === TLS_STATE_FAILED) {
1440
1536
  return this.alert
1441
1537
  }
1442
- if (off < 0 || len < 0 || off > toI32(data.length) - len) {
1538
+ if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(toI32(data.length))) {
1443
1539
  return this.fail(TLS_ALERT_INTERNAL_ERROR)
1444
1540
  }
1445
1541
  if (len === 0) {
@@ -1450,28 +1546,39 @@ export class TlsServer {
1450
1546
  }
1451
1547
  // The buffer only ever holds one message, so bytes that would take it
1452
1548
  // past the largest one allowed are refused before they are copied.
1453
- if (len > TLS_MAX_HANDSHAKE_MESSAGE + 4 - this.inputLength) {
1549
+ const total: i64 = toI64(this.inputLength) + toI64(len)
1550
+ if (total > toI64(TLS_MAX_HANDSHAKE_MESSAGE + 4)) {
1454
1551
  return this.fail(TLS_ALERT_DECODE_ERROR)
1455
1552
  }
1456
- this.reserveInput(this.inputLength + len)
1553
+ // At most the largest message, so within an i32.
1554
+ this.reserveInput(toI32(total))
1555
+ // `at` and `j` step beside `k` through `input` and `data`.
1556
+ let at: i32 = this.inputLength
1557
+ let j: i32 = off
1457
1558
  for (
1458
1559
  let k: i32 = 0;
1459
- k < len && off + k < toI32(data.length) && this.inputLength < toI32(this.input.length);
1560
+ k < len && j >= 0 && j < toI32(data.length) && at >= 0 && at < toI32(this.input.length);
1460
1561
  k++
1461
1562
  ) {
1462
- this.input[this.inputLength] = data[off + k]
1463
- this.inputLength = this.inputLength + 1
1563
+ this.input[at] = data[j]
1564
+ at++
1565
+ j++
1464
1566
  }
1567
+ this.inputLength = at
1465
1568
  // Every message a client sends here ends its flight, so the buffer holds
1466
1569
  // at most one: a byte past its end is refused below.
1467
- if (this.inputLength < 4) {
1570
+ // `input` holds at least the `inputLength` bytes copied into it; the
1571
+ // second test is the shape the bounds proof reads.
1572
+ if (this.inputLength < 4 || toI32(this.input.length) < 4) {
1468
1573
  return 0
1469
1574
  }
1575
+ const type: i32 = toI32(this.input[0])
1470
1576
  const length: i32 = (toI32(this.input[1]) << 16) | (toI32(this.input[2]) << 8) | toI32(this.input[3])
1471
1577
  if (length > TLS_MAX_HANDSHAKE_MESSAGE) {
1472
1578
  return this.fail(TLS_ALERT_DECODE_ERROR)
1473
1579
  }
1474
- const whole: i32 = 4 + length
1580
+ // Three bytes, so 0 to 2^24 - 1: the u32 sum cannot wrap.
1581
+ const whole: i32 = toI32(toU32(4) + toU32(length))
1475
1582
  if (this.inputLength < whole) {
1476
1583
  return 0
1477
1584
  }
@@ -1481,7 +1588,7 @@ export class TlsServer {
1481
1588
  // The message stays where it is while it is handled; the buffer is
1482
1589
  // empty again for whatever comes next.
1483
1590
  this.inputLength = 0
1484
- const alert: i32 = this.handleMessage(length)
1591
+ const alert: i32 = this.handleMessage(type, length)
1485
1592
  if (alert !== 0) {
1486
1593
  return this.fail(alert)
1487
1594
  }
@@ -1489,12 +1596,11 @@ export class TlsServer {
1489
1596
  }
1490
1597
 
1491
1598
  /**
1492
- * Dispatches one whole message, `input[0 .. 4 + length)`. `receive`
1599
+ * Dispatches one whole message, `input[0 .. 4 + length)`, whose first byte is `type`. `receive`
1493
1600
  * admits bytes only in the two states that expect them, so the state is
1494
1601
  * one of those two here.
1495
1602
  */
1496
- private handleMessage(length: i32): i32 {
1497
- const type: i32 = toI32(this.input[0])
1603
+ private handleMessage(type: i32, length: i32): i32 {
1498
1604
  if (this.state === TLS_STATE_WAIT_CLIENT_HELLO) {
1499
1605
  return type === TLS_HANDSHAKE_CLIENT_HELLO
1500
1606
  ? this.handleClientHello(length)
@@ -1510,16 +1616,15 @@ export class TlsServer {
1510
1616
  */
1511
1617
  private chooseSuite(hello: TlsClientHelloView): i32 {
1512
1618
  const data: u8[] = hello.data
1513
- const end: i32 = hello.suitesAt + hello.suitesLength
1514
- for (
1515
- let k: i32 = hello.suitesAt;
1516
- k + 1 < end && k >= 0 && k < toI32(data.length) && k + 1 < toI32(data.length);
1517
- k += 2
1518
- ) {
1619
+ const end: i64 = toI64(hello.suitesAt) + toI64(hello.suitesLength)
1620
+ let k: i32 = hello.suitesAt
1621
+ while (toI64(k) + toI64(1) < end && k >= 0 && k < toI32(data.length) - 1) {
1519
1622
  const suite: i32 = (toI32(data[k]) << 8) | toI32(data[k + 1])
1520
1623
  if (tlsSuiteHashLength(suite) > 0) {
1521
1624
  return suite
1522
1625
  }
1626
+ // Both below 2^31, so the u32 sum cannot wrap; past i32 it ends the loop.
1627
+ k = toI32(toU32(k) + toU32(2))
1523
1628
  }
1524
1629
  return 0
1525
1630
  }
@@ -1543,16 +1648,17 @@ export class TlsServer {
1543
1648
  this.alpn = ""
1544
1649
  if (hello.hasAlpn && toI32(this.config.alpn.length) > 0) {
1545
1650
  const data: u8[] = hello.data
1546
- const end: i32 = hello.alpnAt + hello.alpnLength
1651
+ const end: i64 = toI64(hello.alpnAt) + toI64(hello.alpnLength)
1547
1652
  for (const protocol of this.config.alpn) {
1548
1653
  let at: i32 = hello.alpnAt
1549
- while (at < end && at >= 0 && at < toI32(data.length)) {
1654
+ while (toI64(at) < end && at >= 0 && at < toI32(data.length)) {
1550
1655
  const n: i32 = toI32(data[at])
1551
- if (tlsBytesSpell(data, at + 1, n, protocol)) {
1656
+ // `at` is inside `data` and `n` a byte, so the u32 sums cannot wrap.
1657
+ if (tlsBytesSpell(data, toI32(toU32(at) + toU32(1)), n, protocol)) {
1552
1658
  this.alpn = protocol
1553
1659
  return 0
1554
1660
  }
1555
- at = at + 1 + n
1661
+ at = toI32(toU32(at) + toU32(1) + toU32(n))
1556
1662
  }
1557
1663
  }
1558
1664
  return TLS_ALERT_NO_APPLICATION_PROTOCOL
@@ -1658,9 +1764,12 @@ export class TlsServer {
1658
1764
  const prefix: i32 = hello.pskBindersPrefixAt
1659
1765
  this.transcript.update(data, TLS_FROM, prefix)
1660
1766
  this.transcript.hashInto(this.binderHash, TLS_FROM)
1661
- this.transcript.update(data, prefix, 4 + length - prefix)
1767
+ // `length` is three bytes and `prefix` inside the message, so the i64
1768
+ // difference is the i32 it always was.
1769
+ this.transcript.update(data, prefix, toI32(toI64(4) + toI64(length) - toI64(prefix)))
1662
1770
  } else {
1663
- this.transcript.update(data, TLS_FROM, 4 + length)
1771
+ // Three bytes, so the u32 sum cannot wrap.
1772
+ this.transcript.update(data, TLS_FROM, toI32(toU32(4) + toU32(length)))
1664
1773
  }
1665
1774
  // The group is the server's choice, not the client's: x25519 whenever the
1666
1775
  // client lists it, secp256r1 only for a client that does not. A hello
@@ -1724,15 +1833,11 @@ export class TlsServer {
1724
1833
  (!hello.hasServerName || hello.serverNameLength <= TLS_TICKET_SERVER_NAME_MAX)
1725
1834
  const name: u8[] = this.issuing.serverName
1726
1835
  const nameLength: i32 = this.ticketWanted && hello.hasServerName ? hello.serverNameLength : 0
1727
- for (
1728
- let k: i32 = 0;
1729
- k < nameLength &&
1730
- k < toI32(name.length) &&
1731
- hello.serverNameAt + k >= 0 &&
1732
- hello.serverNameAt + k < toI32(data.length);
1733
- k++
1734
- ) {
1735
- name[k] = data[hello.serverNameAt + k]
1836
+ // `j` steps beside `k` through the name's bytes in `data`.
1837
+ let j: i32 = hello.serverNameAt
1838
+ for (let k: i32 = 0; k < nameLength && k < toI32(name.length) && j >= 0 && j < toI32(data.length); k++) {
1839
+ name[k] = data[j]
1840
+ j++
1736
1841
  }
1737
1842
  this.issuing.serverNameLength = nameLength
1738
1843
  if (this.config.quic) {
@@ -1741,12 +1846,11 @@ export class TlsServer {
1741
1846
  while (parameters.length > 0) {
1742
1847
  parameters.pop()
1743
1848
  }
1744
- for (
1745
- let k: i32 = 0;
1746
- k < hello.quicLength && hello.quicAt + k >= 0 && hello.quicAt + k < toI32(data.length);
1747
- k++
1748
- ) {
1749
- parameters.push(data[hello.quicAt + k])
1849
+ // `j`, done with the name, now steps beside `k` through the parameters' bytes in `data`.
1850
+ j = hello.quicAt
1851
+ for (let k: i32 = 0; k < hello.quicLength && j >= 0 && j < toI32(data.length); k++) {
1852
+ parameters.push(data[j])
1853
+ j++
1750
1854
  }
1751
1855
  this.clientTransportParameters = parameters
1752
1856
  }
@@ -1848,15 +1952,12 @@ export class TlsServer {
1848
1952
  if (!tlsSelectPskIdentity(hello, k)) {
1849
1953
  break
1850
1954
  }
1851
- const ticket: u8[] = new Array<u8>(hello.pskIdentityLength)
1852
- for (
1853
- let n: i32 = 0;
1854
- n < toI32(ticket.length) &&
1855
- hello.pskIdentityAt + n >= 0 &&
1856
- hello.pskIdentityAt + n < toI32(data.length);
1857
- n++
1858
- ) {
1859
- ticket[n] = data[hello.pskIdentityAt + n]
1955
+ const ticket: u8[] = tlsZeros(hello.pskIdentityLength)
1956
+ // `j` steps beside `n` through the identity's bytes in `data`.
1957
+ let j: i32 = hello.pskIdentityAt
1958
+ for (let n: i32 = 0; n < toI32(ticket.length) && j >= 0 && j < toI32(data.length); n++) {
1959
+ ticket[n] = data[j]
1960
+ j++
1860
1961
  }
1861
1962
  const opens: boolean =
1862
1963
  tlsTicketOpen(current, currentInfo, ticket, now, opened) ||
@@ -1894,11 +1995,11 @@ export class TlsServer {
1894
1995
  secureZero(this.opened.psk)
1895
1996
  using _scope = arena()
1896
1997
  const psk: u8[] = expose(pool.psk, tlsHeldClone)
1897
- const early: u8[] = new Array<u8>(h)
1998
+ const early: u8[] = tlsZeros(h)
1898
1999
  tlsPskEarlySecretInto(this.kdf, h, psk, early)
1899
2000
  secureZero(psk)
1900
2001
  exposeWith(pool.early, early, tlsHeldCopyIn)
1901
- const binderKey: u8[] = new Array<u8>(h)
2002
+ const binderKey: u8[] = tlsZeros(h)
1902
2003
  tlsBinderKeyInto(this.kdf, h, early, binderKey)
1903
2004
  secureZero(early)
1904
2005
  exposeWith(pool.binderKey, binderKey, tlsHeldCopyIn)
@@ -2012,7 +2113,8 @@ export class TlsServer {
2012
2113
  this.config.quic ? this.config.quicTransportParameters : null,
2013
2114
  this.ownRecordSizeLimit()
2014
2115
  )
2015
- this.reserveOutput(TLS_LEVEL_HANDSHAKE, extensionsEnd + 4 + this.hashLength)
2116
+ // A message's end inside the output and a hash length: the u32 sum cannot wrap.
2117
+ this.reserveOutput(TLS_LEVEL_HANDSHAKE, toI32(toU32(extensionsEnd) + toU32(4) + toU32(this.hashLength)))
2016
2118
  // The secrets' pool is the slot's, chosen before the blocks open.
2017
2119
  const pool: TlsSecretPool = this.secrets()
2018
2120
  this.writeResumedHello(pool, index)
@@ -2080,15 +2182,15 @@ export class TlsServer {
2080
2182
  w.key2 = exposeWith(this.ephemeral, TLS_FROM + 16, tlsWordAt)
2081
2183
  w.key3 = exposeWith(this.ephemeral, TLS_FROM + 24, tlsWordAt)
2082
2184
  w.share0 = tlsWordAt(share, at)
2083
- w.share1 = tlsWordAt(share, at + 8)
2084
- w.share2 = tlsWordAt(share, at + 16)
2085
- w.share3 = tlsWordAt(share, at + 24)
2185
+ w.share1 = tlsWordAfter(share, at, 8)
2186
+ w.share2 = tlsWordAfter(share, at, 16)
2187
+ w.share3 = tlsWordAfter(share, at, 24)
2086
2188
  if (p256) {
2087
- w.share4 = tlsWordAt(share, at + 32)
2088
- w.share5 = tlsWordAt(share, at + 40)
2089
- w.share6 = tlsWordAt(share, at + 48)
2090
- w.share7 = tlsWordAt(share, at + 56)
2091
- w.share8 = tlsWordAt(share, at + 64)
2189
+ w.share4 = tlsWordAfter(share, at, 32)
2190
+ w.share5 = tlsWordAfter(share, at, 40)
2191
+ w.share6 = tlsWordAfter(share, at, 48)
2192
+ w.share7 = tlsWordAfter(share, at, 56)
2193
+ w.share8 = tlsWordAfter(share, at, 64)
2092
2194
  }
2093
2195
  w.hashLength = this.hashLength
2094
2196
  w.salt0 = tlsWordAt(this.salt, 0)
@@ -2209,7 +2311,7 @@ export class TlsServer {
2209
2311
  * with the caller's arena block, copied in, and zeroed.
2210
2312
  */
2211
2313
  private deriveInto(prk: u8[], label: string, dst: Secret<u8[]>): void {
2212
- const out: u8[] = new Array<u8>(this.hashLength)
2314
+ const out: u8[] = tlsZeros(this.hashLength)
2213
2315
  tlsDeriveSecretInto(this.kdf, this.hashLength, prk, label, this.hashes, TLS_FROM, out)
2214
2316
  exposeWith(dst, out, tlsHeldCopyIn)
2215
2317
  secureZero(out)
@@ -2249,7 +2351,11 @@ export class TlsServer {
2249
2351
  return this.fail(TLS_ALERT_INTERNAL_ERROR)
2250
2352
  }
2251
2353
  const h: i32 = this.hashLength
2252
- this.reserveOutput(TLS_LEVEL_HANDSHAKE, this.outputHandshakeLength + 8 + signatureLength + 4 + h)
2354
+ // A length inside the output, at most 65535 and a hash length: the u32 sum cannot wrap.
2355
+ this.reserveOutput(
2356
+ TLS_LEVEL_HANDSHAKE,
2357
+ toI32(toU32(this.outputHandshakeLength) + toU32(8) + toU32(signatureLength) + toU32(4) + toU32(h))
2358
+ )
2253
2359
  // The secrets' pool is the slot's, chosen before the blocks open.
2254
2360
  const pool: TlsSecretPool = this.secrets()
2255
2361
  this.writeCertificateVerify(signature)
@@ -2281,19 +2387,13 @@ export class TlsServer {
2281
2387
  const h: i32 = this.hashLength
2282
2388
  using _scope = arena()
2283
2389
  const at: i32 = this.outputHandshakeLength
2390
+ // Where the body starts: `at` is inside the output, so the u32 sum cannot wrap.
2391
+ const body: i32 = toI32(toU32(at) + toU32(4))
2284
2392
  this.transcript.hashInto(this.hashes, TLS_FROM)
2285
2393
  const serverHandshake: u8[] = expose(pool.serverHandshake, tlsHeldClone)
2286
- tlsFinishedVerifyDataInto(
2287
- this.kdf,
2288
- h,
2289
- serverHandshake,
2290
- this.hashes,
2291
- TLS_FROM,
2292
- this.outputHandshake,
2293
- at + 4
2294
- )
2394
+ tlsFinishedVerifyDataInto(this.kdf, h, serverHandshake, this.hashes, TLS_FROM, this.outputHandshake, body)
2295
2395
  secureZero(serverHandshake)
2296
- const finishedEnd: i32 = tlsWriteFinished(this.outputHandshake, at, this.outputHandshake, at + 4, h)
2396
+ const finishedEnd: i32 = tlsWriteFinished(this.outputHandshake, at, this.outputHandshake, body, h)
2297
2397
  this.sent(TLS_LEVEL_HANDSHAKE, at, finishedEnd)
2298
2398
  }
2299
2399
 
@@ -2308,7 +2408,7 @@ export class TlsServer {
2308
2408
  using _scope = arena()
2309
2409
  this.transcript.hashInto(this.hashes, TLS_FROM)
2310
2410
  const handshake: u8[] = expose(pool.handshake, tlsHeldClone)
2311
- const master: u8[] = new Array<u8>(h)
2411
+ const master: u8[] = tlsZeros(h)
2312
2412
  tlsMasterSecretInto(this.kdf, h, handshake, master)
2313
2413
  secureZero(handshake)
2314
2414
  this.deriveInto(master, "c ap traffic", pool.clientApplication)
@@ -2321,7 +2421,8 @@ export class TlsServer {
2321
2421
  // The only client Finished `handleFinished` accepts is the one just
2322
2422
  // computed, so the transcript through it is known now (RFC 8446 §4.6.1),
2323
2423
  // and the resumption master secret is derived while the master secret is.
2324
- const clientFinished: u8[] = new Array<u8>(4 + h)
2424
+ // A hash length and its header: the u32 sum cannot wrap.
2425
+ const clientFinished: u8[] = tlsZeros(toI32(toU32(4) + toU32(h)))
2325
2426
  tlsWriteFinished(clientFinished, TLS_FROM, finished, TLS_FROM, h)
2326
2427
  this.transcript.update(clientFinished, TLS_FROM, toI32(clientFinished.length))
2327
2428
  this.transcript.hashInto(this.hashes, TLS_FROM)
@@ -2399,6 +2500,10 @@ export class TlsServer {
2399
2500
  }
2400
2501
  const b: u8[] = this.ageAddBytes
2401
2502
  crypto.getRandomValues(b)
2503
+ // Made four bytes long by the constructor; the test is the shape the bounds proof reads.
2504
+ if (toI32(b.length) < 4) {
2505
+ return false
2506
+ }
2402
2507
  const ageAdd: u32 = toU32(
2403
2508
  (toI64(b[0]) << toI64(24)) | (toI64(b[1]) << toI64(16)) | (toI64(b[2]) << toI64(8)) | toI64(b[3])
2404
2509
  )
@@ -2446,11 +2551,14 @@ export class TlsServer {
2446
2551
  * level's buffer, goes into the transcript and stays on the output.
2447
2552
  */
2448
2553
  private sent(level: i32, at: i32, end: i32): void {
2554
+ // Both are offsets inside the buffer, `end` past `at`: the u32 difference
2555
+ // is the i32 one, and cannot wrap.
2556
+ const length: i32 = toI32(toU32(end) - toU32(at))
2449
2557
  if (level === TLS_LEVEL_INITIAL) {
2450
- this.transcript.update(this.outputInitial, at, end - at)
2558
+ this.transcript.update(this.outputInitial, at, length)
2451
2559
  this.outputInitialLength = end
2452
2560
  } else {
2453
- this.transcript.update(this.outputHandshake, at, end - at)
2561
+ this.transcript.update(this.outputHandshake, at, length)
2454
2562
  this.outputHandshakeLength = end
2455
2563
  }
2456
2564
  }
@@ -2470,7 +2578,7 @@ export class TlsServer {
2470
2578
  const none: u8[] = []
2471
2579
  switch (level) {
2472
2580
  case TLS_LEVEL_INITIAL: {
2473
- const out: u8[] = new Array<u8>(this.outputInitialLength)
2581
+ const out: u8[] = tlsZeros(this.outputInitialLength)
2474
2582
  for (let k: i32 = 0; k < toI32(out.length) && k < toI32(this.outputInitial.length); k++) {
2475
2583
  out[k] = this.outputInitial[k]
2476
2584
  }
@@ -2478,7 +2586,7 @@ export class TlsServer {
2478
2586
  return out
2479
2587
  }
2480
2588
  case TLS_LEVEL_HANDSHAKE: {
2481
- const out: u8[] = new Array<u8>(this.outputHandshakeLength)
2589
+ const out: u8[] = tlsZeros(this.outputHandshakeLength)
2482
2590
  for (let k: i32 = 0; k < toI32(out.length) && k < toI32(this.outputHandshake.length); k++) {
2483
2591
  out[k] = this.outputHandshake[k]
2484
2592
  }
@@ -2486,7 +2594,7 @@ export class TlsServer {
2486
2594
  return out
2487
2595
  }
2488
2596
  case TLS_LEVEL_APPLICATION: {
2489
- const out: u8[] = new Array<u8>(this.outputApplicationLength)
2597
+ const out: u8[] = tlsZeros(this.outputApplicationLength)
2490
2598
  for (let k: i32 = 0; k < toI32(out.length) && k < toI32(this.outputApplication.length); k++) {
2491
2599
  out[k] = this.outputApplication[k]
2492
2600
  }