ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/crypto/session.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
3#include <sodium.h>
4#include <string.h>
5
6static asciichat_error_t emit_message(void *context, unsigned channel,
8 const void *payload, size_t length) {
9 crypto_session_t *session = context;
10 if (session->count == CRYPTO_SESSION_MESSAGES ||
12 return SET_ERRNO(ERROR_BUFFER_FULL, "Security control queue is full");
14 &session->messages[(session->head + session->count) %
16 *message = (crypto_session_message_t){
17 .channel = channel, .type = type, .length = length};
18 if (length)
19 memcpy(message->payload, payload, length);
20 ++session->count;
21 return ASCIICHAT_OK;
22}
23
25 if (!crypto || crypto->session)
26 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid security session owner");
27 crypto_session_t *session =
28 SAFE_CALLOC(1, sizeof(*session), crypto_session_t *);
29 if (!session)
30 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate security session");
31 if (mutex_init(&session->mutex, "crypto_session") != 0) {
32 SAFE_FREE(session);
33 return SET_ERRNO(ERROR_THREAD, "Cannot initialize security session mutex");
34 }
35 session->owner = crypto;
36 crypto->session = session;
37 return ASCIICHAT_OK;
38}
39
41 if (!crypto || !crypto->session)
42 return;
43 crypto_session_t *session = crypto->session;
44 crypto->session = NULL;
47 mutex_destroy(&session->mutex);
48 sodium_memzero(session, sizeof(*session));
49 SAFE_FREE(session);
50}
51
53 crypto_session_t **output) {
54 if (!crypto || !output || !crypto->session ||
55 crypto->session->owner != crypto)
56 return SET_ERRNO(ERROR_INVALID_STATE, "Missing security session owner");
57 crypto_session_t *session = crypto->session;
58 mutex_lock(&session->mutex);
59 if (!session->failure && !session->records.crypto) {
60 bool server = crypto->server_role_known
61 ? crypto->server_role
62 : memcmp(crypto->public_key, crypto->peer_public_key,
64 session->failure = crypto_record_init(&session->records, crypto, server);
65 if (!session->failure)
66 session->failure =
67 crypto_rekey_session_init(&session->rotation, &session->records,
68 emit_message, session, time_get_ns());
69 }
70 if (session->failure) {
71 asciichat_error_t result = session->failure;
72 mutex_unlock(&session->mutex);
73 return result;
74 }
75 *output = session;
76 return ASCIICHAT_OK;
77}
78
80 mutex_unlock(&session->mutex);
81}
82
84 if (!session->failure)
85 session->failure = crypto_rekey_session_tick(&session->rotation, now);
86 return session->failure;
87}
88
90 unsigned channel, unsigned delivery,
92 const void *payload, size_t length,
93 uint64_t now, bool *consumed) {
94 if (!session->failure)
95 session->failure =
96 crypto_rekey_session_receive(&session->rotation, channel, delivery,
97 type, payload, length, now, consumed);
98 return session->failure;
99}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_BUFFER_FULL
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_THREAD
⚙️ Common definitions, error codes, macros, and types shared throughout the application
struct crypto_session crypto_session_t
For crypto session, this is the live record that ties owner, mutex, records, rotation together....
#define CRYPTO_SESSION_MESSAGES
asciichat_error_t crypto_session_tick(crypto_session_t *session, uint64_t now)
asciichat_error_t crypto_session_receive(crypto_session_t *session, unsigned channel, unsigned delivery, crypto_rekey_message_t type, const void *payload, size_t length, uint64_t now, bool *consumed)
void crypto_session_unlock(crypto_session_t *session)
asciichat_error_t crypto_session_create(crypto_context_t *crypto)
asciichat_error_t crypto_session_lock(crypto_context_t *crypto, crypto_session_t **output)
void crypto_session_destroy(crypto_context_t *crypto)
asciichat_error_t crypto_record_init(crypto_record_t *state, crypto_context_t *crypto, bool server)
Definition record.c:26
void crypto_record_cleanup(crypto_record_t *state)
Definition record.c:293
asciichat_error_t crypto_rekey_session_receive(crypto_rekey_session_t *s, unsigned flow, unsigned delivery, crypto_rekey_message_t type, const void *payload, size_t bytes, uint64_t now, bool *consumed)
Definition rekey.c:150
asciichat_error_t crypto_rekey_session_tick(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:87
asciichat_error_t crypto_rekey_session_init(crypto_rekey_session_t *s, crypto_record_t *security, crypto_rekey_emit_fn send, void *context, uint64_t now)
Definition rekey.c:63
void crypto_rekey_session_cleanup(crypto_rekey_session_t *s)
Definition rekey.c:244
crypto_rekey_message_t
Definition rekey.h:10
#define CRYPTO_PUBLIC_KEY_SIZE
Public key size (X25519).
Definition sizes.h:72
🔤 String Manipulation and Shell Escaping Utilities
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
struct crypto_session * session
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
crypto_context_t * crypto
Definition record.h:27
uint8_t payload[CRYPTO_PUBLIC_KEY_SIZE]
crypto_session_message_t messages[16]
crypto_rekey_session_t rotation