1
0

Move a common handshake stage in a separate func.

This commit is contained in:
Alexander Andreev 2024-03-26 17:21:37 +04:00
parent 5e45b95fdf
commit de1d4085b9
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -87,11 +87,32 @@ cleanup:
return ret;
}
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;
unsigned char *nonce = NULL;
if ((hello = crypt_hello(ok)) == NULL)
return -1;
@ -111,38 +132,17 @@ int client_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
return -1;
}
if (crypt_key_from_hex_public(rk, buffer) == -1) {
free(hello);
return -1;
}
if (crypt_hello_verify((unsigned char *)buffer, rk) == -1) {
free(hello);
return -2;
}
if ((nonce = crypt_hello_get_nonce(hello, (unsigned char *)buffer, true)) == NULL) {
free(hello);
return -1;
}
int res = handshake(hello, buffer, s, ok, rk);
free(hello);
if (crypt_session_init(s, ok, rk, nonce, true) == -1) {
free(nonce);
return -3;
}
free(nonce);
return 0;
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;
unsigned char *nonce = NULL;
if ((bytes = net_recv(n, buffer, NET_BUF_SIZE)) == -1)
return -1;
@ -150,23 +150,14 @@ int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
if (CRYPT_HELLO_LEN != bytes)
return -1;
if (crypt_key_from_hex_public(rk, buffer) == -1)
return -1;
if (crypt_hello_verify((unsigned char *)buffer, rk) == -1)
return -2;
if ((hello = crypt_hello(ok)) == NULL)
return -1;
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;
int res = handshake(hello, buffer, s, ok, rk);
if (res != 0) {
free(hello);
return res;
}
n->raddr = n->inaddr;
@ -178,7 +169,6 @@ int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
}
free(hello);
free(nonce);
return 0;
}