Compare commits

..

6 Commits

Author SHA1 Message Date
d764002546
added base64_decode fn
All checks were successful
Codacy Scan / Codacy Security Scan (push) Successful in 22s
Build WHY2-chat / test-why2 (./out/why2-chat-client, ./configure.sh, ubuntu-latest, ./out/why2-chat-server) (push) Successful in 1m56s
Test WHY2-core / test-why2 (why2, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-core-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 2m17s
Test Project / test-project (./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./test) (push) Successful in 2m16s
Test WHY2-logger / test-why2 (why2-logger, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-logger-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 2m29s
and tweaked the encode fn a bit
2025-01-29 22:44:46 +01:00
685b8e446e
defined why2_chat_ecc_sign
and its not working as expected lol

i will fix it in next commits, i just need this for change comparison
2025-01-29 21:15:24 +01:00
0b3cb2f957
added local base64_encode fn 2025-01-29 21:14:39 +01:00
5cc8c43b62
declared why2_chat_ecc_sign
used for signing message with ECC
2025-01-29 21:13:59 +01:00
8d17915a05
generating only one keyfile
lol ima stoopid - it seems you need only private key file and you can generate the pub from it
2025-01-29 20:52:16 +01:00
b977d0dd40
removed unused key macro
and renamed the key file
2025-01-29 20:50:54 +01:00
2 changed files with 95 additions and 59 deletions

View File

@ -29,12 +29,13 @@ extern "C" {
#define WHY2_CHAT_ECC NID_secp521r1 //CURVE NAME
#define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION
#define WHY2_CHAT_PUB_KEY "pub"
#define WHY2_CHAT_PRI_KEY "pri"
#define WHY2_CHAT_KEY "secp521r1.pem"
void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) ECC KEYS
void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE)
char *why2_chat_ecc_sign(char *message); //SIGN message WITH ECC KEY
char *why2_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING
#ifdef __cplusplus

View File

@ -32,100 +32,135 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <openssl/pem.h>
#include <openssl/ec.h>
char *ecc_pub = NULL;
char *ecc_pri = NULL;
EVP_PKEY *keypair = NULL; //KEYPAIR
void read_file(FILE *file, char **output)
//LOCAL
char* base64_encode(char *message)
{
//VARIABLES
int buffer_size;
char *buffer;
BIO *bio;
BIO *b64;
BUF_MEM *buffer_ptr;
char* encoded_message;
//GET LENGTH
fseek(file, 0, SEEK_END);
buffer_size = ftell(file);
rewind(file);
//INIT BIOs
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
//READ
buffer = why2_calloc(buffer_size + 1, sizeof(char));
if (fread(buffer, buffer_size, 1, file) != 1) why2_die("Reading keyfile failed!");
buffer[buffer_size] = '\0';
//ENCODE
BIO_write(bio, message, strlen(message));
BIO_flush(bio);
BIO_get_mem_ptr(bio, &buffer_ptr);
//ASSIGN OUTPUT
*output = buffer;
//COPY
encoded_message = why2_malloc(buffer_ptr -> length + 1);
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
encoded_message[buffer_ptr -> length] = '\0';
//DEALLOCATION
BIO_free_all(bio);
return encoded_message;
}
char* base64_decode(char *encoded_message)
{
//VARIABLES
BIO *bio;
BIO *b64;
size_t length = strlen(encoded_message);
char* decoded_message = why2_malloc(length);
//INIT BIOs
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(encoded_message, length);
bio = BIO_push(b64, bio);
//NULL-TERM
decoded_message[BIO_read(bio, decoded_message, length)] = '\0';
//DEALLOCATION
BIO_free_all(bio);
return decoded_message;
}
//GLOBAL
void why2_chat_init_keys(void)
{
//KEY FILES
FILE *public;
FILE *private;
FILE *key; //KEY FILE
//GET PATH TO KEY DIR
char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME"));
char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME")); //GET PATH TO KEY DIR
char *key_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_KEY) + 3); //ALLOCATE THE KEY PATH
//ALLOCATE THE KEY PATHS
char *public_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_PUB_KEY) + 3);
char *private_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_PRI_KEY) + 3);
//GET THE ACTUAL KEY PATH
sprintf(key_path, "%s/%s%c", path, WHY2_CHAT_KEY, '\0');
//GET THE ACTUAL KEY PATHS
sprintf(public_path, "%s/%s%c", path, WHY2_CHAT_PUB_KEY, '\0');
sprintf(private_path, "%s/%s%c", path, WHY2_CHAT_PRI_KEY, '\0');
//CHECK IF KEYS EXIST
if (access(path, R_OK) != 0)
//CHECK IF KEY EXIST
if (access(path, R_OK) != 0) //NOT FOUND - CREATE IT
{
mkdir(path, 0700);
//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_keygen_init(ctx); //INIT KEYGEN
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
printf("Saving keys...\n");
//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
//WRITE THE KEYS INTO KEY-FILE
key = why2_fopen(key_path, "w+");
PEM_write_PrivateKey(key, keypair, NULL, NULL, 0, NULL, NULL); //WRITE THE KEY
//DEALLOCATION
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
} else
{
//OPEN FILES
public = why2_fopen(public_path, "r");
private = why2_fopen(private_path, "r");
//READ THE KEYS
read_file(public, &ecc_pub);
read_file(private, &ecc_pri);
key = why2_fopen(key_path, "r"); //OPEN KEY FILE
keypair = PEM_read_PrivateKey(key, NULL, NULL, NULL); //LOAD KEYPAIR
}
//DEALLOCATION
why2_deallocate(path);
why2_deallocate(public_path);
why2_deallocate(private_path);
why2_deallocate(public);
why2_deallocate(private);
why2_deallocate(key_path);
why2_deallocate(key);
}
char *why2_chat_ecc_sign(char *message)
{
//VARIABLES
EVP_MD_CTX *mdctx = NULL; //SIGNING CONTEXT
size_t siglen;
char *sig; //SIGNATURE
char *encoded_sig; //FINAL (ENCODED) SIGNATURE
//INIT mdctx
mdctx = EVP_MD_CTX_new();
EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, keypair);
EVP_DigestSignUpdate(mdctx, message, strlen(message)); //UPDATE MESSAGE TO SIGN
EVP_DigestSignFinal(mdctx, NULL, &siglen); //COUNT LENGTH
//GENERATE SIGNATURE
sig = why2_malloc(siglen); //ALLOCATE SIGNATURE
EVP_DigestSignFinal(mdctx, (unsigned char*) sig, &siglen);
encoded_sig = base64_encode(sig); //CONVERT sig TO BASE64
//DEALLOCATION
why2_deallocate(sig);
EVP_MD_CTX_free(mdctx);
return encoded_sig;
}
void why2_chat_deallocate_keys(void)
{
why2_deallocate(ecc_pub);
why2_deallocate(ecc_pri);
//DEALLOCATE THE pkey
EVP_PKEY_free(keypair);
}
char *why2_sha256(char *input)