@blamejs/pki 0.6.8 → 0.6.9

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,19 @@ 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.6.9 — 2026-08-31
8
+
9
+ pki.cmp.verify holds a received PKIMessage to the RFC 9483 sec. 3.5 header rules, and pki.path.build and pki.est read a response body an injected transport returns as any BufferSource, not only a Node Buffer.
10
+
11
+ ### Changed
12
+
13
+ - pki.cmp.verify holds a received PKIMessage to the RFC 9483 sec. 3.5 receiving-side header rules once its protection verifies: the pvno must be cmp2000(2) or cmp2021(3), the transactionID must be present, and the senderNonce must be present and carry at least 128 bits. A message failing any of these is a fail-closed verdict (cmp/unsupported-version, cmp/bad-transaction-id, cmp/bad-sender-nonce). A cmp1999(1) message stays valid RFC 9810 syntax that pki.cmp.parse still decodes; the profile refuses it only on receipt. pki.cmp.session applies these checks to every response and additionally refuses a waiting ip/cp/kup CertResponse that carries failInfo (RFC 9483 sec. 4.4). A conformant peer is unaffected; a peer that omits a required header field is now refused.
14
+
15
+ ### Fixed
16
+
17
+ - pki.path.build's opt-in AIA caIssuers fetch reads a certificate an injected transport returns as any BufferSource (a Uint8Array, a raw ArrayBuffer, a DataView), not only a Node Buffer. A typed-array body was coerced to a comma-joined string and failed to parse, silently dropping an AIA-discoverable intermediate from the built path; it is now re-viewed through the byte guard and parsed from its bytes.
18
+ - Every pki.est network verb reads a response body an injected transport returns as any BufferSource, not only a Node Buffer or a string. The transfer decode, the multipart splitter, the response-size checks, and the error diagnostic take the raw bytes through the byte guard, so a cacerts, enrollment, or serverkeygen response delivered in a typed array is parsed and sized from its bytes.
19
+
7
20
  ## v0.6.8 — 2026-08-31
8
21
 
9
22
  pki.cmp.build assembles the ccr cross-certification request body (RFC 9810 sec. 5.3.11), and the ACME client accepts a response body an injected transport returns as any BufferSource, not only a Node Buffer.
@@ -847,6 +847,13 @@ function session(opts) {
847
847
  if (!resp) return { state: "unexpected", reason: "a " + arm + " carried no CertResponse for certReqId " + activeCertReqId };
848
848
  var code = resp.status && resp.status.status ? resp.status.status.code : null;
849
849
  var isGrantLeg = _isGranted(code);
850
+ // RFC 9483 sec. 4.4: a "waiting" CertResponse must not carry failInfo -- a wait is not a failure. Refuse
851
+ // a contradictory failInfo (mirroring the _classifyError waiting check). A granting status carrying
852
+ // failInfo is already refused upstream: the parser rejects failInfo alongside a certifiedKeyPair (RFC 9810
853
+ // sec. 5.3.4), and a grant with no certificate is refused as a missing issued certificate.
854
+ if (code === 3 && resp.status && resp.status.failInfo != null) {
855
+ return { state: "unexpected", reason: "a waiting " + arm + " CertResponse must not carry failInfo (RFC 9483 sec. 4.4)" };
856
+ }
850
857
  // Accumulate the AUTHENTICATED caPubs of EVERY ip/cp/kup leg (waiting AND granting): a CA may deliver the
851
858
  // issuing chain in a `waiting` response and omit it from the grant, so the leaf's issuer must survive the poll.
852
859
  // Bound + validate + dedup the caPubs AS ACCUMULATED, not only when _finish is reached: an authenticated CA
package/lib/cmp-verify.js CHANGED
@@ -523,6 +523,19 @@ function _bufEq(a, x) {
523
523
  return _compare(a, x) === 0;
524
524
  }
525
525
  function _headerChecks(m, opts) {
526
+ // RFC 9483 sec. 3.5: the pvno MUST be cmp2000(2) or cmp2021(3) (failInfo unsupportedVersion). The parser
527
+ // accepts cmp1999(1) as valid RFC 9810 sec. 5.1.1 syntax; the lightweight profile refuses it on receipt.
528
+ if (m.header.pvno !== 2 && m.header.pvno !== 3) {
529
+ return { code: "cmp/unsupported-version", reason: "the header pvno is " + m.header.pvno + "; a received PKIMessage MUST carry cmp2000(2) or cmp2021(3) (RFC 9483 sec. 3.5)" };
530
+ }
531
+ // RFC 9483 sec. 3.5: the transactionID MUST be present (failInfo badDataFormat).
532
+ if (!guard.bytes.isByteSource(m.header.transactionID)) {
533
+ return { code: "cmp/bad-transaction-id", reason: "the header transactionID is absent; a received PKIMessage MUST carry one (RFC 9483 sec. 3.5)" };
534
+ }
535
+ // RFC 9483 sec. 3.5: the senderNonce MUST be present and MUST contain at least 128 bits (failInfo badSenderNonce).
536
+ if (!guard.bytes.isByteSource(m.header.senderNonce) || guard.bytes.lengthOf(m.header.senderNonce) < 16) {
537
+ return { code: "cmp/bad-sender-nonce", reason: "the header senderNonce is absent or shorter than 128 bits (RFC 9483 sec. 3.5)" };
538
+ }
526
539
  if (opts.transactionID != null && !_bufEq(m.header.transactionID, opts.transactionID)) {
527
540
  return { code: "cmp/transaction-id-mismatch", reason: "header.transactionID does not equal the expected value (RFC 9810 sec. 5.1.1)" };
528
541
  }
@@ -1110,6 +1123,12 @@ async function _verify(message, opts) {
1110
1123
  * signature path the authenticated header `sender` field MUST match the signer certificate subject (RFC 9483
1111
1124
  * sec. 3.1), so a certificate the anchor trusts cannot sign under another party's sender name.
1112
1125
  *
1126
+ * On a protection-verified message the RFC 9483 sec. 3.5 receiving-side header rules apply: the `pvno` MUST be
1127
+ * cmp2000(2) or cmp2021(3) (a cmp1999(1) message stays valid RFC 9810 syntax `pki.schema.cmp.parse` decodes,
1128
+ * refused only on receipt), the `transactionID` MUST be present, and the `senderNonce` MUST be present and
1129
+ * carry at least 128 bits; a violation is a `{ valid: false }` verdict carrying `cmp/unsupported-version`,
1130
+ * `cmp/bad-transaction-id`, or `cmp/bad-sender-nonce`.
1131
+ *
1113
1132
  * Returns a verdict (never a bare boolean): `{ valid, trusted, protectionType, protectionAlg, signer,
1114
1133
  * transactionID, senderNonce, recipNonce, header, body, code?, reason? }`. `valid` is whether the
1115
1134
  * protection is cryptographically intact under the declared algorithm; `trusted` is whether a MAC secret
package/lib/est.js CHANGED
@@ -784,7 +784,17 @@ function classifyResponse(status, headers, body, opts) {
784
784
  // toString(enc, start, end) bounds the decode WITHOUT constructing a subarray
785
785
  // view, so a detached body reads as "" here rather than throwing a raw
786
786
  // TypeError that would mask the est/http-error verdict.
787
- var text = Buffer.isBuffer(body) ? body.toString("utf8", 0, 512) : String(body || "").slice(0, 512);
787
+ var text;
788
+ if (Buffer.isBuffer(body)) {
789
+ text = body.toString("utf8", 0, 512);
790
+ } else if (guard.bytes.isByteSource(body)) {
791
+ // A byte source from an injected transport (a Uint8Array / ArrayBuffer): decode a bounded prefix through
792
+ // the byte guard rather than String()-coercing it to a "48,130,..." comma list.
793
+ try { text = guard.bytes.source(body, EstError, "est/bad-input", "the EST error body").toString("utf8", 0, 512); }
794
+ catch (_e) { /* allow:swallow-unverified a detached / unreadable error body reads as an empty snippet rather than masking the est/http-error verdict with a raw TypeError */ text = ""; }
795
+ } else {
796
+ text = String(body || "").slice(0, 512);
797
+ }
788
798
  throw E("est/http-error", "EST server returned HTTP " + status + (text ? ": " + text : ""));
789
799
  }
790
800
  return { status: "unexpected", httpStatus: status };
@@ -1142,7 +1152,7 @@ function _drive(method, url, body, headers, opts, transport, budgets) {
1142
1152
  // Measure an injected string body as UTF-8 -- the byte width the body is decoded/transfer-decoded at,
1143
1153
  // and what the real socket transport counts -- so a non-ASCII body (multi-byte chars are ~half the
1144
1154
  // count under latin1) cannot undercount past the DoS cap and reach body handling.
1145
- var blen = Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : Buffer.byteLength(String(res.body == null ? "" : res.body), "utf8");
1155
+ var blen = res.body == null ? 0 : (typeof res.body === "string" ? Buffer.byteLength(res.body, "utf8") : (Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : guard.bytes.lengthOf(guard.bytes.source(res.body, EstError, "est/bad-input", "the response body"))));
1146
1156
  if (blen > budgets.maxResponseBytes) throw E("est/response-too-large", "the response body (" + blen + " bytes) exceeds the " + budgets.maxResponseBytes + "-byte cap (RFC 7030 sec. 6)");
1147
1157
  // The transport contract returns lowercased headers, but the injectable seam only promises
1148
1158
  // { status, headers, body }; normalize here so a redirect Location / WWW-Authenticate from an
@@ -1292,7 +1302,7 @@ function _certsResult(op, res, opts, csrSpki) {
1292
1302
  if (verdict.status !== "ok") throw E("est/http-error", "an EST " + op + " response must be HTTP 200 or 202 (RFC 7030 sec. 4.1.3 / 4.2.3), got " + res.status);
1293
1303
  // Measure the body as UTF-8 for parity with the transport cap above (this check only distinguishes empty
1294
1304
  // from non-empty, where the encoding is immaterial, but the shape is kept uniform so no site undercounts).
1295
- var bodyLen = Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : Buffer.byteLength(String(res.body == null ? "" : res.body), "utf8");
1305
+ var bodyLen = res.body == null ? 0 : (typeof res.body === "string" ? Buffer.byteLength(res.body, "utf8") : (Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : guard.bytes.lengthOf(guard.bytes.source(res.body, EstError, "est/bad-input", "the response body"))));
1296
1306
  if (bodyLen === 0) throw E("est/empty-body", "a 200 " + op + " response carried an empty body (RFC 7030 sec. 4.1.3 / 4.2.3)");
1297
1307
  var parsed = parseCertsOnly(transferDecode(res.body));
1298
1308
  if (op === "cacerts") return { certificates: parsed.certificates, crls: parsed.crls };
@@ -1856,7 +1866,7 @@ function _fullcmcResult(res, opts, wanted, sent) {
1856
1866
  "an EST /fullcmc response must be HTTP 200 or 202 (RFC 7030 sec. 4.3.2), got " + res.status);
1857
1867
  }
1858
1868
 
1859
- var bodyLen = Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : Buffer.byteLength(String(res.body == null ? "" : res.body), "utf8");
1869
+ var bodyLen = res.body == null ? 0 : (typeof res.body === "string" ? Buffer.byteLength(res.body, "utf8") : (Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : guard.bytes.lengthOf(guard.bytes.source(res.body, EstError, "est/bad-input", "the response body"))));
1860
1870
  if (bodyLen === 0) throw E("est/empty-body", "a 200 /fullcmc response carried an empty body (RFC 7030 sec. 4.3.2)");
1861
1871
  var der = transferDecode(res.body);
1862
1872
 
@@ -2255,7 +2265,7 @@ async function _serverkeygenResult(res, opts, derived) {
2255
2265
  // A 202 is an enrollment posture (RFC 7030 sec. 4.4 treats /serverkeygen as an enroll): surfaced, never slept.
2256
2266
  if (verdict.status === "retry") return { retry: true, retryAfterSeconds: verdict.retryAfterSeconds, retryAfterDate: verdict.retryAfterDate };
2257
2267
  if (verdict.status !== "ok") throw E("est/http-error", "an EST serverkeygen response must be HTTP 200 or 202 (RFC 7030 sec. 4.4.2), got " + res.status);
2258
- var bodyLen = Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : Buffer.byteLength(String(res.body == null ? "" : res.body), "utf8");
2268
+ var bodyLen = res.body == null ? 0 : (typeof res.body === "string" ? Buffer.byteLength(res.body, "utf8") : (Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : guard.bytes.lengthOf(guard.bytes.source(res.body, EstError, "est/bad-input", "the response body"))));
2259
2269
  if (bodyLen === 0) throw E("est/empty-body", "a 200 serverkeygen response carried an empty body (RFC 7030 sec. 4.4.2)");
2260
2270
  _assertConfidentialCipher(res);
2261
2271
  // parseServerKeygenResponse does the per-part transfer-decode; the recipient coherence is bound to the CSR.
@@ -2357,7 +2367,7 @@ function _csrattrsResult(res, opts) {
2357
2367
  // A 202 is nonconforming for a policy GET (sec. 4.5.2 lists only 200 / 204 / 404).
2358
2368
  if (verdict.status === "retry") throw E("est/http-error", "a /csrattrs response must be HTTP 200, 204, or 404, not 202 (RFC 7030 sec. 4.5.2)");
2359
2369
  if (verdict.status !== "ok") throw E("est/http-error", "an EST csrattrs response must be HTTP 200 / 204 / 404 (RFC 7030 sec. 4.5.2), got " + res.status);
2360
- var bodyLen = Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : Buffer.byteLength(String(res.body == null ? "" : res.body), "utf8");
2370
+ var bodyLen = res.body == null ? 0 : (typeof res.body === "string" ? Buffer.byteLength(res.body, "utf8") : (Buffer.isBuffer(res.body) ? guard.bytes.lengthOf(res.body) : guard.bytes.lengthOf(guard.bytes.source(res.body, EstError, "est/bad-input", "the response body"))));
2361
2371
  // An empty HTTP body is distinct from a valid EMPTY CsrAttrs (`30 00` / base64 `MAA=`), which parses as {items:[]}.
2362
2372
  if (bodyLen === 0) throw E("est/empty-body", "a 200 csrattrs response carried an empty body (RFC 7030 sec. 4.5.2)");
2363
2373
  var attrs = csrattrsFmt.parse(transferDecode(res.body));
package/lib/guard-text.js CHANGED
@@ -34,15 +34,14 @@ var intrinsic = require("./guard-intrinsic");
34
34
  // UTF-8 rule all still run and all still pass.
35
35
  // _byteLength measures the string arm against the cap. One returning 0 admits a string
36
36
  // of any size, which is the allocation-DoS this guard exists to bound.
37
- // _isInteger decides whether the cap itself is usable, and _isBuffer decides which arm
38
- // an input takes.
37
+ // _isInteger decides whether the cap itself is usable; guard.bytes.isByteSource decides
38
+ // whether an input takes the byte arm or the string arm.
39
39
  // The two method captures are uncurried, so their call sites read no property of them; see
40
40
  // guard-intrinsic for why capturing a method and then reading `.call` on it reopens the hole.
41
41
  var _toString = intrinsic.uncurry(Buffer.prototype.toString);
42
42
  var _decode = intrinsic.uncurry(TextDecoder.prototype.decode);
43
43
  // Already plain functions rather than methods, so these are invoked directly.
44
44
  var _byteLength = Buffer.byteLength;
45
- var _isBuffer = Buffer.isBuffer;
46
45
  var _isInteger = Number.isInteger;
47
46
  var _TextDecoder = TextDecoder;
48
47
 
@@ -58,10 +57,10 @@ var LATIN1 = "latin1";
58
57
  // tooLarge: code for the over-cap reject,
59
58
  // badDecode: code for a fatal-charset (bad-UTF-8) reject
60
59
  // (required only when fatal is set),
61
- // badInput: code for a non-Buffer/non-string reject,
60
+ // badInput: code for a non-byte-source/non-string reject,
62
61
  // label: human phrase for the message }
63
62
  // @enforced-by behavioral -- cap-before-copy has no rename-proof code shape; the
64
- // over-cap RED vectors + the detached re-view through guard.bytes.view (itself
63
+ // over-cap RED vectors + the detached re-view through guard.bytes.source (itself
65
64
  // enforced) are the guard.
66
65
  function decode(input, maxBytes, ErrorClass, spec) {
67
66
  // maxBytes is an authoring input: an undefined / NaN / fractional cap makes
@@ -72,12 +71,12 @@ function decode(input, maxBytes, ErrorClass, spec) {
72
71
  throw new TypeError("guard.text.decode: maxBytes must be a non-negative integer");
73
72
  }
74
73
  var charset = spec.charset || LATIN1;
75
- if (_isBuffer(input)) {
76
- // Re-view through the byte guard first so a detached backing ArrayBuffer (a
77
- // transferred / structuredClone'd Buffer, which reads as zero-length) fails
78
- // closed here -- the same detached-buffer defense the byte boundaries get --
79
- // instead of being decoded as an empty string. Then cap, then decode.
80
- input = bytes.view(input, ErrorClass, spec.badInput, spec.label);
74
+ if (bytes.isByteSource(input)) {
75
+ // Re-view through the byte guard first so any byte source (a Buffer, a Uint8Array or other typed array, a
76
+ // DataView, or a raw ArrayBuffer -- a caller-injected transport may return any of these) is normalized to a
77
+ // usable view, and a detached backing ArrayBuffer (a transferred / structuredClone'd view, which reads as
78
+ // zero-length) fails closed here instead of being decoded as an empty string. Then cap, then decode.
79
+ input = bytes.source(input, ErrorClass, spec.badInput, spec.label);
81
80
  // Measured through the byte guard's captured intrinsic getter, never `input.length`. On a typed
82
81
  // array `length` is an accessor on the prototype and it is configurable, so a replacement
83
82
  // returning 0 lets a buffer of any size past the cap and straight into the decode -- the
@@ -96,7 +95,7 @@ function decode(input, maxBytes, ErrorClass, spec) {
96
95
  if (byteLen > maxBytes) throw new ErrorClass(spec.tooLarge, spec.label + " exceeds the size cap");
97
96
  return input;
98
97
  }
99
- throw new ErrorClass(spec.badInput, spec.label + " expects a string or Buffer");
98
+ throw new ErrorClass(spec.badInput, spec.label + " expects a string or a byte source (Buffer / TypedArray / DataView / ArrayBuffer)");
100
99
  }
101
100
 
102
101
  // Frozen for the reason the family header gives: a boundary reads its guard off this object at the
@@ -3352,7 +3352,12 @@ async function _aiaFetchOne(uri, aia) {
3352
3352
  var res = await aia.transport({ method: "GET", url: uri, tls: aia.tls, timeout: aia.timeout, maxResponseBytes: aia.maxResponseBytes, blockPrivateAddresses: true });
3353
3353
  res = res || {};
3354
3354
  if (res.status !== 200) throw E("path/aia-status", "an AIA fetch returned HTTP " + res.status + " (only 200 is a cert source; no redirect following)");
3355
- var body = Buffer.isBuffer(res.body) ? res.body : Buffer.from(res.body == null ? "" : String(res.body), "latin1");
3355
+ // A binary view from an injected transport (Buffer / Uint8Array / ArrayBuffer / DataView) is re-viewed
3356
+ // through the byte guard, as the request path does; only a genuine string body falls back to a latin1
3357
+ // decode -- a typed array is never String()-coerced to "48,130,..." garbage that then fails to parse.
3358
+ var body = guard.bytes.isByteSource(res.body)
3359
+ ? guard.bytes.source(res.body, PathError, "path/aia-bad-body", "the AIA response body")
3360
+ : Buffer.from(res.body == null ? "" : String(res.body), "latin1");
3356
3361
  if (body.length === 0) throw E("path/aia-empty", "an AIA fetch returned an empty body");
3357
3362
  // Belt for an INJECTED transport that ignores maxResponseBytes (the real transport streaming-aborts at the cap).
3358
3363
  if (body.length > aia.maxResponseBytes) throw E("path/aia-too-large", "an AIA response exceeds the " + aia.maxResponseBytes + "-byte cap");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blamejs/pki",
3
- "version": "0.6.8",
3
+ "version": "0.6.9",
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:ce70e235-3eb4-4497-bf76-5f97f81cffee",
5
+ "serialNumber": "urn:uuid:2f668b08-8394-4a54-b202-eac9892a6e2f",
6
6
  "version": 1,
7
7
  "metadata": {
8
- "timestamp": "2026-08-31T13:12:39.009Z",
8
+ "timestamp": "2026-08-31T15:15:24.682Z",
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.6.8",
22
+ "bom-ref": "@blamejs/pki@0.6.9",
23
23
  "type": "application",
24
24
  "name": "pki",
25
- "version": "0.6.8",
25
+ "version": "0.6.9",
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.6.8",
29
+ "purl": "pkg:npm/%40blamejs/pki@0.6.9",
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.6.8",
57
+ "ref": "@blamejs/pki@0.6.9",
58
58
  "dependsOn": []
59
59
  }
60
60
  ]