Jump to content
  • Advertisement
Sign in to follow this  
Enrico

RakNet & builtin encryption problems

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I tried to make an example with RakClient and RakServer using builtin encryption. This works fine, when I use to RakPeer to connect my systems. But exactly the same procedure doesnt work with server and client. I looked through the source of RakServer and RakClient and both call RakPeer::InitializeSecurity(). So both initialisation calls, peer and client/server, are the same. But it doesnt work. In the client/server environment the public key never matches. Here is my whole sample program:
#include <stdio.h>
#include <stdlib.h>
#include <raknet/GetTime.h>
#include <raknet/Rand.h>
#include <raknet/RSACrypt.h>
#include <raknet/DataBlockEncryptor.h>
#include <raknet/Rand.h>
#include <raknet/RakClientInterface.h>
#include <raknet/RakServerInterface.h>
#include <raknet/PacketEnumerations.h>
#include <raknet/RakNetworkFactory.h>

#ifdef _WIN32
#include <windows.h> // Sleep
#else
#include <unistd.h> // usleep
#endif
void PrintPacketHeader(Packet *packet)
{
	switch (packet->data[0])
	{
		case ID_RSA_PUBLIC_KEY_MISMATCH:
			printf("Public key mismatch.\nThe connecting system's public key does not\nmatch what the sender sent.\n");
			break;
		case ID_CONNECTION_REQUEST_ACCEPTED:
			printf("Connection request accepted.\n");
			break;
		case ID_NEW_INCOMING_CONNECTION:
			printf("New incoming connection.\n");
			break;
		case ID_RECEIVED_STATIC_DATA:
			printf("Got static data.\n");
			break;
		case ID_MODIFIED_PACKET:
			printf("Packet checksum invalid.  Either RSA decrypt function gave the wrong value\nor the packet was tampered with.\n");
			break;
		default:
			printf("Got unknown packet type %i\n", packet->data[0]);
			break;
	}
}


#if 0
int main(void)
{
	// RSACrypt is a class that handles RSA encryption/decryption internally
	big::RSACrypt<RSA_BIT_SIZE> rsacrypt;

	// These are the sizes necessary for e,n,p,q
	// e,n is the public key
	// p,q is the private key
	u32 p;
	RSA_BIT_SIZE q;
	BIGHALFSIZE(RSA_BIT_SIZE, e);
	BIGHALFSIZE(RSA_BIT_SIZE, n);

	FILE *fp;
	unsigned long time;
	RakPeerInterface *host, *client;
	host=RakNetworkFactory::GetRakPeerInterface();
	client=RakNetworkFactory::GetRakPeerInterface();
	Packet *packet;
	bool peer1GotMessage, peer2GotMessage;

	rsacrypt.generateKeys();
	rsacrypt.getPublicKey(p,q);
	rsacrypt.getPrivateKey(e,n);


	host->InitializeSecurity(0, 0,(char*)e, (char*)n);
	client->InitializeSecurity((char*)&p, (char*)q, 0, 0);

	printf("Initializing peers.\n");
	host->Initialize(8,1234,0);
	host->SetMaximumIncomingConnections(8);
	client->Initialize(1,0,0);
	client->Connect("127.0.0.1", 1234, 0, 0);
	printf("Running connection for 2 seconds.\n");

	peer1GotMessage=false;
	peer2GotMessage=false;
	time = RakNet::GetTime() + 2000;
	while (RakNet::GetTime() < time)
	{
		packet=host->Receive();
		if (packet)
		{
			peer1GotMessage=true;
			printf("Host got: ");
			PrintPacketHeader(packet);
			host->DeallocatePacket(packet);
		}
		packet=client->Receive();
		if (packet)
		{
			peer2GotMessage=true;
			printf("Connecting system got: ");
			PrintPacketHeader(packet);
			client->DeallocatePacket(packet);
		}
		Sleep(30);
	}
	client->Disconnect(0);
	host->Disconnect(0);

	RakNetworkFactory::DestroyRakPeerInterface(host);
	RakNetworkFactory::DestroyRakPeerInterface(client);
}
#else
int main(void)
{
	// These are the sizes necessary for e,n,p,q
	// e,n is the public key
	// p,q is the private key
	u32 p;
	RSA_BIT_SIZE q;
	BIGHALFSIZE(RSA_BIT_SIZE, e);
	BIGHALFSIZE(RSA_BIT_SIZE, n);

	// RSACrypt is a class that handles RSA encryption/decryption internally
	big::RSACrypt<RSA_BIT_SIZE> rsacrypt;

	rsacrypt.generateKeys();
	rsacrypt.getPublicKey(p,q);
	rsacrypt.getPrivateKey(e,n);

	RakServerInterface *server = RakNetworkFactory::GetRakServerInterface();
	server->InitializeSecurity((char*)e, (char*)n);

	RakClientInterface *client = RakNetworkFactory::GetRakClientInterface();
	client->InitializeSecurity((char*)p, (char*)q);

	printf("Initializing interfaces.\n");
	server->Start(100, 0, 0, 60100);
	client->Connect("127.0.0.1", 60100, 0, 0, 0);

	unsigned long time = RakNet::GetTime() + 2000;
	while (RakNet::GetTime() < time)
	{
		Packet *packet=server->Receive();
		if (packet)
		{
			printf("Host got: ");
			PrintPacketHeader(packet);
			server->DeallocatePacket(packet);
		}
		packet=client->Receive();
		if (packet)
		{
			printf("Connecting system got: ");
			PrintPacketHeader(packet);
			client->DeallocatePacket(packet);
		}
		Sleep(30);
	}
	client->Disconnect(0);
	server->Disconnect(0);

	RakNetworkFactory::DestroyRakServerInterface(server);
	RakNetworkFactory::DestroyRakClientInterface(client);

	return 0;
}
#endif
I can connect the clients to the server with this code:
client->InitializeSecurity(NULL, NULL); 
But this means, I dont need to distribute my public keys. From the soure I see, that RakNet will generate new private and public keys internal, when I pass NULL for both arguments. So I could skip the creation phase and generate the keys via RakNet by passing NULL to both InitializeSecurity()-calls. But with that method, key creation takes some time and I dont know, if my server has powerful hardware later to generate keys on the fly for every client... Thanks in advance :)

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!