ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 414 of file negotiation.c.

415 {
416 if (!transport || !ctx || mode > ACIP_CRYPTO_FULL || protocol > NETWORK_PROTOCOL_DISCOVERY)
417 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client negotiation");
418 if (is_quic(transport) && !ACIP_CRYPTO_HAS_ENCRYPT(mode))
419 return SET_ERRNO(ERROR_CRYPTO, "QUIC requires ACIP encryption");
420 uint64_t timeout = transport->receive_timeout_ns;
421 asciichat_error_t result = protocol == NETWORK_PROTOCOL_DISCOVERY ? discovery_client_negotiate(transport, ctx, mode)
422 : client_negotiate(transport, ctx, mode);
423 transport->receive_timeout_ns = timeout;
424 if (result == ASCIICHAT_OK)
425 result = finish_transport_security(transport, ctx);
426 return result;
427}
#define SET_ERRNO(code,...)
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
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_CRYPTO
Definition error_codes.h:93
@ ERROR_INVALID_PARAM
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1148
#define ACIP_CRYPTO_FULL
Full crypto: encryption + authentication.
Definition packet.h:1141
@ NETWORK_PROTOCOL_DISCOVERY
Definition negotiation.h:5
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 200 of file negotiation.c.

201 {
202 if (!n || !n->handshake || !transport || n->ready)
203 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid connection negotiation state");
204 if (is_quic(transport) && n->step == 0 && type == PACKET_TYPE_PROTOCOL_VERSION &&
205 length == sizeof(protocol_version_packet_t)) {
207 memcpy(&version, payload, sizeof(version));
209 return SET_ERRNO(ERROR_CRYPTO, "QUIC requires ACIP encryption");
210 }
212 asciichat_error_t result = media_receive(n, transport, type, payload, length);
213 if (result == ASCIICHAT_OK && n->ready) {
214 result = finish_transport_security(transport, n->handshake);
215 if (result != ASCIICHAT_OK)
216 n->ready = false;
217 }
218 return result;
219 }
221 asciichat_error_t result;
222 switch (n->step) {
223 case 0: {
224 if (type != PACKET_TYPE_PROTOCOL_VERSION || length != sizeof(protocol_version_packet_t))
225 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Expected discovery protocol version");
227 memcpy(&version, payload, sizeof(version));
229 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Unsupported discovery protocol version");
230 uint8_t mode = version.supports_encryption;
232 if (result != ASCIICHAT_OK)
233 return result;
235 if (mode == ACIP_CRYPTO_NONE) {
237 n->ready = true;
238 return ASCIICHAT_OK;
239 }
240 result = crypto_handshake_server_send_parameters(ctx, transport);
241 if (result == ASCIICHAT_OK)
242 result = crypto_handshake_server_start(ctx, transport);
243 break;
244 }
245 case 1:
246 result = crypto_handshake_server_auth_challenge(ctx, transport, type, payload, length);
247 break;
248 case 2:
249 result = crypto_handshake_server_complete(ctx, transport, type, payload, length);
250 break;
251 default:
252 return SET_ERRNO(ERROR_CRYPTO, "Invalid discovery negotiation state");
253 }
254 if (result != ASCIICHAT_OK)
255 return result;
256 ++n->step;
258 if (n->ready)
259 transport->crypto_ctx = &ctx->crypto_ctx;
260 if (n->ready) {
261 result = finish_transport_security(transport, ctx);
262 if (result != ASCIICHAT_OK)
263 n->ready = false;
264 }
265 return result;
266}
@ CRYPTO_HANDSHAKE_DISABLED
@ CRYPTO_HANDSHAKE_READY
#define NET_TO_HOST_U16(val)
Definition endian.h:111
unsigned char uint8_t
Definition common.h:60
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:76
#define ACIP_CRYPTO_NONE
No encryption, no authentication (plaintext only).
Definition packet.h:1138
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
Definition packet.h:303
#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:5
asciichat_error_t crypto_policy_check_mode(bool transport_secure, bool encrypt, bool no_encrypt, bool require_auth, uint8_t mode)
Definition policy.c:17
crypto_context_t * crypto_ctx
Optional encryption context.
Cryptographic handshake context structure.
crypto_handshake_state_t state
crypto_handshake_context_t * handshake
Definition negotiation.h:14
network_protocol_t protocol
Definition negotiation.h:13
Protocol version negotiation packet structure (Packet Type 1).
Definition packet.h:860
uint16_t protocol_version
Major protocol version (must match for compatibility).
Definition packet.h:862
uint8_t supports_encryption
Encryption & authentication mode bitmask (ACIP_CRYPTO_* constants: NONE, ENCRYPT, AUTH,...
Definition packet.h:868

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.