Skip to content

Commit beabdd5

Browse files
author
skelly
committed
Fixed issue #41 and some other minor stuff
1 parent 98c1344 commit beabdd5

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

client/src/cipher.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,27 @@ EVP_PKEY *generate_rsa_private_key(void) {
127127

128128
char *write_rsa_public_key(EVP_PKEY *pkey) {
129129
BIO *bio = BIO_new(BIO_s_mem());
130+
if (bio == NULL) {
131+
handle_openssl_error();
132+
return NULL;
133+
}
134+
130135
if (PEM_write_bio_PUBKEY(bio, pkey) <= 0) {
131136
handle_openssl_error();
132-
EVP_PKEY_free(pkey);
133137
BIO_free(bio);
134138
return NULL;
135139
}
136140

137141
char *pem_key = NULL;
138142
long pem_len = BIO_get_mem_data(bio, &pem_key);
143+
139144
char *copy = malloc(pem_len + 1);
145+
if (copy == NULL) {
146+
fprintf(stderr, "Memory allocation failed\n");
147+
BIO_free(bio);
148+
return NULL;
149+
}
150+
140151
memcpy(copy, pem_key, pem_len);
141152
copy[pem_len] = '\0';
142153

client/src/http.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const char *POST_REQ_TEMPLATE =
1111
"Host: localhost\r\n" // Add the host
1212
"Content-Type: %s\r\n"
1313
"Content-Length: %d\r\n"
14-
"Connection: close\r\n" // Optionally, close connection after request
14+
"Connection: Keep-Alive\r\n" // Optionally, close connection after request
1515
"\r\n%s"; // Ensure the body follows after \r\n\r\n
1616

1717
// forward declare helper functions and leave them at the end
@@ -59,10 +59,12 @@ static long parse_http_status_code(const char *buf) {
5959
char *endptr;
6060
long status_code;
6161

62-
status_code_start = strstr(buf, " ") + 1;
62+
status_code_start = strstr(buf, " ");
6363
if (status_code_start == NULL) {
6464
return HTTP_INVALID_RESPONSE;
6565
}
66+
status_code_start +=1;
67+
6668
status_code = strtol(status_code_start, &endptr, 10);
6769
if (endptr == status_code_start) {
6870
return HTTP_INVALID_RESPONSE;

client/src/main.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ int main(void) {
4343
EVP_PKEY *pkey = NULL;
4444
char *private_key = NULL;
4545
char *public_key = NULL;
46+
47+
unsigned char *b64_decoded_aes_key;
4648
unsigned char *aes_key = NULL;
4749
size_t key_len = 0;
4850

@@ -52,7 +54,7 @@ int main(void) {
5254
exit(1);
5355
}
5456

55-
log_set_level(LOG_DEBUG);
57+
log_set_level(LOG_INFO);
5658
setup_hints(&hints);
5759

5860
if (h_getaddrinfo(SERVER_IP, SERVER_PORT, &hints, &ainfo) != 0) {
@@ -85,17 +87,17 @@ int main(void) {
8587
}
8688

8789
log_info("Base64 encoded key: %s", http_post_res.data);
88-
base64_decode(http_post_res.data, &aes_key, &key_len);
90+
base64_decode(http_post_res.data, &b64_decoded_aes_key, &key_len);
8991
log_info("Key size (decoded): %zu", key_len);
9092

91-
print_hex(aes_key, key_len);
9293

93-
aes_key = decrypt_rsa_oaep_evp(pkey, (unsigned char *)http_post_res.data, key_len, &key_len);
94+
aes_key = decrypt_rsa_oaep_evp(pkey, b64_decoded_aes_key, key_len, &key_len);
9495
if (aes_key == NULL) {
9596
log_error("Failed to decrypt data from server");
9697
goto cleanup;
9798
}
9899

100+
print_hex(aes_key, key_len);
99101

100102
if (http_get(sfd, "/", &http_fraction_res) != HTTP_SUCCESS) {
101103
log_error("Failed to retrieve fraction links");

client/src/utils.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ int split_fraction_links(char *data, char *data_arr[], int maxlines) {
2525
}
2626

2727
void print_hex(const unsigned char *data, size_t size) {
28+
if (data == NULL) {
29+
log_error("Null data pointer\n"); // had to learn the hard f way
30+
return;
31+
}
2832
for (size_t i = 0; i < size; i++) {
2933
if (i%20==0) puts("");
3034
printf("%02X ", data[i]);

server/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def list_directory(self, path):
107107
self.send_header("Content-type", f"text/plain; charset={enc}")
108108
self.send_header("Content-Length", str(len(encoded)))
109109
self.end_headers()
110-
110+
111111
return f
112112

113113

0 commit comments

Comments
 (0)