ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
security.c File Reference

Go to the source code of this file.

Functions

void acip_security_cleanup (acip_security_t *security)
asciichat_error_t acip_security_start (acip_security_t *security, acip_transport_t *transport)
asciichat_error_t acip_security_receive (acip_security_t *security, acip_transport_t *transport, packet_type_t type, const void *payload, size_t length)
bool acip_security_rekey_packet (uint16_t type)
asciichat_error_t acip_security_rekey_encode (unsigned flow, crypto_rekey_message_t type, const void *payload, size_t length, void *packet, size_t capacity, size_t *written)
asciichat_error_t acip_security_rekey_receive (crypto_rekey_session_t *session, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint64_t now, bool *consumed)
asciichat_error_t acip_security_rekey_seal (crypto_record_t *records, unsigned flow, crypto_rekey_message_t message, const void *payload, size_t length, uint8_t **wire, size_t *written)
asciichat_error_t acip_security_activate (acip_transport_t *transport)
asciichat_error_t acip_security_decrypt (crypto_context_t *crypto, unsigned flow, unsigned delivery, const void *payload, size_t length, uint8_t **output, size_t *written, uint64_t *sequence, bool *consumed)
asciichat_error_t acip_security_next_control (crypto_context_t *crypto, acip_traffic_class_t *flow, uint8_t **wire, size_t *length)
asciichat_error_t acip_security_flush (acip_transport_t *transport)
asciichat_error_t acip_security_request_rekey (acip_transport_t *transport)
asciichat_error_t acip_security_seal (crypto_context_t *crypto, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written)
asciichat_error_t acip_security_open (crypto_context_t *crypto, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written, uint64_t *sequence, bool *consumed)

Function Documentation

◆ acip_security_activate()

asciichat_error_t acip_security_activate ( acip_transport_t * transport)

Definition at line 166 of file network/acip/security.c.

166 {
167 if (!transport)
168 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing secure transport");
169 crypto_context_t *crypto = transport->crypto_ctx;
170 if (!crypto || !crypto->encrypt_data)
171 return ASCIICHAT_OK;
172 crypto_session_t *session = NULL;
173 asciichat_error_t result = crypto_session_lock(crypto, &session);
174 if (result == ASCIICHAT_OK) {
175 crypto_session_unlock(session);
176 atomic_store_bool(&transport->packet_security_active, true);
177 }
178 return result;
179}
#define SET_ERRNO(code,...)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
struct crypto_session crypto_session_t
For crypto session, this is the live record that ties owner, mutex, records, rotation together....
void crypto_session_unlock(crypto_session_t *session)
asciichat_error_t crypto_session_lock(crypto_context_t *crypto, crypto_session_t **output)
atomic_t packet_security_active
Publishes the stable post-handshake crypto owner.
crypto_context_t * crypto_ctx
Optional encryption context.
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.

References ASCIICHAT_OK, atomic_store_bool(), acip_transport::crypto_ctx, crypto_session_lock(), crypto_session_unlock(), crypto_context_t::encrypt_data, ERROR_INVALID_PARAM, acip_transport::packet_security_active, and SET_ERRNO.

Referenced by acip_security_receive(), and quic_transport_begin_security().

◆ acip_security_cleanup()

void acip_security_cleanup ( acip_security_t * security)

Definition at line 11 of file network/acip/security.c.

11 {
12 if (!security)
13 return;
16}
void crypto_handshake_destroy(crypto_handshake_context_t *ctx)
Cleanup crypto handshake context with secure memory wiping.
#define SAFE_FREE(ptr)
crypto_handshake_context_t handshake
Definition security.h:19

References crypto_handshake_context_t::client_whitelist, crypto_handshake_destroy(), acip_security::handshake, and SAFE_FREE.

Referenced by acip_security_init_options(), and browser_session_destroy().

◆ acip_security_decrypt()

asciichat_error_t acip_security_decrypt ( crypto_context_t * crypto,
unsigned flow,
unsigned delivery,
const void * payload,
size_t length,
uint8_t ** output,
size_t * written,
uint64_t * sequence,
bool * consumed )

Definition at line 181 of file network/acip/security.c.

182 {
183 if (!output || !written || !consumed)
184 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing security receive output");
185 *output = NULL;
186 *written = 0;
187 *consumed = false;
188 crypto_session_t *session = NULL;
189 asciichat_error_t result = crypto_session_lock(crypto, &session);
190 if (result != ASCIICHAT_OK)
191 return result;
192 result = acip_packet_decrypt_payload(&session->records, flow, delivery, payload, length, output, written, sequence);
193 if (result == ASCIICHAT_OK) {
194 packet_header_t header;
195 memcpy(&header, *output, sizeof(header));
196 result = crypto_session_receive(session, session->records.last_receive_channel, delivery, rekey_message(NET_TO_HOST_U16(header.type)), *output + sizeof(header), *written - sizeof(header),
197 time_get_ns(), consumed);
198 }
199 crypto_session_unlock(session);
200 if (result != ASCIICHAT_OK) {
201 buffer_release(*output);
202 *output = NULL;
203 *written = 0;
204 }
205 return result;
206}
#define buffer_release(data)
Definition buffer.h:99
#define NET_TO_HOST_U16(val)
Definition endian.h:115
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)
asciichat_error_t acip_packet_decrypt_payload(crypto_record_t *records, unsigned flow, unsigned delivery, const void *payload, size_t length, uint8_t **output, size_t *written, uint64_t *sequence)
Definition prepare.c:164
unsigned last_receive_channel
Definition record.h:32
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641

References acip_packet_decrypt_payload(), ASCIICHAT_OK, buffer_release, crypto_session_lock(), crypto_session_receive(), crypto_session_unlock(), ERROR_INVALID_PARAM, crypto_record::last_receive_channel, NET_TO_HOST_U16, crypto_session::records, and SET_ERRNO.

Referenced by acip_security_open(), browser_client_decrypt_packet(), client_decrypt_packet(), and packet_decrypt_envelope().

◆ acip_security_flush()

asciichat_error_t acip_security_flush ( acip_transport_t * transport)

Definition at line 232 of file network/acip/security.c.

232 {
233 if (!transport || !atomic_load_bool(&transport->packet_security_active))
234 return ASCIICHAT_OK;
235 for (unsigned i = 0; i < CRYPTO_SESSION_MESSAGES; ++i) {
236 uint8_t *wire = NULL;
237 size_t length = 0;
239 asciichat_error_t result = acip_security_next_control(transport->crypto_ctx, &channel, &wire, &length);
240 if (result != ASCIICHAT_OK || !wire)
241 return result;
242 defer(buffer_release(wire));
243 socket_buffer_t slice = {wire, length};
244 acip_send_options_t options = {.traffic_class = channel, .delivery = ACIP_DELIVERY_RELIABLE};
245 if (transport->methods->sendv_ex)
246 result = transport->methods->sendv_ex(transport, &slice, 1, &options);
247 else if (transport->methods->sendv)
248 result = transport->methods->sendv(transport, &slice, 1);
249 else
250 result = transport->methods->send(transport, wire, length);
251 if (result != ASCIICHAT_OK)
252 return result;
253 }
254 return ASCIICHAT_OK;
255}
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
unsigned char uint8_t
#define CRYPTO_SESSION_MESSAGES
asciichat_error_t acip_security_next_control(crypto_context_t *crypto, acip_traffic_class_t *flow, uint8_t **wire, size_t *length)
asciichat_error_t(* send)(acip_transport_t *transport, const void *data, size_t len)
Send data through this transport.
asciichat_error_t(* sendv)(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count)
Custom destroy implementation (optional).
asciichat_error_t(* sendv_ex)(acip_transport_t *transport, const socket_buffer_t *buffers, size_t count, const acip_send_options_t *options)
const acip_transport_methods_t * methods
Method table (virtual functions).
acip_traffic_class_t
Definition traffic.h:9
@ ACIP_TRAFFIC_CONTROL
Definition traffic.h:10
@ ACIP_DELIVERY_RELIABLE
Definition traffic.h:16

References ACIP_DELIVERY_RELIABLE, acip_security_next_control(), ACIP_TRAFFIC_CONTROL, ASCIICHAT_OK, atomic_load_bool(), buffer_release, acip_transport::crypto_ctx, CRYPTO_SESSION_MESSAGES, acip_transport::methods, acip_transport::packet_security_active, acip_transport_methods_t::send, acip_transport_methods_t::sendv, and acip_transport_methods_t::sendv_ex.

Referenced by browser_session_tick().

◆ acip_security_next_control()

asciichat_error_t acip_security_next_control ( crypto_context_t * crypto,
acip_traffic_class_t * flow,
uint8_t ** wire,
size_t * length )

Definition at line 208 of file network/acip/security.c.

208 {
209 if (!flow || !wire || !length)
210 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing security output");
211 *wire = NULL;
212 *length = 0;
213 crypto_session_t *session = NULL;
214 asciichat_error_t result = crypto_session_lock(crypto, &session);
215 if (result != ASCIICHAT_OK)
216 return result;
217 defer(crypto_session_unlock(session));
218 result = crypto_session_tick(session, time_get_ns());
219 if (result == ASCIICHAT_OK && session->count) {
220 crypto_session_message_t *message = &session->messages[session->head];
221 *flow = (acip_traffic_class_t)message->channel;
222 result = acip_security_rekey_seal(&session->records, message->channel, message->type, message->payload, message->length, wire, length);
223 if (result == ASCIICHAT_OK) {
224 memset(message, 0, sizeof(*message));
225 session->head = (session->head + 1) % CRYPTO_SESSION_MESSAGES;
226 --session->count;
227 }
228 }
229 return result;
230}
asciichat_error_t crypto_session_tick(crypto_session_t *session, uint64_t now)
asciichat_error_t acip_security_rekey_seal(crypto_record_t *records, unsigned flow, crypto_rekey_message_t message, const void *payload, size_t length, uint8_t **wire, size_t *written)
crypto_session_message_t messages[16]

References acip_security_rekey_seal(), ASCIICHAT_OK, crypto_session_message_t::channel, crypto_session::count, crypto_session_lock(), CRYPTO_SESSION_MESSAGES, crypto_session_tick(), crypto_session_unlock(), ERROR_INVALID_PARAM, crypto_session::head, crypto_session_message_t::length, crypto_session::messages, crypto_session_message_t::payload, crypto_session::records, SET_ERRNO, and crypto_session_message_t::type.

Referenced by acip_security_flush(), and browser_transport_security_output().

◆ acip_security_open()

asciichat_error_t acip_security_open ( crypto_context_t * crypto,
acip_traffic_class_t flow,
acip_delivery_t delivery,
const void * packet,
size_t length,
uint8_t ** output,
size_t * written,
uint64_t * sequence,
bool * consumed )

Definition at line 279 of file network/acip/security.c.

280 {
281 packet_header_t header;
282 asciichat_error_t result = packet_validate(packet, length, 0, &header);
283 if (result != ASCIICHAT_OK)
284 return result;
285 if (NET_TO_HOST_U16(header.type) != PACKET_TYPE_ENCRYPTED)
286 return SET_ERRNO(ERROR_CRYPTO, "Expected authenticated session record");
287 return acip_security_decrypt(crypto, flow, delivery, (const uint8_t *)packet + sizeof(header), length - sizeof(header), output, written, sequence, consumed);
288}
@ ERROR_CRYPTO
Definition error_codes.h:97
asciichat_error_t packet_validate(const void *packet, size_t length, size_t limit, packet_header_t *output)
Definition packet.c:81
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion).
Definition packet.h:369
asciichat_error_t acip_security_decrypt(crypto_context_t *crypto, unsigned flow, unsigned delivery, const void *payload, size_t length, uint8_t **output, size_t *written, uint64_t *sequence, bool *consumed)

References acip_security_decrypt(), ASCIICHAT_OK, ERROR_CRYPTO, NET_TO_HOST_U16, PACKET_TYPE_ENCRYPTED, packet_validate(), and SET_ERRNO.

Referenced by browser_session_receive(), and browser_transport_security_packet().

◆ acip_security_receive()

asciichat_error_t acip_security_receive ( acip_security_t * security,
acip_transport_t * transport,
packet_type_t type,
const void * payload,
size_t length )

Definition at line 31 of file network/acip/security.c.

31 {
32 if (!security || !transport || (!payload && length) || security->ready)
33 return SET_ERRNO(ERROR_INVALID_STATE, "Unexpected peer security packet");
34 crypto_handshake_context_t *ctx = &security->handshake;
36 if (!security->version_received) {
37 if (type != PACKET_TYPE_PROTOCOL_VERSION || length != sizeof(protocol_version_packet_t))
38 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Expected peer security negotiation before media");
40 memcpy(&version, payload, sizeof(version));
41 uint8_t mode = version.supports_encryption;
44 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Unsupported peer security protocol");
45 if (ctx->is_server) {
46 // Match secure WebSocket policy: defaults do not override a client's explicit no-encrypt.
47 if ((ctx->has_password || ctx->require_client_auth) && !ACIP_CRYPTO_HAS_AUTH(mode))
48 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Peer omitted required authentication");
49 if (security->require_encryption && !ACIP_CRYPTO_HAS_ENCRYPT(mode))
50 return SET_ERRNO(ERROR_CRYPTO, "Peer omitted required encryption");
51 if (security->forbid_encryption && ACIP_CRYPTO_HAS_ENCRYPT(mode))
52 return SET_ERRNO(ERROR_CRYPTO, "Peer requires encryption but local policy disables it");
53 version.supports_encryption = mode;
54 result = packet_send_via_transport(transport, PACKET_TYPE_PROTOCOL_VERSION, &version, sizeof(version), 0);
55 if (result != ASCIICHAT_OK)
56 return result;
57 } else if ((mode & security->mode) != security->mode) {
58 return SET_ERRNO(ERROR_CRYPTO, "Peer attempted to downgrade requested security");
59 }
60 if (security->forbid_encryption && ACIP_CRYPTO_HAS_ENCRYPT(mode))
61 return SET_ERRNO(ERROR_CRYPTO, "Peer requires encryption but local policy disables it");
62 security->mode = mode;
63 security->version_received = true;
65 if (mode == ACIP_CRYPTO_NONE) {
67 security->ready = true;
68 return ASCIICHAT_OK;
69 }
70 if (!ctx->is_server)
71 return ASCIICHAT_OK;
72 result = crypto_handshake_server_send_parameters(ctx, transport);
73 if (result == ASCIICHAT_OK)
74 result = crypto_handshake_server_start(ctx, transport);
75 return result;
76 }
77 if (ctx->is_server) {
79 result = crypto_handshake_server_auth_challenge(ctx, transport, type, payload, length);
81 result = crypto_handshake_server_complete(ctx, transport, type, payload, length);
82 else
83 result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Unexpected peer authentication state");
84 } else {
85 switch (type) {
87 if (security->parameters_received || ctx->state != CRYPTO_HANDSHAKE_INIT || length != sizeof(crypto_parameters_packet_t))
88 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid peer crypto parameters");
89 result = crypto_handshake_set_parameters(ctx, payload);
90 if (result == ASCIICHAT_OK && ctx->crypto_ctx.encrypt_data != !!ACIP_CRYPTO_HAS_ENCRYPT(security->mode))
91 return SET_ERRNO(ERROR_CRYPTO, "Peer cipher does not match negotiated policy");
92 security->parameters_received = result == ASCIICHAT_OK;
93 break;
95 if (!security->parameters_received)
96 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Missing peer crypto parameters");
97 if (ctx->verify_server_key && length == ctx->crypto_ctx.public_key_size)
98 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Peer omitted the required identity key");
99 result = crypto_handshake_client_key_exchange(ctx, transport, type, payload, length);
100 break;
102 result = crypto_handshake_client_auth_response(ctx, transport, type, payload, length);
103 break;
106 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Peer skipped required mutual authentication");
107 result = crypto_handshake_client_complete(ctx, transport, type, payload, length);
108 break;
111 result = crypto_handshake_client_complete(ctx, transport, type, payload, length);
112 break;
113 default:
114 result = SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Application packet before peer authentication");
115 }
116 }
117 if (result == ASCIICHAT_OK)
118 security->ready = ctx->state == CRYPTO_HANDSHAKE_READY;
119 else
121 if (result == ASCIICHAT_OK && security->ready)
122 result = acip_security_activate(transport);
123 return result;
124}
asciichat_error_t crypto_handshake_set_parameters(crypto_handshake_context_t *ctx, const crypto_parameters_packet_t *params)
Set crypto parameters from crypto_parameters_packet_t.
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ERROR_CRYPTO_AUTH
Definition error_codes.h:99
key_type_t type
Definition key_types.h:102
@ KEY_TYPE_ED25519
Definition key_types.h:58
#define ACIP_CRYPTO_NONE
No encryption, no authentication (plaintext only).
Definition packet.h:1157
#define ACIP_CRYPTO_HAS_AUTH(mode)
Check if crypto mode includes authentication.
Definition packet.h:1174
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1167
#define ACIP_CRYPTO_FULL
Full crypto: encryption + authentication.
Definition packet.h:1160
@ PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE
Server -> Client: "encryption ready" (UNENCRYPTED).
Definition packet.h:352
@ PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT
Server -> Client: {server_pubkey[32]} (UNENCRYPTED).
Definition packet.h:340
@ PACKET_TYPE_CRYPTO_AUTH_FAILED
Server -> Client: "authentication failed" (UNENCRYPTED).
Definition packet.h:348
@ PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP
Server -> Client: {HMAC[32]} server proves knowledge (UNENCRYPTED).
Definition packet.h:350
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
Definition packet.h:317
@ PACKET_TYPE_CRYPTO_AUTH_CHALLENGE
Server -> Client: {nonce[32]} (UNENCRYPTED).
Definition packet.h:344
@ PACKET_TYPE_CRYPTO_PARAMETERS
Server -> Client: Chosen algorithms + data sizes (UNENCRYPTED).
Definition packet.h:338
#define PROTOCOL_VERSION_MAJOR
Major protocol version number.
#define PROTOCOL_VERSION_MINOR
Minor protocol version number.
#define PROTOCOL_VERSION_PATCH
Patch protocol version number.
asciichat_error_t crypto_handshake_client_complete(crypto_handshake_context_t *ctx, acip_transport_t *transport, packet_type_t packet_type, const uint8_t *payload, size_t payload_len)
Client: Wait for handshake complete confirmation.
asciichat_error_t crypto_handshake_client_key_exchange(crypto_handshake_context_t *ctx, acip_transport_t *transport, packet_type_t packet_type, const uint8_t *payload, size_t payload_len)
Client: Process server's public key and send our public key.
asciichat_error_t crypto_handshake_client_auth_response(crypto_handshake_context_t *ctx, acip_transport_t *transport, packet_type_t packet_type, const uint8_t *payload, size_t payload_len)
Client: Process auth challenge and send response.
asciichat_error_t crypto_handshake_server_send_parameters(crypto_handshake_context_t *ctx, acip_transport_t *transport)
Server: Send CRYPTO_PARAMETERS and set handshake context sizes.
asciichat_error_t crypto_handshake_server_complete(crypto_handshake_context_t *ctx, acip_transport_t *transport, packet_type_t packet_type, const uint8_t *payload, size_t payload_len)
Server: Process auth response and complete handshake.
asciichat_error_t crypto_handshake_server_auth_challenge(crypto_handshake_context_t *ctx, acip_transport_t *transport, packet_type_t packet_type, const uint8_t *payload, size_t payload_len)
Server: Process client's public key and send auth challenge.
asciichat_error_t crypto_handshake_server_start(crypto_handshake_context_t *ctx, acip_transport_t *transport)
Server: Start crypto handshake by sending public key.
asciichat_error_t acip_security_activate(acip_transport_t *transport)
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183
bool parameters_received
Definition security.h:24
bool require_encryption
Definition security.h:22
uint8_t mode
Definition security.h:20
bool forbid_encryption
Definition security.h:21
bool version_received
Definition security.h:23
Cryptographic handshake context structure.
Crypto parameters packet structure (Packet Type 15).
Definition packet.h:1056
Protocol version negotiation packet structure (Packet Type 1).
Definition packet.h:871
uint16_t protocol_patch
Patch protocol revision.
Definition packet.h:877
uint16_t protocol_revision
Minor protocol revision (server can be newer).
Definition packet.h:875
uint16_t protocol_version
Major protocol version (must match for compatibility).
Definition packet.h:873
uint8_t supports_encryption
Encryption & authentication mode bitmask (ACIP_CRYPTO_* constants: NONE, ENCRYPT, AUTH,...
Definition packet.h:879

References ACIP_CRYPTO_FULL, ACIP_CRYPTO_HAS_AUTH, ACIP_CRYPTO_HAS_ENCRYPT, ACIP_CRYPTO_NONE, acip_security_activate(), ASCIICHAT_OK, crypto_handshake_context_t::client_private_key, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_AUTHENTICATING, crypto_handshake_client_auth_response(), crypto_handshake_client_complete(), crypto_handshake_client_key_exchange(), CRYPTO_HANDSHAKE_DISABLED, CRYPTO_HANDSHAKE_FAILED, CRYPTO_HANDSHAKE_INIT, CRYPTO_HANDSHAKE_KEY_EXCHANGE, CRYPTO_HANDSHAKE_READY, crypto_handshake_server_auth_challenge(), crypto_handshake_server_complete(), crypto_handshake_server_send_parameters(), crypto_handshake_server_start(), crypto_handshake_set_parameters(), crypto_context_t::encrypt_data, ERROR_CRYPTO, ERROR_CRYPTO_AUTH, ERROR_INVALID_STATE, ERROR_NETWORK_PROTOCOL, acip_security::forbid_encryption, acip_security::handshake, crypto_handshake_context_t::has_password, crypto_handshake_context_t::is_server, KEY_TYPE_ED25519, acip_security::mode, NET_TO_HOST_U16, packet_send_via_transport(), PACKET_TYPE_CRYPTO_AUTH_CHALLENGE, PACKET_TYPE_CRYPTO_AUTH_FAILED, PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE, PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT, PACKET_TYPE_CRYPTO_PARAMETERS, PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP, PACKET_TYPE_PROTOCOL_VERSION, acip_security::parameters_received, protocol_version_packet_t::protocol_patch, protocol_version_packet_t::protocol_revision, protocol_version_packet_t::protocol_version, PROTOCOL_VERSION_MAJOR, PROTOCOL_VERSION_MINOR, PROTOCOL_VERSION_PATCH, crypto_context_t::public_key_size, acip_security::ready, crypto_handshake_context_t::require_client_auth, acip_security::require_encryption, SET_ERRNO, crypto_handshake_context_t::state, protocol_version_packet_t::supports_encryption, private_key_t::type, crypto_handshake_context_t::verify_server_key, and acip_security::version_received.

Referenced by browser_session_receive().

◆ acip_security_rekey_encode()

asciichat_error_t acip_security_rekey_encode ( unsigned flow,
crypto_rekey_message_t type,
const void * payload,
size_t length,
void * packet,
size_t capacity,
size_t * written )

Definition at line 145 of file network/acip/security.c.

145 {
146 if (type <= CRYPTO_REKEY_NONE || type > CRYPTO_REKEY_NEEDED || flow >= CRYPTO_RECORD_CHANNELS)
147 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rekey message");
148 return packet_encode(rekey_types[type], payload, length, flow, packet, capacity, written);
149}
asciichat_error_t packet_encode(packet_type_t type, const void *payload, size_t length, uint32_t client_id, void *output, size_t capacity, size_t *written)
Definition packet.c:63
#define CRYPTO_RECORD_CHANNELS
Definition record.h:11
@ CRYPTO_REKEY_NEEDED
Definition rekey.h:17

References CRYPTO_RECORD_CHANNELS, CRYPTO_REKEY_NEEDED, ERROR_INVALID_PARAM, packet_encode(), and SET_ERRNO.

Referenced by acip_security_rekey_seal().

◆ acip_security_rekey_packet()

bool acip_security_rekey_packet ( uint16_t type)

Definition at line 141 of file network/acip/security.c.

141 {
142 return rekey_message(type) != CRYPTO_REKEY_NONE;
143}
@ CRYPTO_REKEY_NONE
Definition rekey.h:11

References CRYPTO_REKEY_NONE.

Referenced by quic_connection_submit().

◆ acip_security_rekey_receive()

asciichat_error_t acip_security_rekey_receive ( crypto_rekey_session_t * session,
acip_traffic_class_t flow,
acip_delivery_t delivery,
const void * packet,
size_t length,
uint64_t now,
bool * consumed )

Definition at line 151 of file network/acip/security.c.

151 {
152 packet_header_t header;
153 asciichat_error_t result = packet_validate(packet, length, 0, &header);
154 if (result != ASCIICHAT_OK)
155 return result;
156 return crypto_rekey_session_receive(session, flow, delivery, rekey_message(NET_TO_HOST_U16(header.type)), (const uint8_t *)packet + sizeof(header), length - sizeof(header), now, consumed);
157}
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:116

References ASCIICHAT_OK, crypto_rekey_session_receive(), NET_TO_HOST_U16, and packet_validate().

◆ acip_security_rekey_seal()

asciichat_error_t acip_security_rekey_seal ( crypto_record_t * records,
unsigned flow,
crypto_rekey_message_t message,
const void * payload,
size_t length,
uint8_t ** wire,
size_t * written )

Definition at line 159 of file network/acip/security.c.

159 {
161 size_t packet_length = 0;
162 asciichat_error_t result = acip_security_rekey_encode(flow, message, payload, length, packet, sizeof(packet), &packet_length);
163 return result == ASCIICHAT_OK ? acip_packet_seal(records, (acip_traffic_class_t)flow, ACIP_DELIVERY_RELIABLE, packet, packet_length, wire, written) : result;
164}
asciichat_error_t acip_security_rekey_encode(unsigned flow, crypto_rekey_message_t type, const void *payload, size_t length, void *packet, size_t capacity, size_t *written)
asciichat_error_t acip_packet_seal(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written)
Definition prepare.c:108
#define CRYPTO_PUBLIC_KEY_SIZE
Public key size (X25519).
Definition sizes.h:71

References ACIP_DELIVERY_RELIABLE, acip_packet_seal(), acip_security_rekey_encode(), ASCIICHAT_OK, and CRYPTO_PUBLIC_KEY_SIZE.

Referenced by acip_security_next_control().

◆ acip_security_request_rekey()

asciichat_error_t acip_security_request_rekey ( acip_transport_t * transport)

Definition at line 257 of file network/acip/security.c.

257 {
258 if (!transport || !transport->crypto_ctx || !transport->crypto_ctx->encrypt_data)
259 return SET_ERRNO(ERROR_INVALID_STATE, "An encrypted authenticated session is required for rotation");
260 crypto_session_t *session = NULL;
261 asciichat_error_t result = crypto_session_lock(transport->crypto_ctx, &session);
262 if (result == ASCIICHAT_OK) {
263 result = crypto_rekey_session_request(&session->rotation, time_get_ns());
264 crypto_session_unlock(session);
265 }
266 return result;
267}
asciichat_error_t crypto_rekey_session_request(crypto_rekey_session_t *s, uint64_t now)
Definition rekey.c:107
crypto_rekey_session_t rotation

References ASCIICHAT_OK, acip_transport::crypto_ctx, crypto_rekey_session_request(), crypto_session_lock(), crypto_session_unlock(), crypto_context_t::encrypt_data, ERROR_INVALID_STATE, crypto_session::rotation, and SET_ERRNO.

◆ acip_security_seal()

asciichat_error_t acip_security_seal ( crypto_context_t * crypto,
acip_traffic_class_t flow,
acip_delivery_t delivery,
const void * packet,
size_t length,
uint8_t ** output,
size_t * written )

Definition at line 269 of file network/acip/security.c.

269 {
270 crypto_session_t *session = NULL;
271 asciichat_error_t result = crypto_session_lock(crypto, &session);
272 if (result == ASCIICHAT_OK) {
273 result = acip_packet_seal(&session->records, flow, delivery, packet, length, output, written);
274 crypto_session_unlock(session);
275 }
276 return result;
277}

References acip_packet_seal(), ASCIICHAT_OK, crypto_session_lock(), crypto_session_unlock(), and crypto_session::records.

Referenced by browser_client_encrypt_packet(), browser_transport_security_packet(), client_encrypt_packet(), quic_connection_submit(), and quic_connection_submit_datagram().

◆ acip_security_start()

asciichat_error_t acip_security_start ( acip_security_t * security,
acip_transport_t * transport )

Definition at line 18 of file network/acip/security.c.

18 {
19 if (!security || !transport || security->mode > ACIP_CRYPTO_FULL || security->handshake.state != CRYPTO_HANDSHAKE_INIT)
20 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid peer security initialization");
21 security->handshake.addressless_transport = true;
22 transport->crypto_ctx = &security->handshake.crypto_ctx;
24 if (security->handshake.is_server)
25 return ASCIICHAT_OK;
27 protocol_version_packet_init(&version, security->mode);
28 return packet_send_via_transport(transport, PACKET_TYPE_PROTOCOL_VERSION, &version, sizeof(version), 0);
29}
void protocol_version_packet_init(protocol_version_packet_t *version, uint8_t supports_encryption)
Initialize a protocol version packet with the current wire version.
Definition packet.c:37

References ACIP_CRYPTO_FULL, ACIP_CRYPTO_HAS_ENCRYPT, crypto_handshake_context_t::addressless_transport, ASCIICHAT_OK, acip_transport::crypto_ctx, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_INIT, crypto_context_t::encrypt_data, ERROR_INVALID_STATE, acip_security::handshake, crypto_handshake_context_t::is_server, acip_security::mode, packet_send_via_transport(), PACKET_TYPE_PROTOCOL_VERSION, protocol_version_packet_init(), SET_ERRNO, and crypto_handshake_context_t::state.

Referenced by browser_session_start().