From d8014d1ed8dd2374f77d670a1368958c8a10541a Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 24 Jan 2014 18:51:15 +0100 Subject: ProtoProxy: Fixed connection on *nix. --- Tools/ProtoProxy/Connection.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index cd66e2dfd..b63935f38 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -243,7 +243,8 @@ void cConnection::Run(void) FD_ZERO(&ReadFDs); FD_SET(m_ServerSocket, &ReadFDs); FD_SET(m_ClientSocket, &ReadFDs); - int res = select(2, &ReadFDs, NULL, NULL, NULL); + SOCKET MaxSocket = std::max(m_ServerSocket, m_ClientSocket); + int res = select(MaxSocket + 1, &ReadFDs, NULL, NULL, NULL); if (res <= 0) { printf("select() failed: %d; aborting client", SocketError); -- cgit v1.2.3 From 8f1890e877467bd458910e4b7e5d8dcaceb25854 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 25 Jan 2014 19:19:37 +0100 Subject: ProtoProxy: Modified to use PolarSSL. --- Tools/ProtoProxy/Connection.cpp | 51 ++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index b63935f38..510d3645d 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -378,13 +378,13 @@ bool cConnection::RelayFromServer(void) } case csEncryptedUnderstood: { - m_ServerDecryptor.ProcessData((byte *)Buffer, (byte *)Buffer, res); + m_ServerDecryptor.ProcessData((Byte *)Buffer, (Byte *)Buffer, res); DataLog(Buffer, res, "Decrypted %d bytes from the SERVER", res); return DecodeServersPackets(Buffer, res); } case csEncryptedUnknown: { - m_ServerDecryptor.ProcessData((byte *)Buffer, (byte *)Buffer, res); + m_ServerDecryptor.ProcessData((Byte *)Buffer, (Byte *)Buffer, res); DataLog(Buffer, res, "Decrypted %d bytes from the SERVER", res); return CLIENTSEND(Buffer, res); } @@ -423,7 +423,7 @@ bool cConnection::RelayFromClient(void) case csEncryptedUnknown: { DataLog(Buffer, res, "Decrypted %d bytes from the CLIENT", res); - m_ServerEncryptor.ProcessData((byte *)Buffer, (byte *)Buffer, res); + m_ServerEncryptor.ProcessData((Byte *)Buffer, (Byte *)Buffer, res); return SERVERSEND(Buffer, res); } } @@ -473,13 +473,13 @@ bool cConnection::SendData(SOCKET a_Socket, cByteBuffer & a_Data, const char * a -bool cConnection::SendEncryptedData(SOCKET a_Socket, Encryptor & a_Encryptor, const char * a_Data, int a_Size, const char * a_Peer) +bool cConnection::SendEncryptedData(SOCKET a_Socket, cAESCFBEncryptor & a_Encryptor, const char * a_Data, int a_Size, const char * a_Peer) { DataLog(a_Data, a_Size, "Encrypting %d bytes to %s", a_Size, a_Peer); - const byte * Data = (const byte *)a_Data; + const Byte * Data = (const Byte *)a_Data; while (a_Size > 0) { - byte Buffer[64 KiB]; + Byte Buffer[64 KiB]; int NumBytes = (a_Size > sizeof(Buffer)) ? sizeof(Buffer) : a_Size; a_Encryptor.ProcessData(Buffer, Data, NumBytes); bool res = SendData(a_Socket, (const char *)Buffer, NumBytes, a_Peer); @@ -497,7 +497,7 @@ bool cConnection::SendEncryptedData(SOCKET a_Socket, Encryptor & a_Encryptor, co -bool cConnection::SendEncryptedData(SOCKET a_Socket, Encryptor & a_Encryptor, cByteBuffer & a_Data, const char * a_Peer) +bool cConnection::SendEncryptedData(SOCKET a_Socket, cAESCFBEncryptor & a_Encryptor, cByteBuffer & a_Data, const char * a_Peer) { AString All; a_Data.ReadAll(All); @@ -2701,7 +2701,7 @@ bool cConnection::ParseMetadata(cByteBuffer & a_Buffer, AString & a_Metadata) int Length = 0; switch (Type) { - case 0: Length = 1; break; // byte + case 0: Length = 1; break; // Byte case 1: Length = 2; break; // short case 2: Length = 4; break; // int case 3: Length = 4; break; // float @@ -2860,37 +2860,26 @@ void cConnection::LogMetadata(const AString & a_Metadata, size_t a_IndentCount) void cConnection::SendEncryptionKeyResponse(const AString & a_ServerPublicKey, const AString & a_Nonce) { // Generate the shared secret and encrypt using the server's public key - byte SharedSecret[16]; - byte EncryptedSecret[128]; + Byte SharedSecret[16]; + Byte EncryptedSecret[128]; memset(SharedSecret, 0, sizeof(SharedSecret)); // Use all zeroes for the initial secret - RSA::PublicKey pk; - CryptoPP::StringSource src(a_ServerPublicKey, true); - ByteQueue bq; - src.TransferTo(bq); - bq.MessageEnd(); - pk.Load(bq); - RSAES::Encryptor rsaEncryptor(pk); - RandomPool rng; - time_t CurTime = time(NULL); - rng.Put((const byte *)&CurTime, sizeof(CurTime)); - int EncryptedLength = rsaEncryptor.FixedCiphertextLength(); - ASSERT(EncryptedLength <= sizeof(EncryptedSecret)); - rsaEncryptor.Encrypt(rng, SharedSecret, sizeof(SharedSecret), EncryptedSecret); - m_ServerEncryptor.SetKey(SharedSecret, 16, MakeParameters(Name::IV(), ConstByteArrayParameter(SharedSecret, 16, true))(Name::FeedbackSize(), 1)); - m_ServerDecryptor.SetKey(SharedSecret, 16, MakeParameters(Name::IV(), ConstByteArrayParameter(SharedSecret, 16, true))(Name::FeedbackSize(), 1)); + m_Server.GetPrivateKey().Encrypt(SharedSecret, sizeof(SharedSecret), EncryptedSecret, sizeof(EncryptedSecret)); + + m_ServerEncryptor.Init(SharedSecret, SharedSecret); + m_ServerDecryptor.Init(SharedSecret, SharedSecret); // Encrypt the nonce: - byte EncryptedNonce[128]; - rsaEncryptor.Encrypt(rng, (const byte *)(a_Nonce.data()), a_Nonce.size(), EncryptedNonce); + Byte EncryptedNonce[128]; + m_Server.GetPrivateKey().Encrypt((const Byte *)a_Nonce.data(), a_Nonce.size(), EncryptedNonce, sizeof(EncryptedNonce)); // Send the packet to the server: Log("Sending PACKET_ENCRYPTION_KEY_RESPONSE to the SERVER"); cByteBuffer ToServer(1024); ToServer.WriteByte(0x01); // To server: Encryption key response - ToServer.WriteBEShort(EncryptedLength); - ToServer.WriteBuf(EncryptedSecret, EncryptedLength); - ToServer.WriteBEShort(EncryptedLength); - ToServer.WriteBuf(EncryptedNonce, EncryptedLength); + ToServer.WriteBEShort((short)sizeof(EncryptedSecret)); + ToServer.WriteBuf(EncryptedSecret, sizeof(EncryptedSecret)); + ToServer.WriteBEShort((short)sizeof(EncryptedNonce)); + ToServer.WriteBuf(EncryptedNonce, sizeof(EncryptedNonce)); SERVERSEND(ToServer); m_ServerState = csEncryptedUnderstood; m_IsServerEncrypted = true; -- cgit v1.2.3 From ae897804a0474994eff56ec63bd1eb8ca7b3aaaa Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 28 Jan 2014 23:53:33 +0100 Subject: ProtoProxy: Added encryption support. --- Tools/ProtoProxy/Connection.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index 510d3645d..91d2fc42f 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -1302,6 +1302,7 @@ bool cConnection::HandleServerLoginEncryptionKeyRequest(void) } Log("Got PACKET_ENCRYPTION_KEY_REQUEST from the SERVER:"); Log(" ServerID = %s", ServerID.c_str()); + DataLog(PublicKey.data(), PublicKey.size(), " Public key (%u bytes)", (unsigned)PublicKey.size()); // Reply to the server: SendEncryptionKeyResponse(PublicKey, Nonce); @@ -2863,14 +2864,25 @@ void cConnection::SendEncryptionKeyResponse(const AString & a_ServerPublicKey, c Byte SharedSecret[16]; Byte EncryptedSecret[128]; memset(SharedSecret, 0, sizeof(SharedSecret)); // Use all zeroes for the initial secret - m_Server.GetPrivateKey().Encrypt(SharedSecret, sizeof(SharedSecret), EncryptedSecret, sizeof(EncryptedSecret)); + cPublicKey PubKey(a_ServerPublicKey); + int res = PubKey.Encrypt(SharedSecret, sizeof(SharedSecret), EncryptedSecret, sizeof(EncryptedSecret)); + if (res < 0) + { + Log("Shared secret encryption failed: %d (0x%x)", res, res); + return; + } m_ServerEncryptor.Init(SharedSecret, SharedSecret); m_ServerDecryptor.Init(SharedSecret, SharedSecret); // Encrypt the nonce: Byte EncryptedNonce[128]; - m_Server.GetPrivateKey().Encrypt((const Byte *)a_Nonce.data(), a_Nonce.size(), EncryptedNonce, sizeof(EncryptedNonce)); + res = PubKey.Encrypt((const Byte *)a_Nonce.data(), a_Nonce.size(), EncryptedNonce, sizeof(EncryptedNonce)); + if (res < 0) + { + Log("Nonce encryption failed: %d (0x%x)", res, res); + return; + } // Send the packet to the server: Log("Sending PACKET_ENCRYPTION_KEY_RESPONSE to the SERVER"); @@ -2880,6 +2892,11 @@ void cConnection::SendEncryptionKeyResponse(const AString & a_ServerPublicKey, c ToServer.WriteBuf(EncryptedSecret, sizeof(EncryptedSecret)); ToServer.WriteBEShort((short)sizeof(EncryptedNonce)); ToServer.WriteBuf(EncryptedNonce, sizeof(EncryptedNonce)); + DataLog(EncryptedSecret, sizeof(EncryptedSecret), "Encrypted secret (%u bytes)", (unsigned)sizeof(EncryptedSecret)); + DataLog(EncryptedNonce, sizeof(EncryptedNonce), "Encrypted nonce (%u bytes)", (unsigned)sizeof(EncryptedNonce)); + cByteBuffer Len(5); + Len.WriteVarInt(ToServer.GetReadableSpace()); + SERVERSEND(Len); SERVERSEND(ToServer); m_ServerState = csEncryptedUnderstood; m_IsServerEncrypted = true; -- cgit v1.2.3 From bc556e7f00ee28198b5ba3e46c1c06caab8fc37b Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 10 Mar 2014 12:21:18 -0700 Subject: Fixed Issues in ProtoProxy --- Tools/ProtoProxy/Connection.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index 91d2fc42f..be908f303 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -131,8 +131,6 @@ } \ } - -#define MAX_ENC_LEN 1024 @@ -473,14 +471,14 @@ bool cConnection::SendData(SOCKET a_Socket, cByteBuffer & a_Data, const char * a -bool cConnection::SendEncryptedData(SOCKET a_Socket, cAESCFBEncryptor & a_Encryptor, const char * a_Data, int a_Size, const char * a_Peer) +bool cConnection::SendEncryptedData(SOCKET a_Socket, cAESCFBEncryptor & a_Encryptor, const char * a_Data, size_t a_Size, const char * a_Peer) { DataLog(a_Data, a_Size, "Encrypting %d bytes to %s", a_Size, a_Peer); const Byte * Data = (const Byte *)a_Data; while (a_Size > 0) { Byte Buffer[64 KiB]; - int NumBytes = (a_Size > sizeof(Buffer)) ? sizeof(Buffer) : a_Size; + size_t NumBytes = (a_Size > sizeof(Buffer)) ? sizeof(Buffer) : a_Size; a_Encryptor.ProcessData(Buffer, Data, NumBytes); bool res = SendData(a_Socket, (const char *)Buffer, NumBytes, a_Peer); if (!res) @@ -2263,7 +2261,9 @@ bool cConnection::HandleServerSpawnObjectVehicle(void) HANDLE_SERVER_PACKET_READ(ReadByte, Byte, Yaw); HANDLE_SERVER_PACKET_READ(ReadBEInt, int, DataIndicator); AString ExtraData; - short VelocityX, VelocityY, VelocityZ; + short VelocityX = 0; + short VelocityY = 0; + short VelocityZ = 0; if (DataIndicator != 0) { HANDLE_SERVER_PACKET_READ(ReadBEShort, short, SpeedX); @@ -2697,7 +2697,7 @@ bool cConnection::ParseMetadata(cByteBuffer & a_Buffer, AString & a_Metadata) a_Metadata.push_back(x); while (x != 0x7f) { - int Index = ((unsigned)((unsigned char)x)) & 0x1f; // Lower 5 bits = index + //int Index = ((unsigned)((unsigned char)x)) & 0x1f; // Lower 5 bits = index int Type = ((unsigned)((unsigned char)x)) >> 5; // Upper 3 bits = type int Length = 0; switch (Type) @@ -2772,7 +2772,7 @@ void cConnection::LogMetadata(const AString & a_Metadata, size_t a_IndentCount) { int Index = ((unsigned)((unsigned char)a_Metadata[pos])) & 0x1f; // Lower 5 bits = index int Type = ((unsigned)((unsigned char)a_Metadata[pos])) >> 5; // Upper 3 bits = type - int Length = 0; + //int Length = 0; switch (Type) { case 0: @@ -2827,7 +2827,7 @@ void cConnection::LogMetadata(const AString & a_Metadata, size_t a_IndentCount) ASSERT(!"Cannot parse item description from metadata"); return; } - int After = bb.GetReadableSpace(); + //int After = bb.GetReadableSpace(); int BytesConsumed = BytesLeft - bb.GetReadableSpace(); Log("%sslot[%d] = %s (%d bytes)", Indent.c_str(), Index, ItemDesc.c_str(), BytesConsumed); -- cgit v1.2.3 From 98e15a34a416c31d4689836f4f38161f1270513c Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 10 Mar 2014 13:18:53 -0700 Subject: Fixed xofts issues --- Tools/ProtoProxy/Connection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index be908f303..73688d310 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -2697,7 +2697,7 @@ bool cConnection::ParseMetadata(cByteBuffer & a_Buffer, AString & a_Metadata) a_Metadata.push_back(x); while (x != 0x7f) { - //int Index = ((unsigned)((unsigned char)x)) & 0x1f; // Lower 5 bits = index + // int Index = ((unsigned)((unsigned char)x)) & 0x1f; // Lower 5 bits = index int Type = ((unsigned)((unsigned char)x)) >> 5; // Upper 3 bits = type int Length = 0; switch (Type) -- cgit v1.2.3 From ef58b0eb54c700800597031c37c3e76fef87cdfb Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 12 Mar 2014 09:49:37 -0700 Subject: Fixed comments an assert --- Tools/ProtoProxy/Connection.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index 73688d310..46119ff42 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -2772,7 +2772,7 @@ void cConnection::LogMetadata(const AString & a_Metadata, size_t a_IndentCount) { int Index = ((unsigned)((unsigned char)a_Metadata[pos])) & 0x1f; // Lower 5 bits = index int Type = ((unsigned)((unsigned char)a_Metadata[pos])) >> 5; // Upper 3 bits = type - //int Length = 0; + // int Length = 0; switch (Type) { case 0: @@ -2827,7 +2827,7 @@ void cConnection::LogMetadata(const AString & a_Metadata, size_t a_IndentCount) ASSERT(!"Cannot parse item description from metadata"); return; } - //int After = bb.GetReadableSpace(); + // int After = bb.GetReadableSpace(); int BytesConsumed = BytesLeft - bb.GetReadableSpace(); Log("%sslot[%d] = %s (%d bytes)", Indent.c_str(), Index, ItemDesc.c_str(), BytesConsumed); -- cgit v1.2.3 From e3646fc877f52c848cfb8a383fdbe89140663199 Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 14 Mar 2014 08:05:35 -0700 Subject: Fixed a couple of unneeded returns in ProtoProxy --- Tools/ProtoProxy/Connection.cpp | 4 ---- 1 file changed, 4 deletions(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index 46119ff42..f02b503f1 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -387,8 +387,6 @@ bool cConnection::RelayFromServer(void) return CLIENTSEND(Buffer, res); } } - - return true; } @@ -425,8 +423,6 @@ bool cConnection::RelayFromClient(void) return SERVERSEND(Buffer, res); } } - - return true; } -- cgit v1.2.3 From 446a6515029e66b1f76358dcee1ccfe59c432bdd Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 4 Apr 2014 08:55:48 +0200 Subject: ProtoProxy: Fixed a few Clang and MSVC warnings. --- Tools/ProtoProxy/Connection.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'Tools/ProtoProxy/Connection.cpp') diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp index f02b503f1..d9b8e3dd1 100644 --- a/Tools/ProtoProxy/Connection.cpp +++ b/Tools/ProtoProxy/Connection.cpp @@ -387,6 +387,8 @@ bool cConnection::RelayFromServer(void) return CLIENTSEND(Buffer, res); } } + ASSERT(!"Unhandled server state while relaying from server"); + return false; } @@ -423,6 +425,8 @@ bool cConnection::RelayFromClient(void) return SERVERSEND(Buffer, res); } } + ASSERT(!"Unhandled server state while relaying from client"); + return false; } @@ -438,11 +442,11 @@ double cConnection::GetRelativeTime(void) -bool cConnection::SendData(SOCKET a_Socket, const char * a_Data, int a_Size, const char * a_Peer) +bool cConnection::SendData(SOCKET a_Socket, const char * a_Data, size_t a_Size, const char * a_Peer) { - DataLog(a_Data, a_Size, "Sending data to %s, %d bytes", a_Peer, a_Size); + DataLog(a_Data, a_Size, "Sending data to %s, %u bytes", a_Peer, (unsigned)a_Size); - int res = send(a_Socket, a_Data, a_Size, 0); + int res = send(a_Socket, a_Data, (int)a_Size, 0); if (res <= 0) { Log("%s closed the socket: %d, %d; aborting connection", a_Peer, res, SocketError); -- cgit v1.2.3