@amritk/nish-aarch64-linux 0.49.1 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/std/crypto/jwt.ts CHANGED
@@ -86,14 +86,7 @@
86
86
  * functions share the importing program's flat symbol namespace
87
87
  * (`docs/wp26-stdlib.md` §3e).
88
88
  */
89
- import {
90
- HMAC_SHA256,
91
- HMAC_SHA384,
92
- HMAC_SHA512,
93
- HmacKey,
94
- hmacKeyedInto,
95
- hmacKeyedVerify,
96
- } from "nish/crypto/hmac"
89
+ import { HmacHash, HmacKey, hmacKeyedInto, hmacKeyedVerify } from "nish/crypto/hmac"
97
90
  import {
98
91
  RSA_HASH_SHA256,
99
92
  RSA_HASH_SHA384,
@@ -239,9 +232,14 @@ const jwtSignatureSize = (key: JwtKey): i32 => {
239
232
  }
240
233
 
241
234
  const jwtCopy = (a: u8[]): u8[] => {
242
- const out: u8[] = []
243
- for (let k: i32 = 0; k < toI32(a.length); k += 1) {
244
- out.push(a[k])
235
+ const n: i32 = toI32(a.length)
236
+ if (n <= 0) {
237
+ return []
238
+ }
239
+ const out: u8[] = new Array<u8>(n)
240
+ // Always true; the prover does not carry `out.length === n` from `new Array`.
241
+ if (toI32(a.length) <= toI32(out.length)) {
242
+ out.set(a, 0)
245
243
  }
246
244
  return out
247
245
  }
@@ -342,18 +340,15 @@ const jwtPublicKey = (
342
340
  * @returns A new key that verifies and signs, or `null`.
343
341
  */
344
342
  export const jwtHmacKey = (alg: i32, key: Secret<u8[]>): JwtKey | null => {
345
- let hash: i32 = 0
346
- let size: i32 = 0
347
- if (alg === JWT_ALG_HS256) {
348
- hash = HMAC_SHA256
349
- size = 32
350
- } else if (alg === JWT_ALG_HS384) {
351
- hash = HMAC_SHA384
343
+ let hash: HmacHash = HmacHash.Sha256
344
+ let size: i32 = 32
345
+ if (alg === JWT_ALG_HS384) {
346
+ hash = HmacHash.Sha384
352
347
  size = 48
353
348
  } else if (alg === JWT_ALG_HS512) {
354
- hash = HMAC_SHA512
349
+ hash = HmacHash.Sha512
355
350
  size = 64
356
- } else {
351
+ } else if (alg !== JWT_ALG_HS256) {
357
352
  return null
358
353
  }
359
354
  if (expose(key, jwtLength) < size) {
@@ -662,7 +657,7 @@ const jwtHexValue = (b: i32): i32 => {
662
657
  }
663
658
 
664
659
  /** How many bytes code point `cp` takes in UTF-8. */
665
- const jwtUtf8Width = (cp: i32): i32 => {
660
+ const jwtUtf8Width = (cp: i32): integer<1, 4> => {
666
661
  if (cp < 0x80) {
667
662
  return 1
668
663
  }
@@ -676,7 +671,7 @@ const jwtUtf8Width = (cp: i32): i32 => {
676
671
  }
677
672
 
678
673
  /** Byte `k` of the `width`-byte UTF-8 form of `cp`. */
679
- const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
674
+ const jwtUtf8Byte = (cp: i32, width: integer<1, 4>, k: integer<0, 3>): i32 => {
680
675
  if (width === 1) {
681
676
  return cp
682
677
  }
@@ -686,8 +681,8 @@ const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
686
681
  } else if (width === 3) {
687
682
  lead = 0xe0
688
683
  }
689
- // Unsigned: `k` is below `width`, so the count cannot wrap.
690
- const shift: i32 = toI32((toU32(width) - toU32(k) - toU32(1)) * toU32(6))
684
+ // `k` is below `width`, so the shift is a whole number of six-bit groups.
685
+ const shift: i32 = (width - k - 1) * 6
691
686
  const tail: i32 = 0x80
692
687
  return k === 0 ? lead | (cp >> shift) : tail | ((cp >> shift) & 0x3f)
693
688
  }
@@ -1049,9 +1044,9 @@ class JwtJson {
1049
1044
  if (lo < 0xdc00 || lo > 0xdfff) {
1050
1045
  return -1
1051
1046
  }
1052
- // Unsigned: each surrogate's low ten bits, so the sum stays below 0x110000.
1053
- const plane: u32 = toU32(0x10000)
1054
- return toI32(plane + (toU32(hi & 0x3ff) << toU32(10)) + toU32(lo & 0x3ff))
1047
+ // Each surrogate's low ten bits, so the sum stays below 0x110000.
1048
+ const high: i32 = (hi & 0x3ff) * 1024
1049
+ return high + (lo & 0x3ff) + 0x10000
1055
1050
  }
1056
1051
 
1057
1052
  /** The four hex digits at `i` as a number, or `-1`. */
@@ -1189,7 +1184,7 @@ class JwtJson {
1189
1184
  continue
1190
1185
  }
1191
1186
  const cp: i32 = this.escapeCodePoint(i)
1192
- const width: i32 = jwtUtf8Width(cp)
1187
+ const width: integer<1, 4> = jwtUtf8Width(cp)
1193
1188
  for (let k: i32 = 0; k < width; k += 1) {
1194
1189
  if (o >= n) {
1195
1190
  return false
@@ -1228,7 +1223,7 @@ class JwtJson {
1228
1223
  continue
1229
1224
  }
1230
1225
  const cp: i32 = this.escapeCodePoint(i)
1231
- const width: i32 = jwtUtf8Width(cp)
1226
+ const width: integer<1, 4> = jwtUtf8Width(cp)
1232
1227
  for (let k: i32 = 0; k < width; k += 1) {
1233
1228
  if (o >= 0 && o < toI32(out.length)) {
1234
1229
  out[o] = toU8(jwtUtf8Byte(cp, width, k))
@@ -1253,9 +1248,18 @@ class JwtJson {
1253
1248
  if (!this.isInteger()) {
1254
1249
  return toI64(0)
1255
1250
  }
1256
- // Unsigned: 0 - 2^63 wraps to the bits of -2^63, and every smaller
1257
- // magnitude to its negation.
1258
- return this.at(this.tokStart) === JWT_CH_MINUS ? toI64(toU64(0) - this.value) : toI64(this.value)
1251
+ const magnitude: u64 = this.value
1252
+ if (this.at(this.tokStart) !== JWT_CH_MINUS || magnitude === toU64(0)) {
1253
+ return toI64(magnitude)
1254
+ }
1255
+ // A negative number's magnitude is at most 2^63 (the scan's limit), so one
1256
+ // less fits an `i64` and its negation less one is the value, -2^63 included.
1257
+ const lessMagnitude: u64 = magnitude - toU64(1)
1258
+ const less: i64 = toI64(lessMagnitude)
1259
+ if (less < 0) {
1260
+ return toI64(0)
1261
+ }
1262
+ return -less - 1
1259
1263
  }
1260
1264
 
1261
1265
  /**
@@ -1719,17 +1723,21 @@ export class JwtVerifier {
1719
1723
  return JWT_ERR_CLAIMS
1720
1724
  }
1721
1725
  const leeway: i64 = jwtClamp(this.leeway, toI64(0), toI64(JWT_MAX_LEEWAY))
1722
- // Unsigned: `now` is held to [0, 2^62] and `leeway` to [0, 2^31 - 1], so
1723
- // both sums and the difference fit an `i64`, and the wrapped bits are theirs.
1724
- const early: i64 = toI64(toU64(now) - toU64(leeway))
1725
- const late: i64 = toI64(toU64(now) + toU64(leeway))
1726
- if (this.hasExp && !(early < this.exp)) {
1726
+ // `verify` holds `now` to [0, 2^62]: the floors here let src/bounds.ts
1727
+ // prove `now - leeway`, and a claim is compared past `now + leeway` as
1728
+ // its distance from `now`, which cannot overflow.
1729
+ if (now < 0 || leeway < 0) {
1730
+ return JWT_ERR_CLAIMS
1731
+ }
1732
+ if (this.hasExp && !(now - leeway < this.exp)) {
1727
1733
  return JWT_ERR_EXPIRED
1728
1734
  }
1729
- if (this.hasNbf && this.nbf > late) {
1735
+ const nbf: i64 = this.nbf
1736
+ if (this.hasNbf && nbf > now && nbf - now > leeway) {
1730
1737
  return JWT_ERR_NOT_YET
1731
1738
  }
1732
- if (this.hasIat && this.iat > late) {
1739
+ const iat: i64 = this.iat
1740
+ if (this.hasIat && iat > now && iat - now > leeway) {
1733
1741
  return JWT_ERR_IAT
1734
1742
  }
1735
1743
  if (issuer !== null && !issOk) {
@@ -1880,7 +1888,10 @@ export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): strin
1880
1888
  for (let i: i32 = 0; i < size; i += 1) {
1881
1889
  tag.push(toU8(0))
1882
1890
  }
1883
- hmacKeyedInto(hmac, msg, 0, toI32(msg.length), tag, 0)
1891
+ // `false` once the `HmacKey` itself is wiped.
1892
+ if (!hmacKeyedInto(hmac, msg, 0, toI32(msg.length), tag, 0)) {
1893
+ return null
1894
+ }
1884
1895
  sig = tag
1885
1896
  } else if (key.alg === JWT_ALG_ES256) {
1886
1897
  sig = p256SignSha256(key.priv, msg)
@@ -157,7 +157,7 @@
157
157
  * (`docs/wp26-stdlib.md` §3e).
158
158
  */
159
159
  import { Secret, expose, exposeWith, secret, wipe } from "nish:secret"
160
- import { SHA256_SIZE, Sha256, sha256 } from "nish/crypto/sha256"
160
+ import { SHA256_SIZE, Sha256, sha256, sha256Absorbed } from "nish/crypto/sha256"
161
161
  import { timingSafeEqual } from "nish/crypto/ct"
162
162
 
163
163
  /** The length in bytes of a private key: one scalar, big-endian. */
@@ -3733,9 +3733,12 @@ const p256BaseSelect = (
3733
3733
  const u: u32 = (digit ^ ctEq(top, 0)) & low
3734
3734
  p256SetZero(out.x)
3735
3735
  p256SetZero(out.y)
3736
+ // `c` is a comb below P256_COMBS (11) and `j` an entry below
3737
+ // P256_COMB_ENTRIES (32), which the masks keep: they let src/bounds.ts prove
3738
+ // the offset, P256_BASE_ENTRY (16) words an entry, in range.
3739
+ const first: i32 = (c & 15) * (P256_COMB_ENTRIES * P256_BASE_ENTRY)
3736
3740
  for (let j: i32 = 0; j < entries; j++) {
3737
- // In u32: `c` is a comb below P256_COMBS, so the offset is inside the table.
3738
- const at: i32 = toI32((toU32(c) * toU32(entries) + toU32(j)) * toU32(P256_BASE_ENTRY))
3741
+ const at: i32 = first + (j & 31) * P256_BASE_ENTRY
3739
3742
  p256BaseMove(out, table, at, ctEq(toU32(j), u))
3740
3743
  }
3741
3744
  p256SetZero(s.t1)
@@ -4001,16 +4004,10 @@ const p256WipeHash = (h: Sha256): void => {
4001
4004
  const p256Mac = (k: u8[], a: u8[], b: u8[], c: u8[], d: u8[]): u8[] => {
4002
4005
  const ipad: u8[] = p256PadBlock(k, 0x36)
4003
4006
  const opad: u8[] = p256PadBlock(k, 0x5c)
4004
- const inner = new Sha256()
4005
- inner.update(ipad, P256_FROM, toI32(ipad.length))
4006
- inner.update(a, P256_FROM, toI32(a.length))
4007
- inner.update(b, P256_FROM, toI32(b.length))
4008
- inner.update(c, P256_FROM, toI32(c.length))
4009
- inner.update(d, P256_FROM, toI32(d.length))
4007
+ const inner: Sha256 = sha256Absorbed(ipad, a, b, c, d)
4010
4008
  const innerDigest: u8[] = inner.digest()
4011
- const outer = new Sha256()
4012
- outer.update(opad, P256_FROM, toI32(opad.length))
4013
- outer.update(innerDigest, P256_FROM, toI32(innerDigest.length))
4009
+ const empty: u8[] = new Array<u8>(0)
4010
+ const outer: Sha256 = sha256Absorbed(opad, innerDigest, empty, empty, empty)
4014
4011
  const tag: u8[] = outer.digest()
4015
4012
  wipe(ipad)
4016
4013
  wipe(opad)
@@ -5107,8 +5104,9 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
5107
5104
  * `p256SignSha256` of `msg[off .. off + len)` in `scratch`, written as `r || s`
5108
5105
  * at `out[at .. at + 64)`: the same 64 bytes `p256SignSha256` answers for the
5109
5106
  * same key and message. Answers `false`, writing nothing, for a private key
5110
- * `p256PublicKey` would refuse. A message or signature window outside its
5111
- * array panics before anything is read.
5107
+ * `p256PublicKey` would refuse, for a signature window outside `out` before
5108
+ * anything is read, and for a message window outside `msg` before any nonce
5109
+ * is derived (`p256ShaInto`'s test).
5112
5110
  *
5113
5111
  * It runs inside an arena block of its own and stores nothing, so a signature
5114
5112
  * leaves the arena where it was; every secret the scratch held — RFC 6979's
@@ -5122,7 +5120,7 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
5122
5120
  * @param len The message's length in bytes.
5123
5121
  * @param out The array the signature is written into.
5124
5122
  * @param at Where the 64 bytes start in `out`.
5125
- * @returns `true` once the signature is written, `false` for a refused key,
5123
+ * @returns `true` once the signature is written, `false` for a refused key or window,
5126
5124
  * for a scratch whose arrays a hashing step refuses (which
5127
5125
  * `new P256SignScratch()` never builds), or when RFC 6979 finds no usable
5128
5126
  * nonce within 64 rounds.
@@ -5136,11 +5134,8 @@ export const p256SignSha256Into = (
5136
5134
  out: u8[],
5137
5135
  at: i32
5138
5136
  ): boolean => {
5139
- if (!p256WindowFits(toI32(msg.length), off, len)) {
5140
- panic("p256SignSha256Into: the message window is outside its array")
5141
- }
5142
5137
  if (!p256WindowFits(toI32(out.length), at, P256_SIGNATURE_SIZE)) {
5143
- panic("p256SignSha256Into: the signature window is outside its array")
5138
+ return false
5144
5139
  }
5145
5140
  // Whether the table is built yet is public: it is the first signature or not. It
5146
5141
  // is built before the arena opens, so it outlives this call.
@@ -58,7 +58,11 @@ export class PkiRoots {
58
58
  roots: X509Certificate[]
59
59
  /** Every file that was read, in order, whether or not it held a root. */
60
60
  sources: string[]
61
- /** The PEM blocks that were not a root, and the files too large or past the cap to read. */
61
+ /**
62
+ * The PEM blocks that were not a root, and the files too large or past the
63
+ * cap to read. The count stops at `PKI_ROOTS_MAX_SKIPPED`: a value there
64
+ * means at least that many were skipped.
65
+ */
62
66
  skipped: i32 = 0
63
67
 
64
68
  /** An empty load: no roots, no sources, nothing skipped. */
@@ -87,11 +91,18 @@ export const pkiSystemBundles = (): string[] => [
87
91
  "/etc/ssl/cert.pem",
88
92
  ]
89
93
 
94
+ /** Where `PkiRoots.skipped` stops counting, 2^31 − 1: a value there means at least that many. */
95
+ export const PKI_ROOTS_MAX_SKIPPED: i32 = 2147483647
96
+
90
97
  /** The directory read beside the system bundle. */
91
98
  export const PKI_SYSTEM_DIR: string = "/etc/ssl/certs"
92
99
 
93
- /** What one load has read so far, and the DER already kept. */
94
- class PkiRootsLoad {
100
+ /**
101
+ * What one load has read so far, and the DER already kept.
102
+ *
103
+ * @internal
104
+ */
105
+ export class PkiRootsLoad {
95
106
  /** @internal */
96
107
  out: PkiRoots
97
108
  /** @internal */
@@ -99,6 +110,7 @@ class PkiRootsLoad {
99
110
  /** @internal */
100
111
  files: i32 = 0
101
112
 
113
+ /** A load that has read nothing: no roots, no sources, nothing skipped. */
102
114
  constructor() {
103
115
  this.out = new PkiRoots()
104
116
  this.seen = new Set<string>()
@@ -106,12 +118,20 @@ class PkiRootsLoad {
106
118
  }
107
119
 
108
120
  /**
109
- * Counts one PEM block or file skipped. The count is not capped: past
110
- * `PKI_ROOTS_MAX_FILES` a trust directory's entries are each counted without
111
- * a read, so no bound below 2^31 holds for it (PKI-R8).
121
+ * Counts one PEM block or file skipped, up to `PKI_ROOTS_MAX_SKIPPED`. Past
122
+ * `PKI_ROOTS_MAX_FILES` a trust directory's entries are each counted without a
123
+ * read, so no smaller bound holds; the count stops there rather than wrapping
124
+ * or panicking, and the field's doc says what a value at the cap means
125
+ * (PKI-R8).
126
+ *
127
+ * @internal
128
+ * @param load the load whose `out.skipped` counts one more skip
112
129
  */
113
- const pkiRootsSkip = (load: PkiRootsLoad): void => {
114
- load.out.skipped = load.out.skipped + 1
130
+ export const pkiRootsSkip = (load: PkiRootsLoad): void => {
131
+ const skipped: i32 = load.out.skipped
132
+ if (skipped < PKI_ROOTS_MAX_SKIPPED) {
133
+ load.out.skipped = skipped + 1
134
+ }
115
135
  }
116
136
 
117
137
  /**
@@ -202,11 +222,14 @@ const pkiRootsSplit = (load: PkiRootsLoad, text: string): void => {
202
222
  if (toI32(text.charCodeAt(lineEnd)) === 10) {
203
223
  break
204
224
  }
205
- // Unsigned: lineEnd is below text.length, so it cannot wrap.
206
- lineEnd = toI32(toU32(lineEnd) + 1)
225
+ // Below text.length, so the next index fits an `i32`.
226
+ lineEnd = lineEnd + 1
227
+ }
228
+ let next: i32 = n
229
+ if (lineEnd >= 0 && lineEnd < n) {
230
+ // Below n, so the next index fits an `i32`.
231
+ next = lineEnd + 1
207
232
  }
208
- // Unsigned: lineEnd is below n on that side, so it cannot wrap.
209
- const next: i32 = lineEnd < n ? toI32(toU32(lineEnd) + 1) : n
210
233
  // A carriage return, space or tab before the line feed is no part of the
211
234
  // line, so a BEGIN with trailing whitespace still opens a block and is
212
235
  // counted when `pemToDer` refuses it, rather than lost unseen.
package/std/crypto/pki.ts CHANGED
@@ -282,18 +282,20 @@ const pkiFoldEqualAt = (a: string, aAt: i32, b: string, bAt: i32, n: i32): boole
282
282
  if (aAt < 0 || bAt < 0 || n < 0 || n > toI32(a.length) - aAt || n > toI32(b.length) - bAt) {
283
283
  return false
284
284
  }
285
+ let x: i32 = aAt
286
+ let y: i32 = bAt
285
287
  for (let i: i32 = 0; i < n; 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
288
  // Restates the range checked above for the index proof, which does not
290
- // carry it through the sum.
289
+ // carry it through `aAt + i`; past it, each step to the next index is
290
+ // proven.
291
291
  if (x < 0 || y < 0 || x >= toI32(a.length) || y >= toI32(b.length)) {
292
292
  return false
293
293
  }
294
294
  if (pkiLower(toI32(a.charCodeAt(x))) !== pkiLower(toI32(b.charCodeAt(y)))) {
295
295
  return false
296
296
  }
297
+ x = x + 1
298
+ y = y + 1
297
299
  }
298
300
  return true
299
301
  }
@@ -341,17 +343,6 @@ const pkiValidDns = (s: string): boolean => {
341
343
  return label > 0
342
344
  }
343
345
 
344
- const pkiLabelCount = (s: string): i32 => {
345
- let count: i32 = 1
346
- for (let i: i32 = 0; i < toI32(s.length); i++) {
347
- if (toI32(s.charCodeAt(i)) === 46) {
348
- // Unsigned: count is at most i + 1, below s.length, so it cannot wrap.
349
- count = toI32(toU32(count) + 1)
350
- }
351
- }
352
- return count
353
- }
354
-
355
346
  // ---------------------------------------------------------------------------
356
347
  // Address literals (RFC 3986 §3.2.2, RFC 4291 §2.2)
357
348
  // ---------------------------------------------------------------------------
@@ -396,8 +387,9 @@ const pkiParseIpv4 = (s: string, start: i32, end: i32): u8[] | null => {
396
387
  if (d < 0 || digits >= 3 || value > 25 || (digits > 0 && value === 0)) {
397
388
  return null
398
389
  }
399
- // Unsigned: value is at most 25 and d a digit, so the sum cannot wrap.
400
- value = toI32(toU32(value) * 10 + toU32(d))
390
+ // The masks keep what the check above holds (`value` at most 25, `d` a
391
+ // digit) and let src/bounds.ts prove the sum.
392
+ value = (value & 31) * 10 + (d & 15)
401
393
  digits++
402
394
  if (value > 255) {
403
395
  return null
@@ -549,15 +541,12 @@ const pkiDnsMatch = (san: string, host: string): boolean => {
549
541
  const rest: string = name.substring(2, n)
550
542
  const dot: i32 = toI32(host.indexOf("."))
551
543
  const restLength: i32 = n - 2
552
- return (
553
- pkiValidDns(rest) &&
554
- pkiLabelCount(rest) >= 2 &&
555
- dot > 0 &&
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)
560
- )
544
+ // `indexOf` answers below the length; the bound restated proves `dot + 1`.
545
+ // A valid `rest` with a dot has the two labels or more a wildcard needs.
546
+ if (dot <= 0 || dot >= toI32(host.length) || !pkiValidDns(rest) || toI32(rest.indexOf(".")) < 0) {
547
+ return false
548
+ }
549
+ return restLength === toI32(host.length) - 1 - dot && pkiFoldEqualAt(host, dot + 1, rest, 0, restLength)
561
550
  }
562
551
  return pkiValidDns(name) && n === toI32(host.length) && pkiFoldEqualAt(name, 0, host, 0, n)
563
552
  }
@@ -943,32 +932,30 @@ const pkiConstraintsCode = (s: PkiSearch, issuer: X509Certificate): i32 => {
943
932
  }
944
933
  // Each name below the issuer, the host's included, against each subtree.
945
934
  // 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
935
+ // In `u64` throughout: `names` is at most PKI_MAX_NAME_CHECKS before each
936
+ // add and each length below 2^32, and `subtrees` is below 2^33, so no sum
937
+ // or product here comes near 2^64.
938
+ const subtrees: u64 = toU64(issuer.ncPermitted.length) + toU64(issuer.ncExcluded.length)
939
+ const budget: u64 = toU64(PKI_MAX_NAME_CHECKS)
940
+ let cost: u64 = toU64(0)
941
+ if (subtrees > toU64(0)) {
942
+ let names: u64 = toU64(1)
950
943
  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)) {
944
+ names = names + toU64(c.sanDns.length) + toU64(c.sanIp.length) + toU64(1)
945
+ if (names > budget) {
955
946
  s.spent = true
956
947
  return PKI_ERR_BUDGET
957
948
  }
958
949
  }
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))
950
+ cost = names * subtrees
962
951
  }
963
952
  const used: i32 = s.nameChecks
964
- if (cost < 0 || cost > toI64(PKI_MAX_NAME_CHECKS)) {
953
+ if (used < 0 || cost > budget) {
965
954
  s.spent = true
966
955
  return PKI_ERR_BUDGET
967
956
  }
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)) {
957
+ const total: u64 = toU64(used) + cost
958
+ if (total > budget) {
972
959
  s.spent = true
973
960
  return PKI_ERR_BUDGET
974
961
  }
@@ -1081,14 +1068,15 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
1081
1068
  return PKI_ERR_EXT_KEY_USAGE
1082
1069
  }
1083
1070
  if (cand.bcPathLen >= 0) {
1084
- let below: i32 = 0
1071
+ // A `u32` count of the path's certificates, which cannot come near 2^32;
1072
+ // `bcPathLen` is not negative here, so its `u32` is its value.
1073
+ let below: u32 = toU32(0)
1085
1074
  for (let i: i32 = 1; i < toI32(s.path.length); i++) {
1086
1075
  if (!pkiSelfIssued(s.path[i])) {
1087
- // Unsigned: below counts certificates of the path, so it cannot wrap.
1088
- below = toI32(toU32(below) + 1)
1076
+ below = below + toU32(1)
1089
1077
  }
1090
1078
  }
1091
- if (below > cand.bcPathLen) {
1079
+ if (below > toU32(cand.bcPathLen)) {
1092
1080
  return PKI_ERR_PATH_LENGTH
1093
1081
  }
1094
1082
  }
@@ -1141,10 +1129,10 @@ const pkiCandidate = (s: PkiSearch, child: X509Certificate, cand: X509Certificat
1141
1129
  */
1142
1130
  const pkiExtend = (s: PkiSearch, child: X509Certificate): boolean => {
1143
1131
  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))
1132
+ // pkiIssuerCode keeps the path to PKI_MAX_PATH (8) certificates, which the
1133
+ // mask keeps: it lets src/bounds.ts prove both positions.
1134
+ const above: i32 = (depth & 15) + 1
1135
+ const atRoot: i32 = PKI_AT_ROOT + above
1148
1136
  if (toI32(child.issuer.length) > 2) {
1149
1137
  const roots: X509Certificate[] | undefined = s.store.bySubject.get(pkiHex(child.issuer))
1150
1138
  if (roots !== undefined) {
@@ -1399,12 +1387,12 @@ export const pkiVerifyServer = (
1399
1387
  if (count > PKI_MAX_CHAIN_CERTS) {
1400
1388
  return new PkiResult(PKI_ERR_CHAIN_TOO_LONG, none)
1401
1389
  }
1402
- let bytes: i64 = 0
1390
+ // In `u64`: at most PKI_MAX_CHAIN_BYTES before each add, and a length below
1391
+ // 2^32, so the sum comes nowhere near 2^64.
1392
+ let bytes: u64 = toU64(0)
1403
1393
  for (const der of chain) {
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)) {
1394
+ bytes = bytes + toU64(der.length)
1395
+ if (bytes > toU64(PKI_MAX_CHAIN_BYTES)) {
1408
1396
  return new PkiResult(PKI_ERR_CHAIN_TOO_LONG, none)
1409
1397
  }
1410
1398
  }