diff --git a/CMakeLists.txt b/CMakeLists.txt index a5cecae..db69f72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,9 @@ project(p2p C) set(CMAKE_C_STANDARD 99) set(GCC_COVERAGE_COMPILE_FLAGS "-Werror") -add_executable(p2p main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c) -add_executable(p2p-2 main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c) -add_executable(p2p-3 main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c) +add_executable(p2p main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h) +add_executable(p2p-2 main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h) +add_executable(p2p-3 main.c peer.h peer.c utility.c utility.h modules/webio/webio.h modules/webio/webio.c modules/config.h modules/config.c modules/map.h modules/map.c modules/crypto.c modules/crypto.h) set_target_properties( p2p-2 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/p2p-2 ) set_target_properties( p2p-3 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/p2p-3 ) # Függvények kipróbálására hoztam létre diff --git a/main.c b/main.c index 3463e0f..8d44eb8 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,12 @@ #include #include -#include "peer.h" +#include "peer.h" +#include "modules/crypto.h" #include "modules/webio/webio.h" #include "modules/config.h" #pragma comment(lib, "ws2_32.lib") -//#include "webinterface.h" #ifdef RANDOM_PORT #define DEFAULT_PORT "0" @@ -20,27 +20,29 @@ int main(void) { map config = config_load(); - FILE *seed_file; - seed_file = fopen("seed.txt", "r"); - char seed[513]; - if (seed_file == NULL) { - logger_log("Seed not found! Generating a new one..."); - strcpy(seed, generateSeed(512)); - seed_file = fopen("seed.txt", "w"); - fprintf(seed_file, "%s", seed); + RSA* r = createRSAfromFile("private.pem",0); + if(r == NULL){ + logger_log("RSA key not found! Generating a new one..."); + r = generate_key(); - } else { - fgets(seed, 512, seed_file); } - fclose(seed_file); + FILE *pubkey; + pubkey = fopen("public.pem", "r"); + char pub[257]; + char buf[257]; char id[MD5_DIGEST_LENGTH]; - md5(seed, id); - + ZeroMemory(pub,257); + while(fgets(buf,256,pubkey)!= NULL){ + if(buf[0] == '-') continue; + strcat(pub,buf); + } + md5(pub,id); node_data mynode; strcpy(mynode.id, id); - - if(map_isFound(config,"nickname")) - strcpy(mynode.nick,map_getValue(config,"nickname")); + strcpy(mynode.pubkey, pub); + if(map_isFound(config,"nickname")) { + strcpy(mynode.nick, map_getValue(config, "nickname")); + } if(map_isFound(config,"port")) mynode.port = atoi(map_getValue(config,"port")); else @@ -52,7 +54,6 @@ int main(void) { logger_log("Error at startup! Error code: %d", WSAGetLastError()); WSACleanup(); } - struct addrinfo hint = {}; struct addrinfo *result = NULL; @@ -150,7 +151,7 @@ int main(void) { char *command =(char*) malloc(64); sprintf(command,"start http://127.0.0.1:%d",ntohs(webIo.sockaddr.sin_port)); - system(command); + // system(command); free(command); logger_log("Starting main loop..."); diff --git a/modules/crypto.c b/modules/crypto.c new file mode 100644 index 0000000..f77aaa2 --- /dev/null +++ b/modules/crypto.c @@ -0,0 +1,119 @@ +// +// Készítette: Toldi Balázs Ádám +// Dátum: 2019. 10. 20. +// +#include "crypto.h" + +#undef APPMACROS_ONLY +#include + + +RSA * createRSA(unsigned char * key,int public) +{ + RSA *rsa= NULL; + BIO *keybio ; + keybio = BIO_new_mem_buf(key, -1); + if (keybio==NULL) + { + printf( "Failed to create key BIO"); + return 0; + } + if(public) + { + rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL); + } + else + { + rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL); + } + if(rsa == NULL) + { + printf( "Failed to create RSA"); + } + + return rsa; +} + +RSA *createRSAfromFile(char *file, int pub) { + FILE * fp = fopen(file,"rb"); + + if(fp == NULL) + { + printf("Unable to open file %s \n",file); + return NULL; + } + RSA *rsa= RSA_new() ; + if(pub) + { + rsa = PEM_read_RSA_PUBKEY(fp, &rsa,NULL, NULL); + } + else + { + rsa = PEM_read_RSAPrivateKey(fp, &rsa,NULL, NULL); + } + return rsa; +} + + + +RSA *generate_key() { + int ret = 0; + RSA *r = NULL; + BIGNUM *bne = NULL; + BIO *bp_public = NULL, *bp_private = NULL; + + int bits = 1024; + unsigned long e = RSA_F4; + + // 1. generate rsa key + bne = BN_new(); + ret = BN_set_word(bne,e); + if(ret != 1){ + BIO_free_all(bp_public); + BIO_free_all(bp_private); + RSA_free(r); + BN_free(bne); + } + + r = RSA_new(); + ret = RSA_generate_key_ex(r, bits, bne, NULL); + if(ret != 1){ + BIO_free_all(bp_public); + BIO_free_all(bp_private); + RSA_free(r); + BN_free(bne); + } + + // 2. save public key + bp_public = BIO_new_file("public.pem", "w+"); + ret = PEM_write_bio_RSAPublicKey(bp_public, r); + if(ret != 1){ + BIO_free_all(bp_public); + BIO_free_all(bp_private); + RSA_free(r); + BN_free(bne); + } + + // 3. save private key + bp_private = BIO_new_file("private.pem", "w+"); + ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL); + BIO_free_all(bp_public); + BIO_free_all(bp_private); + RSA_free(r); + BN_free(bne); + + return r; + +} + +int public_encrypt(unsigned char *data, int data_len, unsigned char *key, unsigned char *encrypted) { + RSA * rsa = createRSA(key,1); + int result = RSA_public_encrypt(data_len,data,encrypted,rsa,RSA_PKCS1_PADDING); + return result; +} + +int private_decrypt(unsigned char *enc_data, int data_len, unsigned char *key, unsigned char *decrypted) { + RSA * rsa = createRSA(key,0); + int result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,RSA_PKCS1_PADDING); + return result; +} diff --git a/modules/crypto.h b/modules/crypto.h new file mode 100644 index 0000000..74f1f3b --- /dev/null +++ b/modules/crypto.h @@ -0,0 +1,18 @@ +// +// Készítette: Toldi Balázs Ádám +// Dátum: 2019. 10. 20. +// + +#pragma once + +#include +#include + + + +RSA * createRSA(unsigned char * key,int public); +RSA* createRSAfromFile(char* filename,int pub); +RSA* generate_key(); + +int public_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted); +int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted); \ No newline at end of file diff --git a/modules/map.h b/modules/map.h index bf57af2..8d4ef82 100644 --- a/modules/map.h +++ b/modules/map.h @@ -12,7 +12,7 @@ typedef struct Pair{ char key[65]; - char value[65]; + char value[513]; }pair; typedef struct Map diff --git a/modules/webio/webio.c b/modules/webio/webio.c index 3a24f7b..6356963 100644 --- a/modules/webio/webio.c +++ b/modules/webio/webio.c @@ -183,7 +183,6 @@ int webio_handleGETrequest(SOCKET client,WebIO wio,char* file,peerList list){ return -2; } else{ strcat(path, file); - printf("%s\n",file); FILE *fp; fp = fopen(path, "r"); @@ -269,7 +268,6 @@ void webio_getHeader(char* folder,char**result) { char path[65]; strcpy(path, folder); strcat(path, "/header.html"); - printf(path); FILE* fp; fp = fopen(path, "r"); diff --git a/peer.c b/peer.c index 0d3d925..756c99b 100644 --- a/peer.c +++ b/peer.c @@ -21,7 +21,7 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f } logger_log("Connected to peer!Sending handshake..."); char handshake[DEFAULT_BUFLEN]; - sprintf(handshake,"@id=%s&port=%d",my.id,my.port); + sprintf(handshake,"@id=%s&port=%d&pubkey=%s",my.id,my.port,my.pubkey); if(strlen(my.nick) != 0) { char buf[DEFAULT_BUFLEN]; @@ -68,6 +68,14 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f logger_log("Error: Invalid response!ID not found in handshake."); return -1; } + + if(map_isFound(m,"pubkey")) { + strcpy(node.pubkey, map_getValue(m, "pubkey")); + } else { + logger_log("Error: Invalid response!RSA public key not found in handshake."); + return -1; + } + if(map_isFound(m,"port")) { node.port = atoi(map_getValue(m, "port")); } else { @@ -78,7 +86,7 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f if(map_isFound(m,"nickname")) { strcpy(node.nick, map_getValue(m, "nickname")); } - + map_dump(m); Peer p; p.peerData = node; p.socket = sock; @@ -139,6 +147,13 @@ int peer_HandleConnection(SOCKET listening,peerList *peerList, node_data my,fd_s logger_log("Error: Invalid response!ID not found in handshake."); return -1; } + if(map_isFound(m,"pubkey")) { + strcpy(node.pubkey, map_getValue(m, "pubkey")); + } else { + logger_log("Error: Invalid response!RSA public key not found in handshake."); + return -1; + } + if(map_isFound(m,"port")) { node.port = atoi(map_getValue(m, "port")); } else { @@ -161,9 +176,11 @@ int peer_HandleConnection(SOCKET listening,peerList *peerList, node_data my,fd_s closesocket(sock); return -1; } + map_dump(m); logger_log("Handshake recived! Sending response!"); char* handshake = (char*) calloc(DEFAULT_BUFLEN, sizeof(char)); - sprintf(handshake,"@id=%s&port=%d",my.id,my.port); + sprintf(handshake,"@id=%s&port=%d&pubkey=%s",my.id,my.port,my.pubkey); + if(strlen(my.nick) != 0) { ZeroMemory(buf,DEFAULT_BUFLEN); sprintf(buf, "&nickname=%s",my.nick); diff --git a/peer.h b/peer.h index 0a788bb..a4b4ce1 100644 --- a/peer.h +++ b/peer.h @@ -9,6 +9,7 @@ typedef struct Node_data { char ip[NI_MAXHOST]; char id[33]; + char pubkey[256+1]; char nick[30]; int port; } node_data;