ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
session.h File Reference

Go to the source code of this file.

Data Structures

struct  crypto_session_message_t
struct  crypto_session
 For crypto session, this is the live record that ties owner, mutex, records, rotation together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. More...

Macros

#define CRYPTO_SESSION_MESSAGES   16

Typedefs

typedef struct crypto_session crypto_session_t
 For crypto session, this is the live record that ties owner, mutex, records, rotation together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record.

Functions

asciichat_error_t crypto_session_create (crypto_context_t *)
void crypto_session_destroy (crypto_context_t *)
asciichat_error_t crypto_session_lock (crypto_context_t *, crypto_session_t **)
void crypto_session_unlock (crypto_session_t *)
asciichat_error_t crypto_session_tick (crypto_session_t *, uint64_t now_ns)
asciichat_error_t crypto_session_receive (crypto_session_t *, unsigned, unsigned, crypto_rekey_message_t, const void *, size_t, uint64_t now_ns, bool *consumed)

Macro Definition Documentation

◆ CRYPTO_SESSION_MESSAGES

#define CRYPTO_SESSION_MESSAGES   16

Typedef Documentation

◆ crypto_session_t

For crypto session, this is the live record that ties owner, mutex, records, rotation together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record.

Function Documentation

◆ crypto_session_create()

asciichat_error_t crypto_session_create ( crypto_context_t * crypto)

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

19 {
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}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
@ ERROR_INVALID_STATE
@ 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....
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 34 of file lib/crypto/session.c.

34 {
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}
void crypto_record_cleanup(crypto_record_t *state)
Definition record.c:248
void crypto_rekey_session_cleanup(crypto_rekey_session_t *s)
Definition rekey.c:203
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 46 of file lib/crypto/session.c.

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

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_security_epoch(), browser_security_init(), browser_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_ns,
bool * consumed )

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

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

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_ns )

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

73 {
74 if (!session->failure)
75 session->failure = crypto_rekey_session_tick(&session->rotation, now);
76 return session->failure;
77}
asciichat_error_t crypto_rekey_session_tick(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:72

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

Referenced by acip_security_next_control(), and browser_security_tick().

◆ crypto_session_unlock()