1
0
Fork 0
tetatet/src/main.c

184 lines
3.9 KiB
C
Raw Normal View History

// #include "audio.h"
2024-03-21 17:51:03 +04:00
#include "crypt.h"
2024-03-24 20:14:16 +04:00
#include "net.h"
2024-03-21 17:51:03 +04:00
#define CHANNELS 1
#define SAMPLE_RATE 48000
#define FRAMES_PER_BUFFFER 960
2024-03-26 04:51:31 +04:00
#define AUDIO_BUF_SIZE (FRAMES_PER_BUFFFER * CHANNELS)
2024-03-21 17:51:03 +04:00
2024-03-26 04:51:31 +04:00
#define NET_BUF_SIZE 1024
2024-03-21 04:10:02 +04:00
2024-03-26 04:51:31 +04:00
int client_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk);
int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk);
2024-03-26 04:51:31 +04:00
void usage(void) {
2024-03-26 04:54:18 +04:00
fprintf(stderr, "tetatet c|s ADDR PORT\n");
2024-03-26 04:51:31 +04:00
}
2024-03-26 04:51:31 +04:00
int main(int argc, char **argv) {
int ret = 0;
2024-03-26 04:51:31 +04:00
if (argc < 4) {
usage();
return -1;
}
if (sodium_init() == -1) {
2024-03-21 17:51:03 +04:00
return -1;
}
// if (audio_init_soundsystem() == -1)
// return -1;
2024-03-21 17:51:03 +04:00
2024-03-26 04:51:31 +04:00
char direction = argv[1][0];
char *addr = argv[2];
char *port = argv[3];
net_t n;
crypt_session_t s;
crypt_key_t ok, rk;
2024-03-21 17:51:03 +04:00
2024-03-26 04:51:31 +04:00
if (crypt_key_gen(&ok) == -1) {
ret = -1;
goto cleanup;
}
2024-03-26 04:51:31 +04:00
if (direction == 'c') {
if (net_client_init(&n, addr, port) == -1) {
ret = -1;
goto cleanup;
}
2024-03-26 04:51:31 +04:00
int res = client_handshake(&n, &s, &ok, &rk);
2024-03-26 04:51:31 +04:00
if (res != 0) {
ret = res;
goto cleanup;
}
} else if (direction == 's') {
if (net_server_init(&n, addr, port) == -1) {
ret = -1;
goto cleanup;
}
2024-03-23 04:22:01 +04:00
2024-03-26 04:51:31 +04:00
int res = server_handshake(&n, &s, &ok, &rk);
2024-03-23 04:22:01 +04:00
2024-03-26 04:51:31 +04:00
if (res != 0) {
ret = -1;
goto cleanup;
}
} else {
usage();
}
2024-03-23 04:22:01 +04:00
2024-03-26 04:51:31 +04:00
fprintf(stderr, "A connection established!\n");
2024-03-23 04:22:01 +04:00
2024-03-26 04:51:31 +04:00
cleanup:
2024-03-26 04:51:31 +04:00
net_destroy(&n);
crypt_session_destroy(&s);
crypt_key_destroy(&ok);
crypt_key_destroy(&rk);
2024-03-24 20:14:16 +04:00
2024-03-26 04:51:31 +04:00
// if (audio_terminate_soundsystem() == -1)
// return -1;
2024-03-24 20:14:16 +04:00
2024-03-26 04:51:31 +04:00
return ret;
}
2024-03-26 04:51:31 +04:00
int client_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk) {
char buffer[NET_BUF_SIZE];
ssize_t bytes = 0;
unsigned char *hello = NULL;
unsigned char *nonce = NULL;
2024-03-26 04:51:31 +04:00
if ((hello = crypt_hello(ok)) == NULL)
return -1;
if (net_send(n, (char *)hello, CRYPT_HELLO_LEN) == -1) {
free(hello);
return -1;
}
2024-03-24 20:14:16 +04:00
2024-03-26 04:51:31 +04:00
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1) {
free(hello);
return -1;
}
2024-03-26 04:51:31 +04:00
if (CRYPT_HELLO_LEN != bytes) {
free(hello);
return -1;
}
2024-03-26 04:51:31 +04:00
if (crypt_key_from_hex_public(rk, buffer) == -1) {
free(hello);
return -1;
}
2024-03-26 04:51:31 +04:00
if (crypt_hello_verify((unsigned char *)buffer, rk) == -1) {
free(hello);
return -2;
}
2024-03-26 04:51:31 +04:00
if ((nonce = crypt_hello_get_nonce(hello, (unsigned char *)buffer, true)) == NULL) {
free(hello);
return -1;
}
2024-03-26 04:51:31 +04:00
free(hello);
2024-03-26 04:51:31 +04:00
if (crypt_session_init(s, ok, rk, nonce, true) == -1) {
free(nonce);
return -3;
}
2024-03-26 04:51:31 +04:00
free(nonce);
2024-03-26 04:51:31 +04:00
return 0;
}
2024-03-26 04:51:31 +04:00
int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk) {
char buffer[NET_BUF_SIZE];
ssize_t bytes = 0;
unsigned char *hello = NULL;
unsigned char *nonce = NULL;
2024-03-26 04:51:31 +04:00
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1)
return -1;
2024-03-26 04:51:31 +04:00
if (CRYPT_HELLO_LEN != bytes)
return -1;
2024-03-23 04:22:01 +04:00
2024-03-26 04:51:31 +04:00
if (crypt_key_from_hex_public(rk, buffer) == -1)
return -1;
2024-03-23 04:22:01 +04:00
2024-03-26 04:51:31 +04:00
if (crypt_hello_verify((unsigned char *)buffer, rk) == -1)
return -2;
2024-03-26 04:51:31 +04:00
if ((hello = crypt_hello(ok)) == NULL)
return -1;
2024-03-24 20:14:16 +04:00
2024-03-26 04:51:31 +04:00
if ((nonce = crypt_hello_get_nonce(hello, (unsigned char *)buffer, false)) == NULL) {
free(hello);
return -1;
}
if (crypt_session_init(s, ok, rk, nonce, false) == -1) {
free(nonce);
return -3;
}
n->raddr = n->inaddr;
n->raddr_len = n->inaddr_len;
if (net_send(n, (char *)hello, CRYPT_HELLO_LEN) == -1) {
free(hello);
return -1;
}
free(hello);
2024-03-26 04:51:31 +04:00
free(nonce);
return 0;
}