Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,16 @@ int main(int argc, char *argv[])

generate_encryption_tools(&send_encryption_tools);

char username[256] = "user1";
const char* password = "password";
unsigned char hashed_password[crypto_hash_BYTES];
crypto_hash(hashed_password, (const unsigned char*)password, strlen(password));
if (do_handshake_client(SERVER_PORT, CLIENT_PORT, &send_encryption_tools, &read_encryption_tools, handshake_message))
FATAL("Failed to do handshake\n");

if (login(username, hashed_password, &send_encryption_tools))
FATAL("Failed to validate credentials\n");

if (!strcmp(argv[1], "-up"))
{
CHECK_ARGS(3, "The filename must be provided");
Expand Down
8 changes: 8 additions & 0 deletions src/client/client_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ int do_handshake_client(int port, int response_port, ENCRYPTION_TOOLS *send_encr
free(handshake_message);
return 0;
}

int login(char *username, unsigned char *hashed_password, ENCRYPTION_TOOLS *send_encryption_tools){
LOGIN_MESSAGE message = {0};
memcpy((char *)message.username, (char *)username,sizeof(message.username));
memcpy((char *)message.hashed_password, (char *)hashed_password, sizeof(message.hashed_password));
int err = send_login_message(SERVER_PORT, &message, send_encryption_tools);
return err;
}
4 changes: 4 additions & 0 deletions src/client/client_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,7 @@ int send_handshake_message(int port, int response_port, ENCRYPTION_TOOLS *encryp
memcpy(msg.nonce, encryption_tools->nonce, crypto_box_NONCEBYTES);
return send_message(&msg, port, NULL);
}

int send_login_message(int port, LOGIN_MESSAGE* message, ENCRYPTION_TOOLS *encryption_tools){
return send_memory_zone(message, sizeof(*message), LOGIN, port, encryption_tools->nonce, encryption_tools->private_key, encryption_tools->public_key);
}
1 change: 1 addition & 0 deletions src/include/client_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
int send_message(void *message, int port, ENCRYPTION_TOOLS *encryption_tools);
int send_handshake_message(int port, int response_port, ENCRYPTION_TOOLS *encryption_tools);
int do_handshake_client(int port, int response_port, ENCRYPTION_TOOLS *send_encryption_tools, ENCRYPTION_TOOLS *read_encryption_tools, HAND_SHAKE_MESSAGE *handshake_message);
int login(char *username, unsigned char *hashed_password, ENCRYPTION_TOOLS *send_encryption_tools);

#endif
1 change: 1 addition & 0 deletions src/include/client_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ int send_memory_zone(void *start, size_t len, MESSAGE_TYPE msg_type, int port, u
int read_message(void **msg, ENCRYPTION_TOOLS *encryption_tools);
int send_message(void *message, int port, ENCRYPTION_TOOLS *encryption_tools);
int send_handshake_message(int port, int response_port, ENCRYPTION_TOOLS *encryption_tools);
int send_login_message(int port, LOGIN_MESSAGE* message, ENCRYPTION_TOOLS *encryption_tools);

#endif
7 changes: 6 additions & 1 deletion src/include/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ typedef enum __attribute__((packed))
typedef enum __attribute__((packed))
{
HAND_SHAKE = 'H',
TRANSFERT = 'T'
TRANSFERT = 'T',
LOGIN = 'L',
} MESSAGE_TYPE;

typedef struct __attribute__((packed))
Expand Down Expand Up @@ -44,5 +45,9 @@ typedef struct {
unsigned char nonce[crypto_box_NONCEBYTES];
} HAND_SHAKE_MESSAGE;

typedef struct {
const char username[256];
unsigned char hashed_password[crypto_hash_BYTES];
} LOGIN_MESSAGE;

#endif
1 change: 1 addition & 0 deletions src/include/server_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

int read_message(void **msg, ENCRYPTION_TOOLS *encryption_tools);
int do_handshake_server(ENCRYPTION_TOOLS *send_encryption_tools, ENCRYPTION_TOOLS *read_encryption_tools, HAND_SHAKE_MESSAGE **handshake_message);
int read_login(unsigned char **authorized_user_list, ENCRYPTION_TOOLS *encryption_tools);

#endif
11 changes: 10 additions & 1 deletion src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ int main(int argc, char **argv)
FATAL("Failed to initialize sodium\n");

HAND_SHAKE_MESSAGE *handshake_message = NULL;


unsigned char *user1 = (unsigned char *)"user1";
unsigned char user1_hashed_passwd[129] = "b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86";
unsigned char *authorized_user_list[] = {user1, user1_hashed_passwd};

generate_encryption_tools(&send_encryption_tools);
unsigned char *public_server_key = malloc(crypto_box_PUBLICKEYBYTES);
memcpy(public_server_key, send_encryption_tools.public_key, crypto_box_PUBLICKEYBYTES);
Expand All @@ -82,6 +86,11 @@ int main(int argc, char **argv)
continue;
}

if (read_login(authorized_user_list, &read_encryption_tools)) {
ERROR("Failed to confirm credential\n");
continue;
}

MESSAGE *message = NULL;
if (read_message((void**)&message, &read_encryption_tools))
{
Expand Down
31 changes: 31 additions & 0 deletions src/server/server_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,34 @@ int do_handshake_server(ENCRYPTION_TOOLS *send_encryption_tools, ENCRYPTION_TOOL
memcpy(read_encryption_tools->public_key, (*handshake_message)->public_key, crypto_box_PUBLICKEYBYTES);
return 0;
}

int hex_to_bin(const char *hex, unsigned char *bin) {
size_t len = strlen(hex);
if (len % 2 != 0) {
return -1;
}

for (size_t i = 0; i < len / 2; i++) {
if (sscanf(hex + 2 * i, "%2hhx", &bin[i]) != 1) {
return -1;
}
}
return 0;
}

int read_login(unsigned char** authorized_user_list, ENCRYPTION_TOOLS *encryption_tools) {
LOGIN_MESSAGE* login_message = NULL;
size_t len;

int err = read_bytes(LOGIN, (void**)&login_message, &len, encryption_tools->nonce, encryption_tools->private_key, encryption_tools->public_key);
if (err)
return err;
unsigned char stored_hash_bin[crypto_hash_BYTES];
if (hex_to_bin((const char*)authorized_user_list[1], stored_hash_bin) != 0) {
printf("Error: Failed to convert hex to binary.\n");
return 1;
}
int cmp = strcmp((const char*)login_message->username, (const char*)authorized_user_list[0]) != 0 || memcmp(login_message->hashed_password, stored_hash_bin, crypto_hash_BYTES) != 0;
free(login_message);
return cmp;
}