nish 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
@@ -132,6 +132,29 @@ const TLS_TICKET_PLAIN_FIXED: i32 = 24
132
132
  /** Where the server name's length byte sits in the plaintext. */
133
133
  const TLS_TICKET_NAME_AT: i32 = 22
134
134
 
135
+ /**
136
+ * `a + b`, held at the `i64` range's ends rather than past them. Two values of one
137
+ * sign sum within 2^64 of zero, so their sum in `u64` reads across the sign bit only
138
+ * when it has left the range.
139
+ */
140
+ const tlsTicketAdd = (a: i64, b: i64): i64 => {
141
+ if (a >= 0) {
142
+ if (b < 0) {
143
+ return a + b
144
+ }
145
+ const sum: i64 = toI64(toU64(a) + toU64(b))
146
+ return sum >= 0 ? sum : toI64((toU64(0) - toU64(1)) >> toU64(1))
147
+ }
148
+ if (b >= 0) {
149
+ return a + b
150
+ }
151
+ const sum: i64 = toI64(toU64(a) + toU64(b))
152
+ return sum < 0 ? sum : toI64(toU64(1) << toU64(63))
153
+ }
154
+
155
+ /** `now - then` for a `now` no earlier than `then`: exact, since two `i64`s in that order differ by less than 2^64. */
156
+ const tlsTicketElapsed = (now: i64, then: i64): u64 => toU64(now) - toU64(then)
157
+
135
158
  /**
136
159
  * A ticket key's public record. A class, so the program may keep it in a
137
160
  * field or anywhere else; the key it describes is a `Secret` the program
@@ -165,8 +188,11 @@ export class TlsTicketKeyInfo {
165
188
  * @param other The record to copy from, left unchanged.
166
189
  */
167
190
  copyFrom(other: TlsTicketKeyInfo): void {
168
- for (let k: i32 = 0; k < TLS_TICKET_KEY_ID_SIZE; k++) {
169
- this.id[k] = other.id[k]
191
+ const to: u8[] = this.id
192
+ const from: u8[] = other.id
193
+ // Both ids are `TLS_TICKET_KEY_ID_SIZE` bytes from the constructor, so the length bounds never stop the copy.
194
+ for (let k: i32 = 0; k < TLS_TICKET_KEY_ID_SIZE && k < toI32(to.length) && k < toI32(from.length); k++) {
195
+ to[k] = from[k]
170
196
  }
171
197
  this.createdAt = other.createdAt
172
198
  this.sealed = other.sealed
@@ -219,10 +245,11 @@ export class TlsTicketState {
219
245
  /**
220
246
  * When the ticket stops opening: its issue time plus its lifetime. It opens at that instant, and not after.
221
247
  *
222
- * @returns The expiry in `monotonicNanos()`, which `TlsTicketStrikes.take` takes as its `expiresAt`.
248
+ * @returns The expiry in `monotonicNanos()`, which `TlsTicketStrikes.take` takes as its `expiresAt`;
249
+ * one past the `i64` range is held at its end.
223
250
  */
224
251
  expiresAt(): i64 {
225
- return this.issuedAt + toI64(this.lifetime) * TLS_TICKET_NANOS
252
+ return tlsTicketAdd(this.issuedAt, toI64(this.lifetime) * TLS_TICKET_NANOS)
226
253
  }
227
254
  }
228
255
 
@@ -252,31 +279,39 @@ export const tlsTicketKeyDraw = (info: TlsTicketKeyInfo, now: i64): Secret<u8[]>
252
279
  * @returns True when the program should rotate now; the key still opens tickets for a while after.
253
280
  */
254
281
  export const tlsTicketKeyDue = (info: TlsTicketKeyInfo, now: i64): boolean =>
255
- now - info.createdAt >= TLS_TICKET_SEAL_WINDOW || info.sealed >= info.sealCap
282
+ (now >= info.createdAt && tlsTicketElapsed(now, info.createdAt) >= toU64(TLS_TICKET_SEAL_WINDOW)) ||
283
+ info.sealed >= info.sealCap
256
284
 
257
285
  /** True when the key `info` describes may seal at `now`: within its issue window, inclusive, and under its cap. */
258
286
  const tlsTicketKeySeals = (info: TlsTicketKeyInfo, now: i64): boolean =>
259
- now >= info.createdAt && now - info.createdAt <= TLS_TICKET_SEAL_WINDOW && info.sealed < info.sealCap
287
+ now >= info.createdAt &&
288
+ tlsTicketElapsed(now, info.createdAt) <= toU64(TLS_TICKET_SEAL_WINDOW) &&
289
+ info.sealed < info.sealCap
260
290
 
261
291
  /** True when the key `info` describes may open at `now`: within 14 hours of its draw, inclusive. */
262
292
  const tlsTicketKeyOpens = (info: TlsTicketKeyInfo, now: i64): boolean =>
263
- now >= info.createdAt && now - info.createdAt <= TLS_TICKET_OPEN_WINDOW
293
+ now >= info.createdAt && tlsTicketElapsed(now, info.createdAt) <= toU64(TLS_TICKET_OPEN_WINDOW)
264
294
 
265
295
  /** The plaintext's length for `state`, or -1 when the state cannot be sealed. */
266
296
  const tlsTicketPlainLength = (state: TlsTicketState): i32 => {
267
297
  const hashLength: i32 = tlsSuiteHashLength(state.suite)
298
+ const nameLength: i32 = state.serverNameLength
299
+ const pskLength: i32 = state.pskLength
300
+ // A known suite's hash is 32 or 48 bytes, so the PSK's bound below never refuses one the hash test passed.
268
301
  if (
269
302
  hashLength === 0 ||
270
- state.pskLength !== hashLength ||
271
- state.pskLength > toI32(state.psk.length) ||
272
- state.serverNameLength < 0 ||
273
- state.serverNameLength > TLS_TICKET_SERVER_NAME_MAX ||
274
- state.serverNameLength > toI32(state.serverName.length) ||
303
+ pskLength !== hashLength ||
304
+ pskLength < 0 ||
305
+ pskLength > 48 ||
306
+ pskLength > toI32(state.psk.length) ||
307
+ nameLength < 0 ||
308
+ nameLength > 255 ||
309
+ nameLength > toI32(state.serverName.length) ||
275
310
  state.lifetime < 0
276
311
  ) {
277
312
  return -1
278
313
  }
279
- return TLS_TICKET_PLAIN_FIXED + state.serverNameLength + state.pskLength
314
+ return TLS_TICKET_PLAIN_FIXED + nameLength + pskLength
280
315
  }
281
316
 
282
317
  /**
@@ -289,34 +324,92 @@ const tlsTicketPlainLength = (state: TlsTicketState): i32 => {
289
324
  */
290
325
  export const tlsTicketSize = (state: TlsTicketState): i32 => {
291
326
  const plain: i32 = tlsTicketPlainLength(state)
292
- return plain < 0 ? 0 : TLS_TICKET_KEY_ID_SIZE + TLS_TICKET_NONCE_SIZE + plain + POLY1305_TAG_SIZE
327
+ // `tlsTicketPlainLength` answers at most 327, so the upper test never refuses.
328
+ if (plain < 0 || plain > 327) {
329
+ return 0
330
+ }
331
+ return TLS_TICKET_KEY_ID_SIZE + TLS_TICKET_NONCE_SIZE + plain + POLY1305_TAG_SIZE
293
332
  }
294
333
 
295
- /** Writes `value`'s low `size` bytes big-endian at `at`. */
296
- const tlsTicketPut = (buf: u8[], at: i32, value: i64, size: i32): void => {
297
- for (let k: i32 = 0; k < size; k++) {
298
- buf[at + k] = toU8(toI32((value >> (toI64(size - 1 - k) * 8)) & 255))
334
+ /**
335
+ * Writes `value`'s low `size` bytes, 0 to 8, big-endian at `at`, and answers
336
+ * true; false, writing nothing, when they do not all fit in `buf`. Every
337
+ * caller has sized `buf` to hold them and refuses on false.
338
+ */
339
+ const tlsTicketPut = (buf: u8[], at: i32, value: i64, size: i32): boolean => {
340
+ if (at < 0 || size < 0 || size > 8 || at > toI32(buf.length) - size) {
341
+ return false
342
+ }
343
+ for (let j: i32 = at; j < toI32(buf.length) && j - at < size; j++) {
344
+ const shift: i64 = toI64(size - 1) * 8 - toI64(j - at) * 8
345
+ buf[j] = toU8(toI32((value >> shift) & 255))
299
346
  }
347
+ return true
300
348
  }
301
349
 
302
- /** Reads `size` bytes big-endian at `at`. */
350
+ /**
351
+ * Reads `size` bytes, 0 to 8, big-endian at `at`, or -1 when they do not all
352
+ * lie inside `buf`. `tlsTicketParse` checks every window before it reads, so
353
+ * no read reaches the -1; an 8-byte value may itself be -1, and only that
354
+ * entry check stands for those.
355
+ */
303
356
  const tlsTicketGet = (buf: u8[], at: i32, size: i32): i64 => {
304
357
  let value: i64 = 0
305
- for (let k: i32 = 0; k < size; k++) {
306
- value = (value << 8) | toI64(buf[at + k])
358
+ if (at < 0 || size < 0 || size > 8 || at > toI32(buf.length) - size) {
359
+ return -1
360
+ }
361
+ for (let j: i32 = at; j < toI32(buf.length) && j - at < size; j++) {
362
+ value = (value << 8) | toI64(buf[j])
307
363
  }
308
364
  return value
309
365
  }
310
366
 
311
- /** A copy of `buf[from .. to)`. */
312
- const tlsTicketRange = (buf: u8[], from: i32, to: i32): u8[] => {
313
- const out: u8[] = new Array<u8>(to - from)
314
- for (let k: i32 = 0; k < toI32(out.length); k++) {
315
- const at: i32 = from + k
316
- if (at >= 0 && at < toI32(buf.length)) {
317
- out[k] = buf[at]
367
+ /**
368
+ * Copies `length` bytes from `src[srcAt]` to `dst[dstAt]` and answers true;
369
+ * false, copying nothing, when either window does not fit. Every caller has
370
+ * checked both windows and refuses on false; the bounds in the loop are the
371
+ * prover's.
372
+ */
373
+ const tlsTicketCopy = (dst: u8[], dstAt: i32, src: u8[], srcAt: i32, length: i32): boolean => {
374
+ if (
375
+ length < 0 ||
376
+ dstAt < 0 ||
377
+ srcAt < 0 ||
378
+ toI64(dstAt) + toI64(length) > toI64(dst.length) ||
379
+ toI64(srcAt) + toI64(length) > toI64(src.length)
380
+ ) {
381
+ return false
382
+ }
383
+ for (let k: i32 = 0; k < length; k++) {
384
+ // A position past an `i32` wraps negative and is skipped like one past the array.
385
+ const to: i32 = toI32(toI64(dstAt) + toI64(k))
386
+ const from: i32 = toI32(toI64(srcAt) + toI64(k))
387
+ if (to >= 0 && to < toI32(dst.length) && from >= 0 && from < toI32(src.length)) {
388
+ dst[to] = src[from]
318
389
  }
319
390
  }
391
+ return true
392
+ }
393
+
394
+ /**
395
+ * A copy of `buf[from .. to)`, in an array made at its exact size so that no
396
+ * growth leaves a stray copy of a plaintext behind; empty when the window is
397
+ * not within `buf` or is longer than 65,535 bytes, which no caller asks for:
398
+ * a ticket over that length is refused before it is opened.
399
+ */
400
+ const tlsTicketRange = (buf: u8[], from: i32, to: i32): u8[] => {
401
+ const none: u8[] = []
402
+ if (from < 0 || to > toI32(buf.length)) {
403
+ return none
404
+ }
405
+ const length: i64 = toI64(to) - toI64(from)
406
+ if (length < 0 || length > 65535) {
407
+ return none
408
+ }
409
+ const out: u8[] = new Array<u8>(toU16(length))
410
+ if (!tlsTicketCopy(out, 0, buf, from, toI32(out.length))) {
411
+ return none
412
+ }
320
413
  return out
321
414
  }
322
415
 
@@ -357,8 +450,8 @@ const tlsTicketSealExposed = (key: u8[], input: u8[]): u8[] => {
357
450
  *
358
451
  * @param key The ticket key, lent: it is read only inside `exposeWith`, and stays the caller's to wipe.
359
452
  * @param info The key's record: its id heads the ticket and its count goes up by one on a seal.
360
- * @param state What the ticket carries; its `issuedAt` is overwritten with `now`, unless the key
361
- * cannot seal at `now`, the state does not fit a ticket or `out` is too short.
453
+ * @param state What the ticket carries; its `issuedAt` is overwritten with `now` only when the
454
+ * ticket is sealed, so a refusal leaves the state untouched.
362
455
  * @param now The issue time, in `monotonicNanos()`.
363
456
  * @param out Where the ticket is written, from index 0; at least `tlsTicketSize(state)` bytes.
364
457
  * @returns How many bytes of `out` hold the ticket, or 0 when nothing was sealed.
@@ -372,38 +465,58 @@ export const tlsTicketSeal = (
372
465
  ): i32 => {
373
466
  const plainLength: i32 = tlsTicketPlainLength(state)
374
467
  const size: i32 = tlsTicketSize(state)
375
- if (!tlsTicketKeySeals(info, now) || plainLength < 0 || size > toI32(out.length)) {
468
+ const nameLength: i32 = state.serverNameLength
469
+ const pskLength: i32 = state.pskLength
470
+ const id: u8[] = info.id
471
+ // `tlsTicketPlainLength` has bounded the plaintext and both lengths, and a key that seals has `sealed`
472
+ // under its cap, so the tests after the first three never refuse a record `tlsTicketKeyDraw` filled.
473
+ if (
474
+ !tlsTicketKeySeals(info, now) ||
475
+ plainLength < 0 ||
476
+ size > toI32(out.length) ||
477
+ plainLength > 327 ||
478
+ nameLength < 0 ||
479
+ nameLength > 255 ||
480
+ pskLength < 0 ||
481
+ pskLength > 48 ||
482
+ toI32(id.length) !== TLS_TICKET_KEY_ID_SIZE
483
+ ) {
376
484
  return 0
377
485
  }
378
486
  using _scope = arena()
379
- state.issuedAt = now
380
487
  const head: i32 = TLS_TICKET_KEY_ID_SIZE + TLS_TICKET_NONCE_SIZE
381
- const input: u8[] = new Array<u8>(head + plainLength)
488
+ const input: u8[] = new Array<u8>(toU16(head + plainLength))
382
489
  const nonce: u8[] = new Array<u8>(TLS_TICKET_NONCE_SIZE)
383
490
  crypto.getRandomValues(nonce)
384
- input.set(info.id, 0)
385
- input.set(nonce, TLS_TICKET_KEY_ID_SIZE)
386
- tlsTicketPut(input, head, toI64(state.suite), 2)
387
- tlsTicketPut(input, head + 2, now, 8)
388
- tlsTicketPut(input, head + 10, toI64(state.lifetime), 4)
389
- tlsTicketPut(input, head + 14, state.chainStart, 8)
390
491
  const nameAt: i32 = head + TLS_TICKET_NAME_AT
391
- input[nameAt] = toU8(state.serverNameLength)
392
- for (let k: i32 = 0; k < state.serverNameLength; k++) {
393
- input[nameAt + 1 + k] = state.serverName[k]
394
- }
395
- const pskAt: i32 = nameAt + 1 + state.serverNameLength
396
- input[pskAt] = toU8(state.pskLength)
397
- for (let k: i32 = 0; k < state.pskLength; k++) {
398
- input[pskAt + 1 + k] = state.psk[k]
492
+ const pskAt: i32 = nameAt + 1 + nameLength
493
+ // `input` was made at the plaintext's size, so every field fits; a state
494
+ // whose arrays are shorter than its lengths is refused, not sealed short.
495
+ const filled: boolean =
496
+ tlsTicketCopy(input, 0, id, 0, TLS_TICKET_KEY_ID_SIZE) &&
497
+ tlsTicketCopy(input, TLS_TICKET_KEY_ID_SIZE, nonce, 0, TLS_TICKET_NONCE_SIZE) &&
498
+ tlsTicketPut(input, head, toI64(state.suite), 2) &&
499
+ tlsTicketPut(input, head + 2, now, 8) &&
500
+ tlsTicketPut(input, head + 10, toI64(state.lifetime), 4) &&
501
+ tlsTicketPut(input, head + 14, state.chainStart, 8) &&
502
+ tlsTicketPut(input, nameAt, toI64(nameLength), 1) &&
503
+ tlsTicketCopy(input, nameAt + 1, state.serverName, 0, nameLength) &&
504
+ tlsTicketPut(input, pskAt, toI64(pskLength), 1) &&
505
+ tlsTicketCopy(input, pskAt + 1, state.psk, 0, pskLength)
506
+ if (!filled) {
507
+ secureZero(input)
508
+ return 0
399
509
  }
400
510
  const sealed: u8[] = exposeWith(key, input, tlsTicketSealExposed)
401
511
  let written: i32 = 0
402
- if (toI32(sealed.length) === plainLength + POLY1305_TAG_SIZE) {
403
- out.set(info.id, 0)
404
- out.set(nonce, TLS_TICKET_KEY_ID_SIZE)
405
- out.set(sealed, head)
406
- info.sealed = info.sealed + 1
512
+ if (
513
+ toI32(sealed.length) === plainLength + POLY1305_TAG_SIZE &&
514
+ tlsTicketCopy(out, 0, input, 0, head) &&
515
+ tlsTicketCopy(out, head, sealed, 0, toI32(sealed.length))
516
+ ) {
517
+ // Under its cap, so the count never reaches the `i64` top `tlsTicketAdd` would hold it at.
518
+ info.sealed = tlsTicketAdd(info.sealed, 1)
519
+ state.issuedAt = now
407
520
  written = size
408
521
  }
409
522
  secureZero(input)
@@ -445,12 +558,20 @@ const tlsTicketParse = (plain: u8[], now: i64, out: TlsTicketState): boolean =>
445
558
  const issuedAt: i64 = tlsTicketGet(plain, 2, 8)
446
559
  const lifetime: i64 = tlsTicketGet(plain, 10, 4)
447
560
  const chainStart: i64 = tlsTicketGet(plain, 14, 8)
448
- const nameLength: i32 = toI32(plain[TLS_TICKET_NAME_AT])
561
+ // Each length is one byte, so its bound below never refuses. The name's bound stays the literal 255
562
+ // (`TLS_TICKET_SERVER_NAME_MAX`): the prover reads literal guards, and the sums below need it.
563
+ const nameLength: i32 = toI32(tlsTicketGet(plain, TLS_TICKET_NAME_AT, 1))
564
+ if (nameLength < 0 || nameLength > 255) {
565
+ return false
566
+ }
449
567
  const pskAt: i32 = TLS_TICKET_NAME_AT + 1 + nameLength
450
568
  if (pskAt >= length) {
451
569
  return false
452
570
  }
453
- const pskLength: i32 = toI32(plain[pskAt])
571
+ const pskLength: i32 = toI32(tlsTicketGet(plain, pskAt, 1))
572
+ if (pskLength < 0 || pskLength > 255) {
573
+ return false
574
+ }
454
575
  const hashLength: i32 = tlsSuiteHashLength(suite)
455
576
  if (
456
577
  pskAt + 1 + pskLength !== length ||
@@ -466,32 +587,41 @@ const tlsTicketParse = (plain: u8[], now: i64, out: TlsTicketState): boolean =>
466
587
  out.lifetime = toI32(lifetime)
467
588
  out.chainStart = chainStart
468
589
  out.serverNameLength = nameLength
469
- for (let k: i32 = 0; k < nameLength; k++) {
470
- out.serverName[k] = plain[TLS_TICKET_NAME_AT + 1 + k]
471
- }
472
590
  out.pskLength = pskLength
473
- for (let k: i32 = 0; k < pskLength; k++) {
474
- out.psk[k] = plain[pskAt + 1 + k]
591
+ // Both windows were checked above; a copy that cannot land whole refuses.
592
+ if (
593
+ !tlsTicketCopy(out.serverName, 0, plain, TLS_TICKET_NAME_AT + 1, nameLength) ||
594
+ !tlsTicketCopy(out.psk, 0, plain, pskAt + 1, pskLength)
595
+ ) {
596
+ return false
475
597
  }
476
598
  return (
477
599
  lifetime <= toI64(TLS_TICKET_LIFETIME_SECONDS) &&
478
600
  chainStart <= issuedAt &&
479
601
  issuedAt <= now &&
480
602
  now <= out.expiresAt() &&
481
- now - chainStart <= TLS_TICKET_CHAIN_LIMIT
603
+ tlsTicketElapsed(now, chainStart) <= toU64(TLS_TICKET_CHAIN_LIMIT)
482
604
  )
483
605
  }
484
606
 
485
- /** True when `ticket` is long enough to hold a state and names `info`'s key id. */
607
+ /**
608
+ * True when `ticket` is long enough to hold a state, no longer than 65,535 bytes
609
+ * (a `ticket<1..2^16-1>`, RFC 8446 §4.6.1), and names `info`'s key id.
610
+ */
486
611
  const tlsTicketNamesKey = (info: TlsTicketKeyInfo, ticket: u8[]): boolean => {
487
612
  if (
488
613
  toI32(ticket.length) <
489
- TLS_TICKET_KEY_ID_SIZE + TLS_TICKET_NONCE_SIZE + TLS_TICKET_PLAIN_FIXED + POLY1305_TAG_SIZE
614
+ TLS_TICKET_KEY_ID_SIZE + TLS_TICKET_NONCE_SIZE + TLS_TICKET_PLAIN_FIXED + POLY1305_TAG_SIZE ||
615
+ toI32(ticket.length) > 65535
490
616
  ) {
491
617
  return false
492
618
  }
493
- for (let k: i32 = 0; k < TLS_TICKET_KEY_ID_SIZE && k < toI32(ticket.length); k++) {
494
- if (ticket[k] !== info.id[k]) {
619
+ const id: u8[] = info.id
620
+ if (toI32(id.length) !== TLS_TICKET_KEY_ID_SIZE) {
621
+ return false
622
+ }
623
+ for (let k: i32 = 0; k < TLS_TICKET_KEY_ID_SIZE && k < toI32(ticket.length) && k < toI32(id.length); k++) {
624
+ if (ticket[k] !== id[k]) {
495
625
  return false
496
626
  }
497
627
  }
@@ -501,8 +631,8 @@ const tlsTicketNamesKey = (info: TlsTicketKeyInfo, ticket: u8[]): boolean => {
501
631
  /**
502
632
  * Opens `ticket` under `key`, the key `info` describes, into `out`, and
503
633
  * answers whether it opened at `now`. False — with nothing of the key read —
504
- * when the ticket names another key id or the key is past 14 hours of awake
505
- * time; false when the tag does not verify, the plaintext is malformed, or the
634
+ * when the ticket names another key id, is longer than 65,535 bytes, or the key
635
+ * is past 14 hours of awake time; false when the tag does not verify, the plaintext is malformed, or the
506
636
  * ticket is outside its bounds (`tlsTicketParse`). On true, `out` holds the
507
637
  * state and the ticket's nonce in `id`, its strike-register id. On false after
508
638
  * the AEAD ran, the state's PSK array is wiped. The plaintext is wiped once,
@@ -529,12 +659,10 @@ export const tlsTicketOpen = (
529
659
  }
530
660
  using _scope = arena()
531
661
  const plain: u8[] = exposeWith(key, ticket, tlsTicketOpenExposed)
532
- const opened: boolean = tlsTicketParse(plain, now, out)
533
- if (opened) {
534
- for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE; k++) {
535
- out.id[k] = ticket[TLS_TICKET_KEY_ID_SIZE + k]
536
- }
537
- } else {
662
+ const opened: boolean =
663
+ tlsTicketParse(plain, now, out) &&
664
+ tlsTicketCopy(out.id, 0, ticket, TLS_TICKET_KEY_ID_SIZE, TLS_TICKET_NONCE_SIZE)
665
+ if (!opened) {
538
666
  secureZero(out.psk)
539
667
  }
540
668
  secureZero(plain)
@@ -584,18 +712,29 @@ export class TlsTicketStrikes {
584
712
  longest: i32
585
713
 
586
714
  /**
587
- * Makes the whole table now, so that no `take` ever allocates; it panics on a capacity below 1.
715
+ * Makes the whole table now, so that no `take` ever allocates; it panics on a capacity below 1, or above
716
+ * 178,956,969, past which the ids' table would not fit an `i32` length.
588
717
  *
589
718
  * @param capacity How many ids the register can hold at once, `TLS_TICKET_STRIKES_DEFAULT` when in doubt.
590
719
  */
591
720
  constructor(capacity: i32) {
592
- if (capacity < 1) {
593
- panic("TlsTicketStrikes: a capacity below 1")
721
+ if (capacity < 1 || capacity > 178956969) {
722
+ panic("TlsTicketStrikes: a capacity below 1 or above 178956969")
594
723
  }
595
724
  this.capacity = capacity
596
- this.ids = new Array<u8>(capacity * TLS_TICKET_NONCE_SIZE)
597
- this.expires = new Array<i64>(capacity)
598
- this.used = new Array<u8>(capacity)
725
+ const ids: u8[] = []
726
+ const expires: i64[] = []
727
+ const used: u8[] = []
728
+ for (let slot: i32 = 0; slot < capacity; slot++) {
729
+ for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE; k++) {
730
+ ids.push(0)
731
+ }
732
+ expires.push(0)
733
+ used.push(0)
734
+ }
735
+ this.ids = ids
736
+ this.expires = expires
737
+ this.used = used
599
738
  this.filled = 0
600
739
  this.longest = 0
601
740
  this.refusedFull = 0
@@ -608,11 +747,17 @@ export class TlsTicketStrikes {
608
747
  * @returns A slot index, from 0 to `capacity - 1`.
609
748
  */
610
749
  home(id: u8[]): i32 {
611
- let hash: i64 = 2166136261
750
+ const capacity: i64 = toI64(this.capacity)
751
+ // The constructor refuses a capacity below 1, so this never answers.
752
+ if (capacity < 1) {
753
+ return 0
754
+ }
755
+ // FNV-1a keeps 32 bits, which a `u64` product of a 32-bit hash and the prime holds exactly.
756
+ let hash: u64 = 2166136261
612
757
  for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE && k < toI32(id.length); k++) {
613
- hash = ((hash ^ toI64(id[k])) * 16777619) & 4294967295
758
+ hash = ((hash ^ toU64(id[k])) * 16777619) & 4294967295
614
759
  }
615
- return toI32(hash % toI64(this.capacity))
760
+ return toI32(toI64(hash) % capacity)
616
761
  }
617
762
 
618
763
  /**
@@ -621,9 +766,15 @@ export class TlsTicketStrikes {
621
766
  * @internal Read by `net/tls.ts`.
622
767
  */
623
768
  holds(slot: i32, id: u8[]): boolean {
769
+ const ids: u8[] = this.ids
770
+ // A slot outside the table holds nothing; the constructor bounds the table at 178,956,969 slots.
771
+ if (slot < 0 || slot > 178956969) {
772
+ return false
773
+ }
624
774
  const at: i32 = slot * TLS_TICKET_NONCE_SIZE
625
775
  for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE && k < toI32(id.length); k++) {
626
- if (this.ids[at + k] !== id[k]) {
776
+ const j: i32 = toI32(toI64(at) + toI64(k))
777
+ if (j < 0 || j >= toI32(ids.length) || ids[j] !== id[k]) {
627
778
  return false
628
779
  }
629
780
  }
@@ -638,9 +789,12 @@ export class TlsTicketStrikes {
638
789
  * @returns The number of slots ever used that hold `id`.
639
790
  */
640
791
  count(id: u8[]): i32 {
792
+ const capacity: i32 = this.capacity
793
+ const used: u8[] = this.used
641
794
  let found: i32 = 0
642
- for (let slot: i32 = 0; slot < this.capacity; slot++) {
643
- if (this.used[slot] !== 0 && this.holds(slot, id)) {
795
+ // `found` is at most `capacity`, so its bound never stops the count.
796
+ for (let slot: i32 = 0; slot < capacity && slot < toI32(used.length); slot++) {
797
+ if (used[slot] !== 0 && this.holds(slot, id) && found < 2147483647) {
644
798
  found++
645
799
  }
646
800
  }
@@ -654,9 +808,17 @@ export class TlsTicketStrikes {
654
808
  * @returns The number of live entries, from 0 to `capacity`.
655
809
  */
656
810
  live(now: i64): i32 {
811
+ const capacity: i32 = this.capacity
812
+ const used: u8[] = this.used
813
+ const expires: i64[] = this.expires
657
814
  let held: i32 = 0
658
- for (let slot: i32 = 0; slot < this.capacity; slot++) {
659
- if (this.used[slot] !== 0 && this.expires[slot] >= now) {
815
+ // `held` is at most `capacity`, so its bound never stops the count.
816
+ for (
817
+ let slot: i32 = 0;
818
+ slot < capacity && slot < toI32(used.length) && slot < toI32(expires.length);
819
+ slot++
820
+ ) {
821
+ if (used[slot] !== 0 && expires[slot] >= now && held < 2147483647) {
660
822
  held++
661
823
  }
662
824
  }
@@ -683,48 +845,78 @@ export class TlsTicketStrikes {
683
845
  if (toI32(id.length) !== TLS_TICKET_NONCE_SIZE) {
684
846
  return false
685
847
  }
686
- const bound: i64 = now + TLS_TICKET_LIFETIME_NANOS
848
+ const bound: i64 = tlsTicketAdd(now, TLS_TICKET_LIFETIME_NANOS)
687
849
  const until: i64 = expiresAt < bound ? expiresAt : bound
850
+ const capacity: i32 = this.capacity
851
+ const used: u8[] = this.used
852
+ const expires: i64[] = this.expires
853
+ const ids: u8[] = this.ids
854
+ const longest: i32 = this.longest
688
855
  const home: i32 = this.home(id)
856
+ // The constructor makes `capacity` slots in each array and `home` answers one of them, so a table this
857
+ // does not describe is refused, failing closed, and never reached.
858
+ if (capacity < 1 || home < 0 || home >= capacity) {
859
+ return false
860
+ }
861
+ const divisor: i64 = toI64(capacity)
862
+ if (divisor < 1) {
863
+ return false
864
+ }
689
865
  let free: i32 = -1
690
- for (let n: i32 = 0; n < this.capacity; n++) {
691
- const slot: i32 = (home + n) % this.capacity
692
- if (this.used[slot] === 0) {
866
+ for (let n: i32 = 0; n < capacity; n++) {
867
+ const slot: i32 = toI32((toI64(home) + toI64(n)) % divisor)
868
+ if (slot < 0 || slot >= toI32(used.length) || slot >= toI32(expires.length)) {
869
+ return false
870
+ }
871
+ if (used[slot] === 0) {
693
872
  if (free < 0) {
694
873
  free = slot
695
874
  }
696
875
  break
697
876
  }
698
- if (this.expires[slot] < now) {
877
+ if (expires[slot] < now) {
699
878
  if (free < 0) {
700
879
  free = slot
701
880
  }
702
- } else if (n <= this.longest && this.holds(slot, id)) {
881
+ } else if (n <= longest && this.holds(slot, id)) {
703
882
  return false
704
883
  }
705
- if (free >= 0 && n >= this.longest) {
884
+ if (free >= 0 && n >= longest) {
706
885
  break
707
886
  }
708
887
  }
709
888
  if (free < 0) {
710
- this.refusedFull = this.refusedFull + 1
889
+ this.refusedFull = tlsTicketAdd(this.refusedFull, 1)
711
890
  return false
712
891
  }
713
- if (this.used[free] === 0) {
714
- this.used[free] = 1
715
- this.filled = this.filled + 1
892
+ if (free >= toI32(used.length) || free >= toI32(expires.length) || free > 178956969) {
893
+ return false
716
894
  }
717
895
  const at: i32 = free * TLS_TICKET_NONCE_SIZE
896
+ if (at > toI32(ids.length) - TLS_TICKET_NONCE_SIZE) {
897
+ return false
898
+ }
899
+ if (used[free] === 0) {
900
+ used[free] = 1
901
+ const filled: i32 = this.filled
902
+ // `filled` counts slots ever used, at most `capacity`, so its bound never stops it.
903
+ if (filled < 2147483647) {
904
+ this.filled = filled + 1
905
+ }
906
+ }
718
907
  for (let k: i32 = 0; k < TLS_TICKET_NONCE_SIZE && k < toI32(id.length); k++) {
719
- this.ids[at + k] = id[k]
908
+ const j: i32 = toI32(toI64(at) + toI64(k))
909
+ if (j >= 0 && j < toI32(ids.length)) {
910
+ ids[j] = id[k]
911
+ }
720
912
  }
721
- this.expires[free] = until
722
- let distance: i32 = free - home
913
+ expires[free] = until
914
+ let distance: i64 = toI64(free) - toI64(home)
723
915
  if (distance < 0) {
724
- distance = distance + this.capacity
916
+ distance = distance + toI64(capacity)
725
917
  }
726
- if (distance > this.longest) {
727
- this.longest = distance
918
+ if (distance > toI64(longest)) {
919
+ this.longest = toI32(distance)
728
920
  }
729
921
  return true
730
922
  }
@@ -255,7 +255,7 @@ export const tlsClientChain = (chain: u8[][], serverName: string, nowMs: i64, tr
255
255
  */
256
256
  export const tlsClientVerdict = (tls: TlsClient, trust: TlsClientTrust, nowMs: i64): i32 => {
257
257
  const chain: u8[][] = tls.peerCertificates()
258
- if (toI32(chain.length) === 0) {
258
+ if (toI32(chain.length) < 1) {
259
259
  return TLS_ALERT_BAD_CERTIFICATE
260
260
  }
261
261
  const leaf: X509Certificate | null = x509ParseCertificate(chain[0])