@amritk/nish-aarch64-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/crypto/pki.ts CHANGED
@@ -256,28 +256,42 @@ const pkiBytesEqual = (a: u8[], b: u8[]): boolean => {
256
256
  return true
257
257
  }
258
258
 
259
- const pkiHexDigits: string = "0123456789abcdef"
259
+ /** The lower-case hex digit of `v`, 0 to 15, as a one-character string. */
260
+ const pkiHexNibble = (v: i32): string => String.fromCharCode(v < 10 ? 48 + v : 87 + v)
260
261
 
261
- /** `bytes` in lower-case hex, a `Map` key. */
262
+ /**
263
+ * `bytes` in lower-case hex, a `Map` key. Linear: a for-of reads each octet,
264
+ * with no index left to prove, and pushes its two one-character digits, and
265
+ * one `join` copies the `2n` digits out, so `n` octets cost about `2n`
266
+ * allocations and `O(n)` time. Not `String.fromBytes`, whose range check no
267
+ * proof removes yet.
268
+ */
262
269
  const pkiHex = (bytes: u8[]): string => {
263
- const out: u8[] = new Array<u8>(2 * toI32(bytes.length))
264
- for (let i: i32 = 0; i < toI32(bytes.length) && 2 * i + 1 < toI32(out.length); i++) {
265
- const b: i32 = toI32(bytes[i])
266
- out[2 * i] = toU8(toI32(pkiHexDigits.charCodeAt(b >> 4)))
267
- out[2 * i + 1] = toU8(toI32(pkiHexDigits.charCodeAt(b & 15)))
270
+ const out: string[] = []
271
+ for (const b of bytes) {
272
+ out.push(pkiHexNibble(toI32(b) >> 4))
273
+ out.push(pkiHexNibble(toI32(b) & 15))
268
274
  }
269
- return String.fromBytes(out, 0, toI32(out.length))
275
+ return out.join("")
270
276
  }
271
277
 
272
278
  const pkiLower = (c: i32): i32 => (c >= 65 && c <= 90 ? c + 32 : c)
273
279
 
274
280
  /** Whether `a[aAt ..]` and `b[bAt ..]` agree for `n` bytes, ASCII case folded; both ranges must lie inside. */
275
281
  const pkiFoldEqualAt = (a: string, aAt: i32, b: string, bAt: i32, n: i32): boolean => {
276
- if (aAt < 0 || bAt < 0 || n < 0 || aAt + n > toI32(a.length) || bAt + n > toI32(b.length)) {
282
+ if (aAt < 0 || bAt < 0 || n < 0 || n > toI32(a.length) - aAt || n > toI32(b.length) - bAt) {
277
283
  return false
278
284
  }
279
285
  for (let i: i32 = 0; i < n; i++) {
280
- if (pkiLower(toI32(a.charCodeAt(aAt + i))) !== pkiLower(toI32(b.charCodeAt(bAt + i)))) {
286
+ // Unsigned: aAt + i < a.length and bAt + i < b.length, so neither wraps.
287
+ const x: i32 = toI32(toU32(aAt) + toU32(i))
288
+ const y: i32 = toI32(toU32(bAt) + toU32(i))
289
+ // Restates the range checked above for the index proof, which does not
290
+ // carry it through the sum.
291
+ if (x < 0 || y < 0 || x >= toI32(a.length) || y >= toI32(b.length)) {
292
+ return false
293
+ }
294
+ if (pkiLower(toI32(a.charCodeAt(x))) !== pkiLower(toI32(b.charCodeAt(y)))) {
281
295
  return false
282
296
  }
283
297
  }
@@ -285,8 +299,10 @@ const pkiFoldEqualAt = (a: string, aAt: i32, b: string, bAt: i32, n: i32): boole
285
299
  }
286
300
 
287
301
  /** `s` without one trailing dot. */
288
- const pkiStripDot = (s: string): string =>
289
- s.length > 0 && toI32(s.charCodeAt(toI32(s.length) - 1)) === 46 ? s.slice(0, toI32(s.length) - 1) : s
302
+ const pkiStripDot = (s: string): string => {
303
+ const n: i32 = toI32(s.length)
304
+ return n >= 1 && toI32(s.charCodeAt(n - 1)) === 46 ? s.substring(0, n - 1) : s
305
+ }
290
306
 
291
307
  /**
292
308
  * Whether `s` is a DNS name a hostname can be: one to 253 bytes of labels of
@@ -314,10 +330,10 @@ const pkiValidDns = (s: string): boolean => {
314
330
  c === 45 ||
315
331
  c === 95
316
332
  ) {
317
- label++
318
- if (label > 63) {
333
+ if (label >= 63) {
319
334
  return false
320
335
  }
336
+ label++
321
337
  } else {
322
338
  return false
323
339
  }
@@ -329,7 +345,8 @@ const pkiLabelCount = (s: string): i32 => {
329
345
  let count: i32 = 1
330
346
  for (let i: i32 = 0; i < toI32(s.length); i++) {
331
347
  if (toI32(s.charCodeAt(i)) === 46) {
332
- count++
348
+ // Unsigned: count is at most i + 1, below s.length, so it cannot wrap.
349
+ count = toI32(toU32(count) + 1)
333
350
  }
334
351
  }
335
352
  return count
@@ -361,7 +378,8 @@ const pkiParseIpv4 = (s: string, start: i32, end: i32): u8[] | null => {
361
378
  let part: i32 = 0
362
379
  let value: i32 = 0
363
380
  let digits: i32 = 0
364
- for (let i: i32 = start; i <= end; i++) {
381
+ let i: i32 = start
382
+ while (i <= end) {
365
383
  const c: i32 = i < end ? toI32(s.charCodeAt(i)) : 46
366
384
  if (c === 46) {
367
385
  if (digits === 0 || part > 3) {
@@ -373,15 +391,22 @@ const pkiParseIpv4 = (s: string, start: i32, end: i32): u8[] | null => {
373
391
  digits = 0
374
392
  } else {
375
393
  const d: i32 = pkiDigit(c)
376
- if (d < 0 || (digits > 0 && value === 0)) {
394
+ // A fourth digit, or one after 26 or more, passes 255; a leading zero
395
+ // is refused.
396
+ if (d < 0 || digits >= 3 || value > 25 || (digits > 0 && value === 0)) {
377
397
  return null
378
398
  }
379
- value = value * 10 + d
399
+ // Unsigned: value is at most 25 and d a digit, so the sum cannot wrap.
400
+ value = toI32(toU32(value) * 10 + toU32(d))
380
401
  digits++
381
402
  if (value > 255) {
382
403
  return null
383
404
  }
384
405
  }
406
+ if (i >= end) {
407
+ break
408
+ }
409
+ i++
385
410
  }
386
411
  return part === 4 ? out : null
387
412
  }
@@ -412,10 +437,10 @@ const pkiParseIpv6 = (s: string): u8[] | null => {
412
437
  value = 0
413
438
  digits = 0
414
439
  }
415
- colons++
416
- if (colons > 2 || (colons === 2 && gap >= 0)) {
440
+ if (colons >= 2 || (colons === 1 && gap >= 0)) {
417
441
  return null
418
442
  }
443
+ colons++
419
444
  if (colons === 2) {
420
445
  gap = toI32(words.length)
421
446
  }
@@ -431,7 +456,7 @@ const pkiParseIpv6 = (s: string): u8[] | null => {
431
456
  group = i
432
457
  }
433
458
  colons = 0
434
- value = value * 16 + d
459
+ value = (value << 4) | d
435
460
  digits++
436
461
  }
437
462
  }
@@ -440,8 +465,15 @@ const pkiParseIpv6 = (s: string): u8[] | null => {
440
465
  if (quad === null || toI32(words.length) > 6) {
441
466
  return null
442
467
  }
443
- words.push((toI32(quad[0]) << 8) | toI32(quad[1]))
444
- words.push((toI32(quad[2]) << 8) | toI32(quad[3]))
468
+ // pkiParseIpv4 returns four octets or null; restated for the index proof.
469
+ const q: u8[] = quad
470
+ if (toI32(q.length) < 4) {
471
+ return null
472
+ }
473
+ const high: i32 = (toI32(q[0]) << 8) | toI32(q[1])
474
+ const low: i32 = (toI32(q[2]) << 8) | toI32(q[3])
475
+ words.push(high)
476
+ words.push(low)
445
477
  } else if (digits > 0) {
446
478
  if (toI32(words.length) >= 8) {
447
479
  return null
@@ -454,14 +486,28 @@ const pkiParseIpv6 = (s: string): u8[] | null => {
454
486
  if ((gap < 0 && count !== 8) || (gap >= 0 && count > 7)) {
455
487
  return null
456
488
  }
457
- const out: u8[] = new Array<u8>(16)
458
- for (let w: i32 = 0; w < count; w++) {
459
- const at: i32 = gap >= 0 && w >= gap ? w + 8 - count : w
460
- if (at >= 0 && 2 * at + 1 < 16) {
461
- out[2 * at] = toU8(words[w] >> 8)
462
- out[2 * at + 1] = toU8(words[w] & 255)
489
+ // The eight groups: the words, with the `::` gap filled by the zero groups
490
+ // that bring them to eight.
491
+ const groups: i32[] = []
492
+ for (let w: i32 = 0; w < toI32(words.length); w++) {
493
+ const word: i32 = words[w]
494
+ if (w === gap) {
495
+ for (let z: i32 = count; z < 8; z++) {
496
+ groups.push(0)
497
+ }
498
+ }
499
+ groups.push(word)
500
+ }
501
+ if (gap === count) {
502
+ for (let z: i32 = count; z < 8; z++) {
503
+ groups.push(0)
463
504
  }
464
505
  }
506
+ const out: u8[] = []
507
+ for (const g of groups) {
508
+ out.push(toU8(g >> 8))
509
+ out.push(toU8(g & 255))
510
+ }
465
511
  return out
466
512
  }
467
513
 
@@ -472,8 +518,13 @@ const pkiParseIpv6 = (s: string): u8[] | null => {
472
518
  */
473
519
  const pkiParseIp = (host: string): u8[] | null => {
474
520
  const n: i32 = toI32(host.length)
475
- if (n >= 2 && toI32(host.charCodeAt(0)) === 91 && toI32(host.charCodeAt(n - 1)) === 93) {
476
- return pkiParseIpv6(host.slice(1, n - 1))
521
+ if (
522
+ n >= 2 &&
523
+ toI32(host.length) >= 2 &&
524
+ toI32(host.charCodeAt(0)) === 91 &&
525
+ toI32(host.charCodeAt(n - 1)) === 93
526
+ ) {
527
+ return pkiParseIpv6(host.substring(1, n - 1))
477
528
  }
478
529
  if (host.indexOf(":") >= 0) {
479
530
  return pkiParseIpv6(host)
@@ -494,16 +545,18 @@ const pkiParseIp = (host: string): u8[] | null => {
494
545
  const pkiDnsMatch = (san: string, host: string): boolean => {
495
546
  const name: string = pkiStripDot(san)
496
547
  const n: i32 = toI32(name.length)
497
- if (n >= 2 && toI32(name.charCodeAt(0)) === 42 && toI32(name.charCodeAt(1)) === 46) {
498
- const rest: string = name.slice(2, n)
548
+ if (toI32(name.length) >= 2 && toI32(name.charCodeAt(0)) === 42 && toI32(name.charCodeAt(1)) === 46) {
549
+ const rest: string = name.substring(2, n)
499
550
  const dot: i32 = toI32(host.indexOf("."))
500
551
  const restLength: i32 = n - 2
501
552
  return (
502
553
  pkiValidDns(rest) &&
503
554
  pkiLabelCount(rest) >= 2 &&
504
555
  dot > 0 &&
505
- toI32(host.length) - dot - 1 === restLength &&
506
- pkiFoldEqualAt(host, dot + 1, rest, 0, restLength)
556
+ // Unsigned: dot and restLength are below host.length, so the sum cannot
557
+ // wrap.
558
+ toI32(toU32(dot) + toU32(restLength) + 1) === toI32(host.length) &&
559
+ pkiFoldEqualAt(host, toI32(toU32(dot) + 1), rest, 0, restLength)
507
560
  )
508
561
  }
509
562
  return pkiValidDns(name) && n === toI32(host.length) && pkiFoldEqualAt(name, 0, host, 0, n)
@@ -662,11 +715,12 @@ export const pkiVerifySignature = (cert: X509Certificate, issuer: X509Certificat
662
715
 
663
716
  /** `pkiSignatureCode` within the call's budget of signatures. */
664
717
  const pkiCheckSignature = (s: PkiSearch, cert: X509Certificate, issuer: X509Certificate): i32 => {
665
- if (s.signatures >= PKI_MAX_SIGNATURES) {
718
+ const used: i32 = s.signatures
719
+ if (used >= PKI_MAX_SIGNATURES) {
666
720
  s.spent = true
667
721
  return PKI_ERR_BUDGET
668
722
  }
669
- s.signatures = s.signatures + 1
723
+ s.signatures = used + 1
670
724
  return pkiSignatureCode(cert, issuer)
671
725
  }
672
726
 
@@ -683,7 +737,7 @@ const pkiDnsWithin = (name: string, base: string): boolean => {
683
737
  const b: string = pkiStripDot(base)
684
738
  const nb: i32 = toI32(b.length)
685
739
  const nn: i32 = toI32(name.length)
686
- if (nb === 0) {
740
+ if (toI32(b.length) < 1) {
687
741
  return true
688
742
  }
689
743
  if (toI32(b.charCodeAt(0)) === 46) {
@@ -692,12 +746,22 @@ const pkiDnsWithin = (name: string, base: string): boolean => {
692
746
  if (nn === nb) {
693
747
  return pkiFoldEqualAt(name, 0, b, 0, nb)
694
748
  }
695
- return nn > nb && toI32(name.charCodeAt(nn - nb - 1)) === 46 && pkiFoldEqualAt(name, nn - nb, b, 0, nb)
749
+ if (nn <= nb) {
750
+ return false
751
+ }
752
+ // The octet before the base's last nb: nn > nb >= 1, so it lies in name.
753
+ const dot: i32 = nn - nb - 1
754
+ return (
755
+ dot >= 0 &&
756
+ dot < toI32(name.length) &&
757
+ toI32(name.charCodeAt(dot)) === 46 &&
758
+ pkiFoldEqualAt(name, nn - nb, b, 0, nb)
759
+ )
696
760
  }
697
761
 
698
762
  /** `base` as the name its subtree is rooted at: without its leading dot. */
699
763
  const pkiSubtreeRoot = (base: string): string =>
700
- base.length > 0 && toI32(base.charCodeAt(0)) === 46 ? base.slice(1, toI32(base.length)) : base
764
+ toI32(base.length) >= 1 && toI32(base.charCodeAt(0)) === 46 ? base.substring(1, toI32(base.length)) : base
701
765
 
702
766
  /**
703
767
  * Whether `issuer`'s dNSName constraints allow the name `dns`: inside one
@@ -709,7 +773,7 @@ const pkiSubtreeRoot = (base: string): string =>
709
773
  const pkiDnsAllowed = (issuer: X509Certificate, dns: string): boolean => {
710
774
  const name: string = pkiStripDot(dns)
711
775
  const wildcard: boolean = name.startsWith("*.")
712
- const base: string = wildcard ? name.slice(2, toI32(name.length)) : name
776
+ const base: string = wildcard ? name.substring(2, toI32(name.length)) : name
713
777
  let listed: boolean = false
714
778
  let inside: boolean = false
715
779
  for (const p of issuer.ncPermitted) {
@@ -877,16 +941,38 @@ const pkiConstraintsCode = (s: PkiSearch, issuer: X509Certificate): i32 => {
877
941
  return PKI_ERR_NAME_CONSTRAINTS
878
942
  }
879
943
  }
880
- let names: i32 = 1
881
- for (const c of s.path) {
882
- names = names + toI32(c.sanDns.length) + toI32(c.sanIp.length) + 1
944
+ // Each name below the issuer, the host's included, against each subtree.
945
+ // Past PKI_MAX_NAME_CHECKS names, one subtree is already over the budget.
946
+ const subtrees: i64 = toI64(issuer.ncPermitted.length) + toI64(issuer.ncExcluded.length)
947
+ let cost: i64 = 0
948
+ if (subtrees > 0) {
949
+ let names: i64 = 1
950
+ for (const c of s.path) {
951
+ // Unsigned: names is at most PKI_MAX_NAME_CHECKS before the add and each
952
+ // length below 2^32, so the sum cannot wrap.
953
+ names = toI64(toU64(names) + toU64(toI64(c.sanDns.length)) + toU64(toI64(c.sanIp.length)) + 1)
954
+ if (names > toI64(PKI_MAX_NAME_CHECKS)) {
955
+ s.spent = true
956
+ return PKI_ERR_BUDGET
957
+ }
958
+ }
959
+ // Unsigned: names is at most PKI_MAX_NAME_CHECKS and subtrees below 2^33,
960
+ // so the product stays below 2^51.
961
+ cost = toI64(toU64(names) * toU64(subtrees))
962
+ }
963
+ const used: i32 = s.nameChecks
964
+ if (cost < 0 || cost > toI64(PKI_MAX_NAME_CHECKS)) {
965
+ s.spent = true
966
+ return PKI_ERR_BUDGET
883
967
  }
884
- const cost: i32 = names * (toI32(issuer.ncPermitted.length) + toI32(issuer.ncExcluded.length))
885
- if (cost > PKI_MAX_NAME_CHECKS - s.nameChecks) {
968
+ // Unsigned: used and cost are each at most PKI_MAX_NAME_CHECKS, so the sum
969
+ // cannot wrap.
970
+ const total: i64 = toI64(toU64(toI64(used)) + toU64(cost))
971
+ if (total > toI64(PKI_MAX_NAME_CHECKS)) {
886
972
  s.spent = true
887
973
  return PKI_ERR_BUDGET
888
974
  }
889
- s.nameChecks = s.nameChecks + cost
975
+ s.nameChecks = toI32(total)
890
976
  if (s.host.length > 0 && !pkiDnsAllowed(issuer, s.host)) {
891
977
  return PKI_ERR_NAME_CONSTRAINTS
892
978
  }
@@ -966,7 +1052,7 @@ const pkiCriticalitySound = (c: X509Certificate): boolean => !c.akiCritical && (
966
1052
  */
967
1053
  const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certificate, root: boolean): i32 => {
968
1054
  // No intermediate is added past PKI_MAX_PATH - 1, so a root always fits.
969
- if (!root && toI32(s.path.length) + 2 > PKI_MAX_PATH) {
1055
+ if (!root && toI32(s.path.length) > PKI_MAX_PATH - 2) {
970
1056
  return PKI_ERR_CHAIN_TOO_LONG
971
1057
  }
972
1058
  if (pkiIsDistrusted(s.store, cand)) {
@@ -998,7 +1084,8 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
998
1084
  let below: i32 = 0
999
1085
  for (let i: i32 = 1; i < toI32(s.path.length); i++) {
1000
1086
  if (!pkiSelfIssued(s.path[i])) {
1001
- below++
1087
+ // Unsigned: below counts certificates of the path, so it cannot wrap.
1088
+ below = toI32(toU32(below) + 1)
1002
1089
  }
1003
1090
  }
1004
1091
  if (below > cand.bcPathLen) {
@@ -1054,6 +1141,10 @@ const pkiCandidate = (s: PkiSearch, child: X509Certificate, cand: X509Certificat
1054
1141
  */
1055
1142
  const pkiExtend = (s: PkiSearch, child: X509Certificate): boolean => {
1056
1143
  const depth: i32 = toI32(s.path.length)
1144
+ // Unsigned: pkiIssuerCode keeps the path to PKI_MAX_PATH certificates, so
1145
+ // neither position wraps.
1146
+ const above: i32 = toI32(toU32(depth) + 1)
1147
+ const atRoot: i32 = toI32(toU32(PKI_AT_ROOT) + toU32(above))
1057
1148
  if (toI32(child.issuer.length) > 2) {
1058
1149
  const roots: X509Certificate[] | undefined = s.store.bySubject.get(pkiHex(child.issuer))
1059
1150
  if (roots !== undefined) {
@@ -1067,7 +1158,7 @@ const pkiExtend = (s: PkiSearch, child: X509Certificate): boolean => {
1067
1158
  s.path.push(root)
1068
1159
  return true
1069
1160
  }
1070
- pkiNote(s, code, PKI_AT_ROOT + depth + 1)
1161
+ pkiNote(s, code, atRoot)
1071
1162
  }
1072
1163
  }
1073
1164
  }
@@ -1078,7 +1169,7 @@ const pkiExtend = (s: PkiSearch, child: X509Certificate): boolean => {
1078
1169
  return false
1079
1170
  }
1080
1171
  if (code !== PKI_OK) {
1081
- pkiNote(s, code, depth + 1)
1172
+ pkiNote(s, code, above)
1082
1173
  } else {
1083
1174
  s.path.push(cand)
1084
1175
  if (pkiExtend(s, cand)) {
@@ -1087,7 +1178,9 @@ const pkiExtend = (s: PkiSearch, child: X509Certificate): boolean => {
1087
1178
  if (s.spent) {
1088
1179
  return false
1089
1180
  }
1090
- s.path.pop()
1181
+ if (toI32(s.path.length) > 0) {
1182
+ s.path.pop()
1183
+ }
1091
1184
  }
1092
1185
  }
1093
1186
  }
@@ -1122,8 +1215,9 @@ const pkiSerialSound = (c: X509Certificate): boolean => {
1122
1215
  const pkiSanDnsSound = (san: string): boolean => {
1123
1216
  const name: string = pkiStripDot(san)
1124
1217
  const n: i32 = toI32(name.length)
1125
- const wild: boolean = n >= 2 && toI32(name.charCodeAt(0)) === 42 && toI32(name.charCodeAt(1)) === 46
1126
- return name.indexOf("_") < 0 && pkiValidDns(wild ? name.slice(2, n) : name)
1218
+ const wild: boolean =
1219
+ toI32(name.length) >= 2 && toI32(name.charCodeAt(0)) === 42 && toI32(name.charCodeAt(1)) === 46
1220
+ return name.indexOf("_") < 0 && pkiValidDns(wild ? name.substring(2, n) : name)
1127
1221
  }
1128
1222
 
1129
1223
  /**
@@ -1305,28 +1399,38 @@ export const pkiVerifyServer = (
1305
1399
  if (count > PKI_MAX_CHAIN_CERTS) {
1306
1400
  return new PkiResult(PKI_ERR_CHAIN_TOO_LONG, none)
1307
1401
  }
1308
- let bytes: i32 = 0
1402
+ let bytes: i64 = 0
1309
1403
  for (const der of chain) {
1310
- bytes = bytes + toI32(der.length)
1311
- if (bytes > PKI_MAX_CHAIN_BYTES) {
1404
+ // Unsigned: bytes is at most PKI_MAX_CHAIN_BYTES before the add and a
1405
+ // length below 2^32, so the sum cannot wrap.
1406
+ bytes = toI64(toU64(bytes) + toU64(toI64(der.length)))
1407
+ if (bytes > toI64(PKI_MAX_CHAIN_BYTES)) {
1312
1408
  return new PkiResult(PKI_ERR_CHAIN_TOO_LONG, none)
1313
1409
  }
1314
1410
  }
1315
1411
  const certs: X509Certificate[] = []
1412
+ let first: X509Certificate | null = null
1316
1413
  for (const der of chain) {
1317
1414
  const c: X509Certificate | null = x509ParseCertificate(der)
1318
1415
  if (c === null) {
1319
1416
  return new PkiResult(PKI_ERR_MALFORMED, none)
1320
1417
  }
1321
1418
  if (
1322
- toI32(c.sanDns.length) + toI32(c.sanIp.length) > PKI_MAX_NAMES ||
1323
- toI32(c.ncPermitted.length) + toI32(c.ncExcluded.length) > PKI_MAX_SUBTREES
1419
+ toI64(c.sanDns.length) + toI64(c.sanIp.length) > toI64(PKI_MAX_NAMES) ||
1420
+ toI64(c.ncPermitted.length) + toI64(c.ncExcluded.length) > toI64(PKI_MAX_SUBTREES)
1324
1421
  ) {
1325
1422
  return new PkiResult(PKI_ERR_BUDGET, none)
1326
1423
  }
1424
+ if (first === null) {
1425
+ first = c
1426
+ }
1327
1427
  certs.push(c)
1328
1428
  }
1329
- const leaf: X509Certificate = certs[0]
1429
+ // The chain is not empty, so the loop set it; restated for the type.
1430
+ if (first === null) {
1431
+ return new PkiResult(PKI_ERR_MALFORMED, none)
1432
+ }
1433
+ const leaf: X509Certificate = first
1330
1434
  const leafCode: i32 = pkiLeafCode(leaf, hostname, nowMs)
1331
1435
  if (leafCode !== PKI_OK) {
1332
1436
  return new PkiResult(leafCode, none)