I want to setup a secure socket connection between a Server and a Client. My protocol is:
- The Client generates a RSA keypair and send it's public key to the server.
- The Server generates a AES key, encypts the key with the client's public key and ends it to the client.
- The client decrypts the AES key with the private key.
- All messages are encrypted and decrypted with the AES key.
The problem:
This protocol is very vulnerable against Man-in-the-middle attacks. A attacker could easily send his own public key to the server, and receive the AES key. The attacker can spoof to be the server to the client and to be the client to the server.
How to make my connection secure?