Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool cConnection::HandleClientLoginEncryptionKeyResponse(void)
- {
- HANDLE_CLIENT_PACKET_READ(ReadBEShort, short, SharedSecretLength);
- AString SharedSecret;
- if (!m_ClientBuffer.ReadString(SharedSecret, SharedSecretLength))
- {
- return false;
- }
- HANDLE_CLIENT_PACKET_READ(ReadBEShort, short, NonceLength);
- AString Nonce;
- if (!m_ClientBuffer.ReadString(Nonce, NonceLength))
- {
- return false;
- }
- m_ClientBuffer.CommitRead();
- Byte DecryptedSharedSecret[16];
- cRsaPrivateKey PrivKey(m_Server.GetPrivateKey());
- int res = PrivKey.Decrypt((const Byte *)SharedSecret.data(), SharedSecretLength, DecryptedSharedSecret, sizeof(DecryptedSharedSecret));
- if (res < 0)
- {
- Log("Shared secret decryption failed: %d (0x%x)", res, res);
- return false;
- }
- //m_SharedSecret = DecryptedSharedSecret;
- Byte NewSharedSecret[128];
- cCryptoKey PubKey(m_ServerPublicKey);
- int res2 = PubKey.Encrypt((const Byte *)DecryptedSharedSecret, sizeof(DecryptedSharedSecret), NewSharedSecret, sizeof(NewSharedSecret));
- if (res2 < 0)
- {
- Log("Shared secret encryption failed: %d (0x%x)", res2, res2);
- return false;
- }
- // Send packet to server with our own shared secret
- cByteBuffer Packet(1024);
- Packet.WriteVarInt(0x01);
- Packet.WriteBEShort(sizeof(NewSharedSecret));
- Packet.WriteBuf(NewSharedSecret, sizeof(NewSharedSecret));
- Packet.WriteBEShort(NonceLength);
- Packet.WriteBuf(Nonce.data(), NonceLength);
- AString Pkt;
- Packet.ReadAll(Pkt);
- cByteBuffer ToServer(1024);
- ToServer.WriteVarUTF8String(Pkt);
- SERVERSEND(ToServer);
- m_ServerEncryptor.Init(DecryptedSharedSecret, DecryptedSharedSecret);
- m_ServerDecryptor.Init(DecryptedSharedSecret, DecryptedSharedSecret);
- m_ClientEncryptor.Init(DecryptedSharedSecret, DecryptedSharedSecret);
- m_ClientDecryptor.Init(DecryptedSharedSecret, DecryptedSharedSecret);
- m_ServerState = csEncryptedUnderstood;
- m_ClientState = csEncryptedUnderstood;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement