Commit e3dbedb2 authored by Brickner_cp's avatar Brickner_cp

DNS

parent 181b8dde
......@@ -270,6 +270,9 @@ namespace PcapDotNet.Packets.TestUtils
return new DnsResourceDataNextDomainSecure(random.NextDnsDomainName(),
((Func<DnsType>)(() => random.NextEnum<DnsType>())).GenerateArray(random.Next(100)));
case DnsType.DnsKey:
return new DnsResourceDataDnsKey(random.NextBool(), random.NextBool(), random.NextByte(), random.NextEnum<DnsAlgorithm>(), random.NextDataSegment(random.Next(100)));
default:
return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100)));
}
......
......@@ -1244,7 +1244,7 @@ namespace PcapDotNet.Packets.Dns
}
/// <summary>
/// RFC 2535.
/// RFCs 2535, 2536, 2537, 2539, 3110, 3755, 4034, 5155, 5702, 5933.
/// The key algorithm.
/// </summary>
public enum DnsAlgorithm
......@@ -5232,4 +5232,140 @@ namespace PcapDotNet.Packets.Dns
private readonly List<DnsType> _typesExist;
}
/// <summary>
/// RFC 4034.
/// <pre>
/// +-----+----------+----------+----------+--------------------+
/// | bit | 0-6 | 7 | 8-14 | 15 |
/// +-----+----------+----------+----------+--------------------+
/// | 0 | Reserved | Zone Key | Reserved | Secure Entry Point |
/// +-----+----------+----------+----------+--------------------+
/// | 16 | Protocol | Algorithm |
/// +-----+---------------------+-------------------------------+
/// | 32 | Public Key |
/// | ... | |
/// +-----+---------------------+-------------------------------+
/// </pre>
/// </summary>
[DnsTypeRegistration(Type = DnsType.DnsKey)]
public sealed class DnsResourceDataDnsKey : DnsResourceDataSimple, IEquatable<DnsResourceDataDnsKey>
{
public const byte ProtocolValue = 3;
private static class Offset
{
public const int ZoneKey = 0;
public const int SecureEntryPoint = 1;
public const int Protocol = sizeof(ushort);
public const int Algorithm = Protocol + sizeof(byte);
public const int PublicKey = Algorithm + sizeof(byte);
}
private static class Mask
{
public const byte ZoneKey = 0x01;
public const byte SecureEntryPoint = 0x01;
}
private const int ConstantPartLength = Offset.PublicKey;
public DnsResourceDataDnsKey(bool zoneKey, bool secureEntryPoint, byte protocol, DnsAlgorithm algorithm, DataSegment publicKey)
{
ZoneKey = zoneKey;
SecureEntryPoint = secureEntryPoint;
Protocol = protocol;
Algorithm = algorithm;
PublicKey = publicKey;
}
/// <summary>
/// If true, the DNSKEY record holds a DNS zone key, and the DNSKEY RR's owner name must be the name of a zone.
/// If false, then the DNSKEY record holds some other type of DNS public key and must not be used to verify RRSIGs that cover RRsets.
/// </summary>
public bool ZoneKey { get; private set; }
/// <summary>
/// RFC 3757.
/// If true, then the DNSKEY record holds a key intended for use as a secure entry point.
/// This flag is only intended to be a hint to zone signing or debugging software as to the intended use of this DNSKEY record;
/// validators must not alter their behavior during the signature validation process in any way based on the setting of this bit.
/// This also means that a DNSKEY RR with the SEP bit set would also need the Zone Key flag set in order to be able to generate signatures legally.
/// A DNSKEY RR with the SEP set and the Zone Key flag not set MUST NOT be used to verify RRSIGs that cover RRsets.
/// </summary>
public bool SecureEntryPoint { get; private set; }
/// <summary>
/// Musthave value 3, and the DNSKEY RR MUST be treated as invalid during signature verification if it is found to be some value other than 3.
/// </summary>
public byte Protocol { get; private set; }
/// <summary>
/// Identifies the public key's cryptographic algorithm and determines the format of the Public Key field.
/// </summary>
public DnsAlgorithm Algorithm { get; private set; }
/// <summary>
/// The public key material.
/// The format depends on the algorithm of the key being stored.
/// </summary>
public DataSegment PublicKey { get; private set; }
public bool Equals(DnsResourceDataDnsKey other)
{
return other != null &&
ZoneKey.Equals(other.ZoneKey) &&
SecureEntryPoint.Equals(other.SecureEntryPoint) &&
Protocol.Equals(other.Protocol) &&
Algorithm.Equals(other.Algorithm) &&
PublicKey.Equals(other.PublicKey);
}
public override bool Equals(DnsResourceData other)
{
return Equals(other as DnsResourceDataDnsKey);
}
internal DnsResourceDataDnsKey()
: this(false, false, ProtocolValue, DnsAlgorithm.None, DataSegment.Empty)
{
}
internal override int GetLength()
{
return ConstantPartLength + PublicKey.Length;
}
internal override void WriteDataSimple(byte[] buffer, int offset)
{
byte flagsByte0 = 0;
if (ZoneKey)
flagsByte0 |= Mask.ZoneKey;
buffer.Write(offset + Offset.ZoneKey, flagsByte0);
byte flagsByte1 = 0;
if (SecureEntryPoint)
flagsByte1 |= Mask.SecureEntryPoint;
buffer.Write(offset + Offset.SecureEntryPoint, flagsByte1);
buffer.Write(offset + Offset.Protocol, Protocol);
buffer.Write(offset + Offset.Algorithm, (byte)Algorithm);
PublicKey.Write(buffer, offset + Offset.PublicKey);
}
internal override DnsResourceData CreateInstance(DataSegment data)
{
if (data.Length < ConstantPartLength)
return null;
bool zoneKey = data.ReadBool(Offset.ZoneKey, Mask.ZoneKey);
bool secureEntryPoint = data.ReadBool(Offset.SecureEntryPoint, Mask.SecureEntryPoint);
byte protocol = data[Offset.Protocol];
DnsAlgorithm algorithm = (DnsAlgorithm)data[Offset.Algorithm];
DataSegment publicKey = data.SubSegment(Offset.PublicKey, data.Length - ConstantPartLength);
return new DnsResourceDataDnsKey(zoneKey, secureEntryPoint, protocol, algorithm, publicKey);
}
}
}
......@@ -342,6 +342,8 @@
/// <summary>
/// RFCs 3755, 4034.
/// DNSKEY.
/// Represents the public key.
/// Payload type: DnsResourceDataDnsKey.
/// </summary>
DnsKey = 48,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment