MCPcopy Create free account
hub / github.com/cuberite/cuberite / SendEncryptionKeyResponse

Method SendEncryptionKeyResponse

Tools/ProtoProxy/Connection.cpp:2861–2903  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2859
2860
2861void cConnection::SendEncryptionKeyResponse(const AString & a_ServerPublicKey, const AString & a_Nonce)
2862{
2863 // Generate the shared secret and encrypt using the server's public key
2864 Byte SharedSecret[16];
2865 Byte EncryptedSecret[128];
2866 memset(SharedSecret, 0, sizeof(SharedSecret)); // Use all zeroes for the initial secret
2867 cPublicKey PubKey(a_ServerPublicKey);
2868 int res = PubKey.Encrypt(SharedSecret, sizeof(SharedSecret), EncryptedSecret, sizeof(EncryptedSecret));
2869 if (res < 0)
2870 {
2871 Log("Shared secret encryption failed: %d (0x%x)", res, res);
2872 return;
2873 }
2874
2875 m_ServerEncryptor.Init(SharedSecret, SharedSecret);
2876 m_ServerDecryptor.Init(SharedSecret, SharedSecret);
2877
2878 // Encrypt the nonce:
2879 Byte EncryptedNonce[128];
2880 res = PubKey.Encrypt((const Byte *)a_Nonce.data(), a_Nonce.size(), EncryptedNonce, sizeof(EncryptedNonce));
2881 if (res < 0)
2882 {
2883 Log("Nonce encryption failed: %d (0x%x)", res, res);
2884 return;
2885 }
2886
2887 // Send the packet to the server:
2888 Log("Sending PACKET_ENCRYPTION_KEY_RESPONSE to the SERVER");
2889 cByteBuffer ToServer(1024);
2890 ToServer.WriteByte(0x01); // To server: Encryption key response
2891 ToServer.WriteBEShort((short)sizeof(EncryptedSecret));
2892 ToServer.WriteBuf(EncryptedSecret, sizeof(EncryptedSecret));
2893 ToServer.WriteBEShort((short)sizeof(EncryptedNonce));
2894 ToServer.WriteBuf(EncryptedNonce, sizeof(EncryptedNonce));
2895 DataLog(EncryptedSecret, sizeof(EncryptedSecret), "Encrypted secret (%u bytes)", (unsigned)sizeof(EncryptedSecret));
2896 DataLog(EncryptedNonce, sizeof(EncryptedNonce), "Encrypted nonce (%u bytes)", (unsigned)sizeof(EncryptedNonce));
2897 cByteBuffer Len(5);
2898 Len.WriteVarInt(ToServer.GetReadableSpace());
2899 SERVERSEND(Len);
2900 SERVERSEND(ToServer);
2901 m_ServerState = csEncryptedUnderstood;
2902 m_IsServerEncrypted = true;
2903}
2904
2905
2906

Callers

nothing calls this directly

Calls 8

EncryptMethod · 0.80
sizeMethod · 0.80
WriteBEShortMethod · 0.80
GetReadableSpaceMethod · 0.80
InitMethod · 0.45
WriteByteMethod · 0.45
WriteBufMethod · 0.45
WriteVarIntMethod · 0.45

Tested by

no test coverage detected