@amritk/nish-aarch64-linux 0.37.0 → 0.39.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 (73) hide show
  1. package/INSTALL.md +28 -16
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +380 -47
  4. package/package.json +1 -1
  5. package/runtime/core/arrays.d.ts +4 -0
  6. package/runtime/core/arrays.mjs +33 -0
  7. package/runtime/core/host.d.ts +11 -0
  8. package/runtime/core/host.mjs +35 -0
  9. package/runtime/nish.d.ts +645 -66
  10. package/runtime/nish.h +101 -15
  11. package/runtime/nish.mjs +69 -6
  12. package/runtime/node.mjs +117 -0
  13. package/runtime/runtime-aes.c +12 -4
  14. package/runtime/runtime-async.c +267 -51
  15. package/runtime/runtime-checksum.c +207 -15
  16. package/runtime/runtime-cross.c +2 -1
  17. package/runtime/runtime-net.c +32 -3
  18. package/runtime/runtime-os.c +174 -27
  19. package/runtime/runtime-region.c +170 -8
  20. package/runtime/runtime-sandbox.c +377 -17
  21. package/runtime/runtime-sha256.c +11 -4
  22. package/runtime/runtime-text.c +1068 -0
  23. package/runtime/runtime-wasm.c +18 -3
  24. package/runtime/runtime.c +55 -961
  25. package/runtime/vite.mjs +48 -11
  26. package/scripts/build.sh +113 -36
  27. package/std/LICENSE-fdlibm +8 -0
  28. package/std/README.md +7 -4
  29. package/std/array.ts +266 -0
  30. package/std/compress/checksum.ts +13 -0
  31. package/std/compress/deflate.ts +0 -2
  32. package/std/crypto/p256.ts +247 -74
  33. package/std/crypto/p384.ts +3 -1
  34. package/std/crypto/pki.ts +35 -9
  35. package/std/crypto/x25519.ts +153 -64
  36. package/std/crypto/x509.ts +113 -10
  37. package/std/host/text.ts +86 -0
  38. package/std/json-tokenizer.ts +20 -0
  39. package/std/json-typed.ts +217 -8
  40. package/std/math.ts +743 -41
  41. package/std/net/dns-resolver.ts +46 -6
  42. package/std/net/dns.ts +50 -13
  43. package/std/net/fetch.ts +691 -129
  44. package/std/net/handler.ts +142 -16
  45. package/std/net/http-compress.ts +17 -9
  46. package/std/net/http-fields.ts +19 -4
  47. package/std/net/http1-codec.ts +517 -154
  48. package/std/net/http1-server.ts +508 -25
  49. package/std/net/http2-cleartext.ts +526 -0
  50. package/std/net/http2-tls.ts +48 -0
  51. package/std/net/http2.ts +84 -0
  52. package/std/net/postgres-async.ts +2223 -0
  53. package/std/net/postgres-carrier.ts +343 -10
  54. package/std/net/postgres.ts +129 -11
  55. package/std/net/prefork.ts +195 -11
  56. package/std/net/quic-datagram.ts +30 -0
  57. package/std/net/quic-listener.ts +38 -12
  58. package/std/net/quic-packet.ts +19 -6
  59. package/std/net/quic-recovery.ts +134 -19
  60. package/std/net/quic-stream.ts +174 -36
  61. package/std/net/quic.ts +418 -18
  62. package/std/net/router.ts +39 -17
  63. package/std/net/serve.ts +867 -0
  64. package/std/net/tls/codec.ts +110 -32
  65. package/std/net/tls/record-server.ts +37 -7
  66. package/std/net/tls-tcp.ts +56 -1
  67. package/std/net/tls.ts +196 -50
  68. package/std/net/websocket.ts +55 -18
  69. package/std/net/webtransport.ts +179 -1
  70. package/std/regex.ts +69 -47
  71. package/std/region.ts +62 -11
  72. package/std/secret.ts +26 -59
  73. package/std/text.ts +464 -5
@@ -54,21 +54,21 @@
54
54
  * an input length.
55
55
  *
56
56
  * WP34 N6's disassembly check (`tests/ct-asm.js`) verifies the part a secret
57
- * flows through: `tests/cases/ct_asm_x25519` holds copies of `f25519Mul`,
58
- * `f25519Square`, `f25519MulA24`, `f25519Add`, `f25519Sub`, `f25519Swap` and
59
- * one pass of `x25519Ladder`'s loop, and the check reads each one's `clang -O2`
60
- * machine code for x86-64 and aarch64 and finds no branch and no load or store
61
- * at an address the scalar bit or a limb reaches.
62
- *
63
- * They are copies because the check refuses every branch, loop back-edges
64
- * included, and this module's own functions keep their loops and the early
65
- * `return`s below; the copies drop the guards and write `f25519Mul`'s outer
66
- * loop out a row at a time, and the fixture's `main` holds them to the same RFC
67
- * 7748 vectors as this module. A change to one of those functions belongs in
68
- * that fixture too. What stays discipline rather than proof is the rest: the
69
- * loops around the step, `f25519Decode`, `f25519Encode` and `f25519Invert`,
70
- * whose branches are all on public positions. Weekly, `tests/ct-timing.js`
71
- * times the fixture's functions on real x86-64 and aarch64 hardware as well.
57
+ * flows through: `tests/cases/ct_asm_x25519` holds copies of the field
58
+ * arithmetic, `f25519Swap`, the encodings and both halves of the ladder, and
59
+ * the check reads each one's `clang -O2` machine code for x86-64 and aarch64
60
+ * and finds no branch on a secret and no load or store at an address the
61
+ * scalar bit or a limb reaches. The rolled loops (the ladder's 255 steps, the
62
+ * inversion's square chains, the encodings) are read whole, back edge included.
63
+ *
64
+ * They are copies because a case compiles to one module. `tests/run.js` holds
65
+ * each to this module's text, with the early `return`s dropped (each guards a
66
+ * public length, but is a branch) and every index unchecked, so a change here
67
+ * that the copies miss fails that check. What stays outside the check is
68
+ * the allocation and wiping around the work in `x25519Ladder`, `f25519Decode`
69
+ * and `f25519Encode`, whose branches are all on public positions. Weekly,
70
+ * `tests/ct-timing.js` times the fixture's functions on real x86-64 and
71
+ * aarch64 hardware as well.
72
72
  *
73
73
  * **Loop bounds are literals.** A field element is always ten limbs and an
74
74
  * encoding always 32 bytes, and each function that indexes one opens with an
@@ -280,6 +280,24 @@ export const f25519Sub = (out: i64[], f: i64[], g: i64[]): void => {
280
280
  }
281
281
  }
282
282
 
283
+ /**
284
+ * Row `i` of `f25519Mul`'s product: `f[i]` (doubled where both limbs are odd)
285
+ * times each limb of `g`, added into `wide[i + j]`. The ten rows are written
286
+ * out rather than looped: `clang -O2` then keeps `wide` in registers and
287
+ * stack slots it can name, where a rolled row loop indexes it by a counter,
288
+ * and an indexed store into the frame is one tests/ct-asm.js cannot place
289
+ * (`docs/security/ct-verification.md`).
290
+ */
291
+ const f25519MulRow = (wide: i64[], f: i64[], doubled: i64[], g: i64[], i: i32): void => {
292
+ for (let j: i32 = 0; j < 10; j++) {
293
+ const k: i32 = i + j
294
+ const fi: i64 = (i & j & 1) === 1 ? doubled[i] : f[i]
295
+ if (k >= 0 && k < 19) {
296
+ wide[k] = f25519Add64(wide[k], f25519Mul64(fi, g[j]))
297
+ }
298
+ }
299
+ }
300
+
283
301
  /**
284
302
  * `out = f * g`, reduced. `out` may be `f` or `g`: the product is built in
285
303
  * scratch arrays and only copied out at the end.
@@ -312,17 +330,18 @@ export const f25519Mul = (out: i64[], f: i64[], g: i64[]): void => {
312
330
  for (let i: i32 = 0; i < 10; i++) {
313
331
  doubled[i] = f25519Mul64(f[i], toI64((i & 1) + 1))
314
332
  }
315
- // The plain product, limb `i + j` of nineteen.
333
+ // The plain product, limb `i + j` of nineteen, a row of `f` at a time.
316
334
  const wide: i64[] = new Array<i64>(19)
317
- for (let i: i32 = 0; i < 10; i++) {
318
- for (let j: i32 = 0; j < 10; j++) {
319
- const k: i32 = i + j
320
- const fi: i64 = (i & j & 1) === 1 ? doubled[i] : f[i]
321
- if (k >= 0 && k < 19) {
322
- wide[k] = f25519Add64(wide[k], f25519Mul64(fi, g[j]))
323
- }
324
- }
325
- }
335
+ f25519MulRow(wide, f, doubled, g, 0)
336
+ f25519MulRow(wide, f, doubled, g, 1)
337
+ f25519MulRow(wide, f, doubled, g, 2)
338
+ f25519MulRow(wide, f, doubled, g, 3)
339
+ f25519MulRow(wide, f, doubled, g, 4)
340
+ f25519MulRow(wide, f, doubled, g, 5)
341
+ f25519MulRow(wide, f, doubled, g, 6)
342
+ f25519MulRow(wide, f, doubled, g, 7)
343
+ f25519MulRow(wide, f, doubled, g, 8)
344
+ f25519MulRow(wide, f, doubled, g, 9)
326
345
  // Limbs 10 to 18 are past bit 255, and 2^255 = 19 (mod p): fold them down.
327
346
  const h: i64[] = new Array<i64>(10)
328
347
  for (let k: i32 = 0; k < 9; k++) {
@@ -488,6 +507,16 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
488
507
  if (toI32(bytes.length) < 32) {
489
508
  return f
490
509
  }
510
+ f25519DecodeInto(f, bytes)
511
+ return f
512
+ }
513
+
514
+ /**
515
+ * `f25519Decode`'s loop, into `f`: the 32 bytes of `bytes` streamed into ten
516
+ * limbs. Apart so that tests/cases/ct_asm_x25519 can read this loop in the
517
+ * assembly as the module has it (`docs/security/ct-verification.md`).
518
+ */
519
+ const f25519DecodeInto = (f: i64[], bytes: u8[]): void => {
491
520
  let acc: i64 = 0
492
521
  let bits: i64 = 0
493
522
  let limb: i32 = 0
@@ -508,7 +537,6 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
508
537
  }
509
538
  }
510
539
  }
511
- return f
512
540
  }
513
541
 
514
542
  /**
@@ -530,6 +558,16 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
530
558
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
531
559
  */
532
560
  export const f25519Encode = (f: i64[]): u8[] => {
561
+ const out: u8[] = new Array<u8>(32)
562
+ f25519EncodeInto(out, f)
563
+ return out
564
+ }
565
+
566
+ /**
567
+ * `f25519Encode`'s reduction and packing, into `out`, 32 bytes. Apart for the
568
+ * same reason as `f25519DecodeInto`.
569
+ */
570
+ const f25519EncodeInto = (out: u8[], f: i64[]): void => {
533
571
  const h: i64[] = new Array<i64>(10)
534
572
  f25519Copy(h, f)
535
573
  f25519Carry(h)
@@ -543,7 +581,6 @@ export const f25519Encode = (f: i64[]): u8[] => {
543
581
  f25519CarryChain(h)
544
582
  h[9] = h[9] & f25519LimbMask(9)
545
583
 
546
- const out: u8[] = new Array<u8>(32)
547
584
  let acc: i64 = 0
548
585
  let bits: i64 = 0
549
586
  let at: i32 = 0
@@ -561,7 +598,94 @@ export const f25519Encode = (f: i64[]): u8[] => {
561
598
  if (at >= 0 && at < 32) {
562
599
  out[at] = toU8(acc & toI64(0xff))
563
600
  }
564
- return out
601
+ }
602
+
603
+ /**
604
+ * One step of the ladder, from its two swaps on: `swap` is the bit the loop
605
+ * has just folded into its swap state, a secret that only feeds the swaps'
606
+ * masks. `a` to `t` are scratch, passed in so that `x25519Ladder` can wipe
607
+ * them (ECC-2).
608
+ */
609
+ const x25519LadderStep = (
610
+ x2: i64[],
611
+ z2: i64[],
612
+ x3: i64[],
613
+ z3: i64[],
614
+ u: i64[],
615
+ swap: i64,
616
+ a: i64[],
617
+ aa: i64[],
618
+ b: i64[],
619
+ bb: i64[],
620
+ e: i64[],
621
+ c: i64[],
622
+ d: i64[],
623
+ da: i64[],
624
+ cb: i64[],
625
+ t: i64[]
626
+ ): void => {
627
+ f25519Swap(x2, x3, swap)
628
+ f25519Swap(z2, z3, swap)
629
+ f25519Add(a, x2, z2)
630
+ f25519Square(aa, a)
631
+ f25519Sub(b, x2, z2)
632
+ f25519Square(bb, b)
633
+ f25519Sub(e, aa, bb)
634
+ f25519Add(c, x3, z3)
635
+ f25519Sub(d, x3, z3)
636
+ f25519Mul(da, d, a)
637
+ f25519Mul(cb, c, b)
638
+ f25519Add(t, da, cb)
639
+ f25519Square(x3, t)
640
+ f25519Sub(t, da, cb)
641
+ f25519Square(t, t)
642
+ f25519Mul(z3, u, t)
643
+ f25519Mul(x2, aa, bb)
644
+ f25519MulA24(t, e)
645
+ f25519Add(t, aa, t)
646
+ f25519Mul(z2, e, t)
647
+ }
648
+
649
+ /**
650
+ * The ladder's 255 steps on a clamped `k`, and the two swaps after them. The
651
+ * scratch is `x25519LadderStep`'s. Apart from `x25519Ladder` so that
652
+ * tests/cases/ct_asm_x25519 can read this loop in the assembly as the module
653
+ * has it (`docs/security/ct-verification.md`).
654
+ */
655
+ const x25519LadderLoop = (
656
+ x2: i64[],
657
+ z2: i64[],
658
+ x3: i64[],
659
+ z3: i64[],
660
+ u: i64[],
661
+ k: u8[],
662
+ a: i64[],
663
+ aa: i64[],
664
+ b: i64[],
665
+ bb: i64[],
666
+ e: i64[],
667
+ c: i64[],
668
+ d: i64[],
669
+ da: i64[],
670
+ cb: i64[],
671
+ t: i64[]
672
+ ): void => {
673
+ let swap: i64 = 0
674
+ // Bit 254 down to bit 0: the position is public and picks the byte and the
675
+ // shift; the bit itself is secret and only ever feeds a mask.
676
+ for (let byte: i32 = 31; byte >= 0; byte--) {
677
+ for (let shift: i32 = 7; shift >= 0; shift--) {
678
+ if (byte === 31 && shift === 7) {
679
+ continue
680
+ }
681
+ const bit: i64 = toI64(k[byte] >> toU8(shift)) & toI64(1)
682
+ swap = swap ^ bit
683
+ x25519LadderStep(x2, z2, x3, z3, u, swap, a, aa, b, bb, e, c, d, da, cb, t)
684
+ swap = bit
685
+ }
686
+ }
687
+ f25519Swap(x2, x3, swap)
688
+ f25519Swap(z2, z3, swap)
565
689
  }
566
690
 
567
691
  /**
@@ -594,42 +718,7 @@ const x25519Ladder = (k: u8[], u: i64[]): u8[] => {
594
718
  const cb: i64[] = f25519Zero()
595
719
  const t: i64[] = f25519Zero()
596
720
 
597
- let swap: i64 = 0
598
- // Bit 254 down to bit 0: the position is public and picks the byte and the
599
- // shift; the bit itself is secret and only ever feeds a mask.
600
- for (let byte: i32 = 31; byte >= 0; byte--) {
601
- for (let shift: i32 = 7; shift >= 0; shift--) {
602
- if (byte === 31 && shift === 7) {
603
- continue
604
- }
605
- const bit: i64 = toI64(k[byte] >> toU8(shift)) & toI64(1)
606
- swap = swap ^ bit
607
- f25519Swap(x2, x3, swap)
608
- f25519Swap(z2, z3, swap)
609
- swap = bit
610
-
611
- f25519Add(a, x2, z2)
612
- f25519Square(aa, a)
613
- f25519Sub(b, x2, z2)
614
- f25519Square(bb, b)
615
- f25519Sub(e, aa, bb)
616
- f25519Add(c, x3, z3)
617
- f25519Sub(d, x3, z3)
618
- f25519Mul(da, d, a)
619
- f25519Mul(cb, c, b)
620
- f25519Add(t, da, cb)
621
- f25519Square(x3, t)
622
- f25519Sub(t, da, cb)
623
- f25519Square(t, t)
624
- f25519Mul(z3, u, t)
625
- f25519Mul(x2, aa, bb)
626
- f25519MulA24(t, e)
627
- f25519Add(t, aa, t)
628
- f25519Mul(z2, e, t)
629
- }
630
- }
631
- f25519Swap(x2, x3, swap)
632
- f25519Swap(z2, z3, swap)
721
+ x25519LadderLoop(x2, z2, x3, z3, u, k, a, aa, b, bb, e, c, d, da, cb, t)
633
722
 
634
723
  f25519Invert(t, z2)
635
724
  f25519Mul(x2, x2, t)
@@ -37,8 +37,10 @@
37
37
  * the order of what follows the key (`[1]`, `[2]`, `[3]`), and decodes the
38
38
  * extensions a path validator needs: subjectAltName, basicConstraints,
39
39
  * keyUsage, extKeyUsage, nameConstraints and the two key identifiers. A
40
- * duplicate or malformed one refuses the certificate, and any other critical
41
- * one sets `unknownCritical`. It parses and does not validate: applying these
40
+ * duplicate or malformed one refuses the certificate, and each one's critical
41
+ * flag is kept. An authorityInfoAccess is held to its syntax but not kept, and
42
+ * any other critical extension sets `unknownCritical`. It parses and does not
43
+ * validate: applying these
42
44
  * to a chain is RFC 5280 §6 path validation, which `nish/crypto/pki`'s
43
45
  * `pkiVerifyServer` does. The mint writes two extensions of its own (below).
44
46
  *
@@ -307,6 +309,31 @@ export class X509Certificate {
307
309
  ncUnsupported: boolean = false
308
310
  /** Whether a critical extension is one this module does not decode: RFC 5280 §4.2 has a verifier refuse it. */
309
311
  unknownCritical: boolean = false
312
+ /** Whether an authorityKeyIdentifier extension is present (RFC 5280 §4.2.1.1), with or without its keyIdentifier. */
313
+ akiPresent: boolean = false
314
+ /** Whether the authorityKeyIdentifier carries authorityCertIssuer and authorityCertSerialNumber, which are read and not kept. */
315
+ akiIssuerSerial: boolean = false
316
+ /** Whether the authorityKeyIdentifier is marked critical, which RFC 5280 §4.2.1.1 forbids. */
317
+ akiCritical: boolean = false
318
+ /** Whether the subjectKeyIdentifier is marked critical, which RFC 5280 §4.2.1.2 forbids. */
319
+ skiCritical: boolean = false
320
+ /** Whether keyUsage is marked critical, as RFC 5280 §4.2.1.3 says it should be. */
321
+ kuCritical: boolean = false
322
+ /** Whether subjectAltName is marked critical: RFC 5280 §4.2.1.6 has it critical when the subject is empty, and not otherwise. */
323
+ sanCritical: boolean = false
324
+ /** Whether basicConstraints is marked critical, which RFC 5280 §4.2.1.9 requires of a CA. */
325
+ bcCritical: boolean = false
326
+ /** Whether extKeyUsage is marked critical, which RFC 5280 §4.2.1.12 allows and the Web PKI does not. */
327
+ ekuCritical: boolean = false
328
+ /** Whether nameConstraints is marked critical, which RFC 5280 §4.2.1.10 requires. */
329
+ ncCritical: boolean = false
330
+ /**
331
+ * Whether a policyConstraints extension (RFC 5280 §4.2.1.11) is present. It
332
+ * is not decoded, so a critical one also sets `unknownCritical`.
333
+ */
334
+ pcPresent: boolean = false
335
+ /** Whether policyConstraints is marked critical, which RFC 5280 §4.2.1.11 requires. */
336
+ pcCritical: boolean = false
310
337
 
311
338
  /**
312
339
  * A certificate with every field empty, zero, `false` or `null`, and
@@ -669,12 +696,16 @@ const X509_CE_BASIC_CONSTRAINTS: i32 = 19
669
696
  const X509_CE_NAME_CONSTRAINTS: i32 = 30
670
697
  const X509_CE_AUTHORITY_KEY_ID: i32 = 35
671
698
  const X509_CE_EXT_KEY_USAGE: i32 = 37
699
+ const X509_CE_POLICY_CONSTRAINTS: i32 = 36
672
700
 
673
701
  /** What `x509ReadExtension` answers: an extension it does not decode, one it decoded, one that is malformed. */
674
702
  const X509_EXT_UNKNOWN: i32 = 0
675
703
  const X509_EXT_SOUND: i32 = 1
676
704
  const X509_EXT_MALFORMED: i32 = 2
677
705
 
706
+ /** id-pe-authorityInfoAccess, 1.3.6.1.5.5.7.1.1 (RFC 5280 §4.2.2.1). */
707
+ const x509OidAuthorityInfoAccess = (): u8[] => x509DerOidContents([1, 3, 6, 1, 5, 5, 7, 1, 1])
708
+
678
709
  /** id-kp-serverAuth, 1.3.6.1.5.5.7.3.1 (RFC 5280 §4.2.1.12). */
679
710
  const x509OidServerAuth = (): u8[] => x509DerOidContents([1, 3, 6, 1, 5, 5, 7, 3, 1])
680
711
 
@@ -1883,13 +1914,15 @@ const x509GeneralNamesSound = (der: u8[], e: X509DerElement): boolean => {
1883
1914
  * OPTIONAL }`, its keyIdentifier, which may not be empty, into `out.akid`.
1884
1915
  * The other two are held to their order, to being both present or both
1885
1916
  * absent, as §4.2.1.1 has them, to a non-empty GeneralNames and a minimal
1886
- * INTEGER, and not kept. `false` when it is malformed.
1917
+ * INTEGER, and not kept: `out.akiIssuerSerial` says they are there. `false`
1918
+ * when it is malformed.
1887
1919
  */
1888
1920
  const x509ReadAuthorityKeyId = (der: u8[], start: i32, end: i32, out: X509Certificate): boolean => {
1889
1921
  const seq: X509DerElement | null = x509DerExpect(der, start, end, X509_TAG_SEQUENCE)
1890
1922
  if (seq === null || seq.end !== end) {
1891
1923
  return false
1892
1924
  }
1925
+ out.akiPresent = true
1893
1926
  let at: i32 = seq.start
1894
1927
  if (x509DerPeek(der, at, seq.end) === 0x80) {
1895
1928
  const id: X509DerElement | null = x509DerRead(der, at, seq.end)
@@ -1911,11 +1944,66 @@ const x509ReadAuthorityKeyId = (der: u8[], start: i32, end: i32, out: X509Certif
1911
1944
  ) {
1912
1945
  return false
1913
1946
  }
1947
+ out.akiIssuerSerial = true
1914
1948
  at = serial.end
1915
1949
  }
1916
1950
  return at === seq.end
1917
1951
  }
1918
1952
 
1953
+ /**
1954
+ * Whether authorityInfoAccess (RFC 5280 §4.2.2.1) is well formed: a non-empty
1955
+ * `SEQUENCE OF AccessDescription { accessMethod OID, accessLocation
1956
+ * GeneralName }`, each a sound OID and a sound GeneralName, and nothing after
1957
+ * it. Nothing is kept: no access method is followed.
1958
+ */
1959
+ const x509AuthorityInfoAccessSound = (der: u8[], start: i32, end: i32): boolean => {
1960
+ const seq: X509DerElement | null = x509DerExpect(der, start, end, X509_TAG_SEQUENCE)
1961
+ if (seq === null || seq.end !== end || seq.end === seq.start) {
1962
+ return false
1963
+ }
1964
+ let at: i32 = seq.start
1965
+ while (at < seq.end) {
1966
+ const desc: X509DerElement | null = x509DerExpect(der, at, seq.end, X509_TAG_SEQUENCE)
1967
+ const method: X509DerElement | null =
1968
+ desc === null ? null : x509DerExpect(der, desc.start, desc.end, X509_TAG_OID)
1969
+ const location: X509DerElement | null =
1970
+ desc === null || method === null ? null : x509DerRead(der, method.end, desc.end)
1971
+ if (
1972
+ desc === null ||
1973
+ method === null ||
1974
+ location === null ||
1975
+ location.end !== desc.end ||
1976
+ !x509DerOidSound(der, method) ||
1977
+ x509GeneralNameKind(der, location) === -1
1978
+ ) {
1979
+ return false
1980
+ }
1981
+ at = desc.end
1982
+ }
1983
+ return true
1984
+ }
1985
+
1986
+ /** Notes in `out` that the extension at arc `ce` under 2.5.29, one this module decodes or policyConstraints, is marked critical. */
1987
+ const x509NoteCritical = (ce: i32, out: X509Certificate): void => {
1988
+ if (ce === X509_CE_SUBJECT_ALT_NAME) {
1989
+ out.sanCritical = true
1990
+ } else if (ce === X509_CE_BASIC_CONSTRAINTS) {
1991
+ out.bcCritical = true
1992
+ } else if (ce === X509_CE_KEY_USAGE) {
1993
+ out.kuCritical = true
1994
+ } else if (ce === X509_CE_EXT_KEY_USAGE) {
1995
+ out.ekuCritical = true
1996
+ } else if (ce === X509_CE_NAME_CONSTRAINTS) {
1997
+ out.ncCritical = true
1998
+ } else if (ce === X509_CE_SUBJECT_KEY_ID) {
1999
+ out.skiCritical = true
2000
+ } else if (ce === X509_CE_AUTHORITY_KEY_ID) {
2001
+ out.akiCritical = true
2002
+ } else if (ce === X509_CE_POLICY_CONSTRAINTS) {
2003
+ out.pcCritical = true
2004
+ }
2005
+ }
2006
+
1919
2007
  /**
1920
2008
  * One extension's value, the contents of its extnValue OCTET STRING, decoded
1921
2009
  * into `out` by the arc `ce` it has under 2.5.29: `X509_EXT_SOUND`, or
@@ -1960,9 +2048,12 @@ const x509ExtensionArc = (der: u8[], id: X509DerElement): i32 => {
1960
2048
  * FALSE, extnValue OCTET STRING }` and nothing after it. `false` when any is
1961
2049
  * malformed, a critical flag is not DER's TRUE, an OID appears twice (§4.2:
1962
2050
  * "MUST NOT include more than one instance"), there are more than
1963
- * `X509_MAX_EXTENSIONS`, or a decoded extension is malformed. A critical
1964
- * extension this module does not decode sets `out.unknownCritical`; a
1965
- * non-critical one is skipped.
2051
+ * `X509_MAX_EXTENSIONS`, a decoded extension is malformed, or an
2052
+ * authorityInfoAccess is not `x509AuthorityInfoAccessSound`. A decoded
2053
+ * extension's critical flag is kept (`sanCritical` and the rest), and so is
2054
+ * policyConstraints' presence and flag. A critical extension this module does
2055
+ * not decode, authorityInfoAccess and policyConstraints among them, sets
2056
+ * `out.unknownCritical`; a non-critical one is skipped.
1966
2057
  */
1967
2058
  const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate): boolean => {
1968
2059
  const seq: X509DerElement | null = x509DerExpect(der, e.start, e.end, X509_TAG_SEQUENCE)
@@ -1977,6 +2068,7 @@ const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate):
1977
2068
  const id = new X509DerElement(0, 0, 0, 0)
1978
2069
  const flag = new X509DerElement(0, 0, 0, 0)
1979
2070
  const value = new X509DerElement(0, 0, 0, 0)
2071
+ const aia: u8[] = x509OidAuthorityInfoAccess()
1980
2072
  let at: i32 = seq.start
1981
2073
  while (at < seq.end) {
1982
2074
  if (
@@ -2007,10 +2099,20 @@ const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate):
2007
2099
  if (!x509DerExpectInto(der, next, ext.end, X509_TAG_OCTET_STRING, value) || value.end !== ext.end) {
2008
2100
  return false
2009
2101
  }
2010
- const read: i32 = x509ReadExtension(der, x509ExtensionArc(der, id), value.start, value.end, out)
2102
+ const ce: i32 = x509ExtensionArc(der, id)
2103
+ const read: i32 = x509ReadExtension(der, ce, value.start, value.end, out)
2011
2104
  if (read === X509_EXT_MALFORMED) {
2012
2105
  return false
2013
2106
  }
2107
+ if (x509DerContentsAre(der, id, aia) && !x509AuthorityInfoAccessSound(der, value.start, value.end)) {
2108
+ return false
2109
+ }
2110
+ if (ce === X509_CE_POLICY_CONSTRAINTS) {
2111
+ out.pcPresent = true
2112
+ }
2113
+ if (critical) {
2114
+ x509NoteCritical(ce, out)
2115
+ }
2014
2116
  if (read === X509_EXT_UNKNOWN) {
2015
2117
  out.unknownCritical = out.unknownCritical || critical
2016
2118
  }
@@ -2036,9 +2138,10 @@ const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate):
2036
2138
  *
2037
2139
  * Extensions are read: subjectAltName, basicConstraints, keyUsage,
2038
2140
  * extKeyUsage, nameConstraints and the two key identifiers are decoded into
2039
- * their fields, and `null` is the answer when any of them is malformed or any
2040
- * extension appears twice. A critical extension it does not decode sets
2041
- * `unknownCritical`, which RFC 5280 §4.2 obliges a validator to refuse.
2141
+ * their fields with their critical flags, and `null` is the answer when any of
2142
+ * them, or an authorityInfoAccess, is malformed or any extension appears
2143
+ * twice. A critical extension it does not decode sets `unknownCritical`, which
2144
+ * RFC 5280 §4.2 obliges a validator to refuse.
2042
2145
  *
2043
2146
  * **A parser, not a validator.** It checks that the bytes are one certificate
2044
2147
  * and what its extensions say. It does not check the signature
@@ -0,0 +1,86 @@
1
+ /**
2
+ * `nish/host/text` — text in module storage, for a wasm module and the page
3
+ * that drives it (`docs/wp50-wasm-host.md` §7).
4
+ *
5
+ * A string crosses out of a module as an export's result or a host import's
6
+ * argument, but an export cannot take one: the page writes text into a `u8[]`
7
+ * the module declares as module storage and passes the byte count. These are
8
+ * the module's halves of both directions:
9
+ *
10
+ * import { textRead, textWrite, textWriteInt } from "nish/host/text";
11
+ *
12
+ * export const INBOX = new Array<u8>(256);
13
+ * export const OUTBOX = new Array<u8>(256);
14
+ *
15
+ * // The page: n = new TextEncoder().encodeInto(text, views.INBOX).written
16
+ * export function echo(n: i32): i32 {
17
+ * const text = textRead(INBOX, n);
18
+ * if (text === null) return -1;
19
+ * return textWriteInt(OUTBOX, textWrite(OUTBOX, 0, text), toI64(n));
20
+ * }
21
+ * // The page: new TextDecoder().decode(views.OUTBOX.subarray(0, count))
22
+ *
23
+ * A writer takes the offset to write at and returns the offset after what it
24
+ * wrote, so writes chain. Text that does not fit writes nothing and returns
25
+ * -1, and every writer passes a -1 offset straight through, so a chain that
26
+ * overflowed anywhere ends in -1 with the bytes before the failed write left
27
+ * as they were. Nothing here traps on what a page wrote.
28
+ */
29
+
30
+ /**
31
+ * Writes `s` as its UTF-8 bytes at offset `at`, or nothing when it does not fit.
32
+ *
33
+ * @param buf - the text's storage
34
+ * @param at - the offset to write at; a negative one is an earlier failure
35
+ * @param s - the text
36
+ * @returns the offset after the text, or -1 when `at` is negative or past the
37
+ * buffer, or the text does not fit
38
+ */
39
+ export const textWrite = (buf: u8[], at: i32, s: string): i32 => {
40
+ const length: i32 = toI32(buf.length)
41
+ const n: i32 = toI32(s.length)
42
+ if (at < 0 || at > length || n > length - at) {
43
+ return -1
44
+ }
45
+ for (let i: i32 = 0; i < n; i++) {
46
+ buf[at + i] = toU8(s.charCodeAt(i))
47
+ }
48
+ return at + n
49
+ }
50
+
51
+ /**
52
+ * Writes `n` in decimal, as a template literal formats it.
53
+ *
54
+ * @param buf - the text's storage
55
+ * @param at - the offset to write at; a negative one is an earlier failure
56
+ * @param n - the integer
57
+ * @returns the offset after the digits, or -1 as `textWrite` answers it
58
+ */
59
+ export const textWriteInt = (buf: u8[], at: i32, n: i64): i32 => textWrite(buf, at, `${n}`)
60
+
61
+ /**
62
+ * Writes `x` in its shortest round-tripping form, as a template literal
63
+ * formats it: `0.30000000000000004`, `1e+21`, `NaN`, `-Infinity`.
64
+ *
65
+ * @param buf - the text's storage
66
+ * @param at - the offset to write at; a negative one is an earlier failure
67
+ * @param x - the number
68
+ * @returns the offset after the text, or -1 as `textWrite` answers it
69
+ */
70
+ export const textWriteF64 = (buf: u8[], at: i32, x: f64): i32 => textWrite(buf, at, `${x}`)
71
+
72
+ /**
73
+ * The first `n` bytes of `buf` as a string: what a page's `encodeInto` wrote.
74
+ * The bytes are not checked as UTF-8; a sequence that is not becomes U+FFFD
75
+ * when the string next crosses to the page.
76
+ *
77
+ * @param buf - the text's storage
78
+ * @param n - the byte count the page passed
79
+ * @returns the string, or null when `n` is negative or past the buffer
80
+ */
81
+ export const textRead = (buf: readonly u8[], n: i32): string | null => {
82
+ if (n < 0 || n > toI32(buf.length)) {
83
+ return null
84
+ }
85
+ return String.fromBytes(buf, 0, n)
86
+ }
@@ -1472,6 +1472,26 @@ export class JsonTokenizer {
1472
1472
  return parseFloat(String.fromBytes(src, start, end))
1473
1473
  }
1474
1474
 
1475
+ /**
1476
+ * Goes back to just after the `{` at `start`, the opener of the innermost
1477
+ * open object, so that the next `next()` answers that object's first key
1478
+ * again. The decoder of a tagged union reads ahead to the tag and then
1479
+ * decodes the object from its start; the bytes between are read twice.
1480
+ * Call it only while that object is still the innermost one open, and
1481
+ * before any error: the depth stack is as it was when the `{` was read.
1482
+ *
1483
+ * @param start - the byte offset of the object's `{`, its `tokStart`.
1484
+ *
1485
+ * @internal Called by `JsonDecoder.enterUnion`, std/json-typed.ts.
1486
+ */
1487
+ rewindObject(start: i32): void {
1488
+ this.kind = JSON_TOK_OBJECT_BEGIN
1489
+ this.tokStart = start
1490
+ this.tokEnd = start + 1
1491
+ this.pos = start + 1
1492
+ this.want = JSON_TOK_WANT_KEY_OR_CLOSE
1493
+ }
1494
+
1475
1495
  /**
1476
1496
  * Steps over the value the current token starts, so that the next `next()`
1477
1497
  * answers what follows it. After a key, that is the key's value; after an