Ejemplo n.º 1
0
        public override byte[] GetMessageBytes()
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] data = DnsHeader.GetMessageBytes();
            memoryStream.Write(data, 0, data.Length);

            long rLengthPosition = memoryStream.Position;

            data = DnsHelpers.CanonicaliseDnsName(_algorithmName, false);
            memoryStream.Write(data, 0, data.Length);

            int  timeHigh;
            long timeLow;

            DnsHelpers.ConvertToDnsTime(_timeSigned.ToUniversalTime(), out timeHigh, out timeLow);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)timeHigh) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((uint)timeLow) >> 32));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_fudge) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_mac.Length) >> 16));
            memoryStream.Write(data, 0, data.Length);

            memoryStream.Write(_mac, 0, _mac.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_originalId) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_error) >> 16));
            memoryStream.Write(data, 0, data.Length);


            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_otherData.Length) >> 16));
            memoryStream.Write(data, 0, data.Length);

            if (_otherData.Length != 0)
            {
                memoryStream.Write(_otherData, 0, _otherData.Length);
            }

            // Add the rdata lenght
            long rlength = memoryStream.Position - rLengthPosition;

            memoryStream.Seek(rLengthPosition - 2, SeekOrigin.Begin);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)rlength) >> 16));
            memoryStream.Write(data, 0, data.Length);

            return(memoryStream.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Apply a TSIG record to the request message.
        /// </summary>
        /// <param name="dnsQueryRequest">The <see cref="DnsQueryRequest"/> to add the security headers too.</param>
        /// <returns>A <see cref="DnsQueryRequest"/> instance with additional security attributes assigned.</returns>
        public DnsQueryRequest SecureMessage(DnsQueryRequest dnsQueryRequest)
        {
            DateTime signDateTime = DateTime.Now;
            int      timeHigh;
            long     timeLow;

            byte[] messageBytes = dnsQueryRequest.GetMessageBytes();
            Trace.WriteLine(String.Format("Message Header Bytes: {0}", DnsHelpers.DumpArrayToString(messageBytes)));

            MemoryStream memoryStream = new MemoryStream();

            memoryStream.Write(messageBytes, 0, messageBytes.Length);

            // the shared key name
            byte[] data = DnsHelpers.CanonicaliseDnsName(_name, false);
            memoryStream.Write(data, 0, data.Length);
            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)NsClass.ANY) >> 16));
            memoryStream.Write(data, 0, data.Length);
            // the TTL value
            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((uint)0) >> 32));
            memoryStream.Write(data, 0, data.Length);
            // the algorithim name
            data = DnsHelpers.CanonicaliseDnsName(_algorithimName, true);
            memoryStream.Write(data, 0, data.Length);

            DnsHelpers.ConvertToDnsTime(signDateTime.ToUniversalTime(), out timeHigh, out timeLow);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)timeHigh) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((uint)(IPAddress.HostToNetworkOrder((uint)timeLow) >> 32));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_fudge) >> 16));
            memoryStream.Write(data, 0, data.Length);

            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)RCode.NoError) >> 16));
            memoryStream.Write(data, 0, data.Length);

            // no other data
            data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)0) >> 16));
            memoryStream.Write(data, 0, data.Length);

            byte[] dataToHash = memoryStream.ToArray();
            Trace.WriteLine(String.Format("Data to hash: {0}", DnsHelpers.DumpArrayToString(dataToHash)));
            byte[] mac = _hmac.ComputeHash(dataToHash);
            Trace.WriteLine(String.Format("hash: {0}", DnsHelpers.DumpArrayToString(mac)));

            dnsQueryRequest.AdditionalRRecords.Add(new TSigRecord(_name, _algorithimName, RCode.NoError, _fudge, dnsQueryRequest.TransactionID, new byte[] { }, mac, signDateTime));

            return(dnsQueryRequest);
        }