@amritk/nish-aarch64-linux 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.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/tls/ticket.ts
CHANGED
|
@@ -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
|
-
|
|
169
|
-
|
|
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
|
|
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
|
|
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 &&
|
|
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
|
|
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
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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 +
|
|
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
|
-
|
|
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
|
-
/**
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
/**
|
|
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
|
-
|
|
306
|
-
|
|
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
|
-
/**
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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
|
|
361
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
input
|
|
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 (
|
|
403
|
-
|
|
404
|
-
out
|
|
405
|
-
out
|
|
406
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
474
|
-
|
|
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
|
|
603
|
+
tlsTicketElapsed(now, chainStart) <= toU64(TLS_TICKET_CHAIN_LIMIT)
|
|
482
604
|
)
|
|
483
605
|
}
|
|
484
606
|
|
|
485
|
-
/**
|
|
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
|
-
|
|
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
|
-
|
|
494
|
-
|
|
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
|
|
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 =
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
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
|
-
|
|
597
|
-
|
|
598
|
-
|
|
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
|
-
|
|
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 ^
|
|
758
|
+
hash = ((hash ^ toU64(id[k])) * 16777619) & 4294967295
|
|
614
759
|
}
|
|
615
|
-
return toI32(hash %
|
|
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
|
-
|
|
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
|
-
|
|
643
|
-
|
|
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
|
-
|
|
659
|
-
|
|
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
|
|
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 <
|
|
691
|
-
const slot: i32 = (home + n) %
|
|
692
|
-
if (
|
|
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 (
|
|
877
|
+
if (expires[slot] < now) {
|
|
699
878
|
if (free < 0) {
|
|
700
879
|
free = slot
|
|
701
880
|
}
|
|
702
|
-
} else if (n <=
|
|
881
|
+
} else if (n <= longest && this.holds(slot, id)) {
|
|
703
882
|
return false
|
|
704
883
|
}
|
|
705
|
-
if (free >= 0 && n >=
|
|
884
|
+
if (free >= 0 && n >= longest) {
|
|
706
885
|
break
|
|
707
886
|
}
|
|
708
887
|
}
|
|
709
888
|
if (free < 0) {
|
|
710
|
-
this.refusedFull = this.refusedFull
|
|
889
|
+
this.refusedFull = tlsTicketAdd(this.refusedFull, 1)
|
|
711
890
|
return false
|
|
712
891
|
}
|
|
713
|
-
if (
|
|
714
|
-
|
|
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
|
-
|
|
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
|
-
|
|
722
|
-
let distance:
|
|
913
|
+
expires[free] = until
|
|
914
|
+
let distance: i64 = toI64(free) - toI64(home)
|
|
723
915
|
if (distance < 0) {
|
|
724
|
-
distance = distance +
|
|
916
|
+
distance = distance + toI64(capacity)
|
|
725
917
|
}
|
|
726
|
-
if (distance >
|
|
727
|
-
this.longest = distance
|
|
918
|
+
if (distance > toI64(longest)) {
|
|
919
|
+
this.longest = toI32(distance)
|
|
728
920
|
}
|
|
729
921
|
return true
|
|
730
922
|
}
|
package/std/net/tls/verify.ts
CHANGED
|
@@ -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)
|
|
258
|
+
if (toI32(chain.length) < 1) {
|
|
259
259
|
return TLS_ALERT_BAD_CERTIFICATE
|
|
260
260
|
}
|
|
261
261
|
const leaf: X509Certificate | null = x509ParseCertificate(chain[0])
|