Commit 1aa99597 authored by Brickner_cp's avatar Brickner_cp

DNS

parent c79cf844
...@@ -268,8 +268,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -268,8 +268,7 @@ namespace PcapDotNet.Packets.TestUtils
random.NextDataSegment(random.Next(100))); random.NextDataSegment(random.Next(100)));
case DnsType.NSec: case DnsType.NSec:
return new DnsResourceDataNextDomainSecure(random.NextDnsDomainName(), random.NextDnsTypeArray(random.Next(100)) return new DnsResourceDataNextDomainSecure(random.NextDnsDomainName(), random.NextDnsTypeArray(random.Next(100)));
);
case DnsType.DnsKey: case DnsType.DnsKey:
return new DnsResourceDataDnsKey(random.NextBool(), random.NextBool(), random.NextByte(), random.NextEnum<DnsAlgorithm>(), random.NextDataSegment(random.Next(100))); return new DnsResourceDataDnsKey(random.NextBool(), random.NextBool(), random.NextByte(), random.NextEnum<DnsAlgorithm>(), random.NextDataSegment(random.Next(100)));
...@@ -279,6 +278,10 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -279,6 +278,10 @@ namespace PcapDotNet.Packets.TestUtils
random.NextUShort(), random.NextDataSegment(random.Next(10)), random.NextDataSegment(10), random.NextUShort(), random.NextDataSegment(random.Next(10)), random.NextDataSegment(10),
random.NextDnsTypeArray(random.Next(100))); random.NextDnsTypeArray(random.Next(100)));
case DnsType.NSec3Param:
return new DnsResourceDataNextDomainSecure3Parameters(random.NextEnum<DnsSecNSec3HashAlgorithm>(), random.NextFlags<DnsSecNSec3Flags>(),
random.NextUShort(), random.NextDataSegment(random.Next(10)));
default: default:
return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100))); return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100)));
} }
......
...@@ -5459,18 +5459,10 @@ namespace PcapDotNet.Packets.Dns ...@@ -5459,18 +5459,10 @@ namespace PcapDotNet.Packets.Dns
/// | 32 | Salt Length | Salt | /// | 32 | Salt Length | Salt |
/// +-----+-------------+ | /// +-----+-------------+ |
/// | ... | | /// | ... | |
/// +-----+-------------+--------------------------------+
/// | | Hash Length | Next Hashed Owner Name |
/// +-----+-------------+ |
/// | ... | |
/// +-----+----------------------------------------------+ /// +-----+----------------------------------------------+
/// | | Type Bit Maps | /// | ... | ... |
/// | ... | |
/// +-----+----------------------------------------------+ /// +-----+----------------------------------------------+
/// </pre> public abstract class DnsResourceDataNextDomainSecure3Base : DnsResourceDataSimple
/// </summary>
[DnsTypeRegistration(Type = DnsType.NSec3)]
public sealed class DnsResourceDataNextDomainSecure3 : DnsResourceDataSimple, IEquatable<DnsResourceDataNextDomainSecure3>
{ {
private static class Offset private static class Offset
{ {
...@@ -5481,13 +5473,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -5481,13 +5473,7 @@ namespace PcapDotNet.Packets.Dns
public const int Salt = SaltLength + sizeof(byte); public const int Salt = SaltLength + sizeof(byte);
} }
private const int ConstantPartLength = Offset.Salt + sizeof(byte); private const int ConstantPartLength = Offset.Salt;
public DnsResourceDataNextDomainSecure3(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt,
DataSegment nextHashedOwnerName, IEnumerable<DnsType> existTypes)
: this(hashAlgorithm, flags, iterations, salt, nextHashedOwnerName, new DnsTypeBitmaps(existTypes))
{
}
/// <summary> /// <summary>
/// Identifies the cryptographic hash algorithm used to construct the hash-value. /// Identifies the cryptographic hash algorithm used to construct the hash-value.
...@@ -5511,6 +5497,100 @@ namespace PcapDotNet.Packets.Dns ...@@ -5511,6 +5497,100 @@ namespace PcapDotNet.Packets.Dns
/// </summary> /// </summary>
public DataSegment Salt { get; private set; } public DataSegment Salt { get; private set; }
internal bool EqualsParameters(DnsResourceDataNextDomainSecure3Base other)
{
return other != null &&
HashAlgorithm.Equals(other.HashAlgorithm) &&
Flags.Equals(other.Flags) &&
Iterations.Equals(other.Iterations) &&
Salt.Equals(other.Salt);
}
internal DnsResourceDataNextDomainSecure3Base(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt)
{
if (salt.Length > byte.MaxValue)
throw new ArgumentOutOfRangeException("salt", salt.Length, string.Format("Cannot bigger than {0}.", byte.MaxValue));
HashAlgorithm = hashAlgorithm;
Flags = flags;
Iterations = iterations;
Salt = salt;
}
internal int ParametersLength { get { return GetParametersLength(Salt.Length); } }
internal static int GetParametersLength(int saltLength)
{
return ConstantPartLength + saltLength;
}
internal void WriteParameters(byte[] buffer, int offset)
{
buffer.Write(offset + Offset.HashAlgorithm, (byte)HashAlgorithm);
buffer.Write(offset + Offset.Flags, (byte)Flags);
buffer.Write(offset + Offset.Iterations, Iterations, Endianity.Big);
buffer.Write(offset + Offset.SaltLength, (byte)Salt.Length);
Salt.Write(buffer, offset + Offset.Salt);
}
internal static bool TryReadParameters(DataSegment data, out DnsSecNSec3HashAlgorithm hashAlgorithm, out DnsSecNSec3Flags flags, out ushort iterations, out DataSegment salt)
{
if (data.Length < ConstantPartLength)
{
hashAlgorithm = DnsSecNSec3HashAlgorithm.Sha1;
flags = DnsSecNSec3Flags.None;
iterations = 0;
salt = null;
return false;
}
hashAlgorithm = (DnsSecNSec3HashAlgorithm)data[Offset.HashAlgorithm];
flags = (DnsSecNSec3Flags)data[Offset.Flags];
iterations = data.ReadUShort(Offset.Iterations, Endianity.Big);
int saltLength = data[Offset.SaltLength];
if (data.Length - Offset.Salt < saltLength)
{
salt = null;
return false;
}
salt = data.SubSegment(Offset.Salt, saltLength);
return true;
}
}
/// <summary>
/// RFC 5155.
/// <pre>
/// +-----+-------------+----------+--------+------------+
/// | bit | 0-7 | 8-14 | 15 | 16-31 |
/// +-----+-------------+----------+--------+------------+
/// | 0 | Hash Alg | Reserved | OptOut | Iterations |
/// +-----+-------------+----------+--------+------------+
/// | 32 | Salt Length | Salt |
/// +-----+-------------+ |
/// | ... | |
/// +-----+-------------+--------------------------------+
/// | | Hash Length | Next Hashed Owner Name |
/// +-----+-------------+ |
/// | ... | |
/// +-----+----------------------------------------------+
/// | | Type Bit Maps |
/// | ... | |
/// +-----+----------------------------------------------+
/// </pre>
/// </summary>
[DnsTypeRegistration(Type = DnsType.NSec3)]
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,
DataSegment nextHashedOwnerName, IEnumerable<DnsType> existTypes)
: this(hashAlgorithm, flags, iterations, salt, nextHashedOwnerName, new DnsTypeBitmaps(existTypes))
{
}
/// <summary> /// <summary>
/// Contains the next hashed owner name in hash order. /// Contains the next hashed owner name in hash order.
/// This value is in binary format. /// This value is in binary format.
...@@ -5527,11 +5607,7 @@ namespace PcapDotNet.Packets.Dns ...@@ -5527,11 +5607,7 @@ namespace PcapDotNet.Packets.Dns
public bool Equals(DnsResourceDataNextDomainSecure3 other) public bool Equals(DnsResourceDataNextDomainSecure3 other)
{ {
return other != null && return EqualsParameters(other) &&
HashAlgorithm.Equals(other.HashAlgorithm) &&
Flags.Equals(other.Flags) &&
Iterations.Equals(other.Iterations) &&
Salt.Equals(other.Salt) &&
NextHashedOwnerName.Equals(other.NextHashedOwnerName) && NextHashedOwnerName.Equals(other.NextHashedOwnerName) &&
_typeBitmaps.Equals(other._typeBitmaps); _typeBitmaps.Equals(other._typeBitmaps);
} }
...@@ -5548,16 +5624,12 @@ namespace PcapDotNet.Packets.Dns ...@@ -5548,16 +5624,12 @@ namespace PcapDotNet.Packets.Dns
internal override int GetLength() internal override int GetLength()
{ {
return ConstantPartLength + Salt.Length + NextHashedOwnerName.Length + _typeBitmaps.GetLength(); return ParametersLength + sizeof(byte) + NextHashedOwnerName.Length + _typeBitmaps.GetLength();
} }
internal override void WriteDataSimple(byte[] buffer, int offset) internal override void WriteDataSimple(byte[] buffer, int offset)
{ {
buffer.Write(offset + Offset.HashAlgorithm, (byte)HashAlgorithm); WriteParameters(buffer, offset);
buffer.Write(offset + Offset.Flags, (byte)Flags);
buffer.Write(offset + Offset.Iterations, Iterations, Endianity.Big);
buffer.Write(offset + Offset.SaltLength, (byte)Salt.Length);
Salt.Write(buffer, offset + Offset.Salt);
buffer.Write(offset + NextHashedOwnerNameLengthOffset, (byte)NextHashedOwnerName.Length); buffer.Write(offset + NextHashedOwnerNameLengthOffset, (byte)NextHashedOwnerName.Length);
NextHashedOwnerName.Write(buffer, offset + NextHashedOwnerNameOffset); NextHashedOwnerName.Write(buffer, offset + NextHashedOwnerNameOffset);
_typeBitmaps.Write(buffer, offset + TypeBitmapsOffset); _typeBitmaps.Write(buffer, offset + TypeBitmapsOffset);
...@@ -5565,19 +5637,16 @@ namespace PcapDotNet.Packets.Dns ...@@ -5565,19 +5637,16 @@ namespace PcapDotNet.Packets.Dns
internal override DnsResourceData CreateInstance(DataSegment data) internal override DnsResourceData CreateInstance(DataSegment data)
{ {
if (data.Length < ConstantPartLength) DnsSecNSec3HashAlgorithm hashAlgorithm;
DnsSecNSec3Flags flags;
ushort iterations;
DataSegment salt;
if (!TryReadParameters(data, out hashAlgorithm, out flags, out iterations, out salt))
return null; return null;
DnsSecNSec3HashAlgorithm hashAlgorithm = (DnsSecNSec3HashAlgorithm)data[Offset.HashAlgorithm]; int nextHashedOwnerNameLengthOffset = GetParametersLength(salt.Length);
DnsSecNSec3Flags flags = (DnsSecNSec3Flags)data[Offset.Flags]; if (data.Length - nextHashedOwnerNameLengthOffset < sizeof(byte))
ushort iterations = data.ReadUShort(Offset.Iterations, Endianity.Big);
int saltLength = data[Offset.SaltLength];
if (data.Length - Offset.Salt < saltLength + sizeof(byte))
return null; return null;
DataSegment salt = data.SubSegment(Offset.Salt, saltLength);
int nextHashedOwnerNameLengthOffset = Offset.Salt + saltLength;
int nextHashedOwnerNameOffset = nextHashedOwnerNameLengthOffset + sizeof(byte); int nextHashedOwnerNameOffset = nextHashedOwnerNameLengthOffset + sizeof(byte);
int nextHashedOwnerNameLength = data[nextHashedOwnerNameLengthOffset]; int nextHashedOwnerNameLength = data[nextHashedOwnerNameLengthOffset];
if (data.Length - nextHashedOwnerNameOffset < nextHashedOwnerNameLength) if (data.Length - nextHashedOwnerNameOffset < nextHashedOwnerNameLength)
...@@ -5594,21 +5663,16 @@ namespace PcapDotNet.Packets.Dns ...@@ -5594,21 +5663,16 @@ namespace PcapDotNet.Packets.Dns
private DnsResourceDataNextDomainSecure3(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt, private DnsResourceDataNextDomainSecure3(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt,
DataSegment nextHashedOwnerName, DnsTypeBitmaps typeBitmaps) DataSegment nextHashedOwnerName, DnsTypeBitmaps typeBitmaps)
: base(hashAlgorithm, flags, iterations, salt)
{ {
if (salt.Length > byte.MaxValue)
throw new ArgumentOutOfRangeException("salt", salt.Length, string.Format("Cannot bigger than {0}.", byte.MaxValue));
if (nextHashedOwnerName.Length > byte.MaxValue) if (nextHashedOwnerName.Length > byte.MaxValue)
throw new ArgumentOutOfRangeException("nextHashedOwnerName", nextHashedOwnerName.Length, string.Format("Cannot bigger than {0}.", byte.MaxValue)); throw new ArgumentOutOfRangeException("nextHashedOwnerName", nextHashedOwnerName.Length, string.Format("Cannot bigger than {0}.", byte.MaxValue));
HashAlgorithm = hashAlgorithm;
Flags = flags;
Iterations = iterations;
Salt = salt;
NextHashedOwnerName = nextHashedOwnerName; NextHashedOwnerName = nextHashedOwnerName;
_typeBitmaps = typeBitmaps; _typeBitmaps = typeBitmaps;
} }
private int NextHashedOwnerNameLengthOffset { get { return Offset.Salt + Salt.Length; } } private int NextHashedOwnerNameLengthOffset { get { return ParametersLength; } }
private int NextHashedOwnerNameOffset { get { return NextHashedOwnerNameLengthOffset + sizeof(byte); } } private int NextHashedOwnerNameOffset { get { return NextHashedOwnerNameLengthOffset + sizeof(byte); } }
...@@ -5616,4 +5680,67 @@ namespace PcapDotNet.Packets.Dns ...@@ -5616,4 +5680,67 @@ namespace PcapDotNet.Packets.Dns
private readonly DnsTypeBitmaps _typeBitmaps; private readonly DnsTypeBitmaps _typeBitmaps;
} }
/// <summary>
/// RFC 5155.
/// <pre>
/// +-----+-------------+----------+--------+------------+
/// | bit | 0-7 | 8-14 | 15 | 16-31 |
/// +-----+-------------+----------+--------+------------+
/// | 0 | Hash Alg | Reserved | OptOut | Iterations |
/// +-----+-------------+----------+--------+------------+
/// | 32 | Salt Length | Salt |
/// +-----+-------------+ |
/// | ... | |
/// +-----+----------------------------------------------+
/// </pre>
/// </summary>
[DnsTypeRegistration(Type = DnsType.NSec3Param)]
public sealed class DnsResourceDataNextDomainSecure3Parameters : DnsResourceDataNextDomainSecure3Base, IEquatable<DnsResourceDataNextDomainSecure3Parameters>
{
public DnsResourceDataNextDomainSecure3Parameters(DnsSecNSec3HashAlgorithm hashAlgorithm, DnsSecNSec3Flags flags, ushort iterations, DataSegment salt)
: base(hashAlgorithm, flags, iterations, salt)
{
}
public bool Equals(DnsResourceDataNextDomainSecure3Parameters other)
{
return EqualsParameters(other);
}
public override bool Equals(DnsResourceData other)
{
return Equals(other as DnsResourceDataNextDomainSecure3Parameters);
}
internal DnsResourceDataNextDomainSecure3Parameters()
: this(DnsSecNSec3HashAlgorithm.Sha1, DnsSecNSec3Flags.None, 0, DataSegment.Empty)
{
}
internal override int GetLength()
{
return ParametersLength;
}
internal override void WriteDataSimple(byte[] buffer, int offset)
{
WriteParameters(buffer, offset);
}
internal override DnsResourceData CreateInstance(DataSegment data)
{
DnsSecNSec3HashAlgorithm hashAlgorithm;
DnsSecNSec3Flags flags;
ushort iterations;
DataSegment salt;
if (!TryReadParameters(data, out hashAlgorithm, out flags, out iterations, out salt))
return null;
if (data.Length != GetParametersLength(salt.Length))
return null;
return new DnsResourceDataNextDomainSecure3Parameters(hashAlgorithm, flags, iterations, salt);
}
}
} }
...@@ -365,6 +365,7 @@ ...@@ -365,6 +365,7 @@
/// <summary> /// <summary>
/// RFC 5155. /// RFC 5155.
/// NSEC3PARAM. /// NSEC3PARAM.
/// Payload type: DnsResourceDataNextDomainSecure3Parameters.
/// </summary> /// </summary>
NSec3Param = 51, NSec3Param = 51,
......
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