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
20 changes: 13 additions & 7 deletions faithd/nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ static const char *faithd_sources[] = {
"src/codec/helpers.c",
"src/codec/msg.c",
"src/codec/protocol.c",
"src/codec/commands.c",
"src/codec/signatures.c",
"src/core/core.c",
"src/core/crypto.c",
"src/core/envelopes.c",
"src/codec/envelopes.c",
"src/delivery/routing.c",
"src/logging/logging.c",
"src/reactor/reactor.c",
Expand All @@ -103,6 +104,8 @@ static const char *faithd_sources[] = {
"src/transport/conn.c",
"src/transport/frame.c",
"src/transport/tls.c",
"src/commands/conversation.c",
"src/commands/dispatch.c",
"src/faithd.c",
};

Expand All @@ -114,10 +117,11 @@ static const char *faithd_objects[] = {
BUILD_FOLDER "obj/codec_helpers.o",
BUILD_FOLDER "obj/codec_msg.o",
BUILD_FOLDER "obj/codec_protocol.o",
BUILD_FOLDER "obj/codec_commands.o",
BUILD_FOLDER "obj/codec_signatures.o",
BUILD_FOLDER "obj/core_core.o",
BUILD_FOLDER "obj/core_crypto.o",
BUILD_FOLDER "obj/core_envelopes.o",
BUILD_FOLDER "obj/codec_envelopes.o",
BUILD_FOLDER "obj/delivery_routing.o",
BUILD_FOLDER "obj/logging_logging.o",
BUILD_FOLDER "obj/reactor_reactor.o",
Expand All @@ -129,6 +133,8 @@ static const char *faithd_objects[] = {
BUILD_FOLDER "obj/transport_conn.o",
BUILD_FOLDER "obj/transport_frame.o",
BUILD_FOLDER "obj/transport_tls.o",
BUILD_FOLDER "obj/commands_conversation.o",
BUILD_FOLDER "obj/commands_dispatch.o",
BUILD_FOLDER "obj/faithd.o",
};

Expand All @@ -141,11 +147,11 @@ static const char *test_objects[] = {
};

static const char *test_shared_objects[] = {
BUILD_FOLDER "obj/codec_protocol.o", BUILD_FOLDER "obj/core_crypto.o",
BUILD_FOLDER "obj/core_core.o", BUILD_FOLDER "obj/codec_helpers.o",
BUILD_FOLDER "obj/core_envelopes.o", BUILD_FOLDER "obj/client_client.o",
BUILD_FOLDER "obj/auth_structs.o", BUILD_FOLDER "obj/codec_signatures.o",
BUILD_FOLDER "obj/codec_msg.o",
BUILD_FOLDER "obj/codec_protocol.o", BUILD_FOLDER "obj/core_crypto.o",
BUILD_FOLDER "obj/core_core.o", BUILD_FOLDER "obj/codec_helpers.o",
BUILD_FOLDER "obj/codec_envelopes.o", BUILD_FOLDER "obj/client_client.o",
BUILD_FOLDER "obj/auth_structs.o", BUILD_FOLDER "obj/codec_signatures.o",
BUILD_FOLDER "obj/codec_msg.o", BUILD_FOLDER "obj/codec_commands.o",
};

static bool compile_source_async(Procs *procs, const char *source,
Expand Down
4 changes: 2 additions & 2 deletions faithd/src/auth/device_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ device_link_handle_device_response(server_state_t *s, client_conn_t *cl,

defer: {
/* Copies for logging */
faith_client_id_t client_id = req->auth_id;
faith_auth_id_t client_id = req->auth_id;
faith_device_id_t device_id_new = req->device_id_new;

/* Remove the pending device link request from <cl>,
Expand Down Expand Up @@ -426,7 +426,7 @@ device_link_queue_request_cancellation(server_state_t *s,

faith_status_code_t device_link_queue_request(
server_state_t *s, client_conn_t *recipient_cl, client_conn_t *request_cl,
const faith_client_id_t *auth_id,
const faith_auth_id_t *auth_id,
const uint8_t public_key_new_device[FAITH_ED25519_PUBLIC_KEY_SIZE],
const faith_device_id_t *new_device_id,
faith_envl_stc_device_link_req_t **o_req) {
Expand Down
2 changes: 1 addition & 1 deletion faithd/src/auth/device_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ device_link_queue_request_cancellation(server_state_t *s,

faith_status_code_t device_link_queue_request(
server_state_t *s, client_conn_t *recipient_cl, client_conn_t *request_cl,
const faith_client_id_t *auth_id,
const faith_auth_id_t *auth_id,
const uint8_t public_key_new_device[FAITH_ED25519_PUBLIC_KEY_SIZE],
const faith_device_id_t *new_device_id,
faith_envl_stc_device_link_req_t **o_req);
Expand Down
6 changes: 3 additions & 3 deletions faithd/src/auth/envelopes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "../core/envelopes.h"
#include "../codec/envelopes.h"

#define FAITH_DEVICE_LINK_CODE_SIZE 16

Expand All @@ -9,7 +9,7 @@
FAITH_DEVICE_ID_SIZE /*device ID new*/)

#define FAITH_ENVL_STC_DEVICE_LINK_REQ_BODY_SIZE \
_FAITH_BODY_SIZE(FAITH_CLIENT_ID_SIZE /* auth ID */ + \
_FAITH_BODY_SIZE(FAITH_AUTH_ID_SIZE /* auth ID */ + \
FAITH_ED25519_PUBLIC_KEY_SIZE /*public key new device */ + \
FAITH_DEVICE_ID_SIZE /* device ID new */ + \
FAITH_DEVICE_LINK_CODE_SIZE /* code */ + \
Expand All @@ -25,7 +25,7 @@

typedef struct {
// Auth ID of the session that wants to link a new device
faith_client_id_t auth_id;
faith_auth_id_t auth_id;
uint8_t public_key_new_device[FAITH_ED25519_PUBLIC_KEY_SIZE];
faith_device_id_t device_id_new;

Expand Down
4 changes: 2 additions & 2 deletions faithd/src/auth/handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ reject: {
}

faith_status_code_t auth_authorize_client(
server_state_t *s, client_conn_t *cl, const faith_client_id_t *auth_id,
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) {
if (!s || !cl || cl->closing || !auth_id || !device_id || !public_key)
Expand Down Expand Up @@ -309,7 +309,7 @@ faith_status_code_t auth_authorize_client(

faith_status_code_t
auth_handshake_complete(server_state_t *s, client_conn_t *cl,
const faith_client_id_t *sender_id,
const faith_auth_id_t *sender_id,
const faith_device_id_t *device_id) {
if (!s || !cl || cl->closing || !device_id)
return FAITH_ERR_INVALID;
Expand Down
4 changes: 2 additions & 2 deletions faithd/src/auth/handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ auth_handle_challenge_response(server_state_t *s, client_conn_t *cl,
const faith_envelope_t *challenge_response_envl);

faith_status_code_t auth_authorize_client(
server_state_t *s, client_conn_t *cl, const faith_client_id_t *auth_id,
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);

faith_status_code_t auth_handshake_complete(server_state_t *s,
client_conn_t *cl,
const faith_client_id_t *sender_id,
const faith_auth_id_t *sender_id,
const faith_device_id_t *device_id);

faith_status_code_t auth_queue_auth_pending(server_state_t *s,
Expand Down
2 changes: 1 addition & 1 deletion faithd/src/auth/structs.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "structs.h"

bool faith_client_id_equal(faith_client_id_t a, faith_client_id_t b) {
bool faith_client_id_equal(faith_auth_id_t a, faith_auth_id_t b) {
return memcmp(a.bytes, b.bytes, 16) == 0;
}

Expand Down
12 changes: 6 additions & 6 deletions faithd/src/auth/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define _FH_FOR_EACH_AUTH_DEVICE(SERVER, AUTH_ID, RECIPIENT, STATUS_OUT, BODY) \
do { \
server_state_t *_fh_iter_server = (SERVER); \
const faith_client_id_t *_fh_iter_auth_id = (AUTH_ID); \
const faith_auth_id_t *_fh_iter_auth_id = (AUTH_ID); \
client_session_device_t *_fh_iter_devices = NULL; \
\
(STATUS_OUT) = sess_registry_get_devices( \
Expand Down Expand Up @@ -38,12 +38,12 @@
} \
} while (0)

#define FAITH_CLIENT_ID_SIZE 16
#define FAITH_AUTH_ID_SIZE 16
#define FAITH_DEVICE_ID_SIZE 16

typedef struct {
uint8_t bytes[FAITH_CLIENT_ID_SIZE];
} faith_client_id_t;
uint8_t bytes[FAITH_AUTH_ID_SIZE];
} faith_auth_id_t;

typedef struct {
uint8_t bytes[FAITH_DEVICE_ID_SIZE];
Expand All @@ -53,9 +53,9 @@ typedef struct {
uint64_t nonce;
uint64_t server_nonce;
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE];
faith_client_id_t sender_auth_id;
faith_auth_id_t sender_auth_id;
faith_device_id_t device_id;
} client_auth_handshake_params_t;

bool faith_client_id_equal(faith_client_id_t a, faith_client_id_t b);
bool faith_client_id_equal(faith_auth_id_t a, faith_auth_id_t b);
bool faith_device_id_equal(faith_device_id_t a, faith_device_id_t b);
88 changes: 82 additions & 6 deletions faithd/src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef struct {
uint8_t public_key[FAITH_ED25519_PUBLIC_KEY_SIZE];
uint8_t private_key[FAITH_ED25519_PRIVATE_KEY_SIZE];

faith_client_id_t auth_id;
faith_auth_id_t auth_id;
faith_device_id_t device_id;
} client_side_identity_t;

Expand Down Expand Up @@ -108,6 +108,10 @@ struct faith_client {
client_request_id_t next_request_id;
};

static faith_status_code_t
client_send_envelope_locked(faith_client_t *client,
const faith_envelope_t *envl);

static faith_status_code_t write_bytes_sync(SSL *ssl, const uint8_t *buf,
size_t size) {
if (!ssl || !buf)
Expand Down Expand Up @@ -440,6 +444,50 @@ static uint32_t client_next_backoff_ms(uint32_t current) {
return current;
}

static faith_status_code_t client_send_command(faith_client_t* client, uint8_t* payload, size_t payload_size, faith_command_type_t type) {
if(!client) return FAITH_ERR_INVALID;

if (payload_size > FAITH_COMMAND_PAYLOAD_SIZE_MAX)
return FAITH_ERR_OVERFLOW;

size_t cmd_data_size = FAITH_ENVL_CTS_COMMAND_BODY_SIZE_FIXED + payload_size;

faith_envelope_t cmd_envl = {0};
cmd_envl.type = FAITH_ENVELOPE_COMMAND;
cmd_envl.sender_id = client->ident.auth_id;

faith_envl_cts_command_t cmd = {0};
cmd.payload = payload;
cmd.payload_size = payload_size;
cmd.type = type;

_FH_CHECK_RETURN(faith_random_bytes((uint8_t *)&cmd.cmd_id.bytes,
sizeof(cmd.cmd_id.bytes)));

uint8_t* body = malloc(cmd_data_size);
if(!body) {
return FAITH_ERR_NOMEM;
}

faith_status_code_t _fh_result = FAITH_OK;

faith_body_size_t body_size = 0;
_FH_CHECK_DEFER(faith_encode_command_body(body, &body_size,
cmd_data_size, &cmd));

cmd_envl.body = body;
cmd_envl.body_size = body_size;

_FH_CHECK_DEFER(client_send_envelope_locked(client, &cmd_envl));

free(body);

return FAITH_OK;
defer:
free(body);
return _fh_result;
}

static SSL_CTX *client_create_ssl_ctx(faith_client_t *client) {
SSL_CTX *ctx = NULL;

Expand Down Expand Up @@ -1488,16 +1536,16 @@ static bool hex_char_to_nibble(char c, uint8_t *out) {
return false;
}

static bool client_id_from_hex(const char *hex, faith_client_id_t *out) {
static bool client_id_from_hex(const char *hex, faith_auth_id_t *out) {
if (!hex || !out)
return false;

if (strlen(hex) != FAITH_CLIENT_ID_SIZE * 2)
if (strlen(hex) != FAITH_AUTH_ID_SIZE * 2)
return false;

faith_client_id_t id = {0};
faith_auth_id_t id = {0};

for (size_t i = 0; i < FAITH_CLIENT_ID_SIZE; ++i) {
for (size_t i = 0; i < FAITH_AUTH_ID_SIZE; ++i) {
uint8_t hi = 0;
uint8_t lo = 0;

Expand Down Expand Up @@ -1720,7 +1768,7 @@ faith_status_code_t faith_client_stop(faith_client_t *client) {
}

faith_status_code_t faith_client_send_msg(faith_client_t *client,
faith_client_id_t recipient_auth_id,
faith_auth_id_t recipient_auth_id,
const char *msg) {
if (!client || !msg)
return FAITH_ERR_INVALID;
Expand Down Expand Up @@ -1937,6 +1985,34 @@ faith_client_deny_pending_device_auth(faith_client_t *client) {
return FAITH_OK;
}

faith_status_code_t
faith_client_create_conversation(faith_client_t *client,
faith_auth_id_t conservant) {

faith_cmd_create_converstation_t cmd = {.conversant_id = conservant};
faith_body_size_t payload_size = 0;
uint8_t payload[FAITH_CMD_CREATE_CONVERSATION_BODY_SIZE] = {0};
_FH_CHECK_RETURN(faith_encode_cmd_create_conversation(payload, &payload_size,
sizeof(payload), &cmd));

if (payload_size != sizeof(payload))
return FAITH_ERR_INVALID;

_FH_CHECK_RETURN(client_send_command(client, payload, payload_size,
FAITH_COMMAND_CREATE_CONVERSATION));

if(g_log_enable_tracing) {
char auth_id_hex[33];
_FH_CHECK_RETURN(faith_id128_to_hex(cmd.conversant_id.bytes, auth_id_hex));
nob_log(INFO,
"[client] Successfully sent command "
"FAITH_COMMAND_CREATE_CONVERSATION (conversant_id: %s)",
auth_id_hex);
}

return FAITH_OK;
}

faith_status_code_t faith_client_reconnect(faith_client_t *client) {
if (!client)
return FAITH_ERR_INVALID;
Expand Down
7 changes: 6 additions & 1 deletion faithd/src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ faith_status_code_t faith_client_start(faith_client_t *client);
faith_status_code_t faith_client_stop(faith_client_t *client);

faith_status_code_t faith_client_send_msg(faith_client_t *client,
faith_client_id_t recipient_auth_id,
faith_auth_id_t recipient_auth_id,
const char *msg);

int faith_client_event_fd(faith_client_t *client);
Expand All @@ -87,6 +87,11 @@ faith_client_approve_pending_device_auth(faith_client_t *client);
faith_status_code_t
faith_client_deny_pending_device_auth(faith_client_t *client);

faith_status_code_t
faith_client_create_conversation(faith_client_t *client,
faith_auth_id_t conservant);

faith_status_code_t faith_client_reconnect(faith_client_t *client);


const char *faith_event_name(faith_event_type_t ev);
34 changes: 34 additions & 0 deletions faithd/src/codec/commands.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "commands.h"

#include "helpers.h"

faith_status_code_t faith_encode_cmd_create_conversation(
uint8_t *out_buf, faith_body_size_t *out_size, size_t buf_cap_in_bytes,
const faith_cmd_create_converstation_t *in) {
FAITH_ENCODE_PROLOGUE(FAITH_CMD_CREATE_CONVERSATION_BODY_SIZE);

size_t offset = 0;

FAITH_APPEND_RETURN(out_buf, buf_cap_in_bytes, offset,
in->conversant_id.bytes, sizeof(in->conversant_id.bytes));

FAITH_ENCODE_EPILOGUE(FAITH_CMD_CREATE_CONVERSATION_BODY_SIZE, !=);

return FAITH_OK;
}

faith_status_code_t
faith_decode_cmd_create_conversation(const uint8_t *payload,
faith_body_size_t payload_size,
faith_cmd_create_converstation_t *out) {
FAITH_DECODE_PROLOGUE(FAITH_CMD_CREATE_CONVERSATION_BODY_SIZE, !=);

size_t offset = 0;

FAITH_DECODE_RETURN(payload, payload_size, offset, out->conversant_id.bytes,
sizeof(out->conversant_id));

FAITH_DECODE_EPILOGUE(FAITH_CMD_CREATE_CONVERSATION_BODY_SIZE, !=);

return FAITH_OK;
}
Loading
Loading