Skip to content
Merged
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
9 changes: 9 additions & 0 deletions faithd/src/application/conversation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <stdint.h>

#define FAITH_CONVERSATION_ID_SIZE 16

typedef struct {
uint8_t bytes[FAITH_CONVERSATION_ID_SIZE];
} faith_conversation_id_t;
9 changes: 9 additions & 0 deletions faithd/src/application/user.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "../auth/structs.h"

typedef struct {
faith_auth_id_t auth_id;
faith_device_id_t device_id;
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE];
} application_user_identity_t;
53 changes: 28 additions & 25 deletions faithd/src/auth/device_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../codec/signatures.h"

#include "../delivery/routing.h"
#include "structs.h"

#define NOB_IMPLEMENTATION
#include "../../third_party/nob.h"
Expand All @@ -20,7 +21,7 @@ static faith_status_code_t send_device_auth_response_failed(server_state_t *s,

faith_envelope_t failed_envl = {
.type = FAITH_ENVELOPE_DEVICE_AUTH_RESPONSE_FAILED,
.recipient_id = cl->auth_id,
.recipient_id = cl->ident.auth_id,
.body = NULL,
.body_size = 0,
};
Expand Down Expand Up @@ -79,8 +80,9 @@ device_link_handle_device_response(server_state_t *s, client_conn_t *cl,
if (!cl->authorized) {
char auth_id_hex[33];
char device_id_hex[33];
_FH_CHECK_RETURN(faith_id128_to_hex(cl->auth_id.bytes, auth_id_hex));
_FH_CHECK_RETURN(faith_id128_to_hex(cl->device_id.bytes, device_id_hex));
_FH_CHECK_RETURN(faith_id128_to_hex(cl->ident.auth_id.bytes, auth_id_hex));
_FH_CHECK_RETURN(
faith_id128_to_hex(cl->ident.device_id.bytes, device_id_hex));

nob_log(ERROR,
"[client=%" PRIu64 " fd=%i] Server got unauthorized %s"
Expand Down Expand Up @@ -160,7 +162,7 @@ device_link_handle_device_response(server_state_t *s, client_conn_t *cl,
memcpy(sign_msg.code, req->code, sizeof(req->code));

sign_msg.expires_at_ms = req->expires_at_ms;
sign_msg.device_id_responding = cl->device_id;
sign_msg.device_id_responding = cl->ident.device_id;

sign_msg.type = response_envl->type == FAITH_ENVELOPE_DEVICE_AUTH_APPROVE
? FAITH_DEVICE_LINK_APPROVE
Expand All @@ -186,11 +188,12 @@ device_link_handle_device_response(server_state_t *s, client_conn_t *cl,

client_device_session_data_t *sess = NULL;
{
_FH_CHECK(
sess_registry_get_session(&s->rt, &cl->auth_id, &cl->device_id, &sess));
_FH_CHECK(sess_registry_get_session(&s->rt, &cl->ident.auth_id,
&cl->ident.device_id, &sess));
/* We specifically need routing_get_session() to return FAITH_OK. This is
* returned only if <cl->auth_id> is a registered client_route_user_t
* and <cl->device_id> is a registered client_route_device_t of that user.
* returned only if <cl->ident.auth_id> is a registered client_route_user_t
* and <cl->ident.device_id> is a registered client_route_device_t of that
* user.
* */
if (_fh_rc != FAITH_OK) {
_fh_result = _fh_rc;
Expand All @@ -202,10 +205,10 @@ device_link_handle_device_response(server_state_t *s, client_conn_t *cl,
}
}

/* This means cl->auth_id is registered but cl->device_id is not, effectively
* telling us that the client connection is not yet authorized. Because we
* checked cl->authorized above, this should never happen with correct
* behaviour.*/
/* This means cl->ident.auth_id is registered but cl->ident.device_id is not,
* effectively telling us that the client connection is not yet authorized.
* Because we checked cl->authorized above, this should never happen with
* correct behaviour.*/
if (!sess) {
nob_log(ERROR,
"[client=%" PRIu64
Expand Down Expand Up @@ -265,11 +268,12 @@ device_link_handle_device_response(server_state_t *s, client_conn_t *cl,
faith_envelope_t ack_envl = {0};
ack_envl.type = FAITH_ENVELOPE_DEVICE_AUTH_RESPONSE_ACK;
_FH_CHECK_RETURN(
delivery_route_envelope_to_auth_id(s, cl, &cl->auth_id, &ack_envl));
delivery_route_envelope_to_auth_id(s, cl, &cl->ident.auth_id, &ack_envl));

faith_status_code_t device_loop_rc = FAITH_OK;
_FH_FOR_EACH_AUTH_DEVICE(s, &cl->auth_id, recipient, device_loop_rc,
{ device_link_remove_request(cl); });
_FH_FOR_EACH_AUTH_DEVICE_CONNECTION(s, &cl->ident.auth_id, recipient,
device_loop_rc,
{ device_link_remove_request(cl); });

return device_loop_rc == FAITH_OK ? rc : device_loop_rc;

Expand Down Expand Up @@ -352,7 +356,7 @@ device_link_new_device(server_state_t *s, client_conn_t *cl,
/* Send device authorization request to every already registered device
for that auth ID */
faith_status_code_t device_loop_rc = FAITH_OK;
_FH_FOR_EACH_AUTH_DEVICE(
_FH_FOR_EACH_AUTH_DEVICE_CONNECTION(
s, &params->sender_auth_id, authorized_cl, device_loop_rc, {
faith_envl_stc_device_link_req_t *req = NULL;
_FH_CHECK(device_link_queue_request(
Expand Down Expand Up @@ -384,9 +388,8 @@ device_link_new_device(server_state_t *s, client_conn_t *cl,
/* Send DEVICE_AUTH_PENDING to the connection that requested the
* new device */
_FH_CHECK_RETURN(auth_queue_auth_pending(s, cl));
/* temporarily assign <cl->auth_id> for disconnection purposes later. this
* does not mean that the client is authorized. */
cl->auth_id = params->sender_auth_id;

cl->pending_auth_id = params->sender_auth_id;
server_set_client_state(s, cl, CLIENT_WAIT_FOR_DEVICE_LINK_RESPONSE);

return FAITH_OK;
Expand All @@ -405,15 +408,15 @@ device_link_queue_request_cancellation(server_state_t *s,
&requesting_cl->temp_handshake_params;

faith_status_code_t device_loop_rc = FAITH_OK;
_FH_FOR_EACH_AUTH_DEVICE(
_FH_FOR_EACH_AUTH_DEVICE_CONNECTION(
s, &params->sender_auth_id, authorized_cl, device_loop_rc, {
if (authorized_cl->pending_device_link_conn != requesting_cl)
continue;

/* Send DEVICE_LINK_CANCELLED to the authorized device */
faith_envelope_t envl = {0};
envl.type = FAITH_ENVELOPE_DEVICE_LINK_CANCELLED;
envl.recipient_id = authorized_cl->auth_id;
envl.recipient_id = authorized_cl->ident.auth_id;
_FH_CHECK(server_queue_envelope_or_mark_dead(s, authorized_cl, &envl));

if (_fh_rc != FAITH_OK && device_loop_rc == FAITH_OK)
Expand Down Expand Up @@ -446,9 +449,9 @@ faith_status_code_t device_link_queue_request(
char auth_id_hex[33];
char device_id_hex[33];
_FH_CHECK_RETURN(
faith_id128_to_hex(recipient_cl->auth_id.bytes, auth_id_hex));
faith_id128_to_hex(recipient_cl->ident.auth_id.bytes, auth_id_hex));
_FH_CHECK_RETURN(
faith_id128_to_hex(recipient_cl->device_id.bytes, device_id_hex));
faith_id128_to_hex(recipient_cl->ident.device_id.bytes, device_id_hex));
nob_log(ERROR,
"Not sending device link request to "
"device with device_id: %s (auth_id: %s). Client connection is "
Expand All @@ -462,9 +465,9 @@ faith_status_code_t device_link_queue_request(
char auth_id_hex[33];
char device_id_hex[33];
_FH_CHECK_RETURN(
faith_id128_to_hex(recipient_cl->auth_id.bytes, auth_id_hex));
faith_id128_to_hex(recipient_cl->ident.auth_id.bytes, auth_id_hex));
_FH_CHECK_RETURN(
faith_id128_to_hex(recipient_cl->device_id.bytes, device_id_hex));
faith_id128_to_hex(recipient_cl->ident.device_id.bytes, device_id_hex));
nob_log(ERROR,
"Not sending device link request to "
"device with device_id: %s (auth_id: %s). Another device link "
Expand Down
31 changes: 19 additions & 12 deletions faithd/src/auth/handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "../codec/protocol.h"
#include "../codec/signatures.h"

#include "../delivery/events.h"

faith_status_code_t auth_handle_hello(server_state_t *s, client_conn_t *cl,
const faith_envelope_t *hello_envl) {
// HELLO {
Expand Down Expand Up @@ -252,10 +254,13 @@ faith_status_code_t auth_handle_challenge_response(
return FAITH_OK;
}

_FH_CHECK(auth_authorize_client(s, cl, &params->sender_auth_id,
&params->device_id, verification_public_key,
sess == NULL));
return _fh_rc;
_FH_CHECK_RETURN(
auth_authorize_client(s, cl, &params->sender_auth_id, &params->device_id,
verification_public_key, sess == NULL));

_FH_CHECK_RETURN(delivery_queue_pending_events(s, cl));

return FAITH_OK;
reject: {

/* =============================== */
Expand All @@ -279,7 +284,7 @@ reject: {
faith_status_code_t auth_authorize_client(
server_state_t *s, client_conn_t *cl, const faith_auth_id_t *auth_id,
const faith_device_id_t *device_id,
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE], int register_session) {
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE], bool register_session) {
if (!s || !cl || cl->closing || !auth_id || !device_id || !public_key)
return FAITH_ERR_INVALID;

Expand All @@ -291,13 +296,14 @@ faith_status_code_t auth_authorize_client(
if (register_session) {
/* Register client session */
_FH_CHECK_RETURN(sess_registry_register_session(
&s->rt, &cl->auth_id, &cl->device_id, cl, public_key));
&s->rt, &cl->ident.auth_id, &cl->ident.device_id, cl, public_key));
}

char cl_auth_id_hex[33];
char cl_device_id_hex[33];
_FH_CHECK_RETURN(faith_id128_to_hex(cl->auth_id.bytes, cl_auth_id_hex));
_FH_CHECK_RETURN(faith_id128_to_hex(cl->device_id.bytes, cl_device_id_hex));
_FH_CHECK_RETURN(faith_id128_to_hex(cl->ident.auth_id.bytes, cl_auth_id_hex));
_FH_CHECK_RETURN(
faith_id128_to_hex(cl->ident.device_id.bytes, cl_device_id_hex));

nob_log(INFO,
"[client=%" PRIu64 " fd=%i] Client passed authorization for "
Expand All @@ -324,8 +330,8 @@ auth_handshake_complete(server_state_t *s, client_conn_t *cl,

_FH_CHECK_RETURN(server_queue_envelope_or_mark_dead(s, cl, &hello_ok_envl));

cl->auth_id = *sender_id;
cl->device_id = *device_id;
cl->ident.auth_id = *sender_id;
cl->ident.device_id = *device_id;

server_set_client_state(s, cl, CLIENT_OPEN);

Expand All @@ -334,8 +340,9 @@ auth_handshake_complete(server_state_t *s, client_conn_t *cl,

faith_status_code_t _fh_result = FAITH_OK;

_FH_CHECK_RETURN(faith_id128_to_hex(cl->auth_id.bytes, auth_id_hex));
_FH_CHECK_RETURN(faith_id128_to_hex(cl->device_id.bytes, device_id_hex));
_FH_CHECK_RETURN(faith_id128_to_hex(cl->ident.auth_id.bytes, auth_id_hex));
_FH_CHECK_RETURN(
faith_id128_to_hex(cl->ident.device_id.bytes, device_id_hex));

nob_log(INFO,
"[client=%" PRIu64
Expand Down
2 changes: 1 addition & 1 deletion faithd/src/auth/handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ auth_handle_challenge_response(server_state_t *s, client_conn_t *cl,
faith_status_code_t auth_authorize_client(
server_state_t *s, client_conn_t *cl, const faith_auth_id_t *auth_id,
const faith_device_id_t *device_id,
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE], int register_session);
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE], bool register_session);

faith_status_code_t auth_handshake_complete(server_state_t *s,
client_conn_t *cl,
Expand Down
36 changes: 35 additions & 1 deletion faithd/src/auth/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "../core/core.h"
#include "../core/crypto.h"

#define _FH_FOR_EACH_AUTH_DEVICE(SERVER, AUTH_ID, RECIPIENT, STATUS_OUT, BODY) \
#define _FH_FOR_EACH_AUTH_DEVICE_CONNECTION(SERVER, AUTH_ID, RECIPIENT, \
STATUS_OUT, BODY) \
do { \
server_state_t *_fh_iter_server = (SERVER); \
const faith_auth_id_t *_fh_iter_auth_id = (AUTH_ID); \
Expand Down Expand Up @@ -38,6 +39,39 @@
} \
} while (0)

#define _FH_FOR_EACH_AUTH_DEVICE_SESSION(SERVER, AUTH_ID, DEVICE_SESSION, \
STATUS_OUT, BODY) \
do { \
server_state_t *_fh_iter_server = (SERVER); \
const faith_auth_id_t *_fh_iter_auth_id = (AUTH_ID); \
client_session_device_t *_fh_iter_devices = NULL; \
\
if (!_fh_iter_server || !_fh_iter_auth_id) { \
(STATUS_OUT) = FAITH_ERR_INVALID; \
break; \
} \
\
(STATUS_OUT) = sess_registry_get_devices( \
&_fh_iter_server->rt, _fh_iter_auth_id, &_fh_iter_devices); \
\
if ((STATUS_OUT) == FAITH_ERR_NOT_FOUND) { \
(STATUS_OUT) = FAITH_OK; \
} else if ((STATUS_OUT) == FAITH_OK) { \
ptrdiff_t _fh_iter_count = hmlen(_fh_iter_devices); \
\
for (ptrdiff_t _fh_iter_i = 0; _fh_iter_i < _fh_iter_count; \
++_fh_iter_i) { \
if (!_fh_iter_devices[_fh_iter_i].value) \
continue; \
\
client_device_session_data_t *(DEVICE_SESSION) = \
_fh_iter_devices[_fh_iter_i].value; \
\
BODY \
} \
} \
} while (0)

#define FAITH_AUTH_ID_SIZE 16
#define FAITH_DEVICE_ID_SIZE 16

Expand Down
Loading
Loading