@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
@@ -188,8 +188,6 @@ const X509_MAX_EXTENSIONS: i32 = 64
188
188
  /** What a time field reads as when it is not a valid RFC 5280 time: before any year the reader accepts. */
189
189
  const X509_NO_TIME: i64 = -1000000000000
190
190
 
191
- const X509_MS_PER_DAY: i64 = 86400000
192
-
193
191
  // DER tags (X.690 §8, and the context tags RFC 5280 gives them).
194
192
  const X509_TAG_BOOLEAN: i32 = 0x01
195
193
  const X509_TAG_INTEGER: i32 = 0x02
@@ -405,7 +403,9 @@ const x509DerRead = (der: u8[], at: i32, limit: i32): X509DerElement | null => {
405
403
  * so a pass allocates nothing it leaves behind. `into` is unspecified on `false`.
406
404
  */
407
405
  const x509DerReadInto = (der: u8[], at: i32, limit: i32, into: X509DerElement): boolean => {
408
- if (at < 0 || limit > toI32(der.length) || at > limit - 2) {
406
+ // `at < der.length - 1` follows from `at <= limit - 2` and `limit <= der.length`;
407
+ // spelled out because the bounds proof does not chain the two.
408
+ if (at < 0 || limit < 2 || limit > toI32(der.length) || at > limit - 2 || at >= toI32(der.length) - 1) {
409
409
  return false
410
410
  }
411
411
  const tag: i32 = toI32(der[at])
@@ -420,25 +420,33 @@ const x509DerReadInto = (der: u8[], at: i32, limit: i32, into: X509DerElement):
420
420
  if (count < 1 || count > 4 || count > limit - start) {
421
421
  return false
422
422
  }
423
+ // Unsigned: `count <= limit - start`, so the sum cannot wrap.
424
+ const stop: i32 = toI32(toU32(start) + toU32(count))
425
+ // `start < stop <= limit <= der.length`, spelled out for the same reason.
426
+ if (stop > toI32(der.length) || start >= toI32(der.length)) {
427
+ return false
428
+ }
423
429
  if (toI32(der[start]) === 0 || (count === 4 && toI32(der[start]) >= 0x80)) {
424
430
  return false
425
431
  }
426
432
  length = 0
427
- for (let k: i32 = 0; k < count; k++) {
428
- length = (length << 8) | toI32(der[start + k])
433
+ for (let i: i32 = start; i < stop; i++) {
434
+ length = (length << 8) | toI32(der[i])
429
435
  }
430
436
  if (length < 0x80) {
431
437
  return false
432
438
  }
433
- start = start + count
439
+ start = stop
434
440
  }
435
- if (length > limit - start) {
441
+ // Unsigned: `start <= limit` on both paths, so the difference cannot wrap.
442
+ if (toU32(length) > toU32(limit) - toU32(start)) {
436
443
  return false
437
444
  }
438
445
  into.tag = tag
439
446
  into.at = at
440
447
  into.start = start
441
- into.end = start + length
448
+ // Unsigned: `length <= limit - start`, so the sum cannot wrap.
449
+ into.end = toI32(toU32(start) + toU32(length))
442
450
  return true
443
451
  }
444
452
 
@@ -466,14 +474,51 @@ const x509DerPeek = (der: u8[], at: i32, limit: i32): i32 => {
466
474
  return toI32(der[at])
467
475
  }
468
476
 
477
+ /**
478
+ * The number of `e`'s contents octets, or `-1` if `e` does not lie inside
479
+ * `der`. An element `x509DerReadInto` filled always does; the bounds proof
480
+ * cannot see that through its fields, so every caller treats `-1` as malformed.
481
+ */
482
+ const x509DerSize = (der: u8[], e: X509DerElement): i32 => {
483
+ const start: i32 = e.start
484
+ const end: i32 = e.end
485
+ if (start < 0 || end < start || end > toI32(der.length)) {
486
+ return -1
487
+ }
488
+ // Unsigned: `start <= end`, so the difference cannot wrap.
489
+ return toI32(toU32(end) - toU32(start))
490
+ }
491
+
492
+ /**
493
+ * Contents octet `k` of `e`, or `-1` if `e` has no such octet inside `der`:
494
+ * what a caller that has already checked `x509DerSize` reads, so the `-1` is
495
+ * only for an element `x509DerReadInto` could not have filled.
496
+ */
497
+ const x509DerOctet = (der: u8[], e: X509DerElement, k: i32): i32 => {
498
+ const n: i32 = x509DerSize(der, e)
499
+ if (k < 0 || k >= n) {
500
+ return -1
501
+ }
502
+ // Unsigned: `k < end - start`, so the sum cannot wrap.
503
+ const i: i32 = toI32(toU32(e.start) + toU32(k))
504
+ if (i < 0 || i >= toI32(der.length)) {
505
+ return -1
506
+ }
507
+ return toI32(der[i])
508
+ }
509
+
469
510
  /** A copy of `der[from .. to)`. */
470
511
  const x509DerSlice = (der: u8[], from: i32, to: i32): u8[] => {
471
- const out: u8[] = new Array<u8>(to - from)
472
- for (let k: i32 = 0; k < toI32(out.length); k++) {
473
- const i: i32 = from + k
474
- // Always true for the callers' ranges, and what lets the load drop its check.
475
- if (i >= 0 && i < toI32(der.length)) {
476
- out[k] = der[i]
512
+ const out: u8[] = []
513
+ // Pushed, so no length is computed that could be negative. `i < der.length`
514
+ // always holds for the callers' ranges; tested per octet because a push
515
+ // forgets the length the loop bound would have proved.
516
+ if (from < 0) {
517
+ return out
518
+ }
519
+ for (let i: i32 = from; i < to; i++) {
520
+ if (i < toI32(der.length)) {
521
+ out.push(der[i])
477
522
  }
478
523
  }
479
524
  return out
@@ -497,14 +542,22 @@ const x509BytesEqual = (a: u8[], b: u8[]): boolean => {
497
542
 
498
543
  /** Whether `e`'s contents are exactly `expected`, as an OID is recognised by its encoding. */
499
544
  const x509DerContentsAre = (der: u8[], e: X509DerElement, expected: u8[]): boolean => {
500
- const n: i32 = toI32(expected.length)
501
- if (e.end - e.start !== n) {
545
+ const start: i32 = e.start
546
+ const end: i32 = e.end
547
+ if (
548
+ start < 0 ||
549
+ end < start ||
550
+ end > toI32(der.length) ||
551
+ toU32(end) - toU32(start) !== toU32(expected.length)
552
+ ) {
502
553
  return false
503
554
  }
504
- for (let k: i32 = 0; k < n; k++) {
505
- if (der[e.start + k] !== expected[k]) {
555
+ let k: i32 = 0
556
+ for (let i: i32 = start; i < end; i++) {
557
+ if (k >= toI32(expected.length) || der[i] !== expected[k]) {
506
558
  return false
507
559
  }
560
+ k++
508
561
  }
509
562
  return true
510
563
  }
@@ -515,24 +568,25 @@ const x509DerContentsAre = (der: u8[], e: X509DerElement, expected: u8[]): boole
515
568
  * first octet redundant.
516
569
  */
517
570
  const x509DerIntegerMinimal = (der: u8[], e: X509DerElement): boolean => {
518
- const n: i32 = e.end - e.start
571
+ const n: i32 = x509DerSize(der, e)
519
572
  if (n < 1) {
520
573
  return false
521
574
  }
522
575
  if (n === 1) {
523
576
  return true
524
577
  }
525
- const b0: i32 = toI32(der[e.start])
526
- const b1: i32 = toI32(der[e.start + 1])
578
+ const b0: i32 = x509DerOctet(der, e, 0)
579
+ const b1: i32 = x509DerOctet(der, e, 1)
527
580
  return !((b0 === 0 && b1 < 0x80) || (b0 === 0xff && b1 >= 0x80))
528
581
  }
529
582
 
530
583
  /** The value of a one-octet, non-negative INTEGER such as a version, or `-1`. */
531
584
  const x509DerSmallInteger = (der: u8[], e: X509DerElement): i32 => {
532
- if (e.tag !== X509_TAG_INTEGER || e.end - e.start !== 1 || toI32(der[e.start]) >= 0x80) {
585
+ const value: i32 = x509DerOctet(der, e, 0)
586
+ if (e.tag !== X509_TAG_INTEGER || x509DerSize(der, e) !== 1 || value >= 0x80) {
533
587
  return -1
534
588
  }
535
- return toI32(der[e.start])
589
+ return value
536
590
  }
537
591
 
538
592
  /**
@@ -541,15 +595,16 @@ const x509DerSmallInteger = (der: u8[], e: X509DerElement): i32 => {
541
595
  * octet to hold them, and those bits zero (X.690 §8.6.2 and §11.2.1).
542
596
  */
543
597
  const x509DerBitStringSound = (der: u8[], e: X509DerElement): boolean => {
544
- const n: i32 = e.end - e.start
598
+ const n: i32 = x509DerSize(der, e)
545
599
  if (n < 1) {
546
600
  return false
547
601
  }
548
- const unused: i32 = toI32(der[e.start])
602
+ const unused: i32 = x509DerOctet(der, e, 0)
549
603
  if (unused > 7 || (n === 1 && unused !== 0)) {
550
604
  return false
551
605
  }
552
- return (toI32(der[e.end - 1]) & ((1 << unused) - 1)) === 0
606
+ // `~(-1 << unused)` is the low `unused` bits, spelled without a subtraction.
607
+ return (x509DerOctet(der, e, n - 1) & ~(-1 << unused)) === 0
553
608
  }
554
609
 
555
610
  /**
@@ -557,23 +612,31 @@ const x509DerBitStringSound = (der: u8[], e: X509DerElement): boolean => {
557
612
  * signatures are whole octets, so the leading unused-bits count must be 0.
558
613
  */
559
614
  const x509DerBitStringOctets = (der: u8[], e: X509DerElement): u8[] | null => {
560
- if (e.tag !== X509_TAG_BIT_STRING || !x509DerBitStringSound(der, e) || toI32(der[e.start]) !== 0) {
615
+ if (e.tag !== X509_TAG_BIT_STRING || !x509DerBitStringSound(der, e) || x509DerOctet(der, e, 0) !== 0) {
561
616
  return null
562
617
  }
563
- return x509DerSlice(der, e.start + 1, e.end)
618
+ // Unsigned: a sound BIT STRING has an octet at `e.start`, so the sum cannot wrap.
619
+ return x509DerSlice(der, toI32(toU32(e.start) + 1), e.end)
564
620
  }
565
621
 
566
- /** `count` decimal digits at `der[at ..)` as a number, or `-1` if any is not a digit. */
567
- const x509DerDigits = (der: u8[], at: i32, count: i32): i32 => {
568
- let value: i32 = 0
569
- for (let k: i32 = 0; k < count; k++) {
570
- const d: i32 = toI32(der[at + k]) - 48
571
- if (d < 0 || d > 9) {
622
+ /**
623
+ * The decimal digits in contents octets `from` to `to` of `e` as a number, or
624
+ * `-1` if any is not a digit or there are more than four.
625
+ */
626
+ const x509DerDigits = (der: u8[], e: X509DerElement, from: i32, to: i32): i32 => {
627
+ if (to - from > 4) {
628
+ return -1
629
+ }
630
+ // Unsigned: at most four digits, so the value cannot pass 9999.
631
+ let value: u32 = 0
632
+ for (let k: i32 = from; k < to; k++) {
633
+ const c: i32 = x509DerOctet(der, e, k)
634
+ if (c < 48 || c > 57) {
572
635
  return -1
573
636
  }
574
- value = value * 10 + d
637
+ value = value * 10 + toU32(c - 48)
575
638
  }
576
- return value
639
+ return toI32(value)
577
640
  }
578
641
 
579
642
  /** Whether `year` is a leap year of the Gregorian calendar. */
@@ -587,22 +650,6 @@ const x509DaysInMonth = (year: i32, month: i32): i32 => {
587
650
  return month === 4 || month === 6 || month === 9 || month === 11 ? 30 : 31
588
651
  }
589
652
 
590
- /**
591
- * Days from 1970-01-01 to `year`-`month`-`day`: the days of the years between,
592
- * counted by the leap-year rule over whole years before each, and then the
593
- * months of the year itself. `year` is at least 1.
594
- */
595
- const x509DaysFromCivil = (year: i32, month: i32, day: i32): i32 => {
596
- const y: i32 = year - 1
597
- const before: i32 = 365 * y + y / 4 - y / 100 + y / 400
598
- // 1969 * 365 + 492 - 19 + 4: the same count for 1970.
599
- let days: i32 = before - 719162
600
- for (let m: i32 = 1; m < month; m++) {
601
- days = days + x509DaysInMonth(year, m)
602
- }
603
- return days + day - 1
604
- }
605
-
606
653
  /**
607
654
  * A UTCTime or GeneralizedTime in the forms RFC 5280 §4.1.2.5 allows —
608
655
  * `YYMMDDHHMMSSZ` and `YYYYMMDDHHMMSSZ`, in UTC, with seconds and without a
@@ -612,48 +659,66 @@ const x509DaysFromCivil = (year: i32, month: i32, day: i32): i32 => {
612
659
  * through 2049; the fields must name a real date and a time before 24:00:00.
613
660
  */
614
661
  const x509DerTime = (der: u8[], e: X509DerElement): i64 => {
615
- const n: i32 = e.end - e.start
616
- let at: i32 = e.start
662
+ const n: i32 = x509DerSize(der, e)
663
+ // Where the month starts: after a two- or a four-digit year.
664
+ let at: i32 = 0
617
665
  let year: i32 = 0
618
666
  if (e.tag === X509_TAG_UTC_TIME && n === 13) {
619
- year = x509DerDigits(der, at, 2)
667
+ year = x509DerDigits(der, e, 0, 2)
620
668
  if (year >= 0) {
621
- year = year + (year >= 50 ? 1900 : 2000)
669
+ // Unsigned: two digits, so the sum cannot wrap.
670
+ year = toI32(toU32(year) + (year >= 50 ? 1900 : 2000))
622
671
  }
623
- at = at + 2
672
+ at = 2
624
673
  } else if (e.tag === X509_TAG_GENERALIZED_TIME && n === 15) {
625
- year = x509DerDigits(der, at, 4)
626
- at = at + 4
674
+ year = x509DerDigits(der, e, 0, 4)
675
+ at = 4
627
676
  } else {
628
677
  return X509_NO_TIME
629
678
  }
630
- const month: i32 = x509DerDigits(der, at, 2)
631
- const day: i32 = x509DerDigits(der, at + 2, 2)
632
- const hour: i32 = x509DerDigits(der, at + 4, 2)
633
- const minute: i32 = x509DerDigits(der, at + 6, 2)
634
- const second: i32 = x509DerDigits(der, at + 8, 2)
635
- if (toI32(der[at + 10]) !== 90 || year < 1950 || month < 1 || month > 12) {
679
+ const month: i32 = x509DerDigits(der, e, at, at + 2)
680
+ const day: i32 = x509DerDigits(der, e, at + 2, at + 4)
681
+ const hour: i32 = x509DerDigits(der, e, at + 4, at + 6)
682
+ const minute: i32 = x509DerDigits(der, e, at + 6, at + 8)
683
+ const second: i32 = x509DerDigits(der, e, at + 8, at + 10)
684
+ // `year <= 9999` and `day <= 31` follow from four digits and the month's
685
+ // length; spelled out because the range proof does not see either.
686
+ if (x509DerOctet(der, e, at + 10) !== 90 || year < 1950 || year > 9999 || month < 1 || month > 12) {
636
687
  return X509_NO_TIME
637
688
  }
638
- if (day < 1 || day > x509DaysInMonth(year, month) || hour < 0 || hour > 23) {
689
+ if (day < 1 || day > 31 || day > x509DaysInMonth(year, month) || hour < 0 || hour > 23) {
639
690
  return X509_NO_TIME
640
691
  }
641
692
  if (minute < 0 || minute > 59 || second < 0 || second > 59) {
642
693
  return X509_NO_TIME
643
694
  }
695
+ // Days from 1970-01-01: those of the years between, counted by the
696
+ // leap-year rule over whole years before each, then the months of the year
697
+ // itself. 1969 * 365 + 492 - 19 + 4 is the same count for 1970.
698
+ const y: i32 = year - 1
699
+ const years: i32 = 365 * y + y / 4 - y / 100 + y / 400 - 719162
700
+ // Unsigned: at most 365 days, so the sum cannot wrap.
701
+ let yday: u32 = toU32(day - 1)
702
+ for (let m: i32 = 1; m < month; m++) {
703
+ yday = yday + toU32(x509DaysInMonth(year, m))
704
+ }
705
+ // Two's complement: the true sum lies well inside i32 (the year is at most
706
+ // 9999), so the unsigned add gives it exactly, negative or not.
707
+ const days: i32 = toI32(toU32(years) + yday)
644
708
  const seconds: i32 = hour * 3600 + minute * 60 + second
645
- return toI64(x509DaysFromCivil(year, month, day)) * X509_MS_PER_DAY + toI64(seconds) * 1000
709
+ return toI64(days) * 86400000 + toI64(seconds) * 1000
646
710
  }
647
711
 
648
712
  /**
649
- * The OID's contents octets (X.690 §8.19): the first two arcs as one
650
- * subidentifier `40 * a + b`, then each arc in base 128, big-endian, with the
651
- * top bit set on every octet but its last.
713
+ * The OID's contents octets (X.690 §8.19): each subidentifier in base 128,
714
+ * big-endian, with the top bit set on every octet but its last. The caller
715
+ * writes the first two arcs joined into the first subidentifier, `40 * a + b`:
716
+ * 42 is 1.2, 43 is 1.3, 85 is 2.5 and 96 is 2.16.
652
717
  */
653
- const x509DerOidContents = (arcs: i32[]): u8[] => {
718
+ const x509DerOidContents = (subids: i32[]): u8[] => {
654
719
  const out: u8[] = []
655
- for (let i: i32 = 1; i < toI32(arcs.length); i++) {
656
- const arc: i32 = i === 1 ? arcs[0] * 40 + arcs[1] : arcs[i]
720
+ for (let i: i32 = 0; i < toI32(subids.length); i++) {
721
+ const arc: i32 = subids[i]
657
722
  let shift: i32 = 28
658
723
  while (shift > 0 && arc >> shift === 0) {
659
724
  shift = shift - 7
@@ -668,31 +733,31 @@ const x509DerOidContents = (arcs: i32[]): u8[] => {
668
733
  }
669
734
 
670
735
  /** ecdsa-with-SHA256, 1.2.840.10045.4.3.2 (RFC 5758 §3.2). */
671
- const x509OidEcdsaSha256 = (): u8[] => x509DerOidContents([1, 2, 840, 10045, 4, 3, 2])
736
+ const x509OidEcdsaSha256 = (): u8[] => x509DerOidContents([42, 840, 10045, 4, 3, 2])
672
737
 
673
738
  /** id-ecPublicKey, 1.2.840.10045.2.1 (RFC 5480 §2.1.1). */
674
- const x509OidEcPublicKey = (): u8[] => x509DerOidContents([1, 2, 840, 10045, 2, 1])
739
+ const x509OidEcPublicKey = (): u8[] => x509DerOidContents([42, 840, 10045, 2, 1])
675
740
 
676
741
  /** secp256r1, also called prime256v1 and P-256: 1.2.840.10045.3.1.7 (RFC 5480 §2.1.1.1). */
677
- const x509OidPrime256v1 = (): u8[] => x509DerOidContents([1, 2, 840, 10045, 3, 1, 7])
742
+ const x509OidPrime256v1 = (): u8[] => x509DerOidContents([42, 840, 10045, 3, 1, 7])
678
743
 
679
744
  /** id-at-commonName, 2.5.4.3 (RFC 5280 Appendix A). */
680
- const x509OidCommonName = (): u8[] => x509DerOidContents([2, 5, 4, 3])
745
+ const x509OidCommonName = (): u8[] => x509DerOidContents([85, 4, 3])
681
746
 
682
747
  /** ecdsa-with-SHA384, 1.2.840.10045.4.3.3 (RFC 5758 §3.2). */
683
- const x509OidEcdsaSha384 = (): u8[] => x509DerOidContents([1, 2, 840, 10045, 4, 3, 3])
748
+ const x509OidEcdsaSha384 = (): u8[] => x509DerOidContents([42, 840, 10045, 4, 3, 3])
684
749
 
685
750
  /** secp384r1, 1.3.132.0.34 (RFC 5480 §2.1.1.1). */
686
- const x509OidSecp384r1 = (): u8[] => x509DerOidContents([1, 3, 132, 0, 34])
751
+ const x509OidSecp384r1 = (): u8[] => x509DerOidContents([43, 132, 0, 34])
687
752
 
688
753
  /** The PKCS #1 arc 1.2.840.113549.1.1.`n` (RFC 4055 §2.1 and §5): 1 rsaEncryption, 5, 11, 12 and 13 the PKCS#1 v1.5 signatures, 8 MGF1, 10 RSASSA-PSS. */
689
- const x509OidPkcs1 = (n: i32): u8[] => x509DerOidContents([1, 2, 840, 113549, 1, 1, n])
754
+ const x509OidPkcs1 = (n: i32): u8[] => x509DerOidContents([42, 840, 113549, 1, 1, n])
690
755
 
691
756
  /** id-Ed25519, 1.3.101.112 (RFC 8410 §3). */
692
- const x509OidEd25519 = (): u8[] => x509DerOidContents([1, 3, 101, 112])
757
+ const x509OidEd25519 = (): u8[] => x509DerOidContents([43, 101, 112])
693
758
 
694
759
  /** The NIST hash arc 2.16.840.1.101.3.4.2.`n` (RFC 4055 §2.1): 1 SHA-256, 2 SHA-384, 3 SHA-512. */
695
- const x509OidNistHash = (n: i32): u8[] => x509DerOidContents([2, 16, 840, 1, 101, 3, 4, 2, n])
760
+ const x509OidNistHash = (n: i32): u8[] => x509DerOidContents([96, 840, 1, 101, 3, 4, 2, n])
696
761
 
697
762
  // The extensions decoded, by their arc under 2.5.29 (RFC 5280 §4.2.1).
698
763
  const X509_CE_SUBJECT_KEY_ID: i32 = 14
@@ -710,13 +775,13 @@ const X509_EXT_SOUND: i32 = 1
710
775
  const X509_EXT_MALFORMED: i32 = 2
711
776
 
712
777
  /** id-pe-authorityInfoAccess, 1.3.6.1.5.5.7.1.1 (RFC 5280 §4.2.2.1). */
713
- const x509OidAuthorityInfoAccess = (): u8[] => x509DerOidContents([1, 3, 6, 1, 5, 5, 7, 1, 1])
778
+ const x509OidAuthorityInfoAccess = (): u8[] => x509DerOidContents([43, 6, 1, 5, 5, 7, 1, 1])
714
779
 
715
780
  /** id-kp-serverAuth, 1.3.6.1.5.5.7.3.1 (RFC 5280 §4.2.1.12). */
716
- const x509OidServerAuth = (): u8[] => x509DerOidContents([1, 3, 6, 1, 5, 5, 7, 3, 1])
781
+ const x509OidServerAuth = (): u8[] => x509DerOidContents([43, 6, 1, 5, 5, 7, 3, 1])
717
782
 
718
783
  /** anyExtendedKeyUsage, 2.5.29.37.0 (RFC 5280 §4.2.1.12). */
719
- const x509OidAnyEku = (): u8[] => x509DerOidContents([2, 5, 29, 37, 0])
784
+ const x509OidAnyEku = (): u8[] => x509DerOidContents([85, 29, 37, 0])
720
785
 
721
786
  /**
722
787
  * Whether an OID's contents are X.690 §8.19's: at least one octet, the last
@@ -725,19 +790,17 @@ const x509OidAnyEku = (): u8[] => x509DerOidContents([2, 5, 29, 37, 0])
725
790
  * hide behind a second.
726
791
  */
727
792
  const x509DerOidSound = (der: u8[], e: X509DerElement): boolean => {
728
- if (e.tag !== X509_TAG_OID || e.end <= e.start || toI32(der[e.end - 1]) >= 0x80) {
793
+ const n: i32 = x509DerSize(der, e)
794
+ if (e.tag !== X509_TAG_OID || n < 1 || x509DerOctet(der, e, n - 1) >= 0x80) {
729
795
  return false
730
796
  }
731
797
  let first: boolean = true
732
- for (let k: i32 = e.start; k < e.end; k++) {
733
- // Always true for an element `x509DerRead` gave, and what lets the load drop its check.
734
- if (k >= 0 && k < toI32(der.length)) {
735
- const b: i32 = toI32(der[k])
736
- if (first && b === 0x80) {
737
- return false
738
- }
739
- first = b < 0x80
798
+ for (let k: i32 = 0; k < n; k++) {
799
+ const b: i32 = x509DerOctet(der, e, k)
800
+ if (first && b === 0x80) {
801
+ return false
740
802
  }
803
+ first = b < 0x80
741
804
  }
742
805
  return true
743
806
  }
@@ -924,26 +987,31 @@ const x509KeyType = (der: u8[], alg: X509DerElement): i32 => {
924
987
  }
925
988
 
926
989
  /**
927
- * A minimal, non-negative INTEGER right-aligned into `out[off .. off +
928
- * width)`, or `false` when it is negative, not minimal or wider than `width`
990
+ * A minimal, non-negative INTEGER right-aligned in a fresh array of `width`
991
+ * octets, or `null` when it is negative, not minimal or wider than `width`
929
992
  * octets once a sign octet is set aside.
930
993
  */
931
- const x509DerUnsignedInto = (der: u8[], e: X509DerElement, out: u8[], off: i32, width: i32): boolean => {
932
- if (!x509DerIntegerMinimal(der, e) || toI32(der[e.start]) >= 0x80) {
933
- return false
994
+ const x509DerUnsignedPadded = (der: u8[], e: X509DerElement, width: i32): u8[] | null => {
995
+ const size: i32 = x509DerSize(der, e)
996
+ if (size < 1 || width < 0 || !x509DerIntegerMinimal(der, e) || x509DerOctet(der, e, 0) >= 0x80) {
997
+ return null
934
998
  }
935
- let start: i32 = e.start
936
- if (toI32(der[start]) === 0 && e.end - start > 1) {
937
- start = start + 1
999
+ let from: i32 = 0
1000
+ if (size > 1 && x509DerOctet(der, e, 0) === 0) {
1001
+ from = 1
938
1002
  }
939
- const n: i32 = e.end - start
1003
+ const n: i32 = size - from
940
1004
  if (n > width) {
941
- return false
1005
+ return null
942
1006
  }
943
- for (let k: i32 = 0; k < n; k++) {
944
- out[off + width - n + k] = der[start + k]
1007
+ const out: u8[] = []
1008
+ for (let k: i32 = n; k < width; k++) {
1009
+ out.push(0)
945
1010
  }
946
- return true
1011
+ for (let k: i32 = from; k < size; k++) {
1012
+ out.push(toU8(x509DerOctet(der, e, k)))
1013
+ }
1014
+ return out
947
1015
  }
948
1016
 
949
1017
  /**
@@ -975,10 +1043,14 @@ export const x509DerSignatureRSWidth = (sig: u8[], width: i32): u8[] | null => {
975
1043
  if (s === null || s.end !== seq.end) {
976
1044
  return null
977
1045
  }
978
- const out: u8[] = new Array<u8>(2 * width)
979
- if (!x509DerUnsignedInto(sig, r, out, 0, width) || !x509DerUnsignedInto(sig, s, out, width, width)) {
1046
+ const out: u8[] | null = x509DerUnsignedPadded(sig, r, width)
1047
+ const sOctets: u8[] | null = x509DerUnsignedPadded(sig, s, width)
1048
+ if (out === null || sOctets === null) {
980
1049
  return null
981
1050
  }
1051
+ for (const b of sOctets) {
1052
+ out.push(b)
1053
+ }
982
1054
  return out
983
1055
  }
984
1056
 
@@ -1002,14 +1074,25 @@ const x509DerTlv = (tag: i32, contents: u8[]): u8[] => {
1002
1074
  if (n < 0x80) {
1003
1075
  out.push(toU8(n))
1004
1076
  } else {
1005
- let count: i32 = 0
1006
- for (let v: i32 = n; v > 0; v = v >> 8) {
1007
- count = count + 1
1077
+ let count: i32 = 1
1078
+ if (n > 0xffffff) {
1079
+ count = 4
1080
+ } else if (n > 0xffff) {
1081
+ count = 3
1082
+ } else if (n > 0xff) {
1083
+ count = 2
1008
1084
  }
1009
1085
  out.push(toU8(0x80 | count))
1010
- for (let k: i32 = count - 1; k >= 0; k--) {
1011
- out.push(toU8((n >> (8 * k)) & 0xff))
1086
+ if (count >= 4) {
1087
+ out.push(toU8((n >> 24) & 0xff))
1088
+ }
1089
+ if (count >= 3) {
1090
+ out.push(toU8((n >> 16) & 0xff))
1012
1091
  }
1092
+ if (count >= 2) {
1093
+ out.push(toU8((n >> 8) & 0xff))
1094
+ }
1095
+ out.push(toU8(n & 0xff))
1013
1096
  }
1014
1097
  for (let k: i32 = 0; k < toI32(contents.length); k++) {
1015
1098
  out.push(contents[k])
@@ -1063,16 +1146,14 @@ const x509DerBitString = (octets: u8[]): u8[] => {
1063
1146
  return x509DerTlv(X509_TAG_BIT_STRING, contents)
1064
1147
  }
1065
1148
 
1066
- /** `value` as `width` decimal digits, pushed onto `out`. */
1067
- const x509PushDigits = (out: u8[], value: i32, width: i32): void => {
1068
- let scale: i32 = 1
1069
- for (let k: i32 = 1; k < width; k++) {
1070
- scale = scale * 10
1071
- }
1072
- for (let k: i32 = 0; k < width; k++) {
1073
- out.push(toU8(((value / scale) % 10) + 48))
1074
- scale = scale / 10
1149
+ /** `value`'s last two decimal digits, or its last four when `wide`, pushed onto `out`. */
1150
+ const x509PushDigits = (out: u8[], value: i32, wide: boolean): void => {
1151
+ if (wide) {
1152
+ out.push(toU8(((value / 1000) % 10) + 48))
1153
+ out.push(toU8(((value / 100) % 10) + 48))
1075
1154
  }
1155
+ out.push(toU8(((value / 10) % 10) + 48))
1156
+ out.push(toU8((value % 10) + 48))
1076
1157
  }
1077
1158
 
1078
1159
  /**
@@ -1084,23 +1165,34 @@ const x509DerTimeAt = (seconds: i64): u8[] => {
1084
1165
  let days: i32 = toI32(seconds / 86400)
1085
1166
  const rest: i32 = toI32(seconds % 86400)
1086
1167
  let year: i32 = 1970
1087
- while (days >= (x509IsLeapYear(year) ? 366 : 365)) {
1088
- days = days - (x509IsLeapYear(year) ? 366 : 365)
1089
- year = year + 1
1168
+ while (year < 9999) {
1169
+ const length: i32 = x509IsLeapYear(year) ? 366 : 365
1170
+ if (days < length) {
1171
+ break
1172
+ }
1173
+ // Unsigned: `days` is at least `length` here, so the difference cannot wrap.
1174
+ days = toI32(toU32(days) - toU32(length))
1175
+ year++
1090
1176
  }
1091
1177
  let month: i32 = 1
1092
- while (days >= x509DaysInMonth(year, month)) {
1093
- days = days - x509DaysInMonth(year, month)
1094
- month = month + 1
1178
+ while (month < 12) {
1179
+ const length: i32 = x509DaysInMonth(year, month)
1180
+ if (days < length) {
1181
+ break
1182
+ }
1183
+ // Unsigned: `days` is at least `length` here, so the difference cannot wrap.
1184
+ days = toI32(toU32(days) - toU32(length))
1185
+ month++
1095
1186
  }
1096
1187
  const text: u8[] = []
1097
1188
  const utc: boolean = year < 2050
1098
- x509PushDigits(text, utc ? year % 100 : year, utc ? 2 : 4)
1099
- x509PushDigits(text, month, 2)
1100
- x509PushDigits(text, days + 1, 2)
1101
- x509PushDigits(text, rest / 3600, 2)
1102
- x509PushDigits(text, (rest / 60) % 60, 2)
1103
- x509PushDigits(text, rest % 60, 2)
1189
+ x509PushDigits(text, year, !utc)
1190
+ x509PushDigits(text, month, false)
1191
+ // Unsigned: `days` is below its month's length here, so the sum cannot wrap.
1192
+ x509PushDigits(text, toI32(toU32(days) + 1), false)
1193
+ x509PushDigits(text, rest / 3600, false)
1194
+ x509PushDigits(text, (rest / 60) % 60, false)
1195
+ x509PushDigits(text, rest % 60, false)
1104
1196
  text.push(90)
1105
1197
  return x509DerTlv(utc ? X509_TAG_UTC_TIME : X509_TAG_GENERALIZED_TIME, text)
1106
1198
  }
@@ -1187,8 +1279,21 @@ export const derToPem = (der: u8[], label: string): string => {
1187
1279
  const body: string = base64StdPadEncode(der)
1188
1280
  const n: i32 = toI32(body.length)
1189
1281
  const lines: string[] = [`-----BEGIN ${label}-----`]
1190
- for (let at: i32 = 0; at < n; at = at + 64) {
1191
- lines.push(body.substring(at, at + 64 < n ? at + 64 : n))
1282
+ let at: i32 = 0
1283
+ while (at < n) {
1284
+ // `n` is a length, so `n - 64` cannot wrap, and `at + 64` is then below `n`.
1285
+ const end: i32 = at < n - 64 ? at + 64 : n
1286
+ // `at` is always in `body`, said again for the bounds proof; a line that
1287
+ // stops short of the end is cut below the length, the last one runs to it.
1288
+ if (at < 0 || at >= n) {
1289
+ break
1290
+ }
1291
+ if (end >= 0 && end < n) {
1292
+ lines.push(body.substring(at, end))
1293
+ } else {
1294
+ lines.push(body.substring(at))
1295
+ }
1296
+ at = end
1192
1297
  }
1193
1298
  lines.push(`-----END ${label}-----`)
1194
1299
  lines.push("")
@@ -1221,11 +1326,24 @@ export const pemToDer = (pem: string, label: string): u8[][] | null => {
1221
1326
  while (lineEnd >= 0 && lineEnd < toI32(pem.length) && toI32(pem.charCodeAt(lineEnd)) !== 10) {
1222
1327
  lineEnd = lineEnd + 1
1223
1328
  }
1224
- const next: i32 = lineEnd + 1
1225
- if (lineEnd > lineStart && toI32(pem.charCodeAt(lineEnd - 1)) === 13) {
1226
- lineEnd = lineEnd - 1
1329
+ // Unsigned: `lineEnd` is at most the text's length, so the sum cannot wrap.
1330
+ const next: i32 = toI32(toU32(lineEnd) + 1)
1331
+ // `lineEnd` is in the text, said again for the bounds proof.
1332
+ if (
1333
+ lineEnd > lineStart &&
1334
+ lineEnd >= 1 &&
1335
+ lineEnd <= toI32(pem.length) &&
1336
+ toI32(pem.charCodeAt(lineEnd - 1)) === 13
1337
+ ) {
1338
+ lineEnd--
1339
+ }
1340
+ // Always false (`lineStart` < `n`, the CR step stops at it); said again for the bounds proof.
1341
+ if (lineStart < 0 || lineStart > n || lineEnd < lineStart || lineEnd > n) {
1342
+ return null
1227
1343
  }
1228
- const line: string = pem.slice(lineStart, lineEnd)
1344
+ // A line that stops short of the end is cut below the length; the last one runs to it.
1345
+ const line: string =
1346
+ lineEnd >= 0 && lineEnd < n ? pem.substring(lineStart, lineEnd) : pem.substring(lineStart)
1229
1347
  lineStart = next
1230
1348
  const length: i32 = toI32(line.length)
1231
1349
  if (inside === "") {
@@ -1289,7 +1407,7 @@ const x509Sec1Key = (der: u8[], start: i32, end: i32, needCurve: boolean): Secre
1289
1407
  return null
1290
1408
  }
1291
1409
  const key: X509DerElement | null = x509DerExpect(der, version.end, seq.end, X509_TAG_OCTET_STRING)
1292
- if (key === null || key.end - key.start !== 32) {
1410
+ if (key === null || x509DerSize(der, key) !== 32) {
1293
1411
  return null
1294
1412
  }
1295
1413
  let at: i32 = key.end
@@ -1412,6 +1530,14 @@ const x509WipeBlocks = (blocks: u8[][] | null): void => {
1412
1530
  }
1413
1531
  }
1414
1532
 
1533
+ /** The first of `blocks`, or an empty array when there is none. */
1534
+ const x509FirstBlock = (blocks: u8[][]): u8[] => {
1535
+ for (const block of blocks) {
1536
+ return block
1537
+ }
1538
+ return []
1539
+ }
1540
+
1415
1541
  /**
1416
1542
  * The 32-byte P-256 private key in `pem`, as the `Secret` it is: one `EC
1417
1543
  * PRIVATE KEY` block (SEC1, RFC 5915, which must name its curve) or one
@@ -1431,10 +1557,10 @@ export const x509ParseP256PrivateKey = (pem: string): Secret<u8[]> | null => {
1431
1557
  const pkcs8: u8[][] | null = pemToDer(pem, "PRIVATE KEY")
1432
1558
  let key: Secret<u8[]> | null = null
1433
1559
  if (sec1 !== null && pkcs8 === null && toI32(sec1.length) === 1) {
1434
- const der: u8[] = sec1[0]
1560
+ const der: u8[] = x509FirstBlock(sec1)
1435
1561
  key = x509Sec1Key(der, 0, toI32(der.length), true)
1436
1562
  } else if (pkcs8 !== null && sec1 === null && toI32(pkcs8.length) === 1) {
1437
- key = x509Pkcs8Key(pkcs8[0])
1563
+ key = x509Pkcs8Key(x509FirstBlock(pkcs8))
1438
1564
  }
1439
1565
  // X509-7: the decoded DER holds the scalar too, whichever block it was.
1440
1566
  x509WipeBlocks(sec1)
@@ -1551,8 +1677,22 @@ const x509DerIa5Name = (der: u8[], e: X509DerElement): boolean => {
1551
1677
  return true
1552
1678
  }
1553
1679
 
1554
- /** `der[e.start .. e.end)` as a string, one character per octet, as `x509Bytes` reads one back. */
1555
- const x509DerText = (der: u8[], e: X509DerElement): string => String.fromBytes(der, e.start, e.end)
1680
+ /**
1681
+ * `der[e.start .. e.end)` as a string, one character per octet, as `x509Bytes`
1682
+ * reads one back, or `null` when the range is not in `der`, which the caller
1683
+ * refuses as malformed. Never `""` for a bad range: an empty dNSName base in a
1684
+ * name constraint permits every name, so an invented `""` would fail open.
1685
+ */
1686
+ const x509DerText = (der: u8[], e: X509DerElement): string | null => {
1687
+ const start: i32 = e.start
1688
+ const end: i32 = e.end
1689
+ // Always in the DER for an element `x509DerRead` gave, said again so the
1690
+ // copy cannot panic: no proof removes `String.fromBytes`'s own range check yet.
1691
+ if (start < 0 || end < start || end > toI32(der.length)) {
1692
+ return null
1693
+ }
1694
+ return String.fromBytes(der, start, end)
1695
+ }
1556
1696
 
1557
1697
  /**
1558
1698
  * The form of one GeneralName (RFC 5280 §4.2.1.6), its IMPLICIT tag checked
@@ -1616,13 +1756,18 @@ const x509ReadSubjectAltName = (der: u8[], start: i32, end: i32, out: X509Certif
1616
1756
  return false
1617
1757
  }
1618
1758
  const kind: i32 = x509GeneralNameKind(der, name)
1619
- const length: i32 = name.end - name.start
1759
+ const length: i32 = x509DerSize(der, name)
1620
1760
  if (kind === X509_GN_DNS && length > 0) {
1621
- out.sanDns.push(x509DerText(der, name))
1761
+ const dns: string | null = x509DerText(der, name)
1762
+ if (dns === null) {
1763
+ return false
1764
+ }
1765
+ out.sanDns.push(dns)
1622
1766
  } else if (kind === X509_GN_IP && (length === 4 || length === 16)) {
1623
1767
  out.sanIp.push(x509DerSlice(der, name.start, name.end))
1624
1768
  } else if (kind === X509_GN_DIR || kind === X509_GN_OTHER) {
1625
- out.sanOther = out.sanOther + 1
1769
+ // Unsigned: one per name read, fewer than the DER's octets, so the sum cannot wrap.
1770
+ out.sanOther = toI32(toU32(out.sanOther) + 1)
1626
1771
  } else {
1627
1772
  return false
1628
1773
  }
@@ -1637,23 +1782,25 @@ const x509ReadSubjectAltName = (der: u8[], start: i32, end: i32, out: X509Certif
1637
1782
  * pathLenConstraint, which RFC 5280 §4.2.1.9 makes `INTEGER (0..MAX)`.
1638
1783
  */
1639
1784
  const x509DerNonNegative = (der: u8[], e: X509DerElement): i32 => {
1640
- const n: i32 = e.end - e.start
1641
- if (e.tag !== X509_TAG_INTEGER || !x509DerIntegerMinimal(der, e) || toI32(der[e.start]) >= 0x80 || n > 4) {
1785
+ const n: i32 = x509DerSize(der, e)
1786
+ if (
1787
+ e.tag !== X509_TAG_INTEGER ||
1788
+ !x509DerIntegerMinimal(der, e) ||
1789
+ x509DerOctet(der, e, 0) >= 0x80 ||
1790
+ n > 4
1791
+ ) {
1642
1792
  return -1
1643
1793
  }
1644
1794
  let value: i32 = 0
1645
- for (let k: i32 = e.start; k < e.end; k++) {
1646
- // Always true for an element `x509DerRead` gave, and what lets the load drop its check.
1647
- if (k >= 0 && k < toI32(der.length)) {
1648
- value = (value << 8) | toI32(der[k])
1649
- }
1795
+ for (let k: i32 = 0; k < n; k++) {
1796
+ value = (value << 8) | x509DerOctet(der, e, k)
1650
1797
  }
1651
1798
  return value
1652
1799
  }
1653
1800
 
1654
1801
  /** Whether `e` is DER's TRUE, `01 01 ff`: a DEFAULT FALSE is left out (X.690 §11.5), so a present BOOLEAN is TRUE. */
1655
1802
  const x509DerIsTrue = (der: u8[], e: X509DerElement): boolean =>
1656
- e.tag === X509_TAG_BOOLEAN && e.end - e.start === 1 && toI32(der[e.start]) === 0xff
1803
+ e.tag === X509_TAG_BOOLEAN && x509DerSize(der, e) === 1 && x509DerOctet(der, e, 0) === 0xff
1657
1804
 
1658
1805
  /**
1659
1806
  * basicConstraints (RFC 5280 §4.2.1.9), `SEQUENCE { cA BOOLEAN DEFAULT FALSE,
@@ -1702,17 +1849,18 @@ const x509ReadKeyUsage = (der: u8[], start: i32, end: i32, out: X509Certificate)
1702
1849
  }
1703
1850
  // An empty BIT STRING (n = 0) needs no test of its own: its unused-bits
1704
1851
  // count is held to 0, so the bit check below finds no bit set.
1705
- const n: i32 = bits.end - bits.start - 1
1706
- if (n > 2) {
1852
+ const size: i32 = x509DerSize(der, bits)
1853
+ if (size < 1 || size > 3) {
1707
1854
  return false
1708
1855
  }
1709
- const unused: i32 = toI32(der[bits.start])
1710
- if (((toI32(der[bits.end - 1]) >> unused) & 1) !== 1) {
1856
+ const n: i32 = size - 1
1857
+ const unused: i32 = x509DerOctet(der, bits, 0)
1858
+ if (((x509DerOctet(der, bits, n) >> unused) & 1) !== 1) {
1711
1859
  return false
1712
1860
  }
1713
1861
  let mask: i32 = 0
1714
1862
  for (let j: i32 = 0; j < n; j++) {
1715
- const octet: i32 = toI32(der[bits.start + 1 + j])
1863
+ const octet: i32 = x509DerOctet(der, bits, 1 + j)
1716
1864
  for (let b: i32 = 0; b < 8; b++) {
1717
1865
  if (((octet >> (7 - b)) & 1) === 1) {
1718
1866
  const bit: i32 = 8 * j + b
@@ -1775,7 +1923,7 @@ const x509MaskContiguous = (mask: u8[]): boolean => {
1775
1923
 
1776
1924
  /** Whether a GeneralSubtree's IMPLICIT `BaseDistance` bound is a minimal, non-negative INTEGER. */
1777
1925
  const x509DerBoundSound = (der: u8[], e: X509DerElement): boolean =>
1778
- x509DerIntegerMinimal(der, e) && toI32(der[e.start]) < 0x80
1926
+ x509DerIntegerMinimal(der, e) && x509DerOctet(der, e, 0) < 0x80
1779
1927
 
1780
1928
  /**
1781
1929
  * The GeneralSubtrees filling an IMPLICIT `[0]` or `[1]` of nameConstraints
@@ -1812,15 +1960,21 @@ const x509ReadSubtrees = (
1812
1960
  }
1813
1961
  const subtree = new X509GeneralSubtree()
1814
1962
  subtree.kind = x509GeneralNameKind(der, base)
1815
- const length: i32 = base.end - base.start
1963
+ const length: i32 = x509DerSize(der, base)
1816
1964
  if (subtree.kind === X509_GN_DNS) {
1817
- subtree.dns = x509DerText(der, base)
1965
+ const dns: string | null = x509DerText(der, base)
1966
+ if (dns === null) {
1967
+ return false
1968
+ }
1969
+ subtree.dns = dns
1818
1970
  } else if (subtree.kind === X509_GN_IP) {
1819
1971
  if (length !== 8 && length !== 32) {
1820
1972
  return false
1821
1973
  }
1822
- subtree.ip = x509DerSlice(der, base.start, base.start + length / 2)
1823
- subtree.mask = x509DerSlice(der, base.start + length / 2, base.end)
1974
+ // Unsigned: the half is in the element, so the sum cannot wrap.
1975
+ const half: i32 = toI32(toU32(base.start) + toU32(length / 2))
1976
+ subtree.ip = x509DerSlice(der, base.start, half)
1977
+ subtree.mask = x509DerSlice(der, half, base.end)
1824
1978
  if (!x509MaskContiguous(subtree.mask)) {
1825
1979
  return false
1826
1980
  }
@@ -1836,7 +1990,7 @@ const x509ReadSubtrees = (
1836
1990
  if (
1837
1991
  !x509DerReadInto(der, bound, tree.end, minimum) ||
1838
1992
  !x509DerBoundSound(der, minimum) ||
1839
- (minimum.end - minimum.start === 1 && toI32(der[minimum.start]) === 0)
1993
+ (x509DerSize(der, minimum) === 1 && x509DerOctet(der, minimum, 0) === 0)
1840
1994
  ) {
1841
1995
  return false
1842
1996
  }
@@ -2041,10 +2195,10 @@ const x509ReadExtension = (der: u8[], ce: i32, start: i32, end: i32, out: X509Ce
2041
2195
 
2042
2196
  /** The arc under 2.5.29 an extnID names, 1 to 127 (one octet), or -1 for an OID outside it. */
2043
2197
  const x509ExtensionArc = (der: u8[], id: X509DerElement): i32 => {
2044
- if (id.end - id.start !== 3 || toI32(der[id.start]) !== 0x55 || toI32(der[id.start + 1]) !== 0x1d) {
2198
+ if (x509DerSize(der, id) !== 3 || x509DerOctet(der, id, 0) !== 0x55 || x509DerOctet(der, id, 1) !== 0x1d) {
2045
2199
  return -1
2046
2200
  }
2047
- const arc: i32 = toI32(der[id.start + 2])
2201
+ const arc: i32 = x509DerOctet(der, id, 2)
2048
2202
  return arc < 0x80 ? arc : -1
2049
2203
  }
2050
2204
 
@@ -2405,7 +2559,13 @@ export const x509MintSelfSigned = (
2405
2559
  return null
2406
2560
  }
2407
2561
  const start: i64 = notBeforeMs / 1000
2408
- const end: i64 = start + toI64(days) * 86400
2562
+ // 9999-12-31T23:59:59Z, the last second RFC 5280's times can spell: a
2563
+ // start past it is refused before the end is added to it.
2564
+ if (start < 0 || start > 253402300799) {
2565
+ return null
2566
+ }
2567
+ // Unsigned: `start` is below 2^38 and `days` at most `X509_MAX_DAYS`, so the sum cannot wrap.
2568
+ const end: i64 = toI64(toU64(start) + toU64(days) * 86400)
2409
2569
  // 9999-12-31T23:59:59Z, the last second RFC 5280's times can spell.
2410
2570
  if (end > 253402300799) {
2411
2571
  return null