| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- # DNSSEC verification probe for unbound-sec.
- #
- # Builds a correct EDNS0 query. The DO (DNSSEC OK) bit lives in the OPT
- # record's 4-byte TTL/EXTFLAGS field (0x8000); the header must carry
- # arcount=1 when the OPT record is present. We then assert on the AD
- # (authenticated data) bit in the response header.
- #
- # Checks (all deterministic, no dependency on third-party zone signing):
- # 1. signed zone + DO -> NOERROR, answers, AD=1 (validation works)
- # 2. signed zone, no DO -> NOERROR, answers, AD=0 (AD only when requested)
- # 3. signed zone + DO -> NOERROR, answers, AD=1 (repeat, stability)
- #
- # Exit codes (see run-tests.sh):
- # 0 = all checks passed
- # 1 = at least one CONTENT deviation (e.g. AD bit wrong, rcode wrong)
- # 2 = no content deviation, but at least one check TIMED OUT (infrastructure)
- #
- # A separate restart-persistence + negative (SERVFAIL) check live in run-tests.sh.
- import socket
- import struct
- import sys
- import time
- server = sys.argv[1] if len(sys.argv) > 1 else "unbound"
- port = int(sys.argv[2]) if len(sys.argv) > 2 else 5053
- TRIES = 5
- BACKOFF = 2 # seconds
- class Timeout(Exception):
- pass
- def enc(name):
- out = b""
- for p in name.rstrip(".").split("."):
- out += bytes([len(p)]) + p.encode()
- return out + b"\x00"
- def query(name, qtype, do_bit):
- tid = 0x1234
- flags = 0x0100 # RD
- arcount = 1 if do_bit else 0
- header = struct.pack(">HHHHHH", tid, flags, 1, 0, 0, arcount)
- q = header + enc(name) + struct.pack(">HH", qtype, 1) # type, IN
- if do_bit:
- # OPT: root name, type=41, class=0, TTL/EXTFLAGS=DO(0x8000), rdlen=0
- q += b"\x00" + struct.pack(">HHIH", 41, 0, 0x8000, 0)
- for attempt in range(TRIES):
- if attempt:
- time.sleep(BACKOFF) # backoff between retries (WSL net hiccups)
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.settimeout(15)
- try:
- s.sendto(q, (server, port))
- data, _ = s.recvfrom(4096)
- except socket.timeout:
- continue
- finally:
- s.close()
- rf = struct.unpack(">H", data[2:4])[0]
- an = struct.unpack(">H", data[6:8])[0]
- ad = (rf >> 5) & 1
- rc = rf & 0xF
- return an, ad, rc
- raise Timeout(name)
- def check(label, name, qtype, do_bit, want_ad, want_rc=0):
- """Return 'pass' | 'fail' | 'timeout'."""
- try:
- an, ad, rc = query(name, qtype, do_bit)
- except Timeout:
- print(f"{label}: TIMEOUT (no response after {TRIES} tries)")
- return "timeout"
- good = (rc == want_rc) and (an > 0) and (ad == want_ad)
- print(f"{label}: answers={an} AD={ad} rcode={rc} -> {'PASS' if good else 'FAIL'}")
- return "pass" if good else "fail"
- results = []
- # 1) signed zone, DO -> AD=1
- results.append(check("signed example.com A (DO) ", "example.com", 1, True, want_ad=1))
- # 2) signed zone, no DO -> AD=0
- results.append(check("signed example.com A (no DO)", "example.com", 1, False, want_ad=0))
- # 3) repeat signed+DO for stability -> AD=1
- results.append(check("signed example.com A (DO) ", "example.com", 1, True, want_ad=1))
- if any(r == "fail" for r in results):
- sys.exit(1)
- if any(r == "timeout" for r in results):
- sys.exit(2)
- sys.exit(0)
|