Added RSA key generation
This commit is contained in:
parent
ad4a44ec4e
commit
0595bfb891
8 changed files with 183 additions and 29 deletions
|
@ -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
|
||||
|
|
41
main.c
41
main.c
|
@ -1,12 +1,12 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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...");
|
||||
|
|
119
modules/crypto.c
Normal file
119
modules/crypto.c
Normal file
|
@ -0,0 +1,119 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 20.
|
||||
//
|
||||
#include "crypto.h"
|
||||
|
||||
#undef APPMACROS_ONLY
|
||||
#include <openssl/applink.c>
|
||||
|
||||
|
||||
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;
|
||||
}
|
18
modules/crypto.h
Normal file
18
modules/crypto.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// Készítette: Toldi Balázs Ádám
|
||||
// Dátum: 2019. 10. 20.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
|
||||
|
||||
|
||||
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);
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
typedef struct Pair{
|
||||
char key[65];
|
||||
char value[65];
|
||||
char value[513];
|
||||
}pair;
|
||||
|
||||
typedef struct Map
|
||||
|
|
|
@ -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");
|
||||
|
|
23
peer.c
23
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);
|
||||
|
|
1
peer.h
1
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;
|
||||
|
|
Loading…
Reference in a new issue