ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
lib/crypto/session.c
Go to the documentation of this file.
2#include <ascii-chat/common.h>
3#include <sodium.h>
4#include <string.h>
5
6static asciichat_error_t emit_message(void *context, unsigned channel, crypto_rekey_message_t type, const void *payload,
7 size_t length) {
8 crypto_session_t *session = context;
9 if (session->count == CRYPTO_SESSION_MESSAGES || length > CRYPTO_PUBLIC_KEY_SIZE)
10 return SET_ERRNO(ERROR_BUFFER_FULL, "Security control queue is full");
11 crypto_session_message_t *message = &session->messages[(session->head + session->count) % CRYPTO_SESSION_MESSAGES];
12 *message = (crypto_session_message_t){.channel = channel, .type = type, .length = length};
13 if (length)
14 memcpy(message->payload, payload, length);
15 ++session->count;
16 return ASCIICHAT_OK;
17}
18
20 if (!crypto || crypto->session)
21 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid security session owner");
22 crypto_session_t *session = SAFE_CALLOC(1, sizeof(*session), crypto_session_t *);
23 if (!session)
24 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate security session");
25 if (mutex_init(&session->mutex, "crypto_session") != 0) {
26 SAFE_FREE(session);
27 return SET_ERRNO(ERROR_THREAD, "Cannot initialize security session mutex");
28 }
29 session->owner = crypto;
30 crypto->session = session;
31 return ASCIICHAT_OK;
32}
33
35 if (!crypto || !crypto->session)
36 return;
37 crypto_session_t *session = crypto->session;
38 crypto->session = NULL;
41 mutex_destroy(&session->mutex);
42 sodium_memzero(session, sizeof(*session));
43 SAFE_FREE(session);
44}
45
47 if (!crypto || !output || !crypto->session || crypto->session->owner != crypto)
48 return SET_ERRNO(ERROR_INVALID_STATE, "Missing security session owner");
49 crypto_session_t *session = crypto->session;
50 mutex_lock(&session->mutex);
51 if (!session->failure && !session->records.crypto) {
52 bool server = crypto->server_role_known
53 ? crypto->server_role
54 : memcmp(crypto->public_key, crypto->peer_public_key, CRYPTO_PUBLIC_KEY_SIZE) > 0;
55 session->failure = crypto_record_init(&session->records, crypto, server);
56 if (!session->failure)
57 session->failure =
58 crypto_rekey_session_init(&session->rotation, &session->records, emit_message, session, time_get_ns());
59 }
60 if (session->failure) {
61 asciichat_error_t result = session->failure;
62 mutex_unlock(&session->mutex);
63 return result;
64 }
65 *output = session;
66 return ASCIICHAT_OK;
67}
68
70 mutex_unlock(&session->mutex);
71}
72
74 if (!session->failure)
75 session->failure = crypto_rekey_session_tick(&session->rotation, now);
76 return session->failure;
77}
78
79asciichat_error_t crypto_session_receive(crypto_session_t *session, unsigned channel, unsigned delivery,
80 crypto_rekey_message_t type, const void *payload, size_t length, uint64_t now,
81 bool *consumed) {
82 if (!session->failure)
83 session->failure =
84 crypto_rekey_session_receive(&session->rotation, channel, delivery, type, payload, length, now, consumed);
85 return session->failure;
86}
#define SET_ERRNO(code,...)
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
unsigned long long uint64_t
Definition common.h:63
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ERROR_INVALID_STATE
@ ERROR_BUFFER_FULL
@ ERROR_MEMORY
Definition error_codes.h:51
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_THREAD
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:23
void crypto_record_cleanup(crypto_record_t *state)
Definition record.c:248
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:127
asciichat_error_t crypto_rekey_session_tick(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:72
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:53
void crypto_rekey_session_cleanup(crypto_rekey_session_t *s)
Definition rekey.c:203
crypto_rekey_message_t
Definition rekey.h:6
#define CRYPTO_PUBLIC_KEY_SIZE
Public key size (X25519).
Definition sizes.h:67
🔤 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:18
uint8_t payload[CRYPTO_PUBLIC_KEY_SIZE]
crypto_session_message_t messages[16]
crypto_rekey_session_t rotation