@blamejs/pki 0.3.12 → 0.3.14

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,7 +4,26 @@ 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.3.12 — 2026-07-24
7
+ ## v0.3.14 — 2026-07-24
8
+
9
+ CMS AuthenticatedData is produced and verified (RFC 5652 sec. 9).
10
+
11
+ ### Added
12
+
13
+ - pki.cms.authenticate(content, recipients, opts) produces a CMS AuthenticatedData (RFC 5652 sec. 9): cleartext content authenticated by an HMAC-SHA-256/384/512 MAC, with the fresh MAC key wrapped for each recipient through the same RecipientInfo model pki.cms.encrypt uses (key-transport RSAES-OAEP, key-agreement ECDH/X25519/X448, ML-KEM ori, password pwri, key-wrap kekri). By default it MACs the authenticated attributes (content-type + message-digest of the content) re-tagged to the EXPLICIT SET OF (sec. 9.2); opts.authenticatedAttributes false MACs the content octets directly (id-data only). opts.macAlgorithm selects the HMAC hash and opts.digestAlgorithm the message-digest hash. Returns a DER Buffer or, with opts.pem, a PEM string. RFC 5652 sec. 9, RFC 2104, RFC 4231.
14
+ - pki.cms.decrypt verifies a CMS AuthenticatedData. It recovers the MAC key through the matching RecipientInfo, recomputes the HMAC over the exact RFC 5652 section 9.2 preimage, and -- when authenticated attributes are present -- independently recomputes digest(content) and confirms it equals the message-digest attribute (section 9.3, do not trust the originator's digest), before releasing the content with authenticated true and macAlgorithm / digestAlgorithm in place of contentEncryptionAlgorithm. Every secret-dependent failure -- a wrong recipient key, a forged or tampered MAC, a message-digest mismatch -- collapses to the one uniform cms/decrypt-failed verdict, so a MAC failure is indistinguishable from a key-unwrap failure and leaks no unwrap-success bit. A weak or unknown macAlgorithm (HMAC-SHA-1) is refused with a distinct cms/unsupported-algorithm before any key step.
15
+
16
+ ## v0.3.13 — 2026-07-23
17
+
18
+ CMS gains countersignatures and unsigned attributes (RFC 5652 sec. 11.4).
19
+
20
+ ### Added
21
+
22
+ - pki.cms.countersign(cms, signers, opts) adds RFC 5652 section 11.4 countersignatures to a CMS SignedData. Each countersigner is the same { cert, key, digestAlgorithm?, pss? } descriptor pki.cms.sign takes (RSA, RSASSA-PSS, ECDSA, EdDSA, ML-DSA, SLH-DSA, or composite ML-DSA), and its signature covers the countersigned SignerInfo's signature octets under the section 11.4 preimage -- message-digest bound to those octets, content-type omitted. opts.signerIndex selects which primary signer(s) to countersign (an index, an array, or 'all'), opts.countersignatureOf countersigns an existing countersignature (a nested countersignature), opts.signedAttributes false signs the target signature directly, and the countersigner certificate is embedded by default. The countersigned SignedData's original bytes are preserved so the primary signature verifies unchanged, and multiple countersignatures on one signer land as multiple values of the one id-countersignature attribute. Returns a DER Buffer or, with opts.pem, a PEM string. RFC 5652 sec. 11.4.
23
+ - pki.cms.verify surfaces countersignatures and unsigned attributes per signer. Each res.signers[i] carries countersignatures -- an array of per-countersignature verdicts { ok, sid, cert, digestAlgorithm, ... }, each verified over the exact RFC 5652 section 11.4 preimage, nested for a countersignature of a countersignature -- and unsignedAttrs, the decoded unsigned attributes with their type names. Both are unauthenticated by definition (an unsigned attribute is outside the signature) and never change signers[i].ok or res.valid; a present-but-invalid countersignature is surfaced ok:false, never silently dropped.
24
+ - pki.cms.sign gains an unsignedAttributes option: an array of { type, values } unsigned attributes placed in each SignerInfo, outside the signature. It is the vehicle for attaching an RFC 3161 timestamp token (id-aa-timeStampToken) or another unsigned attribute at signing time. content-type, message-digest, and signing-time are rejected as unsigned attributes (RFC 5652 sec. 11), as is a duplicate attribute type.
25
+
26
+ ## v0.3.12 — 2026-07-23
8
27
 
9
28
  pki.pkcs12.open reads and decrypts a password-integrity PKCS#12 store (RFC 7292, RFC 9579).
10
29
 
package/README.md CHANGED
@@ -230,7 +230,7 @@ is callable today; nothing below is a stub.
230
230
  | `pki.crl` | RFC 5280 §5 certificate revocation list issuance — `sign(spec, issuer, opts)` builds and signs a `CertificateList`: a `spec` of `thisUpdate` / `nextUpdate`, an optional `crlNumber`, a `revoked` array (each entry a `serialNumber` + `revocationDate` with an optional `reason` or `invalidityDate`), and an optional `extensions` object (authority key identifier, issuing distribution point, delta-CRL indicator, freshest CRL, authority information access) or an array of pre-encoded Extension DER; an `issuer` of `{ cert, key }` or `{ name, publicKey, key }`. The signature algorithm is resolved from the issuer key, so RSA (PKCS#1 v1.5 / PSS via `opts.pss`), ECDSA, EdDSA, ML-DSA, SLH-DSA, and the composite arms all sign without a per-algorithm branch. The version is derived from the extension set (v2 when any CRL or entry extension is present, else v1), the outer `signatureAlgorithm` matches `tbsCertList.signature`, an empty revocation list omits the field rather than emitting an empty SEQUENCE, `reasonCode` is an ENUMERATED and `invalidityDate` is always GeneralizedTime, per-extension criticality is fixed by the RFC, and the produced signature is verified under the issuer key before return. `verify(crl, issuer)` checks a CRL signature through the one path-validation signature engine (algorithm-confusion and EdDSA low-order gates included), and `isRevoked(crl, serialNumber)` looks a serial up in the revocation list. Returns DER, or a PEM `X509 CRL` with `opts.pem`; malformed input throws a typed `CrlError`. Parsing stays at `pki.schema.crl.parse` — `sign` / `verify` / `isRevoked` |
231
231
  | `pki.key` | RFC 5958 / RFC 8018 key-material lifecycle — `encrypt(privateKey, password, opts)` wraps a PKCS#8 private key (DER, PEM, or an extractable `CryptoKey`) into an `EncryptedPrivateKeyInfo` under PBES2 (PBKDF2 + AES-CBC-Pad): `opts` selects the `cipher` (`aes-256-cbc` default, `aes-192-cbc`, `aes-128-cbc`), the `prf` (`hmacWithSHA256` default, SHA-384/512, SHA-1), the `iterations` (default 600000), and the `salt`; the plaintext is validated as PKCS#8 before encryption, a default `prf` and `keyLength` are omitted so the parameters are byte-exact with OpenSSL, and the output is re-parsed before return. `decrypt(encrypted, password, opts)` recovers the inner `PrivateKeyInfo` (re-validated through `pki.schema.pkcs8.parse`) — only PBES2/PBKDF2/AES-CBC is accepted (PBES1, PBMAC1, scrypt refused), the salt and iteration count are bounded before any derivation (`opts.maxIterations` lowers the cap), a malformed parameter set or wrong-length IV is a distinct typed error, and — because a MAC-less PBES2-CBC decrypt must not be a padding oracle (RFC 8018 §8) — a wrong password and a valid-pad-but-not-a-key both surface the one uniform `key/decrypt-failed`. `export(key, opts)` / `import(input, opts)` move a private key as PKCS#8 or a public key as SubjectPublicKeyInfo, delegating the encoding to WebCrypto so RSA carries an explicit NULL, EC a named curve, and Ed25519/Ed448/X25519/X448 omit parameters (an ambiguous RSA/EC import requires `opts.algorithm`). `generate(algorithm, opts)` produces a key pair over RSA, ECDSA/ECDH, the Edwards/Montgomery curves, and the FIPS post-quantum ML-DSA / ML-KEM, and `publicFromPrivate(privateKey)` derives the public key. Returns DER or PEM; fail-closed with typed `KeyError`. Parsing stays at `pki.schema.pkcs8.parse` — `encrypt` / `decrypt` / `export` / `import` / `generate` / `publicFromPrivate` |
232
232
  | `pki.pkcs12` | RFC 7292 / RFC 9579 PKCS#12 (.p12/.pfx) issuance — `build(spec, opts)` assembles a password-integrity store. `spec` is the OpenSSL-style `{ key, cert, ca?, friendlyName?, localKeyId? }` or the full `{ safeContents: [...] }`, where each element is a plaintext or PBES2-encrypted `SafeContents` of key / shroudedKey / cert / crl / secret / nested `safeContents` bags. Keys and certs are validated before wrapping; `friendlyName` (BMPString) and `localKeyId` attributes are single-value. The store is protected by a classic Appendix B HMAC (default, max interop) or an RFC 9579 PBMAC1 (`opts.mac.algorithm`), over SHA-256/384/512, with the shrouded keys and cert safes encrypted under RFC 8018 PBES2 (AES-128/192/256-CBC). Every password is encoded the PKCS#12 way — BMPString+NULL for the classic MAC, UTF-8 for the PBES2 bags and PBMAC1 (what OpenSSL and NSS consume) — so a file it emits opens in OpenSSL and NSS, cross-checked bidirectionally. The MAC is computed over the exact AuthenticatedSafe byte range, a DEFAULT-1 `MacData.iterations` is rejected up front, and the store is re-parsed before return. `verifyMac(pfx, password, opts)` recomputes a store's classic or PBMAC1 MAC over `macedBytes` and constant-time-compares it, throwing on a MAC-less or public-key-integrity store. Public-key integrity and legacy-PBE (PKCS#12 Appendix C) bag decryption are not yet built. Returns DER or a PEM `PKCS12`; fail-closed with typed `Pkcs12Error`. `open(pfx, password, opts)` reads a store back: it verifies the MAC **first** (a wrong password is the MAC verdict, not a decrypt error), then PBES2-decrypts every privacy safe and shrouded key bag and returns `{ integrityMode, macVerified, keys, certs, crls, secrets }` — keys as re-validated PKCS#8 DER, certs/CRLs/secrets as raw DER, all with `friendlyName`/`localKeyId`, nested safes recursively. A MAC-less store is refused unless `opts.allowUnauthenticated`, a public-key-integrity or legacy-PBE store is refused, a post-MAC decrypt failure is the uniform `pkcs12/decrypt-failed`, and `opts.keys: 'crypto'` imports each key to a `CryptoKey`; it reads what OpenSSL and NSS produce. Parsing stays at `pki.schema.pkcs12.parse` — `build` / `verifyMac` / `open` |
233
- | `pki.cms` | RFC 5652 §5 CMS SignedData signing + signature verification — `sign(content, signers, opts)` produces a SignedData (attached or detached, one or many signers, RSA / RSASSA-PSS / ECDSA / EdDSA, the post-quantum ML-DSA-44/65/87 (RFC 9882) and SLH-DSA (all twelve FIPS 205 sets, RFC 9814), and composite ML-DSA (pairing ML-DSA with a traditional RSA / ECDSA / EdDSA — accepted only when **both** components verify — draft-ietf-lamps-cms-composite-sigs)); it builds the signed attributes (content-type, message-digest, signing-time) as canonical DER, signs the exact §5.4 preimage, and emits a DER `Buffer` or PEM. `verify(input, opts)` parses a SignedData over the strict `pki.schema.cms` codec, locates each SignerInfo's signer certificate by its issuerAndSerialNumber or subjectKeyIdentifier, and checks the signature over the exact §5.4 preimage: when signed attributes are present it confirms the message-digest attribute equals the content digest and verifies over the DER re-encoding of the SignedAttributes (the on-wire `[0]` tag replaced by a universal SET OF), otherwise directly over the content. It returns a per-signer verdict with the matched signer certificate; it does not chain that certificate to a trust anchor — that is the caller's step through `pki.path.validate`. **Content encryption** (RFC 5652/5083/5084/9629): `encrypt(content, recipients, opts)` produces an EnvelopedData, AuthEnvelopedData (AES-GCM, the authenticated default), or EncryptedData — recipients auto-dispatch off the certificate key to key-transport (RSAES-OAEP; v1.5 never emitted), key-agreement (ephemeral-static ECDH over P-256/384/521 with the X9.63 KDF, and X25519/X448 with HKDF), symmetric key-wrap, password (PBKDF2 + RFC 3211 PWRI-KEK), or the post-quantum ML-KEM KEMRecipientInfo (RFC 9629/9936) — one fresh content key wrapped for every recipient. `decrypt(input, keyMaterial, opts)` recovers the content through the matching arm and returns it with an `authenticated` flag; every secret-dependent failure collapses to one uniform `cms/decrypt-failed` verdict (Bleichenbacher / EFAIL / password-oracle freedom), and PKCS#1 v1.5 is decrypt-only under the RFC 3218 implicit-rejection countermeasure. **Compression** (RFC 3274): `compress(content, opts)` / `decompress(input, opts)` produce and consume a CompressedData (ZLIB, version 0, id-alg-zlibCompress); decompress bounds the uncompressed output at 16 MiB and stops before it is materialized, so a decompression bomb fails closed as `cms/decompress-too-large` — a size transform with no integrity/confidentiality (RFC 8551 §2.4.5). Fail-closed with typed `cms/*` errors — `sign`, `verify`, `encrypt`, `decrypt`, `compress`, `decompress` |
233
+ | `pki.cms` | RFC 5652 §5 CMS SignedData signing + signature verification — `sign(content, signers, opts)` produces a SignedData (attached or detached, one or many signers, RSA / RSASSA-PSS / ECDSA / EdDSA, the post-quantum ML-DSA-44/65/87 (RFC 9882) and SLH-DSA (all twelve FIPS 205 sets, RFC 9814), and composite ML-DSA (pairing ML-DSA with a traditional RSA / ECDSA / EdDSA — accepted only when **both** components verify — draft-ietf-lamps-cms-composite-sigs)); it builds the signed attributes (content-type, message-digest, signing-time) as canonical DER, signs the exact §5.4 preimage, and emits a DER `Buffer` or PEM. `verify(input, opts)` parses a SignedData over the strict `pki.schema.cms` codec, locates each SignerInfo's signer certificate by its issuerAndSerialNumber or subjectKeyIdentifier, and checks the signature over the exact §5.4 preimage: when signed attributes are present it confirms the message-digest attribute equals the content digest and verifies over the DER re-encoding of the SignedAttributes (the on-wire `[0]` tag replaced by a universal SET OF), otherwise directly over the content. It returns a per-signer verdict with the matched signer certificate; it does not chain that certificate to a trust anchor — that is the caller's step through `pki.path.validate`. **Countersignatures** (RFC 5652 §11.4): `countersign(cms, signers, opts)` adds a countersignature — a `SignerInfo` over the countersigned SignerInfo's signature value, any signer algorithm, nestable, the primary bytes preserved so it still verifies — attached as the id-countersignature unsigned attribute; `verify` returns each countersignature's verdict under `signers[i].countersignatures` and every unsigned attribute (an RFC 3161 timestamp token attachable via `sign`'s `unsignedAttributes`) under `signers[i].unsignedAttrs`, surfaced unauthenticated. **Content encryption** (RFC 5652/5083/5084/9629): `encrypt(content, recipients, opts)` produces an EnvelopedData, AuthEnvelopedData (AES-GCM, the authenticated default), or EncryptedData — recipients auto-dispatch off the certificate key to key-transport (RSAES-OAEP; v1.5 never emitted), key-agreement (ephemeral-static ECDH over P-256/384/521 with the X9.63 KDF, and X25519/X448 with HKDF), symmetric key-wrap, password (PBKDF2 + RFC 3211 PWRI-KEK), or the post-quantum ML-KEM KEMRecipientInfo (RFC 9629/9936) — one fresh content key wrapped for every recipient. `decrypt(input, keyMaterial, opts)` recovers the content through the matching arm and returns it with an `authenticated` flag; every secret-dependent failure collapses to one uniform `cms/decrypt-failed` verdict (Bleichenbacher / EFAIL / password-oracle freedom), and PKCS#1 v1.5 is decrypt-only under the RFC 3218 implicit-rejection countermeasure. **AuthenticatedData** (RFC 5652 §9): `authenticate(content, recipients, opts)` produces an `id-ct-authData` — cleartext content plus an HMAC-SHA-256/384/512 MAC (authenticated but not encrypted), the fresh MAC key wrapped for every recipient through the same RecipientInfo model as `encrypt`; the MAC covers the authenticated attributes (content-type + message-digest) re-tagged to the EXPLICIT SET OF (§9.2), or the content octets directly. `decrypt` recovers the MAC key, recomputes the MAC and independently the message-digest (§9.3), and releases the content only after both pass, with every secret-dependent failure collapsing to the uniform `cms/decrypt-failed`. **Compression** (RFC 3274): `compress(content, opts)` / `decompress(input, opts)` produce and consume a CompressedData (ZLIB, version 0, id-alg-zlibCompress); decompress bounds the uncompressed output at 16 MiB and stops before it is materialized, so a decompression bomb fails closed as `cms/decompress-too-large` — a size transform with no integrity/confidentiality (RFC 8551 §2.4.5). Fail-closed with typed `cms/*` errors — `sign`, `verify`, `countersign`, `encrypt`, `authenticate`, `decrypt`, `compress`, `decompress` |
234
234
  | `pki.smime` | RFC 8551 S/MIME message assembly, verification, encryption, and compression over the CMS layer — `sign(content, signers, opts)` wraps a MIME entity as a signed S/MIME message in either form: `multipart/signed` (clear-signed — the content stays readable in any MUA, a detached CMS SignedData rides alongside as `application/pkcs7-signature` with a matching `micalg`) or `application/pkcs7-mime; smime-type=signed-data` (opaque — the whole entity is a base64 CMS SignedData). The signed bytes are the entity's RFC 8551 §3.1.1 canonical form (CRLF line endings); `verify(message, opts)` unwraps both forms and recomputes over the same canonicalizer, so a transport that re-wraps line endings still verifies and a tampered part fails. `encrypt(content, recipients, opts)` envelopes a MIME entity as an opaque `application/pkcs7-mime` message and `decrypt(message, keyMaterial, opts)` opens one — `smime-type=authEnveloped-data` (AES-GCM, confidentiality and integrity, the default) or `smime-type=enveloped-data` (AES-CBC, confidentiality only, so `decrypt` reports `authenticated: false`, the §3.3 no-integrity caveat); the `smime-type` is derived from the CMS body, not the header, and decryption is fail-closed and oracle-free. The crypto is entirely `pki.cms.sign` / `verify` / `encrypt` / `decrypt` — any RSA / RSASSA-PSS / ECDSA / EdDSA / ML-DSA / SLH-DSA signer and any RSA-OAEP / ECDH / X25519 / X448 / AES-KW / PBKDF2 / ML-KEM recipient carries through (algorithm-agnostic). Like `cms.verify`, `verify` returns the per-signer cryptographic verdict plus the recovered content; chaining a signer to a trust anchor is the caller's `pki.path.validate` step. `compress(content, opts)` / `decompress(message, opts)` add the opaque `application/pkcs7-mime; smime-type=compressed-data; name=smime.p7z` frame (RFC 8551 §3.6, RFC 3274) — a size transform with no integrity/confidentiality (§2.4.5), decompress bounded against a bomb; the recovered content, which may itself be signed or enveloped, is returned for the caller to re-verify. Bidirectionally interoperable with `openssl smime` / `openssl cms`. Fail-closed with typed `smime/*` errors — `sign`, `verify`, `encrypt`, `decrypt`, `compress`, `decompress` |
235
235
  | `pki.tsp` | RFC 3161 Time-Stamp Protocol — `sign(messageImprint, tsa, opts)` produces a TimeStampToken: a CMS SignedData (over `pki.cms.sign`) whose content is a `TSTInfo` carrying the timestamped message imprint, the TSA policy, a serial number, and `genTime` (with optional accuracy / nonce / ordering), plus the RFC 3161 §2.4.2 signing-certificate attribute binding the token to the TSA certificate (SHA-2 imprints, any `pki.cms.sign` TSA key). `request` / `parseRequest` build and parse the TimeStampReq a client sends (imprint, requested policy, nonce, certReq), `response` / `parseResponse` the TimeStampResp a TSA returns — a granted status wrapping a token, or a rejection with PKIStatus and failure info, the §2.4.2 status↔token coupling enforced in both directions. `verify(token, data, opts)` verifies a token fail-closed: the CMS signature over the exact signed bytes, the message imprint recomputed from the data, the TSTInfo content type, the ESSCertID(V2) binding to the TSA certificate, the §2.3 critical timeStamping-only extendedKeyUsage, the request nonce when used, and — with a trust anchor supplied — full certification-path validation of the TSA certificate at the token's `genTime`, returning `{ valid, genTime, serialNumber, tstInfo, … }` — `sign`, `request`, `parseRequest`, `response`, `parseResponse`, `verify` |
236
236
  | `pki.ocsp` | RFC 6960 Online Certificate Status Protocol — the responder and relying-party surface. `buildRequest(query, opts)` builds an OCSPRequest for one or more `{ cert, issuer }` pairs (CertID hashed under SHA-1 by default per the RFC 5019 lightweight profile, or SHA-2; optional RFC 9654 nonce, optional requestor signature). `sign(responseData, responder, opts)` produces a signed BasicOCSPResponse over the exact `ResponseData` DER — the issuing CA directly or a delegated responder, any `pki.cms.sign` key including the post-quantum ML-DSA / SLH-DSA sets, with `good` / `revoked` (reason + time) / `unknown` per-certificate status, and `buildErrorResponse(status)` the unsigned §2.3 error (`tryLater` / `unauthorized` / …). `verify(response, opts)` verifies a response fail-closed against the same hardened gates `pki.path.ocspChecker` runs: the CertID binding, responder authorization (the issuing CA or a CA-issued delegate bearing id-kp-OCSPSigning **and** id-pkix-ocsp-nocheck, passing the full out-of-path certificate gates), the signature over `tbsResponseDataBytes`, currency (`thisUpdate`/`nextUpdate`), and the request-nonce echo — returning `{ status: "good" / "revoked" / "unknown", … }`, never a silent accept. Transport-free — `buildRequest`, `sign`, `buildErrorResponse`, `verify` |
@@ -44,7 +44,8 @@ async function decrypt(input, keyMaterial, opts) {
44
44
  var parsed = _parse(input);
45
45
  var ct = parsed.contentTypeName;
46
46
  if (ct === "encryptedData") return _decryptEncryptedData(parsed, keyMaterial, opts);
47
- if (ct !== "envelopedData" && ct !== "authEnvelopedData") throw _err("cms/bad-input", "input is not an EnvelopedData / AuthEnvelopedData / EncryptedData (got " + ct + ")");
47
+ if (ct === "authData") return _verifyAuthenticatedData(parsed, keyMaterial, opts);
48
+ if (ct !== "envelopedData" && ct !== "authEnvelopedData") throw _err("cms/bad-input", "input is not an EnvelopedData / AuthEnvelopedData / EncryptedData / AuthenticatedData (got " + ct + ")");
48
49
 
49
50
  var recips = parsed.recipientInfos || [];
50
51
  var candidates = _selectCandidates(recips, keyMaterial, opts); // stage 1 (typed, distinct)
@@ -365,8 +366,12 @@ async function _decryptPbes2(parsed, eci, km, opts) {
365
366
  // ---- shared helpers (mirror cms-encrypt's builders) ------------------------
366
367
  async function _aesKwUnwrap(kek, wrapped) {
367
368
  var kekKey = await subtle.importKey("raw", kek, { name: "AES-KW" }, false, ["unwrapKey"]);
368
- var cekKey = await subtle.unwrapKey("raw", wrapped, kekKey, { name: "AES-KW" }, { name: "AES-CBC" }, true, ["decrypt"]);
369
- return Buffer.from(await subtle.exportKey("raw", cekKey));
369
+ // Import the unwrapped key as HMAC, not AES-CBC: this function EXPORTS to raw bytes, so the CryptoKey
370
+ // type is transient and the exported bytes are identical for an AES CEK -- but HMAC accepts any wrap
371
+ // length, so an AuthenticatedData MAC key that is not 16/24/32 octets (e.g. a 64-octet HMAC-SHA-512
372
+ // key from another implementation) is recovered instead of rejected before the MAC is even checked.
373
+ var raw = await subtle.unwrapKey("raw", wrapped, kekKey, { name: "AES-KW" }, { name: "HMAC", hash: "SHA-256" }, true, ["sign"]);
374
+ return Buffer.from(await subtle.exportKey("raw", raw));
370
375
  }
371
376
  function _eccSharedInfo(wrapName, ukm, kekBytes) {
372
377
  var kids = [b.sequence([b.oid(O(wrapName))])];
@@ -475,10 +480,96 @@ function _mlkemName(o) { return MLKEM[o]; }
475
480
  function _explicitSetOf(implicitBytes) {
476
481
  // The AAD is the authAttrs re-encoded under the EXPLICIT SET OF tag (RFC 5083 sec. 2.2). The
477
482
  // parser surfaces authAttrsBytes as the transmitted [1] IMPLICIT form; retag 0xA1 -> 0x31.
483
+ // AuthenticatedData authAttrs are transmitted [2] IMPLICIT (0xA2); the same out[0]=0x31 handles it.
478
484
  var out = Buffer.from(implicitBytes);
479
485
  out[0] = 0x31;
480
486
  return out;
481
487
  }
488
+
489
+ // ---- AuthenticatedData verification (RFC 5652 sec. 9.3) ---------------------
490
+ // The macAlgorithm OID name -> WebCrypto HMAC hash, and the digestAlgorithm name -> hash (data-driven).
491
+ var MAC_HASH = { hmacWithSHA256: "SHA-256", hmacWithSHA384: "SHA-384", hmacWithSHA512: "SHA-512" };
492
+ var DIGEST_WC = { sha256: "SHA-256", sha384: "SHA-384", sha512: "SHA-512" };
493
+ // The minimum conveyed MAC-key length (128-bit security floor). A ktri/pwri recipient can wrap a
494
+ // key of any length (unlike AES-KW's 16-octet minimum), so a hostile message could convey a tiny or
495
+ // empty MAC key that weakens the HMAC (or makes subtle.importKey throw); reject it fail-closed.
496
+ var MAC_KEY_MIN_OCTETS = 16;
497
+ function _isDerNull(p) { return Buffer.isBuffer(p) && p.length === 2 && p[0] === 0x05 && p[1] === 0x00; }
498
+
499
+ // Verify a CMS AuthenticatedData (RFC 5652 sec. 9): recover the MAC key through the SAME RecipientInfo
500
+ // unwrap decrypt uses, recompute the HMAC over the sec. 9.2 preimage (the re-tagged [2] authAttrs SET
501
+ // OF when present, else the eContent value octets), constant-time-compare to the transmitted mac, and
502
+ // -- when authAttrs are present -- INDEPENDENTLY confirm message-digest == digest(content) (sec. 9.3
503
+ // do-not-trust-originator). The content is returned ONLY after every check passes. Every
504
+ // secret-dependent failure collapses to the uniform cms/decrypt-failed (no unwrap-success oracle).
505
+ async function _verifyAuthenticatedData(parsed, km, opts) {
506
+ var macAlg = parsed.macAlgorithm;
507
+ var hash = MAC_HASH[macAlg.name];
508
+ if (!hash) throw _err("cms/unsupported-algorithm", "unsupported macAlgorithm " + JSON.stringify(macAlg.name || macAlg.oid) + " (HMAC-SHA-256/384/512 only)");
509
+ if (macAlg.parameters != null && !_isDerNull(macAlg.parameters)) throw _err("cms/unsupported-algorithm", "the macAlgorithm parameters must be absent or DER NULL (RFC 3370)");
510
+ var content = parsed.encapContentInfo.eContent;
511
+ if (content == null) throw _err("cms/bad-input", "a detached AuthenticatedData (absent eContent) is not supported");
512
+
513
+ // Determine the sec. 9.2 preimage + (present mode) the message-digest claim, both structural.
514
+ var preimage, mdCheck = null;
515
+ if (parsed.authAttrsBytes) {
516
+ var dHash = DIGEST_WC[parsed.digestAlgorithm && parsed.digestAlgorithm.name];
517
+ if (!dHash) throw _err("cms/unsupported-algorithm", "unsupported digestAlgorithm " + JSON.stringify(parsed.digestAlgorithm && parsed.digestAlgorithm.name));
518
+ preimage = _explicitSetOf(parsed.authAttrsBytes);
519
+ // The parser already enforced content-type == eContentType (cms/content-type-mismatch) and the
520
+ // single-valued message-digest attribute; read the claimed digest from the parsed authAttrs (which
521
+ // decrypt re-parses from the same bytes the MAC covers).
522
+ var mdAttr = (parsed.authAttrs || []).filter(function (a) { return a.type === O("messageDigest"); })[0];
523
+ mdCheck = { hash: dHash, declared: asn1.read.octetString(asn1.decode(mdAttr.values[0])) };
524
+ } else {
525
+ preimage = Buffer.from(content);
526
+ }
527
+
528
+ var candidates = _selectCandidates(parsed.recipientInfos || [], km, opts);
529
+ for (var ci = 0; ci < candidates.length; ci++) {
530
+ try {
531
+ _assertSupported(candidates[ci].ri, km);
532
+ var macKey = await _acquireCek(candidates[ci].ri, km, opts); // recover the MAC key (uniform failure)
533
+ // RFC 3218 implicit rejection: _ktriCek returns null on an RSA v1.5 unwrap fault, and a hostile
534
+ // ktri/pwri could convey a below-floor (128-bit) key; in EITHER case substitute a fresh random
535
+ // key so the MAC verify still RUNS and fails uniformly -- never a fast-path that distinguishes an
536
+ // invalid/short unwrap from a MAC mismatch (Bleichenbacher / weak-key oracle freedom).
537
+ if (macKey == null || macKey.length < MAC_KEY_MIN_OCTETS) macKey = nodeCrypto.randomBytes(MAC_KEY_MIN_OCTETS);
538
+ var key = await subtle.importKey("raw", macKey, { name: "HMAC", hash: hash }, false, ["verify"]);
539
+ if (!(await subtle.verify({ name: "HMAC" }, key, Buffer.from(parsed.mac), preimage))) throw _fail();
540
+ if (mdCheck) {
541
+ var actual = Buffer.from(await subtle.digest(mdCheck.hash, content));
542
+ if (!actual.equals(mdCheck.declared)) throw _fail(); // sec. 9.3: recompute, never trust the originator's digest
543
+ }
544
+ return {
545
+ content: Buffer.from(content),
546
+ contentType: parsed.encapContentInfo.eContentType, contentTypeName: oid.name(parsed.encapContentInfo.eContentType) || parsed.encapContentInfo.eContentType,
547
+ recipientType: candidates[ci].ri.type, recipientIndex: candidates[ci].index,
548
+ macAlgorithm: macAlg.name || macAlg.oid,
549
+ digestAlgorithm: parsed.digestAlgorithm ? (parsed.digestAlgorithm.name || parsed.digestAlgorithm.oid) : null,
550
+ authenticated: true,
551
+ };
552
+ } catch (e) {
553
+ if (candidates.length === 1) throw e; // one candidate: preserve its exact (distinct or uniform) verdict
554
+ }
555
+ }
556
+ throw _fail(); // every ambiguous candidate failed -> uniform
557
+ }
558
+ // Coverage residual in _verifyAuthenticatedData -- defensive branches reachable only via a hand-built
559
+ // malformed message (the producer never emits them) or unreachable behind an upstream guarantee:
560
+ // * the macAlgorithm-parameters-not-NULL reject and the unsupported-digestAlgorithm reject fire only
561
+ // on a hand-crafted message (this producer emits absent params + SHA-2); the behavioral guard is
562
+ // the frozen cms/unsupported-algorithm code, not a round-trip.
563
+ // * the detached (absent eContent) reject is the deferred-with-condition path (this producer always
564
+ // attaches the content); re-open when a detached AuthenticatedData caller appears.
565
+ // * `parsed.authAttrs || []` / `parsed.recipientInfos || []` -- the parser always surfaces these as
566
+ // arrays (or rejects), so the `|| []` fallbacks never fire.
567
+ // * `oid.name(eContentType) || eContentType` fires only for an unregistered content type; `macAlg.name
568
+ // || macAlg.oid` and `digestAlgorithm.name || .oid` never reach the `.oid` fallback because the
569
+ // algorithm name was already validated against MAC_HASH / DIGEST_WC above.
570
+ // * the `macKey == null` half of the random-key substitution fires only for a hand-crafted RSA v1.5
571
+ // ktri (this producer emits OAEP); its behaviour is identical to the tested below-floor path (a
572
+ // random key -> the MAC verify fails uniformly), so the < 16 vector covers the substitution.
482
573
  function _normKeyDer(key) {
483
574
  if (Buffer.isBuffer(key)) return key;
484
575
  if (key instanceof Uint8Array) return Buffer.from(key);
@@ -458,4 +458,98 @@ function _tlvHeaderLen(der) {
458
458
  return 2 + (lenByte & 0x7f);
459
459
  }
460
460
 
461
- module.exports = { encrypt: encrypt };
461
+ // ---- pki.cms.authenticate (RFC 5652 sec. 9 AuthenticatedData) ---------------
462
+ // AuthenticatedData carries CLEARTEXT content plus a MAC (no content encryption). A single fresh HMAC
463
+ // key is minted, wrapped for every recipient with the EXACT RecipientInfo model EnvelopedData uses for
464
+ // the CEK, and the MAC (HMAC-SHA-2) covers either the re-tagged [2] authAttrs SET OF (default) or the
465
+ // eContent value octets directly. Homes here (not a new module) so the recipient-wrap helpers are
466
+ // module-local; the verify half folds into pki.cms.decrypt.
467
+
468
+ // The macAlgorithm registry: an OID name + the WebCrypto/node hashes (data-driven, not a switch).
469
+ var MAC_ALGS = {
470
+ "hmac-sha256": { oid: "hmacWithSHA256", wc: "SHA-256", node: "sha256" },
471
+ "hmac-sha384": { oid: "hmacWithSHA384", wc: "SHA-384", node: "sha384" },
472
+ "hmac-sha512": { oid: "hmacWithSHA512", wc: "SHA-512", node: "sha512" },
473
+ };
474
+ // The fresh MAC key length: 32 octets is AES-KW-compatible (16/24/32), so it wraps through every
475
+ // recipient arm (kari/kekri/kemri import the wrapped key as AES-CBC) with no codec change, and a
476
+ // 32-octet conveyed key is a valid HMAC key for SHA-256/384/512 (RFC 2104).
477
+ var MAC_KEY_OCTETS = 32;
478
+ // The message-digest hashes the AuthenticatedData verify path (cms-decrypt DIGEST_WC) can consume --
479
+ // the producer never emits a digestAlgorithm its own verifier cannot recompute (the no-orphan rule).
480
+ var SUPPORTED_DIGEST = { sha256: 1, sha384: 1, sha512: 1 };
481
+
482
+ async function authenticate(content, recipients, opts) {
483
+ opts = opts || {};
484
+ var contentBytes = guard.bytes.view(content, CmsError, "cms/bad-input", "content");
485
+ var macName = opts.macAlgorithm || "hmac-sha256";
486
+ var mac = MAC_ALGS[macName];
487
+ if (!mac) throw _err("cms/bad-input", "unsupported macAlgorithm " + JSON.stringify(macName) + " (hmac-sha256/384/512)");
488
+ if (!Array.isArray(recipients) || !recipients.length) throw _err("cms/bad-input", "at least one recipient is required (RFC 5652 sec. 9.1)");
489
+ var contentType = opts.contentType || "data";
490
+ var withAttrs = opts.authenticatedAttributes !== false;
491
+ // RFC 5652 sec. 9.1: authAttrs MUST be present when the eContentType is not id-data.
492
+ if (contentType !== "data" && !withAttrs) throw _err("cms/bad-input", "AuthenticatedData with a non-data contentType requires authenticated attributes (RFC 5652 sec. 9.1)");
493
+
494
+ var macKey = nodeCrypto.randomBytes(MAC_KEY_OCTETS);
495
+ var recips = [];
496
+ for (var i = 0; i < recipients.length; i++) recips.push(await _buildRecipient(macKey, recipients[i], opts));
497
+ var riNodes = recips.map(_taggedRecipient);
498
+
499
+ var digestName = opts.digestAlgorithm || mac.node;
500
+ var digestAlgTagged = null, authAttrsDer = null, preimage;
501
+ if (withAttrs) {
502
+ if (!SUPPORTED_DIGEST[digestName]) throw _err("cms/bad-input", "unsupported digestAlgorithm " + JSON.stringify(digestName) + " (sha256/384/512)");
503
+ // sec. 9.2: content-type (== eContentType) + message-digest (== digest(content)) attributes,
504
+ // SET-OF-sorted, MACed under the EXPLICIT SET OF tag (0x31) but transmitted [2] IMPLICIT (0xA2).
505
+ var mdDigest = nodeCrypto.createHash(digestName).update(contentBytes).digest();
506
+ var pairs = [
507
+ b.sequence([b.oid(O("contentType")), b.setOf([b.oid(O(contentType))])]),
508
+ b.sequence([b.oid(O("messageDigest")), b.setOf([b.octetString(mdDigest)])]),
509
+ ];
510
+ if (opts.authAttrs && opts.authAttrs.length) pairs = pairs.concat(opts.authAttrs);
511
+ // Every authAttr (auto-built or caller-supplied) MUST be a well-formed Attribute SEQUENCE
512
+ // { type OBJECT IDENTIFIER, values SET OF } and each type appears at most once (RFC 5652) -- so a
513
+ // malformed or duplicate caller attribute is rejected BEFORE it is MACed and emitted, never left to
514
+ // fail an operator's parser downstream.
515
+ var seenTypes = {};
516
+ pairs.forEach(function (p) {
517
+ var node;
518
+ try { node = asn1.decode(p); } catch (e) { throw _err("cms/bad-input", "an authenticated attribute is not well-formed DER", e); }
519
+ if (node.tagClass !== "universal" || node.tagNumber !== asn1.TAGS.SEQUENCE || !node.children || node.children.length !== 2 ||
520
+ node.children[1].tagClass !== "universal" || node.children[1].tagNumber !== asn1.TAGS.SET ||
521
+ !node.children[1].children || node.children[1].children.length < 1) {
522
+ throw _err("cms/bad-input", "an authenticated attribute must be an Attribute SEQUENCE { type, non-empty SET OF value } (RFC 5652)");
523
+ }
524
+ var t;
525
+ try { t = asn1.read.oid(node.children[0]); } catch (e) { throw _err("cms/bad-input", "an authenticated attribute type is not an OBJECT IDENTIFIER", e); }
526
+ if (seenTypes[t]) throw _err("cms/bad-input", "authenticated attributes must not repeat an attribute type (RFC 5652): " + t);
527
+ seenTypes[t] = 1;
528
+ });
529
+ var setOf = b.setOf(pairs);
530
+ preimage = setOf; // MAC over the 0x31 SET OF
531
+ authAttrsDer = b.contextConstructed(2, setOf.subarray(_tlvHeaderLen(setOf))); // [2] IMPLICIT on the wire
532
+ digestAlgTagged = b.contextConstructed(1, b.oid(O(digestName))); // [1] IMPLICIT DigestAlgorithmIdentifier
533
+ } else {
534
+ preimage = contentBytes; // MAC over the eContent value octets
535
+ }
536
+
537
+ var hmacKey = await subtle.importKey("raw", macKey, { name: "HMAC", hash: mac.wc }, false, ["sign"]);
538
+ var macValue = Buffer.from(await subtle.sign({ name: "HMAC" }, hmacKey, preimage));
539
+
540
+ var eci = b.sequence([b.oid(O(contentType)), b.explicit(0, b.octetString(contentBytes))]);
541
+ var kids = [b.integer(0n), b.setOf(riNodes), _algId(mac.oid)];
542
+ if (digestAlgTagged) kids.push(digestAlgTagged);
543
+ kids.push(eci);
544
+ if (authAttrsDer) kids.push(authAttrsDer);
545
+ kids.push(b.octetString(macValue));
546
+ var ci = b.sequence([b.oid(O("authData")), b.explicit(0, b.sequence(kids))]);
547
+ // Self-verify: the emitted AuthenticatedData MUST re-parse clean through the strict parser, so a
548
+ // caller-supplied authenticated attribute with a recognized type but extra RFC 5652 constraints (a
549
+ // misplaced type, a signing-time whose value is not a Time) is rejected HERE at build time rather
550
+ // than by the recipient's parser after the fact.
551
+ try { schemaCms.parse(ci); } catch (e) { throw (e instanceof CmsError) ? e : _err("cms/bad-input", "the supplied authenticated attributes produced an invalid AuthenticatedData", e); }
552
+ return opts.pem ? schemaCms.pemEncode(ci, "CMS") : ci;
553
+ }
554
+
555
+ module.exports = { encrypt: encrypt, authenticate: authenticate };
package/lib/cms-sign.js CHANGED
@@ -23,6 +23,7 @@ var frameworkError = require("./framework-error");
23
23
  var signScheme = require("./sign-scheme");
24
24
  var guard = require("./guard-all");
25
25
  var pkiBuild = require("./pki-build");
26
+ var cms = require("./schema-cms");
26
27
  var CmsError = frameworkError.CmsError;
27
28
  var b = asn1.build;
28
29
  function _err(code, message, cause) { return new CmsError(code, message, cause); }
@@ -57,6 +58,73 @@ function _skiValue(cert) {
57
58
  }
58
59
 
59
60
 
61
+ // The SignerInfo SignerIdentifier + its coupled CMSVersion (RFC 5652 sec. 5.3): a [0] IMPLICIT
62
+ // subjectKeyIdentifier => version 3, else an IssuerAndSerialNumber => version 1. Shared by the
63
+ // top-level signer and the sec. 11.4 countersignature (a countersignature IS a SignerInfo).
64
+ function _buildSid(cert, useSki) {
65
+ var sid = useSki
66
+ ? b.contextPrimitive(0, _skiValue(cert)) // [0] IMPLICIT SubjectKeyIdentifier
67
+ : b.sequence([b.raw(pkiBuild.tbsNameField(cert, "issuer")), b.integer(cert.serialNumber)]); // IssuerAndSerialNumber
68
+ return { sid: sid, version: useSki ? 3 : 1 };
69
+ }
70
+
71
+ // Assemble a SignedAttributes SET from resolved attribute pairs (each { type: <dotted OID>, values:
72
+ // [<build node or DER Buffer>] }), returning { setOf, wire }: `setOf` is the canonical DER SET OF
73
+ // (tag 0x31, build.set SET-OF-sorts) the signature covers (RFC 5652 sec. 5.4); `wire` is the same
74
+ // bytes with the on-wire [0] IMPLICIT tag (0xA0). Each attribute type appears AT MOST ONCE (sec.
75
+ // 5.3) -- a duplicate throws. Shared by the top-level signer (content-type + message-digest +
76
+ // signing-time + caller attrs) and the countersignature builder (message-digest over the target
77
+ // signature octets + signing-time, content-type OMITTED per sec. 11.4).
78
+ function _buildSignedAttrs(pairs) {
79
+ var seenTypes = {};
80
+ var attrs = pairs.map(function (p) {
81
+ if (seenTypes[p.type]) throw _err("cms/bad-input", "signedAttrs must not repeat an attribute type (RFC 5652 sec. 5.3): " + p.type);
82
+ seenTypes[p.type] = 1;
83
+ return b.sequence([b.oid(p.type), b.set(p.values)]);
84
+ });
85
+ var setOf = b.set(attrs); // SET OF (tag 0x31) -- the exact bytes the signature covers (sec. 5.4)
86
+ var wire = Buffer.from(setOf); wire[0] = 0xA0; // the on-wire [0] IMPLICIT tag
87
+ return { setOf: setOf, wire: wire };
88
+ }
89
+
90
+ // Resolve caller-supplied additional signed attributes ({ type: OID name or dotted, values: [DER] })
91
+ // into the pair shape _buildSignedAttrs consumes; each MUST carry >= 1 value (Attribute values is
92
+ // SET SIZE (1..MAX)).
93
+ function _resolveAttrPairs(list, what) {
94
+ return (list || []).map(function (a) {
95
+ var vals = (a.values || []).map(function (v) { return _toBuf(v, what); });
96
+ if (!vals.length) throw _err("cms/bad-input", "a signed attribute must carry at least one value (RFC 5652 -- Attribute values is SET SIZE (1..MAX))");
97
+ return { type: /^\d+(\.\d+)+$/.test(a.type) ? a.type : O(a.type), values: vals };
98
+ });
99
+ }
100
+
101
+ // content-type / message-digest / signing-time are content-binding SIGNED attributes and MUST NOT
102
+ // appear as unsigned attributes (RFC 5652 sec. 11.1/11.2/11.3 -- the parser's ATTR_FORBIDDEN_IN).
103
+ var UNSIGNED_FORBIDDEN = {};
104
+ UNSIGNED_FORBIDDEN[O("contentType")] = "content-type";
105
+ UNSIGNED_FORBIDDEN[O("messageDigest")] = "message-digest";
106
+ UNSIGNED_FORBIDDEN[O("signingTime")] = "signing-time";
107
+
108
+ // Build the [1] IMPLICIT unsignedAttrs SET OF (on-wire tag 0xA1) from opts.unsignedAttributes
109
+ // (each { type, values: [DER] } -- a countersignature, an RFC 3161 timestamp token), or null when
110
+ // absent/empty. Unsigned attributes are OUTSIDE the signature; a placement-forbidden type or a
111
+ // duplicate type (RFC 5652 sec. 11 / sec. 5.3) is a config-time cms/bad-input.
112
+ function _buildUnsignedAttrs(list) {
113
+ if (list == null) return null;
114
+ if (!Array.isArray(list)) throw _err("cms/bad-input", "opts.unsignedAttributes must be an array of { type, values }");
115
+ if (!list.length) return null;
116
+ var pairs = _resolveAttrPairs(list, "an unsigned attribute value");
117
+ var seen = {};
118
+ pairs.forEach(function (p) {
119
+ if (UNSIGNED_FORBIDDEN[p.type]) throw _err("cms/bad-input", "the " + UNSIGNED_FORBIDDEN[p.type] + " attribute must not appear as an unsigned attribute (RFC 5652 sec. 11)");
120
+ if (seen[p.type]) throw _err("cms/bad-input", "unsignedAttrs must not repeat an attribute type (RFC 5652 sec. 5.3): " + p.type);
121
+ seen[p.type] = 1;
122
+ });
123
+ var setOf = b.set(pairs.map(function (p) { return b.sequence([b.oid(p.type), b.set(p.values)]); }));
124
+ var wire = Buffer.from(setOf); wire[0] = 0xA1; // SET OF -> [1] IMPLICIT UnsignedAttributes
125
+ return wire;
126
+ }
127
+
60
128
  // Build one SignerInfo (RFC 5652 sec. 5.3) and sign it. Resolves the SignerInfo (as a build
61
129
  // node) plus its digestAlgorithm AlgorithmIdentifier and the signer certificate DER (for the
62
130
  // SignedData digestAlgorithms + certificates sets).
@@ -65,44 +133,28 @@ function _buildSignerInfo(signer, content, eContentType, opts) {
65
133
  var certDer = _normCertDer(so.cert);
66
134
  var cert = x509.parse(certDer);
67
135
  var scheme = signScheme.resolveSignScheme(cert, so, opts.signedAttributes === false, _signE);
68
- var useSki = opts.sid === "ski";
69
- var sid = useSki
70
- ? b.contextPrimitive(0, _skiValue(cert)) // [0] IMPLICIT SubjectKeyIdentifier
71
- : b.sequence([b.raw(pkiBuild.tbsNameField(cert, "issuer")), b.integer(cert.serialNumber)]); // IssuerAndSerialNumber
72
- var version = useSki ? 3 : 1;
136
+ var sidv = _buildSid(cert, opts.sid === "ski");
137
+ var sid = sidv.sid, version = sidv.version;
73
138
 
74
139
  return Promise.resolve().then(function () {
75
140
  if (opts.signedAttributes === false) return content; // sign the content directly (no signed attributes)
76
141
  // Signed attributes (RFC 5652 sec. 5.3): content-type == eContentType, message-digest ==
77
- // digest(content), and (by default) signing-time. build.set canonical-DER SET-OF-sorts them.
78
- // Each attribute type appears AT MOST ONCE across the whole set (RFC 5652 sec. 5.3);
79
- // `seenTypes` catches a caller-supplied attribute that duplicates a built-in or another.
80
- var seenTypes = {};
81
- function _pushAttr(typeOid, values) {
82
- if (seenTypes[typeOid]) throw _err("cms/bad-input", "signedAttrs must not repeat an attribute type (RFC 5652 sec. 5.3): " + typeOid);
83
- seenTypes[typeOid] = 1;
84
- attrs.push(b.sequence([b.oid(typeOid), b.set(values)]));
85
- }
86
- var attrs = [];
87
- _pushAttr(O("contentType"), [b.oid(eContentType)]);
88
- _pushAttr(O("messageDigest"), [b.octetString(_digest(scheme.digest, content))]);
89
- if (opts.signingTime !== false) _pushAttr(O("signingTime"), [_timeValue(opts.signingTime)]);
90
- // Caller-supplied signed attributes (e.g. an RFC 3161 signing-certificate attribute): each
91
- // { type: <OID name or dotted string>, values: [<DER value Buffer>] }. build.set sorts them in.
92
- (opts.additionalSignedAttributes || []).forEach(function (a) {
93
- var vals = (a.values || []).map(function (v) { return _toBuf(v, "a signed attribute value"); });
94
- if (!vals.length) throw _err("cms/bad-input", "a signed attribute must carry at least one value (RFC 5652 -- Attribute values is SET SIZE (1..MAX))");
95
- _pushAttr(/^\d+(\.\d+)+$/.test(a.type) ? a.type : O(a.type), vals);
96
- });
97
- var setOf = b.set(attrs); // SET OF (tag 0x31) -- the exact bytes the signature covers (sec. 5.4)
98
- var wire = Buffer.from(setOf); wire[0] = 0xA0; // the on-wire [0] IMPLICIT tag
99
- return { setOf: setOf, wire: wire };
142
+ // digest(content), and (by default) signing-time, plus any caller-supplied attribute.
143
+ var pairs = [
144
+ { type: O("contentType"), values: [b.oid(eContentType)] },
145
+ { type: O("messageDigest"), values: [b.octetString(_digest(scheme.digest, content))] },
146
+ ];
147
+ if (opts.signingTime !== false) pairs.push({ type: O("signingTime"), values: [_timeValue(opts.signingTime)] });
148
+ pairs = pairs.concat(_resolveAttrPairs(opts.additionalSignedAttributes, "a signed attribute value"));
149
+ return _buildSignedAttrs(pairs);
100
150
  }).then(function (toSign) {
101
151
  var signedBytes = toSign.setOf ? toSign.setOf : toSign; // SET-OF form for signing (sec. 5.4)
102
152
  return signScheme.signOverTbs(scheme, so.key, signedBytes, _signE).then(function (sig) {
103
153
  var fields = [b.integer(BigInt(version)), sid, scheme.digestAlgId];
104
154
  if (toSign.wire) fields.push(toSign.wire); // [0] IMPLICIT signedAttrs
105
155
  fields.push(scheme.sigAlgId, b.octetString(sig));
156
+ var ua = _buildUnsignedAttrs(opts.unsignedAttributes);
157
+ if (ua) fields.push(ua); // [1] IMPLICIT unsignedAttrs
106
158
  return { si: b.sequence(fields), digestAlgId: scheme.digestAlgId, version: version, certDer: certDer };
107
159
  });
108
160
  });
@@ -191,6 +243,179 @@ function _toBuf(v, what) {
191
243
  throw _err("cms/bad-input", what + " must be a Buffer");
192
244
  }
193
245
 
246
+ // ---- pki.cms.countersign (RFC 5652 sec. 11.4) ------------------------------
247
+ // A countersignature is a SignerInfo (Countersignature ::= SignerInfo) over the CONTENTS of the
248
+ // countersigned SignerInfo's signature OCTET STRING -- NOT the eContent -- attached as the
249
+ // id-countersignature unsigned attribute. It reuses the whole build+sign flow (resolveSignScheme /
250
+ // _buildSid / _buildSignedAttrs / signOverTbs); only the preimage (the target signature octets) and
251
+ // the content-type OMISSION are the deltas, and the orchestrator splices the [1] unsignedAttrs into
252
+ // an existing SignedData while preserving the targeted SignerInfo's signed bytes BYTE-FOR-BYTE.
253
+
254
+ // Resolve opts.signerIndex (a number, an array of numbers, or "all"; default 0) to primary indices.
255
+ function _resolveSignerIndices(spec, n) {
256
+ if (spec == null) { if (n < 1) throw _err("cms/bad-input", "the SignedData carries no SignerInfo to countersign"); return [0]; }
257
+ if (spec === "all") { var all = []; for (var i = 0; i < n; i++) all.push(i); return all; }
258
+ var arr = Array.isArray(spec) ? spec : [spec];
259
+ if (!arr.length) throw _err("cms/bad-input", "signerIndex must select at least one signer");
260
+ arr.forEach(function (i) { if (typeof i !== "number" || !Number.isInteger(i) || i < 0 || i >= n) throw _err("cms/bad-input", "signerIndex out of range: " + i); });
261
+ return arr;
262
+ }
263
+
264
+ // Build one countersignature value (RFC 5652 sec. 11.4) over `targetSigOctets`: message-digest bound
265
+ // to digest(targetSigOctets) under the countersignature's own digestAlgorithm, content-type OMITTED,
266
+ // signed through the SAME per-algorithm scheme machinery as a top-level signer.
267
+ function _buildCountersignature(targetSigOctets, countersigner, opts) {
268
+ var so = countersigner || {};
269
+ var certDer = _normCertDer(so.cert);
270
+ var cert = x509.parse(certDer);
271
+ var scheme = signScheme.resolveSignScheme(cert, so, opts.signedAttributes === false, _signE);
272
+ var sidv = _buildSid(cert, opts.sid === "ski");
273
+ return Promise.resolve().then(function () {
274
+ if (opts.signedAttributes === false) return null; // sign the target signature octets directly
275
+ var pairs = [{ type: O("messageDigest"), values: [b.octetString(_digest(scheme.digest, targetSigOctets))] }];
276
+ if (opts.signingTime !== false) pairs.push({ type: O("signingTime"), values: [_timeValue(opts.signingTime)] });
277
+ var extra = _resolveAttrPairs(opts.additionalSignedAttributes, "a countersignature signed attribute value");
278
+ extra.forEach(function (p) { if (p.type === O("contentType")) throw _err("cms/bad-input", "a countersignature must not carry a content-type attribute (RFC 5652 sec. 11.4)"); });
279
+ return _buildSignedAttrs(pairs.concat(extra));
280
+ }).then(function (attrs) {
281
+ return signScheme.signOverTbs(scheme, so.key, attrs ? attrs.setOf : targetSigOctets, _signE).then(function (sig) {
282
+ var fields = [b.integer(BigInt(sidv.version)), sidv.sid, scheme.digestAlgId];
283
+ if (attrs) fields.push(attrs.wire); // [0] IMPLICIT signedAttrs
284
+ fields.push(scheme.sigAlgId, b.octetString(sig));
285
+ return { value: b.sequence(fields), certDer: certDer };
286
+ });
287
+ });
288
+ }
289
+
290
+ // Build the [1] IMPLICIT unsignedAttrs bytes for a SignerInfo, merging `newCsValues` into the ONE
291
+ // id-countersignature attribute (RFC 5652 sec. 11: single instance per type, MULTIPLE values), and
292
+ // keeping every OTHER unsigned attribute and every EXISTING countersignature value verbatim.
293
+ function _mergeCountersig(uaNode, newCsValues) {
294
+ var CS = O("countersignature");
295
+ var others = [], csValues = [];
296
+ if (uaNode) uaNode.children.forEach(function (attr) {
297
+ if (asn1.read.oid(attr.children[0]) === CS) attr.children[1].children.forEach(function (v) { csValues.push(v.bytes); });
298
+ else others.push(attr.bytes);
299
+ });
300
+ newCsValues.forEach(function (v) { csValues.push(v); });
301
+ var csAttr = b.sequence([b.oid(CS), b.set(csValues)]);
302
+ var setOf = b.set(others.concat([csAttr]));
303
+ var wire = Buffer.from(setOf); wire[0] = 0xA1; // SET OF -> [1] IMPLICIT UnsignedAttributes
304
+ return wire;
305
+ }
306
+
307
+ // Append countersignature values to a SignerInfo node, preserving version / sid / digestAlgorithm /
308
+ // signedAttrs / signatureAlgorithm / signature BYTE-FOR-BYTE (the signed preimage), only adding the
309
+ // [1] unsignedAttrs. Returns the new SignerInfo bytes.
310
+ function _appendCountersigs(siNode, newCsValues) {
311
+ var kids = siNode.children;
312
+ var last = kids[kids.length - 1];
313
+ var hasUa = last.tagClass === "context" && last.tagNumber === 1;
314
+ var base = (hasUa ? kids.slice(0, kids.length - 1) : kids).map(function (k) { return k.bytes; });
315
+ base.push(_mergeCountersig(hasUa ? last : null, newCsValues));
316
+ return b.sequence(base);
317
+ }
318
+
319
+ // Splice countersignature values into the j-th countersignature VALUE of a SignerInfo node (nested
320
+ // countersignature, RFC 5652 sec. 11.4). Returns the new SignerInfo bytes.
321
+ function _spliceNested(siNode, j, newCsValues) {
322
+ var kids = siNode.children;
323
+ var last = kids[kids.length - 1];
324
+ var CS = O("countersignature");
325
+ if (!last || last.tagClass !== "context" || last.tagNumber !== 1) throw _err("cms/bad-input", "the target signer carries no countersignature to countersign");
326
+ var found = false;
327
+ var attrs = last.children.map(function (attr) {
328
+ if (asn1.read.oid(attr.children[0]) !== CS) return attr.bytes;
329
+ var values = attr.children[1].children;
330
+ if (j < 0 || j >= values.length) throw _err("cms/bad-input", "countersignatureOf out of range: " + j);
331
+ found = true;
332
+ return b.sequence([b.oid(CS), b.set(values.map(function (v, vi) { return vi === j ? _appendCountersigs(v, newCsValues) : v.bytes; }))]);
333
+ });
334
+ if (!found) throw _err("cms/bad-input", "the target signer carries no countersignature to countersign");
335
+ var setOf = b.set(attrs); var wire = Buffer.from(setOf); wire[0] = 0xA1;
336
+ var base = kids.slice(0, kids.length - 1).map(function (k) { return k.bytes; });
337
+ base.push(wire);
338
+ return b.sequence(base);
339
+ }
340
+
341
+ // The signature-value octets of a SignerInfo node: the last universal OCTET STRING, i.e. the last
342
+ // child unless a [1] IMPLICIT unsignedAttrs (a prior countersignature) follows it.
343
+ function _signatureOctets(siNode) {
344
+ var kids = siNode.children;
345
+ var last = kids[kids.length - 1];
346
+ var sigNode = (last.tagClass === "context" && last.tagNumber === 1) ? kids[kids.length - 2] : last;
347
+ return asn1.read.octetString(sigNode);
348
+ }
349
+
350
+ // The sec. 11.4 preimage for a target: the primary SignerInfo's signature octets, or (nested) the
351
+ // j-th countersignature value's signature octets.
352
+ function _targetPreimage(siNode, opts) {
353
+ if (opts.countersignatureOf == null) return _signatureOctets(siNode);
354
+ var last = siNode.children[siNode.children.length - 1];
355
+ var CS = O("countersignature");
356
+ if (!last || last.tagClass !== "context" || last.tagNumber !== 1) throw _err("cms/bad-input", "the target signer carries no countersignature to countersign");
357
+ var attr = last.children.filter(function (a) { return asn1.read.oid(a.children[0]) === CS; })[0];
358
+ if (!attr) throw _err("cms/bad-input", "the target signer carries no countersignature to countersign");
359
+ var values = attr.children[1].children;
360
+ var j = opts.countersignatureOf;
361
+ if (typeof j !== "number" || !Number.isInteger(j) || j < 0 || j >= values.length) throw _err("cms/bad-input", "countersignatureOf out of range: " + j);
362
+ return _signatureOctets(values[j]);
363
+ }
364
+
365
+ // pki.cms.countersign -- documented by the @primitive block in cms-verify.js (the @module pki.cms home).
366
+ function countersign(cmsInput, signers, opts) {
367
+ opts = opts || {};
368
+ if (typeof opts !== "object" || Buffer.isBuffer(opts)) throw _err("cms/bad-input", "pki.cms.countersign options must be an object");
369
+ var list = Array.isArray(signers) ? signers : [signers];
370
+ if (!list.length) throw _err("cms/bad-input", "pki.cms.countersign requires at least one countersigner");
371
+ if (opts.signingTime != null && opts.signingTime !== false) guard.time.assertValid(opts.signingTime, _err, "cms/bad-input", "signingTime");
372
+ var der = pkix.coerceToDer(cmsInput, { pemLabel: null, PemError: frameworkError.PemError, ErrorClass: CmsError, prefix: "cms" });
373
+ // cms.parse throws a typed cms/* error on a malformed input (mirroring verify's own parse call); a
374
+ // structurally-valid but non-SignedData CMS (an EnvelopedData) parses without a signerInfos array.
375
+ var parsed = cms.parse(der);
376
+ if (!Array.isArray(parsed.signerInfos)) throw _err("cms/bad-input", "pki.cms.countersign input is not a CMS SignedData");
377
+ var targets = _resolveSignerIndices(opts.signerIndex, parsed.signerInfos.length);
378
+ var root = asn1.decode(der);
379
+ var sd = root.children[1].children[0];
380
+ var sdKids = sd.children;
381
+ var siSet = sdKids[sdKids.length - 1];
382
+
383
+ // Build every countersignature value (target x countersigner), then splice into the node tree.
384
+ var jobs = [];
385
+ targets.forEach(function (t) {
386
+ var preimage = _targetPreimage(siSet.children[t], opts);
387
+ list.forEach(function (cs) { jobs.push({ t: t, p: _buildCountersignature(preimage, cs, opts) }); });
388
+ });
389
+ return Promise.all(jobs.map(function (j) { return j.p; })).then(function (built) {
390
+ var byTarget = {}, certDers = [];
391
+ built.forEach(function (res, i) { (byTarget[jobs[i].t] = byTarget[jobs[i].t] || []).push(res.value); certDers.push(res.certDer); });
392
+
393
+ var newSiSet = b.set(siSet.children.map(function (siNode, idx) {
394
+ if (!byTarget[idx]) return siNode.bytes;
395
+ return opts.countersignatureOf == null ? _appendCountersigs(siNode, byTarget[idx]) : _spliceNested(siNode, opts.countersignatureOf, byTarget[idx]);
396
+ }));
397
+
398
+ // Rebuild SignedData: version, digestAlgorithms (UNCHANGED -- a countersignature digest is not a
399
+ // SignedData digestAlgorithm), encapContentInfo, certificates [0]?, crls [1]?, the new signerInfos.
400
+ var certsNode = null, crlsNode = null;
401
+ for (var i = 3; i < sdKids.length - 1; i++) {
402
+ if (sdKids[i].tagClass === "context" && sdKids[i].tagNumber === 0) certsNode = sdKids[i];
403
+ else if (sdKids[i].tagClass === "context" && sdKids[i].tagNumber === 1) crlsNode = sdKids[i];
404
+ }
405
+ var existing = [];
406
+ if (certsNode) certsNode.children.forEach(function (c) { existing.push(c.bytes); });
407
+ if (opts.certificates !== false) certDers.forEach(function (d) { existing.push(d); });
408
+ var allCerts = _dedupe(existing).sort(Buffer.compare); // X.690 sec. 11.6
409
+
410
+ var newSdFields = [sdKids[0].bytes, sdKids[1].bytes, sdKids[2].bytes]; // version, digestAlgs, encap (raw)
411
+ if (allCerts.length) newSdFields.push(b.contextConstructed(0, Buffer.concat(allCerts)));
412
+ if (crlsNode) newSdFields.push(crlsNode.bytes);
413
+ newSdFields.push(newSiSet);
414
+ var newCi = b.sequence([root.children[0].bytes, b.explicit(0, b.sequence(newSdFields))]);
415
+ return opts.pem ? pkix.pemEncode(newCi, "CMS", frameworkError.PemError) : newCi;
416
+ });
417
+ }
418
+
194
419
  // Coverage residual -- three defensive branches are unreachable through the shipped path:
195
420
  // * `_skiValue`'s `cert.extensions || []` fallback -- x509.parse always surfaces `extensions`
196
421
  // as an array (empty when absent), so the `|| []` never fires.
@@ -199,4 +424,13 @@ function _toBuf(v, what) {
199
424
  // * `_assertKeyMatchesScheme`'s `!ka.hash` guard -- an `imp.hash` is set only for an RSA
200
425
  // scheme, which requires `ka.name` to already equal the RSA name (else the earlier name
201
426
  // check throws); an RSA CryptoKey always carries a `hash`, so `!ka.hash` never fires.
202
- module.exports = { sign: sign };
427
+ // Countersign-side residuals also unreachable through the shipped path:
428
+ // * `_resolveSignerIndices`'s `n < 1` throw -- a parsed SignedData always carries at least one
429
+ // SignerInfo, so the default index [0] is always in range.
430
+ // * `_spliceNested`'s no-countersignature / index-out-of-range / not-found throws -- the same node
431
+ // is validated by `_targetPreimage` FIRST (it computes the nested preimage before the build), so
432
+ // by the time `_spliceNested` re-walks it those conditions cannot hold; the checks are
433
+ // belt-and-suspenders against a future caller reordering the two.
434
+ // * the `crls [1]` preservation branches in `countersign` -- pki.cms.sign never emits a crls field,
435
+ // so a store this producer countersigns never carries one to preserve.
436
+ module.exports = { sign: sign, countersign: countersign };
package/lib/cms-verify.js CHANGED
@@ -51,6 +51,7 @@ function _err(code, message, cause) { return new CmsError(code, message, cause);
51
51
 
52
52
  var OID_MESSAGE_DIGEST = oid.byName("messageDigest");
53
53
  var OID_CONTENT_TYPE = oid.byName("contentType");
54
+ var OID_COUNTERSIGNATURE = oid.byName("countersignature");
54
55
 
55
56
  // A digest-algorithm name -> the WebCrypto hash.
56
57
  var DIGEST_HASH = { sha1: "SHA-1", sha256: "SHA-256", sha384: "SHA-384", sha512: "SHA-512" };
@@ -309,12 +310,16 @@ function _decodeSignedAttrs(setOfBytes) {
309
310
 
310
311
  // Verify one SignerInfo. Returns { ok, sid, cert, ... } -- a resolved false is a verdict; a
311
312
  // structural failure carries a code.
312
- function _verifyOne(si, content, eContentType, parsedCerts) {
313
+ // `csTarget`, when set (a Buffer), makes this a COUNTERSIGNATURE verify (RFC 5652 sec. 11.4): the
314
+ // preimage is the countersigned signature octets, not the sec. 5.4 content preimage, and no
315
+ // content-type attribute is required or checked (_computeCountersigBytes). content/eContentType are
316
+ // unused in that mode.
317
+ function _verifyOne(si, content, eContentType, parsedCerts, csTarget) {
313
318
  // A composite ML-DSA SignerInfo (draft-ietf-lamps-cms-composite-sigs) is keyed by its composite
314
319
  // OID, not a SIG_SCHEME name; intercept it before the classical dispatch (its verify has no
315
320
  // single external signature hash, so it never routes through the SIG_SCHEME/sigHash gate below).
316
321
  var composite = compositeSig.COMPOSITE_ALGS[si.signatureAlgorithm.oid];
317
- if (composite) return _verifyComposite(si, composite, content, eContentType, parsedCerts);
322
+ if (composite) return _verifyComposite(si, composite, content, eContentType, parsedCerts, csTarget);
318
323
  var scheme = SIG_SCHEME[si.signatureAlgorithm.name];
319
324
  if (!scheme) return Promise.resolve({ ok: false, code: "cms/unsupported-algorithm", sid: si.sid, message: "unsupported signature algorithm " + JSON.stringify(si.signatureAlgorithm.name) });
320
325
  var digestHash = DIGEST_HASH[si.digestAlgorithm.name];
@@ -362,12 +367,43 @@ function _verifyOne(si, content, eContentType, parsedCerts) {
362
367
  if (!signers.length) return Promise.resolve({ ok: false, code: "cms/signer-cert-not-found", sid: si.sid, message: "no certificate matches this SignerInfo's signer identifier" });
363
368
  var sigBytes = _toBuf(si.signature, "the SignerInfo signature");
364
369
 
365
- return _computeSignedBytes(si, content, eContentType).then(function (signedBytes) {
370
+ return (csTarget != null ? _computeCountersigBytes(si, csTarget) : _computeSignedBytes(si, content, eContentType)).then(function (signedBytes) {
366
371
  if (signedBytes && signedBytes.mismatch) return { ok: false, code: signedBytes.mismatch.code, sid: si.sid, cert: signers[0].der, message: signedBytes.mismatch.message };
367
372
  return _verifyAgainstCandidates(scheme, sigHash, sigBytes, signedBytes, si.sid, signers, pss ? pss.saltLength : 0, scheme.sameKeyOid ? oid.byName(si.signatureAlgorithm.name) : null);
368
373
  });
369
374
  }
370
375
 
376
+ // The exact bytes a countersignature covers (RFC 5652 sec. 11.4): the countersigned SignerInfo's
377
+ // signature value octets (`csTarget`). With signed attributes present, the message-digest attribute
378
+ // MUST equal digest(csTarget) under the countersignature's own digestAlgorithm and the signature
379
+ // covers the re-tagged SignedAttributes ([0] -> universal SET OF) -- the identical transform the
380
+ // top-level signer uses, minus the content-type attribute (which sec. 11.4 FORBIDS). Resolves the
381
+ // signed bytes, or { mismatch } when the message-digest disagrees.
382
+ function _computeCountersigBytes(si, csTarget) {
383
+ return Promise.resolve().then(function () {
384
+ if (!si.signedAttrsBytes) return csTarget; // no signed attributes: sign over the target signature octets directly
385
+ var reTagged = Buffer.from(si.signedAttrsBytes);
386
+ reTagged[0] = 0x31; // [0] IMPLICIT -> universal SET OF
387
+ var attrs;
388
+ try {
389
+ attrs = _decodeSignedAttrs(reTagged);
390
+ } catch (e) { if (e instanceof CmsError) throw e; throw _err("cms/bad-signed-attrs", "the countersignature signedAttrs is not a valid SET OF Attribute", e); }
391
+ // sec. 11.4: a content-type attribute MUST NOT appear (the parser already rejects it at parse, but
392
+ // verify never relies on the parse alone -- a parsed-object input could carry one).
393
+ if (attrs.filter(function (a) { return a.type === OID_CONTENT_TYPE; }).length) throw _err("cms/misplaced-attr", "a countersignature must not carry a content-type attribute (RFC 5652 sec. 11.4)");
394
+ var mdAttr = attrs.filter(function (a) { return a.type === OID_MESSAGE_DIGEST; })[0];
395
+ if (!mdAttr || mdAttr.values.length !== 1) throw _err("cms/bad-signed-attrs", "a countersignature's signedAttrs must carry exactly one message-digest attribute (RFC 5652 sec. 11.4)");
396
+ var declared;
397
+ try {
398
+ declared = asn1.read.octetString(mdAttr.values[0]);
399
+ } catch (e) { throw _err("cms/bad-signed-attrs", "the message-digest attribute value is not an OCTET STRING", e); }
400
+ return _computeDigest(si.digestAlgorithm.name, csTarget).then(function (d) {
401
+ if (!d.equals(declared)) return { mismatch: { code: "cms/message-digest-mismatch", message: "the countersignature message-digest does not match the countersigned signature" } };
402
+ return reTagged;
403
+ });
404
+ });
405
+ }
406
+
371
407
  // The exact bytes the signature covers (RFC 5652 sec. 5.4), shared by the classical and composite
372
408
  // verify paths so both bind the message-digest / content-type checks to the VERIFIED preimage (not
373
409
  // the caller-mutable parsed si.signedAttrs). Resolves the signed bytes, or { mismatch } when a
@@ -418,7 +454,7 @@ function _computeSignedBytes(si, content, eContentType) {
418
454
  // pre-hash. It reuses the sec. 5.4 preimage + message-digest binding UNCHANGED, then dispatches
419
455
  // compositeVerify per candidate certificate (BOTH components must verify -- never an AND->OR
420
456
  // downgrade).
421
- function _verifyComposite(si, comp, content, eContentType, parsedCerts) {
457
+ function _verifyComposite(si, comp, content, eContentType, parsedCerts, csTarget) {
422
458
  if (si.signatureAlgorithm.parameters !== null && si.signatureAlgorithm.parameters !== undefined) {
423
459
  return Promise.resolve({ ok: false, code: "cms/unsupported-algorithm", sid: si.sid, message: "the composite signatureAlgorithm parameters must be absent (draft-ietf-lamps-cms-composite-sigs sec. 3.4)" });
424
460
  }
@@ -447,7 +483,7 @@ function _verifyComposite(si, comp, content, eContentType, parsedCerts) {
447
483
  var signers = _findSignerCerts(si.sid, parsedCerts);
448
484
  if (!signers.length) return Promise.resolve({ ok: false, code: "cms/signer-cert-not-found", sid: si.sid, message: "no certificate matches this SignerInfo's signer identifier" });
449
485
  var sigBytes = _toBuf(si.signature, "the SignerInfo signature");
450
- return _computeSignedBytes(si, content, eContentType).then(function (signedBytes) {
486
+ return (csTarget != null ? _computeCountersigBytes(si, csTarget) : _computeSignedBytes(si, content, eContentType)).then(function (signedBytes) {
451
487
  if (signedBytes && signedBytes.mismatch) return { ok: false, code: signedBytes.mismatch.code, sid: si.sid, cert: signers[0].der, message: signedBytes.mismatch.message };
452
488
  return _verifyCompositeAgainstCandidates(comp, sigBytes, signedBytes, si.sid, signers, si.signatureAlgorithm.oid);
453
489
  });
@@ -480,6 +516,49 @@ function _verifyCompositeAgainstCandidates(comp, sigBytes, signedBytes, sid, can
480
516
  return attempt(0);
481
517
  }
482
518
 
519
+ // The decoded unsigned attributes of a SignerInfo, surfaced UNAUTHENTICATED (they are outside the
520
+ // signature) with each OID resolved to a name -- so a caller can read an attached RFC 3161 timestamp
521
+ // token or inspect a countersignature attribute. NEVER counted toward a signer's ok / res.valid.
522
+ function _surfaceUnsignedAttrs(si) {
523
+ return (si.unsignedAttrs || []).map(function (a) {
524
+ return { type: a.type, typeName: oid.name(a.type) || null, values: a.values };
525
+ });
526
+ }
527
+
528
+ // Verify every countersignature attached to `si` (RFC 5652 sec. 11.4): each id-countersignature
529
+ // value is a SignerInfo over `si`'s signature octets. Returns per-countersignature verdicts; a
530
+ // countersignature's OWN countersignatures verify over ITS signature octets (recursive). A
531
+ // present-but-invalid countersignature is surfaced ok:false -- never silently dropped. The recursion
532
+ // terminates because it only walks the FINITE parsed structure: each nested countersignature value
533
+ // is a sub-encoding of its parent, and the strict decoder already bounds total nesting by
534
+ // C.LIMITS.DER_MAX_DEPTH at parse (CWE-834/770), so a hostile deep chain fails closed before verify.
535
+ function _verifyCountersignatures(si, parsedCerts) {
536
+ // si.signature is always a Buffer from the strict parser (and from walkCountersignature on the
537
+ // recursive path), so _toBuf is a pass-through here -- the SignerInfo is already well-formed.
538
+ var targetSig = _toBuf(si.signature, "the countersigned signature");
539
+ var values = [];
540
+ (si.unsignedAttrs || []).forEach(function (a) {
541
+ if (a.type !== OID_COUNTERSIGNATURE) return;
542
+ a.values.forEach(function (v) { values.push(v && v.bytes ? v.bytes : v); });
543
+ });
544
+ return Promise.all(values.map(function (vDer) { return _verifyOneCountersig(vDer, targetSig, parsedCerts); }));
545
+ }
546
+
547
+ function _verifyOneCountersig(vDer, targetSig, parsedCerts) {
548
+ var csSi;
549
+ try { csSi = cms.walkCountersignature(asn1.decode(Buffer.isBuffer(vDer) ? vDer : Buffer.from(vDer))); }
550
+ catch (e) { return Promise.resolve({ ok: false, code: (e instanceof CmsError ? e.code : "cms/bad-countersignature"), message: e && e.message }); }
551
+ return _verifyOne(csSi, targetSig, null, parsedCerts, targetSig).then(function (verdict) {
552
+ return _verifyCountersignatures(csSi, parsedCerts).then(function (nested) {
553
+ var node = { ok: verdict.ok, sid: verdict.sid, cert: verdict.cert, digestAlgorithm: csSi.digestAlgorithm.name };
554
+ if (verdict.code) node.code = verdict.code;
555
+ node.unsignedAttrs = _surfaceUnsignedAttrs(csSi);
556
+ node.countersignatures = nested;
557
+ return node;
558
+ });
559
+ });
560
+ }
561
+
483
562
  /**
484
563
  * @primitive pki.cms.verify
485
564
  * @signature pki.cms.verify(input, opts?) -> Promise<{ valid, signers }>
@@ -533,8 +612,17 @@ function verify(input, opts) {
533
612
  (parsed.certificates || []).forEach(function (c) { _addCert(parsedCerts, c && c.bytes ? c.bytes : c); });
534
613
  (opts.certs || []).forEach(function (c) { _addCert(parsedCerts, c); });
535
614
  var eContentType = parsed.encapContentInfo.eContentType;
536
- return Promise.all(parsed.signerInfos.map(function (si) { return _verifyOne(si, content, eContentType, parsedCerts); }))
537
- .then(function (signers) { return { valid: signers.length > 0 && signers.every(function (s) { return s.ok === true; }), signers: signers }; });
615
+ return Promise.all(parsed.signerInfos.map(function (si) {
616
+ // res.valid reflects only the PRIMARY signerInfos; a countersignature / unsigned attribute is
617
+ // outside the signature and NEVER flips the top-level verdict. Both are surfaced per signer.
618
+ return _verifyOne(si, content, eContentType, parsedCerts).then(function (verdict) {
619
+ return _verifyCountersignatures(si, parsedCerts).then(function (countersignatures) {
620
+ verdict.countersignatures = countersignatures;
621
+ verdict.unsignedAttrs = _surfaceUnsignedAttrs(si);
622
+ return verdict;
623
+ });
624
+ });
625
+ })).then(function (signers) { return { valid: signers.length > 0 && signers.every(function (s) { return s.ok === true; }), signers: signers }; });
538
626
  }
539
627
  // Parse a candidate cert DER and index its SKI; a cert that will not parse is skipped (it
540
628
  // simply cannot be a signer match, and a malformed embedded cert must not fail the verify).
@@ -595,12 +683,61 @@ function _addCert(out, der) {
595
683
  * @opts sid `"issuerAndSerial"` (default) or `"ski"` (subjectKeyIdentifier).
596
684
  * @opts certificates Embed the signer certificates in the output. Default true.
597
685
  * @opts pem Return a PEM string (`-----BEGIN CMS-----`) instead of a DER Buffer.
686
+ * @opts unsignedAttributes Unsigned attributes for every SignerInfo, each `{ type, values }`
687
+ * (`type` an OID name or dotted string, `values` an array of DER value
688
+ * `Buffer`s). Placed in the SignerInfo `[1]` unsignedAttrs -- OUTSIDE the
689
+ * signature, so they carry no cryptographic assurance and a verifier never
690
+ * reports them authenticated. The vehicle for an RFC 3161 timestamp token
691
+ * (`timeStampToken`); content-type / message-digest / signing-time are
692
+ * rejected here (RFC 5652 sec. 11). Use `pki.cms.countersign` for a
693
+ * countersignature specifically.
598
694
  * @example
599
695
  * var p7 = await pki.cms.sign(Buffer.from("hello"), { cert: signerCertDer, key: signerKeyPkcs8 });
600
696
  * var res = await pki.cms.verify(p7); // res.valid === true
601
697
  */
602
698
  var sign = cmsSign.sign;
603
699
 
700
+ /**
701
+ * @primitive pki.cms.countersign
702
+ * @signature pki.cms.countersign(cms, signers, opts?) -> Promise<Buffer|string>
703
+ * @since 0.3.13
704
+ * @status experimental
705
+ * @spec RFC 5652
706
+ * @related pki.cms.sign, pki.cms.verify, pki.schema.cms.parse
707
+ *
708
+ * Add one or more countersignatures to an existing CMS SignedData (RFC 5652 sec. 11.4). A
709
+ * countersignature is itself a `SignerInfo` whose signature covers the CONTENTS of the countersigned
710
+ * SignerInfo's signature value (NOT the content), attached as the `id-countersignature` unsigned
711
+ * attribute -- the construction Authenticode timestamps and signature-attestation workflows rest on.
712
+ * `cms` is the SignedData (a DER `Buffer` or a PEM `CMS` string -- the wire bytes are preserved, so
713
+ * the countersigned primary still verifies byte-for-byte). Each `signers[i]` is the same descriptor
714
+ * `pki.cms.sign` takes -- `{ cert, key, digestAlgorithm?, pss? }`, any RSA / RSASSA-PSS / ECDSA /
715
+ * EdDSA / ML-DSA / SLH-DSA / composite key -- and countersigns over the target signature octets;
716
+ * `pki.cms.verify` returns each countersignature verdict under `signers[i].countersignatures`. A
717
+ * countersignature never carries a content-type attribute (sec. 11.4); multiple countersignatures on
718
+ * one signer are multiple values of the one id-countersignature attribute.
719
+ *
720
+ * @opts signerIndex Which primary SignerInfo(s) to countersign: an index (default 0), an
721
+ * array of indices, or `"all"`.
722
+ * @opts countersignatureOf Countersign the Nth existing countersignature of the target signer
723
+ * instead of the primary signature (a nested countersignature).
724
+ * @opts signedAttributes Include signed attributes (message-digest + signing-time; NEVER a
725
+ * content-type). Default true; false signs the target signature octets
726
+ * directly.
727
+ * @opts signingTime A `Date` for the countersignature's signing-time, or false to omit it.
728
+ * @opts additionalSignedAttributes Extra signed attributes on the countersignature (a content-type
729
+ * is rejected, RFC 5652 sec. 11.4).
730
+ * @opts sid `"issuerAndSerial"` (default) or `"ski"`.
731
+ * @opts certificates Embed the countersigner certificate(s) in the SignedData. Default true.
732
+ * @opts pem Return a PEM string instead of a DER Buffer.
733
+ * @example
734
+ * var p7 = await pki.cms.sign(Buffer.from("hi"), { cert: signerCertDer, key: signerKeyPkcs8 });
735
+ * var cs = await pki.cms.countersign(p7, { cert: signerCertDer, key: signerKeyPkcs8 });
736
+ * var res = await pki.cms.verify(cs);
737
+ * res.signers[0].countersignatures[0].ok; // true
738
+ */
739
+ var countersign = cmsSign.countersign;
740
+
604
741
  /**
605
742
  * @primitive pki.cms.encrypt
606
743
  * @signature pki.cms.encrypt(content, recipients, opts?) -> Promise<Buffer | string>
@@ -633,25 +770,66 @@ var sign = cmsSign.sign;
633
770
  */
634
771
  var encrypt = cmsEncrypt.encrypt;
635
772
 
773
+ /**
774
+ * @primitive pki.cms.authenticate
775
+ * @signature pki.cms.authenticate(content, recipients, opts?) -> Promise<Buffer | string>
776
+ * @since 0.3.14
777
+ * @status experimental
778
+ * @spec RFC 5652, RFC 2104, RFC 4231, RFC 3370
779
+ * @related pki.cms.decrypt, pki.schema.cms.parse
780
+ *
781
+ * Produce a CMS AuthenticatedData (RFC 5652 sec. 9): CLEARTEXT content plus a MAC (HMAC-SHA-2) --
782
+ * authenticated but NOT encrypted, the authenticated-only sibling of AuthEnvelopedData. A single
783
+ * fresh MAC key is minted and wrapped for every recipient with the SAME RecipientInfo model
784
+ * `pki.cms.encrypt` uses for a content-encryption key, so `recipients` is the identical array of
785
+ * descriptors: `{ cert }` (RSA -> ktri RSAES-OAEP, EC/X25519/X448 -> kari, ML-KEM -> ori/KEMRI),
786
+ * `{ password }` (pwri), or `{ kek, kekId }` (kekri). By default the MAC covers the authenticated
787
+ * attributes (a content-type and a message-digest = digest of the content) re-tagged to the EXPLICIT
788
+ * SET OF (RFC 5652 sec. 9.2); with `authenticatedAttributes: false` (id-data content only) it covers
789
+ * the content octets directly. `pki.cms.decrypt` recovers the MAC key, recomputes the MAC, and --
790
+ * with authenticated attributes -- independently confirms the message-digest before releasing the
791
+ * content, returning `authenticated: true`. Malformed input throws a typed `CmsError`.
792
+ *
793
+ * @opts macAlgorithm `"hmac-sha256"` (default) / `"hmac-sha384"` / `"hmac-sha512"`.
794
+ * @opts digestAlgorithm The authenticated-attributes message-digest hash (defaults to the MAC hash).
795
+ * @opts authenticatedAttributes Include the content-type + message-digest authenticated attributes
796
+ * (the default, and required for a non-`data` content type); false MACs the
797
+ * content directly.
798
+ * @opts authAttrs Extra authenticated attributes appended to the content-type + message-digest pair.
799
+ * @opts contentType The encapsulated content type (an OID name). Default `data`.
800
+ * @opts keyIdentifier `"issuerAndSerial"` (default) or `"subjectKeyIdentifier"`.
801
+ * @opts pem Return a PEM string (`-----BEGIN CMS-----`) instead of a DER Buffer.
802
+ * @example
803
+ * var r = pki.cms; // authenticate a message to an RSA recipient, then verify via decrypt
804
+ * var auth = await r.authenticate(Buffer.from("hi"), [{ cert: recipientCertDer }]);
805
+ * var out = await r.decrypt(auth, { key: recipientKeyPkcs8, cert: recipientCertDer });
806
+ * out.authenticated; // true
807
+ */
808
+ var authenticate = cmsEncrypt.authenticate;
809
+
636
810
  /**
637
811
  * @primitive pki.cms.decrypt
638
812
  * @signature pki.cms.decrypt(input, keyMaterial, opts?) -> Promise<{ content, contentType, contentTypeName, recipientType, recipientIndex, contentEncryptionAlgorithm, authenticated }>
639
813
  * @since 0.2.23
640
814
  * @status experimental
641
- * @spec RFC 5652, RFC 5083, RFC 5084, RFC 3560, RFC 5753, RFC 8418, RFC 9629, RFC 9936, RFC 3211, RFC 8018, RFC 3218
642
- * @related pki.cms.encrypt, pki.schema.cms.parse
815
+ * @spec RFC 5652, RFC 5083, RFC 5084, RFC 3560, RFC 5753, RFC 8418, RFC 9629, RFC 9936, RFC 3211, RFC 8018, RFC 3218, RFC 2104
816
+ * @related pki.cms.encrypt, pki.cms.authenticate, pki.schema.cms.parse
643
817
  *
644
- * Decrypt a CMS EnvelopedData / AuthEnvelopedData / EncryptedData (DER Buffer or PEM). It selects
645
- * the recipient the key material targets, acquires the content-encryption key through the matching
646
- * arm (ktri OAEP or PKCS#1 v1.5 decrypt-only; kari ECDH / X25519 / X448; kekri; pwri; ori/ML-KEM),
647
- * and decrypts + authenticates the content. `keyMaterial` is `{ key, cert }` (the recipient
648
- * private key + its certificate, which drives recipient matching), `{ password }`, `{ kek, kekId? }`,
649
- * or `{ cek }` (EncryptedData raw-key mode). Fail-closed and oracle-free: every secret-dependent
650
- * failure -- a bad key-wrap, a padding fault, a GCM tag mismatch, a PWRI check-byte mismatch --
651
- * collapses to the SINGLE uniform `cms/decrypt-failed` verdict (Bleichenbacher / EFAIL oracle
652
- * freedom), and the PKCS#1 v1.5 arm applies the RFC 3218 implicit-rejection countermeasure so its
653
- * failure is indistinguishable. `authenticated` is true only for AuthEnvelopedData; a CBC
654
- * EnvelopedData surfaces `authenticated: false` (the EFAIL caveat in the verdict itself).
818
+ * Decrypt a CMS EnvelopedData / AuthEnvelopedData / EncryptedData, or verify a CMS AuthenticatedData
819
+ * (DER Buffer or PEM). It selects the recipient the key material targets, acquires the
820
+ * content-encryption or MAC key through the matching arm (ktri OAEP or PKCS#1 v1.5 decrypt-only; kari
821
+ * ECDH / X25519 / X448; kekri; pwri; ori/ML-KEM), and decrypts (or MAC-verifies) the content.
822
+ * `keyMaterial` is `{ key, cert }` (the recipient private key + its certificate, which drives recipient
823
+ * matching), `{ password }`, `{ kek, kekId? }`, or `{ cek }` (EncryptedData raw-key mode). Fail-closed
824
+ * and oracle-free: every secret-dependent failure -- a bad key-wrap, a padding fault, a GCM tag
825
+ * mismatch, a PWRI check-byte mismatch, an AuthenticatedData MAC/message-digest mismatch -- collapses
826
+ * to the SINGLE uniform `cms/decrypt-failed` verdict (Bleichenbacher / EFAIL oracle freedom, and no
827
+ * unwrap-success bit for the MAC path), and the PKCS#1 v1.5 arm applies the RFC 3218 implicit-rejection
828
+ * countermeasure so its failure is indistinguishable. For an AuthenticatedData the MAC (HMAC-SHA-2)
829
+ * and, when authenticated attributes are present, the message-digest are verified before the content
830
+ * is released, and the result carries `macAlgorithm` / `digestAlgorithm` in place of
831
+ * `contentEncryptionAlgorithm`. `authenticated` is true for AuthEnvelopedData and AuthenticatedData; a
832
+ * CBC EnvelopedData surfaces `authenticated: false` (the EFAIL caveat in the verdict itself).
655
833
  *
656
834
  * @opts recipientIndex Explicitly select the recipient by index (overrides key-material matching).
657
835
  * @opts maxIterations Lower the PBKDF2 iteration cap (a DoS bound; downward only).
@@ -708,4 +886,4 @@ var compress = cmsCompress.compress;
708
886
  */
709
887
  var decompress = cmsCompress.decompress;
710
888
 
711
- module.exports = { verify: verify, sign: sign, encrypt: encrypt, decrypt: decrypt, compress: compress, decompress: decompress };
889
+ module.exports = { verify: verify, sign: sign, countersign: countersign, encrypt: encrypt, authenticate: authenticate, decrypt: decrypt, compress: compress, decompress: decompress };
package/lib/schema-cms.js CHANGED
@@ -1204,6 +1204,12 @@ function walkEnvelopedData(node) { return schema.walk(ENVELOPED_DATA, node, NS).
1204
1204
  // walkEnvelopedData: the node is the bare structure, typed cms/* on rejection.
1205
1205
  function walkSignedData(node) { return schema.walk(SIGNED_DATA, node, NS).result; }
1206
1206
  function walkEncryptedData(node) { return schema.walk(ENCRYPTED_DATA, node, NS).result; }
1207
+ // Validate + surface one countersignature value (RFC 5652 sec. 11.4, Countersignature ::=
1208
+ // SignerInfo) into the same parsed shape parse() gives a top-level SignerInfo -- so pki.cms.verify
1209
+ // can verify a countersignature through the ONE SignerInfo definition (its sec. 11.4 rules
1210
+ // enforced: message-digest present, content-type FORBIDDEN via mode "countersig"). The node is the
1211
+ // bare id-countersignature value; a malformed / content-type-carrying value throws a typed cms/*.
1212
+ function walkCountersignature(node) { return schema.walk(COUNTERSIGNATURE_SIGNER_INFO, node, NS).result; }
1207
1213
 
1208
1214
  // assertAttachedCiphertext(eci, E, code, label): reject an EnvelopedData /
1209
1215
  // EncryptedData encryptedContentInfo whose encryptedContent (the payload itself)
@@ -1227,6 +1233,7 @@ module.exports = {
1227
1233
  walkEnvelopedData: walkEnvelopedData,
1228
1234
  walkSignedData: walkSignedData,
1229
1235
  walkEncryptedData: walkEncryptedData,
1236
+ walkCountersignature: walkCountersignature,
1230
1237
  assertAttachedCiphertext: assertAttachedCiphertext,
1231
1238
  // The structure's own algorithm tables, exported (like the walk* helpers) as the single source
1232
1239
  // of truth the crypto layer (cms-encrypt / cms-decrypt) shares -- so the wrap<->KEK-length, the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blamejs/pki",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
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",
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:a2281c9c-5058-4827-83e2-53a8a4561863",
5
+ "serialNumber": "urn:uuid:7db025af-2d42-4d80-9d99-9e6ec17cd13f",
6
6
  "version": 1,
7
7
  "metadata": {
8
- "timestamp": "2026-07-24T02:15:58.035Z",
8
+ "timestamp": "2026-07-24T06:23:40.600Z",
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.3.12",
22
+ "bom-ref": "@blamejs/pki@0.3.14",
23
23
  "type": "application",
24
24
  "name": "pki",
25
- "version": "0.3.12",
25
+ "version": "0.3.14",
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.3.12",
29
+ "purl": "pkg:npm/%40blamejs/pki@0.3.14",
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.3.12",
57
+ "ref": "@blamejs/pki@0.3.14",
58
58
  "dependsOn": []
59
59
  }
60
60
  ]