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

Go to the source code of this file.

Functions

asciichat_error_t network_negotiation_receive (network_negotiation_t *n, acip_transport_t *transport, packet_type_t type, const void *payload, size_t length)
asciichat_error_t network_client_negotiate (acip_transport_t *transport, crypto_handshake_context_t *ctx, uint8_t mode, network_protocol_t protocol)

Function Documentation

◆ network_client_negotiate()

asciichat_error_t network_client_negotiate ( acip_transport_t * transport,
crypto_handshake_context_t * ctx,
uint8_t mode,
network_protocol_t protocol )

Drive a client wire profile using connection-owned security storage. A single absolute deadline covers negotiation; the caller retains transport and credentials until cancellation and all users have drained.

Definition at line 504 of file negotiation.c.

507 {
508 if (!transport || !ctx || mode > ACIP_CRYPTO_FULL ||
510 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client negotiation");
511 if (is_quic(transport) && !ACIP_CRYPTO_HAS_ENCRYPT(mode))
512 return SET_ERRNO(ERROR_CRYPTO, "QUIC requires ACIP encryption");
513 uint64_t timeout = transport->receive_timeout_ns;
514 asciichat_error_t result =
516 ? discovery_client_negotiate(transport, ctx, mode)
517 : client_negotiate(transport, ctx, mode);
518 transport->receive_timeout_ns = timeout;
519 if (result == ASCIICHAT_OK)
520 result = finish_transport_security(transport, ctx);
521 return result;
522}
#define SET_ERRNO(code,...)
unsigned long long uint64_t
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_CRYPTO
@ ERROR_INVALID_PARAM
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1171
#define ACIP_CRYPTO_FULL
Full crypto: encryption + authentication.
Definition packet.h:1164
@ NETWORK_PROTOCOL_DISCOVERY
Definition negotiation.h:11
uint64_t receive_timeout_ns
Optional TCP receive timeout override; zero uses the normal polling timeout.

References ACIP_CRYPTO_FULL, ACIP_CRYPTO_HAS_ENCRYPT, ASCIICHAT_OK, ERROR_CRYPTO, ERROR_INVALID_PARAM, NETWORK_PROTOCOL_DISCOVERY, acip_transport::receive_timeout_ns, and SET_ERRNO.

Referenced by discovery_session_start(), and network_connection_establish().

◆ network_negotiation_receive()

asciichat_error_t network_negotiation_receive ( network_negotiation_t * n,
acip_transport_t * transport,
packet_type_t type,
const void * payload,
size_t length )

Definition at line 240 of file negotiation.c.

244 {
245 if (!n || !n->handshake || !transport || n->ready)
247 "Invalid connection negotiation state");
248 if (is_quic(transport) && n->step == 0 &&
250 length == sizeof(protocol_version_packet_t)) {
252 memcpy(&version, payload, sizeof(version));
254 return SET_ERRNO(ERROR_CRYPTO, "QUIC requires ACIP encryption");
255 }
257 asciichat_error_t result =
258 media_receive(n, transport, type, payload, length);
259 if (result == ASCIICHAT_OK && n->ready) {
260 result = finish_transport_security(transport, n->handshake);
261 if (result != ASCIICHAT_OK)
262 n->ready = false;
263 }
264 return result;
265 }
267 asciichat_error_t result;
268 switch (n->step) {
269 case 0: {
270 if (type != PACKET_TYPE_PROTOCOL_VERSION ||
271 length != sizeof(protocol_version_packet_t))
273 "Expected discovery protocol version");
275 memcpy(&version, payload, sizeof(version));
278 "Unsupported discovery protocol version");
279 uint8_t mode = version.supports_encryption;
280 result =
282 n->forbid_encryption, n->require_auth, mode);
283 if (result != ASCIICHAT_OK)
284 return result;
287 if (mode == ACIP_CRYPTO_NONE) {
289 n->ready = true;
290 return ASCIICHAT_OK;
291 }
292 result = crypto_handshake_server_send_parameters(ctx, transport);
293 if (result == ASCIICHAT_OK)
294 result = crypto_handshake_server_start(ctx, transport);
295 break;
296 }
297 case 1:
298 result = crypto_handshake_server_auth_challenge(ctx, transport, type,
299 payload, length);
300 break;
301 case 2:
302 result =
303 crypto_handshake_server_complete(ctx, transport, type, payload, length);
304 break;
305 default:
306 return SET_ERRNO(ERROR_CRYPTO, "Invalid discovery negotiation state");
307 }
308 if (result != ASCIICHAT_OK)
309 return result;
310 ++n->step;
312 if (n->ready)
313 transport->crypto_ctx = &ctx->crypto_ctx;
314 if (n->ready) {
315 result = finish_transport_security(transport, ctx);
316 if (result != ASCIICHAT_OK)
317 n->ready = false;
318 }
319 return result;
320}
#define NET_TO_HOST_U16(val)
Definition endian.h:115
unsigned char uint8_t
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
#define ACIP_CRYPTO_NONE
No encryption, no authentication (plaintext only).
Definition packet.h:1161
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
Definition packet.h:317
#define PROTOCOL_VERSION_MAJOR
Major protocol version number.
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.
@ NETWORK_PROTOCOL_MEDIA
Definition negotiation.h:10
asciichat_error_t crypto_policy_check_mode(bool transport_secure, bool encrypt, bool no_encrypt, bool require_auth, uint8_t mode)
Definition policy.c:24
crypto_context_t * crypto_ctx
Optional encryption context.
Cryptographic handshake context structure.
crypto_handshake_context_t * handshake
Definition negotiation.h:25
network_protocol_t protocol
Definition negotiation.h:24
Protocol version negotiation packet structure (Packet Type 1).
Definition packet.h:871
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_HAS_ENCRYPT, ACIP_CRYPTO_NONE, ASCIICHAT_OK, acip_transport::crypto_ctx, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_DISABLED, CRYPTO_HANDSHAKE_READY, crypto_handshake_server_auth_challenge(), crypto_handshake_server_complete(), crypto_handshake_server_send_parameters(), crypto_handshake_server_start(), crypto_policy_check_mode(), crypto_context_t::encrypt_data, network_negotiation_t::encrypt_data, ERROR_CRYPTO, ERROR_INVALID_STATE, ERROR_NETWORK_PROTOCOL, network_negotiation_t::forbid_encryption, network_negotiation_t::handshake, NET_TO_HOST_U16, NETWORK_PROTOCOL_MEDIA, PACKET_TYPE_PROTOCOL_VERSION, network_negotiation_t::protocol, protocol_version_packet_t::protocol_version, PROTOCOL_VERSION_MAJOR, network_negotiation_t::ready, network_negotiation_t::require_auth, network_negotiation_t::require_encryption, network_negotiation_t::secure, SET_ERRNO, crypto_handshake_context_t::state, network_negotiation_t::step, and protocol_version_packet_t::supports_encryption.