Skip to content

TSIG validation raises BadTime before verifying MAC (RFC 8945 §5.2) #1287

Closed

Description

@nrnvgh

I tried searching the closed issues and didn't see anything obvious which mentioned this.

Describe the bug
dns.tsig.validate() still checks the TSIG time window before verifying the MAC, but RFC 8945 changed that order. RFC 8945 §5.2 requires this check order:

  1. key
  2. MAC
  3. time values
  4. truncation policy

Under RFC 2845 some implementations checked time first, then used an unvalidated request MAC when signing a BADTIME reply (CVE-2017-3142 / CVE-2017-3143 / CVE-2017-11104). RFC 8945 §10.1 therefore treats the request MAC as invalid until verified, and requires the MAC check before the time check.

Current validate() does roughly:

  1. peer TSIG error codes
  2. time_signed / fudge (may raise BadTime)
  3. key name / algorithm
  4. MAC verify (may raise BadSignature)

So a message that is both outside the fudge window and has a bad MAC raises BadTime and never reaches MAC verification. Callers that map exceptions to RFC 8945 error replies then cannot tell BADTIME from BADSIG, and cannot know whether the request MAC was validated before signing a BADTIME response (ref: §5.3.2).

Expected behavior: verify the MAC (after the key check) before evaluating time, so a forged/invalid MAC yields BadSignature even when time_signed is also skewed.

To Reproduce

import time
import dns.message
import dns.name
import dns.rdataclass
import dns.rdatatype
import dns.rrset
import dns.tsig
from dns.rdtypes.ANY.TSIG import TSIG


SECRET = "dGVzdC1zZWNyZXQtdHNpZy1rZXktMTIzNA=="  # base64
KEY_NAME = dns.name.from_text("test-key.")
KEY = dns.tsig.Key(KEY_NAME, SECRET, algorithm=dns.tsig.HMAC_SHA256)

def forge(time_signed):
    """Build a SOA query whose TSIG has an invalid MAC and the given Time Signed."""
    query = dns.message.make_query("example.test.", dns.rdatatype.SOA)
    query.tsig = dns.rrset.from_rdata(
        KEY_NAME,
        0,
        TSIG(
            dns.rdataclass.ANY,
            dns.rdatatype.TSIG,
            dns.tsig.HMAC_SHA256,
            time_signed,
            300,  # fudge
            b"\x00" * dns.tsig.mac_sizes[dns.tsig.HMAC_SHA256],
            query.id,
            0,  # error
            b"",
        ),
    )
    return query.to_wire()


for label, time_signed in (("bad MAC, good time", int(time.time())), ("bad MAC, bad time", 0)):
    try:
        dns.message.from_wire(forge(time_signed), keyring=KEY)
        print(f"{label}: accepted (no exception)")
    except Exception as exc:
        print(f"{label}: {type(exc).__name__}")

Context (please complete the following information):

  • dnspython version: current main (dns/tsig.py validate() still raises BadTime before ctx.verify())
  • Python version: 3.12
  • OS: macOS

Activity

  1. adityaanikam commented on Aug 15, 2026

    @adityaanikam
    Contributor

    Confirmed the exact ordering against current main: the time_signed and fudge check at line 306 raises BadTime well before the key check, algorithm check, and ctx.verify() at line 313, which is the only thing that can raise BadSignature. Matches your five step trace exactly. The practical effect is narrower than it might sound, since a bad MAC with a good timestamp still reaches ctx.verify() and correctly raises BadSignature today, it's specifically the combination of a bad MAC and a bad timestamp where BadTime preempts what should have been BadSignature, exactly your second repro case.

    Since this changes which exception type callers see for that specific combination, and touches code with actual CVE history behind the ordering requirement, this feels like something that wants a maintainer's sign off on the reordering itself before a PR goes up, even though the fix is small. Given the RFC citations are this specific, is this something you'd want to submit yourself, or should it wait for someone from the project to confirm the approach first?

  2. rthalley commented on Aug 15, 2026

    @rthalley
    Owner

    I think we should follow the RFC, and that the change between BadTime and BadSignature can be handled with a release note as other than for people testing servers, who probably want us to follow the RFC, most users only care that their TSIG is not good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions