generating only one keyfile

lol ima stoopid - it seems you need only private key file and you can generate the pub from it
This commit is contained in:
Václav Šmejkal 2025-01-29 20:52:16 +01:00
parent b977d0dd40
commit 8d17915a05
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -32,8 +32,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <openssl/pem.h> #include <openssl/pem.h>
#include <openssl/ec.h> #include <openssl/ec.h>
char *ecc_pub = NULL; EVP_PKEY *keypair = NULL; //KEYPAIR
char *ecc_pri = NULL;
void read_file(FILE *file, char **output) void read_file(FILE *file, char **output)
{ {
@ -58,74 +57,50 @@ void read_file(FILE *file, char **output)
//GLOBAL //GLOBAL
void why2_chat_init_keys(void) void why2_chat_init_keys(void)
{ {
//KEY FILES FILE *key; //KEY FILE
FILE *public;
FILE *private;
//GET PATH TO KEY DIR char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME")); //GET PATH TO KEY DIR
char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME")); char *key_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_KEY) + 3); //ALLOCATE THE KEY PATH
//ALLOCATE THE KEY PATHS //GET THE ACTUAL KEY PATH
char *public_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_PUB_KEY) + 3); sprintf(key_path, "%s/%s%c", path, WHY2_CHAT_KEY, '\0');
char *private_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_PRI_KEY) + 3);
//GET THE ACTUAL KEY PATHS //CHECK IF KEY EXIST
sprintf(public_path, "%s/%s%c", path, WHY2_CHAT_PUB_KEY, '\0'); if (access(path, R_OK) != 0) //NOT FOUND - CREATE IT
sprintf(private_path, "%s/%s%c", path, WHY2_CHAT_PRI_KEY, '\0');
//CHECK IF KEYS EXIST
if (access(path, R_OK) != 0)
{ {
mkdir(path, 0700); mkdir(path, 0700);
//SOME USER OUTPUT //SOME USER OUTPUT
printf("You are probably running WHY2-Chat for the first time now.\nGenerating ECC keys...\n"); printf("No ECC key found.\nGenerating...\n\n");
//VARIABLES
EVP_PKEY *pkey = NULL; //KEYPAIR
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); //CREATE CTX EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); //CREATE CTX
EVP_PKEY_keygen_init(ctx); //INIT KEYGEN EVP_PKEY_keygen_init(ctx); //INIT KEYGEN
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, WHY2_CHAT_ECC); //SETUP ECC EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, WHY2_CHAT_ECC); //SETUP ECC
EVP_PKEY_keygen(ctx, &keypair); //GENERATE ECC KEYPAIR
EVP_PKEY_keygen(ctx, &pkey); //GENERATE ECC KEYPAIR //WRITE THE KEYS INTO KEY-FILE
key = why2_fopen(key_path, "w+");
printf("Saving keys...\n"); PEM_write_PrivateKey(key, keypair, NULL, NULL, 0, NULL, NULL); //WRITE THE KEY
//WRITE THE KEYS INTO KEY-FILES
public = why2_fopen(public_path, "w+");
private = why2_fopen(private_path, "w+");
PEM_write_PrivateKey(private, pkey, NULL, NULL, 0, NULL, NULL); //WRITE PRI KEY
PEM_write_PUBKEY(public, pkey); //WRITE PUB KEY
//DEALLOCATION //DEALLOCATION
EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
} else } else
{ {
//OPEN FILES key = why2_fopen(key_path, "r"); //OPEN KEY FILE
public = why2_fopen(public_path, "r"); keypair = PEM_read_PrivateKey(key, NULL, NULL, NULL); //LOAD KEYPAIR
private = why2_fopen(private_path, "r");
//READ THE KEYS
read_file(public, &ecc_pub);
read_file(private, &ecc_pri);
} }
//DEALLOCATION //DEALLOCATION
why2_deallocate(path); why2_deallocate(path);
why2_deallocate(public_path); why2_deallocate(key_path);
why2_deallocate(private_path); why2_deallocate(key);
why2_deallocate(public);
why2_deallocate(private);
} }
void why2_chat_deallocate_keys(void) void why2_chat_deallocate_keys(void)
{ {
why2_deallocate(ecc_pub); //DEALLOCATE THE pkey
why2_deallocate(ecc_pri); EVP_PKEY_free(keypair);
} }
char *why2_sha256(char *input) char *why2_sha256(char *input)