164 lines
3.6 KiB
C
164 lines
3.6 KiB
C
// #include "audio.h"
|
|
#include "crypt.h"
|
|
#include "net.h"
|
|
|
|
#define CHANNELS 1
|
|
#define SAMPLE_RATE 48000
|
|
#define FRAMES_PER_BUFFFER 960
|
|
#define AUDIO_BUF_SIZE (FRAMES_PER_BUFFFER * CHANNELS)
|
|
|
|
#define NET_BUF_SIZE 1024
|
|
|
|
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);
|
|
|
|
void usage(void) {
|
|
fprintf(stderr, "tetatet c|s ADDR PORT\n");
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 4) {
|
|
usage();
|
|
return -1;
|
|
}
|
|
|
|
if (sodium_init() == -1) {
|
|
return -1;
|
|
}
|
|
|
|
// if (audio_init_soundsystem() == -1)
|
|
// return -1;
|
|
|
|
char direction = argv[1][0];
|
|
char *addr = argv[2];
|
|
char *port = argv[3];
|
|
|
|
if (direction != 'c' && direction != 's') {
|
|
usage();
|
|
return -1;
|
|
}
|
|
|
|
int result = 0;
|
|
net_t n;
|
|
crypt_session_t s;
|
|
crypt_key_t ok, rk;
|
|
|
|
if ((result = crypt_key_gen(&ok)) == -1)
|
|
goto cleanup;
|
|
|
|
(direction == 'c') ?
|
|
(result = net_client_init(&n, addr, port))
|
|
: (result = net_server_init(&n, addr, port));
|
|
if (result != 0)
|
|
goto cleanup;
|
|
|
|
(direction == 'c') ?
|
|
(result = client_handshake(&n, &s, &ok, &rk))
|
|
: (result = server_handshake(&n, &s, &ok, &rk));
|
|
if (result != 0)
|
|
goto cleanup;
|
|
|
|
char rkph[CRYPT_PKEY_HEXLEN];
|
|
|
|
crypt_key_export_public(&rk, rkph);
|
|
|
|
fprintf(stderr, "A connection with %s established!\n", rkph);
|
|
|
|
cleanup:
|
|
|
|
net_destroy(&n);
|
|
crypt_session_destroy(&s);
|
|
crypt_key_destroy(&ok);
|
|
crypt_key_destroy(&rk);
|
|
|
|
// if (audio_terminate_soundsystem() == -1)
|
|
// return -1;
|
|
|
|
return result;
|
|
}
|
|
|
|
int handshake(const unsigned char * hello, const char * remote_hello, crypt_session_t *const s, crypt_key_t *const ok, crypt_key_t *const rk) {
|
|
unsigned char *nonce = NULL;
|
|
|
|
if (crypt_key_from_hex_public(rk, remote_hello) == -1)
|
|
return -1;
|
|
|
|
if (crypt_hello_verify((unsigned char *)remote_hello, rk) == -1)
|
|
return -2;
|
|
|
|
if ((nonce = crypt_hello_get_nonce(hello, (unsigned char *)remote_hello, true)) == NULL)
|
|
return -1;
|
|
|
|
if (crypt_session_init(s, ok, rk, nonce, true) == -1) {
|
|
free(nonce);
|
|
return -3;
|
|
}
|
|
|
|
free(nonce);
|
|
|
|
return 0;
|
|
}
|
|
|
|
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;
|
|
|
|
if ((hello = crypt_hello(ok)) == NULL)
|
|
return -1;
|
|
|
|
if (net_send(n, (char *)hello, CRYPT_HELLO_LEN) == -1) {
|
|
free(hello);
|
|
return -1;
|
|
}
|
|
|
|
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1) {
|
|
free(hello);
|
|
return -1;
|
|
}
|
|
|
|
if (CRYPT_HELLO_LEN != bytes) {
|
|
free(hello);
|
|
return -1;
|
|
}
|
|
|
|
int res = handshake(hello, buffer, s, ok, rk);
|
|
|
|
free(hello);
|
|
|
|
return res;
|
|
}
|
|
|
|
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;
|
|
|
|
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1)
|
|
return -1;
|
|
|
|
if (CRYPT_HELLO_LEN != bytes)
|
|
return -1;
|
|
|
|
if ((hello = crypt_hello(ok)) == NULL)
|
|
return -1;
|
|
|
|
|
|
int res = handshake(hello, buffer, s, ok, rk);
|
|
if (res != 0) {
|
|
free(hello);
|
|
return res;
|
|
}
|
|
|
|
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);
|
|
|
|
return 0;
|
|
} |