Commit 2d435fd115 for bind
commit 2d435fd115603e5d0877ad3c1326e928b1374d51
Author: Michal Nowak <mnowak@isc.org>
Date: Tue Oct 13 13:12:50 2026 +0000
Build SIG(0) records with dnspython's SIG rdata type
The SIG(0) tests packed the SIG rdata header with struct and parsed the
bytes back with dns.rdata.from_wire(). dnspython 2.9.0 can parse SIG
from text, so the two forged signatures are now written as
presentation-format records. The signed one is first built with an
empty signature, because SIG(0) signs the record's own rdata followed
by the message, and then completed with Rdata.replace().
Assisted-by: Claude:claude-fable-5-1
diff --git a/bin/tests/system/sig0/tests_hmac_sig0.py b/bin/tests/system/sig0/tests_hmac_sig0.py
index 0f6d070514..953ff5e795 100644
--- a/bin/tests/system/sig0/tests_hmac_sig0.py
+++ b/bin/tests/system/sig0/tests_hmac_sig0.py
@@ -9,7 +9,7 @@
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
-import struct
+import base64
import dns.flags
import dns.message
@@ -33,7 +33,6 @@ def make_sig0_query(qname_str, key_name_str, truncate_sig):
key_tag = 11622
expiration = 0x7FFFFFFF
inception = 0
- signer_name = dns.name.from_text(key_name_str)
# Precalculated HMAC
signature = bytes.fromhex(
"c5bc8e6b21adca9245d1c9e03324ff30d9fb2ef095e9212ea386161a19f5d413"
@@ -41,21 +40,13 @@ def make_sig0_query(qname_str, key_name_str, truncate_sig):
if truncate_sig:
signature = signature[0:1]
- # Construct SIG RDATA header (0=SIG(0), 163=HMACSHA256, 0=Labels)
- sig_rdata_header = struct.pack(
- "!HBBIIIH", 0, 163, 0, 0, expiration, inception, key_tag
- )
-
- sig_rdata_pre_sig = sig_rdata_header + signer_name.to_wire()
-
- # Create the SIG RR
- full_sig_rdata = sig_rdata_pre_sig + signature
- sig_rr = dns.rdata.from_wire(
+ # SIG(0) covers the whole message: type NONE, labels 0, TTL 0;
+ # algorithm 163 is HMAC-SHA256
+ sig_rr = dns.rdata.from_text(
dns.rdataclass.ANY,
dns.rdatatype.SIG,
- full_sig_rdata,
- 0,
- len(full_sig_rdata),
+ f"NONE 163 0 0 {expiration} {inception} {key_tag} {key_name_str} "
+ + base64.b64encode(signature).decode(),
)
sig_rrset = dns.rrset.from_rdata(root, 0, sig_rr)
query.additional.append(sig_rrset)
diff --git a/bin/tests/system/sig0/tests_sig0.py b/bin/tests/system/sig0/tests_sig0.py
index 155b8c87d6..fe172e718e 100644
--- a/bin/tests/system/sig0/tests_sig0.py
+++ b/bin/tests/system/sig0/tests_sig0.py
@@ -12,19 +12,19 @@
import base64
import glob
import os
-import struct
import time
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding, rsa
+import dns.dnssec
import dns.flags
import dns.message
import dns.name
-import dns.rdata
import dns.rdataclass
import dns.rdatatype
+import dns.rdtypes.ANY.SIG
import dns.renderer
import dns.rrset
@@ -83,27 +83,27 @@ def make_sig0_query(key_file, key_name_str):
inception = now - 3600
signer_name = dns.name.from_text(key_name_str)
- # Construct SIG RDATA header (0=SIG(0), 8=RSASHA256, 0=Labels)
- sig_rdata_header = struct.pack(
- "!HBBIIIH", 0, 8, 0, 0, expiration, inception, key_tag
+ # The record starts out without a signature, as its wire form is part
+ # of the data being signed.
+ unsigned_sig = dns.rdtypes.ANY.SIG.SIG(
+ dns.rdataclass.ANY,
+ dns.rdatatype.SIG,
+ type_covered=dns.rdatatype.NONE,
+ algorithm=dns.dnssec.Algorithm.RSASHA256,
+ labels=0,
+ original_ttl=0,
+ expiration=expiration,
+ inception=inception,
+ key_tag=key_tag,
+ signer=signer_name,
+ signature=b"",
)
- sig_rdata_pre_sig = sig_rdata_header + signer_name.to_wire()
-
# Sign: ( SIG RDATA sans signature ) + ( Message )
signature = private_key.sign(
- sig_rdata_pre_sig + msg_bytes, padding.PKCS1v15(), hashes.SHA256()
- )
-
- # Create the SIG RR
- full_sig_rdata = sig_rdata_pre_sig + signature
- sig_rr = dns.rdata.from_wire(
- dns.rdataclass.ANY,
- dns.rdatatype.SIG,
- full_sig_rdata,
- 0,
- len(full_sig_rdata),
+ unsigned_sig.to_wire() + msg_bytes, padding.PKCS1v15(), hashes.SHA256()
)
+ sig_rr = unsigned_sig.replace(signature=signature)
sig_rrset = dns.rrset.from_rdata(qname, 0, sig_rr)
query.additional.append(sig_rrset)
diff --git a/bin/tests/system/sig0_https/tests_sig0_https.py b/bin/tests/system/sig0_https/tests_sig0_https.py
index f7c6147ecd..9589957bd1 100644
--- a/bin/tests/system/sig0_https/tests_sig0_https.py
+++ b/bin/tests/system/sig0_https/tests_sig0_https.py
@@ -9,8 +9,8 @@
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
+import base64
import socket
-import struct
import time
from h2.connection import H2Connection
@@ -31,30 +31,15 @@ pytestmark = [isctest.mark.with_libnghttp2]
def sig0_wire():
now = int(time.time())
- # dnspython doesn't seems to have a way to put this in a higher level way,
- # so let's construct the SIG(0) rdata "manually".
- # `!` means network byte order, `H` 2 bytes, `B` 1 byte, `I` 4 bytes.
- sig_rdata = (
- struct.pack(
- "!HBBIIIH",
- 0, # 0 as SIG(0) doesn't sign a specific RR type, but the whole message.
- 8, # RSA/SHA-256 (but it could by anything for this attack)
- 0, # Labels: SIG(0) does not describe a DNS name, so this is zero
- 0, # TTL
- now + 300, # Expiration
- now - 300, # Inception
- 123, # Key tag (could also be anything for this attack)
- )
- + (dns.name.from_text("sig0.invalid.").to_wire())
- + (b"\x00" * 256) # The invalid signature
- )
-
- sig = dns.rdata.from_wire(
+ # SIG(0) signs the whole message rather than an RR type, so the covered
+ # type is NONE and the labels field is zero. The algorithm and key tag
+ # could be anything for this attack, and so could the invalid signature,
+ # which is 256 zero bytes.
+ sig = dns.rdata.from_text(
dns.rdataclass.ANY,
dns.rdatatype.SIG,
- sig_rdata,
- 0,
- len(sig_rdata),
+ f"NONE RSASHA256 0 0 {now + 300} {now - 300} 123 sig0.invalid. "
+ + base64.b64encode(bytes(256)).decode(),
)
msg = dns.message.make_query(