Compare commits

..

No commits in common. "d764002546419300d087994b4f1414daea4e235b" and "8b7aa98b70adf6baa34d5e9421704f89480a2629" have entirely different histories.

2 changed files with 59 additions and 95 deletions

View File

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

View File

@ -32,135 +32,100 @@ 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>
EVP_PKEY *keypair = NULL; //KEYPAIR char *ecc_pub = NULL;
char *ecc_pri = NULL;
//LOCAL void read_file(FILE *file, char **output)
char* base64_encode(char *message)
{ {
//VARIABLES //VARIABLES
BIO *bio; int buffer_size;
BIO *b64; char *buffer;
BUF_MEM *buffer_ptr;
char* encoded_message;
//INIT BIOs //GET LENGTH
b64 = BIO_new(BIO_f_base64()); fseek(file, 0, SEEK_END);
bio = BIO_new(BIO_s_mem()); buffer_size = ftell(file);
bio = BIO_push(b64, bio); rewind(file);
//ENCODE //READ
BIO_write(bio, message, strlen(message)); buffer = why2_calloc(buffer_size + 1, sizeof(char));
BIO_flush(bio); if (fread(buffer, buffer_size, 1, file) != 1) why2_die("Reading keyfile failed!");
BIO_get_mem_ptr(bio, &buffer_ptr); buffer[buffer_size] = '\0';
//COPY //ASSIGN OUTPUT
encoded_message = why2_malloc(buffer_ptr -> length + 1); *output = buffer;
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 //GLOBAL
void why2_chat_init_keys(void) void why2_chat_init_keys(void)
{ {
FILE *key; //KEY FILE //KEY FILES
FILE *public;
FILE *private;
char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME")); //GET PATH TO KEY DIR //GET PATH TO KEY DIR
char *key_path = why2_malloc(strlen(path) + strlen(WHY2_CHAT_KEY) + 3); //ALLOCATE THE KEY PATH char *path = why2_replace(WHY2_CHAT_KEY_LOCATION, "{HOME}", getenv("HOME"));
//GET THE ACTUAL KEY PATH //ALLOCATE THE KEY PATHS
sprintf(key_path, "%s/%s%c", path, WHY2_CHAT_KEY, '\0'); 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);
//CHECK IF KEY EXIST //GET THE ACTUAL KEY PATHS
if (access(path, R_OK) != 0) //NOT FOUND - CREATE IT 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)
{ {
mkdir(path, 0700); mkdir(path, 0700);
//SOME USER OUTPUT //SOME USER OUTPUT
printf("No ECC key found.\nGenerating...\n\n"); printf("You are probably running WHY2-Chat for the first time now.\nGenerating ECC keys...\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
//WRITE THE KEYS INTO KEY-FILE EVP_PKEY_keygen(ctx, &pkey); //GENERATE ECC KEYPAIR
key = why2_fopen(key_path, "w+");
PEM_write_PrivateKey(key, keypair, NULL, NULL, 0, NULL, NULL); //WRITE THE KEY 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
//DEALLOCATION //DEALLOCATION
EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
} else } else
{ {
key = why2_fopen(key_path, "r"); //OPEN KEY FILE //OPEN FILES
keypair = PEM_read_PrivateKey(key, NULL, NULL, NULL); //LOAD KEYPAIR 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);
} }
//DEALLOCATION //DEALLOCATION
why2_deallocate(path); why2_deallocate(path);
why2_deallocate(key_path); why2_deallocate(public_path);
why2_deallocate(key); why2_deallocate(private_path);
} why2_deallocate(public);
why2_deallocate(private);
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) void why2_chat_deallocate_keys(void)
{ {
//DEALLOCATE THE pkey why2_deallocate(ecc_pub);
EVP_PKEY_free(keypair); why2_deallocate(ecc_pri);
} }
char *why2_sha256(char *input) char *why2_sha256(char *input)