public void ParseTest3() { byte[] bytes = new byte[] { 0x01, 0x01, 0x00, 0x48, // Response type and message length 0x21, 0x12, 0xa4, 0x42, // Magic cookie 0xb7, 0xe7, 0xa7, 0x01, // } 0xbc, 0x34, 0xd6, 0x86, // } Transaction ID 0xfa, 0x87, 0xdf, 0xae, // } 0x80, 0x22, 0x00, 0x0b, // SOFTWARE attribute header 0x74, 0x65, 0x73, 0x74, // } 0x20, 0x76, 0x65, 0x63, // } UTF-8 server name 0x74, 0x6f, 0x72, 0x20, // } 0x00, 0x20, 0x00, 0x14, // XOR-MAPPED-ADDRESS attribute header 0x00, 0x02, 0xa1, 0x47, // Address family (IPv6) and xor'd mapped port number 0x01, 0x13, 0xa9, 0xfa, // } 0xa5, 0xd3, 0xf1, 0x79, // } Xor'd mapped IPv6 address 0xbc, 0x25, 0xf4, 0xb5, // } 0xbe, 0xd2, 0xb9, 0xd9, // } 0x00, 0x08, 0x00, 0x14, // MESSAGE-INTEGRITY attribute header 0xa3, 0x82, 0x95, 0x4e, // } 0x4b, 0xe6, 0x7b, 0xf1, // } 0x17, 0x84, 0xc9, 0x7c, // } HMAC-SHA1 fingerprint 0x82, 0x92, 0xc2, 0x75, // } 0xbf, 0xe3, 0xed, 0x41, // } 0x80, 0x28, 0x00, 0x04, // FINGERPRINT attribute header 0xc8, 0xfb, 0x0b, 0x4c, // CRC32 fingerprint }; string password = @"VOkJxbRl1RmTxUk/WvJxBt"; TurnMessage message = TurnMessage.Parse(bytes, TurnMessageRfc.Rfc5389); Assert.AreEqual(@"test vector", message.Software.Value); Assert.AreEqual(@"2001:db8:1234:5678:11:2233:4455:6677", message.XorMappedAddress.IpAddress.ToString()); Assert.AreEqual(32853, message.XorMappedAddress.Port); byte[] messageIntegrity = message.ComputeMessageIntegrity(password, false); Helpers.AreArrayEqual(messageIntegrity, message.MessageIntegrity.Value); Assert.AreEqual(0x48, bytes[3]); uint fingerprint = message.ComputeFingerprint(); Assert.AreEqual(fingerprint, message.Fingerprint.Value); byte[] actual = message.GetBytes(password, false, 0x20); Helpers.AreArrayEqual(bytes, actual); TurnMessage message2 = new TurnMessage() { MessageType = MessageType.BindingResponse, TransactionId = message.TransactionId, Software = message.Software, XorMappedAddress = message.XorMappedAddress, MessageIntegrity = new MessageIntegrity(), Fingerprint = new Fingerprint(), }; byte[] actual2 = message2.GetBytes(password, false, 0x20); Helpers.AreArrayEqual(bytes, actual2); }
public void ParseTest4() { byte[] bytes = new byte[] { 0x00, 0x01, 0x00, 0x60, // Request type and message length 0x21, 0x12, 0xa4, 0x42, // Magic cookie 0x78, 0xad, 0x34, 0x33, // } 0xc6, 0xad, 0x72, 0xc0, // } Transaction ID 0x29, 0xda, 0x41, 0x2e, // } 0x00, 0x06, 0x00, 0x12, // USERNAME attribute header 0xe3, 0x83, 0x9e, 0xe3, // } 0x83, 0x88, 0xe3, 0x83, // } 0xaa, 0xe3, 0x83, 0x83, // } Username value (18 bytes) and padding (2 bytes) 0xe3, 0x82, 0xaf, 0xe3, // } 0x82, 0xb9, 0x00, 0x00, // } 0x00, 0x15, 0x00, 0x1c, // NONCE attribute header 0x66, 0x2f, 0x2f, 0x34, // } 0x39, 0x39, 0x6b, 0x39, // } 0x35, 0x34, 0x64, 0x36, // } 0x4f, 0x4c, 0x33, 0x34, // } Nonce value 0x6f, 0x4c, 0x39, 0x46, // } 0x53, 0x54, 0x76, 0x79, // } 0x36, 0x34, 0x73, 0x41, // } 0x00, 0x14, 0x00, 0x0b, // REALM attribute header 0x65, 0x78, 0x61, 0x6d, // } 0x70, 0x6c, 0x65, 0x2e, // } Realm value (11 bytes) and padding (1 byte) 0x6f, 0x72, 0x67, 0x00, // } 0x00, 0x08, 0x00, 0x14, // MESSAGE-INTEGRITY attribute header 0xf6, 0x70, 0x24, 0x65, // } 0x6d, 0xd6, 0x4a, 0x3e, // } 0x02, 0xb8, 0xe0, 0x71, // } HMAC-SHA1 fingerprint 0x2e, 0x85, 0xc9, 0xa2, // } 0x8c, 0xa8, 0x96, 0x66, // } }; string password = "******"; string username = "******"; string nonce = "f//499k954d6OL34oL9FSTvy64sA"; string realm = "example.org"; TurnMessage message = TurnMessage.Parse(bytes, TurnMessageRfc.Rfc5389); Assert.AreEqual(username, message.Username.Value); Assert.AreEqual(nonce, message.Nonce.Value); Assert.AreEqual(realm, message.Realm.Value); byte[] messageIntegrity = message.ComputeMessageIntegrity(password, true); Helpers.AreArrayEqual(messageIntegrity, message.MessageIntegrity.Value); Assert.IsTrue(message.IsMessageIntegrityValid(password, true)); byte[] actual = message.GetBytes(password, true); Helpers.AreArrayEqual(bytes, actual); TurnMessage message2 = new TurnMessage() { MessageType = MessageType.BindingRequest, TransactionId = message.TransactionId, Username = message.Username, Nonce = message.Nonce, Realm = message.Realm, MessageIntegrity = new MessageIntegrity(), }; byte[] actual2 = message2.GetBytes(password, true); Helpers.AreArrayEqual(bytes, actual2); }
public void ParseTest2() { byte[] bytes = new byte[] { 0x01, 0x01, 0x00, 0x3c, // Response type and message length 0x21, 0x12, 0xa4, 0x42, // Magic cookie 0xb7, 0xe7, 0xa7, 0x01, // } 0xbc, 0x34, 0xd6, 0x86, // } Transaction ID 0xfa, 0x87, 0xdf, 0xae, // } 0x80, 0x22, 0x00, 0x0b, // SOFTWARE attribute header 0x74, 0x65, 0x73, 0x74, // } 0x20, 0x76, 0x65, 0x63, // } UTF-8 server name 0x74, 0x6f, 0x72, 0x20, // } 0x00, 0x20, 0x00, 0x08, // XOR-MAPPED-ADDRESS attribute header 0x00, 0x01, 0xa1, 0x47, // Address family (IPv4) and xor'd mapped port number 0xe1, 0x12, 0xa6, 0x43, // Xor'd mapped IPv4 address 0x00, 0x08, 0x00, 0x14, // MESSAGE-INTEGRITY attribute header 0x2b, 0x91, 0xf5, 0x99, // } 0xfd, 0x9e, 0x90, 0xc3, // } 0x8c, 0x74, 0x89, 0xf9, // } HMAC-SHA1 fingerprint 0x2a, 0xf9, 0xba, 0x53, // } 0xf0, 0x6b, 0xe7, 0xd7, // } 0x80, 0x28, 0x00, 0x04, // FINGERPRINT attribute header 0xc0, 0x7d, 0x4c, 0x96, // CRC32 fingerprint }; string password = @"VOkJxbRl1RmTxUk/WvJxBt"; TurnMessage message = TurnMessage.Parse(bytes, TurnMessageRfc.Rfc5389); Assert.AreEqual(@"test vector", message.Software.Value); Assert.AreEqual(@"192.0.2.1", message.XorMappedAddress.IpAddress.ToString()); Assert.AreEqual(32853, message.XorMappedAddress.Port); byte[] messageIntegrity = message.ComputeMessageIntegrity(password, false); Helpers.AreArrayEqual(messageIntegrity, message.MessageIntegrity.Value); Assert.AreEqual(0x3c, bytes[3]); uint fingerprint = message.ComputeFingerprint(); Assert.AreEqual(fingerprint, message.Fingerprint.Value); byte[] actual = message.GetBytes(password, false, 0x20); Helpers.AreArrayEqual(bytes, actual); TurnMessage message2 = new TurnMessage() { MessageType = MessageType.BindingResponse, TransactionId = message.TransactionId, Software = message.Software, XorMappedAddress = message.XorMappedAddress, MessageIntegrity = new MessageIntegrity(), Fingerprint = new Fingerprint(), }; byte[] actual2 = message2.GetBytes(password, false, 0x20); Helpers.AreArrayEqual(bytes, actual2); }
public void MS_Message_Integrity_With_Data_Offset_In_Array() { var key2 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var created = new TurnMessage() { MessageType = MessageType.AllocateRequest, TransactionId = TransactionId.Generate(), MagicCookie = new MagicCookie(), MsVersion = new MsVersion() { Value = 1, }, MsUsername = new MsUsername() { Value = new byte[52], }, Nonce = new Nonce(TurnMessageRfc.MsTurn) { Value = "17938999-C63D-41cd-87A4-FE80975F03BA", }, Realm = new Realm(TurnMessageRfc.MsTurn) { Value = "test.test", }, MessageIntegrity = new MessageIntegrity(), }; int offset = 120; var buffer = new byte[4096]; created.ComputeMessageLength(); created.GetBytes(buffer, offset, key2); var parsed = TurnMessage.Parse(buffer, offset, created.TotalMessageLength, TurnMessageRfc.MsTurn); Assert.IsTrue(parsed.IsValidMessageIntegrity(key2)); }
public void MsTurnMessageTest6() { byte[] expected = new byte[] { 0x01, 0x15, 0x00, 0xA9, 0x1B, 0xE1, 0x64, 0x3C, 0x91, 0x14, 0x00, 0x00, 0x1B, 0xE1, 0x64, 0x3C, 0x91, 0x14, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x04, 0x72, 0xC6, 0x4B, 0xC6, 0x00, 0x12, 0x00, 0x08, 0x00, 0x01, 0x61, 0x4F, 0xD5, 0x1B, 0x5D, 0x66, 0x00, 0x13, 0x00, 0x91, 0x00, 0x8F, 0x80, 0xC8, 0x00, 0x09, 0x36, 0x15, 0xDA, 0x97, 0x70, 0x3F, 0x62, 0xFD, 0x22, 0x6C, 0x49, 0x35, 0x03, 0xAA, 0x99, 0xC4, 0x96, 0x3D, 0x05, 0xFE, 0x10, 0xE8, 0xFF, 0xEC, 0x9D, 0x6F, 0xA2, 0x26, 0x44, 0x7C, 0x40, 0xE5, 0x7C, 0xB0, 0xAB, 0x2A, 0xA4, 0x66, 0x1F, 0x5E, 0x74, 0x25, 0x56, 0x3A, 0x0D, 0x64, 0x8D, 0xBD, 0x8D, 0xFB, 0xF4, 0x81, 0xA3, 0xED, 0x60, 0x35, 0x2B, 0x01, 0x7E, 0xC4, 0xF8, 0x6D, 0xF0, 0x26, 0xBD, 0xFF, 0xF3, 0x31, 0xA7, 0xA7, 0xDC, 0xAF, 0x89, 0x9C, 0xFA, 0xC1, 0xC7, 0x3E, 0xC0, 0x65, 0x53, 0x9C, 0x90, 0xDD, 0xAF, 0xCC, 0x02, 0x2E, 0xF0, 0xF3, 0xB9, 0xAA, 0x5D, 0xB6, 0x07, 0xB5, 0x17, 0x2A, 0xDE, 0xF0, 0x23, 0x52, 0x01, 0x7F, 0xD3, 0xE6, 0x16, 0xB1, 0xF2, 0x90, 0x16, 0x5A, 0x79, 0xD6, 0xE9, 0xC9, 0xB8, 0x5F, 0x53, 0xE0, 0x25, 0x63, 0x5B, 0x09, 0x80, 0x00, 0x00, 0x05, 0x01, 0x85, 0xBA, 0x35, 0xA0, 0xC8, 0x79, 0xF7, 0xED, 0x47, 0x91, }; TurnMessage message = new TurnMessage() { IsAttributePaddingDisabled = true, MessageType = MessageType.DataIndication, TransactionId = new TransactionId(expected, 4), MagicCookie = new MagicCookie(), RemoteAddress = new RemoteAddress() { IpEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("213.27.93.102"), 24911), }, Data = new Data() { Value = new byte[] { 0x00, 0x8F, 0x80, 0xC8, 0x00, 0x09, 0x36, 0x15, 0xDA, 0x97, 0x70, 0x3F, 0x62, 0xFD, 0x22, 0x6C, 0x49, 0x35, 0x03, 0xAA, 0x99, 0xC4, 0x96, 0x3D, 0x05, 0xFE, 0x10, 0xE8, 0xFF, 0xEC, 0x9D, 0x6F, 0xA2, 0x26, 0x44, 0x7C, 0x40, 0xE5, 0x7C, 0xB0, 0xAB, 0x2A, 0xA4, 0x66, 0x1F, 0x5E, 0x74, 0x25, 0x56, 0x3A, 0x0D, 0x64, 0x8D, 0xBD, 0x8D, 0xFB, 0xF4, 0x81, 0xA3, 0xED, 0x60, 0x35, 0x2B, 0x01, 0x7E, 0xC4, 0xF8, 0x6D, 0xF0, 0x26, 0xBD, 0xFF, 0xF3, 0x31, 0xA7, 0xA7, 0xDC, 0xAF, 0x89, 0x9C, 0xFA, 0xC1, 0xC7, 0x3E, 0xC0, 0x65, 0x53, 0x9C, 0x90, 0xDD, 0xAF, 0xCC, 0x02, 0x2E, 0xF0, 0xF3, 0xB9, 0xAA, 0x5D, 0xB6, 0x07, 0xB5, 0x17, 0x2A, 0xDE, 0xF0, 0x23, 0x52, 0x01, 0x7F, 0xD3, 0xE6, 0x16, 0xB1, 0xF2, 0x90, 0x16, 0x5A, 0x79, 0xD6, 0xE9, 0xC9, 0xB8, 0x5F, 0x53, 0xE0, 0x25, 0x63, 0x5B, 0x09, 0x80, 0x00, 0x00, 0x05, 0x01, 0x85, 0xBA, 0x35, 0xA0, 0xC8, 0x79, 0xF7, 0xED, 0x47, 0x91, } }, }; byte[] bytes = message.GetBytes(null); Helpers.AreArrayEqual(expected, bytes); }
private void SendMessage(Socket socket, TurnMessage message) { byte[] buffer1 = new byte[4096]; message.ComputeMessageLength(); message.GetBytes(buffer1, TcpFramingHeader.TcpFramingHeaderLength, key2); TcpFramingHeader.GetBytes(buffer1, 0, TcpFrameType.ControlMessage, message.TotalMessageLength); int size = TcpFramingHeader.TcpFramingHeaderLength + message.TotalMessageLength; if (socket.Send(buffer1, size, SocketFlags.None) != size) throw new Exception("Send failed!"); }
private void SendTurn(TurnMessage message, ServerEndPoint local, IPEndPoint remote) { ServerAsyncEventArgs p; int offset; message.ComputeMessageLength(); GetBuffer(local, remote, message.TotalMessageLength, out p, out offset); message.GetBytes(p.Buffer, offset, Authentificater.Key2); turnServer.SendAsync(p); }