| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- # 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)
- #
- # A separate restart-persistence check lives in run-tests.sh.
- import socket
- import struct
- import sys
- server = sys.argv[1] if len(sys.argv) > 1 else "unbound"
- port = int(sys.argv[2]) if len(sys.argv) > 2 else 5053
- 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, tries=5):
- import time
- 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)
- last = None
- for attempt in range(tries):
- if attempt:
- time.sleep(2) # 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 as e:
- last = e
- 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 last
- def check(label, name, qtype, do_bit, want_ad, want_rc=0):
- an, ad, rc = query(name, qtype, do_bit)
- 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 good
- ok = True
- # 1) signed zone, DO -> AD=1
- ok &= check("signed example.com A (DO) ", "example.com", 1, True, want_ad=1)
- # 2) signed zone, no DO -> AD=0
- ok &= check("signed example.com A (no DO)", "example.com", 1, False, want_ad=0)
- # 3) repeat signed+DO for stability -> AD=1
- ok &= check("signed example.com A (DO) ", "example.com", 1, True, want_ad=1)
- sys.exit(0 if ok else 1)
|