// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The bytes a SIG(0) signature is computed over (RFC 2931 §3.1), written //! down. //! //! SIG(0) is the other way to authenticate a request -- TSIG's shared secret //! replaced by a public key, which is what makes it usable between parties who //! never agreed a secret. It is the same division of labor as `tsig`: this //! file says *which bytes*, and the signing and the verifying happen wherever //! the private key is, because a message library has no business holding one. //! //! ### What gets signed //! //! §3.1, and it is two things rather than TSIG's three: //! //! 1. the SIG's own record data with the signature field **omitted** -- not //! zeroed, omitted, which is `writeVariables`; //! 2. the message as it stood before the signature was added to it, which is //! `writeMessage`. //! //! `data = RDATA | request - SIG(0)`, as the RFC writes it. //! //! ### The record itself is mostly constants //! //! §3 spends most of the SIG record on fields that mean nothing here and fixes //! them: the type covered is **zero**, which is what makes a SIG a SIG(0) //! rather than a signature over an RRset; the label count and the original TTL //! are zero; the owner name should be the root, to save the space; the class //! should be `any` and the TTL zero, because none of it is a record anybody //! will ever store. What is left carrying information is the algorithm, the //! two timestamps, the key tag and the signer's name. //! //! ### Verifying works from the bytes that arrived //! //! `coveredRdata` hands back the record data as received, less the signature, //! rather than writing the fields out again. Re-encoding would be a second //! opinion about how the signer's name should be written -- compressed or not, //! in the case it was sent in or another -- and a signature is over bytes. const std = @import("std"); const Writer = std.Io.Writer; const Builder = @import("Builder.zig"); const Header = @import("Header.zig"); const Message = @import("Message.zig"); const Name = @import("Name.zig"); const Record = @import("Record.zig"); const rdata = @import("rdata.zig"); const types = @import("types.zig"); /// The type covered, which is zero and is the whole of what makes this a /// SIG(0): a SIG covering type zero covers no RRset, so it must be covering /// the message it is in. pub const type_covered: types.Type = @fromBackingInt(0); /// The owner name §3 asks for: the root, because the field is meaningless and /// a single zero octet is the cheapest way to say nothing. pub const owner: Name = .root; /// The class §3 asks for. Like TSIG's, it is a constant standing where a class /// would be. pub const class: types.Class = .any; /// The TTL, zero: nothing may cache a signature over a message. pub const ttl: u32 = 0; /// The label count and original TTL, both zero, both meaningless for a /// signature that covers no RRset. pub const labels: u8 = 0; /// See `labels`. pub const original_ttl: u32 = 0; /// The fields of a SIG(0) that are signed and are not the message: the record /// data with the signature left out (RFC 2931 §3.1 item 1). pub const Variables = struct { /// The DNSSEC algorithm number: 13 is ECDSA P-256/SHA-256, 14 is ECDSA /// P-384/SHA-384, 15 is Ed25519. Which of them a signer can actually do is /// not a question for this file. algorithm: u8, /// When the signature stops being valid, in Unix seconds. §3.1 leaves the /// window to the signer; a few minutes either side of now is what /// everything in practice uses, since the only thing it has to survive is /// the flight of one message. expiration: u32, /// When it starts being valid. inception: u32, /// Which key signed it, as the checksum over the `key` record's data that /// RFC 4034 Appendix B defines. See `rdata.Dnskey.keyTag`. key_tag: u16, /// The name the verifier will find the `key` record at, which for a /// request signature is the signing host or entity rather than a zone. signer: Name, /// The variables of a signature that has already been parsed, for /// verifying one. pub fn of(signature: rdata.Rrsig) Variables { return .{ .algorithm = signature.algorithm, .expiration = signature.expiration, .inception = signature.inception, .key_tag = signature.key_tag, .signer = signature.signer, }; } test of { const signature: rdata.Rrsig = .{ .type_covered = type_covered, .algorithm = 15, .labels = labels, .original_ttl = original_ttl, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example"), .signature = "not a signature", }; const variables: Variables = .of(signature); try testing.expectEqual(@as(u16, 0x1234), variables.key_tag); try testing.expectFmt("host.example.", "{f}", .{variables.signer}); } }; /// The record data with the signature omitted (RFC 2931 §3.1 item 1). /// /// The signer's name goes in uncompressed, because record data that a verifier /// must be able to reconstruct cannot contain a pointer into a message it may /// no longer have -- RFC 4034 §6.2 for the DNSSEC types, and the same argument /// exactly. pub fn writeVariables(w: *Writer, variables: Variables) Writer.Error!void { try w.writeInt(u16, @backingInt(type_covered), .big); try w.writeInt(u8, variables.algorithm, .big); try w.writeInt(u8, labels, .big); try w.writeInt(u32, original_ttl, .big); try w.writeInt(u32, variables.expiration, .big); try w.writeInt(u32, variables.inception, .big); try w.writeInt(u16, variables.key_tag, .big); try variables.signer.writeTo(w); } test writeVariables { var digest: [256]u8 = undefined; var w: Writer = .fixed(&digest); try writeVariables(&w, .{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example"), }); try testing.expectEqualSlices( u8, "\x00\x00" ++ // type covered: zero, which is what makes it SIG(0) "\x0f\x00" ++ // the algorithm, then a label count of zero "\x00\x00\x00\x00" ++ // the original TTL, zero too "\x5f\x00\x00\x00\x5e\xff\xff\x00" ++ // expiration, inception "\x12\x34" ++ // the key tag "\x04host\x07example\x00", // and the signer, uncompressed w.buffered(), ); } /// The message as it stood before the signature was added (RFC 2931 §3.1 /// item 2). /// /// "before the request RR counts have been adjusted for the inclusion of the /// SIG(0)", which is two things: the additional count goes back down by one, /// and the message ends where the signature's record began. Unlike TSIG there /// is no identifier to put back -- a SIG(0) carries no original id, and a /// forwarder that rewrites the identifier breaks the signature, which is /// exactly the tampering it is there to detect. /// /// `signature` is null for a message that carries none, in which case the /// bytes are the message as it is. pub fn writeMessage(w: *Writer, message: Message, signature: ?Record) Writer.Error!void { const bytes = message.bytes; const record = signature orelse { try w.writeAll(bytes); return; }; var head: [Header.len]u8 = bytes[0..Header.len].*; const additional = std.mem.readInt(u16, head[10..12], .big); std.mem.writeInt(u16, head[10..12], additional -| 1, .big); try w.writeAll(&head); try w.writeAll(bytes[Header.len..record.start]); } test writeMessage { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0x4321, .opcode = .update }, .{}); try b.question(.{ .name = Name.literal("example.com"), .type = .soa }); const unsigned_len = b.pos; try b.record(.additional, recordSpec(.{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example"), }, "not a signature")); const message = try Message.parse(b.finish()); const found = (try find(message)).?; var digest: [512]u8 = undefined; var w: Writer = .fixed(&digest); try writeMessage(&w, message, found.record); // The message as it was before the SIG(0) went on: shorter by the record, // and with an additional count of zero again. try testing.expectEqual(@as(usize, unsigned_len), w.buffered().len); try testing.expectEqual(@as(u16, 0), (try Header.parse(w.buffered())).counts.additional); } /// The record to write, given the variables and the signature they produced. pub fn recordSpec(variables: Variables, signature: []const u8) Builder.RecordSpec { return .{ .name = owner, .class = class, .ttl = ttl, .data = .{ .sig = .{ .type_covered = type_covered, .algorithm = variables.algorithm, .labels = labels, .original_ttl = original_ttl, .expiration = variables.expiration, .inception = variables.inception, .key_tag = variables.key_tag, .signer = variables.signer, .signature = signature, } }, }; } test recordSpec { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0x4321, .opcode = .update }, .{}); try b.question(.{ .name = Name.literal("example.com"), .type = .soa }); try b.record(.additional, recordSpec(.{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example"), }, "not a signature")); // Every field §3 fixes is filled in: the root as owner, class ANY, TTL 0. const message = try Message.parse(b.finish()); var it = message.additional(); const record = (try it.next()).?; try testing.expect(record.name.isRoot()); try testing.expectEqual(types.Class.any, record.class); try testing.expectEqual(types.Type.sig, record.type); } /// The record data as it arrived, less the signature: what a verifier hashes /// rather than what it would have written. pub fn coveredRdata(record: Record, signature: rdata.Rrsig) []const u8 { const without = record.rdata.len -| signature.signature.len; return record.rdata[0..without]; } test coveredRdata { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0x4321, .opcode = .update }, .{}); try b.question(.{ .name = Name.literal("example.com"), .type = .soa }); try b.record(.additional, recordSpec(.{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example"), }, "not a signature")); const message = try Message.parse(b.finish()); const found = (try find(message)).?; // What arrived, less the signature: for a well-made SIG(0), the same bytes // `writeVariables` would write from the parsed fields. var digest: [256]u8 = undefined; var w: Writer = .fixed(&digest); try writeVariables(&w, .of(found.sig)); try testing.expectEqualSlices(u8, w.buffered(), coveredRdata(found.record, found.sig)); } pub const FindError = error{ /// A SIG(0) was found somewhere other than last in the additional section. /// Its signature covers the message up to where it begins, so one with /// records after it covers something other than the message that arrived. Sig0NotLast, } || Message.OptError; /// The SIG(0) on a message, if it has one. /// /// A `sig` record that covers a real type is a DNSSEC signature over an RRset /// and not a signature on this message, so the type covered has to be zero /// before this says yes. pub fn find(message: Message) FindError!?Found { var records = message.additional(); var last: ?Record = null; var seen_before_last = false; while (try records.next()) |record| { if (last) |previous| { if (previous.type == .sig) seen_before_last = true; } last = record; } const record = last orelse return null; if (seen_before_last) return error.Sig0NotLast; if (record.type != .sig) return null; const data = record.data() catch return error.MalformedRdata; if (data.sig.type_covered != type_covered) return null; return .{ .record = record, .sig = data.sig }; } test find { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0x4321, .opcode = .update }, .{}); try b.question(.{ .name = Name.literal("example.com"), .type = .soa }); try b.record(.additional, recordSpec(.{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example"), }, "not a signature")); const message = try Message.parse(b.finish()); const found = (try find(message)).?; try testing.expectFmt("host.example.", "{f}", .{found.sig.signer}); try testing.expectEqualSlices(u8, "not a signature", found.sig.signature); } /// A signature and the record it came from, which is what says where the /// signed part of the message ends. pub const Found = struct { record: Record, sig: rdata.Rrsig, }; const testing = std.testing; /// A message with a SIG(0) on the end of it, built the way a signer would. fn signedMessage(buffer: []u8, signer: Name, signature: []const u8) ![]u8 { var names: Name.Buffer = undefined; var b = try Builder.init(buffer, .{ .id = 0x4321, .opcode = .update }, .{}); try b.question(.{ .name = try .fromText(&names, "example.com"), .type = .soa }); try b.record(.additional, recordSpec(.{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = signer, }, signature)); return b.finish(); } test "the constants RFC 2931 §3 fixes" { try testing.expectEqual(@as(u16, 0), @backingInt(type_covered)); try testing.expect(owner.isRoot()); try testing.expectEqual(types.Class.any, class); try testing.expectEqual(@as(u32, 0), ttl); } test "the variables are the record data with the signature left out" { var buffer: [512]u8 = undefined; var written: [256]u8 = undefined; const signer = Name.literal("host.example.com."); const message = try Message.parse(try signedMessage(&buffer, signer, "not a signature")); const found = (try find(message)).?; var w: Writer = .fixed(&written); try writeVariables(&w, .of(found.sig)); // Byte for byte what arrived, less the signature -- which is the whole // point of `coveredRdata` being a slice rather than a re-encoding. try testing.expectEqualSlices(u8, coveredRdata(found.record, found.sig), w.buffered()); try testing.expectEqual( found.record.rdata.len - "not a signature".len, w.buffered().len, ); } test "the message signed is the message before the signature was added" { var buffer: [512]u8 = undefined; var written: [512]u8 = undefined; const signer = Name.literal("host.example.com."); const bytes = try signedMessage(&buffer, signer, "not a signature"); const message = try Message.parse(bytes); const found = (try find(message)).?; var w: Writer = .fixed(&written); try writeMessage(&w, message, found.record); const covered = w.buffered(); // It stops where the record began... try testing.expectEqual(@as(usize, found.record.start), covered.len); // ...the additional count is back to zero... try testing.expectEqual(@as(u16, 0), std.mem.readInt(u16, covered[10..12], .big)); // ...and the identifier is untouched, because a SIG(0) has nowhere to put // the original and a rewritten one is tampering. try testing.expectEqual(@as(u16, 0x4321), std.mem.readInt(u16, covered[0..2], .big)); // Everything between the header and the record is as it was sent. try testing.expectEqualSlices(u8, bytes[Header.len..found.record.start], covered[Header.len..]); } test "a message with no signature is signed whole" { var buffer: [512]u8 = undefined; var written: [512]u8 = undefined; var names: Name.Buffer = undefined; var b = try Builder.init(&buffer, .{ .id = 7, .opcode = .update }, .{}); try b.question(.{ .name = try .fromText(&names, "example.com"), .type = .soa }); const message = try Message.parse(b.finish()); try testing.expectEqual(@as(?Found, null), try find(message)); var w: Writer = .fixed(&written); try writeMessage(&w, message, null); try testing.expectEqualSlices(u8, message.bytes, w.buffered()); } test "a sig over a real type is a DNSSEC signature and not a SIG(0)" { var buffer: [512]u8 = undefined; var names: Name.Buffer = undefined; var b = try Builder.init(&buffer, .{ .id = 9 }, .{}); try b.question(.{ .name = try .fromText(&names, "example.com"), .type = .a }); try b.record(.additional, .{ .name = Name.literal("example.com."), .class = .in, .ttl = 3600, .data = .{ .sig = .{ .type_covered = .a, .algorithm = 15, .labels = 2, .original_ttl = 3600, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("example.com."), .signature = "not a signature", } }, }); const message = try Message.parse(b.finish()); try testing.expectEqual(@as(?Found, null), try find(message)); } test "a signature that is not last covers something other than this message" { var buffer: [512]u8 = undefined; var names: Name.Buffer = undefined; var b = try Builder.init(&buffer, .{ .id = 11, .opcode = .update }, .{}); try b.question(.{ .name = try .fromText(&names, "example.com"), .type = .soa }); try b.record(.additional, recordSpec(.{ .algorithm = 15, .expiration = 0x5f000000, .inception = 0x5effff00, .key_tag = 0x1234, .signer = Name.literal("host.example.com."), }, "not a signature")); try b.record(.additional, .{ .name = Name.literal("www.example.com."), .ttl = 3600, .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, }); const message = try Message.parse(b.finish()); try testing.expectError(error.Sig0NotLast, find(message)); }