Commit db7acd85 authored by Brickner_cp's avatar Brickner_cp

DNS

parent 1aa99597
...@@ -264,7 +264,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -264,7 +264,7 @@ namespace PcapDotNet.Packets.TestUtils
random.NextDataSegment(random.Next(20))); random.NextDataSegment(random.Next(20)));
case DnsType.IpSecKey: case DnsType.IpSecKey:
return new DnsResourceDataIpSecKey(random.NextByte(), random.NextDnsGateway(), random.NextEnum<DnsGatewayPublicKeyAlgorithm>(), return new DnsResourceDataIpSecKey(random.NextByte(), random.NextDnsGateway(), random.NextEnum<DnsPublicKeyAlgorithm>(),
random.NextDataSegment(random.Next(100))); random.NextDataSegment(random.Next(100)));
case DnsType.NSec: case DnsType.NSec:
...@@ -282,6 +282,11 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -282,6 +282,11 @@ namespace PcapDotNet.Packets.TestUtils
return new DnsResourceDataNextDomainSecure3Parameters(random.NextEnum<DnsSecNSec3HashAlgorithm>(), random.NextFlags<DnsSecNSec3Flags>(), return new DnsResourceDataNextDomainSecure3Parameters(random.NextEnum<DnsSecNSec3HashAlgorithm>(), random.NextFlags<DnsSecNSec3Flags>(),
random.NextUShort(), random.NextDataSegment(random.Next(10))); random.NextUShort(), random.NextDataSegment(random.Next(10)));
case DnsType.Hip:
return new DnsResourceDataHostIdentityProtocol(random.NextDataSegment(random.NextInt(0, 100)), random.NextEnum<DnsPublicKeyAlgorithm>(),
random.NextDataSegment(random.NextInt(0, 100)),
((Func<DnsDomainName>)(() => random.NextDnsDomainName())).GenerateArray(random.NextInt(0, 10)));
default: default:
return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100))); return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100)));
} }
......
...@@ -4916,7 +4916,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -4916,7 +4916,7 @@ namespace PcapDotNet.Packets.Dns
/// <summary> /// <summary>
/// Identifies the public key's cryptographic algorithm and determines the format of the public key field. /// Identifies the public key's cryptographic algorithm and determines the format of the public key field.
/// </summary> /// </summary>
public enum DnsGatewayPublicKeyAlgorithm : byte public enum DnsPublicKeyAlgorithm : byte
{ {
/// <summary> /// <summary>
/// Indicates that no key is present. /// Indicates that no key is present.
...@@ -4971,7 +4971,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -4971,7 +4971,7 @@ namespace PcapDotNet.Packets.Dns
public const int ConstPartLength = Offset.Gateway; public const int ConstPartLength = Offset.Gateway;
public DnsResourceDataIpSecKey(byte precedence, DnsGateway gateway, DnsGatewayPublicKeyAlgorithm algorithm, DataSegment publicKey) public DnsResourceDataIpSecKey(byte precedence, DnsGateway gateway, DnsPublicKeyAlgorithm algorithm, DataSegment publicKey)
{ {
Precedence = precedence; Precedence = precedence;
Gateway = gateway; Gateway = gateway;
...@@ -4999,7 +4999,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -4999,7 +4999,7 @@ namespace PcapDotNet.Packets.Dns
/// <summary> /// <summary>
/// Identifies the public key's cryptographic algorithm and determines the format of the public key field. /// Identifies the public key's cryptographic algorithm and determines the format of the public key field.
/// </summary> /// </summary>
public DnsGatewayPublicKeyAlgorithm Algorithm { get; private set;} public DnsPublicKeyAlgorithm Algorithm { get; private set;}
/// <summary> /// <summary>
/// Contains the algorithm-specific portion of the KEY RR RDATA. /// Contains the algorithm-specific portion of the KEY RR RDATA.
...@@ -5021,7 +5021,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -5021,7 +5021,7 @@ namespace PcapDotNet.Packets.Dns
} }
internal DnsResourceDataIpSecKey() internal DnsResourceDataIpSecKey()
: this(0, DnsGateway.None, DnsGatewayPublicKeyAlgorithm.None, DataSegment.Empty) : this(0, DnsGateway.None, DnsPublicKeyAlgorithm.None, DataSegment.Empty)
{ {
} }
...@@ -5048,7 +5048,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -5048,7 +5048,7 @@ namespace PcapDotNet.Packets.Dns
byte precedence = dns[offsetInDns + Offset.Precedence]; byte precedence = dns[offsetInDns + Offset.Precedence];
DnsGatewayType gatewayType = (DnsGatewayType)dns[offsetInDns + Offset.GatewayType]; DnsGatewayType gatewayType = (DnsGatewayType)dns[offsetInDns + Offset.GatewayType];
DnsGatewayPublicKeyAlgorithm algorithm = (DnsGatewayPublicKeyAlgorithm)dns[offsetInDns + Offset.Algorithm]; DnsPublicKeyAlgorithm algorithm = (DnsPublicKeyAlgorithm)dns[offsetInDns + Offset.Algorithm];
DnsGateway gateway = DnsGateway.CreateInstance(gatewayType, dns, offsetInDns + Offset.Gateway, length - ConstPartLength); DnsGateway gateway = DnsGateway.CreateInstance(gatewayType, dns, offsetInDns + Offset.Gateway, length - ConstPartLength);
if (gateway == null) if (gateway == null)
return null; return null;
...@@ -5583,8 +5583,6 @@ namespace PcapDotNet.Packets.Dns ...@@ -5583,8 +5583,6 @@ namespace PcapDotNet.Packets.Dns
[DnsTypeRegistration(Type = DnsType.NSec3)] [DnsTypeRegistration(Type = DnsType.NSec3)]
public sealed class DnsResourceDataNextDomainSecure3 : DnsResourceDataNextDomainSecure3Base, IEquatable<DnsResourceDataNextDomainSecure3> public sealed class DnsResourceDataNextDomainSecure3 : DnsResourceDataNextDomainSecure3Base, IEquatable<DnsResourceDataNextDomainSecure3>
{ {
//private const int ConstantPartLength = Offset.Salt + sizeof(byte);
public DnsResourceDataNextDomainSecure3(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt, public DnsResourceDataNextDomainSecure3(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt,
DataSegment nextHashedOwnerName, IEnumerable<DnsType> existTypes) DataSegment nextHashedOwnerName, IEnumerable<DnsType> existTypes)
: this(hashAlgorithm, flags, iterations, salt, nextHashedOwnerName, new DnsTypeBitmaps(existTypes)) : this(hashAlgorithm, flags, iterations, salt, nextHashedOwnerName, new DnsTypeBitmaps(existTypes))
...@@ -5743,4 +5741,150 @@ namespace PcapDotNet.Packets.Dns ...@@ -5743,4 +5741,150 @@ namespace PcapDotNet.Packets.Dns
return new DnsResourceDataNextDomainSecure3Parameters(hashAlgorithm, flags, iterations, salt); return new DnsResourceDataNextDomainSecure3Parameters(hashAlgorithm, flags, iterations, salt);
} }
} }
/// <summary>
/// RFC 5205.
/// <pre>
/// +-----+------------+--------------+-----------+
/// | bit | 0-7 | 8-15 | 16-31 |
/// +-----+------------+--------------+-----------+
/// | 0 | HIT Length | PK Algorithm | PK Length |
/// +-----+------------+--------------+-----------+
/// | 32 | HIT |
/// | ... | |
/// +-----+---------------------------------------+
/// | | Public Key |
/// | ... | |
/// +-----+---------------------------------------+
/// | | Rendezvous Servers |
/// | ... | |
/// +-----+---------------------------------------+
/// </pre>
/// </summary>
[DnsTypeRegistration(Type = DnsType.Hip)]
public sealed class DnsResourceDataHostIdentityProtocol: DnsResourceDataNoCompression, IEquatable<DnsResourceDataHostIdentityProtocol>
{
private static class Offset
{
public const int HostIdentityTagLength = 0;
public const int PublicKeyAlgorithm = HostIdentityTagLength + sizeof(byte);
public const int PublicKeyLength = PublicKeyAlgorithm + sizeof(byte);
public const int HostIdentityTag = PublicKeyLength + sizeof(ushort);
}
private const int ConstantPartLength = Offset.HostIdentityTag;
public DnsResourceDataHostIdentityProtocol(DataSegment hostIdentityTag, DnsPublicKeyAlgorithm publicKeyAlgorithm, DataSegment publicKey,
IEnumerable<DnsDomainName> rendezvousServers)
{
if (hostIdentityTag.Length > byte.MaxValue)
throw new ArgumentOutOfRangeException("hostIdentityTag", hostIdentityTag.Length, string.Format("Cannot be bigger than {0}.", byte.MaxValue));
if (hostIdentityTag.Length > ushort.MaxValue)
throw new ArgumentOutOfRangeException("publicKey", publicKey.Length, string.Format("Cannot be bigger than {0}.", ushort.MaxValue));
HostIdentityTag = hostIdentityTag;
PublicKeyAlgorithm = publicKeyAlgorithm;
PublicKey = publicKey;
RendezvousServers = rendezvousServers.ToArray().AsReadOnly();
}
/// <summary>
/// Stored as a binary value in network byte order.
/// </summary>
public DataSegment HostIdentityTag { get; private set; }
/// <summary>
/// Identifies the public key's cryptographic algorithm and determines the format of the public key field.
/// </summary>
public DnsPublicKeyAlgorithm PublicKeyAlgorithm { get; private set; }
/// <summary>
/// Contains the algorithm-specific portion of the KEY RR RDATA.
/// </summary>
public DataSegment PublicKey { get; private set; }
/// <summary>
/// Indicates one or more domain names of rendezvous server(s).
/// Must not be compressed.
/// The rendezvous server(s) are listed in order of preference (i.e., first rendezvous server(s) are preferred),
/// defining an implicit order amongst rendezvous servers of a single RR.
/// When multiple HIP RRs are present at the same owner name,
/// this implicit order of rendezvous servers within an RR must not be used to infer a preference order between rendezvous servers stored in different RRs.
/// </summary>
public ReadOnlyCollection<DnsDomainName> RendezvousServers { get; private set; }
public bool Equals(DnsResourceDataHostIdentityProtocol other)
{
return other != null &&
HostIdentityTag.Equals(other.HostIdentityTag) &&
PublicKeyAlgorithm.Equals(other.PublicKeyAlgorithm) &&
PublicKey.Equals(other.PublicKey) &&
RendezvousServers.SequenceEqual(RendezvousServers);
}
public override bool Equals(DnsResourceData other)
{
return Equals(other as DnsResourceDataHostIdentityProtocol);
}
internal DnsResourceDataHostIdentityProtocol()
: this(DataSegment.Empty, DnsPublicKeyAlgorithm.None, DataSegment.Empty, new DnsDomainName[0])
{
}
internal override int GetLength()
{
return ConstantPartLength + HostIdentityTag.Length + PublicKey.Length + RendezvousServers.Sum(rendezvousServer => rendezvousServer.NonCompressedLength);
}
internal override int WriteData(byte[] buffer, int offset)
{
buffer.Write(offset + Offset.HostIdentityTagLength, (byte)HostIdentityTag.Length);
buffer.Write(offset + Offset.PublicKeyAlgorithm, (byte)PublicKeyAlgorithm);
buffer.Write(offset + Offset.PublicKeyLength, (ushort)PublicKey.Length, Endianity.Big);
HostIdentityTag.Write(buffer, offset + Offset.HostIdentityTag);
int numBytesWritten = ConstantPartLength + HostIdentityTag.Length;
PublicKey.Write(buffer, offset + numBytesWritten);
numBytesWritten += PublicKey.Length;
foreach (DnsDomainName rendezvousServer in RendezvousServers)
{
rendezvousServer.WriteUncompressed(buffer, offset + numBytesWritten);
numBytesWritten += rendezvousServer.NonCompressedLength;
}
return numBytesWritten;
}
internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
{
if (length < ConstantPartLength)
return null;
int hostIdentityTagLength = dns[offsetInDns + Offset.HostIdentityTagLength];
DnsPublicKeyAlgorithm publicKeyAlgorithm = (DnsPublicKeyAlgorithm)dns[offsetInDns + Offset.PublicKeyAlgorithm];
int publicKeyLength = dns.ReadUShort(offsetInDns + Offset.PublicKeyLength, Endianity.Big);
if (length < ConstantPartLength + hostIdentityTagLength + publicKeyLength)
return null;
DataSegment hostIdentityTag = dns.SubSegment(offsetInDns + Offset.HostIdentityTag, hostIdentityTagLength);
int publicKeyOffset = offsetInDns + ConstantPartLength + hostIdentityTagLength;
DataSegment publicKey = dns.SubSegment(publicKeyOffset, publicKeyLength);
offsetInDns += ConstantPartLength + hostIdentityTagLength + publicKeyLength;
length -= ConstantPartLength + hostIdentityTagLength + publicKeyLength;
List<DnsDomainName> rendezvousServers = new List<DnsDomainName>();
while (length != 0)
{
DnsDomainName rendezvousServer;
int rendezvousServerLength;
if (!DnsDomainName.TryParse(dns, offsetInDns, length, out rendezvousServer, out rendezvousServerLength))
return null;
rendezvousServers.Add(rendezvousServer);
offsetInDns += rendezvousServerLength;
length -= rendezvousServerLength;
}
return new DnsResourceDataHostIdentityProtocol(hostIdentityTag, publicKeyAlgorithm, publicKey, rendezvousServers);
}
}
} }
...@@ -372,6 +372,7 @@ ...@@ -372,6 +372,7 @@
/// <summary> /// <summary>
/// RFC 5205. /// RFC 5205.
/// Host Identity Protocol. /// Host Identity Protocol.
/// Payload type: DnsResourceDataHostIdentityProtocol.
/// </summary> /// </summary>
Hip = 55, Hip = 55,
......
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