MailSharp / MailSharp.DNS / Records / RecordDS.cs
Code · 60 lines · 2179 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60using MailSharp.DNS.Records;
using System.Security.Cryptography.X509Certificates;
using System.Text;
/*
 * http://tools.ietf.org/rfc/rfc3658.txt
 * 
2.4.  Wire Format of the DS record

   The DS (type=43) record contains these fields: key tag, algorithm,
   digest type, and the digest of a public key KEY record that is
   allowed and/or used to sign the child's apex KEY RRset.  Other keys
   MAY sign the child's apex KEY RRset.

                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           key tag             |  algorithm    |  Digest type  |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                digest  (length depends on type)               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                (SHA-1 digest is 20 bytes)                     |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

 */

namespace MailSharp.DNS.Records;

public sealed record RecordDS : DnsRecord
{
	public ushort KeyTag { get; init; }
	public byte Algorithm { get; init; }
	public byte DigestType { get; init; }
	public byte[] Digest { get; init; } = [];

	public RecordDS(RecordReader rr) : base(rr)
	{
		ushort length = rr.ReadUInt16(-2);

		KeyTag = rr.ReadUInt16();
		Algorithm = rr.ReadByte();
		DigestType = rr.ReadByte();
		Digest = rr.ReadBytes(length - 4);
	}

	public override string ToString()
	{
		// Digest wordt altijd in hex getoond (hoofdletters), zonder spaties
		string digestHex = Digest.Length == 0
			? "."
			: Convert.ToHexString(Digest);

		return $"{KeyTag} {Algorithm} {DigestType} {digestHex}";
	}
}