public void DnsRecordFactory_DsRecord()
        {
            var expectedDigest = "3490A6806D47F17A34C29E2CE80E8A999FFBE4BE";
            var expectedBytes  = Enumerable.Range(0, expectedDigest.Length)
                                 .Where(x => x % 2 == 0)
                                 .Select(x => Convert.ToByte(expectedDigest.Substring(x, 2), 16))
                                 .ToArray();

            var name   = DnsString.Parse("example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteInt16NetworkOrder(31589);
            writer.WriteByte(8); // algorithm
            writer.WriteByte(1); // type
            writer.WriteBytes(expectedBytes, expectedBytes.Length);

            var factory = GetFactory(writer.Data.ToArray());

            var info = new ResourceRecordInfo(name, ResourceRecordType.DS, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as DsRecord;

            Assert.Equal(expectedBytes, result.Digest);
            Assert.Equal(expectedDigest, result.DigestAsString);
            Assert.Equal(31589, result.KeyTag);
            Assert.Equal(8, (int)result.Algorithm);
            Assert.Equal(1, result.DigestType);
        }
Beispiel #2
0
        private static byte[] GetResponseBytes(DnsQueryResponse message, byte[] answerData)
        {
            using (var writer = new DnsDatagramWriter())
            {
                writer.WriteUInt16NetworkOrder((ushort)message.Header.Id);
                writer.WriteUInt16NetworkOrder((ushort)message.Header.HeaderFlags);
                // lets iterate answers only, makse it easier
                //writer.WriteUInt16Network((ushort)message.Header.QuestionCount);
                writer.WriteUInt16NetworkOrder(0);
                writer.WriteUInt16NetworkOrder(1);
                //writer.WriteUInt16Network((ushort)message.Header.NameServerCount);
                writer.WriteUInt16NetworkOrder(0);
                //writer.WriteUInt16Network((ushort)message.Header.AdditionalCount);
                writer.WriteUInt16NetworkOrder(0);

                var answer = message.Answers.First();
                writer.WriteHostName(answer.DomainName.Value);
                writer.WriteUInt16NetworkOrder((ushort)answer.RecordType);
                writer.WriteUInt16NetworkOrder((ushort)answer.RecordClass);
                writer.WriteUInt32NetworkOrder((uint)answer.TimeToLive);
                writer.WriteUInt16NetworkOrder((ushort)answerData.Length);

                //writer.Extend(answerData.Length);   // the following data->length
                writer.WriteBytes(answerData, answerData.Length);

                return(writer.Data.ToArray());
            }
        }
        public void DnsRecordFactory_SRVRecord()
        {
            var name = DnsString.Parse("result.example.com");

            using (var pool = new PooledBytes(DnsDatagramWriter.BufferSize))
            {
                var writer = new DnsDatagramWriter(new ArraySegment <byte>(pool.Buffer));
                writer.WriteBytes(
                    new byte[] { 0, 1, 1, 0, 2, 3 },
                    6);
                writer.WriteHostName(name.Value);
                var factory = GetFactory(
                    writer.Data.ToArray(),
                    out var reader);

                var info = new ResourceRecord(
                    DnsString.Parse("query.example.com"),
                    SrvRecord.ResourceRecordType,
                    QueryClass.In,
                    0,
                    writer.Data.Count);

                var result = factory.GetRecord(
                    info,
                    reader) as SrvRecord ?? throw new Exception();

                Assert.Equal(
                    result.Target,
                    name);
                Assert.True(result.Priority == 1);
                Assert.True(result.Weight == 256);
                Assert.True(result.Port == 515);
            }
        }
 public ArraySegment <byte> BlockCopyLarge()
 {
     using (var writer = new DnsDatagramWriter())
     {
         writer.WriteBytes(sourceBig, sourceBig.Length);
         return(writer.Data);
     }
 }
        public void DnsRecordFactory_SRVRecord()
        {
            var name   = DnsString.Parse("result.example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteBytes(new byte[] { 0, 1, 1, 0, 2, 3 }, 6);
            writer.WriteHostName(name.Value);
            var factory = GetFactory(writer.Data.ToArray());

            var info = new ResourceRecordInfo("query.example.com", ResourceRecordType.SRV, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as SrvRecord;

            Assert.Equal(result.Target, name);
            Assert.True(result.Priority == 1);
            Assert.True(result.Weight == 256);
            Assert.True(result.Port == 515);
        }
        public void DnsRecordFactory_RRSIGRecord()
        {
            var   type                = ResourceRecordType.NSEC;
            var   algorithmNumber     = DnsSecurityAlgorithm.ECDSAP256SHA256;
            var   labels              = 5;
            var   originalTtl         = 300;
            var   signatureExpiration = 1589414400;
            var   signatureInception  = 1587600000;
            short keytag              = 3942;
            var   signersName         = DnsString.Parse("result.example.com");
            var   signatureString     = "kfyyKQoPZJFyOFSDqav7wj5XNRPqZssV2K2k8MJun28QSsCMHyWOjw9Hk4KofnEIUWNui3mMgAEFYbwoeRKkMf5uDAh6ryJ4veQNj86mgYJrpJppUplqlqJE8o1bx0I1VfwheL+M23bL5MnqSGiI5igmMDyeVUraVOO4RQyfGN0=";
            var   signature           = Convert.FromBase64String(signatureString);

            var writer = new DnsDatagramWriter();

            writer.WriteInt16NetworkOrder((short)type);
            writer.WriteByte((byte)algorithmNumber);
            writer.WriteByte((byte)labels);
            writer.WriteInt32NetworkOrder(originalTtl);
            writer.WriteInt32NetworkOrder(signatureExpiration);
            writer.WriteInt32NetworkOrder(signatureInception);
            writer.WriteInt16NetworkOrder(keytag);
            writer.WriteHostName(signersName.Value);
            writer.WriteBytes(signature, signature.Length);

            var factory = GetFactory(writer.Data.ToArray());
            var info    = new ResourceRecordInfo("query.example.com", ResourceRecordType.RRSIG, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as RRSigRecord;

            Assert.Equal(type, result.CoveredType);
            Assert.Equal(algorithmNumber, result.Algorithm);
            Assert.Equal(labels, result.Labels);
            Assert.Equal(originalTtl, result.OriginalTtl);
            Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(signatureExpiration), result.SignatureExpiration);
            Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(signatureInception), result.SignatureInception);
            Assert.Equal(signersName.Value, result.SignersName);

            Assert.Equal(signature, result.Signature);
            Assert.Equal(signatureString, result.SignatureAsString);
        }
        public void DnsRecordFactory_DnsKeyRecord()
        {
            var expectedPublicKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var expectedBytes     = Encoding.UTF8.GetBytes(expectedPublicKey);
            var name   = DnsString.Parse("example.com");
            var writer = new DnsDatagramWriter();

            writer.WriteInt16NetworkOrder(256);
            writer.WriteByte(3);
            writer.WriteByte((byte)DnsSecurityAlgorithm.RSASHA256);
            writer.WriteBytes(expectedBytes, expectedBytes.Length);

            var factory = GetFactory(writer.Data.ToArray());

            var info = new ResourceRecordInfo(name, ResourceRecordType.DNSKEY, QueryClass.IN, 0, writer.Data.Count);

            var result = factory.GetRecord(info) as DnsKeyRecord;

            Assert.Equal(expectedBytes, result.PublicKey);
            Assert.Equal(256, result.Flags);
            Assert.Equal(3, result.Protocol);
            Assert.Equal(DnsSecurityAlgorithm.RSASHA256, result.Algorithm);
        }