From 5318e1afcf42772a2e582e11490ed66659478277 Mon Sep 17 00:00:00 2001 From: Bazsalanszky Date: Mon, 28 Oct 2019 13:39:27 +0100 Subject: [PATCH] Fixed RSA key generation --- main.c | 14 +++++++++----- modules/crypto.c | 15 ++++++++++++--- modules/crypto.h | 3 ++- modules/peer.c | 4 ++-- test.c | 5 +++-- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/main.c b/main.c index 651a58f..b20376e 100644 --- a/main.c +++ b/main.c @@ -21,22 +21,27 @@ int main(void) { map config = config_load(); - //TODO: KijavĂ­tani ezt + RSA* r = createRSAfromFile("private.pem",0); if(r == NULL){ logger_log("RSA key not found! Generating a new one..."); r = generate_key(); - + if(r == NULL){ + printOpenSSLError("Error generating RSA key pair!"); + return 2; + } + r = createRSAfromFile("private.pem",0); } - char pub[513]; + char pub[16964]; char priv[2049]; RSA_getPublicKey(r,pub); RSA_getPrivateKey(r,priv); + RSA_free(r); char buf[513]; char id[MD5_DIGEST_LENGTH]; - md5(pub,id); + md5(priv,id); node_data mynode; strcpy(mynode.id, id); strcpy(mynode.pubkey_str, pub); @@ -154,7 +159,6 @@ int main(void) { if (k != -1) { logger_log("Peer disconnected(%s->%s)", inet_ntoa(peerList1.array[k].sockaddr.sin_addr),peerList1.array[k].peerData.id); peer_removeFromList(&peerList1, k); - closesocket(sock); FD_CLR(sock, &master); } }else{ diff --git a/modules/crypto.c b/modules/crypto.c index c4bffbb..1193fb1 100644 --- a/modules/crypto.c +++ b/modules/crypto.c @@ -39,7 +39,6 @@ RSA *createRSAfromFile(char *file, int pub) { if(fp == NULL) { - printf("Unable to open file %s \n",file); return NULL; } RSA *rsa= RSA_new() ; @@ -72,6 +71,7 @@ RSA *generate_key() { BIO_free_all(bp_private); RSA_free(r); BN_free(bne); + return NULL; } r = RSA_new(); @@ -81,15 +81,16 @@ RSA *generate_key() { BIO_free_all(bp_private); RSA_free(r); BN_free(bne); + return NULL; } - bp_public = BIO_new_file("public.pem", "w+"); - ret = PEM_write_bio_RSAPublicKey(bp_public, r); + ret = PEM_write_bio_RSA_PUBKEY(bp_public, r); if(ret != 1){ BIO_free_all(bp_public); BIO_free_all(bp_private); RSA_free(r); BN_free(bne); + return NULL; } bp_private = BIO_new_file("private.pem", "w+"); @@ -187,4 +188,12 @@ int base64Decode(const char* input, unsigned char**buffer,size_t* len) { //Decod return (0); //success } +void printOpenSSLError(char *msg) { + char * err = malloc(130);; + ERR_load_crypto_strings(); + ERR_error_string(ERR_get_error(), err); + logger_log("%s ERROR: %s\n",msg, err); + free(err); +} + diff --git a/modules/crypto.h b/modules/crypto.h index 03d7ec5..e8a0087 100644 --- a/modules/crypto.h +++ b/modules/crypto.h @@ -14,6 +14,7 @@ #include #include #include +#include "../utility.h" @@ -29,4 +30,4 @@ int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, u int base64Encode(const unsigned char* input ,size_t len, char** outpub); size_t calcDecodeLength(const char* b64input); int base64Decode(const char* input, unsigned char**buffer,size_t* len); -void printLastError(char *msg); \ No newline at end of file +void printOpenSSLError(char *msg); \ No newline at end of file diff --git a/modules/peer.c b/modules/peer.c index 1edf232..2fcddc9 100644 --- a/modules/peer.c +++ b/modules/peer.c @@ -24,7 +24,6 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f char handshake[DEFAULT_BUFLEN],*base64Key; base64Encode((unsigned char*)my.pubkey_str,strlen(my.pubkey_str),&base64Key); sprintf(handshake,"@id=%s&port=%d&pubkey=%s",my.id,my.port,base64Key); - logger_log("%s",handshake); if(strlen(my.nick) != 0) { char buf[DEFAULT_BUFLEN]; ZeroMemory(buf,DEFAULT_BUFLEN); @@ -51,6 +50,7 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f return -1; } map m = getHandshakeData(buf); + map_dump(m); node_data node; strcpy(node.ip,ip); @@ -278,7 +278,7 @@ void peer_addTolist(peerList *list, struct peer p){ list->array[list->length++] = p; } void peer_removeFromList(struct peerList* list, int i){ - + closesocket(list->array[i].socket); for (int k=i; k < list->length-1; ++k) list->array[k] =list->array[k+1]; list->length--; diff --git a/test.c b/test.c index d531c42..f665b7b 100644 --- a/test.c +++ b/test.c @@ -9,10 +9,11 @@ int main(){ - printf("%d\n",SOMAXCONN); + generate_key(); RSA* r = createRSAfromFile("public.pem",1); + char plainText[1024/8] = "Hello this is Ravi"; //key length : 2048 - char pubkey[1024]; + char pubkey[4096]; RSA_getPublicKey(r,pubkey); printf("%s\n",pubkey);