@blamejs/core 0.12.34 → 0.12.36
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 +4 -0
- package/README.md +2 -1
- package/index.js +1 -0
- package/lib/cose.js +182 -0
- package/lib/cwt.js +13 -8
- package/lib/eat.js +240 -0
- package/package.json +1 -1
- package/sbom.cdx.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,10 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.12.x
|
|
10
10
|
|
|
11
|
+
- v0.12.36 (2026-05-24) — **`b.cose.encrypt0` / `b.cose.decrypt0` — COSE_Encrypt0 single-recipient AEAD (RFC 9052 §5.2).** Completes the COSE family with encryption alongside the v0.12.33 signing: COSE_Encrypt0 is the single-recipient AEAD container where the recipient already holds the symmetric key (direct mode). The default algorithm is ChaCha20/Poly1305 (COSE alg 24) — AES-GCM stays opt-in, since hard-rule #2 forbids AES-GCM as a default. The Enc_structure (`["Encrypt0", protected, external_aad]`) is bound as the AEAD associated data so the algorithm + any external context are authenticated, and the authentication tag is appended to the ciphertext per COSE. Composes the in-tree `b.cbor` codec and `node:crypto` AEAD. **Added:** *`b.cose.encrypt0(plaintext, opts)` / `b.cose.decrypt0(coseEncrypt0, opts)`* — `encrypt0` produces a tagged COSE_Encrypt0 with `alg` in the protected header and a random 12-byte IV in the unprotected header (label 5); `alg` is `"ChaCha20-Poly1305"` (default), `"A256GCM"`, or `"A128GCM"`, with the key length enforced (32 / 16 bytes). `decrypt0` reads the algorithm from the protected header (must be in the required `opts.algorithms` allowlist), reconstructs the Enc_structure as the AEAD AAD, and returns `{ plaintext, alg, protectedHeaders, unprotectedHeaders }`; a wrong key, tampered ciphertext, or `external_aad` mismatch fails AEAD authentication and is refused with `cose/decrypt-failed`. `external_aad` binds request context into the tag.
|
|
12
|
+
|
|
13
|
+
- v0.12.35 (2026-05-24) — **`b.eat` — Entity Attestation Token (RFC 9711) over `b.cwt`.** An EAT is the token a Relying Party asks a device or software entity to produce to prove what it is and what state it is in — a freshness nonce, a Universal Entity ID, OEM / hardware identifiers, debug status, software measurements, and nested submodule attestations. `b.eat` is the RFC 9711 profile over the v0.12.34 `b.cwt`: it maps the EAT claim names to their IANA CWT claim-key integer labels and adds the attestation-specific verification on top of the CWT signature + time checks. The central control is the verifier-nonce binding: when the Relying Party supplies a fresh `expectedNonce`, the token's `eat_nonce` (claim 10) must match it (constant-time compare) — without it a captured attestation replays forever. `verify` also enforces a debug-status policy (`requireDebugDisabled` refuses an `enabled` or absent `dbgstat`) and pins the `eat_profile`. RFC 9711 is a finalized standard; signing follows `b.cwt` / `b.cose` (ES256/384/512 + EdDSA interoperable today, ML-DSA-87 PQC-forward). **Added:** *`b.eat.sign(claims, opts)` / `b.eat.verify(eat, opts)`* — `sign` maps EAT claim names (`nonce`, `ueid`, `oemid`, `hwmodel`, `dbgstat`, `eat_profile`, `swname`/`swversion`, `measurements`, `submods`, …) to their RFC 9711 integer labels and accepts the `dbgstat` enum by name (`disabled-since-boot` → 2); standard CWT claims (`iss` / `exp` / …) pass through. `verify` returns `{ claims, raw, alg, protectedHeaders }` with the labels mapped back to friendly names and `dbgstat` decoded to its enum name. Attestation enforcement: `expectedNonce` requires a matching `eat_nonce` (refused `eat/nonce-mismatch`, missing `eat/nonce-missing` — `eat_nonce` may be a single byte string or an array for multiple verifiers), `requireDebugDisabled` refuses a non-disabled `dbgstat` (`eat/debug-not-disabled`), and `expectedProfile` pins `eat_profile`. The signature, algorithm allowlist, and `exp`/`nbf` checks delegate to `b.cwt` / `b.cose`. · *`b.cwt.sign` accepts a `Map`* — `b.cwt.sign` now takes either a plain object (string keys, standard claims mapped by name) or a `Map`, which preserves integer claim keys verbatim — profiles like `b.eat` resolve their claim names to integer labels and pass them through without the keys being stringified. The plain-object path is unchanged.
|
|
14
|
+
|
|
11
15
|
- v0.12.34 (2026-05-24) — **`b.cwt` — CBOR Web Token (RFC 8392) sign / verify over `b.cose`.** A CWT is the CBOR-native counterpart to JWT — a signed claims set for constrained / IoT, FIDO attestation, and verifiable-credential contexts. `b.cwt` composes the v0.12.33 `b.cose` (COSE_Sign1 signature + mandatory algorithm allowlist) and v0.12.32 `b.cbor` (deterministic claims encoding) and layers the standard-claim handling on top: `sign` takes a friendly claims object, maps the standard claims to their RFC 8392 §3.1.1 integer labels (iss=1, sub=2, aud=3, exp=4, nbf=5, iat=6, cti=7), and signs; `verify` checks the COSE signature, decodes the claims, and enforces the time + identity claims — a passed `exp` (with clock-skew tolerance), a future `nbf`, and an `iss` / `aud` mismatch against the expected values are each refused. Signing algorithms follow `b.cose`: classical ES256/384/512 + EdDSA (final COSE ids, interoperable today) and ML-DSA-87 (PQC-forward). RFC 8392 is a finalized standard, so CWTs produced here interoperate with other COSE/CWT implementations. **Added:** *`b.cwt.sign(claims, opts)` / `b.cwt.verify(cwt, opts)`* — `sign` maps standard claim names to integer labels and keeps custom claims verbatim; `exp` / `nbf` / `iat` must be non-negative integer NumericDates. `opts.tagged` wraps the COSE_Sign1 in the CWT CBOR tag 61 (RFC 8392 §6); `verify` accepts tagged or bare input. `verify` returns `{ claims, raw, alg, protectedHeaders }` — `claims` is the friendly object (labels mapped back to names), `raw` the integer-keyed Map. Standard-claim enforcement: `exp` past `now + clockSkewSec` (default 60s) is refused with `cwt/expired`, `nbf` beyond `now - skew` with `cwt/not-yet-valid`, and `expectedIssuer` / `expectedAudience` mismatches with `cwt/issuer-mismatch` / `cwt/audience-mismatch` (aud may be a single value or an array). `opts.now` overrides the clock for testing. The signature itself is verified by `b.cose.verify`, so a tampered token fails there.
|
|
12
16
|
|
|
13
17
|
- v0.12.33 (2026-05-24) — **`b.cose` — COSE_Sign1 sign / verify (RFC 9052) over the in-tree CBOR codec.** COSE is the signed-statement substrate under SCITT, CWT, and C2PA — the CBOR-native counterpart to JWS. `b.cose` ships COSE_Sign1 signing and verification composing the v0.12.32 `b.cbor` codec for the deterministic Sig_structure encoding. It signs with the classical COSE algorithms that interoperate today — ES256 / ES384 / ES512 (ECDSA) and EdDSA (Ed25519), all with final IANA algorithm ids (RFC 9053) — and with ML-DSA-87 (FIPS 204) for PQC-forward deployments. Verification accepts the same set, so the framework both produces COSE other implementations read today and consumes third-party COSE. There is no classical default: the caller names the algorithm and supplies the key. **Added:** *`b.cose.sign(payload, opts)` / `b.cose.verify(coseSign1, opts)`* — `sign` produces a tagged COSE_Sign1 with `alg` in the integrity-protected header; `verify` returns `{ payload, alg, protectedHeaders, unprotectedHeaders }`. The Sig_structure (`["Signature1", protected, external_aad, payload]`) is deterministically CBOR-encoded; ECDSA signatures use the IEEE-P1363 fixed-width encoding COSE mandates (RFC 9053 §2.1), not ASN.1 DER. `external_aad` is bound into the signature. v1 is single-signer with an attached payload; detached payload, COSE_Sign (multi-signer), COSE_Mac0, and COSE_Encrypt are deferred-with-condition (operator demand). **Security:** *Bounded, alg-allowlisted, crit-checked verification* — `verify` decodes the COSE_Sign1 bytes AND the protected-header bstr through the bounded `b.cbor.decode` (depth + size caps, indefinite-length / tag / duplicate-key refusal). `opts.algorithms` is a required allowlist (no defaults — name the accepted algorithms). A `crit` header (label 2) listing a header label the verifier does not understand is refused (RFC 9052 §3.1 crit-bypass defense), as is a `crit` label absent from the protected header. The COSE algorithm switch refuses any unrecognized id at the default branch. · *ML-DSA-87 COSE algorithm id is a non-final draft* — ML-DSA-87 uses COSE algorithm id `-50`, a requested (non-final) IANA assignment from draft-ietf-cose-dilithium — an ML-DSA-87 COSE_Sign1 is not yet broadly interoperable and the id may change; it is pinned deliberately with the re-open condition being IANA finalization. SLH-DSA-SHAKE-256f has no registered COSE algorithm id at all and cannot be represented in COSE. The COSE_Sign1 mechanism and the classical algorithms are stable; ML-DSA-87 is the forward-looking opt-in.
|
package/README.md
CHANGED
|
@@ -126,8 +126,9 @@ The framework bundles the surface a typical Node app reaches for. Every primitiv
|
|
|
126
126
|
- **JSON / SQL / schema** — `b.safeJson` (with `maxKeys` cap defending CVE-2026-21717 V8 HashDoS), `b.safeBuffer`, `b.safeSql`, `b.safeSchema`
|
|
127
127
|
- **URL + path** — `b.safeUrl` (IDN mixed-script / homograph refuse); `b.safeJsonPath` (refuses filter `?(...)`, deep-scan `$..`, script-shape `(@.x)` for safe Postgres JSONB ops)
|
|
128
128
|
- **Binary codec** — `b.cbor` bounded deterministic CBOR (RFC 8949 §4.2): depth/size caps, indefinite-length + reserved-info + tag + duplicate-key refusal, `requireDeterministic` canonical-form check; the in-tree substrate under COSE / CWT / SCITT / WebAuthn attestation
|
|
129
|
-
- **COSE signing** — `b.cose` COSE_Sign1 sign/verify (RFC 9052) over `b.cbor`: classical ES256/384/512 + EdDSA (final COSE ids, interoperable today) plus ML-DSA-87 (PQC-forward, draft id); bounded + alg-allowlisted + crit-bypass-checked verification; the signed-statement substrate under SCITT / CWT / C2PA
|
|
129
|
+
- **COSE signing + encryption** — `b.cose` COSE_Sign1 sign/verify + COSE_Encrypt0 (RFC 9052) over `b.cbor`: classical ES256/384/512 + EdDSA (final COSE ids, interoperable today) plus ML-DSA-87 (PQC-forward, draft id); bounded + alg-allowlisted + crit-bypass-checked verification; single-recipient AEAD (ChaCha20/Poly1305 default, AES-GCM opt-in) with Enc_structure-bound AAD; the signed-statement substrate under SCITT / CWT / C2PA
|
|
130
130
|
- **CBOR Web Token** — `b.cwt` CWT sign/verify (RFC 8392) over `b.cose`: standard-claim mapping (iss/sub/aud/exp/nbf/iat/cti) + `exp`/`nbf` clock-skew enforcement + `iss`/`aud` matching; the CBOR-native JWT for constrained / IoT / FIDO / verifiable-credential contexts
|
|
131
|
+
- **Entity Attestation Token** — `b.eat` EAT sign/verify (RFC 9711) over `b.cwt`: device + software attestation claims (ueid / oemid / hwmodel / measurements / submods) with verifier-nonce freshness binding, `dbgstat` debug-status policy, and `eat_profile` pinning
|
|
131
132
|
- **Document parsers** — `b.parsers` (XML / TOML / YAML / .env); `b.config` (schema-validated env)
|
|
132
133
|
- **File-type detection** — `b.fileType` magic-byte content classification with deny-on-upload categories (image / document / archive / executable / etc.)
|
|
133
134
|
### Content-safety gates
|
package/index.js
CHANGED
package/lib/cose.js
CHANGED
|
@@ -330,10 +330,192 @@ async function verify(coseSign1, opts) {
|
|
|
330
330
|
};
|
|
331
331
|
}
|
|
332
332
|
|
|
333
|
+
// ---- COSE_Encrypt0 (RFC 9052 §5.2) — single-recipient AEAD ----
|
|
334
|
+
|
|
335
|
+
var COSE_ENCRYPT0_TAG = 16; // allow:raw-byte-literal — RFC 9052 COSE_Encrypt0 CBOR tag
|
|
336
|
+
var HDR_IV = 5; // RFC 9052 §3.1 unprotected header label: IV
|
|
337
|
+
var AEAD_TAG_LEN = 16; // allow:raw-byte-literal — AEAD authentication tag length (bytes)
|
|
338
|
+
|
|
339
|
+
// AEAD algorithm: COSE id → node cipher + key / IV sizes. ChaCha20/
|
|
340
|
+
// Poly1305 (24) is the default; AES-GCM is opt-in (project hard-rule
|
|
341
|
+
// #2 forbids AES-GCM as a default).
|
|
342
|
+
var AEAD_NAME_TO_ID = { "ChaCha20-Poly1305": 24, "A256GCM": 3, "A128GCM": 1 }; // allow:raw-byte-literal — COSE AEAD algorithm identifiers (RFC 9053), not sizes
|
|
343
|
+
var AEAD_ID_TO_NAME = {};
|
|
344
|
+
Object.keys(AEAD_NAME_TO_ID).forEach(function (k) { AEAD_ID_TO_NAME[AEAD_NAME_TO_ID[k]] = k; });
|
|
345
|
+
|
|
346
|
+
function _aeadParams(algId) {
|
|
347
|
+
switch (algId) {
|
|
348
|
+
case 24: return { cipher: "chacha20-poly1305", keyLen: 32, ivLen: 12 }; // allow:raw-byte-literal — ChaCha20/Poly1305 key+IV sizes
|
|
349
|
+
case 3: return { cipher: "aes-256-gcm", keyLen: 32, ivLen: 12 }; // allow:raw-byte-literal — AES-256-GCM key+IV sizes
|
|
350
|
+
case 1: return { cipher: "aes-128-gcm", keyLen: 16, ivLen: 12 }; // allow:raw-byte-literal — AES-128-GCM key+IV sizes
|
|
351
|
+
default:
|
|
352
|
+
throw new CoseError("cose/unknown-alg", "cose: unrecognized AEAD COSE alg id " + algId);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Enc_structure (§5.3) = [ "Encrypt0", body_protected (bstr), external_aad (bstr) ]
|
|
357
|
+
// — deterministically CBOR-encoded, used as the AEAD associated data.
|
|
358
|
+
function _encStructure(protectedBstr, externalAad) {
|
|
359
|
+
return cbor.encode(["Encrypt0", protectedBstr, externalAad]);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* @primitive b.cose.encrypt0
|
|
364
|
+
* @signature b.cose.encrypt0(plaintext, opts)
|
|
365
|
+
* @since 0.12.36
|
|
366
|
+
* @status stable
|
|
367
|
+
* @related b.cose.decrypt0, b.cose.sign
|
|
368
|
+
*
|
|
369
|
+
* Encrypt bytes into a tagged COSE_Encrypt0 (RFC 9052 §5.2), a
|
|
370
|
+
* single-recipient AEAD container where the recipient already holds
|
|
371
|
+
* the symmetric key (direct mode). Default algorithm is
|
|
372
|
+
* <code>ChaCha20-Poly1305</code>; <code>A256GCM</code> / <code>A128GCM</code>
|
|
373
|
+
* are opt-in. The Enc_structure is bound as the AEAD associated data,
|
|
374
|
+
* and the authentication tag is appended to the ciphertext per COSE.
|
|
375
|
+
*
|
|
376
|
+
* @opts
|
|
377
|
+
* {
|
|
378
|
+
* alg: string, // "ChaCha20-Poly1305" (default) | "A256GCM" | "A128GCM"
|
|
379
|
+
* key: Buffer, // symmetric key (32 bytes for ChaCha/A256GCM, 16 for A128GCM)
|
|
380
|
+
* iv?: Buffer, // 12-byte IV (random if omitted)
|
|
381
|
+
* externalAad?: Buffer, // bound into the AEAD tag
|
|
382
|
+
* unprotectedHeaders?: object,
|
|
383
|
+
* }
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* var enc = b.cose.encrypt0(Buffer.from("secret"), { alg: "ChaCha20-Poly1305", key: k });
|
|
387
|
+
*/
|
|
388
|
+
function encrypt0(plaintext, opts) {
|
|
389
|
+
validateOpts.requireObject(opts, "cose.encrypt0", CoseError);
|
|
390
|
+
validateOpts(opts, ["alg", "key", "iv", "externalAad", "unprotectedHeaders"], "cose.encrypt0");
|
|
391
|
+
var alg = opts.alg || "ChaCha20-Poly1305";
|
|
392
|
+
if (!(alg in AEAD_NAME_TO_ID)) {
|
|
393
|
+
throw new CoseError("cose/unknown-alg", "cose.encrypt0: alg must be one of " + Object.keys(AEAD_NAME_TO_ID).join(" / "));
|
|
394
|
+
}
|
|
395
|
+
var algId = AEAD_NAME_TO_ID[alg];
|
|
396
|
+
var p = _aeadParams(algId);
|
|
397
|
+
var key = _bstr(opts.key);
|
|
398
|
+
if (key.length !== p.keyLen) throw new CoseError("cose/bad-key", "cose.encrypt0: " + alg + " requires a " + p.keyLen + "-byte key");
|
|
399
|
+
var iv = opts.iv != null ? _bstr(opts.iv) : nodeCrypto.randomBytes(p.ivLen);
|
|
400
|
+
if (iv.length !== p.ivLen) throw new CoseError("cose/bad-iv", "cose.encrypt0: " + alg + " requires a " + p.ivLen + "-byte IV");
|
|
401
|
+
|
|
402
|
+
var protMap = new Map(); protMap.set(HDR_ALG, algId);
|
|
403
|
+
var protectedBstr = cbor.encode(protMap);
|
|
404
|
+
var aad = _encStructure(protectedBstr, opts.externalAad == null ? Buffer.alloc(0) : _bstr(opts.externalAad));
|
|
405
|
+
|
|
406
|
+
var cipher = nodeCrypto.createCipheriv(p.cipher, key, iv, { authTagLength: AEAD_TAG_LEN });
|
|
407
|
+
cipher.setAAD(aad);
|
|
408
|
+
var ct = Buffer.concat([cipher.update(_bstr(plaintext)), cipher.final()]);
|
|
409
|
+
var ciphertext = Buffer.concat([ct, cipher.getAuthTag()]); // COSE appends the auth tag to the ciphertext
|
|
410
|
+
|
|
411
|
+
var unprot = new Map(); unprot.set(HDR_IV, iv);
|
|
412
|
+
if (opts.unprotectedHeaders && typeof opts.unprotectedHeaders === "object") {
|
|
413
|
+
var uk = Object.keys(opts.unprotectedHeaders);
|
|
414
|
+
for (var i = 0; i < uk.length; i++) {
|
|
415
|
+
var label = Number(uk[i]);
|
|
416
|
+
// The IV (label 5) is managed via opts.iv and must match the IV
|
|
417
|
+
// the AEAD used — refuse an override that would emit a token whose
|
|
418
|
+
// stored IV disagrees with the one it was encrypted under.
|
|
419
|
+
if (label === HDR_IV) {
|
|
420
|
+
throw new CoseError("cose/reserved-header",
|
|
421
|
+
"cose.encrypt0: unprotectedHeaders must not set label 5 (IV) — pass opts.iv instead");
|
|
422
|
+
}
|
|
423
|
+
unprot.set(label, opts.unprotectedHeaders[uk[i]]);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return cbor.encode(new cbor.Tag(COSE_ENCRYPT0_TAG, [protectedBstr, unprot, ciphertext]));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* @primitive b.cose.decrypt0
|
|
431
|
+
* @signature b.cose.decrypt0(coseEncrypt0, opts)
|
|
432
|
+
* @since 0.12.36
|
|
433
|
+
* @status stable
|
|
434
|
+
* @related b.cose.encrypt0
|
|
435
|
+
*
|
|
436
|
+
* Decrypt a COSE_Encrypt0 and return the plaintext. The algorithm is
|
|
437
|
+
* read from the protected header and must be in
|
|
438
|
+
* <code>opts.algorithms</code>; the Enc_structure is reconstructed as
|
|
439
|
+
* the AEAD associated data and authentication failure (wrong key /
|
|
440
|
+
* tampered ciphertext or AAD) is refused.
|
|
441
|
+
*
|
|
442
|
+
* @opts
|
|
443
|
+
* {
|
|
444
|
+
* key: Buffer, // symmetric key
|
|
445
|
+
* algorithms: string[], // required — accepted AEAD algs (allowlist)
|
|
446
|
+
* externalAad?: Buffer, // must match what was encrypted
|
|
447
|
+
* maxBytes?: number,
|
|
448
|
+
* maxDepth?: number,
|
|
449
|
+
* }
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* var pt = b.cose.decrypt0(enc, { key: k, algorithms: ["ChaCha20-Poly1305"] }).plaintext;
|
|
453
|
+
*/
|
|
454
|
+
function decrypt0(coseEncrypt0, opts) {
|
|
455
|
+
validateOpts.requireObject(opts, "cose.decrypt0", CoseError);
|
|
456
|
+
validateOpts(opts, ["key", "algorithms", "externalAad", "maxBytes", "maxDepth"], "cose.decrypt0");
|
|
457
|
+
if (!Array.isArray(opts.algorithms) || opts.algorithms.length === 0) {
|
|
458
|
+
throw new CoseError("cose/algorithms-required", "cose.decrypt0: opts.algorithms is required (no defaults — name the accepted algorithms)");
|
|
459
|
+
}
|
|
460
|
+
var decoded = cbor.decode(_bstr(coseEncrypt0), { allowedTags: [COSE_ENCRYPT0_TAG], maxBytes: opts.maxBytes, maxDepth: opts.maxDepth });
|
|
461
|
+
var arr = (decoded instanceof cbor.Tag && decoded.tag === COSE_ENCRYPT0_TAG) ? decoded.value : decoded;
|
|
462
|
+
if (!Array.isArray(arr) || arr.length !== 3) {
|
|
463
|
+
throw new CoseError("cose/malformed", "cose.decrypt0: not a COSE_Encrypt0 (expected a 3-element array)");
|
|
464
|
+
}
|
|
465
|
+
var protectedBstr = arr[0], unprotected = arr[1], ciphertext = arr[2];
|
|
466
|
+
if (!Buffer.isBuffer(protectedBstr) || !Buffer.isBuffer(ciphertext)) {
|
|
467
|
+
throw new CoseError("cose/malformed", "cose.decrypt0: protected header and ciphertext must be byte strings");
|
|
468
|
+
}
|
|
469
|
+
if (!(unprotected instanceof Map)) {
|
|
470
|
+
throw new CoseError("cose/malformed", "cose.decrypt0: unprotected header must be a CBOR map");
|
|
471
|
+
}
|
|
472
|
+
var protMap = protectedBstr.length === 0 ? new Map()
|
|
473
|
+
: cbor.decode(protectedBstr, { maxBytes: opts.maxBytes, maxDepth: opts.maxDepth });
|
|
474
|
+
if (!(protMap instanceof Map)) {
|
|
475
|
+
throw new CoseError("cose/malformed", "cose.decrypt0: protected header is not a CBOR map");
|
|
476
|
+
}
|
|
477
|
+
var algId = protMap.get(HDR_ALG);
|
|
478
|
+
var algName = AEAD_ID_TO_NAME[algId];
|
|
479
|
+
if (algName === undefined) {
|
|
480
|
+
throw new CoseError("cose/unknown-alg", "cose.decrypt0: unrecognized AEAD alg id " + algId);
|
|
481
|
+
}
|
|
482
|
+
if (opts.algorithms.indexOf(algName) === -1) {
|
|
483
|
+
throw new CoseError("cose/alg-not-allowed", "cose.decrypt0: alg '" + algName + "' is not in the allowlist");
|
|
484
|
+
}
|
|
485
|
+
var p = _aeadParams(algId);
|
|
486
|
+
var key = _bstr(opts.key);
|
|
487
|
+
if (key.length !== p.keyLen) throw new CoseError("cose/bad-key", "cose.decrypt0: " + algName + " requires a " + p.keyLen + "-byte key");
|
|
488
|
+
var iv = unprotected.get(HDR_IV);
|
|
489
|
+
if (!Buffer.isBuffer(iv) || iv.length !== p.ivLen) {
|
|
490
|
+
throw new CoseError("cose/bad-iv", "cose.decrypt0: missing or wrong-length IV (unprotected label 5)");
|
|
491
|
+
}
|
|
492
|
+
if (ciphertext.length < AEAD_TAG_LEN) {
|
|
493
|
+
throw new CoseError("cose/malformed", "cose.decrypt0: ciphertext shorter than the AEAD tag");
|
|
494
|
+
}
|
|
495
|
+
var tag = ciphertext.subarray(ciphertext.length - AEAD_TAG_LEN);
|
|
496
|
+
var ct = ciphertext.subarray(0, ciphertext.length - AEAD_TAG_LEN);
|
|
497
|
+
var aad = _encStructure(protectedBstr, opts.externalAad == null ? Buffer.alloc(0) : _bstr(opts.externalAad));
|
|
498
|
+
|
|
499
|
+
var decipher = nodeCrypto.createDecipheriv(p.cipher, key, iv, { authTagLength: AEAD_TAG_LEN });
|
|
500
|
+
decipher.setAAD(aad);
|
|
501
|
+
decipher.setAuthTag(tag);
|
|
502
|
+
var pt;
|
|
503
|
+
try {
|
|
504
|
+
pt = Buffer.concat([decipher.update(ct), decipher.final()]);
|
|
505
|
+
} catch (_e) {
|
|
506
|
+
throw new CoseError("cose/decrypt-failed", "cose.decrypt0: AEAD authentication failed (wrong key, tampered ciphertext, or AAD mismatch)");
|
|
507
|
+
}
|
|
508
|
+
return { plaintext: pt, alg: algName, protectedHeaders: protMap, unprotectedHeaders: unprotected };
|
|
509
|
+
}
|
|
510
|
+
|
|
333
511
|
module.exports = {
|
|
334
512
|
sign: sign,
|
|
335
513
|
verify: verify,
|
|
514
|
+
encrypt0: encrypt0,
|
|
515
|
+
decrypt0: decrypt0,
|
|
336
516
|
ALGORITHMS: ALG_NAME_TO_ID,
|
|
517
|
+
AEAD_ALGORITHMS: AEAD_NAME_TO_ID,
|
|
337
518
|
COSE_SIGN1_TAG: COSE_SIGN1_TAG,
|
|
519
|
+
COSE_ENCRYPT0_TAG: COSE_ENCRYPT0_TAG,
|
|
338
520
|
CoseError: CoseError,
|
|
339
521
|
};
|
package/lib/cwt.js
CHANGED
|
@@ -101,23 +101,28 @@ function _readTagHead(buf) {
|
|
|
101
101
|
*/
|
|
102
102
|
async function sign(claims, opts) {
|
|
103
103
|
if (!claims || typeof claims !== "object" || Array.isArray(claims)) {
|
|
104
|
-
throw new CwtError("cwt/bad-claims", "cwt.sign: claims must be a plain object");
|
|
104
|
+
throw new CwtError("cwt/bad-claims", "cwt.sign: claims must be a plain object or a Map");
|
|
105
105
|
}
|
|
106
106
|
validateOpts.requireObject(opts, "cwt.sign", CwtError);
|
|
107
107
|
validateOpts(opts, ["alg", "privateKey", "kid", "tagged", "externalAad"], "cwt.sign");
|
|
108
108
|
|
|
109
|
+
// Accept a plain object (string keys) OR a Map. A Map preserves
|
|
110
|
+
// INTEGER claim keys verbatim — profiles like b.eat pass their
|
|
111
|
+
// already-resolved integer labels through and must not have them
|
|
112
|
+
// stringified.
|
|
113
|
+
var source = (claims instanceof Map)
|
|
114
|
+
? claims
|
|
115
|
+
: new Map(Object.keys(claims).map(function (k) { return [k, claims[k]]; }));
|
|
116
|
+
|
|
109
117
|
var map = new Map();
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
var name = keys[i];
|
|
113
|
-
var value = claims[name];
|
|
114
|
-
if (NUMERIC_DATE_CLAIMS[name] &&
|
|
118
|
+
source.forEach(function (value, name) {
|
|
119
|
+
if (typeof name === "string" && NUMERIC_DATE_CLAIMS[name] &&
|
|
115
120
|
(typeof value !== "number" || !Number.isInteger(value) || value < 0)) {
|
|
116
121
|
throw new CwtError("cwt/bad-numeric-date",
|
|
117
122
|
"cwt.sign: claim '" + name + "' must be a non-negative integer NumericDate (seconds)");
|
|
118
123
|
}
|
|
119
|
-
map.set(Object.prototype.hasOwnProperty.call(STD, name) ? STD[name] : name, value);
|
|
120
|
-
}
|
|
124
|
+
map.set((typeof name === "string" && Object.prototype.hasOwnProperty.call(STD, name)) ? STD[name] : name, value);
|
|
125
|
+
});
|
|
121
126
|
|
|
122
127
|
var claimsCbor = cbor.encode(map);
|
|
123
128
|
var coseSign1 = await cose.sign(claimsCbor, {
|
package/lib/eat.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module b.eat
|
|
4
|
+
* @nav Crypto
|
|
5
|
+
* @title Entity Attestation Token (EAT)
|
|
6
|
+
*
|
|
7
|
+
* @intro
|
|
8
|
+
* RFC 9711 Entity Attestation Token — a CWT (or JWT) profile that
|
|
9
|
+
* carries attestation claims describing the state of a device or
|
|
10
|
+
* software entity: a freshness nonce, a Universal Entity ID, OEM /
|
|
11
|
+
* hardware identifiers, debug status, software measurements, and
|
|
12
|
+
* nested submodule attestations. EAT is the token a Relying Party
|
|
13
|
+
* asks a device to produce to prove what it is and what state it is
|
|
14
|
+
* in. This module is the EAT profile over <code>b.cwt</code> — it
|
|
15
|
+
* maps the RFC 9711 claim names to their CWT claim-key integer
|
|
16
|
+
* labels and adds the attestation-specific verification.
|
|
17
|
+
*
|
|
18
|
+
* <code>b.eat.sign(claims, opts)</code> takes a friendly claims
|
|
19
|
+
* object (<code>nonce</code>, <code>ueid</code>, <code>oemid</code>,
|
|
20
|
+
* <code>dbgstat</code>, <code>eat_profile</code>,
|
|
21
|
+
* <code>measurements</code>, <code>submods</code>, … plus the
|
|
22
|
+
* standard CWT claims) and signs it as a CWT.
|
|
23
|
+
*
|
|
24
|
+
* <code>b.eat.verify(eat, opts)</code> verifies the CWT (signature +
|
|
25
|
+
* alg allowlist + time claims, via <code>b.cwt</code>) and then
|
|
26
|
+
* enforces the attestation contract:
|
|
27
|
+
*
|
|
28
|
+
* - <strong>Nonce binding</strong> — when the Relying Party supplied
|
|
29
|
+
* a fresh <code>expectedNonce</code>, the token's
|
|
30
|
+
* <code>eat_nonce</code> (claim 10) MUST match it (constant-time
|
|
31
|
+
* compare). This is the freshness / anti-replay defense: without
|
|
32
|
+
* it a captured attestation can be replayed indefinitely.
|
|
33
|
+
* - <strong>Debug status</strong> — <code>requireDebugDisabled</code>
|
|
34
|
+
* refuses a token whose <code>dbgstat</code> is
|
|
35
|
+
* <code>enabled</code> (0) or absent; only the disabled states
|
|
36
|
+
* (1–4) pass.
|
|
37
|
+
* - <strong>Profile</strong> — <code>expectedProfile</code> pins the
|
|
38
|
+
* <code>eat_profile</code> claim.
|
|
39
|
+
*
|
|
40
|
+
* Signing algorithms follow <code>b.cwt</code> / <code>b.cose</code>:
|
|
41
|
+
* ES256/384/512 + EdDSA (interoperable today) and ML-DSA-87.
|
|
42
|
+
*
|
|
43
|
+
* @card
|
|
44
|
+
* RFC 9711 Entity Attestation Token over b.cwt — sign / verify
|
|
45
|
+
* device + software attestation claims with verifier-nonce binding,
|
|
46
|
+
* debug-status policy, and profile pinning.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
var cwt = require("./cwt");
|
|
50
|
+
var bCrypto = require("./crypto");
|
|
51
|
+
var validateOpts = require("./validate-opts");
|
|
52
|
+
var { defineClass } = require("./framework-error");
|
|
53
|
+
|
|
54
|
+
var EatError = defineClass("EatError", { alwaysPermanent: true });
|
|
55
|
+
|
|
56
|
+
// RFC 9711 / IANA CWT Claims registry claim keys.
|
|
57
|
+
var EAT = { // allow:raw-byte-literal — RFC 9711 / IANA CWT claim-key labels, not byte sizes
|
|
58
|
+
nonce: 10, ueid: 256, sueids: 257, oemid: 258, hwmodel: 259, hwversion: 260, // allow:raw-byte-literal — CWT claim keys
|
|
59
|
+
uptime: 261, oemboot: 262, dbgstat: 263, location: 264, eat_profile: 265, // allow:raw-byte-literal — CWT claim keys
|
|
60
|
+
submods: 266, swname: 270, swversion: 271, manifests: 272, measurements: 273, // allow:raw-byte-literal — CWT claim keys
|
|
61
|
+
};
|
|
62
|
+
var EAT_BY_LABEL = {};
|
|
63
|
+
Object.keys(EAT).forEach(function (k) { EAT_BY_LABEL[EAT[k]] = k; });
|
|
64
|
+
|
|
65
|
+
// RFC 9711 §4.3.1 debug-status enumeration.
|
|
66
|
+
var DBGSTAT = {
|
|
67
|
+
"enabled": 0, "disabled": 1, "disabled-since-boot": 2,
|
|
68
|
+
"disabled-permanently": 3, "disabled-fully-and-permanently": 4,
|
|
69
|
+
};
|
|
70
|
+
var DBGSTAT_BY_VALUE = {};
|
|
71
|
+
Object.keys(DBGSTAT).forEach(function (k) { DBGSTAT_BY_VALUE[DBGSTAT[k]] = k; });
|
|
72
|
+
|
|
73
|
+
// Standard CWT claim labels → names (RFC 8392 §3.1.1) so EAT's
|
|
74
|
+
// friendly output names the standard claims alongside the EAT ones.
|
|
75
|
+
var STD_NAME = { 1: "iss", 2: "sub", 3: "aud", 4: "exp", 5: "nbf", 6: "iat", 7: "cti" };
|
|
76
|
+
|
|
77
|
+
function _toBuf(x) {
|
|
78
|
+
if (Buffer.isBuffer(x)) return x;
|
|
79
|
+
if (x instanceof Uint8Array) return Buffer.from(x);
|
|
80
|
+
if (typeof x === "string") return Buffer.from(x, "utf8");
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function _nonceMatches(claimValue, expected) {
|
|
85
|
+
var exp = _toBuf(expected);
|
|
86
|
+
if (!exp) return false;
|
|
87
|
+
// eat_nonce may be a single byte string or an array of them (one per
|
|
88
|
+
// verifier). Constant-time compare against each candidate.
|
|
89
|
+
var candidates = Array.isArray(claimValue) ? claimValue : [claimValue];
|
|
90
|
+
for (var i = 0; i < candidates.length; i++) {
|
|
91
|
+
var c = _toBuf(candidates[i]);
|
|
92
|
+
if (c && c.length === exp.length && bCrypto.timingSafeEqual(c, exp)) return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @primitive b.eat.sign
|
|
99
|
+
* @signature b.eat.sign(claims, opts)
|
|
100
|
+
* @since 0.12.35
|
|
101
|
+
* @status stable
|
|
102
|
+
* @related b.eat.verify, b.cwt.sign
|
|
103
|
+
*
|
|
104
|
+
* Sign EAT attestation claims into a CWT. EAT claim names map to their
|
|
105
|
+
* RFC 9711 integer labels; <code>dbgstat</code> accepts the enum name
|
|
106
|
+
* (<code>"disabled-since-boot"</code>) or its integer. Standard CWT
|
|
107
|
+
* claims (<code>iss</code> / <code>exp</code> / …) pass through to
|
|
108
|
+
* <code>b.cwt.sign</code>.
|
|
109
|
+
*
|
|
110
|
+
* @opts
|
|
111
|
+
* {
|
|
112
|
+
* alg: string, // COSE signing alg (ES256 / EdDSA / ML-DSA-87 / …)
|
|
113
|
+
* privateKey: object, // signing key
|
|
114
|
+
* kid?: string,
|
|
115
|
+
* tagged?: boolean, // CWT tag 61
|
|
116
|
+
* }
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* var eat = await b.eat.sign(
|
|
120
|
+
* { nonce: rpNonce, ueid: deviceUeid, oemid: oem, dbgstat: "disabled-permanently",
|
|
121
|
+
* eat_profile: "https://example.com/eat/profile-1", iat: Math.floor(Date.now()/1000) },
|
|
122
|
+
* { alg: "ES256", privateKey: deviceKey });
|
|
123
|
+
*/
|
|
124
|
+
async function sign(claims, opts) {
|
|
125
|
+
if (!claims || typeof claims !== "object" || Array.isArray(claims)) {
|
|
126
|
+
throw new EatError("eat/bad-claims", "eat.sign: claims must be a plain object");
|
|
127
|
+
}
|
|
128
|
+
validateOpts.requireObject(opts, "eat.sign", EatError);
|
|
129
|
+
// Translate EAT claim names to their integer labels into a Map (so
|
|
130
|
+
// the integer keys survive — a plain object would stringify them).
|
|
131
|
+
// Standard CWT names (iss/sub/aud/exp/nbf/iat/cti) + custom keys are
|
|
132
|
+
// left for b.cwt.sign to handle.
|
|
133
|
+
var mapped = new Map();
|
|
134
|
+
var keys = Object.keys(claims);
|
|
135
|
+
for (var i = 0; i < keys.length; i++) {
|
|
136
|
+
var name = keys[i];
|
|
137
|
+
var value = claims[name];
|
|
138
|
+
if (name === "dbgstat" && typeof value === "string") {
|
|
139
|
+
if (!Object.prototype.hasOwnProperty.call(DBGSTAT, value)) {
|
|
140
|
+
throw new EatError("eat/bad-dbgstat",
|
|
141
|
+
"eat.sign: dbgstat must be one of " + Object.keys(DBGSTAT).join(" / ") + " (or an integer 0-4)");
|
|
142
|
+
}
|
|
143
|
+
value = DBGSTAT[value];
|
|
144
|
+
}
|
|
145
|
+
mapped.set(Object.prototype.hasOwnProperty.call(EAT, name) ? EAT[name] : name, value);
|
|
146
|
+
}
|
|
147
|
+
return cwt.sign(mapped, opts);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @primitive b.eat.verify
|
|
152
|
+
* @signature b.eat.verify(eat, opts)
|
|
153
|
+
* @since 0.12.35
|
|
154
|
+
* @status stable
|
|
155
|
+
* @related b.eat.sign, b.cwt.verify
|
|
156
|
+
*
|
|
157
|
+
* Verify an EAT and return its attestation claims. Delegates the CWT
|
|
158
|
+
* signature + algorithm-allowlist + time-claim checks to
|
|
159
|
+
* <code>b.cwt.verify</code>, then enforces the attestation contract:
|
|
160
|
+
* the <code>eat_nonce</code> must match <code>expectedNonce</code>
|
|
161
|
+
* (when supplied — the freshness/anti-replay binding),
|
|
162
|
+
* <code>requireDebugDisabled</code> refuses a non-disabled
|
|
163
|
+
* <code>dbgstat</code>, and <code>expectedProfile</code> pins
|
|
164
|
+
* <code>eat_profile</code>.
|
|
165
|
+
*
|
|
166
|
+
* @opts
|
|
167
|
+
* {
|
|
168
|
+
* algorithms: string[], // required — accepted COSE algs
|
|
169
|
+
* publicKey?: object,
|
|
170
|
+
* keyResolver?: function,
|
|
171
|
+
* expectedNonce?: Buffer, // require eat_nonce to match (freshness)
|
|
172
|
+
* requireDebugDisabled?: boolean, // refuse dbgstat enabled / absent
|
|
173
|
+
* expectedProfile?: string, // pin eat_profile
|
|
174
|
+
* expectedIssuer?: string, // forwarded to b.cwt.verify
|
|
175
|
+
* expectedAudience?: string,
|
|
176
|
+
* clockSkewSec?: number,
|
|
177
|
+
* now?: number,
|
|
178
|
+
* externalAad?: Buffer,
|
|
179
|
+
* }
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* var att = await b.eat.verify(eat, { algorithms: ["ES256"], publicKey: devicePub, expectedNonce: rpNonce, requireDebugDisabled: true });
|
|
183
|
+
* // → { claims: { nonce, ueid, dbgstat: "disabled-permanently", ... }, raw: Map, alg }
|
|
184
|
+
*/
|
|
185
|
+
async function verify(eat, opts) {
|
|
186
|
+
validateOpts.requireObject(opts, "eat.verify", EatError);
|
|
187
|
+
var out = await cwt.verify(eat, {
|
|
188
|
+
algorithms: opts.algorithms, publicKey: opts.publicKey, keyResolver: opts.keyResolver,
|
|
189
|
+
expectedIssuer: opts.expectedIssuer, expectedAudience: opts.expectedAudience,
|
|
190
|
+
clockSkewSec: opts.clockSkewSec, now: opts.now, externalAad: opts.externalAad,
|
|
191
|
+
});
|
|
192
|
+
var raw = out.raw;
|
|
193
|
+
|
|
194
|
+
// Nonce binding — the freshness / anti-replay defense. When the RP
|
|
195
|
+
// supplied a nonce, the token MUST carry a matching eat_nonce.
|
|
196
|
+
if (opts.expectedNonce != null) {
|
|
197
|
+
if (!raw.has(EAT.nonce)) {
|
|
198
|
+
throw new EatError("eat/nonce-missing", "eat.verify: expectedNonce supplied but token has no eat_nonce claim");
|
|
199
|
+
}
|
|
200
|
+
if (!_nonceMatches(raw.get(EAT.nonce), opts.expectedNonce)) {
|
|
201
|
+
throw new EatError("eat/nonce-mismatch", "eat.verify: eat_nonce does not match expectedNonce (stale / replayed attestation)");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Debug status — refuse a token that can't prove debug is disabled.
|
|
206
|
+
if (opts.requireDebugDisabled === true) {
|
|
207
|
+
var ds = raw.get(EAT.dbgstat);
|
|
208
|
+
if (typeof ds !== "number" || ds < DBGSTAT.disabled) {
|
|
209
|
+
throw new EatError("eat/debug-not-disabled",
|
|
210
|
+
"eat.verify: requireDebugDisabled — dbgstat is " +
|
|
211
|
+
(ds === undefined ? "absent" : (DBGSTAT_BY_VALUE[ds] || ds)) + ", not a disabled state");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (opts.expectedProfile != null && raw.get(EAT.eat_profile) !== opts.expectedProfile) {
|
|
216
|
+
throw new EatError("eat/profile-mismatch", "eat.verify: eat_profile does not match expectedProfile");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Build friendly claims: EAT + standard labels → names; decode the
|
|
220
|
+
// dbgstat enum to its name.
|
|
221
|
+
var claims = {};
|
|
222
|
+
raw.forEach(function (v, k) {
|
|
223
|
+
var name = Object.prototype.hasOwnProperty.call(EAT_BY_LABEL, k) ? EAT_BY_LABEL[k]
|
|
224
|
+
: (Object.prototype.hasOwnProperty.call(STD_NAME, k) ? STD_NAME[k] : k);
|
|
225
|
+
if (name === "dbgstat" && typeof v === "number" && Object.prototype.hasOwnProperty.call(DBGSTAT_BY_VALUE, v)) {
|
|
226
|
+
v = DBGSTAT_BY_VALUE[v];
|
|
227
|
+
}
|
|
228
|
+
claims[name] = v;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return { claims: claims, raw: raw, alg: out.alg, protectedHeaders: out.protectedHeaders };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
module.exports = {
|
|
235
|
+
sign: sign,
|
|
236
|
+
verify: verify,
|
|
237
|
+
CLAIM_LABELS: EAT,
|
|
238
|
+
DBGSTAT: DBGSTAT,
|
|
239
|
+
EatError: EatError,
|
|
240
|
+
};
|
package/package.json
CHANGED
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:
|
|
5
|
+
"serialNumber": "urn:uuid:0d92b39a-5bed-4091-9927-a887660568ee",
|
|
6
6
|
"version": 1,
|
|
7
7
|
"metadata": {
|
|
8
|
-
"timestamp": "2026-05-
|
|
8
|
+
"timestamp": "2026-05-25T00:19:35.328Z",
|
|
9
9
|
"lifecycles": [
|
|
10
10
|
{
|
|
11
11
|
"phase": "build"
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
],
|
|
21
21
|
"component": {
|
|
22
|
-
"bom-ref": "@blamejs/core@0.12.
|
|
22
|
+
"bom-ref": "@blamejs/core@0.12.36",
|
|
23
23
|
"type": "application",
|
|
24
24
|
"name": "blamejs",
|
|
25
|
-
"version": "0.12.
|
|
25
|
+
"version": "0.12.36",
|
|
26
26
|
"scope": "required",
|
|
27
27
|
"author": "blamejs contributors",
|
|
28
28
|
"description": "The Node framework that owns its stack.",
|
|
29
|
-
"purl": "pkg:npm/%40blamejs/core@0.12.
|
|
29
|
+
"purl": "pkg:npm/%40blamejs/core@0.12.36",
|
|
30
30
|
"properties": [],
|
|
31
31
|
"externalReferences": [
|
|
32
32
|
{
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"components": [],
|
|
55
55
|
"dependencies": [
|
|
56
56
|
{
|
|
57
|
-
"ref": "@blamejs/core@0.12.
|
|
57
|
+
"ref": "@blamejs/core@0.12.36",
|
|
58
58
|
"dependsOn": []
|
|
59
59
|
}
|
|
60
60
|
]
|