@amritk/nish-aarch64-linux 0.38.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 (58) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +348 -37
  3. package/package.json +1 -1
  4. package/runtime/core/host.d.ts +11 -0
  5. package/runtime/core/host.mjs +35 -0
  6. package/runtime/nish.d.ts +301 -18
  7. package/runtime/nish.h +74 -10
  8. package/runtime/nish.mjs +69 -6
  9. package/runtime/node.mjs +117 -0
  10. package/runtime/runtime-aes.c +12 -4
  11. package/runtime/runtime-async.c +43 -10
  12. package/runtime/runtime-checksum.c +207 -15
  13. package/runtime/runtime-net.c +32 -3
  14. package/runtime/runtime-os.c +174 -27
  15. package/runtime/runtime-region.c +94 -10
  16. package/runtime/runtime-sandbox.c +377 -17
  17. package/runtime/runtime-sha256.c +11 -4
  18. package/runtime/runtime-wasm.c +15 -0
  19. package/runtime/runtime.c +5 -0
  20. package/scripts/build.sh +27 -8
  21. package/std/README.md +5 -4
  22. package/std/compress/checksum.ts +13 -0
  23. package/std/compress/deflate.ts +0 -2
  24. package/std/crypto/p256.ts +247 -74
  25. package/std/crypto/p384.ts +3 -1
  26. package/std/crypto/pki.ts +35 -9
  27. package/std/crypto/x25519.ts +153 -64
  28. package/std/crypto/x509.ts +113 -10
  29. package/std/json-tokenizer.ts +20 -0
  30. package/std/json-typed.ts +217 -8
  31. package/std/net/handler.ts +130 -16
  32. package/std/net/http1-codec.ts +499 -150
  33. package/std/net/http1-server.ts +495 -22
  34. package/std/net/http2-cleartext.ts +526 -0
  35. package/std/net/http2-tls.ts +48 -0
  36. package/std/net/http2.ts +84 -0
  37. package/std/net/postgres-async.ts +1304 -86
  38. package/std/net/postgres-carrier.ts +343 -10
  39. package/std/net/postgres.ts +122 -7
  40. package/std/net/prefork.ts +195 -11
  41. package/std/net/quic-datagram.ts +30 -0
  42. package/std/net/quic-listener.ts +38 -12
  43. package/std/net/quic-packet.ts +19 -6
  44. package/std/net/quic-recovery.ts +134 -19
  45. package/std/net/quic-stream.ts +174 -36
  46. package/std/net/quic.ts +418 -18
  47. package/std/net/router.ts +39 -17
  48. package/std/net/serve.ts +162 -58
  49. package/std/net/tls/codec.ts +110 -32
  50. package/std/net/tls/record-server.ts +37 -7
  51. package/std/net/tls-tcp.ts +56 -1
  52. package/std/net/tls.ts +196 -50
  53. package/std/net/websocket.ts +55 -18
  54. package/std/net/webtransport.ts +179 -1
  55. package/std/regex.ts +69 -47
  56. package/std/region.ts +62 -11
  57. package/std/secret.ts +26 -59
  58. package/std/text.ts +233 -0
package/std/crypto/pki.ts CHANGED
@@ -50,10 +50,12 @@
50
50
  * a root: one that reached a root beats any that did not, a longer one a
51
51
  * shorter, and the first found a later one of the same length.
52
52
  *
53
+ * An extension marked against RFC 5280 §4.2 refuses the chain, but for a
54
+ * non-critical nameConstraints, which binds as a critical one does.
53
55
  * Revocation (CRLs, OCSP, Certificate Transparency), certificate policies,
54
- * RFC 5280 §7.1's Name folding and an extension's criticality are not
55
- * checked; each is a finding of docs/security/crypto-pki.md. Nothing here is
56
- * secret — certificates, names and public keys — so nothing is wiped.
56
+ * RFC 5280 §7.1's Name folding and that nameConstraints are not checked;
57
+ * each is a finding of docs/security/crypto-pki.md. Nothing here is secret —
58
+ * certificates, names and public keys — so nothing is wiped.
57
59
  *
58
60
  * Written for this module from RFC 5280 (§4.2 and §6.1), RFC 6125 (§6.4),
59
61
  * RFC 4291 §2.2 and RFC 3986 §3.2.2 (address literals), RFC 5758, RFC 8017
@@ -852,12 +854,16 @@ const pkiTimeCode = (c: X509Certificate, nowMs: i64): i32 => {
852
854
  * The key identifiers an issuer `cand` must carry (RFC 5280 §4.2.1.1 and
853
855
  * §4.2.1.2), as a code: a subjectKeyIdentifier, which a root may lack, as
854
856
  * one Mozilla ships does, and an authorityKeyIdentifier unless it is
855
- * self-issued, even on a root. A self-issued root's authorityKeyIdentifier,
856
- * when present, is its own subjectKeyIdentifier.
857
+ * self-issued, even on a root. A root's authorityKeyIdentifier, when
858
+ * present, has a keyIdentifier and no authorityCertIssuer or serial, as
859
+ * webpki holds it; a self-issued root's is its own subjectKeyIdentifier.
857
860
  */
858
861
  const pkiKeyIdsCode = (cand: X509Certificate, root: boolean): i32 => {
859
862
  const skid: u8[] | null = cand.skid
860
863
  const akid: u8[] | null = cand.akid
864
+ if (root && cand.akiPresent && (akid === null || cand.akiIssuerSerial)) {
865
+ return PKI_ERR_MALFORMED
866
+ }
861
867
  if (skid === null) {
862
868
  return root ? PKI_OK : PKI_ERR_MALFORMED
863
869
  }
@@ -867,10 +873,18 @@ const pkiKeyIdsCode = (cand: X509Certificate, root: boolean): i32 => {
867
873
  return root && akid !== null && !pkiBytesEqual(akid, skid) ? PKI_ERR_MALFORMED : PKI_OK
868
874
  }
869
875
 
876
+ /**
877
+ * Whether the extensions RFC 5280 fixes the criticality of on every
878
+ * certificate are marked as it has them: authorityKeyIdentifier not critical
879
+ * (§4.2.1.1), policyConstraints critical (§4.2.1.11).
880
+ */
881
+ const pkiCriticalitySound = (c: X509Certificate): boolean => !c.akiCritical && (!c.pcPresent || c.pcCritical)
882
+
870
883
  /**
871
884
  * The checks of the issuer `cand` added above the path, a root of the store
872
885
  * when `root` holds and an intermediate otherwise: its validity, its critical
873
- * extensions, that it is a CA, its extKeyUsage, its name constraints, its
886
+ * extensions and the criticality RFC 5280 fixes, among them a critical
887
+ * basicConstraints (§4.2.1.9), that it is a CA, its extKeyUsage, its name constraints, its
874
888
  * signature over `child` — the only check that costs — and, once that
875
889
  * verifies, its key identifiers, so a wrong key reads as a signature. An
876
890
  * intermediate also needs room for a root above it and a keyUsage that
@@ -893,6 +907,12 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
893
907
  if (cand.version !== 2 || !cand.bcPresent || !cand.bcCa) {
894
908
  return PKI_ERR_NOT_CA
895
909
  }
910
+ // The root too: limbo refuses a root with a non-critical basicConstraints,
911
+ // and every root of Mozilla's store is marked as these rules ask
912
+ // (crypto-pki.md, "Each structural rule on a root").
913
+ if (!cand.bcCritical || !pkiCriticalitySound(cand)) {
914
+ return PKI_ERR_MALFORMED
915
+ }
896
916
  if (!root && cand.keyUsage >= 0 && (cand.keyUsage & X509_KU_KEY_CERT_SIGN) === 0) {
897
917
  return PKI_ERR_KEY_USAGE
898
918
  }
@@ -1042,7 +1062,9 @@ const pkiSubscriberKeyCode = (leaf: X509Certificate): i32 => {
1042
1062
  * The checks of the leaf's profile, once a path above it is found: no keyCertSign
1043
1063
  * and no nameConstraints, no `anyExtendedKeyUsage`, a key a server may
1044
1064
  * hold, a sound serial number, an authorityKeyIdentifier unless it is
1045
- * self-issued, and dNSNames of letters, digits and hyphens. The common name
1065
+ * self-issued, the criticality RFC 5280 fixes, a subjectAltName critical
1066
+ * exactly when the subject is empty (§4.2.1.6), an extKeyUsage that is not
1067
+ * critical, as webpki holds it, and dNSNames of letters, digits and hyphens. The common name
1046
1068
  * is not read: limbo's own passing cases carry ones their subjectAltName
1047
1069
  * lacks.
1048
1070
  */
@@ -1053,14 +1075,18 @@ const pkiLeafProfileCode = (leaf: X509Certificate): i32 => {
1053
1075
  if (leaf.ncPresent) {
1054
1076
  return PKI_ERR_NAME_CONSTRAINTS
1055
1077
  }
1056
- if (leaf.ekuAny) {
1078
+ if (leaf.ekuAny || leaf.ekuCritical) {
1057
1079
  return PKI_ERR_EXT_KEY_USAGE
1058
1080
  }
1059
1081
  const key: i32 = pkiSubscriberKeyCode(leaf)
1060
1082
  if (key !== PKI_OK) {
1061
1083
  return key
1062
1084
  }
1063
- if (!pkiSerialSound(leaf) || (leaf.akid === null && !pkiSelfIssued(leaf))) {
1085
+ if (!pkiSerialSound(leaf) || (leaf.akid === null && !pkiSelfIssued(leaf)) || !pkiCriticalitySound(leaf)) {
1086
+ return PKI_ERR_MALFORMED
1087
+ }
1088
+ // An empty subject is the empty SEQUENCE, `30 00`.
1089
+ if (leaf.sanPresent && leaf.sanCritical !== (leaf.subject.length === 2)) {
1064
1090
  return PKI_ERR_MALFORMED
1065
1091
  }
1066
1092
  for (const d of leaf.sanDns) {
@@ -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
@@ -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