// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The record types DNSSEC added: keys, the digests that link a child zone to //! its parent, signatures, and the two ways of proving that a name does not //! exist. //! //! These share a property nothing else in DNS has: their bytes are the input //! to a signature, so a validator must be able to reproduce them exactly. That //! is why names in them are never compressed -- a pointer in one is //! `error.CompressionForbidden`, except in the older `sig`, whose signer a //! reader still has to follow -- and why this library will not compress them //! even when there is a matching name earlier in the message. //! //! Nothing here verifies anything. `Dnskey.keyTag` computes the number that //! links a `dnskey` to the `rrsig` that used it and to the `ds` in the parent, //! which is arithmetic and not cryptography; the cryptography is somebody //! else's library, and this one's job is to hand it the right bytes. const std = @import("std"); const Writer = std.Io.Writer; const Builder = @import("../Builder.zig"); const Name = @import("../Name.zig"); const presentation = @import("../presentation.zig"); const types = @import("../types.zig"); const wire = @import("../wire.zig"); const Type = types.Type; const Decoder = wire.Decoder; pub const Error = wire.Error || error{ /// The record data is not the shape its type requires. MalformedRdata, }; /// A delegation signer: the hash of a key in the child zone, published by the /// parent. This record, and not the key itself, is what crosses the zone cut, /// which is what lets a parent delegate without holding the child's key. /// /// `cds` is the same shape: the child's proposal for what its parent should /// publish. So are `dlv` and `ta`, which hold the same digest for a key /// published somewhere other than the parent. pub const Ds = struct { /// Which key is being hashed. See `Dnskey.keyTag`. key_tag: u16, /// The signing algorithm of the key being hashed. algorithm: u8, /// Which hash was used: 1 is SHA-1, 2 is SHA-256, 4 is SHA-384. digest_type: u8, /// The hash itself, over the owner name and the key's record data. digest: []const u8, /// Reads the three fixed fields and takes the rest of the record data as /// the digest. The digest's length is not checked against `digest_type`: /// that is a meaning rule, and a digest type registered after this library /// was written has to come through. pub fn parse(d: *Decoder) Error!Ds { return .{ .key_tag = try d.int(u16), .algorithm = try d.int(u8), .digest_type = try d.int(u8), .digest = d.takeRest(), }; } test parse { var d = try wire.Decoder.init("\x30\x39\x0d\x02\xde\xad\xbe\xef"); const ds = try Ds.parse(&d); try std.testing.expectEqual(@as(u16, 12345), ds.key_tag); try std.testing.expectEqualSlices(u8, "\xde\xad\xbe\xef", ds.digest); } /// Writes the fields as they are; there is no name to compress. pub fn writeTo(self: Ds, b: *Builder) Builder.Error!void { try b.int(u16, self.key_tag); try b.int(u8, self.algorithm); try b.int(u8, self.digest_type); try b.writeAll(self.digest); } test writeTo { const ds: Ds = .{ .key_tag = 12345, .algorithm = 13, .digest_type = 2, .digest = "\xde\xad\xbe\xef" }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try ds.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x30\x39\x0d\x02\xde\xad\xbe\xef", b.buffer[12..b.pos]); } /// The fields in decimal and the digest in uppercase hex, as a zone file /// writes them. pub fn format(self: Ds, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} ", .{ self.key_tag, self.algorithm, self.digest_type }); try presentation.writeHexUpper(w, self.digest); } test format { const ds: Ds = .{ .key_tag = 12345, .algorithm = 13, .digest_type = 2, .digest = "\xde\xad\xbe\xef" }; try std.testing.expectFmt("12345 13 2 DEADBEEF", "{f}", .{ds}); } }; /// A public key for a zone. `cdnskey` is the same shape, and so is the older /// `key` it replaced. pub const Dnskey = struct { /// Bit 7 is the zone key flag and bit 15 the secure entry point flag; see /// `isZoneKey` and `isSecureEntryPoint`. Everything else is reserved. flags: u16, /// Always 3. RFC 4034 §2.1.2 kept the field and fixed the value, and a /// record with any other value here is to be ignored. protocol: u8, /// The signing algorithm: 8 is RSA/SHA-256, 13 is ECDSA P-256/SHA-256, 15 /// is Ed25519. algorithm: u8, /// The key, in whatever form the algorithm defines. key: []const u8, /// Whether this key signs the zone's records, as opposed to being present /// for some other purpose. pub fn isZoneKey(self: Dnskey) bool { return self.flags & 0x0100 != 0; } test isZoneKey { const zsk: Dnskey = .{ .flags = 256, .protocol = 3, .algorithm = 13, .key = "" }; try std.testing.expect(zsk.isZoneKey()); // A key with the bit clear is not to be used to validate the zone. const other: Dnskey = .{ .flags = 0, .protocol = 3, .algorithm = 13, .key = "" }; try std.testing.expect(!other.isZoneKey()); } /// Whether the zone's owner intends this key to be the one its parent /// points at. It is a hint and nothing more: no validator is allowed to /// rely on it, because nothing enforces it. pub fn isSecureEntryPoint(self: Dnskey) bool { return self.flags & 0x0001 != 0; } test isSecureEntryPoint { // 257 is the usual key signing key, 256 the usual zone signing key. const ksk: Dnskey = .{ .flags = 257, .protocol = 3, .algorithm = 13, .key = "" }; const zsk: Dnskey = .{ .flags = 256, .protocol = 3, .algorithm = 13, .key = "" }; try std.testing.expect(ksk.isSecureEntryPoint()); try std.testing.expect(!zsk.isSecureEntryPoint()); } /// The key tag: the number an `rrsig` and a `ds` use to say which key they /// mean. It is a checksum over the record data and not an identifier -- /// two different keys in one zone can share one, which is why a validator /// that finds two must try both. /// /// The algorithm is RFC 4034 Appendix B: add up the data as big-endian /// 16-bit words, fold the carry back in, keep sixteen bits. Algorithm 1, /// RSA/MD5, is the exception the appendix carves out, and takes the tag /// from the end of the modulus instead. pub fn keyTag(self: Dnskey) u16 { if (self.algorithm == 1) { if (self.key.len < 4) return 0; return std.mem.readInt(u16, self.key[self.key.len - 3 ..][0..2], .big); } // The first four bytes are the flags, the protocol and the algorithm, // at even, odd, even and odd positions respectively. var sum: u32 = self.flags; sum += @as(u32, self.protocol) << 8; sum += self.algorithm; for (self.key, 0..) |byte, i| { sum += if (i % 2 == 0) @as(u32, byte) << 8 else byte; } sum += (sum >> 16) & 0xffff; return @truncate(sum); } test keyTag { const zsk: Dnskey = .{ .flags = 256, .protocol = 3, .algorithm = 13, .key = "\x01\x02\x03\x04" }; const ksk: Dnskey = .{ .flags = 257, .protocol = 3, .algorithm = 13, .key = "\x01\x02\x03\x04" }; // The flags are summed with everything else, so the same key bytes // published as a key signing key get a different tag. try std.testing.expectEqual(@as(u16, 2067), zsk.keyTag()); try std.testing.expectEqual(@as(u16, 2068), ksk.keyTag()); } /// Reads the three fixed fields and takes the rest of the record data as /// the key. A `protocol` other than 3 is carried rather than refused: RFC /// 4034 says such a key is to be ignored, and ignoring it is the caller's /// decision. pub fn parse(d: *Decoder) Error!Dnskey { return .{ .flags = try d.int(u16), .protocol = try d.int(u8), .algorithm = try d.int(u8), .key = d.takeRest(), }; } test parse { var d = try wire.Decoder.init("\x01\x01\x03\x0d\x01\x02\x03\x04"); const key = try Dnskey.parse(&d); try std.testing.expect(key.isSecureEntryPoint()); try std.testing.expectEqualSlices(u8, "\x01\x02\x03\x04", key.key); } /// Writes the fields as they are; there is no name to compress. pub fn writeTo(self: Dnskey, b: *Builder) Builder.Error!void { try b.int(u16, self.flags); try b.int(u8, self.protocol); try b.int(u8, self.algorithm); try b.writeAll(self.key); } test writeTo { const key: Dnskey = .{ .flags = 257, .protocol = 3, .algorithm = 13, .key = "\x01\x02\x03\x04" }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try key.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x01\x01\x03\x0d\x01\x02\x03\x04", b.buffer[12..b.pos]); } /// The fields in decimal and the key in base64, as a zone file writes /// them. pub fn format(self: Dnskey, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} ", .{ self.flags, self.protocol, self.algorithm }); try presentation.writeBase64(w, self.key); } test format { const key: Dnskey = .{ .flags = 257, .protocol = 3, .algorithm = 13, .key = "\x01\x02\x03\x04" }; try std.testing.expectFmt("257 3 13 AQIDBA==", "{f}", .{key}); } }; /// The signature over one RRset: every record with the same owner, class and /// type, signed together, which is why a single record can never be /// authenticated on its own. /// /// `sig`, which `rrsig` replaced, is the same shape; the one difference is /// whether its signer may arrive compressed, which `parse` is told. pub const Rrsig = struct { /// Which type this signs. type_covered: Type, algorithm: u8, /// How many labels the owner name had when it was signed. Fewer than the /// name in the message means the answer came from a wildcard, and this /// field is how a validator knows to reconstruct the name that was /// actually signed. labels: u8, /// The TTL the records had in the zone, which is not necessarily the TTL /// they arrived with: a cache counts down, and the signature covers the /// original. original_ttl: u32, /// When the signature stops being valid, as seconds since the Unix epoch, /// compared using serial arithmetic (RFC 4034 §3.1.5) so that it keeps /// working past 2106. expiration: u32, /// When it starts being valid. inception: u32, /// Which key signed it. See `Dnskey.keyTag`. key_tag: u16, /// The zone the key belongs to. signer: Name, signature: []const u8, /// Reads the fixed fields and the signer, and takes the rest of the record /// data as the signature. `compression` is the type's: RFC 3597 §4 asks a /// reader to decompress a `sig` signer, and RFC 4034 §3.1.7 forbids /// compressing an `rrsig` one, so with `.forbidden` a pointer in the /// signer is `error.CompressionForbidden`. pub fn parse(d: *Decoder, compression: wire.Compression) Error!Rrsig { return .{ .type_covered = try d.enumeration(Type), .algorithm = try d.int(u8), .labels = try d.int(u8), .original_ttl = try d.int(u32), .expiration = try d.int(u32), .inception = try d.int(u32), .key_tag = try d.int(u16), .signer = try d.nameWith(compression), .signature = d.takeRest(), }; } test parse { // A message holding example.com, then an RRSIG whose signer is a // pointer back to it. const message = "\x07example\x03com\x00" ++ "\x00\x01\x0d\x02\x00\x00\x0e\x10\x65\x92\x00\x80\x65\x69\x22\x00\x30\x39" ++ "\xc0\x00" ++ "\x01\x02\x03"; const at_rdata = (try wire.Decoder.init(message)).at(13); var rrsig = at_rdata; try std.testing.expectError(error.CompressionForbidden, Rrsig.parse(&rrsig, .forbidden)); // The same bytes read as a `sig`, which predates the prohibition. var sig = at_rdata; const parsed = try Rrsig.parse(&sig, .allowed); try std.testing.expectFmt("example.com.", "{f}", .{parsed.signer}); try std.testing.expectEqualSlices(u8, "\x01\x02\x03", parsed.signature); } /// Writes the fields, with the signer never compressed -- for `sig` as /// well as `rrsig`, since a reader of either accepts it written out. pub fn writeTo(self: Rrsig, b: *Builder) Builder.Error!void { try b.int(u16, @backingInt(self.type_covered)); try b.int(u8, self.algorithm); try b.int(u8, self.labels); try b.int(u32, self.original_ttl); try b.int(u32, self.expiration); try b.int(u32, self.inception); try b.int(u16, self.key_tag); try b.name(self.signer, .uncompressed); try b.writeAll(self.signature); } test writeTo { const rrsig: Rrsig = .{ .type_covered = .a, .algorithm = 13, .labels = 2, .original_ttl = 3600, .expiration = 1704067200, .inception = 1701388800, .key_tag = 12345, .signer = Name.literal("example.com"), .signature = "\x01\x02\x03", }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); // Even with compression on and the name already in the message, the // signer is written out in full. try b.name(Name.literal("example.com"), .compressed); const start = b.pos; try rrsig.writeTo(&b); try std.testing.expectEqualSlices( u8, "\x00\x01\x0d\x02\x00\x00\x0e\x10\x65\x92\x00\x80\x65\x69\x22\x00\x30\x39" ++ "\x07example\x03com\x00" ++ "\x01\x02\x03", b.buffer[start..b.pos], ); } /// The fields as a zone file writes them: the type covered by its /// mnemonic, the two validity times as `YYYYMMDDHHmmSS` in UTC, the /// signer as an absolute name and the signature in base64. pub fn format(self: Rrsig, w: *Writer) Writer.Error!void { try w.print("{f} {d} {d} {d} ", .{ self.type_covered, self.algorithm, self.labels, self.original_ttl, }); try writeTimestamp(w, self.expiration); try w.writeByte(' '); try writeTimestamp(w, self.inception); try w.print(" {d} {f} ", .{ self.key_tag, self.signer }); try presentation.writeBase64(w, self.signature); } test format { const rrsig: Rrsig = .{ .type_covered = .mx, .algorithm = 13, .labels = 2, .original_ttl = 3600, // 2024-01-01T00:00:00Z and 2023-12-01T00:00:00Z. .expiration = 1704067200, .inception = 1701388800, .key_tag = 12345, .signer = Name.literal("example.com"), .signature = "\x01\x02\x03", }; try std.testing.expectFmt( "MX 13 2 3600 20240101000000 20231201000000 12345 example.com. AQID", "{f}", .{rrsig}, ); } }; /// The `YYYYMMDDHHmmSS` a zone file writes a signature validity time as. The /// value is unsigned seconds since 1970, so it needs no calendar arithmetic /// before the epoch and none after 2106 either -- it simply wraps, and the /// comparison that matters is a serial one. fn writeTimestamp(w: *Writer, seconds: u32) Writer.Error!void { const epoch: std.time.epoch.EpochSeconds = .{ .secs = seconds }; const day = epoch.getEpochDay(); const year_day = day.calculateYearDay(); const month_day = year_day.calculateMonthDay(); const time = epoch.getDaySeconds(); try w.print("{d:0>4}{d:0>2}{d:0>2}{d:0>2}{d:0>2}{d:0>2}", .{ year_day.year, month_day.month.numeric(), month_day.day_index + 1, time.getHoursIntoDay(), time.getMinutesIntoHour(), time.getSecondsIntoMinute(), }); } /// Which record types exist at a name, as a sparse bitmap. /// /// The encoding is a list of 256-type windows, each holding only as many bytes /// as it needs. That is what keeps an `nsec` small: a name with an `a` and an /// `rrsig` needs one window and four bytes, rather than the eight kilobytes a /// flat bitmap over the whole type space would take. pub const TypeBitmap = struct { /// The windows exactly as they appeared. bytes: []const u8, /// The empty bitmap, which claims no types at all. pub const empty: TypeBitmap = .{ .bytes = "" }; /// Reads the rest of the record data as a bitmap, checking that the /// windows are the right shape and in the increasing order RFC 4034 /// §4.1.2 requires -- the order is what lets `contains` stop early, and a /// bitmap that is out of order would answer differently depending on how /// it was read. The same section has a window end at its last nonzero /// byte, so one ending in a zero byte, or made of nothing but zeros, is /// refused too: it claims nothing a shorter window would not, and two /// bitmaps holding the same types would no longer be the same bytes. pub fn parse(d: *Decoder) Error!TypeBitmap { const bytes = d.takeRest(); var i: usize = 0; var previous: ?u8 = null; while (i < bytes.len) { if (i + 2 > bytes.len) return error.MalformedRdata; const window = bytes[i]; const length = bytes[i + 1]; if (length == 0 or length > 32) return error.MalformedRdata; if (previous) |p| if (window <= p) return error.MalformedRdata; previous = window; i += 2 + @as(usize, length); if (i > bytes.len) return error.MalformedRdata; if (bytes[i - 1] == 0) return error.MalformedRdata; } return .{ .bytes = bytes }; } test parse { // One window, number 0, of six bytes: A, MX, RRSIG and NSEC. var d = try wire.Decoder.init("\x00\x06\x40\x01\x00\x00\x00\x03"); const bitmap = try TypeBitmap.parse(&d); try std.testing.expect(bitmap.contains(.mx)); // A trailing zero byte claims nothing, and is refused. var padded = try wire.Decoder.init("\x00\x02\x40\x00"); try std.testing.expectError(error.MalformedRdata, TypeBitmap.parse(&padded)); } /// Whether a type is in the bitmap. pub fn contains(self: TypeBitmap, t: Type) bool { const value = @backingInt(t); const want_window: u8 = @intCast(value >> 8); const index_in_window: u8 = @truncate(value); var i: usize = 0; while (i + 2 <= self.bytes.len) { const window = self.bytes[i]; const length = self.bytes[i + 1]; if (window == want_window) { const byte_index = index_in_window / 8; if (byte_index >= length) return false; const byte = self.bytes[i + 2 + byte_index]; return byte & (@as(u8, 0x80) >> @truncate(index_in_window % 8)) != 0; } if (window > want_window) return false; // the windows are in order i += 2 + @as(usize, length); } return false; } test contains { var buf: [64]u8 = undefined; const bitmap = try TypeBitmap.encode(&buf, &.{ .a, .mx, @fromBackingInt(1234) }); try std.testing.expect(bitmap.contains(.mx)); // TYPE1234 is in window 4, which costs the bitmap nothing for 1 to 3. try std.testing.expect(bitmap.contains(@fromBackingInt(1234))); try std.testing.expect(!bitmap.contains(.aaaa)); } /// Every type in the bitmap, in increasing order. pub const Iterator = struct { bytes: []const u8, /// Where the current window's header starts. window: usize = 0, /// Which bit of the current window comes next. bit: u16 = 0, /// The next type, or `null` once the bitmap is exhausted. A window /// running past the end stops the walk rather than being read, which /// `parse` already refuses but a bitmap built by hand may hold. pub fn next(self: *Iterator) ?Type { while (self.window + 2 <= self.bytes.len) { const number = self.bytes[self.window]; const length = self.bytes[self.window + 1]; const bitmap = self.bytes[self.window + 2 ..]; if (length > bitmap.len) return null; while (self.bit < @as(u16, length) * 8) { const here = self.bit; self.bit += 1; const byte = bitmap[here / 8]; if (byte & (@as(u8, 0x80) >> @truncate(here % 8)) != 0) { return @fromBackingInt(@as(u16, number) << 8 | here); } } self.window += 2 + @as(usize, length); self.bit = 0; } return null; } test next { var buf: [16]u8 = undefined; const bitmap = try TypeBitmap.encode(&buf, &.{ .a, .mx }); var it = bitmap.iterator(); try std.testing.expectEqual(Type.a, it.next().?); try std.testing.expectEqual(Type.mx, it.next().?); try std.testing.expectEqual(@as(?Type, null), it.next()); } }; /// Walks the types in the bitmap, in increasing order. pub fn iterator(self: TypeBitmap) Iterator { return .{ .bytes = self.bytes }; } test iterator { var d = try wire.Decoder.init("\x00\x06\x40\x01\x00\x00\x00\x03"); const bitmap = try TypeBitmap.parse(&d); var count: usize = 0; var it = bitmap.iterator(); while (it.next()) |_| count += 1; // A, MX, RRSIG and NSEC. try std.testing.expectEqual(@as(usize, 4), count); } pub const EncodeError = error{ /// The types were not given in increasing order, which the encoding /// has no way to express. NotSorted, /// The buffer was too small. NoSpace, }; /// Builds a bitmap in `buf` from types given in increasing order, and /// returns a bitmap over what was written. A window needs two bytes plus /// as many as it takes to reach the bit for its highest type -- 34 at /// most, for type 255 of its window -- so `buf` need never be larger than /// 34 times the number of windows the types fall in. pub fn encode(buf: []u8, sorted: []const Type) EncodeError!TypeBitmap { var out: usize = 0; var window_at: ?usize = null; var current_window: u8 = 0; var previous: ?u16 = null; for (sorted) |t| { const value = @backingInt(t); if (previous) |p| if (value <= p) return error.NotSorted; previous = value; const window: u8 = @intCast(value >> 8); const index: u8 = @truncate(value); if (window_at == null or window != current_window) { if (out + 2 > buf.len) return error.NoSpace; window_at = out; current_window = window; buf[out] = window; buf[out + 1] = 0; out += 2; } const needed = @as(usize, index / 8) + 1; const header = window_at.?; while (buf[header + 1] < needed) { if (out >= buf.len) return error.NoSpace; buf[out] = 0; out += 1; buf[header + 1] += 1; } buf[header + 2 + index / 8] |= @as(u8, 0x80) >> @truncate(index % 8); } return .{ .bytes = buf[0..out] }; } test encode { var buf: [16]u8 = undefined; const bitmap = try TypeBitmap.encode(&buf, &.{ .a, .mx }); // Window 0, two bytes long: bit 1 for A, bit 15 for MX. try std.testing.expectEqualSlices(u8, "\x00\x02\x40\x01", bitmap.bytes); try std.testing.expectError(error.NotSorted, TypeBitmap.encode(&buf, &.{ .mx, .a })); } /// Writes the windows as they are. pub fn writeTo(self: TypeBitmap, b: *Builder) Builder.Error!void { try b.writeAll(self.bytes); } test writeTo { var types_buf: [16]u8 = undefined; const bitmap = try TypeBitmap.encode(&types_buf, &.{ .a, .mx }); var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try bitmap.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x00\x02\x40\x01", b.buffer[12..b.pos]); } /// The types, separated by spaces, as a zone file lists them. pub fn format(self: TypeBitmap, w: *Writer) Writer.Error!void { var it = self.iterator(); var first = true; while (it.next()) |t| { if (!first) try w.writeByte(' '); first = false; try w.print("{f}", .{t}); } } test format { var buf: [64]u8 = undefined; const bitmap = try TypeBitmap.encode(&buf, &.{ .a, .mx, @fromBackingInt(1234) }); // A type with no mnemonic is written in the RFC 3597 form. try std.testing.expectFmt("A MX TYPE1234", "{f}", .{bitmap}); } }; /// Proof that a name does not exist, by naming the gap it would have been in: /// this name exists, the next one that exists is that one, and nothing lies /// between them. /// /// The cost is that a zone can be walked: ask for a name you know is absent, /// and the denial tells you the next name that is present. `nsec3` exists /// because of that. pub const Nsec = struct { /// The next name in the zone, in canonical order. Never compressed -- the /// signature covers these bytes. next: Name, /// Which types exist at *this* name. types: TypeBitmap, /// Reads the next name, refusing a compression pointer in it with /// `error.CompressionForbidden`, then the rest of the record data as a /// bitmap, which `TypeBitmap.parse` checks. pub fn parse(d: *Decoder) Error!Nsec { return .{ .next = try d.uncompressedName(), .types = try TypeBitmap.parse(d), }; } test parse { var d = try wire.Decoder.init("\x04host\x07example\x03com\x00" ++ "\x00\x02\x40\x01"); const nsec = try Nsec.parse(&d); try std.testing.expectFmt("host.example.com.", "{f}", .{nsec.next}); try std.testing.expect(nsec.types.contains(.mx)); } /// Writes the next name in full, never compressed, and then the bitmap. pub fn writeTo(self: Nsec, b: *Builder) Builder.Error!void { try b.name(self.next, .uncompressed); try self.types.writeTo(b); } test writeTo { const nsec: Nsec = .{ .next = Name.literal("host.example.com"), .types = .{ .bytes = "\x00\x02\x40\x01" }, }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try nsec.writeTo(&b); try std.testing.expectEqualSlices( u8, "\x04host\x07example\x03com\x00" ++ "\x00\x02\x40\x01", b.buffer[12..b.pos], ); } /// The next name, absolute, and then the types it lists, which are left /// off entirely when there are none. pub fn format(self: Nsec, w: *Writer) Writer.Error!void { try w.print("{f}", .{self.next}); if (self.types.bytes.len > 0) try w.print(" {f}", .{self.types}); } test format { const nsec: Nsec = .{ .next = Name.literal("host.example.com"), .types = .{ .bytes = "\x00\x02\x40\x01" }, }; try std.testing.expectFmt("host.example.com. A MX", "{f}", .{nsec}); // With no types, nothing follows the name -- not even a space. const bare: Nsec = .{ .next = Name.literal("host.example.com"), .types = .empty }; try std.testing.expectFmt("host.example.com.", "{f}", .{bare}); } }; /// The parameters an `nsec3` chain was built with. It sits at the zone apex so /// that a resolver can hash a name the same way the signer did before it goes /// looking for the record that denies it. pub const Nsec3param = struct { /// Always 1, meaning SHA-1. hash_algorithm: u8, /// Only the opt-out bit is defined, and only in `nsec3`; here it is /// always zero. flags: u8, /// How many extra times to hash. RFC 9276 says to use zero: the iterations /// cost the server more than they cost an attacker. iterations: u16, /// Mixed into the hash. May be empty, and RFC 9276 says it should be. salt: []const u8, /// Reads the fixed fields and the salt, which is a length byte and then /// that many bytes. A salt that runs past the end is `error.Truncated`; /// nonzero flags are carried, not refused. pub fn parse(d: *Decoder) Error!Nsec3param { return .{ .hash_algorithm = try d.int(u8), .flags = try d.int(u8), .iterations = try d.int(u16), .salt = try d.characterString(), }; } test parse { // What RFC 9276 recommends: SHA-1, no extra iterations, no salt. var d = try wire.Decoder.init("\x01\x00\x00\x00\x00"); const param = try Nsec3param.parse(&d); try std.testing.expectEqual(@as(u16, 0), param.iterations); try std.testing.expectEqual(@as(usize, 0), param.salt.len); } /// Writes the fields, with the salt behind its length byte. pub fn writeTo(self: Nsec3param, b: *Builder) Builder.Error!void { try b.int(u8, self.hash_algorithm); try b.int(u8, self.flags); try b.int(u16, self.iterations); try b.characterString(self.salt); } test writeTo { const param: Nsec3param = .{ .hash_algorithm = 1, .flags = 0, .iterations = 10, .salt = "\xab\xcd" }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try param.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x01\x00\x00\x0a\x02\xab\xcd", b.buffer[12..b.pos]); } /// The fields in decimal and the salt in uppercase hex, or `-` when there /// is none. pub fn format(self: Nsec3param, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} ", .{ self.hash_algorithm, self.flags, self.iterations }); try writeSalt(w, self.salt); } test format { const salted: Nsec3param = .{ .hash_algorithm = 1, .flags = 0, .iterations = 10, .salt = "\xab\xcd" }; try std.testing.expectFmt("1 0 10 ABCD", "{f}", .{salted}); const unsalted: Nsec3param = .{ .hash_algorithm = 1, .flags = 0, .iterations = 0, .salt = "" }; try std.testing.expectFmt("1 0 0 -", "{f}", .{unsalted}); } }; fn writeSalt(w: *Writer, salt: []const u8) Writer.Error!void { // A zone file writes an absent salt as a dash, because an empty field // would be indistinguishable from a missing one. if (salt.len == 0) return w.writeByte('-'); try presentation.writeHexUpper(w, salt); } /// `nsec` over hashed names. The owner name of one of these is the hash of a /// name in the zone, and `next_hashed` is the hash of the next one, so the /// chain proves the same thing without ever naming what is in the gap. pub const Nsec3 = struct { hash_algorithm: u8, /// The lowest bit, 0x01 (bit 7 in RFC 5155's numbering), is opt-out: /// this record makes no claim about unsigned delegations between it and /// the next, which is what lets a large zone sign only the names that /// need it. flags: u8, iterations: u16, salt: []const u8, /// The hash of the next name in the zone, in hash order. Raw bytes; the /// base32hex a zone file shows is this, encoded. next_hashed: []const u8, /// Which types exist at the name this record's owner is the hash of. types: TypeBitmap, /// Whether the opt-out bit is set. pub fn isOptOut(self: Nsec3) bool { return self.flags & 0x01 != 0; } test isOptOut { const nsec3: Nsec3 = .{ .hash_algorithm = 1, .flags = 1, .iterations = 0, .salt = "", .next_hashed = "\x01\x02\x03\x04\x05", .types = .empty, }; try std.testing.expect(nsec3.isOptOut()); } /// Reads the fixed fields, the salt and the next hashed owner name, each /// behind a length byte, and then the rest of the record data as a /// bitmap. An empty next hashed owner name is a shape rule broken, and /// `error.MalformedRdata`; its length is not checked against the hash /// algorithm, which would be a meaning rule. pub fn parse(d: *Decoder) Error!Nsec3 { const hash_algorithm = try d.int(u8); const flags = try d.int(u8); const iterations = try d.int(u16); const salt = try d.characterString(); // RFC 5155 §3.1.6 gives the hash one to 255 bytes. The salt may be // empty and often is; the hash may not, since an NSEC3 with no next // name denies nothing. const next_hashed = try d.characterString(); if (next_hashed.len == 0) return error.MalformedRdata; return .{ .hash_algorithm = hash_algorithm, .flags = flags, .iterations = iterations, .salt = salt, .next_hashed = next_hashed, .types = try TypeBitmap.parse(d), }; } test parse { // No salt, a five-byte hash, and a bitmap holding A. var d = try wire.Decoder.init("\x01\x01\x00\x00\x00\x05\x01\x02\x03\x04\x05\x00\x01\x40"); const nsec3 = try Nsec3.parse(&d); try std.testing.expectEqualSlices(u8, "\x01\x02\x03\x04\x05", nsec3.next_hashed); try std.testing.expect(nsec3.types.contains(.a)); // With no next hashed owner name it denies nothing, and is refused. var empty = try wire.Decoder.init("\x01\x01\x00\x00\x00\x00\x00\x01\x40"); try std.testing.expectError(error.MalformedRdata, Nsec3.parse(&empty)); } /// Writes the fields, with the salt and the next hashed owner name each /// behind its length byte, and then the bitmap. pub fn writeTo(self: Nsec3, b: *Builder) Builder.Error!void { try b.int(u8, self.hash_algorithm); try b.int(u8, self.flags); try b.int(u16, self.iterations); try b.characterString(self.salt); try b.characterString(self.next_hashed); try self.types.writeTo(b); } test writeTo { const nsec3: Nsec3 = .{ .hash_algorithm = 1, .flags = 0, .iterations = 0, .salt = "\xab\xcd", .next_hashed = "\x01\x02\x03\x04\x05", .types = .{ .bytes = "\x00\x01\x40" }, }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try nsec3.writeTo(&b); try std.testing.expectEqualSlices( u8, "\x01\x00\x00\x00\x02\xab\xcd\x05\x01\x02\x03\x04\x05\x00\x01\x40", b.buffer[12..b.pos], ); } /// The fields in decimal, the salt in uppercase hex or `-` when there is /// none, the next hashed owner name in base32hex, and the types, which /// are left off entirely when there are none. pub fn format(self: Nsec3, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} ", .{ self.hash_algorithm, self.flags, self.iterations }); try writeSalt(w, self.salt); try w.writeByte(' '); try presentation.writeBase32Hex(w, self.next_hashed); if (self.types.bytes.len > 0) { try w.writeByte(' '); try w.print("{f}", .{self.types}); } } test format { const nsec3: Nsec3 = .{ .hash_algorithm = 1, .flags = 1, .iterations = 0, .salt = "\xab\xcd", .next_hashed = "\x01\x02\x03\x04\x05", .types = .{ .bytes = "\x00\x01\x40" }, }; try std.testing.expectFmt("1 1 0 ABCD 04106105 A", "{f}", .{nsec3}); } }; /// What a child zone wants its parent to copy up (RFC 7477). /// /// A delegation is held by the parent and the data behind it by the child, so /// changing a name server means asking somebody else to edit their zone. This /// record is the child saying, in its own zone, which of its records the /// parent should go and read -- which turns that request into something a /// program can act on. pub const Csync = struct { /// The child's `soa` serial at which this request becomes valid. A parent /// must not act on it until it has seen the child's zone reach this. serial: u32, /// Bit 0 is "immediate" and bit 1 is "soaminimum"; the rest are reserved. flags: u16, /// Which types the parent should copy: `ns`, `a`, `aaaa`. types: TypeBitmap, /// Whether the parent may act at once rather than waiting to see the /// serial. pub fn isImmediate(self: Csync) bool { return self.flags & 0x0001 != 0; } test isImmediate { const csync: Csync = .{ .serial = 42, .flags = 0x0001, .types = .empty }; try std.testing.expect(csync.isImmediate()); try std.testing.expect(!csync.isSoaMinimum()); } /// Whether the parent should hold the change for the child's `soa` /// minimum, so that a cached answer has expired before it takes effect. pub fn isSoaMinimum(self: Csync) bool { return self.flags & 0x0002 != 0; } test isSoaMinimum { const csync: Csync = .{ .serial = 42, .flags = 0x0002, .types = .empty }; try std.testing.expect(csync.isSoaMinimum()); try std.testing.expect(!csync.isImmediate()); } /// Reads the serial and the flags, then the rest of the record data as a /// bitmap, which `TypeBitmap.parse` checks. Reserved flag bits are /// carried. pub fn parse(d: *Decoder) Error!Csync { return .{ .serial = try d.int(u32), .flags = try d.int(u16), .types = try TypeBitmap.parse(d), }; } test parse { // Serial 42, immediate, and a bitmap holding NS. var d = try wire.Decoder.init("\x00\x00\x00\x2a\x00\x01\x00\x01\x20"); const csync = try Csync.parse(&d); try std.testing.expectEqual(@as(u32, 42), csync.serial); try std.testing.expect(csync.types.contains(.ns)); } /// Writes the fields and then the bitmap. pub fn writeTo(self: Csync, b: *Builder) Builder.Error!void { try b.int(u32, self.serial); try b.int(u16, self.flags); try self.types.writeTo(b); } test writeTo { const csync: Csync = .{ .serial = 42, .flags = 1, .types = .{ .bytes = "\x00\x01\x20" } }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try csync.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x00\x00\x00\x2a\x00\x01\x00\x01\x20", b.buffer[12..b.pos]); } /// The serial and the flags in decimal, then the types, which are left /// off entirely when there are none. pub fn format(self: Csync, w: *Writer) Writer.Error!void { try w.print("{d} {d}", .{ self.serial, self.flags }); if (self.types.bytes.len > 0) try w.print(" {f}", .{self.types}); } test format { const csync: Csync = .{ .serial = 42, .flags = 3, .types = .{ .bytes = "\x00\x01\x20" } }; try std.testing.expectFmt("42 3 NS", "{f}", .{csync}); const bare: Csync = .{ .serial = 42, .flags = 3, .types = .empty }; try std.testing.expectFmt("42 3", "{f}", .{bare}); } }; /// A digest over an entire zone (RFC 8976). /// /// DNSSEC signs each RRset on its own, so a resolver can verify every answer /// it is given and still never learn that a name was left out. This record /// covers the zone as a whole, which is what somebody who has just downloaded /// all of it wants -- a transfer, a root zone copy, anything served from a /// file rather than answered a question at a time. pub const Zonemd = struct { /// The `soa` serial this digest was computed at. A digest for a serial /// other than the zone's own is not a failure, it is the wrong digest. serial: u32, /// How the zone was laid out before hashing. 1 is the simple scheme, which /// is every record in canonical order. scheme: u8, /// 1 is SHA-384, 2 is SHA-512. algorithm: u8, digest: []const u8, /// The shortest digest RFC 8976 §2.2.4 allows, whatever the algorithm -- /// including the private ones, so it is not a number a registry can move. pub const min_digest_len = 12; /// Reads the fixed fields and takes the rest of the record data as the /// digest, refusing one shorter than `min_digest_len` with /// `error.MalformedRdata`. The length the algorithm implies is not /// checked: that is a meaning rule. pub fn parse(d: *Decoder) Error!Zonemd { const serial = try d.int(u32); const scheme = try d.int(u8); const algorithm = try d.int(u8); const digest = d.takeRest(); if (digest.len < min_digest_len) return error.MalformedRdata; return .{ .serial = serial, .scheme = scheme, .algorithm = algorithm, .digest = digest, }; } test parse { var d = try wire.Decoder.init("\x77\x35\x94\x01\x01\x01" ++ @as([12]u8, @splat(0xde))); const zonemd = try Zonemd.parse(&d); try std.testing.expectEqual(@as(u32, 2000000001), zonemd.serial); // Eleven bytes is one short of the least RFC 8976 allows. var short = try wire.Decoder.init("\x77\x35\x94\x01\x01\x01" ++ @as([11]u8, @splat(0xde))); try std.testing.expectError(error.MalformedRdata, Zonemd.parse(&short)); } /// Writes the fields as they are; there is no name to compress. pub fn writeTo(self: Zonemd, b: *Builder) Builder.Error!void { try b.int(u32, self.serial); try b.int(u8, self.scheme); try b.int(u8, self.algorithm); try b.writeAll(self.digest); } test writeTo { const digest: [12]u8 = @splat(0xde); const zonemd: Zonemd = .{ .serial = 2000000001, .scheme = 1, .algorithm = 1, .digest = &digest }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try zonemd.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x77\x35\x94\x01\x01\x01" ++ &digest, b.buffer[12..b.pos]); } /// The fields in decimal and the digest in uppercase hex. pub fn format(self: Zonemd, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} ", .{ self.serial, self.scheme, self.algorithm }); try presentation.writeHexUpper(w, self.digest); } test format { const zonemd: Zonemd = .{ .serial = 2000000001, .scheme = 1, .algorithm = 1, .digest = "\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef", }; try std.testing.expectFmt("2000000001 1 1 DEADBEEFDEADBEEFDEADBEEF", "{f}", .{zonemd}); } }; /// An SSH host key fingerprint, so that a client can check the key it is /// offered against the DNS rather than against whatever it remembered. pub const Sshfp = struct { /// 1 RSA, 2 DSA, 3 ECDSA, 4 Ed25519, 6 Ed448. algorithm: u8, /// 1 SHA-1, 2 SHA-256. fingerprint_type: u8, fingerprint: []const u8, /// Reads the two fixed fields and takes the rest of the record data as /// the fingerprint, whatever its length: what length `fingerprint_type` /// implies is a meaning rule. pub fn parse(d: *Decoder) Error!Sshfp { return .{ .algorithm = try d.int(u8), .fingerprint_type = try d.int(u8), .fingerprint = d.takeRest(), }; } test parse { var d = try wire.Decoder.init("\x04\x02\x12\x34\x56\x78"); const sshfp = try Sshfp.parse(&d); try std.testing.expectEqual(@as(u8, 4), sshfp.algorithm); try std.testing.expectEqualSlices(u8, "\x12\x34\x56\x78", sshfp.fingerprint); } /// Writes the fields as they are; there is no name to compress. pub fn writeTo(self: Sshfp, b: *Builder) Builder.Error!void { try b.int(u8, self.algorithm); try b.int(u8, self.fingerprint_type); try b.writeAll(self.fingerprint); } test writeTo { const sshfp: Sshfp = .{ .algorithm = 4, .fingerprint_type = 2, .fingerprint = "\x12\x34\x56\x78" }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try sshfp.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x04\x02\x12\x34\x56\x78", b.buffer[12..b.pos]); } /// The fields in decimal and the fingerprint in lowercase hex, which is /// how `ssh-keygen -r` prints it. pub fn format(self: Sshfp, w: *Writer) Writer.Error!void { try w.print("{d} {d} ", .{ self.algorithm, self.fingerprint_type }); try presentation.writeHexLower(w, self.fingerprint); } test format { const sshfp: Sshfp = .{ .algorithm = 4, .fingerprint_type = 2, .fingerprint = "\xab\xcd\xef" }; try std.testing.expectFmt("4 2 abcdef", "{f}", .{sshfp}); } }; /// A TLS certificate association: DANE. Which certificate, or which issuer, a /// service on a given port is allowed to present. `smimea` is the same shape, /// for an email address's S/MIME certificate. pub const Tlsa = struct { /// 0 and 1 constrain the certificate to one a public authority issued; 2 /// and 3 replace the public authorities entirely for this name. usage: u8, /// 0 matches the whole certificate, 1 just its public key. selector: u8, /// 0 is the data itself, 1 its SHA-256, 2 its SHA-512. matching_type: u8, data: []const u8, /// Reads the three fixed fields and takes the rest of the record data as /// the association data. Data whose length does not match what /// `matching_type` implies -- 32 bytes for SHA-256, say -- is carried /// rather than refused: that is a meaning rule, and a matching type /// registered later may imply another length. pub fn parse(d: *Decoder) Error!Tlsa { return .{ .usage = try d.int(u8), .selector = try d.int(u8), .matching_type = try d.int(u8), .data = d.takeRest(), }; } test parse { // Matching type 1 is SHA-256, but four bytes is carried all the same. var d = try wire.Decoder.init("\x03\x01\x01\x12\x34\x56\x78"); const tlsa = try Tlsa.parse(&d); try std.testing.expectEqual(@as(u8, 3), tlsa.usage); try std.testing.expectEqualSlices(u8, "\x12\x34\x56\x78", tlsa.data); } /// Writes the fields as they are; there is no name to compress. pub fn writeTo(self: Tlsa, b: *Builder) Builder.Error!void { try b.int(u8, self.usage); try b.int(u8, self.selector); try b.int(u8, self.matching_type); try b.writeAll(self.data); } test writeTo { const tlsa: Tlsa = .{ .usage = 3, .selector = 1, .matching_type = 1, .data = "\x12\x34\x56\x78" }; var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{}); try tlsa.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x03\x01\x01\x12\x34\x56\x78", b.buffer[12..b.pos]); } /// The fields in decimal and the data in lowercase hex. pub fn format(self: Tlsa, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} ", .{ self.usage, self.selector, self.matching_type }); try presentation.writeHexLower(w, self.data); } test format { const tlsa: Tlsa = .{ .usage = 3, .selector = 1, .matching_type = 1, .data = "\xab\xcd\xef" }; try std.testing.expectFmt("3 1 1 abcdef", "{f}", .{tlsa}); } }; const testing = std.testing; test "a key tag is a checksum of the record data" { // The example.com key signing key, whose tag `dig +multi` reports as // 2371 -- which is how this was checked against an implementation that is // not this one. const key = [_]u8{ 0x99, 0xdb, 0x2c, 0xc1, 0x4c, 0xab, 0xdc, 0x33, 0xd6, 0xd7, 0x7d, 0xa6, 0x3a, 0x2f, 0x15, 0xf7, 0x11, 0x12, 0x58, 0x4f, 0x23, 0x4e, 0x8d, 0x1d, 0xc4, 0x28, 0xe3, 0x9e, 0x8a, 0x4a, 0x97, 0xe1, 0xaa, 0x27, 0x1a, 0x55, 0x5d, 0xc9, 0x07, 0x01, 0xe1, 0x7e, 0x2a, 0x4c, 0x4b, 0x6f, 0x12, 0x0b, 0x7c, 0x32, 0xd4, 0x4f, 0x4a, 0xc0, 0x2b, 0xd8, 0x94, 0xcf, 0x2d, 0x4b, 0xe7, 0x77, 0x8a, 0x19, }; const ksk: Dnskey = .{ .flags = 257, .protocol = 3, .algorithm = 13, .key = &key }; try testing.expectEqual(@as(u16, 2371), ksk.keyTag()); try testing.expect(ksk.isZoneKey()); try testing.expect(ksk.isSecureEntryPoint()); // The same key published as a zone signing key is a different key, and // gets a different tag: the flags are part of what is summed. const zsk: Dnskey = .{ .flags = 256, .protocol = 3, .algorithm = 13, .key = &key }; try testing.expectEqual(@as(u16, 2370), zsk.keyTag()); try testing.expect(!zsk.isSecureEntryPoint()); } test "a type bitmap" { // The bitmap from RFC 4034 section 4.3: A, MX, RRSIG, NSEC and TYPE1234. const bytes = "\x00\x06\x40\x01\x00\x00\x00\x03" ++ "\x04\x1b" ++ @as([26]u8, @splat(0)) ++ "\x20"; var d = try wire.Decoder.init(bytes); const bitmap = try TypeBitmap.parse(&d); for ([_]Type{ .a, .mx, .rrsig, .nsec, @fromBackingInt(1234) }) |t| { try testing.expect(bitmap.contains(t)); } for ([_]Type{ .aaaa, .ns, .soa, @fromBackingInt(1235) }) |t| { try testing.expect(!bitmap.contains(t)); } try testing.expectFmt("A MX RRSIG NSEC TYPE1234", "{f}", .{bitmap}); } test "a type bitmap round trips through encode" { const wanted = [_]Type{ .a, .mx, .rrsig, .nsec, @fromBackingInt(1234) }; var buf: [64]u8 = undefined; const bitmap = try TypeBitmap.encode(&buf, &wanted); try testing.expectFmt("A MX RRSIG NSEC TYPE1234", "{f}", .{bitmap}); for (wanted) |t| try testing.expect(bitmap.contains(t)); var d = try wire.Decoder.init(bitmap.bytes); const reparsed = try TypeBitmap.parse(&d); try testing.expectEqualSlices(u8, bitmap.bytes, reparsed.bytes); try testing.expectError(error.NotSorted, TypeBitmap.encode(&buf, &.{ .mx, .a })); } test "malformed type bitmaps are refused" { for ([_][]const u8{ "\x00", // a window header cut in half "\x00\x00", // a window of no bytes "\x00\x21" ++ @as([33]u8, @splat(0)), // a window of more than 32 "\x00\x01\x40\x00\x01\x40", // windows out of order "\x00\x04\x40", // a window shorter than it claims "\x00\x02\x40\x00", // a window ending in a zero byte "\x00\x01\x00", // a window of nothing but zeros "\x00\x01\x40\x01\x02\x40\x00", // the same, in a later window }) |bytes| { var d = try wire.Decoder.init(bytes); try testing.expectError(error.MalformedRdata, TypeBitmap.parse(&d)); } } test "an RRSIG prints its validity as a timestamp" { var buf: Name.Buffer = undefined; const rrsig: Rrsig = .{ .type_covered = .a, .algorithm = 13, .labels = 2, .original_ttl = 3600, // 2024-01-01T00:00:00Z and 2023-12-01T00:00:00Z. .expiration = 1704067200, .inception = 1701388800, .key_tag = 12345, .signer = try Name.fromText(&buf, "example.com"), .signature = "\x01\x02\x03", }; try testing.expectFmt( "A 13 2 3600 20240101000000 20231201000000 12345 example.com. AQID", "{f}", .{rrsig}, ); } test "an NSEC3 with no salt prints a dash" { var d = try wire.Decoder.init("\x01\x01\x00\x00\x00\x05\x01\x02\x03\x04\x05\x00\x01\x40"); const nsec3 = try Nsec3.parse(&d); try testing.expect(nsec3.isOptOut()); try testing.expectFmt("1 1 0 - 04106105 A", "{f}", .{nsec3}); } test "an NSEC3 with no next hashed owner name is refused" { var d = try wire.Decoder.init("\x01\x01\x00\x00\x00\x00\x00\x01\x40"); try testing.expectError(error.MalformedRdata, Nsec3.parse(&d)); } test "the zone maintenance types" { var d = try wire.Decoder.init("\x00\x00\x00\x2a\x00\x03\x00\x06\x00\x00\x00\x00\x00\x03"); const csync = try Csync.parse(&d); try testing.expectEqual(@as(u32, 42), csync.serial); try testing.expect(csync.isImmediate()); try testing.expect(csync.isSoaMinimum()); try testing.expectFmt("42 3 RRSIG NSEC", "{f}", .{csync}); var z = try wire.Decoder.init("\x77\x35\x94\x01\x01\x01" ++ "\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef"); const zonemd = try Zonemd.parse(&z); try testing.expectEqual(@as(u32, 2000000001), zonemd.serial); try testing.expectFmt("2000000001 1 1 DEADBEEFDEADBEEFDEADBEEF", "{f}", .{zonemd}); // Eleven bytes is one short of the least RFC 8976 allows. var short = try wire.Decoder.init("\x77\x35\x94\x01\x01\x01" ++ @as([11]u8, @splat(0xde))); try testing.expectError(error.MalformedRdata, Zonemd.parse(&short)); }