Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabtz committed Oct 11, 2024
1 parent 41ecc3a commit 938970b
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 171 deletions.
Binary file modified client/client
Binary file not shown.
8 changes: 5 additions & 3 deletions client/include/cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#include <openssl/pem.h>

typedef struct{
unsigned char *decryptedtext;
unsigned char *decrypted_text;
size_t text_size;
} decrypted;
} decrypted_t;

decrypted *decrypt_fraction(fraction_t *fraction);
decrypted_t *decrypt_fraction(fraction_t *fraction);
char *generate_publickey(void);
void decrypted_free(decrypted_t *decrypted);

#endif
16 changes: 7 additions & 9 deletions client/include/load.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#include <stdint.h>
#define _GNU_SOURCE
#include <linux/module.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>

#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

#include "../include/log.h"
#include "../include/cipher.h"
#include "../include/log.h"

int load_lkm(const unsigned char* lkm, ssize_t total_size);
int is_lkm_loaded(const char *name);
int remove_lkm();
int create_lkm(int num_links,fraction_t *fractions);
uint8_t *decrypt_lkm(fraction_t *fractions, int fractions_count, ssize_t *len);
int load_lkm(const uint8_t *lkm, ssize_t total_size);
113 changes: 62 additions & 51 deletions client/src/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,117 @@
#include "../include/fraction.h"
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <string.h>

decrypted decryptedstr;

void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
static void handle_errors(void) {
ERR_print_errors_fp(stderr);
abort();
}

int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
static int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;

if (!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
if (!(ctx = EVP_CIPHER_CTX_new()))
handle_errors();

if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handle_errors();

if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handle_errors();
plaintext_len = len;

if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handle_errors();
plaintext_len += len;

EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_CTX_free(ctx);

return plaintext_len;
return plaintext_len;
}
decrypted *decrypt_fraction(fraction_t *fraction){

decrypted_t *decrypt_fraction(fraction_t *fraction) {

size_t decrypted_size;

unsigned char key[32] = {
0x6d, 0x46, 0x75, 0x32, 0x4c, 0x2f, 0x69, 0x34, 0x78, 0x65, 0x76, 0x4a,
0x34, 0x4e, 0x33, 0x36, 0x72, 0x44, 0x74, 0x35, 0x35, 0x5a, 0x4f, 0x34,
0x35, 0x4b, 0x63, 0x72, 0x6e, 0x30, 0x75, 0x57
};
unsigned char key[32] = {0x6d, 0x46, 0x75, 0x32, 0x4c, 0x2f, 0x69, 0x34,
0x78, 0x65, 0x76, 0x4a, 0x34, 0x4e, 0x33, 0x36,
0x72, 0x44, 0x74, 0x35, 0x35, 0x5a, 0x4f, 0x34,
0x35, 0x4b, 0x63, 0x72, 0x6e, 0x30, 0x75, 0x57};

unsigned char *decrypted_text = malloc(fraction->data_size);

if (decrypted_text == NULL) {
log_error("Cannot assign memory for the decrypted text");
return NULL;
}

unsigned char *decryptedtext = malloc(fraction->data_size+1);
decrypted_size = decrypt((unsigned char *)fraction->data, fraction->data_size,
key, (unsigned char *)fraction->iv, decrypted_text);

if (decryptedtext == NULL) {
fprintf(stderr, "Cannot assign memory for the decrypted text.\n");
return NULL;
}
decrypted_t *decr = malloc(sizeof(decrypted_t));

decrypted_size = decrypt((unsigned char*)fraction->data, fraction->data_size, key, (unsigned char*) fraction->iv, decryptedtext);
if (decr == NULL) {
log_error("Could not allocate memory for decrypted struct");
return NULL;
}

decryptedstr.decryptedtext = decryptedtext;
decryptedstr.text_size = decrypted_size;
decr->decrypted_text = decrypted_text;
decr->text_size = decrypted_size;

return decr;
}

return &decryptedstr;
void decrypted_free(decrypted_t *decrypted) {
free(decrypted->decrypted_text);
free(decrypted);
}

char *generate_publickey(void){
char *generate_publickey(void) {

EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pctx = NULL;

pctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
if(pctx == NULL){
handleErrors();
if (pctx == NULL) {
handle_errors();
return NULL;
}
if(EVP_PKEY_keygen_init(pctx) <= 0){
handleErrors();
if (EVP_PKEY_keygen_init(pctx) <= 0) {
handle_errors();
EVP_PKEY_CTX_free(pctx);
return NULL;
}
if(EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048) <= 0){
handleErrors();
if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048) <= 0) {
handle_errors();
EVP_PKEY_CTX_free(pctx);
return NULL;
}

if(EVP_PKEY_generate(pctx, &pkey) <= 0){
handleErrors();
if (EVP_PKEY_generate(pctx, &pkey) <= 0) {
handle_errors();
EVP_PKEY_CTX_free(pctx);
return NULL;
}
EVP_PKEY_CTX_free(pctx);

BIO *bio = BIO_new(BIO_s_mem());

if(PEM_write_bio_PUBKEY(bio,pkey) <= 0){
handleErrors();
if (PEM_write_bio_PUBKEY(bio, pkey) <= 0) {
handle_errors();
EVP_PKEY_free(pkey);
BIO_free(bio);
return NULL;
}

char *pem_key = NULL;
long pem_len = BIO_get_mem_data(bio,&pem_key);

long pem_len = BIO_get_mem_data(bio, &pem_key);
char *copy = malloc(pem_len);
memcpy(copy, pem_key, pem_len);
BIO_free(bio);

return pem_key;
return copy;
}
10 changes: 5 additions & 5 deletions client/src/fraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ int download_fraction(int sfd, char *url, fraction_t *fraction) {
// Parse the URL to get the path
path = get_path_from_url(url);
if (!path) {
log_error("Invalid URL: %s\n", url);
log_error("Invalid URL: %s", url);
return 1;
}

// Perform the HTTP GET request
if (http_get(sfd, path, &res) != HTTP_SUCCESS) {
log_error("Failed to download: %s\n", url);
log_error("Failed to download: %s", url);
return 1;
}

Expand Down Expand Up @@ -45,7 +45,7 @@ int fraction_parse(char *data, size_t size, fraction_t *fraction) {

// Ensure the data size is sufficient
if (size < HEADER_SIZE) {
log_error("Insufficient size: %lu\n", size);
log_error("Insufficient size: %lu", size);
return 1;

}
Expand All @@ -59,15 +59,15 @@ int fraction_parse(char *data, size_t size, fraction_t *fraction) {

// Check the magic number
if (!check_magic(magic)) {
log_error("Wrong magic number: %02x\n", magic);
log_error("Wrong magic number: %02x", magic);
return 1;
}

// Allocate memory for fraction data
data_size = size - HEADER_SIZE;
fraction->data = malloc(data_size);
if (!fraction->data) {
log_error("Failed to allocate data for fraction\n");
log_error("Failed to allocate data for fraction");
return 1;
}
// Set the extracted values in the fraction structure
Expand Down
4 changes: 2 additions & 2 deletions client/src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ int http_get(int sfd, const char *path, http_res_t *res) {
req_buf_len = strlen(request_buf);

if (send_request(sfd, request_buf) < 0) {
log_error("Error: failed to send request\n");
log_error("Error: failed to send request");
err = HTTP_SOCKET_ERR;
goto error;
}

log_debug("Sent GET request\n");
log_debug("Sent GET request");

res->request = malloc(req_buf_len + 1);
if (res->request == NULL) {
Expand Down
Loading

0 comments on commit 938970b

Please sign in to comment.