예제 #1
0
        public bool Process(TurnMessage request, out TurnMessage response)
        {
            lock (syncRoot)
            {
                ErrorCode? errorCode = ValidateRequest(request);

                if (errorCode != null)
                {
                    response = new TurnMessage()
                    {
                        MessageType = request.MessageType.GetErrorResponseType(),
                        TransactionId = request.TransactionId,
                        MagicCookie = new MagicCookie(),
                        ErrorCodeAttribute = new ErrorCodeAttribute()
                        {
                            ErrorCode = (int)errorCode,
                            ReasonPhrase = ((ErrorCode)errorCode).GetReasonPhrase(),
                        },
                        Realm = new Realm(TurnMessageRfc.MsTurn) { Value = Realm, },
                        Nonce = new Nonce(TurnMessageRfc.MsTurn) { Value = NewNonce(), },
                        MsVersion = new MsVersion() { Value = 1, },
                    };
                }
                else
                {
                    response = null;
                }

                return response == null;
            }
        }
예제 #2
0
        public static TurnMessage Parse(byte[] bytes, int startIndex, int length, TurnMessageRfc rfc)
        {
            TurnMessage message = new TurnMessage();

            message.ParseHeader(bytes, startIndex, length);
            message.ParseAttributes(bytes, startIndex, length, rfc);

            return(message);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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));
        }
예제 #6
0
        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);
        }
예제 #7
0
        public void DoubleTcpAllocationTest()
        {
            var msUsername = new MsUsername()
            {
                Value = new byte[52],
            };

            using (HMACSHA1 sha1 = new HMACSHA1(key1))
            {
                sha1.ComputeHash(msUsername.Value, 0, msUsername.TokenBlobLength);
                Array.Copy(sha1.Hash, 0, msUsername.Value, msUsername.TokenBlobLength, MsUsername.HashOfTokenBlobLength);
            }

            var allocate1 = new TurnMessage()
            {
                MessageType = MessageType.AllocateRequest,
                TransactionId = TransactionId.Generate(),

                MagicCookie = new MagicCookie(),
                MsVersion = new MsVersion()
                {
                    Value = 1,
                },
                MsUsername = msUsername,
            };

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(turnIp, tcpPort);

            var allocate1response = SendReceiveMessage(socket, allocate1);

            Assert.AreEqual(MessageType.AllocateErrorResponse, allocate1response.MessageType);
            Assert.AreEqual(401, allocate1response.ErrorCodeAttribute.ErrorCode);

            var allocate2 = new TurnMessage()
            {
                MessageType = MessageType.AllocateRequest,
                TransactionId = TransactionId.Generate(),

                MagicCookie = new MagicCookie(),
                MsVersion = new MsVersion()
                {
                    Value = 1,
                },
                MsUsername = msUsername,
                Nonce = allocate1response.Nonce,
                Realm = new Realm(TurnMessageRfc.MsTurn)
                {
                    Value = realm,
                },
                MessageIntegrity = new MessageIntegrity(),
            };

            var allocate2response = SendReceiveMessage(socket, allocate2);

            Assert.AreEqual(MessageType.AllocateResponse, allocate2response.MessageType);

            var allocate3response = SendReceiveMessage(socket, allocate2);

            Assert.AreEqual(MessageType.AllocateResponse, allocate3response.MessageType);
        }
예제 #8
0
 private TurnMessage SendReceiveMessage(Socket socket, TurnMessage message)
 {
     SendMessage(socket, message);
     return ReceiveMessage(socket);
 }
예제 #9
0
        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);
        }
예제 #10
0
        private TurnMessage ProcessSetActiveDestinationRequest(Allocation allocation, TurnMessage request, IPEndPoint reflexEndPoint)
        {
            if (allocation == null)
                throw new TurnServerException(ErrorCode.NoBinding);

            if (request.DestinationAddress == null)
                throw new TurnServerException(ErrorCode.BadRequest);

            allocation.ActiveDestination = request.DestinationAddress.IpEndPoint;
            allocation.Permissions.Permit(request.DestinationAddress.IpEndPoint);

            logger.WriteInformation(string.Format("Set Active Destination: {2} --> {0} <--> {1}", allocation.Alocated.ToString(), allocation.Reflexive.ToString(), allocation.ActiveDestination.ToString()));

            uint sequenceNumber = (request.MsSequenceNumber != null) ? request.MsSequenceNumber.SequenceNumber : 0;

            return new TurnMessage()
            {
                IsAttributePaddingDisabled = true,
                MessageType = MessageType.SetActiveDestinationResponse,
                TransactionId = request.TransactionId,

                MagicCookie = new MagicCookie(),

                Realm = new Realm(TurnMessageRfc.MsTurn)
                {
                    Ignore = true,
                    Value = Authentificater.Realm,
                },
                MsUsername = new MsUsername()
                {
                    Ignore = true,
                    Value = request.MsUsername.Value,
                },
                //MsUsername = allocation.Username,
                MessageIntegrity = new MessageIntegrity(),

                MsSequenceNumber = new MsSequenceNumber()
                {
                    ConnectionId = allocation.ConnectionId,
                    SequenceNumber = sequenceNumber,//allocation.SequenceNumber,
                },
            };
        }
예제 #11
0
        private TurnMessage ProcessSendRequest(Allocation allocation, TurnMessage request, ref ServerAsyncEventArgs e)
        {
            try
            {
                if (allocation == null)
                    throw new TurnServerException(ErrorCode.NoBinding);

                if (request.Data == null || request.DestinationAddress == null)
                    throw new TurnServerException(ErrorCode.BadRequest);

                allocation.Permissions.Permit(request.DestinationAddress.IpEndPoint);

                e.LocalEndPoint = allocation.Alocated;
                e.RemoteEndPoint = request.DestinationAddress.IpEndPoint;
                e.Offset = request.Data.ValueRefOffset;
                e.Count = request.Data.ValueRefLength;
                e.ConnectionId = ServerAsyncEventArgs.AnyNewConnectionId;

                peerServer.SendAsync(e);

                e = null;
            }
            catch (Exception ex)
            {
                logger.WriteWarning(ex.ToString());
            }

            // [MS-TURN] The server MUST NOT respond to a client with either
            // a Send response or a Send error response.
            return null;
        }
예제 #12
0
        private TurnMessage ProcessAllocateRequest(ref Allocation allocation, TurnMessage request, ServerEndPoint local, IPEndPoint remote)
        {
            uint sequenceNumber = (request.MsSequenceNumber != null) ? request.MsSequenceNumber.SequenceNumber : 0;

            {
                uint lifetime = (request.Lifetime != null) ? ((request.Lifetime.Value > MaxLifetime.Seconds) ? MaxLifetime.Seconds : request.Lifetime.Value) : DefaultLifetime.Seconds;

                if (allocation != null)
                {
                    allocation.Lifetime = lifetime;

                    if (lifetime == 0)
                        logger.WriteInformation(string.Format("Update Allocation: {2} seconds {0} <--> {1}", allocation.Alocated.ToString(), allocation.Reflexive.ToString(), lifetime));
                }
                else
                {
                    if (lifetime <= 0)
                        throw new TurnServerException(ErrorCode.NoBinding);

                    ProtocolPort pp = new ProtocolPort() { Protocol = local.Protocol, };
                    if (peerServer.Bind(ref pp) != SocketError.Success)
                        throw new TurnServerException(ErrorCode.ServerError);

                    allocation = new Allocation()
                    {
                        TransactionId = request.TransactionId,
                        ConnectionId = ConnectionIdGenerator.Generate(local, remote),

                        Local = local,
                        Alocated = new ServerEndPoint(pp, PublicIp),
                        Real = new ServerEndPoint(pp, RealIp),
                        Reflexive = new IPEndPoint(remote.Address, remote.Port),

                        Lifetime = lifetime,
                    };

                    allocations.Replace(allocation);

                    logger.WriteInformation(string.Format("Allocated: {0} <--> {1} for {2} seconds", allocation.Alocated.ToString(), allocation.Reflexive.ToString(), allocation.Lifetime));
                }
            }

            return new TurnMessage()
            {
                IsAttributePaddingDisabled = true,
                MessageType = MessageType.AllocateResponse,
                TransactionId = request.TransactionId,

                MagicCookie = new MagicCookie(),

                MappedAddress = new MappedAddress()
                {
                    IpAddress = allocation.Alocated.Address,
                    Port = (UInt16)allocation.Alocated.Port,
                },

                Lifetime = new Lifetime() { Value = allocation.Lifetime, },
                Bandwidth = new Bandwidth() { Value = 750, },

                XorMappedAddress = new XorMappedAddress(TurnMessageRfc.MsTurn)
                {
                    IpAddress = remote.Address,
                    Port = (UInt16)remote.Port,
                },

                Realm = new Realm(TurnMessageRfc.MsTurn)
                {
                    Ignore = true,
                    Value = Authentificater.Realm,
                },
                MsUsername = new MsUsername()
                {
                    Ignore = true,
                    Value = request.MsUsername.Value,
                },
                //MsUsername = allocation.Username,
                MessageIntegrity = new MessageIntegrity(),

                MsSequenceNumber = new MsSequenceNumber()
                {
                    ConnectionId = allocation.ConnectionId,
                    SequenceNumber = sequenceNumber,//allocation.SequenceNumber,
                },
            };
        }
예제 #13
0
        private bool PeerServer_Received(ServersManager<PeerConnection> s, BaseConnection с, ref ServerAsyncEventArgs e)
        {
            //lock (syncRoot)
            {
                try
                {
                    Allocation allocation = allocations.Get(e.LocalEndPoint);

                    if (allocation != null)
                    {
                        if (allocation.Permissions.IsPermited(e.RemoteEndPoint))
                        {
                            allocation.TouchLifetime();

                            if (allocation.ActiveDestination.IsEqual(e.RemoteEndPoint))
                            {
                                if (e.LocalEndPoint.Protocol == ServerProtocol.Tcp)
                                {
                                    TcpFramingHeader.GetBytes(e.Buffer, e.Offset, TcpFrameType.EndToEndData, e.BytesTransferred);

                                    e.Count = e.OffsetOffset + e.BytesTransferred;
                                    e.OffsetOffset = 0;
                                }
                                else
                                {
                                    e.Count = e.BytesTransferred;
                                }

                                e.LocalEndPoint = allocation.Local;
                                e.RemoteEndPoint = allocation.Reflexive;
                                e.ConnectionId = ServerAsyncEventArgs.AnyConnectionId;

                                turnServer.SendAsync(e);

                                e = null;
                            }
                            else
                            {
                                TurnMessage message = new TurnMessage()
                                {
                                    IsAttributePaddingDisabled = true,
                                    MessageType = MessageType.DataIndication,
                                    TransactionId = TransactionServer.GenerateTransactionId(),

                                    MagicCookie = new MagicCookie(),

                                    RemoteAddress = new RemoteAddress()
                                    {
                                        IpAddress = e.RemoteEndPoint.Address,
                                        Port = (UInt16)e.RemoteEndPoint.Port,
                                    },

                                    Data = new Data()
                                    {
                                        ValueRef = e.Buffer,
                                        ValueRefOffset = e.Offset,
                                        ValueRefLength = e.BytesTransferred,
                                    },
                                };

                                SendTurn(message, allocation.Local, allocation.Reflexive);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.WriteWarning(ex.ToString());
                }
            }

            return true;
        }
예제 #14
0
        public static TurnMessage Parse(byte[] bytes, int startIndex, int length, TurnMessageRfc rfc)
        {
            TurnMessage message = new TurnMessage();

            message.ParseHeader(bytes, startIndex, length);
            message.ParseAttributes(bytes, startIndex, length, rfc);

            return message;
        }
예제 #15
0
 public void SetResponse(TurnMessage newResponse)
 {
     lock (sync)
     {
         response = newResponse;
     }
 }
예제 #16
0
        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);
        }
예제 #17
0
        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!");
        }
예제 #18
0
        private ErrorCode? ValidateRequest(TurnMessage request)
        {
            if (request.MessageIntegrity == null)
                return ErrorCode.Unauthorized;

            if (request.MessageType == MessageType.AllocateRequest)
            {
                if (request.Realm == null)
                    return ErrorCode.MissingRealm;

                if (request.Nonce == null)
                    return ErrorCode.MissingNonce;

                if (IsValidNonce(request.Nonce.Value) == false)
                    return ErrorCode.StaleNonce;
            }

            if (request.MsUsername == null)
                return ErrorCode.MissingUsername;

            if (request.IsValidMsUsername(Key1) == false)
                return ErrorCode.UnknownUsername;

            if (request.Realm != null)
            {
                if (request.Realm.Value != Realm)
                    return ErrorCode.MissingRealm;
            }
            else
                request.Realm = realm;

            if (request.IsValidMessageIntegrity(Key2) == false)
                return ErrorCode.IntegrityCheckFailure;

            return null;
        }