@blamejs/pki 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,20 @@ All notable changes to `@blamejs/pki` are documented here. The format
4
4
  follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); this
5
5
  project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## v0.4.1 — 2026-08-08
8
+
9
+ pki.schema.c509 encodes and decodes the compact subjectDirectoryAttributes value form -- a C509 certificate's subject directory attributes ride their draft-20 registry integers (or unwrapped OIDs) with their directory-string values, interoperating with a conformant C509 implementation rather than only this decoder.
10
+
11
+ ### Added
12
+
13
+ - pki.schema.c509 encodes and decodes the compact value form for the subjectDirectoryAttributes extension (draft-ietf-cose-cbor-encoded-cert-20 sec. 3.3): a flat array of (attribute type, attribute values) pairs where each type is a sec. 8.6 registry integer (the sign selecting the directory-string type) or an unwrapped OID, and each values slot is a non-empty array holding the attribute's SET of one or more values -- the string values for a registry-integer type, or the raw DER attribute values for an unwrapped-OID type. Both directions invert to the DER extnValue byte-for-byte, so a certificate carrying the extension is the specific compact shape a conformant C509 implementation reads rather than an opaque DER byte string. The encoder is guarded per attribute and per extension: an attribute whose value is not a directory string, or whose value SET mixes string types, uses the unwrapped-OID form for that attribute (keeping the rest of the extension compact); a value the compact form cannot represent falls the whole extension back to the unwrapped-OID byte-string form instead of encoding lossily; and a malformed compact value fails closed with a typed C509Error.
14
+ - The OID registry gains the subjectDirectoryAttributes certificate-extension identifier (2.5.29.9), resolvable through pki.oid.byName / pki.oid.name.
15
+ - The ASN.1 codec reads NumericString (pki.asn1.TAGS.NUMERIC_STRING), the X.520 syntax of the x121Address and internationalISDNNumber directory attributes. It is validated strictly like every other string type: a value outside the digits-and-space set the type permits is rejected as malformed rather than decoded.
16
+
17
+ ### Changed
18
+
19
+ - A C509 certificate carrying subjectDirectoryAttributes now encodes to -- and decodes from -- its compact CBOR value shape rather than the unwrapped-OID byte-string form an earlier release emitted; both reconstruct the same DER, but the CBOR bytes differ, so re-encode any C509 produced by an earlier release.
20
+
7
21
  ## v0.4.0 — 2026-08-06
8
22
 
9
23
  CRL issuance and verification, PKCS#12 build and open, attribute-certificate issuance, and the key-material lifecycle graduate to stable, and pki.schema.c509 adds the compact policyMappings and policyConstraints value forms -- a C509 certificate's policy mappings and policy constraints now ride their specific draft-20 CBOR shape and interoperate with a conformant C509 implementation rather than only this decoder.
package/README.md CHANGED
@@ -204,7 +204,7 @@ is callable today; nothing below is a stub.
204
204
  | `pki.webcrypto` | A W3C WebCrypto (`SubtleCrypto`) engine over `node:crypto` — `sign`/`verify`/`encrypt`/`decrypt`/`deriveBits`/`digest`/`generateKey`/`importKey`/`exportKey` across RSA, ECDSA, ECDH, Ed25519/Ed448, AES, HMAC, HKDF, PBKDF2, SHA — **and** post-quantum ML-DSA-44/65/87 and SLH-DSA signatures, plus ML-KEM-512/768/1024 key generation and certificate/PKCS#8 import — the RFC 9935 seed / expandedKey / both private-key CHOICE is validated fail-closed, so an OpenSSL-legacy bare-seed or an internally inconsistent key is rejected with a typed error (KEM encapsulation lands with CMS KEM-decrypt). Zero-dependency, OpenSSL-interoperable |
205
205
  | `pki.schema` | The schema family — `parse` detects which PKI format DER / PEM encodes and routes to the right parser, `all` enumerates the registered formats, and the engine + per-format members are grouped here |
206
206
  | `pki.schema.x509` | Parse DER / PEM certificates into structured, validated fields, with named + partly-decoded extensions — including the RFC 3739 / ETSI EN 319 412-5 qualified-certificate `qcStatements` (EU-qualified declaration, reliance limit, QSCD flag, certificate type, retention, PDS URLs, country of qualification; unknown statements preserved opaque) and the Microsoft Active Directory Certificate Services enrollment extensions (certificate template, CA version, previous-CA-certificate hash, application policies), fail-closed — `parse`, `pemDecode`, `pemEncode` |
207
- | `pki.schema.c509` | Parse **and encode** C509 CBOR-encoded certificates (draft-ietf-cose-cbor-encoded-cert) — the compact CBOR profile of X.509, decoded fail-closed under deterministic CBOR; an explicit `parse` call (CBOR, not DER, so not auto-routed). `encode(input)` is the byte-exact inverse: a DER X.509 v3 certificate forward-transforms to a compact type-3 C509 whose reconstruction reproduces the original DER byte for byte (so the original signature still verifies), or a `parse` result re-emits its native array — canonical deterministic CBOR with the registry integer shorthands, the C509 compressions, and the compact draft-20 per-extension value forms — the scalar extensions (keyUsage, basicConstraints, extended key usage, subject key identifier, and more), the general-name-bearing extensions (subjectAltName, issuer alt name, name constraints, CRL distribution points, authority/subject information access, and the full authority key identifier) over one shared GeneralNames value codec, certificate policies (registry-integer or OID policy identifiers with their CPS-URI and UserNotice qualifiers), and policy mappings and policy constraints; a certificate outside the invertible set throws a typed `C509Error` |
207
+ | `pki.schema.c509` | Parse **and encode** C509 CBOR-encoded certificates (draft-ietf-cose-cbor-encoded-cert) — the compact CBOR profile of X.509, decoded fail-closed under deterministic CBOR; an explicit `parse` call (CBOR, not DER, so not auto-routed). `encode(input)` is the byte-exact inverse: a DER X.509 v3 certificate forward-transforms to a compact type-3 C509 whose reconstruction reproduces the original DER byte for byte (so the original signature still verifies), or a `parse` result re-emits its native array — canonical deterministic CBOR with the registry integer shorthands, the C509 compressions, and the compact draft-20 per-extension value forms — the scalar extensions (keyUsage, basicConstraints, extended key usage, subject key identifier, and more), the general-name-bearing extensions (subjectAltName, issuer alt name, name constraints, CRL distribution points, authority/subject information access, and the full authority key identifier) over one shared GeneralNames value codec, certificate policies (registry-integer or OID policy identifiers with their CPS-URI and UserNotice qualifiers), and policy mappings and policy constraints, and subject directory attributes; a certificate outside the invertible set throws a typed `C509Error` |
208
208
  | `pki.schema.crl` | Parse DER / PEM X.509 CRLs per RFC 5280 §5 — revoked serials with real-`Date` revocation times, named + partly-decoded extensions, fail-closed — `parse`, `pemDecode`, `pemEncode` |
209
209
  | `pki.schema.csr` | Parse DER / PEM PKCS#10 certification requests per RFC 2986 — subject DN, public key, requested attributes, signature, fail-closed — `parse`, `pemDecode`, `pemEncode` |
210
210
  | `pki.schema.pkcs8` | Parse DER / PEM PKCS#8 private keys per RFC 5208 / 5958 — algorithm, raw key bytes, attributes, optional public key, fail-closed; encrypted keys recognized (not decrypted) — `parse`, `parseEncrypted`, `pemDecode`, `pemEncode` |
package/lib/asn1-der.js CHANGED
@@ -66,6 +66,7 @@ var UNIVERSAL_TYPES = {
66
66
  UTF8_STRING: { tag: 0x0c, form: "primitive" },
67
67
  SEQUENCE: { tag: 0x10, form: "constructed" },
68
68
  SET: { tag: 0x11, form: "constructed" },
69
+ NUMERIC_STRING: { tag: 0x12, form: "primitive" },
69
70
  PRINTABLE_STRING: { tag: 0x13, form: "primitive" },
70
71
  TELETEX_STRING: { tag: 0x14, form: "primitive" },
71
72
  IA5_STRING: { tag: 0x16, form: "primitive" },
@@ -95,7 +96,7 @@ var CONSTRUCTED_BIT = 0x20;
95
96
  // constructed-capable set is DERIVED from the registry above and keyed by tag
96
97
  // for an O(1) decode-time form check. It is a WHITELIST: a universal tag
97
98
  // outside it -- a registered primitive type and an unregistered one
98
- // (NumericString, GeneralString, ObjectDescriptor, REAL, ...) alike -- has no
99
+ // (GeneralString, ObjectDescriptor, REAL, ...) alike -- has no
99
100
  // DER constructed form, so the default is reject.
100
101
  var CONSTRUCTED_ONLY_UNIVERSAL_TAGS = Object.create(null);
101
102
  Object.keys(UNIVERSAL_TYPES).forEach(function (k) {
@@ -622,6 +623,15 @@ function _decodePrintable(buf) {
622
623
  return s;
623
624
  }
624
625
 
626
+ // NumericString (X.680 sec. 41, Table 9) permits ONLY the digits and SPACE. It carries the X.520 syntax of
627
+ // the x121Address and internationalISDNNumber directory attributes, so a value of this type must be readable
628
+ // -- and strictly, since anything outside that alphabet is not a valid encoding of the type.
629
+ function _decodeNumeric(buf) {
630
+ var s = buf.toString("latin1");
631
+ if (!NUMERIC_RE.test(s)) throw new Asn1Error("asn1/bad-numeric-string", "NumericString has characters outside the digits-and-space set");
632
+ return s;
633
+ }
634
+
625
635
  // UTF8String must be well-formed UTF-8; a lenient decoder substitutes U+FFFD
626
636
  // for invalid sequences, silently mangling hostile input into valid text.
627
637
  function _decodeUtf8Strict(buf) {
@@ -637,6 +647,7 @@ function readString(node) {
637
647
  _expectPrimitive(node, "readString");
638
648
  switch (node.tagNumber) {
639
649
  case TAGS.UTF8_STRING: return _decodeUtf8Strict(node.content);
650
+ case TAGS.NUMERIC_STRING: return _decodeNumeric(node.content);
640
651
  case TAGS.PRINTABLE_STRING: return _decodePrintable(node.content);
641
652
  case TAGS.IA5_STRING: return _decodeIa5(node.content);
642
653
  case TAGS.TELETEX_STRING: return _decodeText(node.content, "latin1");
@@ -870,6 +881,7 @@ function encodeOidContent(dotted) {
870
881
  }
871
882
 
872
883
  var PRINTABLE_RE = /^[A-Za-z0-9 '()+,\-./:=?]*$/;
884
+ var NUMERIC_RE = /^[0-9 ]*$/;
873
885
 
874
886
  function _fmtTwo(n) { return (n < 10 ? "0" : "") + n; }
875
887
 
package/lib/oid.js CHANGED
@@ -60,6 +60,7 @@ var FAMILIES = {
60
60
 
61
61
  // RFC 5280 certificate extensions.
62
62
  certExtension: { base: [2, 5, 29], of: {
63
+ subjectDirectoryAttributes: 9, // id-ce 9 (RFC 5280 sec. 4.2.1.8) -> 2.5.29.9
63
64
  subjectKeyIdentifier: 14, keyUsage: 15, subjectAltName: 17, issuerAltName: 18,
64
65
  basicConstraints: 19,
65
66
  // CRL + CRL-entry extensions (RFC 5280 sec. 5.2, sec. 5.3)
@@ -57,10 +57,12 @@ var PK_ALG_BY_INT = {
57
57
  // EC curve -> field size in bytes (the SEC1 coordinate width), for point-length validation.
58
58
  var EC_FIELD_BYTES = { "prime256v1": 32, "secp384r1": 48, "secp521r1": 66 };
59
59
 
60
- // sec. 8.6 RDN attribute types (abs(int) -> name; the sign selects the X.509 string type). emailAddress
61
- // (int 0) is an IA5String value not covered by the DirectoryString SpecialText codec below, so it is
62
- // declined; a DN carrying it encodes via the ~oid attribute form, not this alias table.
60
+ // sec. 8.6 RDN attribute types (abs(int) -> name; the sign selects the X.509 string type: positive utf8String,
61
+ // negative printableString). An attribute whose value is ALWAYS an IA5String is instead "unambiguously
62
+ // represented using a non-negative int" (draft sec. 3.1.4) -- emailAddress is int 0, so its value reconstructs
63
+ // as an IA5String and its sign carries no string-type meaning.
63
64
  var ATTR_BY_INT = {
65
+ 0: _name("emailAddress"),
64
66
  1: _name("commonName"),
65
67
  2: _name("surname"),
66
68
  3: _name("serialNumber"),
@@ -87,6 +89,7 @@ var EXT_BY_INT = {
87
89
  7: _name("authorityKeyIdentifier"),
88
90
  8: _name("extKeyUsage"),
89
91
  9: _name("authorityInfoAccess"),
92
+ 24: _name("subjectDirectoryAttributes"),
90
93
  25: _name("issuerAltName"),
91
94
  26: _name("nameConstraints"),
92
95
  27: _name("policyMappings"),
@@ -104,7 +107,7 @@ var EXT_COMPACT = {
104
107
  extKeyUsage: 1, inhibitAnyPolicy: 1, ocspNoCheck: 1, tlsFeature: 1,
105
108
  subjectAltName: 1, issuerAltName: 1, nameConstraints: 1, cRLDistributionPoints: 1,
106
109
  freshestCRL: 1, authorityInfoAccess: 1, subjectInfoAccess: 1, certificatePolicies: 1,
107
- policyMappings: 1, policyConstraints: 1,
110
+ policyMappings: 1, policyConstraints: 1, subjectDirectoryAttributes: 1,
108
111
  };
109
112
  // sec. 8.12 Extended Key Usages registry (C509 int -> registered id-kp purpose name). A KeyPurposeId
110
113
  // outside this set encodes as an unwrapped ~oid; a C509 int outside it fails closed on decode.
@@ -240,14 +243,36 @@ function _specialText(node) {
240
243
 
241
244
  // A single Name (sec. 3.1.4/sec. 3.1.6): the CBOR simple null (issuer only) | a bare SpecialText single
242
245
  // commonName | an array of RDNAttributes. Surfaces { dn, rdns, eui64? } shape-compatible with x509.
246
+ // An rdn's (attribute type, sign) pair DECLARES an X.509 string type, so its text must be valid for that type --
247
+ // a printableString sign over a non-PrintableString character, a non-ASCII emailAddress (IA5String-only), or a
248
+ // value outside the type's SIZE. Assert it by running the SAME builder the reconstruction uses, so a Name this
249
+ // parse accepts is always one it can rebuild, and a natively-signed certificate (which never reconstructs) is
250
+ // held to the identical rule. Applied to BOTH Name forms -- the array form and the bare single-commonName form.
251
+ // The builder's asn1/* fault is re-thrown in this module's domain rather than leaking onto the parse surface.
252
+ function _assertAttrValue(rdn) {
253
+ try { _reconAttrValue(rdn); }
254
+ catch (e) {
255
+ if (e instanceof C509Error) throw e;
256
+ throw _err("c509/bad-name", "the " + rdn.type + " value is not valid for the string type its attribute integer declares", e);
257
+ }
258
+ }
259
+
243
260
  function _name509(node, isSubject) {
244
261
  if (!isSubject && node.majorType === 7 && node.ai === 22) return null; // issuer == subject (self-signed)
245
262
  // A bare SpecialText (not an array) is a single commonName attribute (attributeType == +1).
246
263
  if (node.majorType === 3 || node.majorType === 2 || node.majorType === 6) {
247
264
  var sv = _specialText(node);
248
- if (sv.eui64) return { rdns: [{ type: "commonName", eui64: sv.eui64 }], eui64: sv.eui64, dn: "CN=" + _macToEui64String(sv.eui64) };
265
+ // The bare form is a single commonName -- hold its value to the SAME rules the array form applies, so a
266
+ // natively-signed certificate (which never reconstructs) cannot carry a value the reconstruction would refuse.
267
+ if (sv.eui64) {
268
+ var euiRdn = { type: "commonName", eui64: sv.eui64 };
269
+ _assertAttrValue(euiRdn);
270
+ return { rdns: [euiRdn], eui64: sv.eui64, dn: "CN=" + _macToEui64String(sv.eui64) };
271
+ }
249
272
  var val = sv.text !== undefined ? sv.text : sv.hex;
250
- return { rdns: [{ type: "commonName", value: val }], dn: "CN=" + val };
273
+ var bareRdn = { type: "commonName", value: val };
274
+ _assertAttrValue(bareRdn);
275
+ return { rdns: [bareRdn], dn: "CN=" + val };
251
276
  }
252
277
  if (node.majorType !== 4) throw _err("c509/bad-name", "a C509 Name must be null, a SpecialText, or an array of RDN attributes");
253
278
  var rdns = [];
@@ -268,7 +293,13 @@ function _name509(node, isSubject) {
268
293
  if (tname === undefined) throw _err("c509/bad-name", "attribute type integer " + ti + " has no C509 registry row");
269
294
  var v = _specialText(kids[i + 1]);
270
295
  var vv = v.text !== undefined ? v.text : (v.hex !== undefined ? v.hex : _macToEui64String(v.eui64));
271
- rdns.push({ type: tname, value: vv, printable: ti < 0 });
296
+ // The RENDERED text is what carries into the rdn -- deliberately NOT the raw eui64, because the EUI shortcut
297
+ // in _reconAttrValue is unconditional and would take precedence over the attribute's declared string type,
298
+ // rebuilding a tag-48 emailAddress or countryName as a UTF8String. Only the bare-SpecialText form above is
299
+ // always a commonName and may carry it; here the value is held to whatever type the attribute integer declares.
300
+ var rdn = { type: tname, value: vv, printable: ti < 0 };
301
+ _assertAttrValue(rdn);
302
+ rdns.push(rdn);
272
303
  parts.push(_shortName(tname) + "=" + vv);
273
304
  }
274
305
  return { rdns: rdns, dn: parts.join(",") };
@@ -420,8 +451,8 @@ function _otherNameToDer(node, intVal) {
420
451
  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");
421
452
  typeId = _oidName(node.children[0], "c509/bad-extensions", "an otherName type-id").oid;
422
453
  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)");
423
- inner = node.children[1].content;
424
- try { asn1.decode(inner); } catch (e) { throw _err("c509/bad-extensions", "an otherName value is not a single well-formed DER element", e); }
454
+ // The [0] EXPLICIT inner TLV is an ANY spliced verbatim -- strict-validate it, not just its framing.
455
+ inner = _requireStrictDerTlv(node.children[1].content, "c509/bad-extensions", "an otherName value");
425
456
  } else if (intVal === -1) { // id-on-hardwareModuleName: [ ~oid(hwType), bytes(hwSerialNum) ] -> HardwareModuleName
426
457
  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");
427
458
  if (node.children[1].majorType !== 2) throw _err("c509/bad-extensions", "a hardwareModuleName hwSerialNum must be a CBOR byte string");
@@ -802,6 +833,8 @@ function _extValueToDer(name, node) {
802
833
  if (pcFields.length === 0) throw _err("c509/bad-extensions", "policyConstraints must contain requireExplicitPolicy or inhibitPolicyMapping (RFC 5280 sec. 4.2.1.11)");
803
834
  return b.sequence(pcFields); // positional slot -> tag makes [0] < [1] unique + ascending by construction
804
835
  }
836
+ case "subjectDirectoryAttributes": // [ type, values, type, values, ... ] -> SEQUENCE OF Attribute (sec. 3.3)
837
+ return _sdaToDer(node);
805
838
  default:
806
839
  throw _err("c509/bad-extensions", "extension " + name + " has no compact value decoder");
807
840
  }
@@ -964,6 +997,8 @@ function _extValueFromDer(name, der) {
964
997
  }
965
998
  return cbor.build.array([repC, ipmC]);
966
999
  }
1000
+ case "subjectDirectoryAttributes": // SEQUENCE OF Attribute -> [ type, values, ... ]
1001
+ return _sdaFromDer(node);
967
1002
  default:
968
1003
  return null;
969
1004
  }
@@ -972,6 +1007,219 @@ function _extValueFromDer(name, der) {
972
1007
  }
973
1008
  }
974
1009
 
1010
+ // ---- subjectDirectoryAttributes compact value codec (draft-20 sec. 3.3 / 8.6 / 8.8, RFC 5280 sec. 4.2.1.8) ----
1011
+ // SubjectDirectoryAttributes ::= SEQUENCE SIZE(1..MAX) OF Attribute ::= SEQUENCE { type, values SET OF value }.
1012
+ // The compact form is a flat CBOR array [ type1, values1, ... ] where each values slot is ITSELF a non-empty
1013
+ // CBOR array -- an Attribute's SET may hold more than one value, unlike a Name's single-value RDN. type is a
1014
+ // sec. 8.6 registry int (the sign selects the string type) with text values, or an unwrapped ~oid with raw DER
1015
+ // value bytes (an unregistered type stays compact via its own ~oid form, never dropping the whole extension).
1016
+ // The extension-level _tryCompactExtValue round-trip guard is the byte-exactness net.
1017
+
1018
+ // A ~oid-form attributeValue must be exactly ONE non-empty, well-formed DER AttributeValue TLV. An empty byte
1019
+ // string would build a SET OF SIZE 0 (RFC 5280 sec. 4.2.1.8 requires at least one value); a byte string carrying
1020
+ // more than one TLV would fan one declared value into several SET members (draft-20 sec. 3.3: one `bytes` is one
1021
+ // AttributeValue). There is no downstream DER decoder to catch either (SDA ships c509-only), so the reconstruct
1022
+ // arm self-enforces the single-TLV shape rather than splice a degenerate or invalid Attribute.
1023
+ // ---- the shared strict-DER gate for a raw-spliced ANY value --------------------------------------
1024
+ // Several reconstruct sites splice CALLER/ATTACKER-supplied bytes into the rebuilt certificate verbatim (an
1025
+ // AlgorithmIdentifier's `parameters`, a generic otherName's `value [0] EXPLICIT ANY`, a subjectDirectoryAttributes
1026
+ // `~oid`-form AttributeValue). asn1.decode validates TLV FRAMING ONLY -- it rejects truncation, trailing bytes and
1027
+ // non-minimal lengths, and enforces the DER primitive/constructed rules, but it does NOT check per-type CONTENT:
1028
+ // it frames an empty INTEGER, the reserved end-of-contents tag 0, and an out-of-alphabet string quite happily.
1029
+ // Splicing those produces a certificate an independent decoder refuses to load, and one this toolkit's OWN readers
1030
+ // reject -- so every such site routes through the one gate below instead of a bare asn1.decode.
1031
+
1032
+ // Every universal PRIMITIVE tag whose content this toolkit can strictly validate, mapped to its reader. A
1033
+ // universal primitive OUTSIDE this map has no strict content validator here, so it is REJECTED rather than
1034
+ // spliced unchecked. The documented residual: a value whose type this toolkit cannot yet strictly validate is
1035
+ // not compact-representable -- NumericString / VideotexString / GraphicString / GeneralString (no content reader),
1036
+ // and a fractional-seconds GeneralizedTime (the X.690 sec. 11.7 relaxation is deliberately scoped to the codec
1037
+ // and RFC 3161 timestamping, and must not creep into a third consumer). On the ENCODE path such a value simply
1038
+ // degrades to the byte-exact ~oid + byte-string form, losing nothing; on the DECODE path it is a fail-closed
1039
+ // verdict rather than a guess.
1040
+ var _ANY_VALUE_READERS = (function () {
1041
+ var m = {}, R = asn1.read, T = asn1.TAGS;
1042
+ m[T.BOOLEAN] = R.boolean; m[T.INTEGER] = R.integer; m[T.ENUMERATED] = R.enumerated;
1043
+ m[T.BIT_STRING] = R.bitString; m[T.OCTET_STRING] = R.octetString; m[T.NULL] = R.nullValue;
1044
+ m[T.OBJECT_IDENTIFIER] = R.oid; m[T.UTC_TIME] = R.time; m[T.GENERALIZED_TIME] = R.time;
1045
+ [T.UTF8_STRING, T.PRINTABLE_STRING, T.NUMERIC_STRING, T.IA5_STRING, T.TELETEX_STRING, T.VISIBLE_STRING, T.BMP_STRING, T.UNIVERSAL_STRING].forEach(function (t) { m[t] = R.string; });
1046
+ return m;
1047
+ })();
1048
+
1049
+ // A universal SET's required member order depends on a type the ANY does not carry: X.690 sec. 11.6 orders a
1050
+ // SET OF by the members' full encodings, while a structured SET is ordered by TAG (X.680 sec. 8.6) -- and the two
1051
+ // differ whenever the constructed bit does (a SEQUENCE member, tag 16, sorts BEFORE a PrintableString, tag 19, by
1052
+ // tag but AFTER it by octets). A structured SET cannot repeat a tag, so a repeated tag proves SET OF and the
1053
+ // octet rule binds; with all-distinct tags either reading is possible, so accept a value that satisfies EITHER
1054
+ // (rejecting only what is non-canonical under BOTH readings -- sound in both directions, never a guess).
1055
+ var _TAG_CLASS_RANK = { universal: 0, application: 1, context: 2, private: 3 };
1056
+ function _setOrderOk(kids) {
1057
+ var i, dup = false, seen = {};
1058
+ for (i = 0; i < kids.length; i++) {
1059
+ var key = kids[i].tagClass + ":" + kids[i].tagNumber;
1060
+ if (seen[key]) { dup = true; break; }
1061
+ seen[key] = true;
1062
+ }
1063
+ var octetAsc = true, tagAsc = true;
1064
+ for (i = 1; i < kids.length; i++) {
1065
+ if (Buffer.compare(kids[i - 1].bytes, kids[i].bytes) > 0) octetAsc = false;
1066
+ // Tag order ranks by CLASS first (universal < application < context < private, X.680 sec. 8.6), then by tag
1067
+ // number -- compare the class's NUMBER, never its name (the names do not sort in class order).
1068
+ var pc = _TAG_CLASS_RANK[kids[i - 1].tagClass], cc = _TAG_CLASS_RANK[kids[i].tagClass];
1069
+ if (pc !== cc ? pc > cc : kids[i - 1].tagNumber > kids[i].tagNumber) tagAsc = false;
1070
+ }
1071
+ return dup ? octetAsc : (octetAsc || tagAsc);
1072
+ }
1073
+
1074
+ // Strict-validate a decoded DER element at ANY depth, in the caller's error domain. Rejects the reserved EOC tag
1075
+ // 0; runs a universal primitive through its strict content reader (or rejects a type with none); recurses into a
1076
+ // constructed element's children; and holds a universal SET to a canonical order. Only SEQUENCE and SET are
1077
+ // accepted as universal CONSTRUCTED types -- an EXTERNAL / EMBEDDED PDV / CHARACTER STRING has mandatory
1078
+ // components this gate cannot verify (the degenerate empty form is not a valid encoding of any of them), so it is
1079
+ // refused for the same reason an unvalidatable primitive is. A NON-universal element (a legitimately context- or
1080
+ // application-tagged ANY) passes on its framing, but its constructed children are still walked.
1081
+ function _strictDerElement(node, code, label) {
1082
+ if (node.tagClass === "universal" && node.tagNumber === 0) throw _err(code, label + " must not use the reserved end-of-contents encoding (tag 0)");
1083
+ if (node.constructed) {
1084
+ if (node.tagClass === "universal" && node.tagNumber !== asn1.TAGS.SEQUENCE && node.tagNumber !== asn1.TAGS.SET) {
1085
+ throw _err(code, label + " of universal constructed type " + node.tagNumber + " has no strict DER structure validator here");
1086
+ }
1087
+ var kids = node.children; // asn1.decode always sets a (possibly empty) children array on a constructed node
1088
+ for (var i = 0; i < kids.length; i++) _strictDerElement(kids[i], code, label);
1089
+ if (node.tagClass === "universal" && node.tagNumber === asn1.TAGS.SET && !_setOrderOk(kids)) {
1090
+ throw _err(code, label + " has a SET whose members are in no canonical DER order (X.690 sec. 11.6 / X.680 sec. 8.6)");
1091
+ }
1092
+ return;
1093
+ }
1094
+ if (node.tagClass === "universal") {
1095
+ // Validate-or-reject: a universal primitive with no strict content reader is NOT accepted on framing alone
1096
+ // (asn1.decode frames a malformed NumericString "12 01 40" happily), so the map is exhaustive by refusal.
1097
+ var reader = _ANY_VALUE_READERS[node.tagNumber];
1098
+ if (!reader) throw _err(code, label + " of universal type " + node.tagNumber + " has no strict DER content validator here");
1099
+ try { reader(node); } catch (e) { throw _err(code, label + " is not a valid DER element for its type", e); }
1100
+ }
1101
+ }
1102
+ // Raw ANY bytes about to be spliced verbatim must be exactly ONE non-empty, well-formed AND strictly-valid DER
1103
+ // element: framing + no-trailing-data via asn1.decode, then content / structure / SET order via
1104
+ // _strictDerElement, both reported in the CALLER's error domain. Returns the bytes so a call site can wrap a
1105
+ // splice inline. Used by every reconstruct site that emits caller-supplied ANY bytes verbatim.
1106
+ function _requireStrictDerTlv(content, code, label) {
1107
+ if (content.length === 0) throw _err(code, label + " must be a non-empty DER element");
1108
+ var node;
1109
+ try { node = asn1.decode(content); } catch (e) { throw _err(code, label + " must be exactly one well-formed DER element (no trailing data)", e); }
1110
+ _strictDerElement(node, code, label);
1111
+ return content;
1112
+ }
1113
+
1114
+ // asn1.build.set SORTS its members, so handing it a non-canonically-ordered values list would SILENTLY rewrite
1115
+ // the declared order -- many distinct C509 encodings would reconstruct one and the same certificate. Require the
1116
+ // declared order to already BE canonical, so the compact form maps one-to-one onto the DER it rebuilds.
1117
+ function _derSetInDeclaredOrder(vals) {
1118
+ for (var i = 1; i < vals.length; i++) {
1119
+ if (Buffer.compare(vals[i - 1], vals[i]) > 0) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes attributeValue list must be in DER ascending order (X.690 sec. 11.6)");
1120
+ }
1121
+ return b.set(vals);
1122
+ }
1123
+
1124
+ // [ type1, values1, ... ] -> the DER SubjectDirectoryAttributes. A malformed native value fails closed
1125
+ // (c509/bad-extensions); on the encode path the same throw is a round-trip mismatch -> whole-ext ~oid fallback.
1126
+ function _sdaToDer(node) {
1127
+ if (node.majorType !== 4 || !node.children) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes value must be a CBOR array");
1128
+ var kids = node.children;
1129
+ if (kids.length === 0 || kids.length % 2 !== 0) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes array must be non-empty (attributeType, attributeValue) pairs (sec. 3.3)");
1130
+ var attrs = [];
1131
+ for (var i = 0; i + 1 < kids.length; i += 2) {
1132
+ var typeNode = kids[i], valuesNode = kids[i + 1];
1133
+ if (valuesNode.majorType !== 4 || !valuesNode.children || valuesNode.children.length < 1) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes attributeValue must be a non-empty CBOR array (SET OF, SIZE 1..MAX, RFC 5280 sec. 4.2.1.8)");
1134
+ var vals = [];
1135
+ if (typeNode.majorType === 0 || typeNode.majorType === 1) { // int form: a sec. 8.6 registry alias, text values
1136
+ var ti = Number(cbor.read.int(typeNode));
1137
+ var tname = ATTR_BY_INT[Math.abs(ti)];
1138
+ if (tname === undefined) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes attribute type int " + ti + " has no C509 sec. 8.6 registry row");
1139
+ // countryName / serialNumber are PrintableString-restricted -- their int MUST carry the negative
1140
+ // (printableString) sign. A non-negative sign is ambiguous/nonconformant: fail closed rather than
1141
+ // silently coerce the value to PrintableString (the ~oid form is the escape hatch for a genuine odd value).
1142
+ if ((tname === "countryName" || tname === "serialNumber") && ti >= 0) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes " + tname + " must carry the negative (printableString) sign (it is PrintableString-restricted)");
1143
+ for (var vi = 0; vi < valuesNode.children.length; vi++) {
1144
+ var vn = valuesNode.children[vi];
1145
+ if (vn.majorType !== 3) throw _err("c509/bad-extensions", "a subjectDirectoryAttributes int-form attribute value must be a CBOR text string (a non-string value requires the ~oid form)");
1146
+ // _reconAttrValue -> b.printable / b.utf8 throws only an Asn1Error for a value outside its string type's
1147
+ // alphabet (a printableString sign over non-PrintableString characters); remap it to this module's
1148
+ // domain so attacker-controlled native input fails closed as c509/bad-extensions, not a leaked asn1/*.
1149
+ try { vals.push(_reconAttrValue({ type: tname, value: cbor.read.textString(vn), printable: ti < 0 })); }
1150
+ catch (e) { throw _err("c509/bad-extensions", "a subjectDirectoryAttributes " + tname + " value is not valid for its string type", e); }
1151
+ }
1152
+ attrs.push(b.sequence([b.oid(oid.byName(tname)), _derSetInDeclaredOrder(vals)]));
1153
+ } else if (typeNode.majorType === 2) { // ~oid form: raw AttributeValue TLVs
1154
+ var dotted = _oidName(typeNode, "c509/bad-extensions", "a subjectDirectoryAttributes attribute type").oid;
1155
+ for (var vj = 0; vj < valuesNode.children.length; vj++) {
1156
+ var vb = valuesNode.children[vj];
1157
+ if (vb.majorType !== 2) throw _err("c509/bad-extensions", "a ~oid-form subjectDirectoryAttributes value must be a CBOR byte string (a raw DER AttributeValue)");
1158
+ vals.push(b.raw(_requireStrictDerTlv(vb.content, "c509/bad-extensions", "a ~oid-form subjectDirectoryAttributes AttributeValue")));
1159
+ }
1160
+ attrs.push(b.sequence([b.oid(dotted), _derSetInDeclaredOrder(vals)]));
1161
+ } else {
1162
+ throw _err("c509/bad-extensions", "a subjectDirectoryAttributes attribute type must be a CBOR integer (sec. 8.6) or a ~oid");
1163
+ }
1164
+ }
1165
+ return b.sequence(attrs);
1166
+ }
1167
+
1168
+ // One DER Attribute's SET members -> the int-form { sign, values:[CBOR text] }, or null when the int form is
1169
+ // not confidently byte-exact (an unregistered type, a non-utf8/printable value, or a SET mixing utf8String and
1170
+ // printableString -- one sign cannot express it). A null routes that ONE attribute to the always-byte-exact
1171
+ // ~oid + raw-bytes form (a still-compact sec. 3.3 SDA form); the extension round-trip guard is the ultimate net.
1172
+ function _sdaTryIntForm(tint, name, valueNodes) {
1173
+ if (tint === undefined) return null;
1174
+ var i;
1175
+ // An IA5String-only attribute (emailAddress) rides its non-negative int with IA5String values -- its type,
1176
+ // not a sign, fixes the string type, so it is matched separately from the utf8/printable sign convention.
1177
+ if (name === "emailAddress") {
1178
+ var mails = [];
1179
+ for (i = 0; i < valueNodes.length; i++) {
1180
+ if (valueNodes[i].tagClass !== "universal" || valueNodes[i].tagNumber !== asn1.TAGS.IA5_STRING) return null;
1181
+ mails.push(cbor.build.textString(asn1.read.string(valueNodes[i])));
1182
+ }
1183
+ return { sign: 1, values: mails };
1184
+ }
1185
+ var sign = 0, out = []; // sign: -1 printableString, +1 utf8String
1186
+ for (i = 0; i < valueNodes.length; i++) {
1187
+ var vn = valueNodes[i], s;
1188
+ if (vn.tagClass === "universal" && vn.tagNumber === asn1.TAGS.UTF8_STRING) s = 1;
1189
+ else if (vn.tagClass === "universal" && vn.tagNumber === asn1.TAGS.PRINTABLE_STRING) s = -1;
1190
+ else return null; // any other value tag -> the ~oid form preserves it exactly
1191
+ if (sign === 0) sign = s;
1192
+ else if (sign !== s) return null; // a mixed utf8/printable SET -- the headline trap
1193
+ out.push(cbor.build.textString(asn1.read.string(vn)));
1194
+ }
1195
+ return { sign: sign, values: out };
1196
+ }
1197
+
1198
+ // The DER SubjectDirectoryAttributes -> the flat compact CBOR array, or null (whole-ext ~oid fallback). Per
1199
+ // attribute: the int form when the type is sec. 8.6-registered AND every value is a single-signed utf8/
1200
+ // printable string, else the ~oid + raw-bytes form (always byte-exact).
1201
+ function _sdaFromDer(node) {
1202
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || !node.children || node.children.length < 1) return null;
1203
+ var out = [];
1204
+ for (var ai = 0; ai < node.children.length; ai++) {
1205
+ var attr = node.children[ai];
1206
+ if (attr.tagClass !== "universal" || attr.tagNumber !== asn1.TAGS.SEQUENCE || !attr.children || attr.children.length !== 2) return null;
1207
+ var setNode = attr.children[1];
1208
+ if (setNode.tagClass !== "universal" || setNode.tagNumber !== asn1.TAGS.SET || !setNode.children || setNode.children.length < 1) return null; // SET OF, non-empty
1209
+ var dotted = asn1.read.oid(attr.children[0]);
1210
+ var nm = oid.name(dotted), tint = nm != null ? ATTR_TO_INT[nm] : undefined;
1211
+ var intForm = _sdaTryIntForm(tint, nm, setNode.children);
1212
+ if (intForm != null) {
1213
+ out.push(cbor.build.int(BigInt(intForm.sign < 0 ? -tint : tint)));
1214
+ out.push(cbor.build.array(intForm.values));
1215
+ } else {
1216
+ out.push(_oidCbor(dotted));
1217
+ out.push(cbor.build.array(setNode.children.map(function (v) { return cbor.build.byteString(v.bytes); })));
1218
+ }
1219
+ }
1220
+ return cbor.build.array(out);
1221
+ }
1222
+
975
1223
  // Encode-side guard: emit the compact value only when it decodes back to the EXACT DER extnValue, so a
976
1224
  // non-canonical or unrepresentable value can never produce a lossy compact form (it falls back to ~oid).
977
1225
  // A value _extValueFromDer produced but _extValueToDer cannot reconstruct -- an empty GeneralNames the DER
@@ -1063,9 +1311,20 @@ function _macToEui64String(buf) {
1063
1311
  // One RDN attribute value -> its DER string. The C509 sign convention: a positive attribute int ->
1064
1312
  // utf8String, a negative -> printableString; countryName / serialNumber are PrintableString-restricted.
1065
1313
  function _reconAttrValue(rdn) {
1066
- if (rdn.eui64) return b.utf8(_macToEui64String(rdn.eui64));
1067
- if (rdn.type === "countryName" || rdn.type === "serialNumber") return b.printable(String(rdn.value));
1068
- return rdn.printable ? b.printable(String(rdn.value)) : b.utf8(String(rdn.value));
1314
+ if (rdn.eui64) return b.utf8(_macToEui64String(rdn.eui64)); // the bare-form commonName MAC (a fixed 17 chars)
1315
+ var s = String(rdn.value);
1316
+ // The attribute's registered ASN.1 type carries a SIZE constraint as well as an alphabet, and a value outside
1317
+ // it is not a valid encoding of that type: every DirectoryString-valued attribute is SIZE (1..MAX) and
1318
+ // emailAddress is IA5String SIZE (1..MAX) (RFC 5280 App. A.1), so an empty value is never valid; countryName
1319
+ // "SHALL have length 2" (draft sec. 3.1.4, X.520 SIZE (2)). Enforced HERE, the single place the value is built
1320
+ // for its declared type, so the decode, reconstruct and subject-directory-attributes paths cannot disagree.
1321
+ if (s.length === 0) throw _err("c509/bad-name", "a " + rdn.type + " value must be non-empty (SIZE (1..MAX))");
1322
+ if (rdn.type === "countryName" && s.length !== 2) throw _err("c509/bad-name", "a countryName value must have length 2 (draft sec. 3.1.4)");
1323
+ // emailAddress is an IA5String-only attribute (draft sec. 3.1.4): its type, not the int's sign, fixes the
1324
+ // string type, so it reconstructs as an IA5String whichever way the non-negative int was written.
1325
+ if (rdn.type === "emailAddress") return b.ia5(s);
1326
+ if (rdn.type === "countryName" || rdn.type === "serialNumber") return b.printable(s);
1327
+ return rdn.printable ? b.printable(s) : b.utf8(s);
1069
1328
  }
1070
1329
 
1071
1330
  // A Name -> the DER RDNSequence (SEQUENCE OF SET OF SEQUENCE{ type, value }); one attribute per RDN.
@@ -1149,13 +1408,11 @@ function _reconExtensions(exts) {
1149
1408
  function _reconAlgId(alg) {
1150
1409
  var fields = [b.oid(alg.oid)];
1151
1410
  if (alg.parameters && alg.parameters.length) {
1152
- // AlgorithmIdentifier.parameters is ANY -- exactly one well-formed DER element. Validate the supplied
1153
- // bytes decode as a single element (the strict decoder rejects trailing bytes / malformed encodings)
1154
- // before re-emitting them, so a malformed or multi-element parameter blob fails closed rather than
1155
- // producing an invalid reconstructed AlgorithmIdentifier.
1156
- try { asn1.decode(alg.parameters); }
1157
- catch (e) { throw _err("c509/non-invertible", "algorithm parameters are not a single well-formed DER element", e); }
1158
- fields.push(b.raw(alg.parameters));
1411
+ // AlgorithmIdentifier.parameters is ANY, spliced verbatim -- so it gets the SAME strict gate as every other
1412
+ // raw-ANY splice, not just a framing check. Framing alone admits an empty INTEGER / reserved tag 0 / a
1413
+ // non-minimal INTEGER, each of which reconstructs an AlgorithmIdentifier that an independent X.509 decoder
1414
+ // refuses to load and that this toolkit's own readers reject.
1415
+ fields.push(b.raw(_requireStrictDerTlv(alg.parameters, "c509/non-invertible", "algorithm parameters")));
1159
1416
  }
1160
1417
  return b.sequence(fields);
1161
1418
  }
@@ -1226,6 +1483,9 @@ function parse(input) {
1226
1483
  var f = root.children;
1227
1484
  if (f.length !== 11) throw _err("c509/bad-tbs", "a C509 certificate must be an array of exactly 11 elements, got " + f.length);
1228
1485
 
1486
+ // The type slot must be a CBOR integer -- guard the major type here so a text/array/byte-string field 0 fails
1487
+ // in this module's domain rather than leaking cbor.read.int's cbor/unexpected-major fault to the caller.
1488
+ if (f[0].majorType !== 0 && f[0].majorType !== 1) throw _err("c509/bad-certificate-type", "c509CertificateType must be a CBOR integer");
1229
1489
  var type = Number(cbor.read.int(f[0]));
1230
1490
  if (type !== 2 && type !== 3) throw _err("c509/bad-certificate-type", "c509CertificateType must be 2 (native) or 3 (re-encoded), got " + type);
1231
1491
 
@@ -1492,6 +1752,11 @@ function _c509NameFromDer(nameBytes) {
1492
1752
  if (attrName == null || ATTR_TO_INT[attrName] === undefined) throw _err("c509/non-invertible", "attribute type " + attrName + " has no C509 registry integer");
1493
1753
  var valNode = attr.children[1];
1494
1754
  var value = asn1.read.string(valNode);
1755
+ // The IA5String type belongs ONLY to an IA5-only attribute (emailAddress, draft sec. 3.1.4), whose value
1756
+ // reconstructs from its type rather than the int's sign. Refuse either mismatch here with a precise verdict
1757
+ // instead of emitting an int form whose reconstruction would differ from the source bytes.
1758
+ var isIa5 = valNode.tagClass === "universal" && valNode.tagNumber === asn1.TAGS.IA5_STRING;
1759
+ if ((attrName === "emailAddress") !== isIa5) throw _err("c509/non-invertible", "attribute " + attrName + " carries a " + (isIa5 ? "IA5String" : "non-IA5String") + " value the C509 sec. 8.6 int form cannot represent");
1495
1760
  var eui = attrName === "commonName" ? _euiFromCn(value) : null;
1496
1761
  if (eui) rdns.push({ type: attrName, value: value, eui64: eui }); // tag-48 MAC commonName
1497
1762
  else rdns.push({ type: attrName, value: value, printable: valNode.tagNumber === asn1.TAGS.PRINTABLE_STRING });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blamejs/pki",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Pure-JavaScript PKI toolkit that owns its stack — X.509, ASN.1/DER, CMS, PQC-first.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "blamejs contributors",
@@ -68,6 +68,8 @@
68
68
  "coverage": "c8 --include=lib/** --include=index.js --reporter=text-summary --reporter=lcov node test/smoke.js",
69
69
  "check:swallows": "node scripts/check-swallow-coverage.js",
70
70
  "coverage:gated": "npm run coverage && npm run check:swallows",
71
+ "coverage:unified": "node scripts/coverage-unified.js",
72
+ "coverage:unified:gated": "npm run coverage:unified && npm run check:swallows",
71
73
  "prepack": "node scripts/check-pack-against-gitignore.js",
72
74
  "check:vendor-currency": "node scripts/check-vendor-currency.js"
73
75
  },
package/sbom.cdx.json CHANGED
@@ -2,10 +2,10 @@
2
2
  "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
3
3
  "bomFormat": "CycloneDX",
4
4
  "specVersion": "1.5",
5
- "serialNumber": "urn:uuid:40b5fc50-7b82-41d2-9e80-f01b6be97878",
5
+ "serialNumber": "urn:uuid:62c970a5-3536-480a-8269-a0c5d85b82eb",
6
6
  "version": 1,
7
7
  "metadata": {
8
- "timestamp": "2026-08-06T23:41:09.454Z",
8
+ "timestamp": "2026-08-08T02:38:25.668Z",
9
9
  "lifecycles": [
10
10
  {
11
11
  "phase": "build"
@@ -19,14 +19,14 @@
19
19
  }
20
20
  ],
21
21
  "component": {
22
- "bom-ref": "@blamejs/pki@0.4.0",
22
+ "bom-ref": "@blamejs/pki@0.4.1",
23
23
  "type": "application",
24
24
  "name": "pki",
25
- "version": "0.4.0",
25
+ "version": "0.4.1",
26
26
  "scope": "required",
27
27
  "author": "blamejs contributors",
28
28
  "description": "Pure-JavaScript PKI toolkit that owns its stack — X.509, ASN.1/DER, CMS, PQC-first.",
29
- "purl": "pkg:npm/%40blamejs/pki@0.4.0",
29
+ "purl": "pkg:npm/%40blamejs/pki@0.4.1",
30
30
  "properties": [],
31
31
  "externalReferences": [
32
32
  {
@@ -54,7 +54,7 @@
54
54
  "components": [],
55
55
  "dependencies": [
56
56
  {
57
- "ref": "@blamejs/pki@0.4.0",
57
+ "ref": "@blamejs/pki@0.4.1",
58
58
  "dependsOn": []
59
59
  }
60
60
  ]