Fixed RSA key generation

This commit is contained in:
Bazsalanszky 2019-10-28 13:43:27 +01:00
parent 41c8916e29
commit 68cf643d82
Signed by: Bazsalanszky
GPG key ID: 0998CF5510B134D9
3 changed files with 16 additions and 6 deletions

View file

@ -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);
}

View file

@ -14,6 +14,7 @@
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#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);
void printOpenSSLError(char *msg);

View file

@ -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--;