Fixed RSA key generation
This commit is contained in:
parent
41c8916e29
commit
5318e1afcf
5 changed files with 28 additions and 13 deletions
14
main.c
14
main.c
|
@ -21,22 +21,27 @@
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
map config = config_load();
|
map config = config_load();
|
||||||
//TODO: Kijavítani ezt
|
|
||||||
RSA* r = createRSAfromFile("private.pem",0);
|
RSA* r = createRSAfromFile("private.pem",0);
|
||||||
if(r == NULL){
|
if(r == NULL){
|
||||||
logger_log("RSA key not found! Generating a new one...");
|
logger_log("RSA key not found! Generating a new one...");
|
||||||
r = generate_key();
|
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];
|
char priv[2049];
|
||||||
RSA_getPublicKey(r,pub);
|
RSA_getPublicKey(r,pub);
|
||||||
RSA_getPrivateKey(r,priv);
|
RSA_getPrivateKey(r,priv);
|
||||||
|
RSA_free(r);
|
||||||
char buf[513];
|
char buf[513];
|
||||||
char id[MD5_DIGEST_LENGTH];
|
char id[MD5_DIGEST_LENGTH];
|
||||||
|
|
||||||
md5(pub,id);
|
md5(priv,id);
|
||||||
node_data mynode;
|
node_data mynode;
|
||||||
strcpy(mynode.id, id);
|
strcpy(mynode.id, id);
|
||||||
strcpy(mynode.pubkey_str, pub);
|
strcpy(mynode.pubkey_str, pub);
|
||||||
|
@ -154,7 +159,6 @@ int main(void) {
|
||||||
if (k != -1) {
|
if (k != -1) {
|
||||||
logger_log("Peer disconnected(%s->%s)", inet_ntoa(peerList1.array[k].sockaddr.sin_addr),peerList1.array[k].peerData.id);
|
logger_log("Peer disconnected(%s->%s)", inet_ntoa(peerList1.array[k].sockaddr.sin_addr),peerList1.array[k].peerData.id);
|
||||||
peer_removeFromList(&peerList1, k);
|
peer_removeFromList(&peerList1, k);
|
||||||
closesocket(sock);
|
|
||||||
FD_CLR(sock, &master);
|
FD_CLR(sock, &master);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
|
|
@ -39,7 +39,6 @@ RSA *createRSAfromFile(char *file, int pub) {
|
||||||
|
|
||||||
if(fp == NULL)
|
if(fp == NULL)
|
||||||
{
|
{
|
||||||
printf("Unable to open file %s \n",file);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
RSA *rsa= RSA_new() ;
|
RSA *rsa= RSA_new() ;
|
||||||
|
@ -72,6 +71,7 @@ RSA *generate_key() {
|
||||||
BIO_free_all(bp_private);
|
BIO_free_all(bp_private);
|
||||||
RSA_free(r);
|
RSA_free(r);
|
||||||
BN_free(bne);
|
BN_free(bne);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = RSA_new();
|
r = RSA_new();
|
||||||
|
@ -81,15 +81,16 @@ RSA *generate_key() {
|
||||||
BIO_free_all(bp_private);
|
BIO_free_all(bp_private);
|
||||||
RSA_free(r);
|
RSA_free(r);
|
||||||
BN_free(bne);
|
BN_free(bne);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bp_public = BIO_new_file("public.pem", "w+");
|
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){
|
if(ret != 1){
|
||||||
BIO_free_all(bp_public);
|
BIO_free_all(bp_public);
|
||||||
BIO_free_all(bp_private);
|
BIO_free_all(bp_private);
|
||||||
RSA_free(r);
|
RSA_free(r);
|
||||||
BN_free(bne);
|
BN_free(bne);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bp_private = BIO_new_file("private.pem", "w+");
|
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
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <openssl/rsa.h>
|
#include <openssl/rsa.h>
|
||||||
#include <openssl/pem.h>
|
#include <openssl/pem.h>
|
||||||
#include <openssl/err.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);
|
int base64Encode(const unsigned char* input ,size_t len, char** outpub);
|
||||||
size_t calcDecodeLength(const char* b64input);
|
size_t calcDecodeLength(const char* b64input);
|
||||||
int base64Decode(const char* input, unsigned char**buffer,size_t* len);
|
int base64Decode(const char* input, unsigned char**buffer,size_t* len);
|
||||||
void printLastError(char *msg);
|
void printOpenSSLError(char *msg);
|
|
@ -24,7 +24,6 @@ int peer_ConnetctTo(char* ip,int port,peerList* peerList, node_data my,fd_set* f
|
||||||
char handshake[DEFAULT_BUFLEN],*base64Key;
|
char handshake[DEFAULT_BUFLEN],*base64Key;
|
||||||
base64Encode((unsigned char*)my.pubkey_str,strlen(my.pubkey_str),&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);
|
sprintf(handshake,"@id=%s&port=%d&pubkey=%s",my.id,my.port,base64Key);
|
||||||
logger_log("%s",handshake);
|
|
||||||
if(strlen(my.nick) != 0) {
|
if(strlen(my.nick) != 0) {
|
||||||
char buf[DEFAULT_BUFLEN];
|
char buf[DEFAULT_BUFLEN];
|
||||||
ZeroMemory(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;
|
return -1;
|
||||||
}
|
}
|
||||||
map m = getHandshakeData(buf);
|
map m = getHandshakeData(buf);
|
||||||
|
map_dump(m);
|
||||||
node_data node;
|
node_data node;
|
||||||
strcpy(node.ip,ip);
|
strcpy(node.ip,ip);
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ void peer_addTolist(peerList *list, struct peer p){
|
||||||
list->array[list->length++] = p;
|
list->array[list->length++] = p;
|
||||||
}
|
}
|
||||||
void peer_removeFromList(struct peerList* list, int i){
|
void peer_removeFromList(struct peerList* list, int i){
|
||||||
|
closesocket(list->array[i].socket);
|
||||||
for (int k=i; k < list->length-1; ++k)
|
for (int k=i; k < list->length-1; ++k)
|
||||||
list->array[k] =list->array[k+1];
|
list->array[k] =list->array[k+1];
|
||||||
list->length--;
|
list->length--;
|
||||||
|
|
5
test.c
5
test.c
|
@ -9,10 +9,11 @@
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
printf("%d\n",SOMAXCONN);
|
generate_key();
|
||||||
RSA* r = createRSAfromFile("public.pem",1);
|
RSA* r = createRSAfromFile("public.pem",1);
|
||||||
|
|
||||||
char plainText[1024/8] = "Hello this is Ravi"; //key length : 2048
|
char plainText[1024/8] = "Hello this is Ravi"; //key length : 2048
|
||||||
char pubkey[1024];
|
char pubkey[4096];
|
||||||
RSA_getPublicKey(r,pubkey);
|
RSA_getPublicKey(r,pubkey);
|
||||||
printf("%s\n",pubkey);
|
printf("%s\n",pubkey);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue