@blamejs/core 0.18.11 → 0.18.13

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.
@@ -1,4 +1,4 @@
1
- // @blamejs/pki v0.3.29 — vendored (Apache-2.0). Zero-dep pure CJS.
1
+ // @blamejs/pki v0.3.32 — vendored (Apache-2.0). Zero-dep pure CJS.
2
2
  // https://github.com/blamejs/pki Exports: x509, crl, pkcs12, key, webcrypto, schema, csr, cms, ...
3
3
  // Backs lib/mtls-engine-default.js (PQC-capable CA + PKCS#12 engine).
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -137,7 +137,7 @@ var require_package = __commonJS({
137
137
  "node_modules/@blamejs/pki/package.json"(exports2, module2) {
138
138
  module2.exports = {
139
139
  name: "@blamejs/pki",
140
- version: "0.3.29",
140
+ version: "0.3.32",
141
141
  description: "Pure-JavaScript PKI toolkit that owns its stack \u2014 X.509, ASN.1/DER, CMS, PQC-first.",
142
142
  license: "Apache-2.0",
143
143
  author: "blamejs contributors",
@@ -2494,6 +2494,10 @@ var require_oid = __commonJS({
2494
2494
  aaControls: 6,
2495
2495
  acProxying: 10,
2496
2496
  acmeIdentifier: 31,
2497
+ // id-pe-subjectInfoAccess (RFC 5280 sec. 4.2.2.2) -- the SIA extension companion to the AIA.
2498
+ subjectInfoAccess: 11,
2499
+ // id-pe-tlsfeature (RFC 7633) -- the TLS Feature (formerly "must-staple") extension.
2500
+ tlsFeature: 24,
2497
2501
  // id-pe-qcStatements (RFC 3739 sec. 3.2.6) -- the qualified-certificate-statements extension.
2498
2502
  qcStatements: 3
2499
2503
  } },
@@ -2540,8 +2544,23 @@ var require_oid = __commonJS({
2540
2544
  selectedAttrType: { base: [2, 5, 1, 5], of: { clearance: 55 } },
2541
2545
  // PKIX Access Descriptor methods (id-ad, RFC 5280 sec. 4.2.2.1/sec. 4.2.2.2). id-ad-ocsp
2542
2546
  // is the arc the OCSP responder OIDs hang under; id-ad-caIssuers names the AIA
2543
- // CA-issuers access method.
2544
- adAccess: { base: [1, 3, 6, 1, 5, 5, 7, 48], of: { ocsp: 1, caIssuers: 2 } },
2547
+ // CA-issuers access method. The remaining id-ad methods (timeStamping RFC 3161, caRepository
2548
+ // + rpkiManifest/signedObject/rpkiNotify RFC 6487/8182) complete the C509 Information Access
2549
+ // registry (draft-ietf-cose-cbor-encoded-cert sec. 8.11); they carry the id-ad- prefix so the
2550
+ // name->OID reverse stays unambiguous next to the id-kp "timeStamping" purpose (a distinct arc).
2551
+ adAccess: { base: [1, 3, 6, 1, 5, 5, 7, 48], of: {
2552
+ ocsp: 1,
2553
+ caIssuers: 2,
2554
+ "id-ad-timeStamping": 3,
2555
+ "id-ad-caRepository": 5,
2556
+ "id-ad-rpkiManifest": 10,
2557
+ "id-ad-signedObject": 11,
2558
+ "id-ad-rpkiNotify": 13
2559
+ } },
2560
+ // id-on -- RFC 5280 sec. 4.2.1.6 otherName type-ids the C509 sec. 8.13 registry gives their own
2561
+ // negative ints: id-on-hardwareModuleName (RFC 4108), id-on-SmtpUTF8Mailbox (RFC 9598),
2562
+ // id-on-MACAddress (I-D.ietf-lamps-macaddress-on) on the id-pkix 8 arc.
2563
+ pkixOn: { base: [1, 3, 6, 1, 5, 5, 7, 8], of: { hardwareModuleName: 4, smtpUtf8Mailbox: 9, macAddress: 12 } },
2545
2564
  // OCSP (RFC 6960) on the id-pkix-ocsp arc (= id-ad-ocsp). id-pkix-ocsp-basic is
2546
2565
  // the ResponseBytes.responseType this build decodes; id-pkix-ocsp-nonce (sec. 4.4.1)
2547
2566
  // names the nonce extension; the remaining members name the other OCSP extensions
@@ -2572,15 +2591,29 @@ var require_oid = __commonJS({
2572
2591
  } },
2573
2592
  regInfo: { base: [1, 3, 6, 1, 5, 5, 7, 5, 2], of: { utf8Pairs: 1, certReq: 2 } },
2574
2593
  // PKIX extended key purposes (id-kp, RFC 5280 sec. 4.2.1.12). timeStamping is required
2575
- // -- critical and sole -- on an RFC 3161 TSA signing certificate (sec. 2.3).
2594
+ // -- critical and sole -- on an RFC 3161 TSA signing certificate (sec. 2.3). The SSH
2595
+ // (RFC 6187), CMC (RFC 6402), and Bundle Security (RFC 9174) purposes complete the C509
2596
+ // Extended Key Usages registry (draft-ietf-cose-cbor-encoded-cert sec. 8.12).
2576
2597
  pkixKp: { base: [1, 3, 6, 1, 5, 5, 7, 3], of: {
2577
2598
  serverAuth: 1,
2578
2599
  clientAuth: 2,
2579
2600
  codeSigning: 3,
2580
2601
  emailProtection: 4,
2581
2602
  timeStamping: 8,
2582
- ocspSigning: 9
2603
+ ocspSigning: 9,
2604
+ secureShellClient: 21,
2605
+ secureShellServer: 22,
2606
+ cmcCA: 27,
2607
+ cmcRA: 28,
2608
+ cmcArchive: 29,
2609
+ cmKGA: 32,
2610
+ bundleSecurity: 35
2583
2611
  } },
2612
+ // Kerberos PKINIT extended key purposes (id-pkinit, RFC 4556 sec. 3.2.3 / 3.2.4): the client
2613
+ // and KDC authentication EKUs (C509 Extended Key Usages registry integers 10 / 11).
2614
+ pkinitKp: { base: [1, 3, 6, 1, 5, 2, 3], of: { pkinitClientAuth: 4, pkinitKdc: 5 } },
2615
+ // Wi-SUN FAN device extended key purpose (C509 Extended Key Usages registry integer 20).
2616
+ wisun: { base: [1, 3, 6, 1, 4, 1, 45605], of: { fanDevice: 1 } },
2584
2617
  // Google Certificate Transparency (RFC 6962) on the 1.3.6.1.4.1.11129.2.4 arc:
2585
2618
  // the SCT-list X.509 extension (sec. 3.3), the precertificate poison (sec. 3.1), the
2586
2619
  // precert-signing EKU (sec. 3.1, naming only), and the OCSP-delivered SCT list
@@ -7420,6 +7453,8 @@ var require_schema_c509 = __commonJS({
7420
7453
  var oid = require_oid();
7421
7454
  var x509 = require_schema_x509();
7422
7455
  var pkix = require_schema_pkix();
7456
+ var schema = require_schema_engine();
7457
+ var guard = require_guard_all();
7423
7458
  var constants = require_constants();
7424
7459
  var frameworkError = require_framework_error();
7425
7460
  var validator = require_validator_all();
@@ -7455,8 +7490,9 @@ var require_schema_c509 = __commonJS({
7455
7490
  2: _name("surname"),
7456
7491
  3: _name("serialNumber"),
7457
7492
  4: _name("countryName"),
7458
- 6: _name("localityName"),
7459
- 7: _name("stateOrProvinceName"),
7493
+ 5: _name("localityName"),
7494
+ 6: _name("stateOrProvinceName"),
7495
+ 7: _name("streetAddress"),
7460
7496
  8: _name("organizationName"),
7461
7497
  9: _name("organizationalUnitName"),
7462
7498
  10: _name("title")
@@ -7466,9 +7502,79 @@ var require_schema_c509 = __commonJS({
7466
7502
  2: _name("keyUsage"),
7467
7503
  3: _name("subjectAltName"),
7468
7504
  4: _name("basicConstraints"),
7469
- 7: _name("keyUsage"),
7470
- 10: _name("authorityKeyIdentifier")
7505
+ 5: _name("cRLDistributionPoints"),
7506
+ 7: _name("authorityKeyIdentifier"),
7507
+ 8: _name("extKeyUsage"),
7508
+ 9: _name("authorityInfoAccess"),
7509
+ 25: _name("issuerAltName"),
7510
+ 26: _name("nameConstraints"),
7511
+ 29: _name("freshestCRL"),
7512
+ 30: _name("inhibitAnyPolicy"),
7513
+ 31: _name("subjectInfoAccess"),
7514
+ 36: _name("ocspNoCheck"),
7515
+ 38: _name("tlsFeature")
7516
+ };
7517
+ var EXT_COMPACT = {
7518
+ subjectKeyIdentifier: 1,
7519
+ keyUsage: 1,
7520
+ basicConstraints: 1,
7521
+ authorityKeyIdentifier: 1,
7522
+ extKeyUsage: 1,
7523
+ inhibitAnyPolicy: 1,
7524
+ ocspNoCheck: 1,
7525
+ tlsFeature: 1,
7526
+ subjectAltName: 1,
7527
+ issuerAltName: 1,
7528
+ nameConstraints: 1,
7529
+ cRLDistributionPoints: 1,
7530
+ freshestCRL: 1,
7531
+ authorityInfoAccess: 1,
7532
+ subjectInfoAccess: 1
7533
+ };
7534
+ var EKU_BY_INT = {
7535
+ 0: _name("anyExtendedKeyUsage"),
7536
+ 1: _name("serverAuth"),
7537
+ 2: _name("clientAuth"),
7538
+ 3: _name("codeSigning"),
7539
+ 4: _name("emailProtection"),
7540
+ 8: _name("timeStamping"),
7541
+ 9: _name("ocspSigning"),
7542
+ 10: _name("pkinitClientAuth"),
7543
+ 11: _name("pkinitKdc"),
7544
+ 12: _name("secureShellClient"),
7545
+ 13: _name("secureShellServer"),
7546
+ 14: _name("bundleSecurity"),
7547
+ 15: _name("cmcCA"),
7548
+ 16: _name("cmcRA"),
7549
+ 17: _name("cmcArchive"),
7550
+ 18: _name("cmKGA"),
7551
+ 20: _name("fanDevice")
7552
+ };
7553
+ var EKU_TO_INT = {};
7554
+ Object.keys(EKU_BY_INT).forEach(function(k) {
7555
+ EKU_TO_INT[oid.byName(EKU_BY_INT[k])] = Number(k);
7556
+ });
7557
+ var NS = pkix.makeNS("c509", C509Error, oid);
7558
+ var GN_LEAF = pkix.generalName(NS, { decodeValue: true });
7559
+ var GN_LEAF_SUBTREE = pkix.generalName(NS, { decodeValue: true, subtreeBase: true });
7560
+ var ON_NAME_BY_INT = { "-1": _name("hardwareModuleName"), "-2": _name("smtpUtf8Mailbox"), "-3": _name("macAddress") };
7561
+ var ON_INT_BY_OID = {};
7562
+ Object.keys(ON_NAME_BY_INT).forEach(function(k) {
7563
+ ON_INT_BY_OID[oid.byName(ON_NAME_BY_INT[k])] = Number(k);
7564
+ });
7565
+ var IA_BY_INT = {
7566
+ 1: _name("ocsp"),
7567
+ 2: _name("caIssuers"),
7568
+ 3: _name("id-ad-timeStamping"),
7569
+ 5: _name("id-ad-caRepository"),
7570
+ 10: _name("id-ad-rpkiManifest"),
7571
+ 11: _name("id-ad-signedObject"),
7572
+ 13: _name("id-ad-rpkiNotify")
7471
7573
  };
7574
+ var IA_TO_INT = {};
7575
+ Object.keys(IA_BY_INT).forEach(function(k) {
7576
+ IA_TO_INT[oid.byName(IA_BY_INT[k])] = Number(k);
7577
+ });
7472
7578
  function _biguint(node, code, label) {
7473
7579
  if (!node || node.majorType !== 2) throw _err(code, label + " must be an unwrapped CBOR byte string (~biguint)");
7474
7580
  var b2 = node.content;
@@ -7478,7 +7584,7 @@ var require_schema_c509 = __commonJS({
7478
7584
  }
7479
7585
  function _time(node, allowNull, label) {
7480
7586
  if (allowNull && node.majorType === 7 && node.ai === 22) return null;
7481
- if (!node || node.majorType !== 0) throw _err("c509/bad-validity", label + " must be an unwrapped CBOR epoch integer (~time)");
7587
+ if (node.majorType !== 0) throw _err("c509/bad-validity", label + " must be an unwrapped CBOR epoch integer (~time)");
7482
7588
  var secs = node.argument;
7483
7589
  if (secs > MAX_EPOCH_SECONDS) throw _err("c509/bad-validity", label + " is outside the representable Date range");
7484
7590
  return new Date(C.TIME.seconds(Number(secs)));
@@ -7539,6 +7645,7 @@ var require_schema_c509 = __commonJS({
7539
7645
  var kids = node.children || [];
7540
7646
  if (kids.length % 2 !== 0) throw _err("c509/bad-name", "a C509 Name array must be attribute-type/value pairs (dangling attribute type)");
7541
7647
  for (var i = 0; i + 1 < kids.length; i += 2) {
7648
+ if (kids[i].majorType !== 0 && kids[i].majorType !== 1) throw _err("c509/bad-name", "a C509 Name attribute type must be a CBOR integer");
7542
7649
  var ti = Number(cbor.read.int(kids[i]));
7543
7650
  var tname = ATTR_BY_INT[Math.abs(ti)];
7544
7651
  if (tname === void 0) throw _err("c509/bad-name", "attribute type integer " + ti + " has no C509 registry row");
@@ -7552,6 +7659,532 @@ var require_schema_c509 = __commonJS({
7552
7659
  function _shortName(n) {
7553
7660
  return n === "commonName" ? "CN" : n === "countryName" ? "C" : n === "organizationName" ? "O" : n === "organizationalUnitName" ? "OU" : n === "localityName" ? "L" : n === "stateOrProvinceName" ? "ST" : n;
7554
7661
  }
7662
+ function _cborUint(node, label) {
7663
+ if (node.majorType !== 0) throw _err("c509/bad-extensions", "a " + label + " value must be a CBOR unsigned integer");
7664
+ return cbor.read.int(node);
7665
+ }
7666
+ function _cborIntVal(node, label) {
7667
+ if (node.majorType !== 0 && node.majorType !== 1) throw _err("c509/bad-extensions", "a " + label + " value must be a CBOR integer");
7668
+ return cbor.read.int(node);
7669
+ }
7670
+ function _ekuPurposeOid(node) {
7671
+ if (node.majorType === 0 || node.majorType === 1) {
7672
+ var i = Number(cbor.read.int(node));
7673
+ var nm = EKU_BY_INT[i];
7674
+ if (nm === void 0) throw _err("c509/bad-extensions", "an extKeyUsage int " + i + " has no C509 registry row");
7675
+ return oid.byName(nm);
7676
+ }
7677
+ return _oidName(node, "c509/bad-extensions", "an extKeyUsage KeyPurposeId").oid;
7678
+ }
7679
+ function _oidCbor(dotted) {
7680
+ return cbor.build.byteString(asn1.encodeOidContent(dotted));
7681
+ }
7682
+ function _isCborNull(node) {
7683
+ return node.majorType === 7 && node.ai === 22;
7684
+ }
7685
+ function _ia5Bytes(node, tag) {
7686
+ if (node.majorType !== 3) throw _err("c509/bad-extensions", "a GeneralName [" + tag + "] value must be a CBOR text string (IA5String)");
7687
+ var s = cbor.read.textString(node);
7688
+ if (s.length === 0) throw _err("c509/bad-extensions", "a GeneralName [" + tag + "] IA5String must be non-empty");
7689
+ for (var i = 0; i < s.length; i++) {
7690
+ if (s.charCodeAt(i) > 127) throw _err("c509/bad-extensions", "a GeneralName [" + tag + "] IA5String has a non-ASCII code point");
7691
+ }
7692
+ var buf = Buffer.from(s, "latin1");
7693
+ guard.name.assertPrintableIa5(buf, _err, "c509/bad-extensions", "GeneralName [" + tag + "]");
7694
+ return buf;
7695
+ }
7696
+ function _namedBitsToContent(value) {
7697
+ if (!Number.isInteger(value) || value <= 0 || value > 511) return null;
7698
+ var hi = 0;
7699
+ for (var t = value; t; t >>= 1) hi++;
7700
+ hi -= 1;
7701
+ var buf = Buffer.alloc((hi >> 3) + 1);
7702
+ for (var bit = 0; bit <= hi; bit++) {
7703
+ if (value & 1 << bit) buf[bit >> 3] |= 128 >> (bit & 7);
7704
+ }
7705
+ return { unusedBits: 7 - (hi & 7), bytes: buf };
7706
+ }
7707
+ function _namedBitsFromContent(bytes, unusedBits) {
7708
+ var total = bytes.length * 8 - unusedBits, value = 0;
7709
+ for (var bit = 0; bit < total && bit < 31; bit++) {
7710
+ if (bytes[bit >> 3] & 128 >> (bit & 7)) value |= 1 << bit;
7711
+ }
7712
+ return value > 0 && value <= 511 ? value : null;
7713
+ }
7714
+ function _generalNameToDer(intVal, valueNode, ipMode) {
7715
+ if (intVal === 1 || intVal === 2 || intVal === 6) return b.contextPrimitive(intVal, _ia5Bytes(valueNode, intVal));
7716
+ if (intVal === 4) return b.explicit(4, _reconName(_name509(valueNode, true)));
7717
+ if (intVal === 7) return b.contextPrimitive(7, ipMode ? _ncIpToDer(valueNode) : _sanIpBytes(valueNode));
7718
+ if (intVal === 8) return b.contextPrimitive(8, asn1.encodeOidContent(_oidName(valueNode, "c509/bad-extensions", "a registeredID [8]").oid));
7719
+ if (intVal === 0 || intVal === -1 || intVal === -2 || intVal === -3) return _otherNameToDer(valueNode, intVal);
7720
+ throw _err("c509/bad-extensions", "GeneralName int " + intVal + " has no C509 sec. 8.13 registry row");
7721
+ }
7722
+ function _generalNameFromDer(gn, ipMode) {
7723
+ var t = gn.tagNumber;
7724
+ if (t === 1 || t === 2 || t === 6) return [cbor.build.int(BigInt(t)), cbor.build.textString(gn.value)];
7725
+ if (t === 4) {
7726
+ var nm = _dirNameToCbor(gn.bytes);
7727
+ return nm == null ? null : [cbor.build.int(4n), nm];
7728
+ }
7729
+ if (t === 7) {
7730
+ if (!ipMode) return [cbor.build.int(7n), cbor.build.byteString(gn.value)];
7731
+ var ip = _ncIpFromDer(gn.value);
7732
+ return ip == null ? null : [cbor.build.int(7n), cbor.build.byteString(ip)];
7733
+ }
7734
+ if (t === 8) return [cbor.build.int(8n), _oidCbor(gn.value)];
7735
+ if (t === 0) return _otherNameFromDer(gn.value);
7736
+ return null;
7737
+ }
7738
+ function _generalNamesToDer(node) {
7739
+ if (node.majorType !== 4 || !node.children) throw _err("c509/bad-extensions", "a GeneralNames value must be a CBOR array");
7740
+ var kids = node.children;
7741
+ if (kids.length === 0 || kids.length % 2 !== 0) throw _err("c509/bad-extensions", "a GeneralNames array must be non-empty (int, value) pairs (sec. 3.3)");
7742
+ var out = [];
7743
+ for (var i = 0; i + 1 < kids.length; i += 2) out.push(_generalNameToDer(Number(_cborIntVal(kids[i], "a GeneralName type")), kids[i + 1], false));
7744
+ return out;
7745
+ }
7746
+ function _generalNamesFromDer(gnNodes) {
7747
+ var out = [];
7748
+ for (var i = 0; i < gnNodes.length; i++) {
7749
+ var pair = _generalNameFromDer(schema.walk(GN_LEAF, gnNodes[i], NS), false);
7750
+ if (pair == null) return null;
7751
+ out.push(pair[0]);
7752
+ out.push(pair[1]);
7753
+ }
7754
+ return out;
7755
+ }
7756
+ function _otherNameToDer(node, intVal) {
7757
+ var typeId, inner;
7758
+ if (intVal === 0) {
7759
+ if (node.majorType !== 4 || !node.children || node.children.length !== 2) throw _err("c509/bad-extensions", "a generic otherName value must be a CBOR [ ~oid, bytes ] pair");
7760
+ typeId = _oidName(node.children[0], "c509/bad-extensions", "an otherName type-id").oid;
7761
+ if (node.children[1].majorType !== 2) throw _err("c509/bad-extensions", "an otherName value must be a CBOR byte string (the [0] EXPLICIT inner TLV)");
7762
+ inner = node.children[1].content;
7763
+ try {
7764
+ asn1.decode(inner);
7765
+ } catch (e) {
7766
+ throw _err("c509/bad-extensions", "an otherName value is not a single well-formed DER element", e);
7767
+ }
7768
+ } else if (intVal === -1) {
7769
+ if (node.majorType !== 4 || !node.children || node.children.length !== 2) throw _err("c509/bad-extensions", "an id-on-hardwareModuleName value must be a CBOR [ ~oid, bytes ] pair");
7770
+ if (node.children[1].majorType !== 2) throw _err("c509/bad-extensions", "a hardwareModuleName hwSerialNum must be a CBOR byte string");
7771
+ typeId = oid.byName("hardwareModuleName");
7772
+ inner = b.sequence([b.oid(_oidName(node.children[0], "c509/bad-extensions", "a hardwareModuleName hwType").oid), b.octetString(node.children[1].content)]);
7773
+ } else if (intVal === -2) {
7774
+ if (node.majorType !== 3) throw _err("c509/bad-extensions", "an id-on-SmtpUTF8Mailbox value must be a CBOR text string");
7775
+ var smtp = cbor.read.textString(node);
7776
+ if (smtp.length === 0) throw _err("c509/bad-extensions", "an id-on-SmtpUTF8Mailbox value must be non-empty (RFC 9598 SIZE (1..MAX))");
7777
+ typeId = oid.byName("smtpUtf8Mailbox");
7778
+ inner = b.utf8(smtp);
7779
+ } else {
7780
+ if (node.majorType !== 2) throw _err("c509/bad-extensions", "an id-on-MACAddress value must be a CBOR byte string");
7781
+ if (node.content.length !== 6 && node.content.length !== 8) throw _err("c509/bad-extensions", "an id-on-MACAddress value must be 6 (EUI-48) or 8 (EUI-64) octets");
7782
+ typeId = oid.byName("macAddress");
7783
+ inner = b.octetString(node.content);
7784
+ }
7785
+ return b.contextConstructed(0, Buffer.concat([b.oid(typeId), b.explicit(0, inner)]));
7786
+ }
7787
+ function _otherNameFromDer(v) {
7788
+ var onInt = ON_INT_BY_OID[v.typeId];
7789
+ if (onInt === void 0) return [cbor.build.int(0n), cbor.build.array([_oidCbor(v.typeId), cbor.build.byteString(v.valueBytes)])];
7790
+ var inner;
7791
+ try {
7792
+ inner = asn1.decode(v.valueBytes);
7793
+ } catch (_e) {
7794
+ return null;
7795
+ }
7796
+ if (onInt === -1) {
7797
+ if (inner.tagClass !== "universal" || inner.tagNumber !== asn1.TAGS.SEQUENCE || !inner.children || inner.children.length !== 2) return null;
7798
+ var hwType, hwSerial;
7799
+ try {
7800
+ hwType = asn1.read.oid(inner.children[0]);
7801
+ hwSerial = asn1.read.octetString(inner.children[1]);
7802
+ } catch (_e2) {
7803
+ return null;
7804
+ }
7805
+ return [cbor.build.int(-1n), cbor.build.array([_oidCbor(hwType), cbor.build.byteString(hwSerial)])];
7806
+ }
7807
+ if (onInt === -2) {
7808
+ if (inner.tagClass !== "universal" || inner.tagNumber !== asn1.TAGS.UTF8_STRING) return null;
7809
+ var txt;
7810
+ try {
7811
+ txt = asn1.read.string(inner);
7812
+ } catch (_e3) {
7813
+ return null;
7814
+ }
7815
+ return [cbor.build.int(-2n), cbor.build.textString(txt)];
7816
+ }
7817
+ if (inner.tagClass !== "universal" || inner.tagNumber !== asn1.TAGS.OCTET_STRING) return null;
7818
+ var mac;
7819
+ try {
7820
+ mac = asn1.read.octetString(inner);
7821
+ } catch (_e4) {
7822
+ return null;
7823
+ }
7824
+ if (mac.length !== 6 && mac.length !== 8) return null;
7825
+ return [cbor.build.int(-3n), cbor.build.byteString(mac)];
7826
+ }
7827
+ function _dirNameToCbor(gnBytes) {
7828
+ var node = asn1.decode(gnBytes);
7829
+ if (!node.children || node.children.length !== 1) return null;
7830
+ var name;
7831
+ try {
7832
+ name = _c509NameFromDer(node.children[0].bytes);
7833
+ } catch (_e) {
7834
+ return null;
7835
+ }
7836
+ try {
7837
+ return _encName(name, true);
7838
+ } catch (_e2) {
7839
+ return null;
7840
+ }
7841
+ }
7842
+ function _sanIpBytes(node) {
7843
+ if (node.majorType !== 2) throw _err("c509/bad-extensions", "an iPAddress value must be a CBOR byte string");
7844
+ if (node.content.length !== 4 && node.content.length !== 16) throw _err("c509/bad-extensions", "an iPAddress must be 4 (IPv4) or 16 (IPv6) octets");
7845
+ return node.content;
7846
+ }
7847
+ function _ncIpToDer(node) {
7848
+ if (node.majorType !== 2) throw _err("c509/bad-extensions", "a name-constraints iPAddress value must be a CBOR byte string");
7849
+ var buf = node.content, addrLen;
7850
+ if (buf.length === 5) addrLen = 4;
7851
+ else if (buf.length === 17) addrLen = 16;
7852
+ else throw _err("c509/bad-extensions", "a name-constraints iPAddress must be 5 (IPv4) or 17 (IPv6) octets (address + prefix length; RFC 9549 sec. 2.2)");
7853
+ var prefixLen = buf[addrLen], maxBits = addrLen * 8;
7854
+ if (prefixLen > maxBits) throw _err("c509/bad-extensions", "a name-constraints iPAddress prefix length " + prefixLen + " exceeds " + maxBits);
7855
+ var mask = Buffer.alloc(addrLen);
7856
+ for (var bit = 0; bit < prefixLen; bit++) mask[bit >> 3] |= 128 >> (bit & 7);
7857
+ return Buffer.concat([buf.subarray(0, addrLen), mask]);
7858
+ }
7859
+ function _ncIpFromDer(buf) {
7860
+ var addrLen = buf.length >> 1, mask = buf.subarray(addrLen), prefixLen = 0, seenZero = false;
7861
+ for (var i = 0; i < mask.length; i++) {
7862
+ for (var bit = 0; bit < 8; bit++) {
7863
+ if (mask[i] & 128 >> bit) {
7864
+ if (seenZero) return null;
7865
+ prefixLen++;
7866
+ } else seenZero = true;
7867
+ }
7868
+ }
7869
+ return Buffer.concat([buf.subarray(0, addrLen), Buffer.from([prefixLen])]);
7870
+ }
7871
+ function _subtreesToDer(node) {
7872
+ if (node.majorType !== 4 || !node.children) throw _err("c509/bad-extensions", "a GeneralSubtrees value must be a CBOR array");
7873
+ var kids = node.children;
7874
+ if (kids.length === 0 || kids.length % 2 !== 0) throw _err("c509/bad-extensions", "a GeneralSubtrees array must be non-empty (int, value) pairs");
7875
+ var out = [];
7876
+ for (var i = 0; i + 1 < kids.length; i += 2) out.push(b.sequence([_generalNameToDer(Number(_cborIntVal(kids[i], "a GeneralSubtree base type")), kids[i + 1], true)]));
7877
+ return Buffer.concat(out);
7878
+ }
7879
+ function _subtreesFromDer(subtreeNodes) {
7880
+ if (!subtreeNodes.length) return null;
7881
+ var out = [];
7882
+ for (var i = 0; i < subtreeNodes.length; i++) {
7883
+ var st = subtreeNodes[i];
7884
+ if (st.tagClass !== "universal" || st.tagNumber !== asn1.TAGS.SEQUENCE || !st.children || st.children.length !== 1) return null;
7885
+ var pair = _generalNameFromDer(schema.walk(GN_LEAF_SUBTREE, st.children[0], NS), true);
7886
+ if (pair == null) return null;
7887
+ out.push(pair[0]);
7888
+ out.push(pair[1]);
7889
+ }
7890
+ return out;
7891
+ }
7892
+ function _accessMethodOid(node) {
7893
+ if (node.majorType === 0 || node.majorType === 1) {
7894
+ var i = Number(cbor.read.int(node)), nm = IA_BY_INT[i];
7895
+ if (nm === void 0) throw _err("c509/bad-extensions", "an accessMethod int " + i + " has no C509 sec. 8.11 registry row");
7896
+ return oid.byName(nm);
7897
+ }
7898
+ return _oidName(node, "c509/bad-extensions", "an accessMethod").oid;
7899
+ }
7900
+ function _reasonsBitsToDer(value) {
7901
+ if ((value & 1) !== 0) throw _err("c509/bad-extensions", "a cRLDistributionPoints reasons value must not set the reserved unused ReasonFlags bit 0 (RFC 5280 sec. 4.2.1.13)");
7902
+ var c = _namedBitsToContent(value);
7903
+ if (c == null) throw _err("c509/bad-extensions", "a cRLDistributionPoints reasons value must be within the 9 defined ReasonFlags bits");
7904
+ return b.contextPrimitive(1, Buffer.concat([Buffer.from([c.unusedBits]), c.bytes]));
7905
+ }
7906
+ function _serialIntContent(node) {
7907
+ var mag = _minBytes(_biguint(node, "c509/bad-extensions", "an authorityKeyIdentifier serial"));
7908
+ if (mag.length === 0) return Buffer.from([0]);
7909
+ return mag[0] & 128 ? Buffer.concat([Buffer.from([0]), mag]) : mag;
7910
+ }
7911
+ function _dpToDer(dpNode) {
7912
+ if (dpNode.majorType !== 4 || !dpNode.children || dpNode.children.length !== 3) throw _err("c509/bad-extensions", "a DistributionPoint must be a CBOR [ fullName, reasons, cRLIssuer ] array");
7913
+ var fullName = dpNode.children[0], reasons = dpNode.children[1], crlIssuer = dpNode.children[2];
7914
+ var uris;
7915
+ if (fullName.majorType === 3) uris = [b.contextPrimitive(6, _ia5Bytes(fullName, 6))];
7916
+ else if (fullName.majorType === 4 && fullName.children) {
7917
+ if (fullName.children.length < 2) throw _err("c509/bad-extensions", "a DistributionPoint fullName array must hold 2 or more URIs (a single URI is a bare text; sec. 3.3)");
7918
+ uris = fullName.children.map(function(u) {
7919
+ return b.contextPrimitive(6, _ia5Bytes(u, 6));
7920
+ });
7921
+ } else throw _err("c509/bad-extensions", "a DistributionPoint fullName must be a URI text or an array of URIs");
7922
+ var fields = [b.explicit(0, b.contextConstructed(0, Buffer.concat(uris)))];
7923
+ if (!_isCborNull(reasons)) fields.push(_reasonsBitsToDer(Number(_cborUint(reasons, "cRLDistributionPoints reasons"))));
7924
+ if (!_isCborNull(crlIssuer)) fields.push(b.contextConstructed(2, b.explicit(4, _reconName(_name509(crlIssuer, true)))));
7925
+ return b.sequence(fields);
7926
+ }
7927
+ function _dpFromDer(dp) {
7928
+ if (dp.tagClass !== "universal" || dp.tagNumber !== asn1.TAGS.SEQUENCE || !dp.children) return null;
7929
+ var fullNameUris = null, reasonsVal = null, crlIssuerCbor = null, lastTag = -1;
7930
+ for (var i = 0; i < dp.children.length; i++) {
7931
+ var f = dp.children[i];
7932
+ if (f.tagClass !== "context" || f.tagNumber <= lastTag) return null;
7933
+ lastTag = f.tagNumber;
7934
+ if (f.tagNumber === 0) {
7935
+ if (!f.children || f.children.length !== 1) return null;
7936
+ var dpn = f.children[0];
7937
+ if (dpn.tagClass !== "context" || dpn.tagNumber !== 0 || !dpn.children || !dpn.children.length) return null;
7938
+ fullNameUris = [];
7939
+ for (var j = 0; j < dpn.children.length; j++) {
7940
+ var mm = dpn.children[j];
7941
+ if (mm.tagClass !== "context" || mm.tagNumber !== 6) return null;
7942
+ fullNameUris.push(schema.walk(GN_LEAF, mm, NS).value);
7943
+ }
7944
+ } else if (f.tagNumber === 1) {
7945
+ var bs;
7946
+ try {
7947
+ bs = asn1.read.bitStringImplicit(f, 1);
7948
+ } catch (_e) {
7949
+ return null;
7950
+ }
7951
+ reasonsVal = _namedBitsFromContent(bs.bytes, bs.unusedBits);
7952
+ if (reasonsVal == null) return null;
7953
+ } else if (f.tagNumber === 2) {
7954
+ if (!f.children || f.children.length !== 1) return null;
7955
+ var ci = f.children[0];
7956
+ if (ci.tagClass !== "context" || ci.tagNumber !== 4 || !ci.children || ci.children.length !== 1) return null;
7957
+ try {
7958
+ crlIssuerCbor = _encName(_c509NameFromDer(ci.children[0].bytes), true);
7959
+ } catch (_e2) {
7960
+ return null;
7961
+ }
7962
+ } else return null;
7963
+ }
7964
+ if (fullNameUris == null) return null;
7965
+ var oneUri = fullNameUris.length === 1 ? fullNameUris[0] : null;
7966
+ var fnCbor = oneUri != null ? cbor.build.textString(oneUri) : cbor.build.array(fullNameUris.map(function(u) {
7967
+ return cbor.build.textString(u);
7968
+ }));
7969
+ return {
7970
+ triple: cbor.build.array([fnCbor, reasonsVal == null ? cbor.build.nullValue() : cbor.build.uint(BigInt(reasonsVal)), crlIssuerCbor == null ? cbor.build.nullValue() : crlIssuerCbor]),
7971
+ oneUri,
7972
+ noReasons: reasonsVal == null,
7973
+ noIssuer: crlIssuerCbor == null
7974
+ };
7975
+ }
7976
+ function _extValueToDer(name, node) {
7977
+ switch (name) {
7978
+ case "subjectKeyIdentifier":
7979
+ if (node.majorType !== 2) throw _err("c509/bad-extensions", "a subjectKeyIdentifier value must be a CBOR byte string");
7980
+ return b.octetString(node.content);
7981
+ case "keyUsage":
7982
+ return _reconKeyUsageBits(Number(_cborUint(node, "keyUsage")));
7983
+ case "basicConstraints": {
7984
+ var iv = _cborIntVal(node, "basicConstraints");
7985
+ if (iv === -2n) return b.sequence([]);
7986
+ if (iv === -1n) return b.sequence([b.boolean(true)]);
7987
+ if (iv >= 0n) return b.sequence([b.boolean(true), b.integer(iv)]);
7988
+ throw _err("c509/bad-extensions", "a basicConstraints int " + iv + " is outside the -2/-1/pathLen range");
7989
+ }
7990
+ case "authorityKeyIdentifier":
7991
+ if (node.majorType === 2) return b.sequence([b.contextPrimitive(0, node.content)]);
7992
+ if (node.majorType === 4 && node.children && node.children.length === 3) {
7993
+ if (node.children[0].majorType !== 2) throw _err("c509/bad-extensions", "an authorityKeyIdentifier keyIdentifier must be a CBOR byte string");
7994
+ return b.sequence([
7995
+ b.contextPrimitive(0, node.children[0].content),
7996
+ // keyIdentifier [0] IMPLICIT OCTET STRING
7997
+ b.contextConstructed(1, Buffer.concat(_generalNamesToDer(node.children[1]))),
7998
+ // authorityCertIssuer [1] IMPLICIT GeneralNames
7999
+ b.contextPrimitive(2, _serialIntContent(node.children[2]))
8000
+ // authorityCertSerialNumber [2] IMPLICIT INTEGER
8001
+ ]);
8002
+ }
8003
+ throw _err("c509/bad-extensions", "an authorityKeyIdentifier value must be a keyId byte string or the [ keyId, authorityCertIssuer, serial ] array (sec. 3.3)");
8004
+ case "extKeyUsage": {
8005
+ var items;
8006
+ if (node.majorType === 4) {
8007
+ items = node.children || [];
8008
+ if (items.length < 2) throw _err("c509/bad-extensions", "an extKeyUsage array must hold 2 or more KeyPurposeIds; a single purpose omits the array (draft-20 sec. 3.3)");
8009
+ } else {
8010
+ items = [node];
8011
+ }
8012
+ return b.sequence(items.map(function(it) {
8013
+ return b.oid(_ekuPurposeOid(it));
8014
+ }));
8015
+ }
8016
+ case "inhibitAnyPolicy":
8017
+ return b.integer(_cborUint(node, "inhibitAnyPolicy"));
8018
+ case "ocspNoCheck":
8019
+ if (node.majorType !== 7 || !(Buffer.isBuffer(node.bytes) && node.bytes.length === 1 && node.bytes[0] === 246)) throw _err("c509/bad-extensions", "an ocspNoCheck value must be the CBOR simple value null");
8020
+ return b.nullValue();
8021
+ case "tlsFeature": {
8022
+ if (node.majorType !== 4) throw _err("c509/bad-extensions", "a tlsFeature value must be a CBOR array");
8023
+ return b.sequence((node.children || []).map(function(f) {
8024
+ return b.integer(_cborUint(f, "tlsFeature feature"));
8025
+ }));
8026
+ }
8027
+ case "subjectAltName":
8028
+ case "issuerAltName":
8029
+ if (node.majorType === 3) return b.sequence([b.contextPrimitive(2, _ia5Bytes(node, 2))]);
8030
+ return b.sequence(_generalNamesToDer(node));
8031
+ case "nameConstraints": {
8032
+ if (node.majorType !== 4 || !node.children || node.children.length !== 2) throw _err("c509/bad-extensions", "a nameConstraints value must be a 2-element CBOR array [ permitted, excluded ] (sec. 3.3)");
8033
+ var ncFields = [];
8034
+ if (!_isCborNull(node.children[0])) ncFields.push(b.contextConstructed(0, _subtreesToDer(node.children[0])));
8035
+ if (!_isCborNull(node.children[1])) ncFields.push(b.contextConstructed(1, _subtreesToDer(node.children[1])));
8036
+ if (ncFields.length === 0) throw _err("c509/bad-extensions", "nameConstraints must contain permittedSubtrees or excludedSubtrees (RFC 5280 sec. 4.2.1.10)");
8037
+ return b.sequence(ncFields);
8038
+ }
8039
+ case "authorityInfoAccess":
8040
+ case "subjectInfoAccess": {
8041
+ if (node.majorType !== 4 || !node.children) throw _err("c509/bad-extensions", "an " + name + " value must be a CBOR array");
8042
+ var aiaKids = node.children;
8043
+ if (aiaKids.length === 0 || aiaKids.length % 2 !== 0) throw _err("c509/bad-extensions", "an " + name + " array must be non-empty (accessMethod, uri) pairs (sec. 3.3)");
8044
+ var descs = [];
8045
+ for (var ai = 0; ai + 1 < aiaKids.length; ai += 2) descs.push(b.sequence([b.oid(_accessMethodOid(aiaKids[ai])), b.contextPrimitive(6, _ia5Bytes(aiaKids[ai + 1], 6))]));
8046
+ return b.sequence(descs);
8047
+ }
8048
+ case "cRLDistributionPoints":
8049
+ case "freshestCRL":
8050
+ if (node.majorType === 3) return b.sequence([b.sequence([b.explicit(0, b.contextConstructed(0, b.contextPrimitive(6, _ia5Bytes(node, 6))))])]);
8051
+ if (node.majorType !== 4 || !node.children || node.children.length < 1) throw _err("c509/bad-extensions", "a " + name + " value must be a CBOR array of DistributionPoints or a bare URI text (sec. 3.3)");
8052
+ return b.sequence(node.children.map(function(dp) {
8053
+ return _dpToDer(dp);
8054
+ }));
8055
+ default:
8056
+ throw _err("c509/bad-extensions", "extension " + name + " has no compact value decoder");
8057
+ }
8058
+ }
8059
+ function _extValueFromDer(name, der) {
8060
+ var node;
8061
+ try {
8062
+ node = asn1.decode(der);
8063
+ switch (name) {
8064
+ case "subjectKeyIdentifier":
8065
+ return cbor.build.byteString(asn1.read.octetString(node));
8066
+ case "keyUsage": {
8067
+ var bits = _keyUsageBitsFromDer(der);
8068
+ return bits == null ? null : cbor.build.uint(BigInt(bits));
8069
+ }
8070
+ case "basicConstraints": {
8071
+ var kids = node.children || [];
8072
+ if (kids.length === 0) return cbor.build.int(-2n);
8073
+ if (asn1.read.boolean(kids[0]) !== true) return null;
8074
+ if (kids.length === 1) return cbor.build.int(-1n);
8075
+ if (kids.length !== 2) return null;
8076
+ return cbor.build.uint(asn1.read.integer(kids[1]));
8077
+ }
8078
+ case "authorityKeyIdentifier": {
8079
+ var akids = node.children || [];
8080
+ if (akids.length === 1 && akids[0].tagClass === "context" && akids[0].tagNumber === 0) {
8081
+ return cbor.build.byteString(asn1.read.octetStringImplicit(akids[0], 0));
8082
+ }
8083
+ if (akids.length === 3 && akids[0].tagClass === "context" && akids[0].tagNumber === 0 && akids[1].tagClass === "context" && akids[1].tagNumber === 1 && akids[1].children && akids[2].tagClass === "context" && akids[2].tagNumber === 2) {
8084
+ var akiIssuer = _generalNamesFromDer(akids[1].children);
8085
+ if (akiIssuer == null) return null;
8086
+ var akiSerial = asn1.read.integerImplicit(akids[2], 2);
8087
+ if (akiSerial < 0n) return null;
8088
+ return cbor.build.array([cbor.build.byteString(asn1.read.octetStringImplicit(akids[0], 0)), cbor.build.array(akiIssuer), cbor.build.byteString(_minBytes(akiSerial))]);
8089
+ }
8090
+ return null;
8091
+ }
8092
+ case "extKeyUsage": {
8093
+ var purposes = node.children || [];
8094
+ if (!purposes.length) return null;
8095
+ var out = purposes.map(function(p) {
8096
+ var dotted = asn1.read.oid(p), pint = EKU_TO_INT[dotted];
8097
+ return pint !== void 0 ? cbor.build.int(BigInt(pint)) : cbor.build.byteString(asn1.encodeOidContent(dotted));
8098
+ });
8099
+ return out.length === 1 ? out[0] : cbor.build.array(out);
8100
+ }
8101
+ case "inhibitAnyPolicy":
8102
+ return cbor.build.uint(asn1.read.integer(node));
8103
+ case "ocspNoCheck":
8104
+ asn1.read.nullValue(node);
8105
+ return cbor.build.nullValue();
8106
+ case "tlsFeature":
8107
+ return cbor.build.array((node.children || []).map(function(f) {
8108
+ return cbor.build.uint(asn1.read.integer(f));
8109
+ }));
8110
+ case "subjectAltName":
8111
+ case "issuerAltName": {
8112
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || !node.children || node.children.length < 1) return null;
8113
+ var sanMembers = node.children;
8114
+ if (sanMembers.length === 1 && sanMembers[0].tagClass === "context" && sanMembers[0].tagNumber === 2) {
8115
+ return cbor.build.textString(schema.walk(GN_LEAF, sanMembers[0], NS).value);
8116
+ }
8117
+ var sanItems = _generalNamesFromDer(sanMembers);
8118
+ return sanItems == null ? null : cbor.build.array(sanItems);
8119
+ }
8120
+ case "nameConstraints": {
8121
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || !node.children || node.children.length < 1) return null;
8122
+ var permCbor = cbor.build.nullValue(), exclCbor = cbor.build.nullValue(), sawNc = false, ncLast = -1;
8123
+ for (var nci = 0; nci < node.children.length; nci++) {
8124
+ var ncf = node.children[nci];
8125
+ if (ncf.tagClass !== "context" || ncf.tagNumber <= ncLast) return null;
8126
+ ncLast = ncf.tagNumber;
8127
+ var subItems = _subtreesFromDer(ncf.children || []);
8128
+ if (subItems == null) return null;
8129
+ if (ncf.tagNumber === 0) {
8130
+ permCbor = cbor.build.array(subItems);
8131
+ sawNc = true;
8132
+ } else if (ncf.tagNumber === 1) {
8133
+ exclCbor = cbor.build.array(subItems);
8134
+ sawNc = true;
8135
+ } else return null;
8136
+ }
8137
+ return sawNc ? cbor.build.array([permCbor, exclCbor]) : null;
8138
+ }
8139
+ case "authorityInfoAccess":
8140
+ case "subjectInfoAccess": {
8141
+ var aiaDescs = node.children || [];
8142
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || aiaDescs.length < 1) return null;
8143
+ var aiaOut = [];
8144
+ for (var adi = 0; adi < aiaDescs.length; adi++) {
8145
+ var d = aiaDescs[adi];
8146
+ if (d.tagClass !== "universal" || d.tagNumber !== asn1.TAGS.SEQUENCE || !d.children || d.children.length !== 2) return null;
8147
+ var aiaLoc = d.children[1];
8148
+ if (aiaLoc.tagClass !== "context" || aiaLoc.tagNumber !== 6) return null;
8149
+ var methodDotted = asn1.read.oid(d.children[0]), mint = IA_TO_INT[methodDotted];
8150
+ aiaOut.push(mint !== void 0 ? cbor.build.int(BigInt(mint)) : _oidCbor(methodDotted));
8151
+ aiaOut.push(cbor.build.textString(schema.walk(GN_LEAF, aiaLoc, NS).value));
8152
+ }
8153
+ return cbor.build.array(aiaOut);
8154
+ }
8155
+ case "cRLDistributionPoints":
8156
+ case "freshestCRL": {
8157
+ var dps = node.children || [];
8158
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || dps.length < 1) return null;
8159
+ var dpResults = [];
8160
+ for (var dpi = 0; dpi < dps.length; dpi++) {
8161
+ var dpr = _dpFromDer(dps[dpi]);
8162
+ if (dpr == null) return null;
8163
+ dpResults.push(dpr);
8164
+ }
8165
+ if (dpResults.length === 1 && dpResults[0].oneUri != null && dpResults[0].noReasons && dpResults[0].noIssuer) return cbor.build.textString(dpResults[0].oneUri);
8166
+ return cbor.build.array(dpResults.map(function(r) {
8167
+ return r.triple;
8168
+ }));
8169
+ }
8170
+ default:
8171
+ return null;
8172
+ }
8173
+ } catch (_e) {
8174
+ return null;
8175
+ }
8176
+ }
8177
+ function _tryCompactExtValue(name, der) {
8178
+ var compact = _extValueFromDer(name, der);
8179
+ if (compact == null) return null;
8180
+ var recon;
8181
+ try {
8182
+ recon = _extValueToDer(name, cbor.decode(compact));
8183
+ } catch (_e) {
8184
+ return null;
8185
+ }
8186
+ return recon.equals(der) ? compact : null;
8187
+ }
7555
8188
  function _extensions(node) {
7556
8189
  if (node.majorType === 0 || node.majorType === 1) {
7557
8190
  var iv = Number(cbor.read.int(node));
@@ -7570,7 +8203,13 @@ var require_schema_c509 = __commonJS({
7570
8203
  if (name === void 0) throw _err("c509/bad-extensions", "extension type integer " + ei + " has no C509 registry row");
7571
8204
  extOid = oid.byName(name);
7572
8205
  critical = ei < 0;
7573
- valContent = valNode.content;
8206
+ if (EXT_COMPACT[name]) {
8207
+ valContent = _extValueToDer(name, valNode);
8208
+ } else if (valNode.majorType === 2) {
8209
+ valContent = valNode.content;
8210
+ } else {
8211
+ throw _err("c509/bad-extensions", "extension " + name + " has no compact value codec; its int-form value must be a byte string (draft-20 sec. 3.3)");
8212
+ }
7574
8213
  } else {
7575
8214
  var r = _oidName(idNode, "c509/bad-extensions", "an extension id");
7576
8215
  name = r.name;
@@ -7640,16 +8279,9 @@ var require_schema_c509 = __commonJS({
7640
8279
  throw _err("c509/non-invertible", "subjectPublicKey algorithm " + spkAlg.name + " is not in the type-3 reconstruction covered set");
7641
8280
  }
7642
8281
  function _reconKeyUsageBits(value) {
7643
- if (!Number.isInteger(value) || value <= 0 || value > 511) throw _err("c509/non-invertible", "a keyUsage value must be a positive integer within the 9 defined bits");
7644
- var hi = 0;
7645
- for (var t = value; t; t >>= 1) hi++;
7646
- hi -= 1;
7647
- var nbytes = (hi >> 3) + 1;
7648
- var buf = Buffer.alloc(nbytes);
7649
- for (var bit = 0; bit <= hi; bit++) {
7650
- if (value & 1 << bit) buf[bit >> 3] |= 128 >> (bit & 7);
7651
- }
7652
- return b.bitString(buf, 7 - (hi & 7));
8282
+ var c = _namedBitsToContent(value);
8283
+ if (c == null) throw _err("c509/non-invertible", "a keyUsage value must be a positive integer within the 9 defined bits");
8284
+ return b.bitString(c.bytes, c.unusedBits);
7653
8285
  }
7654
8286
  function _reconExtensions(exts) {
7655
8287
  var items = exts.map(function(ext) {
@@ -7760,8 +8392,14 @@ var require_schema_c509 = __commonJS({
7760
8392
  }
7761
8393
  var SIG_ALG_TO_INT = { ecdsaWithSHA256: 0, ecdsaWithSHA384: 1, ecdsaWithSHA512: 2 };
7762
8394
  var PK_ALG_TO_INT = { rsaEncryption: 0, "ecPublicKey|prime256v1": 1, "ecPublicKey|secp384r1": 2, "ecPublicKey|secp521r1": 3 };
7763
- var ATTR_TO_INT = { commonName: 1, surname: 2, serialNumber: 3, countryName: 4, localityName: 6, stateOrProvinceName: 7, organizationName: 8, organizationalUnitName: 9, title: 10 };
7764
- var EXT_TO_INT = { subjectKeyIdentifier: 1, keyUsage: 2, subjectAltName: 3, basicConstraints: 4, authorityKeyIdentifier: 10 };
8395
+ var ATTR_TO_INT = {};
8396
+ Object.keys(ATTR_BY_INT).forEach(function(k) {
8397
+ ATTR_TO_INT[ATTR_BY_INT[k]] = Number(k);
8398
+ });
8399
+ var EXT_TO_INT = {};
8400
+ Object.keys(EXT_BY_INT).forEach(function(k) {
8401
+ if (EXT_COMPACT[EXT_BY_INT[k]]) EXT_TO_INT[EXT_BY_INT[k]] = Number(k);
8402
+ });
7765
8403
  function _minBytes(n) {
7766
8404
  if (n < 0n) throw _err("c509/bad-serial", "a ~biguint value must be non-negative");
7767
8405
  if (n === 0n) return Buffer.alloc(0);
@@ -7812,12 +8450,12 @@ var require_schema_c509 = __commonJS({
7812
8450
  var items = [];
7813
8451
  exts.forEach(function(ext) {
7814
8452
  var ei = EXT_TO_INT[ext.name];
7815
- if (ei !== void 0) {
8453
+ var compact = ei !== void 0 && Buffer.isBuffer(ext.value) ? _tryCompactExtValue(ext.name, ext.value) : null;
8454
+ if (compact != null) {
7816
8455
  items.push(cbor.build.int(BigInt(ext.critical ? -ei : ei)));
7817
- if (!Buffer.isBuffer(ext.value)) throw _err("c509/non-invertible", "extension " + ext.name + " has no byte-string value to encode");
7818
- items.push(cbor.build.byteString(ext.value));
8456
+ items.push(compact);
7819
8457
  } else {
7820
- items.push(cbor.build.byteString(asn1.encodeOidContent(ext.oid)));
8458
+ items.push(cbor.build.byteString(asn1.encodeOidContent(ext.oid || oid.byName(ext.name))));
7821
8459
  if (!Buffer.isBuffer(ext.value)) throw _err("c509/non-invertible", "extension " + (ext.oid || ext.name) + " has no byte-string value to encode");
7822
8460
  var bs = cbor.build.byteString(ext.value);
7823
8461
  items.push(ext.critical ? cbor.build.array([bs]) : bs);
@@ -7894,11 +8532,7 @@ var require_schema_c509 = __commonJS({
7894
8532
  } catch (_e) {
7895
8533
  return null;
7896
8534
  }
7897
- var total = bs.bytes.length * 8 - bs.unusedBits, value = 0;
7898
- for (var bit = 0; bit < total && bit < 31; bit++) {
7899
- if (bs.bytes[bit >> 3] & 128 >> (bit & 7)) value |= 1 << bit;
7900
- }
7901
- return value > 0 && value <= 511 ? value : null;
8535
+ return _namedBitsFromContent(bs.bytes, bs.unusedBits);
7902
8536
  }
7903
8537
  var _NO_EXPIRY = Date.UTC(9999, 11, 31, 23, 59, 59);
7904
8538
  var NODE_TO_WEBCRYPTO = { "prime256v1": "P-256", "secp384r1": "P-384", "secp521r1": "P-521" };