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