@blamejs/core 0.12.48 → 0.12.50
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 +1 -1
- package/lib/network-dnssec.js +567 -12
- 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.50 (2026-05-25) — **`b.network.dns.dnssec.verifyChain` — validate a DNSSEC delegation chain to a pinned root anchor.** Completes local DNSSEC verification: validate a full delegation chain from the root down to a zone against a pinned trust anchor (RFC 4035 §5), instead of trusting any single resolver. For each link, the zone's DNSKEY RRset must be self-signed by one of its keys, and that key must be vouched for either by a pinned anchor (at the root) or by a DS record served + signed by the already-trusted parent — so trust flows root → TLD → zone with no gap. The IANA root KSKs (KSK-2017 tag 20326, KSK-2024 tag 38696) ship as the default anchors; override with opts.trustAnchors for a private root. verifyChain returns the leaf zone's trusted DNSKEY set, which you then hand to verifyRrset / verifyDenial for the actual answer. Composes verifyRrset + verifyDs + the key tag; verified end-to-end against a live root→org chain. **Added:** *`b.network.dns.dnssec.verifyChain(opts)`* — Walks an ordered, root-first list of `links` ({ zone, dnskeys, dnskeyRrsig, dsRdatas?, dsRrsig? }). At each link it verifies the DNSKEY RRset's self-signature (composing `verifyRrset`), then establishes trust in the signing key: at the root by matching a pinned anchor's DS digest (`verifyDs`), at every delegation by verifying the parent-served DS RRset's signature with the already-trusted parent key and confirming the signing KSK matches one of those DS records. Returns `{ ok, zone, keys, path }` with the leaf zone's trusted DNSKEY set. Refuses a root key that matches no anchor (`dnssec/chain-anchor-mismatch`), a KSK that matches no parent DS (`dnssec/chain-ds-mismatch`), and a missing parent key (`dnssec/chain-no-parent-key`). The default `DEFAULT_ROOT_ANCHORS` are the published IANA root KSK DS records; `opts.trustAnchors` overrides them for a private or test root.
|
|
12
|
+
|
|
13
|
+
- v0.12.49 (2026-05-25) — **`b.network.dns.dnssec.verifyDenial` — NSEC / NSEC3 denial-of-existence.** Prove a DNS name does not exist, or has no records of a given type, from the signed NSEC (RFC 4034 §4) or NSEC3 (RFC 5155) records a server returns. This is the other half of local DNSSEC verification: verifyRrset proves a positive answer, verifyDenial proves a negative — so a resolver client can confirm an NXDOMAIN / NODATA itself instead of trusting the upstream resolver. NSEC3 proofs run the closest-encloser / next-closer / covering-range logic over iterated-SHA-1 hashes, with the iteration count capped (default 500) to bound the work an attacker can force, and an Opt-Out NXDOMAIN refused unless explicitly accepted (opt-out only proves 'no signed records', not non-existence). The companion b.network.dns.dnssec.nsec3Hash computes the RFC 5155 §5 hash directly. NSEC verifyRrset support is also enabled: per RFC 6840 §5.1 the NSEC Next Domain Name is not downcased, so its RDATA is verbatim-canonical. **Added:** *`b.network.dns.dnssec.verifyDenial(opts)`* — Proves NXDOMAIN or NODATA from already-verified NSEC / NSEC3 records (supply one of `opts.nsec3` or `opts.nsec`). Like `verifyDs`, it checks the denial RELATION — closest-encloser matching, covering ranges, and type-bitmap absence — not the record signatures, which the caller verifies with `verifyRrset` first. NSEC3 supports name-error proofs (matching closest encloser + covered next-closer + covered wildcard), NODATA (matching record with the type and CNAME absent from the bitmap), Opt-Out DS NODATA, and wildcard NODATA. The iterated-SHA-1 count is capped by `opts.maxIterations` (default 500); an NXDOMAIN proof that depends on an Opt-Out NSEC3 is refused unless `opts.allowOptOut` is set. NSEC supports covering-name NXDOMAIN (with the source-of-synthesis wildcard) and matching-name NODATA. Verified end-to-end against a live iana.org NXDOMAIN proof. · *`b.network.dns.dnssec.nsec3Hash(name, opts)`* — Computes the RFC 5155 §5 NSEC3 hash of a name — iterated SHA-1 over the canonical (lowercased, root-terminated) wire form with the zone salt. The base32hex encoding of the result is the NSEC3 owner label. SHA-1 is the only hash IANA registers for NSEC3, so this is a wire-protocol constant rather than a cryptographic default. Useful for checking an owner label or analyzing a zone's hashing parameters. **Changed:** *`verifyRrset` now accepts NSEC and NSEC3 RRsets* — NSEC (type 47) and NSEC3 (type 50) are no longer refused as uncanonicalizable: NSEC3's next-owner is a hash, and per RFC 6840 §5.1 the NSEC Next Domain Name field is not downcased for DNSSEC canonical form, so both RDATAs are verbatim-canonical. This lets a caller verify the signatures on the records that `verifyDenial` then reasons over.
|
|
14
|
+
|
|
11
15
|
- v0.12.48 (2026-05-25) — **`b.network.dns.dnssec` — local DNSSEC signature verification (RFC 4035).** Verify a DNS answer's RRSIG signature yourself instead of trusting the upstream resolver's AD bit. b.network.dns.dnssec.verifyRrset reconstructs the RFC 4034 §3.1.8.1 signed data — the RRSIG RDATA without the signature, followed by the RRset in canonical form (owner names lowercased, RRs ordered by canonical RDATA, the RRSIG's Original TTL) — and checks the signature against the DNSKEY, enforcing the inception / expiration window. Supports RSA/SHA-256 (alg 8), ECDSA P-256/SHA-256 (13), ECDSA P-384/SHA-384 (14), and Ed25519 (15) — the modern deployed set. verifyDs checks a delegation-signer digest against a DNSKEY (SHA-256 / SHA-384) and keyTag computes the RFC 4034 Appendix B key tag. The verification core is what a chain-walker composes; it defends against a compromised or on-path resolver that lies about authentication. **Added:** *`b.network.dns.dnssec.verifyRrset(opts)`* — Verifies an RRSIG over a canonicalised RRset against a DNSKEY. `opts` carries the owner `name`, the RR `type`, the wire-format `rdatas`, the parsed `rrsig` (algorithm / labels / originalTtl / inception / expiration / keyTag / signerName / signature), and the `dnskey` (algorithm + raw public key). The signed data is rebuilt per RFC 4034 §3.1.8.1: the RRSIG prefix (type covered | algorithm | labels | original TTL | expiration | inception | key tag | canonical signer name) followed by each RR in canonical form (lowercased owner | type | class | original TTL | rdlen | rdata), sorted by `Buffer.compare` on the RDATA. The validity window is enforced against `opts.at` (defaults to now; an invalid Date is refused, not treated as now). An RRSIG whose algorithm disagrees with the DNSKEY is refused before any key is built. RR types that embed domain names in their RDATA (NS, CNAME, SOA, MX, SRV, …) need RDATA-internal name-lowercasing this version does not perform, so they are refused with `dnssec/uncanonicalizable-type` rather than mis-validated; the security-critical DNSKEY / DS and the name-free address / text types (A, AAAA, TXT, CAA, TLSA, …) are fully supported. · *`b.network.dns.dnssec.verifyDs(opts)` / `b.network.dns.dnssec.keyTag(dnskeyRdata)`* — `verifyDs` confirms a delegation-signer record matches a DNSKEY: it checks the key tag, then compares the DS digest (SHA-256 type 2 / SHA-384 type 4) against the digest computed over the canonical owner name and the DNSKEY RDATA, constant-time. `keyTag` computes the RFC 4034 Appendix B 16-bit key tag from a DNSKEY's full RDATA — the identifier an RRSIG or DS uses to select the signing key. Together with `verifyRrset` these are the per-RRset building blocks a recursive chain-walk (root → TLD → zone) composes; the chain-walk itself, NSEC / NSEC3 denial-of-existence, and the bundled IANA root trust anchor are not part of this core.
|
|
12
16
|
|
|
13
17
|
- v0.12.47 (2026-05-25) — **`b.cose.mac0` / `b.cose.macVerify0` — COSE_Mac0 (RFC 9052 §6.2).** Completes the COSE message-type set (COSE_Sign1 / COSE_Encrypt0 / COSE_Mac0) with single shared-key MACs. b.cose.mac0 produces a tagged COSE_Mac0 over a payload using HMAC-SHA-256/384/512 (the COSE-standard MAC algorithms; HMAC is symmetric, so its post-quantum strength is preserved). b.cose.macVerify0 recomputes the tag over the MAC_structure and compares it in constant time, with a mandatory algorithm allowlist. Use when both parties hold a shared key — e.g. an ECDH-derived key — and a non-repudiable signature is not wanted; detached payloads are supported (the proximity mdoc device-MAC variant and MACed CWTs are the consumers). Composes b.cbor + the framework's constant-time compare; no new runtime dependency. **Added:** *`b.cose.mac0(payload, opts)` / `b.cose.macVerify0(coseMac0, opts)`* — `mac0` emits a tagged COSE_Mac0 (tag 17) with `alg` (`HMAC-256/256` | `HMAC-384/384` | `HMAC-512/512`) in the protected header and the HMAC tag computed over the MAC_structure `["MAC0", protected, external_aad, payload]`; `detached: true` emits a nil payload. `macVerify0` reads the algorithm from the protected header (must be in the required `opts.algorithms` allowlist), recomputes the tag, and compares it constant-time — a wrong key, tampered tag, or `external_aad` mismatch is refused with `cose/bad-tag`; a detached payload is supplied via `opts.externalPayload`. `external_aad` binds context into the tag.
|
package/README.md
CHANGED
|
@@ -120,7 +120,7 @@ The framework bundles the surface a typical Node app reaches for. Every primitiv
|
|
|
120
120
|
- In-process CIDR fence (`b.middleware.networkAllowlist`)
|
|
121
121
|
- `Cache-Control: no-store` on every 401 from `requireAuth` / `requireAal` / `requireStepUp` per RFC 9111 §5.2.2.5
|
|
122
122
|
- **Outbound HTTP client** — HTTP/1.1 + HTTP/2 with SSRF gate (cloud-metadata IPs hard-denied; private / loopback / link-local overridable per call); scheme + userinfo + per-host destination allowlist; redirects, multipart, interceptors, progress, encrypted cookie jar (`b.httpClient`, `b.ssrfGuard`, `b.safeUrl`)
|
|
123
|
-
- **Network configurability (`b.network`)** — env-driven NTP / NTS (RFC 8915), IPv4/IPv6 NTP, DNS with IPv6 / DoH / DoT (private-CA pinning) / cache / lookup timeout; local DNSSEC signature verification (RFC 4035 — `b.network.dns.dnssec.verifyRrset` over a canonicalised RRset against RSA / ECDSA P-256·P-384 / Ed25519 DNSKEYs, plus DS-digest + key-tag) so a resolver client can verify
|
|
123
|
+
- **Network configurability (`b.network`)** — env-driven NTP / NTS (RFC 8915), IPv4/IPv6 NTP, DNS with IPv6 / DoH / DoT (private-CA pinning) / cache / lookup timeout; local DNSSEC signature verification (RFC 4035 — `b.network.dns.dnssec.verifyRrset` over a canonicalised RRset against RSA / ECDSA P-256·P-384 / Ed25519 DNSKEYs, plus DS-digest + key-tag, plus `verifyDenial` for NSEC / NSEC3 (RFC 5155) NXDOMAIN / NODATA proofs with iteration caps + Opt-Out handling, plus `verifyChain` to validate a full root→TLD→zone delegation chain against the pinned IANA root anchors) so a resolver client can verify both positive and negative answers instead of trusting the upstream AD bit; outbound HTTP proxy (`HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY`); runtime DPI trust-store CA additions; application-level heartbeats; TCP socket defaults
|
|
124
124
|
- **Error pages** — operator-rendered, no app-frame leakage (`b.errorPage`)
|
|
125
125
|
### Defensive parsers
|
|
126
126
|
|
package/lib/network-dnssec.js
CHANGED
|
@@ -60,16 +60,21 @@ var ALGS = {
|
|
|
60
60
|
// DS digest algorithms (IANA) → node hash.
|
|
61
61
|
var DS_DIGESTS = { 2: "sha256", 4: "sha384" };
|
|
62
62
|
|
|
63
|
-
// RR types whose RDATA contains NO embedded domain name
|
|
64
|
-
// RDATA is already in canonical form (RFC 4034
|
|
65
|
-
// Name-bearing types are refused rather than
|
|
63
|
+
// RR types whose RDATA contains NO embedded domain name that needs
|
|
64
|
+
// downcasing, so the wire RDATA is already in canonical form (RFC 4034
|
|
65
|
+
// §6.2 needs no rewrite). Name-bearing types are refused rather than
|
|
66
|
+
// silently mis-canonicalised. NSEC (47) is included because RFC 6840
|
|
67
|
+
// §5.1 corrected RFC 4034 §6.2: the NSEC Next Domain Name field is NOT
|
|
68
|
+
// downcased for DNSSEC canonical form, so its uncompressed RDATA is
|
|
69
|
+
// verbatim-canonical. NSEC3 (50) carries a hashed next-owner, not a name.
|
|
66
70
|
// (type numbers IANA): A 1, AAAA 28, TXT 16, DNSKEY 48, DS 43, CAA 257,
|
|
67
|
-
// TLSA 52, SSHFP 44, HINFO 13, CDS 59, CDNSKEY 60, OPENPGPKEY 61, SMIMEA
|
|
68
|
-
|
|
71
|
+
// TLSA 52, SSHFP 44, HINFO 13, CDS 59, CDNSKEY 60, OPENPGPKEY 61, SMIMEA
|
|
72
|
+
// 53, NSEC 47, NSEC3 50.
|
|
73
|
+
var NAME_FREE_TYPE_NUMS = [1, 28, 16, 48, 43, 257, 52, 44, 13, 59, 60, 61, 53, 47, 50]; // allow:raw-byte-literal allow:raw-time-literal — IANA DNS type numbers (no downcased embedded names)
|
|
69
74
|
var TYPE_NUM = {
|
|
70
75
|
A: 1, NS: 2, CNAME: 5, SOA: 6, PTR: 12, MX: 15, TXT: 16, AAAA: 28, SRV: 33,
|
|
71
|
-
DS: 43, SSHFP: 44, RRSIG: 46,
|
|
72
|
-
OPENPGPKEY: 61, CAA: 257, HINFO: 13,
|
|
76
|
+
DS: 43, SSHFP: 44, RRSIG: 46, NSEC: 47, DNSKEY: 48, NSEC3: 50, TLSA: 52, // allow:raw-byte-literal allow:raw-time-literal — IANA DNS type numbers
|
|
77
|
+
SMIMEA: 53, CDS: 59, CDNSKEY: 60, OPENPGPKEY: 61, CAA: 257, HINFO: 13,
|
|
73
78
|
};
|
|
74
79
|
|
|
75
80
|
function _bytes(x, what) {
|
|
@@ -319,10 +324,560 @@ function verifyRrset(opts) {
|
|
|
319
324
|
return { ok: true, algorithm: alg.name, keyTag: rrsig.keyTag, signerName: rrsig.signerName };
|
|
320
325
|
}
|
|
321
326
|
|
|
327
|
+
// ---------------------------------------------------------------------------
|
|
328
|
+
// Denial of existence (RFC 4034 §4 NSEC, RFC 5155 NSEC3).
|
|
329
|
+
//
|
|
330
|
+
// These helpers prove a name (or a name+type) DOES NOT EXIST from the
|
|
331
|
+
// signed NSEC / NSEC3 records a server returns in the Authority section.
|
|
332
|
+
// They operate on records the caller has ALREADY verified with
|
|
333
|
+
// verifyRrset — like verifyDs, this is the relation check, not the
|
|
334
|
+
// signature check. Passing unverified records proves nothing.
|
|
335
|
+
// ---------------------------------------------------------------------------
|
|
336
|
+
|
|
337
|
+
var BASE32HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; // allow:raw-byte-literal — RFC 4648 §7 extended-hex alphabet (RFC number in comment)
|
|
338
|
+
var TYPE_DS = 43; // allow:raw-byte-literal — IANA RR type DS
|
|
339
|
+
var TYPE_CNAME = 5;
|
|
340
|
+
var NSEC3_HASH_SHA1 = 1; // RFC 5155 §5 — the only registered NSEC3 hash
|
|
341
|
+
var DEFAULT_MAX_NSEC3_ITERATIONS = 500; // allow:raw-time-literal — DoS ceiling on iterated SHA-1 (RFC 9276 wants 0; deployed zones still use >0)
|
|
342
|
+
|
|
343
|
+
// RFC 4648 §7 base32hex decode (no padding, case-insensitive) — the
|
|
344
|
+
// label encoding of an NSEC3 owner-name hash.
|
|
345
|
+
function _base32hexDecode(s, label) {
|
|
346
|
+
var up = String(s).toUpperCase();
|
|
347
|
+
var bits = 0, value = 0, out = [];
|
|
348
|
+
for (var i = 0; i < up.length; i++) {
|
|
349
|
+
var idx = BASE32HEX.indexOf(up[i]);
|
|
350
|
+
if (idx === -1) throw new DnssecError("dnssec/bad-nsec3", "dnssec: " + label + " is not valid base32hex");
|
|
351
|
+
value = (value << 5) | idx; // allow:raw-byte-literal — base32 5-bit group
|
|
352
|
+
bits += 5; // allow:raw-byte-literal — base32 5-bit group
|
|
353
|
+
if (bits >= 8) { bits -= 8; out.push((value >> bits) & 0xff); } // allow:raw-byte-literal — emit a full octet
|
|
354
|
+
}
|
|
355
|
+
return Buffer.from(out);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// RFC 4034 §4.1.2 / RFC 5155 §3.2.1 type bitmap → Set of type numbers.
|
|
359
|
+
function _parseTypeBitmaps(buf, off, end) {
|
|
360
|
+
var types = new Set();
|
|
361
|
+
var i = off;
|
|
362
|
+
while (i + 2 <= end) {
|
|
363
|
+
var win = buf[i], len = buf[i + 1];
|
|
364
|
+
i += 2;
|
|
365
|
+
if (len < 1 || len > 32 || i + len > end) { // allow:raw-byte-literal — bitmap window ≤ 256 bits = 32 octets (RFC 4034 §4.1.2)
|
|
366
|
+
throw new DnssecError("dnssec/bad-bitmap", "dnssec: malformed NSEC type bitmap");
|
|
367
|
+
}
|
|
368
|
+
for (var j = 0; j < len; j++) {
|
|
369
|
+
var octet = buf[i + j];
|
|
370
|
+
for (var bit = 0; bit < 8; bit++) { // allow:raw-byte-literal — 8 bits per octet
|
|
371
|
+
if (octet & (0x80 >> bit)) types.add(win * 256 + j * 8 + bit); // allow:raw-byte-literal — bit→type-number (window*256 + octet*8 + bit)
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
i += len;
|
|
375
|
+
}
|
|
376
|
+
return types;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Read an uncompressed wire-format domain name (compression pointers are
|
|
380
|
+
// illegal in signed RDATA). Returns { name, end }.
|
|
381
|
+
function _readWireName(buf, off) {
|
|
382
|
+
var labels = [];
|
|
383
|
+
var i = off;
|
|
384
|
+
for (;;) {
|
|
385
|
+
if (i >= buf.length) throw new DnssecError("dnssec/bad-name", "dnssec: truncated name in RDATA");
|
|
386
|
+
var len = buf[i];
|
|
387
|
+
if (len === 0) { i++; break; }
|
|
388
|
+
if ((len & 0xc0) !== 0) throw new DnssecError("dnssec/bad-name", "dnssec: compression pointer in signed RDATA"); // allow:raw-byte-literal — RFC 1035 label-length top-two-bits flag
|
|
389
|
+
i++;
|
|
390
|
+
labels.push(buf.slice(i, i + len).toString("ascii"));
|
|
391
|
+
i += len;
|
|
392
|
+
}
|
|
393
|
+
return { name: labels.length ? labels.join(".") + "." : ".", end: i };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function _parseNsec3Rdata(rd) {
|
|
397
|
+
if (rd.length < 6) throw new DnssecError("dnssec/bad-nsec3", "dnssec: NSEC3 RDATA too short"); // allow:raw-byte-literal — fixed NSEC3 header octets
|
|
398
|
+
var hashAlg = rd[0], flags = rd[1], iterations = rd.readUInt16BE(2), saltLen = rd[4];
|
|
399
|
+
var off = 5 + saltLen;
|
|
400
|
+
if (off + 1 > rd.length) throw new DnssecError("dnssec/bad-nsec3", "dnssec: NSEC3 salt overruns RDATA");
|
|
401
|
+
var salt = rd.slice(5, 5 + saltLen);
|
|
402
|
+
var hashLen = rd[off]; off += 1;
|
|
403
|
+
if (off + hashLen > rd.length) throw new DnssecError("dnssec/bad-nsec3", "dnssec: NSEC3 next-hashed-owner overruns RDATA");
|
|
404
|
+
var nextHashed = rd.slice(off, off + hashLen);
|
|
405
|
+
return { hashAlg: hashAlg, flags: flags, iterations: iterations, salt: salt, nextHashed: nextHashed, types: _parseTypeBitmaps(rd, off + hashLen, rd.length) };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function _parseNsecRdata(rd) {
|
|
409
|
+
var n = _readWireName(rd, 0);
|
|
410
|
+
return { nextName: n.name, types: _parseTypeBitmaps(rd, n.end, rd.length) };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// RFC 5155 §5 iterated hash: IH(salt, x, 0)=SHA-1(x‖salt);
|
|
414
|
+
// IH(salt, x, k)=SHA-1(IH(salt,x,k-1)‖salt). SHA-1 is the only NSEC3
|
|
415
|
+
// hash IANA defines — a wire-protocol constant, not a framework default.
|
|
416
|
+
function _nsec3HashWire(nameWire, salt, iterations) {
|
|
417
|
+
var h = nodeCrypto.createHash("sha1").update(Buffer.concat([nameWire, salt])).digest();
|
|
418
|
+
for (var k = 0; k < iterations; k++) {
|
|
419
|
+
h = nodeCrypto.createHash("sha1").update(Buffer.concat([h, salt])).digest();
|
|
420
|
+
}
|
|
421
|
+
return h;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function _nameLabels(name) {
|
|
425
|
+
var n = String(name).replace(/\.$/, "");
|
|
426
|
+
return n === "" ? [] : n.split(".");
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// RFC 4034 §6.1 canonical name ordering: compare label sequences from
|
|
430
|
+
// the least-significant (rightmost) label, octets lowercased.
|
|
431
|
+
function _canonicalNameCompare(a, b) {
|
|
432
|
+
var la = _nameLabels(a).reverse(), lb = _nameLabels(b).reverse();
|
|
433
|
+
var min = Math.min(la.length, lb.length);
|
|
434
|
+
for (var i = 0; i < min; i++) {
|
|
435
|
+
var c = Buffer.compare(Buffer.from(la[i].toLowerCase(), "ascii"), Buffer.from(lb[i].toLowerCase(), "ascii"));
|
|
436
|
+
if (c !== 0) return c;
|
|
437
|
+
}
|
|
438
|
+
return la.length - lb.length;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Closest-encloser candidates: proper suffixes of qname from longest
|
|
442
|
+
// (qname minus one label) down to the zone apex, longest first.
|
|
443
|
+
function _closestEncloserCandidates(qname, zone) {
|
|
444
|
+
var ql = _nameLabels(qname), zl = _nameLabels(zone);
|
|
445
|
+
var out = [];
|
|
446
|
+
for (var k = ql.length - 1; k >= zl.length; k--) {
|
|
447
|
+
out.push(ql.slice(ql.length - k).join(".") + ".");
|
|
448
|
+
}
|
|
449
|
+
return out;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// The "next closer" name: the closest encloser with one more label of
|
|
453
|
+
// qname prepended (RFC 5155 §1.3).
|
|
454
|
+
function _nextCloser(qname, ce) {
|
|
455
|
+
var ql = _nameLabels(qname), n = _nameLabels(ce).length + 1;
|
|
456
|
+
return ql.slice(ql.length - n).join(".") + ".";
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @primitive b.network.dns.dnssec.nsec3Hash
|
|
461
|
+
* @signature b.network.dns.dnssec.nsec3Hash(name, opts)
|
|
462
|
+
* @since 0.12.49
|
|
463
|
+
* @status stable
|
|
464
|
+
* @related b.network.dns.dnssec.verifyDenial
|
|
465
|
+
*
|
|
466
|
+
* Compute the RFC 5155 §5 NSEC3 hash of a name — iterated SHA-1 over the
|
|
467
|
+
* canonical (lowercased, root-terminated) wire form with the zone's salt.
|
|
468
|
+
* The result is the unencoded hash; the NSEC3 owner label is its
|
|
469
|
+
* base32hex encoding. SHA-1 is the only hash IANA registers for NSEC3,
|
|
470
|
+
* so this is a wire-protocol constant, not a cryptographic default.
|
|
471
|
+
*
|
|
472
|
+
* @opts
|
|
473
|
+
* {
|
|
474
|
+
* salt: Buffer, // zone NSEC3 salt (may be empty)
|
|
475
|
+
* iterations: number, // additional hash iterations (>= 0)
|
|
476
|
+
* }
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* var h = b.network.dns.dnssec.nsec3Hash("a.example.com", { salt: salt, iterations: 0 });
|
|
480
|
+
*/
|
|
481
|
+
function nsec3Hash(name, opts) {
|
|
482
|
+
validateOpts.requireObject(opts, "dnssec.nsec3Hash", DnssecError);
|
|
483
|
+
validateOpts(opts, ["salt", "iterations"], "dnssec.nsec3Hash");
|
|
484
|
+
var salt = _bytes(opts.salt, "salt");
|
|
485
|
+
var iters = opts.iterations;
|
|
486
|
+
if (typeof iters !== "number" || !isFinite(iters) || iters < 0 || Math.floor(iters) !== iters) {
|
|
487
|
+
throw new DnssecError("dnssec/bad-iterations", "dnssec.nsec3Hash: iterations must be a non-negative integer");
|
|
488
|
+
}
|
|
489
|
+
return _nsec3HashWire(_canonicalName(name), salt, iters);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @primitive b.network.dns.dnssec.verifyDenial
|
|
494
|
+
* @signature b.network.dns.dnssec.verifyDenial(opts)
|
|
495
|
+
* @since 0.12.49
|
|
496
|
+
* @status stable
|
|
497
|
+
* @compliance soc2
|
|
498
|
+
* @related b.network.dns.dnssec.verifyRrset, b.network.dns.dnssec.nsec3Hash
|
|
499
|
+
*
|
|
500
|
+
* Prove that a name does not exist (NXDOMAIN) or that a name has no
|
|
501
|
+
* records of a given type (NODATA) from the signed NSEC (RFC 4034 §4) or
|
|
502
|
+
* NSEC3 (RFC 5155) records in a response's Authority section. This is the
|
|
503
|
+
* other half of "verify the answer yourself": <code>verifyRrset</code>
|
|
504
|
+
* proves a positive answer, <code>verifyDenial</code> proves a negative.
|
|
505
|
+
*
|
|
506
|
+
* The records MUST already be verified with <code>verifyRrset</code> —
|
|
507
|
+
* this checks the denial RELATION (closest-encloser, covering ranges,
|
|
508
|
+
* type-bitmap absence), not the signatures. For NSEC3, the iterated-hash
|
|
509
|
+
* count is capped (<code>opts.maxIterations</code>, default 500) to bound
|
|
510
|
+
* the SHA-1 work an attacker can force. An NXDOMAIN proof that relies on
|
|
511
|
+
* an Opt-Out NSEC3 (RFC 5155 §6) is refused unless
|
|
512
|
+
* <code>opts.allowOptOut</code> — opt-out only proves "no signed records",
|
|
513
|
+
* not non-existence.
|
|
514
|
+
*
|
|
515
|
+
* @opts
|
|
516
|
+
* {
|
|
517
|
+
* qname: string, // the queried name
|
|
518
|
+
* qtype: string|number, // queried type (required for proof "nodata")
|
|
519
|
+
* proof: string, // "nxdomain" | "nodata"
|
|
520
|
+
* zone: string, // the signer zone apex (a suffix of qname)
|
|
521
|
+
* nsec3?: [ { owner: string, rdata: Buffer } ], // NSEC3 records (owner = base32hex-label.zone)
|
|
522
|
+
* nsec?: [ { owner: string, rdata: Buffer } ], // NSEC records
|
|
523
|
+
* maxIterations?: number, // NSEC3 iteration cap (default 500)
|
|
524
|
+
* allowOptOut?: boolean, // accept an Opt-Out NXDOMAIN proof (default false)
|
|
525
|
+
* }
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* b.network.dns.dnssec.verifyDenial({
|
|
529
|
+
* qname: "nope.example.com", proof: "nxdomain", zone: "example.com", nsec3: records,
|
|
530
|
+
* });
|
|
531
|
+
*/
|
|
532
|
+
function verifyDenial(opts) {
|
|
533
|
+
validateOpts.requireObject(opts, "dnssec.verifyDenial", DnssecError);
|
|
534
|
+
validateOpts(opts, ["qname", "qtype", "proof", "zone", "nsec3", "nsec", "maxIterations", "allowOptOut"], "dnssec.verifyDenial");
|
|
535
|
+
if (typeof opts.qname !== "string" || opts.qname === "") throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: opts.qname is required");
|
|
536
|
+
if (typeof opts.zone !== "string" || opts.zone === "") throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: opts.zone is required");
|
|
537
|
+
if (opts.proof !== "nxdomain" && opts.proof !== "nodata") throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: opts.proof must be 'nxdomain' or 'nodata'");
|
|
538
|
+
var zl = _nameLabels(opts.zone), ql = _nameLabels(opts.qname);
|
|
539
|
+
if (zl.length > ql.length || zl.join(".").toLowerCase() !== ql.slice(ql.length - zl.length).join(".").toLowerCase()) {
|
|
540
|
+
throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: opts.zone must be a suffix of opts.qname");
|
|
541
|
+
}
|
|
542
|
+
var qtypeNum;
|
|
543
|
+
if (opts.proof === "nodata") {
|
|
544
|
+
if (opts.qtype === undefined || opts.qtype === null) throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: opts.qtype is required for a nodata proof");
|
|
545
|
+
qtypeNum = _typeNumber(opts.qtype);
|
|
546
|
+
} else if (opts.qtype !== undefined && opts.qtype !== null) {
|
|
547
|
+
qtypeNum = _typeNumber(opts.qtype);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
var hasNsec3 = Array.isArray(opts.nsec3) && opts.nsec3.length > 0;
|
|
551
|
+
var hasNsec = Array.isArray(opts.nsec) && opts.nsec.length > 0;
|
|
552
|
+
if (hasNsec3 === hasNsec) {
|
|
553
|
+
throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: supply exactly one of opts.nsec3 or opts.nsec");
|
|
554
|
+
}
|
|
555
|
+
return hasNsec3 ? _verifyNsec3Denial(opts, qtypeNum) : _verifyNsecDenial(opts, qtypeNum);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function _verifyNsec3Denial(opts, qtypeNum) {
|
|
559
|
+
var maxIter = typeof opts.maxIterations === "number" ? opts.maxIterations : DEFAULT_MAX_NSEC3_ITERATIONS;
|
|
560
|
+
if (typeof maxIter !== "number" || !isFinite(maxIter) || maxIter < 0) throw new DnssecError("dnssec/bad-arg", "dnssec.verifyDenial: maxIterations must be a non-negative number");
|
|
561
|
+
|
|
562
|
+
// Parse + sanity-check every NSEC3 record; the chain shares one salt /
|
|
563
|
+
// iteration / hash-algorithm tuple.
|
|
564
|
+
var recs = opts.nsec3.map(function (r, i) {
|
|
565
|
+
if (!r || typeof r.owner !== "string") throw new DnssecError("dnssec/bad-nsec3", "dnssec.verifyDenial: nsec3[" + i + "].owner must be a string");
|
|
566
|
+
var rd = _bytes(r.rdata, "nsec3[" + i + "].rdata");
|
|
567
|
+
var p = _parseNsec3Rdata(rd);
|
|
568
|
+
if (p.hashAlg !== NSEC3_HASH_SHA1) throw new DnssecError("dnssec/unsupported-nsec3-hash", "dnssec.verifyDenial: NSEC3 hash algorithm " + p.hashAlg + " is not supported (only SHA-1 / 1 is defined)");
|
|
569
|
+
if (p.iterations > maxIter) throw new DnssecError("dnssec/nsec3-iterations-excessive", "dnssec.verifyDenial: NSEC3 iterations " + p.iterations + " exceed the cap " + maxIter);
|
|
570
|
+
var firstLabel = _nameLabels(r.owner)[0];
|
|
571
|
+
if (!firstLabel) throw new DnssecError("dnssec/bad-nsec3", "dnssec.verifyDenial: nsec3[" + i + "].owner has no hash label");
|
|
572
|
+
return { ownerHash: _base32hexDecode(firstLabel, "nsec3[" + i + "].owner"), p: p };
|
|
573
|
+
});
|
|
574
|
+
var salt = recs[0].p.salt, iterations = recs[0].p.iterations;
|
|
575
|
+
for (var s = 1; s < recs.length; s++) {
|
|
576
|
+
if (recs[s].p.iterations !== iterations || Buffer.compare(recs[s].p.salt, salt) !== 0) {
|
|
577
|
+
throw new DnssecError("dnssec/nsec3-param-mismatch", "dnssec.verifyDenial: NSEC3 records disagree on salt / iterations");
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function hashOf(name) { return _nsec3HashWire(_canonicalName(name), salt, iterations); }
|
|
582
|
+
function findMatch(name) {
|
|
583
|
+
var h = hashOf(name);
|
|
584
|
+
for (var i = 0; i < recs.length; i++) if (Buffer.compare(recs[i].ownerHash, h) === 0) return recs[i];
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
587
|
+
function findCover(name) {
|
|
588
|
+
var h = hashOf(name);
|
|
589
|
+
for (var i = 0; i < recs.length; i++) {
|
|
590
|
+
var owner = recs[i].ownerHash, next = recs[i].p.nextHashed;
|
|
591
|
+
var oc = Buffer.compare(owner, next);
|
|
592
|
+
var covered = oc < 0
|
|
593
|
+
? (Buffer.compare(owner, h) < 0 && Buffer.compare(h, next) < 0)
|
|
594
|
+
: (Buffer.compare(owner, h) < 0 || Buffer.compare(h, next) < 0); // last NSEC3 wraps past the apex
|
|
595
|
+
if (covered) return recs[i];
|
|
596
|
+
}
|
|
597
|
+
return null;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (opts.proof === "nodata") {
|
|
601
|
+
// RFC 5155 §8.5 — a matching NSEC3 with the type (and CNAME) absent.
|
|
602
|
+
var m = findMatch(opts.qname);
|
|
603
|
+
if (m) {
|
|
604
|
+
if (qtypeNum === TYPE_DS) {
|
|
605
|
+
if (m.p.types.has(TYPE_DS)) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: DS is present in the matching NSEC3 bitmap");
|
|
606
|
+
return { ok: true, proof: "nodata", mechanism: "nsec3", matched: true };
|
|
607
|
+
}
|
|
608
|
+
if (m.p.types.has(qtypeNum)) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: type " + qtypeNum + " is present in the matching NSEC3 bitmap");
|
|
609
|
+
if (m.p.types.has(TYPE_CNAME)) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: name is a CNAME (query should have been redirected)");
|
|
610
|
+
return { ok: true, proof: "nodata", mechanism: "nsec3", matched: true };
|
|
611
|
+
}
|
|
612
|
+
// RFC 5155 §8.6 — Opt-Out DS NODATA: a covering NSEC3 with Opt-Out set
|
|
613
|
+
// proves an insecure delegation has no DS.
|
|
614
|
+
if (qtypeNum === TYPE_DS) {
|
|
615
|
+
var ce = _nsec3ClosestEncloser(opts, recs, findMatch);
|
|
616
|
+
if (ce) {
|
|
617
|
+
var nc = _nextCloser(opts.qname, ce);
|
|
618
|
+
var cov = findCover(nc);
|
|
619
|
+
if (cov && (cov.p.flags & 1) === 1) return { ok: true, proof: "nodata", mechanism: "nsec3", matched: false, optOut: true };
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
// RFC 5155 §8.7 — wildcard NODATA: closest encloser proof + a matching
|
|
623
|
+
// wildcard NSEC3 with the type absent.
|
|
624
|
+
var ce2 = _nsec3ClosestEncloser(opts, recs, findMatch);
|
|
625
|
+
if (ce2) {
|
|
626
|
+
var wc = findMatch("*." + ce2);
|
|
627
|
+
if (wc && !wc.p.types.has(qtypeNum) && !wc.p.types.has(TYPE_CNAME)) {
|
|
628
|
+
return { ok: true, proof: "nodata", mechanism: "nsec3", matched: false, wildcard: true, closestEncloser: ce2 };
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: no matching NSEC3 proves NODATA for the queried type");
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// NXDOMAIN (RFC 5155 §8.4): matching closest encloser + covered next
|
|
635
|
+
// closer + covered wildcard. Opt-Out on the next-closer cover only
|
|
636
|
+
// proves "no signed records", so it is refused unless allowOptOut.
|
|
637
|
+
var ceName = _nsec3ClosestEncloser(opts, recs, findMatch);
|
|
638
|
+
if (!ceName) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: no NSEC3 matches any closest-encloser candidate");
|
|
639
|
+
var nextCloser = _nextCloser(opts.qname, ceName);
|
|
640
|
+
var ncCover = findCover(nextCloser);
|
|
641
|
+
if (!ncCover) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: the next-closer name is not covered by any NSEC3");
|
|
642
|
+
var optOut = (ncCover.p.flags & 1) === 1;
|
|
643
|
+
if (optOut && !opts.allowOptOut) throw new DnssecError("dnssec/denial-opt-out", "dnssec.verifyDenial: NXDOMAIN relies on an Opt-Out NSEC3 (set allowOptOut to accept it as 'no signed records')");
|
|
644
|
+
// The wildcard at the closest encloser must be proven NON-EXISTENT
|
|
645
|
+
// (covered). A MATCHING wildcard means it exists, so the name should
|
|
646
|
+
// have been wildcard-synthesised and NXDOMAIN would be a forgery.
|
|
647
|
+
if (!findCover("*." + ceName)) {
|
|
648
|
+
throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: the wildcard at the closest encloser is not covered (a matching wildcard would mean the name should have been synthesised)");
|
|
649
|
+
}
|
|
650
|
+
return { ok: true, proof: "nxdomain", mechanism: "nsec3", closestEncloser: ceName, optOut: optOut };
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function _nsec3ClosestEncloser(opts, recs, findMatch) {
|
|
654
|
+
var cands = _closestEncloserCandidates(opts.qname, opts.zone);
|
|
655
|
+
for (var i = 0; i < cands.length; i++) if (findMatch(cands[i])) return cands[i];
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function _verifyNsecDenial(opts, qtypeNum) {
|
|
660
|
+
var recs = opts.nsec.map(function (r, i) {
|
|
661
|
+
if (!r || typeof r.owner !== "string") throw new DnssecError("dnssec/bad-nsec", "dnssec.verifyDenial: nsec[" + i + "].owner must be a string");
|
|
662
|
+
return { owner: r.owner, p: _parseNsecRdata(_bytes(r.rdata, "nsec[" + i + "].rdata")) };
|
|
663
|
+
});
|
|
664
|
+
function findMatch(name) {
|
|
665
|
+
for (var i = 0; i < recs.length; i++) if (_canonicalNameCompare(recs[i].owner, name) === 0) return recs[i];
|
|
666
|
+
return null;
|
|
667
|
+
}
|
|
668
|
+
function findCover(name) {
|
|
669
|
+
for (var i = 0; i < recs.length; i++) {
|
|
670
|
+
var owner = recs[i].owner, next = recs[i].p.nextName;
|
|
671
|
+
var oc = _canonicalNameCompare(owner, next);
|
|
672
|
+
var afterOwner = _canonicalNameCompare(owner, name) < 0;
|
|
673
|
+
var covered = oc < 0
|
|
674
|
+
? (afterOwner && _canonicalNameCompare(name, next) < 0)
|
|
675
|
+
: afterOwner; // last NSEC (next wraps to apex): any name after owner
|
|
676
|
+
if (covered) return recs[i];
|
|
677
|
+
}
|
|
678
|
+
return null;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
if (opts.proof === "nodata") {
|
|
682
|
+
var m = findMatch(opts.qname);
|
|
683
|
+
if (!m) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: no NSEC matches the queried name");
|
|
684
|
+
if (m.p.types.has(qtypeNum)) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: type " + qtypeNum + " is present in the matching NSEC bitmap");
|
|
685
|
+
if (qtypeNum !== TYPE_CNAME && m.p.types.has(TYPE_CNAME)) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: name is a CNAME (query should have been redirected)");
|
|
686
|
+
return { ok: true, proof: "nodata", mechanism: "nsec", matched: true };
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// NXDOMAIN (RFC 4035 §5.4): an NSEC covering qname AND an NSEC proving
|
|
690
|
+
// the source-of-synthesis wildcard does not exist.
|
|
691
|
+
var cover = findCover(opts.qname);
|
|
692
|
+
if (!cover) throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: no NSEC covers the queried name");
|
|
693
|
+
// The closest encloser is the longest common ancestor of qname and the
|
|
694
|
+
// covering NSEC's owner/next; the wildcard sits one label below it.
|
|
695
|
+
var ce = _nsecClosestEncloser(opts.qname, cover);
|
|
696
|
+
// The source-of-synthesis wildcard must be proven NON-EXISTENT
|
|
697
|
+
// (covered). A MATCHING wildcard owner means it exists, so the query
|
|
698
|
+
// should have been answered by wildcard expansion, not NXDOMAIN.
|
|
699
|
+
var wildcard = "*." + ce;
|
|
700
|
+
if (!findCover(wildcard)) {
|
|
701
|
+
throw new DnssecError("dnssec/denial-not-proven", "dnssec.verifyDenial: the wildcard at the closest encloser is not covered (a matching wildcard would mean the name should have been synthesised)");
|
|
702
|
+
}
|
|
703
|
+
return { ok: true, proof: "nxdomain", mechanism: "nsec", closestEncloser: ce };
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// The closest encloser for an NSEC NXDOMAIN proof is the longest name
|
|
707
|
+
// that is a suffix of qname and an ancestor of both the covering NSEC's
|
|
708
|
+
// owner and its next name (RFC 4035 §5.3.4 / §5.4).
|
|
709
|
+
function _nsecClosestEncloser(qname, cover) {
|
|
710
|
+
var ql = _nameLabels(qname);
|
|
711
|
+
var a = _commonSuffixLen(qname, cover.owner);
|
|
712
|
+
var b = _commonSuffixLen(qname, cover.p.nextName);
|
|
713
|
+
var ceLen = Math.max(a, b);
|
|
714
|
+
return ql.slice(ql.length - ceLen).join(".") + ".";
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function _commonSuffixLen(a, b) {
|
|
718
|
+
var la = _nameLabels(a).reverse(), lb = _nameLabels(b).reverse();
|
|
719
|
+
var n = 0, min = Math.min(la.length, lb.length);
|
|
720
|
+
while (n < min && la[n].toLowerCase() === lb[n].toLowerCase()) n++;
|
|
721
|
+
return n;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// ---------------------------------------------------------------------------
|
|
725
|
+
// Chain validation (RFC 4035 §5) — walk a delegation chain root → … → zone,
|
|
726
|
+
// anchoring at a pinned trust anchor.
|
|
727
|
+
// ---------------------------------------------------------------------------
|
|
728
|
+
|
|
729
|
+
// IANA root zone trust anchors (DS / SHA-256). KSK-2017 (tag 20326) and
|
|
730
|
+
// KSK-2024 (tag 38696), published at data.iana.org/root-anchors. An
|
|
731
|
+
// operator pins their own via opts.trustAnchors.
|
|
732
|
+
var DEFAULT_ROOT_ANCHORS = [
|
|
733
|
+
{ keyTag: 20326, algorithm: 8, digestType: 2, digest: Buffer.from("E06D44B80B8F1D39A95C0B0D7C65D08458E880409BBC683457104237C7F8EC8D", "hex") }, // allow:raw-byte-literal — IANA root KSK-2017 DS
|
|
734
|
+
{ keyTag: 38696, algorithm: 8, digestType: 2, digest: Buffer.from("683D2D0ACB8C9B712A1948B27F741219298D0A450D612C483AF444A4C0FB2B16", "hex") }, // allow:raw-byte-literal — IANA root KSK-2024 DS
|
|
735
|
+
];
|
|
736
|
+
|
|
737
|
+
function _dnskeyParts(rdata, what) {
|
|
738
|
+
var rd = _bytes(rdata, what || "dnskey rdata");
|
|
739
|
+
if (rd.length < 4) throw new DnssecError("dnssec/bad-key", "dnssec: DNSKEY RDATA too short"); // allow:raw-byte-literal — DNSKEY fixed header octets
|
|
740
|
+
return { flags: rd.readUInt16BE(0), algorithm: rd[3], publicKey: rd.slice(4) };
|
|
741
|
+
}
|
|
742
|
+
function _parseDsRdata(rd) {
|
|
743
|
+
if (rd.length < 5) throw new DnssecError("dnssec/bad-ds", "dnssec: DS RDATA too short"); // allow:raw-byte-literal — DS fixed header octets
|
|
744
|
+
return { keyTag: rd.readUInt16BE(0), algorithm: rd[2], digestType: rd[3], digest: rd.slice(4) };
|
|
745
|
+
}
|
|
746
|
+
// ALL DNSKEYs whose key tag matches — 16-bit key tags collide (RFC 4034
|
|
747
|
+
// App B), so a verifier must try every candidate, not just the first.
|
|
748
|
+
function _keysByTag(dnskeys, tag) {
|
|
749
|
+
var out = [];
|
|
750
|
+
for (var i = 0; i < dnskeys.length; i++) if (keyTag(dnskeys[i]) === tag) out.push(dnskeys[i]);
|
|
751
|
+
return out;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// Verify an RRset against EVERY DNSKEY whose tag matches the RRSIG,
|
|
755
|
+
// returning the key that validated. A wrong colliding key yields
|
|
756
|
+
// `dnssec/bad-signature` — that is not terminal, the next candidate is
|
|
757
|
+
// tried; any other error (expired, alg) is terminal. RFC 4035 §5.3.1.
|
|
758
|
+
function _verifyRrsetWithAnyKey(rrsetBase, rrsig, candidates, noKeyCode, noKeyMsg) {
|
|
759
|
+
if (candidates.length === 0) throw new DnssecError(noKeyCode, noKeyMsg);
|
|
760
|
+
var lastErr = null;
|
|
761
|
+
for (var i = 0; i < candidates.length; i++) {
|
|
762
|
+
var kp = _dnskeyParts(candidates[i]);
|
|
763
|
+
if (kp.algorithm !== rrsig.algorithm) { lastErr = new DnssecError("dnssec/alg-mismatch", "dnssec.verifyChain: candidate key algorithm does not match the RRSIG"); continue; }
|
|
764
|
+
try {
|
|
765
|
+
verifyRrset(Object.assign({}, rrsetBase, { rrsig: rrsig, dnskey: { algorithm: kp.algorithm, publicKey: kp.publicKey } }));
|
|
766
|
+
return candidates[i];
|
|
767
|
+
} catch (e) {
|
|
768
|
+
if (e && e.code === "dnssec/bad-signature") { lastErr = e; continue; } // colliding non-signing key — try the next
|
|
769
|
+
throw e;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
throw lastErr;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* @primitive b.network.dns.dnssec.verifyChain
|
|
777
|
+
* @signature b.network.dns.dnssec.verifyChain(opts)
|
|
778
|
+
* @since 0.12.50
|
|
779
|
+
* @status stable
|
|
780
|
+
* @compliance soc2
|
|
781
|
+
* @related b.network.dns.dnssec.verifyRrset, b.network.dns.dnssec.verifyDs
|
|
782
|
+
*
|
|
783
|
+
* Validate a DNSSEC delegation chain from the root down to a zone, against
|
|
784
|
+
* a pinned trust anchor (RFC 4035 §5). For each link, the zone's DNSKEY
|
|
785
|
+
* RRset must be self-signed by one of its keys; that signing key must be
|
|
786
|
+
* vouched for either by a pinned anchor (root) or by a DS record served by
|
|
787
|
+
* the already-trusted parent. The DS RRset itself is verified against the
|
|
788
|
+
* parent's keys, so trust flows root → TLD → zone with no gap. The default
|
|
789
|
+
* anchors are the IANA root KSKs; override with <code>opts.trustAnchors</code>.
|
|
790
|
+
*
|
|
791
|
+
* This composes <code>verifyRrset</code> + <code>verifyDs</code> + the key
|
|
792
|
+
* tag; it returns the leaf zone's trusted DNSKEY set, which the caller then
|
|
793
|
+
* passes to <code>verifyRrset</code> / <code>verifyDenial</code> for the
|
|
794
|
+
* actual answer.
|
|
795
|
+
*
|
|
796
|
+
* @opts
|
|
797
|
+
* {
|
|
798
|
+
* links: [ { // ordered root-first
|
|
799
|
+
* zone: string,
|
|
800
|
+
* dnskeys: Buffer[], // the zone's DNSKEY RRset RDATAs
|
|
801
|
+
* dnskeyRrsig: { algorithm, labels, originalTtl, expiration, inception, keyTag, signerName, signature },
|
|
802
|
+
* dsRdatas?: Buffer[], // DS RRset for this zone (served by parent; omit for root)
|
|
803
|
+
* dsRrsig?: { ... }, // RRSIG over the DS RRset (signed by parent; omit for root)
|
|
804
|
+
* } ],
|
|
805
|
+
* trustAnchors?: [ { keyTag, algorithm, digestType, digest: Buffer } ], // default IANA root
|
|
806
|
+
* at?: Date, // validity instant (default now)
|
|
807
|
+
* }
|
|
808
|
+
*
|
|
809
|
+
* @example
|
|
810
|
+
* var trusted = b.network.dns.dnssec.verifyChain({ links: [rootLink, orgLink] });
|
|
811
|
+
* // → { ok: true, zone: "org.", keys: [ ...trusted org DNSKEY rdatas ] }
|
|
812
|
+
*/
|
|
813
|
+
function verifyChain(opts) {
|
|
814
|
+
validateOpts.requireObject(opts, "dnssec.verifyChain", DnssecError);
|
|
815
|
+
validateOpts(opts, ["links", "trustAnchors", "at"], "dnssec.verifyChain");
|
|
816
|
+
if (!Array.isArray(opts.links) || opts.links.length === 0) throw new DnssecError("dnssec/bad-arg", "dnssec.verifyChain: opts.links must be a non-empty array");
|
|
817
|
+
var anchors = opts.trustAnchors !== undefined ? opts.trustAnchors : DEFAULT_ROOT_ANCHORS;
|
|
818
|
+
if (!Array.isArray(anchors) || anchors.length === 0) throw new DnssecError("dnssec/bad-arg", "dnssec.verifyChain: opts.trustAnchors must be a non-empty array");
|
|
819
|
+
|
|
820
|
+
var trustedKeys = null, path = [];
|
|
821
|
+
for (var i = 0; i < opts.links.length; i++) {
|
|
822
|
+
var link = opts.links[i];
|
|
823
|
+
if (!link || typeof link.zone !== "string" || link.zone === "") throw new DnssecError("dnssec/bad-link", "dnssec.verifyChain: links[" + i + "].zone is required");
|
|
824
|
+
if (!Array.isArray(link.dnskeys) || link.dnskeys.length === 0) throw new DnssecError("dnssec/bad-link", "dnssec.verifyChain: links[" + i + "].dnskeys must be a non-empty array");
|
|
825
|
+
if (!link.dnskeyRrsig || typeof link.dnskeyRrsig !== "object") throw new DnssecError("dnssec/bad-link", "dnssec.verifyChain: links[" + i + "].dnskeyRrsig is required");
|
|
826
|
+
|
|
827
|
+
// 1. The DNSKEY RRset is self-signed by one of its own keys (trying
|
|
828
|
+
// every key whose tag matches, since tags collide).
|
|
829
|
+
var signer = _verifyRrsetWithAnyKey(
|
|
830
|
+
{ name: link.zone, type: "DNSKEY", rdatas: link.dnskeys, at: opts.at },
|
|
831
|
+
link.dnskeyRrsig,
|
|
832
|
+
_keysByTag(link.dnskeys, link.dnskeyRrsig.keyTag),
|
|
833
|
+
"dnssec/chain-no-signing-key", "dnssec.verifyChain: no DNSKEY in '" + link.zone + "' verifies the DNSKEY RRSIG"
|
|
834
|
+
);
|
|
835
|
+
|
|
836
|
+
// 2. Establish trust in the signing key.
|
|
837
|
+
var signerTag = keyTag(signer);
|
|
838
|
+
if (i === 0) {
|
|
839
|
+
// Root: the signing key must match a pinned anchor's DS digest.
|
|
840
|
+
var matched = false;
|
|
841
|
+
for (var a = 0; a < anchors.length; a++) {
|
|
842
|
+
if (anchors[a].keyTag !== signerTag) continue;
|
|
843
|
+
try { verifyDs({ ownerName: link.zone, dnskeyRdata: signer, ds: anchors[a] }); matched = true; break; } catch (_e) { /* try the next anchor */ }
|
|
844
|
+
}
|
|
845
|
+
if (!matched) throw new DnssecError("dnssec/chain-anchor-mismatch", "dnssec.verifyChain: root DNSKEY does not match any pinned trust anchor");
|
|
846
|
+
} else {
|
|
847
|
+
// Delegation: the parent (already trusted) signed a DS RRset for this
|
|
848
|
+
// zone, and the signing KSK matches one of those DS records.
|
|
849
|
+
if (!Array.isArray(link.dsRdatas) || link.dsRdatas.length === 0 || !link.dsRrsig || typeof link.dsRrsig !== "object") {
|
|
850
|
+
throw new DnssecError("dnssec/bad-link", "dnssec.verifyChain: links[" + i + "] needs dsRdatas + dsRrsig (DS served by the parent)");
|
|
851
|
+
}
|
|
852
|
+
_verifyRrsetWithAnyKey(
|
|
853
|
+
{ name: link.zone, type: "DS", rdatas: link.dsRdatas, at: opts.at },
|
|
854
|
+
link.dsRrsig,
|
|
855
|
+
_keysByTag(trustedKeys, link.dsRrsig.keyTag),
|
|
856
|
+
"dnssec/chain-no-parent-key", "dnssec.verifyChain: no trusted parent key verifies the DS RRSIG for '" + link.zone + "'"
|
|
857
|
+
);
|
|
858
|
+
var dsMatched = false;
|
|
859
|
+
for (var d = 0; d < link.dsRdatas.length; d++) {
|
|
860
|
+
var dsObj = _parseDsRdata(_bytes(link.dsRdatas[d], "dsRdatas[" + d + "]"));
|
|
861
|
+
if (dsObj.keyTag !== signerTag) continue;
|
|
862
|
+
try { verifyDs({ ownerName: link.zone, dnskeyRdata: signer, ds: dsObj }); dsMatched = true; break; } catch (_e) { /* try the next DS */ }
|
|
863
|
+
}
|
|
864
|
+
if (!dsMatched) throw new DnssecError("dnssec/chain-ds-mismatch", "dnssec.verifyChain: the signing KSK of '" + link.zone + "' matches no parent-signed DS");
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
trustedKeys = link.dnskeys;
|
|
868
|
+
path.push(link.zone);
|
|
869
|
+
}
|
|
870
|
+
return { ok: true, zone: opts.links[opts.links.length - 1].zone, keys: trustedKeys, path: path };
|
|
871
|
+
}
|
|
872
|
+
|
|
322
873
|
module.exports = {
|
|
323
|
-
verifyRrset:
|
|
324
|
-
verifyDs:
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
874
|
+
verifyRrset: verifyRrset,
|
|
875
|
+
verifyDs: verifyDs,
|
|
876
|
+
verifyDenial: verifyDenial,
|
|
877
|
+
verifyChain: verifyChain,
|
|
878
|
+
nsec3Hash: nsec3Hash,
|
|
879
|
+
keyTag: keyTag,
|
|
880
|
+
ALGORITHMS: ALGS,
|
|
881
|
+
DEFAULT_ROOT_ANCHORS: DEFAULT_ROOT_ANCHORS,
|
|
882
|
+
DnssecError: DnssecError,
|
|
328
883
|
};
|
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:73355867-afbb-4417-a3e0-56f83b718707",
|
|
6
6
|
"version": 1,
|
|
7
7
|
"metadata": {
|
|
8
|
-
"timestamp": "2026-05-
|
|
8
|
+
"timestamp": "2026-05-25T14:43:55.314Z",
|
|
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.50",
|
|
23
23
|
"type": "application",
|
|
24
24
|
"name": "blamejs",
|
|
25
|
-
"version": "0.12.
|
|
25
|
+
"version": "0.12.50",
|
|
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.50",
|
|
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.50",
|
|
58
58
|
"dependsOn": []
|
|
59
59
|
}
|
|
60
60
|
]
|