Compare commits
No commits in common. "06d835e2b94982961bc2f47dcb3ea02e0643dcad" and "35ec44ae68989ec093266f87d1102196223207a9" have entirely different histories.
06d835e2b9
...
35ec44ae68
@ -43,9 +43,6 @@ extern "C" {
|
||||
void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) ECC KEYS
|
||||
void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE)
|
||||
|
||||
char *why2_chat_base64_encode(char *message, size_t *length); //ENCODE message OF length INTO BASE64 WITH LENGTH DELIMITER (WHY2_CHAT_BASE64_LENGTH_DELIMITER)
|
||||
char *why2_chat_base64_decode(char *encoded_message, size_t *length); //DECODE encoded_message AND SET length TO OUTPUT LENGTH
|
||||
|
||||
char *why2_chat_ecc_sign(char *message); //SIGN message WITH ECC KEY
|
||||
why2_bool why2_chat_ecc_verify_signature(char *message, char *signature, EVP_PKEY *key);
|
||||
|
||||
|
@ -35,8 +35,8 @@ typedef struct
|
||||
} __why2_accept_thread_params;
|
||||
|
||||
//FUNCTIONS
|
||||
void why2_send_socket(char *text, char *username, char *key, int socket); //send socket.... wtf did you expect
|
||||
void why2_send_socket_code(char *params, char *username, char *key, int socket, char *code); //SEND SOCKET BUT WITH CODE
|
||||
void why2_send_socket(char *text, char *username, int socket); //send socket.... wtf did you expect
|
||||
void why2_send_socket_code(char *params, char *username, int socket, char *code); //SEND SOCKET BUT WITH CODE
|
||||
void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD
|
||||
void *why2_authority_communicate_thread(void *arg); //CA COMMUNICATION THREAD
|
||||
void *why2_accept_thread(void *params); //LOOP ACCEPTING CONNECTIONS
|
||||
|
@ -87,7 +87,6 @@ typedef struct
|
||||
typedef struct
|
||||
{
|
||||
char *output_text; //VARIABLE FOR ENCRYPTED/DECRYPTED TEXT
|
||||
unsigned long output_text_length; //LENGTH OF ENCRYPTED/DECRYPTED TEXT
|
||||
char *used_key; //VARIABLE FOR USED/GENERATED KEY
|
||||
unsigned long unused_key_size; //VARIABLE FOR COUNT OF WHY2_UNUSED CHARACTERS IN KEY
|
||||
unsigned long repeated_key_size; //VARIABLE FOR COUNT OF REPEATED CHARACTERS IN KEY (basically reversed unused_key_size)
|
||||
|
@ -36,6 +36,68 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
EVP_PKEY *keypair = NULL; //KEYPAIR
|
||||
|
||||
//LOCAL
|
||||
char *base64_encode(char *message, size_t length)
|
||||
{
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
BIO *b64;
|
||||
BUF_MEM *buffer_ptr;
|
||||
char* encoded_message;
|
||||
|
||||
//INIT BIOs
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
|
||||
bio = BIO_new(BIO_s_mem());
|
||||
bio = BIO_push(b64, bio);
|
||||
|
||||
//ENCODE
|
||||
BIO_write(bio, message, length);
|
||||
BIO_flush(bio);
|
||||
BIO_get_mem_ptr(bio, &buffer_ptr);
|
||||
|
||||
//COPY
|
||||
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) length) + 2);
|
||||
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
|
||||
|
||||
sprintf(encoded_message + buffer_ptr -> length, "%c%zu%c", WHY2_CHAT_BASE64_LENGTH_DELIMITER, length, '\0'); //APPEND LENGTH
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free_all(bio);
|
||||
|
||||
return encoded_message;
|
||||
}
|
||||
|
||||
char *base64_decode(char *encoded_message, size_t *length)
|
||||
{
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
BIO *b64;
|
||||
char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER
|
||||
size_t length_local = strtoull(separator_ptr + 1, NULL, 10);
|
||||
char* decoded_message = why2_malloc(length_local + 1);
|
||||
int decoded_length;
|
||||
|
||||
//INIT BIOs
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
|
||||
bio = BIO_new_mem_buf(encoded_message, separator_ptr - encoded_message);
|
||||
bio = BIO_push(b64, bio);
|
||||
|
||||
//DECODE
|
||||
decoded_length = BIO_read(bio, decoded_message, length_local);
|
||||
|
||||
//NULL-TERM
|
||||
decoded_message[decoded_length] = '\0';
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free_all(bio);
|
||||
|
||||
//SET length
|
||||
if (length != NULL) *length = length_local;
|
||||
|
||||
return decoded_message;
|
||||
}
|
||||
|
||||
void calculate_ecdh_secret(EVP_PKEY *pri, EVP_PKEY *pub, char **secret, size_t *len)
|
||||
{
|
||||
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pri, NULL);
|
||||
@ -96,68 +158,6 @@ void why2_chat_init_keys(void)
|
||||
why2_deallocate(key);
|
||||
}
|
||||
|
||||
char *why2_chat_base64_encode(char *message, size_t *length)
|
||||
{
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
BIO *b64;
|
||||
BUF_MEM *buffer_ptr;
|
||||
char* encoded_message;
|
||||
|
||||
//INIT BIOs
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
|
||||
bio = BIO_new(BIO_s_mem());
|
||||
bio = BIO_push(b64, bio);
|
||||
|
||||
//ENCODE
|
||||
BIO_write(bio, message, *length);
|
||||
BIO_flush(bio);
|
||||
BIO_get_mem_ptr(bio, &buffer_ptr);
|
||||
|
||||
//COPY
|
||||
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) *length) + 2);
|
||||
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
|
||||
|
||||
sprintf(encoded_message + buffer_ptr -> length, "%c%zu%c", WHY2_CHAT_BASE64_LENGTH_DELIMITER, *length, '\0'); //APPEND LENGTH
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free_all(bio);
|
||||
|
||||
return encoded_message;
|
||||
}
|
||||
|
||||
char *why2_chat_base64_decode(char *encoded_message, size_t *length)
|
||||
{
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
BIO *b64;
|
||||
char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER
|
||||
size_t length_local = strtoull(separator_ptr + 1, NULL, 10);
|
||||
char* decoded_message = why2_malloc(length_local + 1);
|
||||
int decoded_length;
|
||||
|
||||
//INIT BIOs
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
|
||||
bio = BIO_new_mem_buf(encoded_message, separator_ptr - encoded_message);
|
||||
bio = BIO_push(b64, bio);
|
||||
|
||||
//DECODE
|
||||
decoded_length = BIO_read(bio, decoded_message, length_local);
|
||||
|
||||
//NULL-TERM
|
||||
decoded_message[decoded_length] = '\0';
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free_all(bio);
|
||||
|
||||
//SET length
|
||||
if (length != NULL) *length = length_local;
|
||||
|
||||
return decoded_message;
|
||||
}
|
||||
|
||||
char *why2_chat_ecc_sign(char *message)
|
||||
{
|
||||
//VARIABLES
|
||||
@ -177,7 +177,7 @@ char *why2_chat_ecc_sign(char *message)
|
||||
sig = why2_malloc(siglen); //ALLOCATE SIGNATURE
|
||||
EVP_DigestSignFinal(mdctx, (unsigned char*) sig, &siglen);
|
||||
|
||||
encoded_sig = why2_chat_base64_encode(sig, &siglen); //CONVERT sig TO BASE64
|
||||
encoded_sig = base64_encode(sig, siglen); //CONVERT sig TO BASE64
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(sig);
|
||||
@ -190,7 +190,7 @@ why2_bool why2_chat_ecc_verify_signature(char *message, char *signature, EVP_PKE
|
||||
{
|
||||
//VARIABLES
|
||||
size_t length;
|
||||
char *decoded_signature = why2_chat_base64_decode(signature, &length); //DECODE SIGNATURE
|
||||
char *decoded_signature = base64_decode(signature, &length); //DECODE SIGNATURE
|
||||
why2_bool returning;
|
||||
|
||||
//INIT CONTEXT
|
||||
@ -232,7 +232,7 @@ char *why2_chat_ecc_serialize_public_key()
|
||||
pubkey[length] = '\0';
|
||||
|
||||
//ENCODE
|
||||
base64_encoded = why2_chat_base64_encode(pubkey, &length);
|
||||
base64_encoded = base64_encode(pubkey, length);
|
||||
|
||||
//DEALLOCATION
|
||||
BIO_free(bio);
|
||||
@ -247,7 +247,7 @@ EVP_PKEY* why2_chat_ecc_deserialize_public_key(char *pubkey)
|
||||
//VARIABLES
|
||||
BIO *bio;
|
||||
EVP_PKEY *key;
|
||||
char *base64_decoded = why2_chat_base64_decode(pubkey, NULL);
|
||||
char *base64_decoded = base64_decode(pubkey, NULL);
|
||||
|
||||
//EXTRACT KEY
|
||||
bio = BIO_new_mem_buf(base64_decoded, -1);
|
||||
@ -265,7 +265,7 @@ char *why2_chat_ecc_shared_key(char *ecc_key)
|
||||
size_t key_length;
|
||||
char *secret = NULL;
|
||||
size_t secret_len;
|
||||
char *recipient_pubkey_decoded = why2_chat_base64_decode(ecc_key, &key_length); //DECODE key
|
||||
char *recipient_pubkey_decoded = base64_decode(ecc_key, &key_length); //DECODE key
|
||||
char *sym_key;
|
||||
BIO *bio = BIO_new_mem_buf(recipient_pubkey_decoded, -1);
|
||||
EVP_PKEY *recipient_pubkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
|
||||
|
@ -44,7 +44,7 @@ void exit_client(WHY2_UNUSED int i) //guess what
|
||||
if (exited) return;
|
||||
exited = 1;
|
||||
|
||||
why2_send_socket_code(NULL, NULL, why2_chat_get_client_server_key(), listen_socket, WHY2_CHAT_CODE_EXIT);
|
||||
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_EXIT);
|
||||
}
|
||||
|
||||
why2_bool command(char *input, char *command, char **arg)
|
||||
@ -264,17 +264,17 @@ int main(void)
|
||||
char *final_message = why2_malloc(strlen(id) + strlen(msg) + 2);
|
||||
sprintf(final_message, "%s;%s%c", id, msg, '\0');
|
||||
|
||||
why2_send_socket_code(final_message, NULL, why2_chat_get_client_server_key(), listen_socket, WHY2_CHAT_CODE_DM); //SEND
|
||||
why2_send_socket_code(final_message, NULL, listen_socket, WHY2_CHAT_CODE_DM); //SEND
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(id);
|
||||
why2_deallocate(final_message);
|
||||
} else if (command(line, WHY2_CHAT_COMMAND_LIST, &cmd_arg)) //LIST CMD
|
||||
{
|
||||
why2_send_socket_code(NULL, NULL, why2_chat_get_client_server_key(), listen_socket, WHY2_CHAT_CODE_LIST);
|
||||
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_LIST);
|
||||
} else if (command(line, WHY2_CHAT_COMMAND_VERSION, &cmd_arg)) //VERSION CMD
|
||||
{
|
||||
why2_send_socket_code(NULL, NULL, why2_chat_get_client_server_key(), listen_socket, WHY2_CHAT_CODE_VERSION);
|
||||
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_VERSION);
|
||||
} else
|
||||
{
|
||||
invalid("command");
|
||||
@ -288,7 +288,7 @@ int main(void)
|
||||
|
||||
char *hash = why2_sha256(line, strlen(line)); //HASHISH
|
||||
|
||||
why2_send_socket_code(hash, NULL, why2_chat_get_client_server_key(), listen_socket, WHY2_CHAT_CODE_PASSWORD); //SEND BUT HASHED
|
||||
why2_send_socket_code(hash, NULL, listen_socket, WHY2_CHAT_CODE_PASSWORD); //SEND BUT HASHED
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(hash);
|
||||
@ -299,10 +299,10 @@ int main(void)
|
||||
{
|
||||
__why2_set_asking_username(0);
|
||||
|
||||
why2_send_socket_code(line, NULL, why2_chat_get_client_server_key(), listen_socket, WHY2_CHAT_CODE_USERNAME);
|
||||
why2_send_socket_code(line, NULL, listen_socket, WHY2_CHAT_CODE_USERNAME);
|
||||
} else
|
||||
{
|
||||
why2_send_socket(line, NULL, why2_chat_get_client_server_key(), listen_socket); //NULL IS SENT BECAUSE IT IS USELESS TO SEND USER FROM CLIENT - SERVER WON'T USE IT
|
||||
why2_send_socket(line, NULL, listen_socket); //NULL IS SENT BECAUSE IT IS USELESS TO SEND USER FROM CLIENT - SERVER WON'T USE IT
|
||||
}
|
||||
}
|
||||
|
||||
@ -319,7 +319,6 @@ int main(void)
|
||||
|
||||
why2_deallocate(cmd_arg);
|
||||
why2_chat_deallocate_keys(); //DEALLOCATE GETTERS FOR KEYS
|
||||
why2_chat_deallocate_client_server_key();
|
||||
|
||||
why2_clean_memory(""); //RUN GARBAGE COLLECTOR
|
||||
|
||||
|
191
src/chat/misc.c
191
src/chat/misc.c
@ -34,8 +34,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#include <why2/chat/crypto.h>
|
||||
#include <why2/chat/flags.h>
|
||||
|
||||
#include <why2/encrypter.h>
|
||||
#include <why2/decrypter.h>
|
||||
#include <why2/llist.h>
|
||||
#include <why2/memory.h>
|
||||
#include <why2/misc.h>
|
||||
@ -52,18 +50,6 @@ typedef struct _connection_node
|
||||
char *key; //EACH USER WILL USE DIFFERENT KEY FOR COMMUNICATION
|
||||
} connection_node_t; //SINGLE LINKED LIST
|
||||
|
||||
typedef struct _read_socket_raw_thread_node
|
||||
{
|
||||
int connection;
|
||||
char *key;
|
||||
} read_socket_raw_thread_node_t;
|
||||
|
||||
enum ENCRYPTION_DECRYPTION
|
||||
{
|
||||
ENCRYPTION,
|
||||
DECRYPTION
|
||||
};
|
||||
|
||||
why2_list_t connection_list = WHY2_LIST_EMPTY;
|
||||
why2_list_t waiting_list = WHY2_LIST_EMPTY;
|
||||
|
||||
@ -127,7 +113,7 @@ void send_to_all(char *json)
|
||||
|
||||
connection_buffer = *(connection_node_t*) node_buffer -> value;
|
||||
|
||||
why2_send_socket(message, username, connection_buffer.key, connection_buffer.connection); //SEND TO CLIENT
|
||||
why2_send_socket(message, username, connection_buffer.connection); //SEND TO CLIENT
|
||||
}
|
||||
|
||||
//DEALLOCATION
|
||||
@ -173,62 +159,7 @@ void remove_non_ascii(char **text)
|
||||
(*text)[j] = '\0';
|
||||
}
|
||||
|
||||
char *__fake_base64(char *text, WHY2_UNUSED size_t *length)
|
||||
{
|
||||
return why2_strdup(text);
|
||||
}
|
||||
|
||||
void encrypt_decrypt_message(char **message, char *key, enum ENCRYPTION_DECRYPTION operation)
|
||||
{
|
||||
//NO ENCRYPTION
|
||||
if (key == NULL) return;
|
||||
|
||||
//CALLBACKS
|
||||
why2_output_flags (*operation_cb)(char*, char*) = NULL;
|
||||
char *(*base64_before_cb)(char*, size_t*) = NULL;
|
||||
char *(*base64_after_cb)(char*, size_t*) = NULL;
|
||||
|
||||
//SELECT CORRECT CALLBACK
|
||||
switch (operation)
|
||||
{
|
||||
case ENCRYPTION:
|
||||
operation_cb = why2_encrypt_text;
|
||||
base64_before_cb = __fake_base64;
|
||||
base64_after_cb = why2_chat_base64_encode;
|
||||
break;
|
||||
|
||||
case DECRYPTION:
|
||||
operation_cb = why2_decrypt_text;
|
||||
base64_before_cb = why2_chat_base64_decode;
|
||||
base64_after_cb = __fake_base64;
|
||||
break;
|
||||
|
||||
default:
|
||||
why2_die("ENCRYPTION_DECRYPTION not implemented!");
|
||||
break;
|
||||
}
|
||||
|
||||
//VARIABLES
|
||||
size_t length = strlen(*message);
|
||||
char *message_decoded = base64_before_cb(*message, &length);
|
||||
|
||||
//SET FLAGS
|
||||
if (why2_get_key_length() < strlen(key)) why2_set_key_length(strlen(key));
|
||||
why2_set_flags((why2_input_flags) { 1, 1, 0, WHY2_v4, WHY2_OUTPUT_TEXT, 0 }); //TODO: Add padding
|
||||
|
||||
//ENCRYPT
|
||||
why2_output_flags output = operation_cb(message_decoded, key);
|
||||
|
||||
//COPY OUTPUT IN BASE64
|
||||
why2_deallocate(*message);
|
||||
*message = base64_after_cb(output.output_text, &output.output_text_length);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message_decoded);
|
||||
why2_deallocate_output(output);
|
||||
}
|
||||
|
||||
char *read_socket_raw(int socket, char *key)
|
||||
char *read_socket_raw(int socket)
|
||||
{
|
||||
if (socket == -1)
|
||||
{
|
||||
@ -237,36 +168,31 @@ char *read_socket_raw(int socket, char *key)
|
||||
}
|
||||
|
||||
char *content_buffer = NULL;
|
||||
size_t content_size = 0;
|
||||
int content_size;
|
||||
char *wait_buffer = why2_malloc(2); //TEMP
|
||||
|
||||
//WAIT TILl RECEIVED MSG (ik it sucks but i can't think of better solution; anyways, this is way more convenient than infinite loop that makes my computer go wroom wroom)
|
||||
recv(socket, wait_buffer, 1, MSG_PEEK);
|
||||
why2_deallocate(wait_buffer);
|
||||
|
||||
//FIND THE RECEIVED SIZE
|
||||
ioctl(socket, FIONREAD, &content_size);
|
||||
|
||||
//ALLOCATE
|
||||
content_buffer = why2_malloc(content_size + 1);
|
||||
|
||||
for (size_t i = 0; i < content_size; i++)
|
||||
do
|
||||
{
|
||||
//END OF MESSAGE REACHED (COULD BE CAUSED BY FAST SENT MESSAGES)
|
||||
if (i >= 2 && strncmp(content_buffer + i - 2, "\"}", 2) == 0) break;
|
||||
//FIND THE SENT SIZE
|
||||
content_size = 0;
|
||||
if (ioctl(socket, FIONREAD, &content_size) < 0 || content_size <= 0) continue;
|
||||
|
||||
//ALLOCATE
|
||||
content_buffer = why2_realloc(content_buffer, content_size + 1);
|
||||
|
||||
//READ JSON MESSAGE
|
||||
if (recv(socket, content_buffer + i, 1, 0) != 1) //READ THE MESSAGE BY CHARACTERS
|
||||
if (recv(socket, content_buffer, content_size, 0) != content_size) //READ THE MESSAGE BY CHARACTERS
|
||||
{
|
||||
fprintf(stderr, "Socket probably read wrongly!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (content_buffer == NULL || strncmp(content_buffer + (content_size - 2), "\"}", 2) != 0);
|
||||
|
||||
content_buffer[content_size] = '\0'; //NULL TERM
|
||||
|
||||
encrypt_decrypt_message(&content_buffer, key, DECRYPTION); //DECRYPT
|
||||
|
||||
//VALIDATE JSON FORMAT
|
||||
struct json_object *json = json_tokener_parse(content_buffer);
|
||||
if (json == NULL)
|
||||
@ -285,9 +211,7 @@ char *read_socket_raw(int socket, char *key)
|
||||
|
||||
void *read_socket_raw_thread(void *socket)
|
||||
{
|
||||
read_socket_raw_thread_node_t node = *(read_socket_raw_thread_node_t*) socket;
|
||||
|
||||
return read_socket_raw(node.connection, node.key);
|
||||
return read_socket_raw(*(int*) socket);
|
||||
}
|
||||
|
||||
void remove_json_syntax_characters(char *text)
|
||||
@ -422,23 +346,20 @@ why2_node_t *find_connection_by_id(unsigned long id)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
char *read_user(int connection, void **raw_ptr, char *key)
|
||||
char *read_user(int connection, void **raw_ptr)
|
||||
{
|
||||
//VARIABLES
|
||||
void *buffer;
|
||||
pthread_t thread_buffer;
|
||||
pthread_t thread_deletion_buffer;
|
||||
|
||||
//PARSE PARAMETERS
|
||||
read_socket_raw_thread_node_t node = { connection, key };
|
||||
|
||||
//RESET VARIABLES
|
||||
*raw_ptr = NULL;
|
||||
|
||||
buffer = &thread_buffer;
|
||||
|
||||
//READ
|
||||
pthread_create(&thread_buffer, NULL, read_socket_raw_thread, &node);
|
||||
pthread_create(&thread_buffer, NULL, read_socket_raw_thread, &connection);
|
||||
why2_list_push(&waiting_list, &buffer, sizeof(buffer));
|
||||
|
||||
//RUN DELETION THREAD
|
||||
@ -462,21 +383,21 @@ char *get_username(int connection)
|
||||
return c_node.username;
|
||||
}
|
||||
|
||||
void send_socket_deallocate(char *text, char *username, char *key, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
||||
void send_socket_deallocate(char *text, char *username, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
||||
{
|
||||
why2_send_socket(text, username, key, socket);
|
||||
why2_send_socket(text, username, socket);
|
||||
|
||||
why2_toml_read_free(username);
|
||||
}
|
||||
|
||||
void send_socket_code_deallocate(char *params, char *username, char *key, int socket, char *code) //SAME AS send_socket_deallocate BUT WITH CODE FIELD
|
||||
void send_socket_code_deallocate(char *params, char *username, int socket, char *code) //SAME AS send_socket_deallocate BUT WITH CODE FIELD
|
||||
{
|
||||
why2_send_socket_code(params, username, key, socket, code);
|
||||
why2_send_socket_code(params, username, socket, code);
|
||||
|
||||
why2_toml_read_free(username);
|
||||
}
|
||||
|
||||
void send_socket(char *text, char *username, char *why2_key, int socket, why2_bool welcome, char *code)
|
||||
void send_socket(char *text, char *username, int socket, why2_bool welcome, char *code)
|
||||
{
|
||||
//VARIABLES
|
||||
char *output = why2_strdup("");
|
||||
@ -536,8 +457,6 @@ void send_socket(char *text, char *username, char *why2_key, int socket, why2_bo
|
||||
}
|
||||
add_brackets(&output);
|
||||
|
||||
encrypt_decrypt_message(&output, why2_key, ENCRYPTION); //ENCRYPT
|
||||
|
||||
//SEND
|
||||
send(socket, output, strlen(output), 0);
|
||||
|
||||
@ -546,16 +465,16 @@ void send_socket(char *text, char *username, char *why2_key, int socket, why2_bo
|
||||
why2_deallocate(output);
|
||||
}
|
||||
|
||||
void send_welcome_socket_deallocate(char *text, char *username, char *key, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
||||
void send_welcome_socket_deallocate(char *text, char *username, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
||||
{
|
||||
send_socket(NULL, username, key, socket, 1, text);
|
||||
send_socket(NULL, username, socket, 1, text);
|
||||
|
||||
why2_toml_read_free(username);
|
||||
}
|
||||
|
||||
void send_welcome_packet(int connection, char *key)
|
||||
void send_welcome_packet(int connection)
|
||||
{
|
||||
send_welcome_socket_deallocate(WHY2_CHAT_CODE_ACCEPT_MESSAGES, why2_chat_server_config("server_username"), key, connection);
|
||||
send_welcome_socket_deallocate(WHY2_CHAT_CODE_ACCEPT_MESSAGES, why2_chat_server_config("server_username"), connection);
|
||||
}
|
||||
|
||||
unsigned long get_latest_id()
|
||||
@ -590,7 +509,7 @@ why2_bool perform_key_exchange_client(int connection)
|
||||
char *read_code = NULL;
|
||||
why2_bool exiting_read = 0;
|
||||
|
||||
why2_send_socket_code(client_pubkey, NULL, NULL, connection, WHY2_CHAT_CODE_CLIENT_SERVER_KEY_EXCHANGE); //SEND pubkey TO SERVER
|
||||
why2_send_socket_code(client_pubkey, NULL, connection, WHY2_CHAT_CODE_CLIENT_SERVER_KEY_EXCHANGE); //SEND pubkey TO SERVER
|
||||
|
||||
why2_deallocate(client_pubkey); //DEALLOCATE client_pubkey
|
||||
|
||||
@ -600,7 +519,7 @@ why2_bool perform_key_exchange_client(int connection)
|
||||
why2_deallocate(read);
|
||||
why2_deallocate(read_code);
|
||||
|
||||
read = read_socket_raw(connection, NULL);
|
||||
read = read_socket_raw(connection);
|
||||
|
||||
read_code = get_string_from_json_string(read, "code");
|
||||
exiting_read = read_code != NULL && strcmp(read_code, WHY2_CHAT_CODE_SERVER_CLIENT_KEY_EXCHANGE) == 0;
|
||||
@ -633,7 +552,7 @@ why2_bool perform_key_exchange_server(int connection, char **key)
|
||||
why2_deallocate(read);
|
||||
why2_deallocate(read_code);
|
||||
|
||||
read = read_socket_raw(connection, NULL);
|
||||
read = read_socket_raw(connection);
|
||||
|
||||
read_code = get_string_from_json_string(read, "code");
|
||||
exiting_read = read_code != NULL && strcmp(read_code, WHY2_CHAT_CODE_CLIENT_SERVER_KEY_EXCHANGE) == 0;
|
||||
@ -645,7 +564,7 @@ why2_bool perform_key_exchange_server(int connection, char **key)
|
||||
|
||||
//SEND CLIENT PUBKEY
|
||||
server_pubkey = why2_chat_ecc_serialize_public_key();
|
||||
why2_send_socket_code(server_pubkey, NULL, NULL, connection, WHY2_CHAT_CODE_SERVER_CLIENT_KEY_EXCHANGE);
|
||||
why2_send_socket_code(server_pubkey, NULL, connection, WHY2_CHAT_CODE_SERVER_CLIENT_KEY_EXCHANGE);
|
||||
|
||||
*key = why2_chat_ecc_shared_key(client_pubkey);
|
||||
|
||||
@ -658,14 +577,14 @@ why2_bool perform_key_exchange_server(int connection, char **key)
|
||||
}
|
||||
|
||||
//GLOBAL
|
||||
void why2_send_socket(char *text, char *username, char *key, int socket)
|
||||
void why2_send_socket(char *text, char *username, int socket)
|
||||
{
|
||||
send_socket(text, username, key, socket, 0, NULL);
|
||||
send_socket(text, username, socket, 0, NULL);
|
||||
}
|
||||
|
||||
void why2_send_socket_code(char *params, char *username, char *key, int socket, char *code)
|
||||
void why2_send_socket_code(char *params, char *username, int socket, char *code)
|
||||
{
|
||||
send_socket(params, username, key, socket, 0, code);
|
||||
send_socket(params, username, socket, 0, code);
|
||||
}
|
||||
|
||||
void *why2_communicate_thread(void *arg)
|
||||
@ -682,7 +601,7 @@ void *why2_communicate_thread(void *arg)
|
||||
|
||||
printf("User connected.\t\t%d\n", connection);
|
||||
|
||||
send_welcome_packet(connection, client_server_key); //TELL USER ALL THE INFO THEY NEED
|
||||
send_welcome_packet(connection); //TELL USER ALL THE INFO THEY NEED
|
||||
|
||||
//GET USERNAME
|
||||
char *config_username = why2_chat_server_config("user_pick_username");
|
||||
@ -708,7 +627,7 @@ void *why2_communicate_thread(void *arg)
|
||||
{
|
||||
if (config_username == NULL) fprintf(stderr, "Your config doesn't contain 'user_pick_username'. Please update your configuration.\n");
|
||||
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_PICK_USERNAME); //ASK USER FOR USERNAME
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_PICK_USERNAME); //ASK USER FOR USERNAME
|
||||
|
||||
while (invalid_username)
|
||||
{
|
||||
@ -728,7 +647,7 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_deallocate(code);
|
||||
why2_deallocate(raw);
|
||||
|
||||
if ((raw = read_user(connection, &raw_ptr, client_server_key)) == NULL) //READ
|
||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
|
||||
{
|
||||
force_exiting = 1; //FAILURE
|
||||
goto deallocation;
|
||||
@ -772,7 +691,7 @@ void *why2_communicate_thread(void *arg)
|
||||
|
||||
if (invalid_username)
|
||||
{
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_INVALID_USERNAME); //TELL THE USER THEY ARE DUMB AS FUCK
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_INVALID_USERNAME); //TELL THE USER THEY ARE DUMB AS FUCK
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -782,7 +701,7 @@ void *why2_communicate_thread(void *arg)
|
||||
char *user_config_path = why2_get_server_users_path();
|
||||
if (!why2_toml_contains(user_config_path, decoded_buffer)) //REGISTRATION
|
||||
{
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||
|
||||
//KEEP READING UNTIL CODE ARRIVES
|
||||
char *code = NULL;
|
||||
@ -794,7 +713,7 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_deallocate(code);
|
||||
why2_deallocate(raw);
|
||||
|
||||
if ((raw = read_user(connection, &raw_ptr, client_server_key)) == NULL) //READ
|
||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
|
||||
{
|
||||
force_exiting = 1; //FAILURE
|
||||
goto deallocation;
|
||||
@ -811,7 +730,7 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_toml_write(user_config_path, username, password); //SAVE PASSWORD
|
||||
} else //LOGIN
|
||||
{
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||
|
||||
unsigned char max_tries = (unsigned char) server_config_int("max_password_tries");
|
||||
|
||||
@ -827,7 +746,7 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_deallocate(code);
|
||||
why2_deallocate(raw);
|
||||
|
||||
if ((raw = read_user(connection, &raw_ptr, client_server_key)) == NULL) //READ
|
||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
|
||||
{
|
||||
force_exiting = 1; //FAILURE
|
||||
goto deallocation;
|
||||
@ -851,7 +770,7 @@ void *why2_communicate_thread(void *arg)
|
||||
goto deallocation;
|
||||
}
|
||||
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_INVALID_PASSWORD);
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_INVALID_PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
@ -905,7 +824,7 @@ void *why2_communicate_thread(void *arg)
|
||||
|
||||
while (!(exiting || force_exiting)) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT]
|
||||
{
|
||||
if ((raw = read_user(connection, &raw_ptr, client_server_key)) == NULL) break; //READ
|
||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) break; //READ
|
||||
json = json_tokener_parse("{}");
|
||||
raw_output = why2_strdup("");
|
||||
|
||||
@ -962,7 +881,7 @@ void *why2_communicate_thread(void *arg)
|
||||
} while (buffer != NULL);
|
||||
|
||||
//SEND
|
||||
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_LIST_SERVER);
|
||||
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_LIST_SERVER);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message);
|
||||
@ -974,7 +893,7 @@ void *why2_communicate_thread(void *arg)
|
||||
sprintf(message, "%s%c", WHY2_VERSION, '\0'); //CREATE THE MESSAGE
|
||||
|
||||
//SEND
|
||||
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_VERSION_SERVER);
|
||||
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_VERSION_SERVER);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message);
|
||||
@ -1027,8 +946,8 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_bool self_pm = pm_connection_node.connection == connection;
|
||||
|
||||
//SEND YOU DUMB FUCK
|
||||
send_socket_code_deallocate(private_msg, why2_chat_server_config("server_username"), pm_connection_node.key, pm_connection_node.connection, WHY2_CHAT_CODE_DM_SERVER); //RECIPIENT
|
||||
if (!self_pm) send_socket_code_deallocate(private_msg, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_DM_SERVER); //AUTHOR
|
||||
send_socket_code_deallocate(private_msg, why2_chat_server_config("server_username"), pm_connection_node.connection, WHY2_CHAT_CODE_DM_SERVER); //RECIPIENT
|
||||
if (!self_pm) send_socket_code_deallocate(private_msg, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_DM_SERVER); //AUTHOR
|
||||
|
||||
why2_deallocate(private_msg);
|
||||
}
|
||||
@ -1064,7 +983,7 @@ void *why2_communicate_thread(void *arg)
|
||||
json_object_put(json);
|
||||
}
|
||||
|
||||
if (exiting) send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), client_server_key, connection, WHY2_CHAT_CODE_SSQC);
|
||||
if (exiting) send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_SSQC);
|
||||
|
||||
printf("User disconnected.\t%d\n", connection);
|
||||
|
||||
@ -1092,12 +1011,12 @@ void *why2_authority_communicate_thread(void *arg)
|
||||
printf("User connected.\t\t%d\n", connection);
|
||||
|
||||
//SEND USER KEY EXCHANGE CODE
|
||||
why2_send_socket_code(NULL, NULL, NULL, connection, WHY2_CHAT_CODE_KEY_EXCHANGE);
|
||||
why2_send_socket_code(NULL, NULL, connection, WHY2_CHAT_CODE_KEY_EXCHANGE);
|
||||
|
||||
do
|
||||
{
|
||||
//READ PACKET
|
||||
if ((raw = read_user(connection, &raw_ptr, NULL)) == NULL) break; //READ
|
||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) break; //READ
|
||||
|
||||
//GET DATA
|
||||
message = get_string_from_json_string(raw, "message");
|
||||
@ -1131,7 +1050,7 @@ void *why2_authority_communicate_thread(void *arg)
|
||||
fread(buffer, buffer_size, 1, cert); //READ
|
||||
|
||||
//SEND STATUS
|
||||
why2_send_socket_code(NULL, NULL, NULL, connection, strcmp(buffer, message) == 0 ? WHY2_CHAT_CODE_SUCCESS : WHY2_CHAT_CODE_FAILURE);
|
||||
why2_send_socket_code(NULL, NULL, connection, strcmp(buffer, message) == 0 ? WHY2_CHAT_CODE_SUCCESS : WHY2_CHAT_CODE_FAILURE);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(buffer);
|
||||
@ -1142,7 +1061,7 @@ void *why2_authority_communicate_thread(void *arg)
|
||||
fwrite(message, 1, strlen(message), cert); //WRITE PUBKEY
|
||||
|
||||
//CONFIRM SUCCESS
|
||||
why2_send_socket_code(NULL, NULL, NULL, connection, WHY2_CHAT_CODE_SUCCESS);
|
||||
why2_send_socket_code(NULL, NULL, connection, WHY2_CHAT_CODE_SUCCESS);
|
||||
}
|
||||
|
||||
//DEALLOCATION
|
||||
@ -1218,7 +1137,7 @@ void why2_clean_connections(void)
|
||||
|
||||
connection_buffer = *(connection_node_t*) node_buffer_2 -> value;
|
||||
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection_buffer.key, connection_buffer.connection, WHY2_CHAT_CODE_SSQC);
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection_buffer.connection, WHY2_CHAT_CODE_SSQC);
|
||||
|
||||
close(connection_buffer.connection);
|
||||
why2_list_remove(&connection_list, node_buffer_2); //REMOVE
|
||||
@ -1276,7 +1195,7 @@ void *why2_listen_server(void *socket)
|
||||
{
|
||||
continuing = 0;
|
||||
|
||||
read = read_socket_raw(connection, why2_chat_get_client_server_key());
|
||||
read = read_socket_raw(connection);
|
||||
if (read == NULL) continue;
|
||||
|
||||
//GET CONTENT
|
||||
@ -1459,7 +1378,7 @@ void *why2_listen_authority(void *socket)
|
||||
do
|
||||
{
|
||||
//READ PACKET
|
||||
read = read_socket_raw(socket_ptr, NULL);
|
||||
read = read_socket_raw(socket_ptr);
|
||||
if (read == NULL) continue; //INVALID PACKET RECEIVED
|
||||
|
||||
//GET DATA
|
||||
@ -1499,7 +1418,7 @@ void *why2_listen_authority(void *socket)
|
||||
//SEND CA CLIENT'S ENCRYPTED PUBKEY
|
||||
char *key = why2_chat_ecc_serialize_public_key();
|
||||
|
||||
why2_send_socket_code(key, username, NULL, socket_ptr, WHY2_CHAT_CODE_CLIENT_KEY_EXCHANGE); //SEND
|
||||
why2_send_socket_code(key, username, socket_ptr, WHY2_CHAT_CODE_CLIENT_KEY_EXCHANGE); //SEND
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(user_config_path);
|
||||
|
@ -70,7 +70,6 @@ why2_output_flags why2_decrypt_text(char *text, char *key)
|
||||
char *key_new = why2_strdup(key); //COPY key TO key_new
|
||||
int *encrypted_text_key_chain;
|
||||
char *used_text = NULL; //COPY text TO used_text
|
||||
unsigned long returning_len = 0;
|
||||
|
||||
if (why2_get_flags().format == WHY2_OUTPUT_BYTE)
|
||||
{
|
||||
@ -123,8 +122,7 @@ why2_output_flags why2_decrypt_text(char *text, char *key)
|
||||
}
|
||||
|
||||
//SET LENGTH (number_buffer)
|
||||
returning_len = number_buffer + 1;
|
||||
returning_text = why2_calloc(returning_len, sizeof(char));
|
||||
returning_text = why2_calloc(number_buffer + 1, sizeof(char));
|
||||
text_key_chain = why2_malloc(sizeof(int) * number_buffer);
|
||||
encrypted_text_key_chain = why2_malloc(sizeof(int) * number_buffer);
|
||||
text_key_chainLength = number_buffer;
|
||||
@ -215,8 +213,7 @@ why2_output_flags why2_decrypt_text(char *text, char *key)
|
||||
}
|
||||
|
||||
//PUT PADDED TEXT INTO text_new
|
||||
returning_len = why2_list_get_size(&split_text) + 1;
|
||||
returning_text = why2_recalloc(returning_text, returning_len, sizeof(char));
|
||||
returning_text = why2_recalloc(returning_text, why2_list_get_size(&split_text) + 1, sizeof(char));
|
||||
why2_node_t *buffer = split_text.head;
|
||||
why2_node_t *buffer_2;
|
||||
unsigned long index_buffer = 0;
|
||||
@ -240,7 +237,6 @@ why2_output_flags why2_decrypt_text(char *text, char *key)
|
||||
why2_output_flags output =
|
||||
{
|
||||
returning_text, //DECRYPTED TEXT
|
||||
returning_len, //LENGTH
|
||||
key_new, //USED KEY
|
||||
why2_count_unused_key_size(returning_text, key_new), // NUMBER OF WHY2_UNUSED CHARS IN KEY
|
||||
why2_count_repeated_key_size(returning_text, key_new), //NUMBER OF REPEATED CHARS IN KEY
|
||||
|
@ -61,7 +61,6 @@ why2_output_flags why2_encrypt_text(char *text, char *key)
|
||||
char *text_buffer = NULL;
|
||||
int *text_key_chain;
|
||||
int number_buffer = 0;
|
||||
unsigned long returning_len = 0;
|
||||
|
||||
if (key != NULL)
|
||||
{
|
||||
@ -150,8 +149,7 @@ why2_output_flags why2_encrypt_text(char *text, char *key)
|
||||
}
|
||||
|
||||
//ALLOCATE returning_text (WITH THE SEPARATORS)
|
||||
returning_len = number_buffer + strlen(text_new);
|
||||
returning_text = why2_calloc(returning_len, sizeof(char));
|
||||
returning_text = why2_calloc(number_buffer + strlen(text_new), sizeof(char));
|
||||
|
||||
//LOAD returning_text
|
||||
for (int i = 0; i < (int) strlen(text_new); i++)
|
||||
@ -173,8 +171,7 @@ why2_output_flags why2_encrypt_text(char *text, char *key)
|
||||
{
|
||||
number_buffer = (strlen(text_new) + 1) * 2; //EACH CHARACTER WILL BE SPLIT INTO TWO CHARS AND FIRST TWO WILL BE LENGTH OF text_new
|
||||
|
||||
returning_len = number_buffer + 1;
|
||||
returning_text = why2_calloc(returning_len, sizeof(char)); //ALLOCATE
|
||||
returning_text = why2_calloc(number_buffer + 1, sizeof(char)); //ALLOCATE
|
||||
|
||||
//SET LENGTH
|
||||
returning_text[0] = (strlen(text_new) & 0x7f) + 1; //+1 BECAUSE WE DON'T WANT \0
|
||||
@ -202,7 +199,6 @@ why2_output_flags why2_encrypt_text(char *text, char *key)
|
||||
why2_output_flags output =
|
||||
{
|
||||
returning_text, //ENCRYPTED TEXT
|
||||
returning_len, //LENGTH
|
||||
key_new, //GENERATED/USED KEY
|
||||
why2_count_unused_key_size(text_new, key_new), // NUMBER OF WHY2_UNUSED CHARS IN KEY
|
||||
why2_count_repeated_key_size(text_new, key_new), //NUMBER OF REPEATED CHARS IN KEY
|
||||
|
@ -74,17 +74,17 @@ why2_input_flags why2_get_flags(void)
|
||||
|
||||
why2_output_flags why2_no_output(enum WHY2_EXIT_CODES exit_code)
|
||||
{
|
||||
char *empty_text = why2_malloc(1); //TEXT
|
||||
empty_text[0] = '\0';
|
||||
char *emptyText = why2_malloc(1); //TEXT
|
||||
emptyText[0] = '\0';
|
||||
|
||||
char *empty_key = why2_malloc(why2_get_key_length() + 1); //KEY
|
||||
for (unsigned long i = 0; i < why2_get_key_length(); i++)
|
||||
char *emptyKey = why2_malloc(why2_get_key_length() + 1); //KEY
|
||||
for (int i = 0; i < (int) why2_get_key_length(); i++)
|
||||
{
|
||||
empty_key[i] = 'x';
|
||||
emptyKey[i] = 'x';
|
||||
}
|
||||
empty_key[why2_get_key_length()] = '\0';
|
||||
emptyKey[why2_get_key_length()] = '\0';
|
||||
|
||||
return (why2_output_flags) { empty_text, 0, empty_key, 0, 0, 0, exit_code };
|
||||
return (why2_output_flags) { emptyText, emptyKey, 0, 0, 0, exit_code };
|
||||
}
|
||||
|
||||
why2_encryption_operation_cb why2_get_encryption_operation(void)
|
||||
|
Loading…
x
Reference in New Issue
Block a user