ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
session.c File Reference

Go to the source code of this file.

Functions

asciichat_error_t crypto_session_create (crypto_context_t *crypto)
void crypto_session_destroy (crypto_context_t *crypto)
asciichat_error_t crypto_session_lock (crypto_context_t *crypto, crypto_session_t **output)
void crypto_session_unlock (crypto_session_t *session)
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)

Function Documentation

◆ crypto_session_create()

asciichat_error_t crypto_session_create ( crypto_context_t * crypto)

Definition at line 24 of file lib/crypto/session.c.

24 {
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}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_THREAD
struct crypto_session crypto_session_t
For crypto session, this is the live record that ties owner, mutex, records, rotation together....
struct crypto_session * session
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.

References ASCIICHAT_OK, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_THREAD, crypto_session::mutex, crypto_session::owner, SAFE_CALLOC, SAFE_FREE, crypto_context_t::session, and SET_ERRNO.

Referenced by crypto_init().

◆ crypto_session_destroy()

void crypto_session_destroy ( crypto_context_t * crypto)

Definition at line 40 of file lib/crypto/session.c.

40 {
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}
void crypto_record_cleanup(crypto_record_t *state)
Definition record.c:293
void crypto_rekey_session_cleanup(crypto_rekey_session_t *s)
Definition rekey.c:244
crypto_rekey_session_t rotation

References crypto_record_cleanup(), crypto_rekey_session_cleanup(), crypto_session::mutex, crypto_session::records, crypto_session::rotation, SAFE_FREE, and crypto_context_t::session.

Referenced by crypto_destroy().

◆ crypto_session_lock()

asciichat_error_t crypto_session_lock ( crypto_context_t * crypto,
crypto_session_t ** output )

Lock and activate the common authenticated-record session. The caller must unlock on every successful return. No transport callbacks run under this lock.

Definition at line 52 of file lib/crypto/session.c.

53 {
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}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
asciichat_error_t crypto_record_init(crypto_record_t *state, crypto_context_t *crypto, bool server)
Definition record.c:26
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
#define CRYPTO_PUBLIC_KEY_SIZE
Public key size (X25519).
Definition sizes.h:72
crypto_context_t * crypto
Definition record.h:27

References ASCIICHAT_OK, crypto_record::crypto, CRYPTO_PUBLIC_KEY_SIZE, crypto_record_init(), crypto_rekey_session_init(), ERROR_INVALID_STATE, crypto_session::failure, crypto_session::mutex, crypto_session::owner, crypto_context_t::peer_public_key, crypto_context_t::public_key, crypto_session::records, crypto_session::rotation, crypto_context_t::server_role, crypto_context_t::server_role_known, crypto_context_t::session, and SET_ERRNO.

Referenced by acip_packet_encrypt(), acip_packet_plan(), acip_security_activate(), acip_security_decrypt(), acip_security_next_control(), acip_security_request_rekey(), acip_security_seal(), browser_transport_security_epoch(), browser_transport_security_init(), browser_transport_security_tick(), and quic_connection_enable_security().

◆ crypto_session_receive()

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 )

Definition at line 89 of file lib/crypto/session.c.

93 {
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}
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

References crypto_rekey_session_receive(), crypto_session::failure, and crypto_session::rotation.

Referenced by acip_security_decrypt().

◆ crypto_session_tick()

asciichat_error_t crypto_session_tick ( crypto_session_t * session,
uint64_t now )

Definition at line 83 of file lib/crypto/session.c.

83 {
84 if (!session->failure)
85 session->failure = crypto_rekey_session_tick(&session->rotation, now);
86 return session->failure;
87}
asciichat_error_t crypto_rekey_session_tick(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:87

References crypto_rekey_session_tick(), crypto_session::failure, and crypto_session::rotation.

Referenced by acip_security_next_control(), and browser_transport_security_tick().

◆ crypto_session_unlock()