Skip to content

Full Toolkit for Next-Level Domain Analysis

Share DNSSEC digest and wire helpers with the DS-chain tab 🔗🔑 (#111)

Felix Wotschofskyafbf1591b48c
diff --git a/lib/dnssec/algorithms.test.ts b/lib/dnssec/algorithms.test.ts
new file mode 100644
index 0000000..0a8ad04
--- /dev/null
+++ b/lib/dnssec/algorithms.test.ts
@@ -0,0 +1,16 @@
+import { describe, expect, it } from 'vitest';
+
+import { algorithmName, isWeakDigest } from './algorithms';
+
+describe('algorithm policy', () => {
+ it('names known algorithms and falls back for unknown ones', () => {
+ expect(algorithmName(8)).toBe('RSASHA256');
+ expect(algorithmName(99)).toBe('Algorithm 99');
+ });
+
+ it('flags weak digests', () => {
+ expect(isWeakDigest(1)).toBe(true); // SHA-1
+ expect(isWeakDigest(2)).toBe(false); // SHA-256
+ expect(isWeakDigest(3)).toBe(true); // GOST
+ });
+});
diff --git a/lib/dnssec/algorithms.ts b/lib/dnssec/algorithms.ts
new file mode 100644
index 0000000..87d74ec
--- /dev/null
+++ b/lib/dnssec/algorithms.ts
@@ -0,0 +1,40 @@
+// Algorithm and DS digest names, plus which digests are too weak to count
+// as a strong match. Signing-algorithm verify policy lives with the
+// signature checker and is not needed for digest-linkage.
+
+const ALGORITHM_NAMES: Record<number, string> = {
+ 1: 'RSAMD5',
+ 3: 'DSA',
+ 5: 'RSASHA1',
+ 6: 'DSA-NSEC3-SHA1',
+ 7: 'RSASHA1-NSEC3-SHA1',
+ 8: 'RSASHA256',
+ 10: 'RSASHA512',
+ 12: 'ECC-GOST',
+ 13: 'ECDSAP256SHA256',
+ 14: 'ECDSAP384SHA384',
+ 15: 'ED25519',
+ 16: 'ED448',
+};
+
+export const algorithmName = (algorithm: number): string =>
+ ALGORITHM_NAMES[algorithm] ?? `Algorithm ${algorithm}`;
+
+export const DIGEST_NAMES: Record<number, string> = {
+ 1: 'SHA-1',
+ 2: 'SHA-256',
+ 3: 'GOST R 34.11-94',
+ 4: 'SHA-384',
+};
+
+export const DIGEST_HASH_ALGOS: Record<number, string> = {
+ 1: 'sha1',
+ 2: 'sha256',
+ 4: 'sha384',
+};
+
+// DS digest algorithms no longer considered safe: SHA-1 and GOST.
+const WEAK_DIGEST_TYPES = new Set([1, 3]);
+
+export const isWeakDigest = (digestType: number): boolean =>
+ WEAK_DIGEST_TYPES.has(digestType);
diff --git a/lib/dnssec/ds-chain.ts b/lib/dnssec/ds-chain.ts
index 4a8029a..722b5ef 100644
--- a/lib/dnssec/ds-chain.ts
+++ b/lib/dnssec/ds-chain.ts
@@ -1,10 +1,17 @@
-import { createHash } from 'node:crypto';
-
import type { Answer, DnskeyData, DsData } from 'dns-packet';
import type { RecordType } from '@/lib/resolvers/base';
import { UserFacingError } from '@/lib/user-facing-error';
+import {
+ algorithmName,
+ DIGEST_HASH_ALGOS,
+ DIGEST_NAMES,
+ isWeakDigest,
+} from './algorithms';
+import { dsMatchesKey } from './ds';
+import { dnskeyKeyTag } from './wire';
+
export type DsChainVerdict = 'intact' | 'unsigned' | 'mismatch';
export type DsChainZone = {
@@ -69,80 +76,12 @@ const ROOT_ANCHORS: DsData[] = [
},
];
-const ALGORITHM_NAMES: Record<number, string> = {
- 1: 'RSAMD5',
- 3: 'DSA',
- 5: 'RSASHA1',
- 6: 'DSA-NSEC3-SHA1',
- 7: 'RSASHA1-NSEC3-SHA1',
- 8: 'RSASHA256',
- 10: 'RSASHA512',
- 12: 'ECC-GOST',
- 13: 'ECDSAP256SHA256',
- 14: 'ECDSAP384SHA384',
- 15: 'ED25519',
- 16: 'ED448',
-};
-
-export const dnssecAlgorithmName = (algorithm: number): string =>
- ALGORITHM_NAMES[algorithm] ?? `Algorithm ${algorithm}`;
+export const dnssecAlgorithmName = algorithmName;
export const dsDigestName = (digestType: number): string =>
- ({ 1: 'SHA-1', 2: 'SHA-256', 3: 'GOST R 34.11-94', 4: 'SHA-384' })[
- digestType
- ] ?? `Digest ${digestType}`;
-
-const HASH_ALGORITHMS: Record<number, string> = {
- 1: 'sha1',
- 2: 'sha256',
- 4: 'sha384',
-};
-
-const wireName = (name: string): Buffer => {
- if (name === '.') return Buffer.from([0]);
- return Buffer.concat([
- ...name
- .toLowerCase()
- .split('.')
- .flatMap((label) => [
- Buffer.from([Buffer.byteLength(label, 'ascii')]),
- Buffer.from(label, 'ascii'),
- ]),
- Buffer.from([0]),
- ]);
-};
-
-const dnskeyRdata = (key: DnskeyData): Buffer => {
- const header = Buffer.alloc(4);
- header.writeUInt16BE(key.flags, 0);
- header.writeUInt8(3, 2);
- header.writeUInt8(key.algorithm, 3);
- return Buffer.concat([header, key.key]);
-};
+ DIGEST_NAMES[digestType] ?? `Digest ${digestType}`;
-const keyTag = (key: DnskeyData): number => {
- const rdata = dnskeyRdata(key);
- // RFC 4034 Appendix B.1 has a special case for the obsolete RSAMD5 key.
- if (key.algorithm === 1) return (rdata.at(-3)! << 8) | rdata.at(-2)!;
- let sum = 0;
- for (let i = 0; i < rdata.length; i++) {
- sum += i & 1 ? rdata[i] : rdata[i] << 8;
- }
- sum += (sum >> 16) & 0xffff;
- return sum & 0xffff;
-};
-
-const dsMatchesKey = (ds: DsData, key: DnskeyData, name: string): boolean => {
- // A DS may only point at a key with the Zone Key flag (RFC 4034 section 5.2).
- if ((key.flags & 0x0100) === 0) return false;
- if (ds.keyTag !== keyTag(key) || ds.algorithm !== key.algorithm) return false;
- const hash = HASH_ALGORITHMS[ds.digestType];
- if (!hash) return false;
- return createHash(hash)
- .update(Buffer.concat([wireName(name), dnskeyRdata(key)]))
- .digest()
- .equals(ds.digest);
-};
+const keyTag = dnskeyKeyTag;
const dnskeysOf = ({ answers }: { answers: Answer[] }): DnskeyData[] =>
answers.flatMap((answer) => (answer.type === 'DNSKEY' ? [answer.data] : []));
@@ -221,7 +160,7 @@ export const resolveDsChain = async (
// broken stronger digest. With no supported digest the zone cannot be
// authenticated and counts as unsigned (RFC 4035 section 5.2).
const supported = dsRecords.flatMap((ds, i) =>
- HASH_ALGORITHMS[ds.digestType] ? [{ ds, matched: matches[i] }] : [],
+ DIGEST_HASH_ALGOS[ds.digestType] ? [{ ds, matched: matches[i] }] : [],
);
const hasStrong = supported.some(({ ds }) => ds.digestType !== 1);
const deciding = supported.filter(
@@ -251,7 +190,7 @@ export const resolveDsChain = async (
digestType: ds.digestType,
digestHex: ds.digest.toString('hex').toUpperCase(),
matched: matches[i],
- weakDigest: ds.digestType === 1 || ds.digestType === 3,
+ weakDigest: isWeakDigest(ds.digestType),
})),
status: verdict,
});
diff --git a/lib/dnssec/ds.test.ts b/lib/dnssec/ds.test.ts
new file mode 100644
index 0000000..ed41af5
--- /dev/null
+++ b/lib/dnssec/ds.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from 'vitest';
+
+import { dsDigest, dsMatchesKey } from './ds';
+import { RFC_DS, RFC_KEY } from './test-vectors';
+import { computeKeyTag, dnskeyKeyTag, dnskeyRdata } from './wire';
+
+describe('DS digest linkage', () => {
+ it('computes the RFC 4034 DS digest (SHA-1)', () => {
+ expect(
+ dsDigest('dskey.example.com', RFC_KEY, 1)?.toString('hex').toUpperCase(),
+ ).toBe('2BB183AF5F22588179A53B0A98631FAD1A292118');
+ });
+
+ it('matches a DS to its DNSKEY', () => {
+ expect(dsMatchesKey(RFC_DS, RFC_KEY, 'dskey.example.com')).toBe(true);
+ });
+
+ it('rejects a DS for the wrong owner name', () => {
+ expect(dsMatchesKey(RFC_DS, RFC_KEY, 'other.example.com')).toBe(false);
+ });
+
+ it('returns null for an unsupported digest type', () => {
+ expect(dsDigest('dskey.example.com', RFC_KEY, 99)).toBeNull();
+ });
+
+ it('matches an RSAMD5 DS using the Appendix B.1 key tag', () => {
+ const key = {
+ flags: 256,
+ algorithm: 1,
+ key: Buffer.from([0x01, 0xaa, 0xbb, 0xcc]),
+ };
+ const ds = {
+ keyTag: dnskeyKeyTag(key),
+ algorithm: 1,
+ digestType: 1,
+ digest: dsDigest('example.com', key, 1)!,
+ };
+ expect(ds.keyTag).toBe(0xaabb);
+ expect(dsMatchesKey(ds, key, 'example.com')).toBe(true);
+ expect(
+ dsMatchesKey(
+ { ...ds, keyTag: computeKeyTag(dnskeyRdata(key)) },
+ key,
+ 'example.com',
+ ),
+ ).toBe(false);
+ });
+
+ it('rejects a DNSKEY without the Zone Key flag', () => {
+ const key = { ...RFC_KEY, flags: RFC_KEY.flags & ~0x0100 };
+ expect(dsMatchesKey(RFC_DS, key, 'dskey.example.com')).toBe(false);
+ });
+
+ it('rejects a DS whose key tag does not match the DNSKEY', () => {
+ // Correct digest + algorithm but a deliberately wrong key tag: a validator
+ // would never select this key, so it must not count as a match.
+ const good = {
+ ...RFC_DS,
+ keyTag: dnskeyKeyTag(RFC_KEY),
+ digest: dsDigest('example', RFC_KEY, 1)!,
+ };
+ const tagMismatch = { ...good, keyTag: good.keyTag + 1 };
+ expect(dsMatchesKey(tagMismatch, RFC_KEY, 'example')).toBe(false);
+ });
+});
diff --git a/lib/dnssec/ds.ts b/lib/dnssec/ds.ts
new file mode 100644
index 0000000..850a5ad
--- /dev/null
+++ b/lib/dnssec/ds.ts
@@ -0,0 +1,43 @@
+import { createHash } from 'node:crypto';
+
+import type { DnskeyData, DsData } from 'dns-packet';
+
+import { DIGEST_HASH_ALGOS } from './algorithms';
+import { dnskeyKeyTag, dnskeyRdata, wireName } from './wire';
+
+// DS digest linkage (RFC 4034 §5.1.4): does a parent's DS record actually
+// hash to one of the child zone's DNSKEYs?
+
+/** DS digest of a DNSKEY: hash(ownerName || DNSKEY RDATA). Null if digest type unsupported. */
+export const dsDigest = (
+ zoneName: string,
+ key: Pick<DnskeyData, 'flags' | 'algorithm' | 'key'>,
+ digestType: number,
+): Buffer | null => {
+ const algo = DIGEST_HASH_ALGOS[digestType];
+ if (!algo) return null;
+ return createHash(algo)
+ .update(Buffer.concat([wireName(zoneName), dnskeyRdata(key)]))
+ .digest();
+};
+
+/**
+ * Whether a DS record authenticates a given DNSKEY of a zone. Following the
+ * validator selection rule (RFC 4035 §5.2), the DS must agree with the DNSKEY on
+ * algorithm and key tag before the digest is verified -- a real resolver picks
+ * candidate keys by tag/algorithm and never reaches the digest for a DS whose
+ * tag is wrong, so a malformed DS (right digest, wrong tag) is correctly treated
+ * as a non-match here too.
+ */
+export const dsMatchesKey = (
+ ds: DsData,
+ key: DnskeyData,
+ zoneName: string,
+): boolean => {
+ // RFC 4034 §5.2: a DS may only point at a Zone Key (bit 7).
+ if ((key.flags & 0x0100) === 0) return false;
+ if (ds.algorithm !== key.algorithm) return false;
+ if (ds.keyTag !== dnskeyKeyTag(key)) return false;
+ const digest = dsDigest(zoneName, key, ds.digestType);
+ return digest !== null && digest.equals(ds.digest);
+};
diff --git a/lib/dnssec/test-vectors.ts b/lib/dnssec/test-vectors.ts
new file mode 100644
index 0000000..52a61d2
--- /dev/null
+++ b/lib/dnssec/test-vectors.ts
@@ -0,0 +1,19 @@
+import type { DnskeyData, DsData } from 'dns-packet';
+
+// RFC 4034 Appendix B worked example: dskey.example.com.
+export const RFC_KEY: DnskeyData = {
+ flags: 256,
+ algorithm: 5, // RSASHA1
+ key: Buffer.from(
+ 'AQOeiiR0GOMYkDshWoSKz9XzfwJr1AYtsmx3TGkJaNXVbfi/2pHm822aJ5iI9BMz' +
+ 'NXxeYCmZDRD99WYwYqUSdjMmmAphXdvxegXd/M5+X7OrzKBaMbCVdFLUUh6DhweJ' +
+ 'BjEVv5f2wwjM9XzcnOf+EPbtG9DMBmADjFDc2w/rljwvFw==',
+ 'base64',
+ ),
+};
+export const RFC_DS: DsData = {
+ keyTag: 60485,
+ algorithm: 5,
+ digestType: 1, // SHA-1
+ digest: Buffer.from('2BB183AF5F22588179A53B0A98631FAD1A292118', 'hex'),
+};
diff --git a/lib/dnssec/wire.test.ts b/lib/dnssec/wire.test.ts
new file mode 100644
index 0000000..c813f9d
--- /dev/null
+++ b/lib/dnssec/wire.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from 'vitest';
+
+import { RFC_KEY } from './test-vectors';
+import { computeKeyTag, dnskeyKeyTag, dnskeyRdata } from './wire';
+
+describe('canonical wire encoding', () => {
+ it('computes the RFC 4034 key tag', () => {
+ expect(computeKeyTag(dnskeyRdata(RFC_KEY))).toBe(60485);
+ expect(dnskeyKeyTag(RFC_KEY)).toBe(60485);
+ });
+
+ it('uses the Appendix B.1 exception for RSAMD5', () => {
+ const key = {
+ flags: 256,
+ algorithm: 1,
+ key: Buffer.from([0x01, 0xaa, 0xbb, 0xcc]),
+ };
+ expect(dnskeyKeyTag(key)).toBe(0xaabb);
+ expect(dnskeyKeyTag(key)).not.toBe(computeKeyTag(dnskeyRdata(key)));
+ });
+});
diff --git a/lib/dnssec/wire.ts b/lib/dnssec/wire.ts
new file mode 100644
index 0000000..a1d0d99
--- /dev/null
+++ b/lib/dnssec/wire.ts
@@ -0,0 +1,47 @@
+import type { DnskeyData } from 'dns-packet';
+
+// Wire-format pieces the DS digest needs (RFC 4034 §5.1.4 / Appendix B).
+// RRSIG canonicalization stays with the signature checker.
+
+/** Canonical wire-format encoding of a domain name (lowercase, length-prefixed). */
+export const wireName = (name: string): Buffer => {
+ const clean = name.replace(/\.$/, '');
+ if (clean === '') return Buffer.from([0]); // root
+ const parts: Buffer[] = [];
+ for (const label of clean.toLowerCase().split('.')) {
+ const labelBuf = Buffer.from(label, 'ascii');
+ parts.push(Buffer.from([labelBuf.length]), labelBuf);
+ }
+ parts.push(Buffer.from([0]));
+ return Buffer.concat(parts);
+};
+
+/** DNSKEY RDATA wire format: flags(2) | protocol(1, always 3) | algorithm(1) | publicKey. */
+export const dnskeyRdata = (
+ key: Pick<DnskeyData, 'flags' | 'algorithm' | 'key'>,
+): Buffer => {
+ const head = Buffer.alloc(4);
+ head.writeUInt16BE(key.flags, 0);
+ head.writeUInt8(3, 2);
+ head.writeUInt8(key.algorithm, 3);
+ return Buffer.concat([head, key.key]);
+};
+
+/** Key tag computation per RFC 4034 Appendix B (general case). */
+export const computeKeyTag = (rdata: Buffer): number => {
+ let ac = 0;
+ for (let i = 0; i < rdata.length; i++) {
+ ac += i & 1 ? rdata[i] : rdata[i] << 8;
+ }
+ ac += (ac >> 16) & 0xffff;
+ return ac & 0xffff;
+};
+
+/** Key tag of a DNSKEY, including the RSAMD5 exception (RFC 4034 Appendix B.1). */
+export const dnskeyKeyTag = (
+ key: Pick<DnskeyData, 'flags' | 'algorithm' | 'key'>,
+): number => {
+ const rdata = dnskeyRdata(key);
+ if (key.algorithm === 1) return (rdata.at(-3)! << 8) | rdata.at(-2)!;
+ return computeKeyTag(rdata);
+};