/// <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); }
internal byte[] GetMessageBytes() { MemoryStream memoryStream = new MemoryStream(); byte[] data = new byte[2]; data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_transactionId) >> 16)); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_flags) >> 16)); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_questions) >> 16)); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_answerRRs) >> 16)); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_authorityRRs) >> 16)); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder(_additionalRecords.Count) >> 16)); memoryStream.Write(data, 0, data.Length); data = DnsHelpers.CanonicaliseDnsName(_name, false); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_nsType) >> 16)); memoryStream.Write(data, 0, data.Length); data = BitConverter.GetBytes((ushort)(IPAddress.HostToNetworkOrder((ushort)_nsClass) >> 16)); memoryStream.Write(data, 0, data.Length); foreach (IDnsRecord dnsRecord in AdditionalRRecords) { data = dnsRecord.GetMessageBytes(); memoryStream.Write(data, 0, data.Length); } Trace.WriteLine(String.Format("The message bytes: {0}", DnsHelpers.DumpArrayToString(memoryStream.ToArray()))); return(memoryStream.ToArray()); }