ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
src/web/client.c
Go to the documentation of this file.
1#include "client.h"
2
3/* Storage capacities in elements; text buffers include the terminating NUL. */
4#define WEB_CLIENT_CRYPTO_PASSWORD_BUFFER_SIZE 256
5
6#include "security.h"
13#include <emscripten.h>
14#include <ascii-chat/common.h>
15#include <string.h>
17
18static acip_render_settings_t remote_render_settings;
19
21asciichat_error_t client_settings_receive(const void *payload, size_t length) {
23 const char *json;
24 size_t json_length;
25 asciichat_error_t result = acip_settings_decode(payload, length, &header, &json, &json_length);
26 if (result != ASCIICHAT_OK)
27 return result;
28 options_t *current = SAFE_MALLOC(sizeof(*current), options_t *), *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
29 // Browser capability packets and the shared renderer always support UTF-8.
30 remote_render_settings.caps.utf8_support = true;
31 acip_render_settings_options(&remote_render_settings, current);
32 result = options_prepare_json(current, json, json_length, candidate, true);
33 if (result == ASCIICHAT_OK)
34 result = acip_render_settings_prepare(&remote_render_settings, candidate);
35 if (result == ASCIICHAT_OK)
36 remote_render_settings.revision = header.revision;
37 SAFE_FREE(current);
38 SAFE_FREE(candidate);
39 return result;
40}
42 options_t *values = SAFE_MALLOC(sizeof(*values), options_t *);
43 acip_render_settings_options(&remote_render_settings, values);
44 char *json = options_values_json(values, true);
45 SAFE_FREE(values);
46 return json;
47}
48
52
53#include <emscripten.h>
54#include <emscripten/html5.h>
55#include <stdlib.h>
56#include <string.h>
57#include <stdint.h>
58
59// JavaScript callback for sending complete ACIP packets from WASM to WebSocket
60// This will be called by the WASM transport to send complete packets (header + payload)
61// clang-format off
62EM_JS(void, js_send_raw_packet, (const uint8_t *packet_data, size_t packet_len), {
63 if (!Module.sendPacketCallback) {
64 console.error('[WASM] sendPacketCallback not registered - cannot send packet');
65 return;
66 }
67
68 // Copy complete packet from WASM memory to JavaScript
69 const packetArray = new Uint8Array(HEAPU8.buffer, packet_data, packet_len);
70 const packetCopy = new Uint8Array(packetArray);
71
72 // Send as raw binary packet via WebSocket
73 Module.sendPacketCallback(packetCopy);
74});
75// clang-format on
76
77#include <options/options.h>
78#include <options/rcu.h>
81#include <ascii-chat/log/log.h>
96
97#include <ascii-chat/common.h>
98#include <ascii-chat/buffer.h>
101#include <opus.h>
102#include <sodium.h>
103
104// ============================================================================
105// WASM Transport Implementation
106// ============================================================================
107
111static asciichat_error_t browser_transport_send(acip_transport_t *transport, const void *data, size_t len) {
112
113 if (!transport || !data || !len)
114 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid browser transport packet");
115
116 // Forward complete packet (header + payload) to JavaScript WebSocket bridge
117 js_send_raw_packet((const uint8_t *)data, len);
118
119 return ASCIICHAT_OK;
120}
121
122static asciichat_error_t browser_transport_recv(acip_transport_t *transport, void **buffer, size_t *out_len, void **out_allocated_buffer) {
123 // Not used - packets arrive via JavaScript callbacks
124 return SET_ERRNO(ERROR_NOT_SUPPORTED, "recv not supported on WASM transport");
125}
126
127static asciichat_error_t browser_transport_close(acip_transport_t *transport) {
128 return ASCIICHAT_OK; // Nothing to close
129}
130
131static acip_transport_type_t browser_transport_get_type(acip_transport_t *transport) {
132 return ACIP_TRANSPORT_WEBSOCKET; // Closest match
133}
134
135static socket_t browser_transport_get_socket(acip_transport_t *transport) {
136 return INVALID_SOCKET_VALUE;
137}
138
139static bool browser_transport_is_connected(acip_transport_t *transport) {
140 return true; // Always "connected" from WASM perspective
141}
142
143static const acip_transport_methods_t browser_transport_methods = {.send = browser_transport_send,
144 .recv = browser_transport_recv,
145 .close = browser_transport_close,
146 .get_type = browser_transport_get_type,
147 .get_socket = browser_transport_get_socket,
148 .is_connected = browser_transport_is_connected,
149 .destroy_impl = NULL};
150
151static acip_transport_t g_wasm_transport = {.methods = &browser_transport_methods, .crypto_ctx = NULL, .impl_data = NULL};
152
153// ============================================================================
154// Global State
155// ============================================================================
156
157// Global state for client session
158static crypto_handshake_context_t g_crypto_handshake_ctx = {0};
159static bool g_initialized = false;
160static bool g_handshake_complete = false;
161static uint8_t g_crypto_mode = ACIP_CRYPTO_ENCRYPT;
162static bool g_version_received = false;
163static char g_crypto_password[WEB_CLIENT_CRYPTO_PASSWORD_BUFFER_SIZE] = {0};
164static uint8_t g_client_identity_key[crypto_sign_SECRETKEYBYTES] = {0};
165static uint8_t g_expected_server_identity_key[crypto_sign_PUBLICKEYBYTES] = {0};
166static bool g_has_client_identity_key = false;
167static bool g_has_expected_server_identity_key = false;
168
169// Opus codec state
170static OpusEncoder *g_opus_encoder = NULL;
171static OpusDecoder *g_opus_decoder = NULL;
172
173// Connection state enum (exposed to JS)
175
176static connection_state_t g_connection_state = CONNECTION_STATE_DISCONNECTED;
177
178static int apply_client_crypto_options(void) {
179 crypto_handshake_context_t *ctx = &g_crypto_handshake_ctx;
180 if (ctx->state != CRYPTO_HANDSHAKE_INIT) {
181 SET_ERRNO(ERROR_INVALID_STATE, "Client handshake is not ready for crypto options");
182 return -1;
183 }
184
185 if (g_crypto_password[0] != '\0') {
186 crypto_result_t result = crypto_derive_password_key(&ctx->crypto_ctx, g_crypto_password);
187 if (result != CRYPTO_OK)
188 return -1;
189 ctx->crypto_ctx.has_password = true;
190 ctx->has_password = true;
191 SAFE_STRNCPY(ctx->password, g_crypto_password, sizeof(ctx->password) - 1);
192 }
193
194 if (g_has_client_identity_key) {
196 memcpy(ctx->client_private_key.key.ed25519, g_client_identity_key, sizeof(g_client_identity_key));
197 memcpy(ctx->client_private_key.public_key, g_client_identity_key + crypto_sign_SEEDBYTES, crypto_sign_PUBLICKEYBYTES);
198 memcpy(ctx->client_public_key.key, g_client_identity_key + crypto_sign_SEEDBYTES, crypto_sign_PUBLICKEYBYTES);
200 }
201
202 if (g_has_expected_server_identity_key) {
203 memcpy(ctx->expected_server_key_bytes, g_expected_server_identity_key, sizeof(ctx->expected_server_key_bytes));
205 ctx->verify_server_key = true;
206 }
207 return 0;
208}
209
211int client_configure_crypto_options(const char *password, const uint8_t *identity_secret_key, const uint8_t *expected_server_public_key) {
212 if (!g_initialized) {
213 SET_ERRNO(ERROR_INVALID_STATE, "Client is not initialized");
214 return -1;
215 }
216
217 sodium_memzero(g_crypto_password, sizeof(g_crypto_password));
218 sodium_memzero(g_client_identity_key, sizeof(g_client_identity_key));
219 sodium_memzero(g_expected_server_identity_key, sizeof(g_expected_server_identity_key));
220 g_has_client_identity_key = false;
221 g_has_expected_server_identity_key = false;
222
223 if (password && password[0] != '\0') {
224 size_t length = strnlen(password, sizeof(g_crypto_password));
225 if (length < 8 || length >= sizeof(g_crypto_password)) {
226 SET_ERRNO(ERROR_INVALID_PARAM, "Client password must contain 8 to %zu bytes", sizeof(g_crypto_password) - 1);
227 return -1;
228 }
229 SAFE_STRNCPY(g_crypto_password, password, sizeof(g_crypto_password) - 1);
230 }
231
232 if (identity_secret_key) {
233 uint8_t derived_public_key[crypto_sign_PUBLICKEYBYTES];
234 if (crypto_sign_ed25519_sk_to_pk(derived_public_key, identity_secret_key) != 0 || sodium_memcmp(derived_public_key, identity_secret_key + crypto_sign_SEEDBYTES, crypto_sign_PUBLICKEYBYTES) != 0) {
235 sodium_memzero(derived_public_key, sizeof(derived_public_key));
236 SET_ERRNO(ERROR_INVALID_PARAM, "Client identity secret key is invalid");
237 return -1;
238 }
239 memcpy(g_client_identity_key, identity_secret_key, sizeof(g_client_identity_key));
240 sodium_memzero(derived_public_key, sizeof(derived_public_key));
241 g_has_client_identity_key = true;
242 }
243
244 if (expected_server_public_key) {
245 memcpy(g_expected_server_identity_key, expected_server_public_key, sizeof(g_expected_server_identity_key));
246 g_has_expected_server_identity_key = true;
247 }
248 return 0;
249}
250
252int client_parse_ssh_private_key(const char *key_path, uint8_t *secret_key_out) {
253 if (!key_path || !secret_key_out) {
254 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid SSH private key input or output");
255 return -1;
256 }
257#ifdef EMSCRIPTEN_BUILD
258 static const char wasm_key_prefix[] = "/tmp/ascii-chat-key-";
259 if (strncmp(key_path, wasm_key_prefix, sizeof(wasm_key_prefix) - 1) != 0) {
260 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid browser SSH private key path");
261 return -1;
262 }
263#endif
264 private_key_t key = {0};
265 asciichat_error_t result = parse_ssh_private_key(key_path, &key);
266 if (result != ASCIICHAT_OK || key.type != KEY_TYPE_ED25519 || key.use_ssh_agent || key.use_gpg_agent) {
267 sodium_memzero(&key, sizeof(key));
268 return -1;
269 }
270 memcpy(secret_key_out, key.key.ed25519, crypto_sign_SECRETKEYBYTES);
271 sodium_memzero(&key, sizeof(key));
272 return 0;
273}
274
276int client_parse_ssh_public_key(const char *key_line, uint8_t *public_key_out) {
277 if (!key_line || !public_key_out) {
278 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid SSH public key input or output");
279 return -1;
280 }
281 return parse_ssh_ed25519_line(key_line, public_key_out) == ASCIICHAT_OK ? 0 : -1;
282}
283
285int client_parse_gpg_public_key(const char *armored_key, uint8_t *public_key_out) {
286 if (!armored_key || !public_key_out) {
287 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid OpenPGP public key input or output");
288 return -1;
289 }
290 return openpgp_parse_armored_pubkey(armored_key, public_key_out) == ASCIICHAT_OK ? 0 : -1;
291}
292
294int client_parse_gpg_public_key_binary(const uint8_t *key_data, size_t key_data_len, uint8_t *public_key_out) {
295 if (!key_data || key_data_len == 0 || !public_key_out) {
296 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid binary OpenPGP public key input or output");
297 return -1;
298 }
299 return openpgp_parse_binary_pubkey(key_data, key_data_len, public_key_out) == ASCIICHAT_OK ? 0 : -1;
300}
301
303int client_parse_gpg_private_key(const char *armored_key, uint8_t *secret_key_out) {
304 if (!armored_key || !secret_key_out) {
305 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid OpenPGP private key input or output");
306 return -1;
307 }
308 uint8_t public_key[crypto_sign_PUBLICKEYBYTES] = {0};
309 uint8_t seed[crypto_sign_SEEDBYTES] = {0};
310 uint8_t derived_public_key[crypto_sign_PUBLICKEYBYTES] = {0};
311 uint8_t secret_key[crypto_sign_SECRETKEYBYTES] = {0};
312 int result = -1;
313
314 if (openpgp_parse_armored_seckey(armored_key, public_key, seed) != ASCIICHAT_OK)
315 goto cleanup;
316 if (crypto_sign_seed_keypair(derived_public_key, secret_key, seed) != 0 || sodium_memcmp(derived_public_key, public_key, sizeof(public_key)) != 0) {
317 SET_ERRNO(ERROR_CRYPTO_KEY, "OpenPGP private key does not match its Ed25519 public key");
318 goto cleanup;
319 }
320
321 memcpy(secret_key_out, secret_key, sizeof(secret_key));
322 result = 0;
323
324cleanup:
325 sodium_memzero(public_key, sizeof(public_key));
326 sodium_memzero(seed, sizeof(seed));
327 sodium_memzero(derived_public_key, sizeof(derived_public_key));
328 sodium_memzero(secret_key, sizeof(secret_key));
329 return result;
330}
331
333int client_parse_gpg_private_key_binary(const uint8_t *key_data, size_t key_data_len, uint8_t *secret_key_out) {
334 if (!key_data || key_data_len == 0 || !secret_key_out) {
335 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid binary OpenPGP private key input or output");
336 return -1;
337 }
338 uint8_t public_key[crypto_sign_PUBLICKEYBYTES] = {0};
339 uint8_t seed[crypto_sign_SEEDBYTES] = {0};
340 uint8_t derived_public_key[crypto_sign_PUBLICKEYBYTES] = {0};
341 uint8_t secret_key[crypto_sign_SECRETKEYBYTES] = {0};
342 int result = -1;
343
344 if (openpgp_parse_binary_seckey(key_data, key_data_len, public_key, seed) != ASCIICHAT_OK)
345 goto cleanup;
346 if (crypto_sign_seed_keypair(derived_public_key, secret_key, seed) != 0 || sodium_memcmp(derived_public_key, public_key, sizeof(public_key)) != 0) {
347 SET_ERRNO(ERROR_CRYPTO_KEY, "OpenPGP private key does not match its Ed25519 public key");
348 goto cleanup;
349 }
350
351 memcpy(secret_key_out, secret_key, sizeof(secret_key));
352 result = 0;
353
354cleanup:
355 sodium_memzero(public_key, sizeof(public_key));
356 sodium_memzero(seed, sizeof(seed));
357 sodium_memzero(derived_public_key, sizeof(derived_public_key));
358 sodium_memzero(secret_key, sizeof(secret_key));
359 return result;
360}
361
363int client_get_crypto_error_message(char *output, size_t output_size) {
364 if (!output || output_size == 0) {
365 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid crypto error output buffer");
366 return -1;
367 }
368 asciichat_error_context_t context = {0};
369 if (!HAS_ERRNO(&context) || !context.context_message[0])
370 return -1;
371 safe_snprintf(output, output_size, "%s", context.context_message);
372 return 0;
373}
374
375// ============================================================================
376// Initialization
377// ============================================================================
378
383int client_init(void) {
384 // The module, renderers, and allocator outlive individual connections.
385 if (g_initialized)
386 return 0;
387 char *argv[] = {"client", NULL};
388 asciichat_error_t err = options_init(1, argv);
389 if (err != ASCIICHAT_OK)
390 return -1;
391
392 // Initialize platform layer
393 err = platform_init();
394 if (err != ASCIICHAT_OK) {
395 return -1;
396 }
397
398 // Honor the requested verbosity and let INFO use stdout (console.log).
399 log_init(NULL, GET_OPTION(log_level), false, false);
400 // Use debug-style format in WASM so browser console shows full log headers
402 log_info("WASM client initialized via logging system");
403
404 if (err != ASCIICHAT_OK) {
405 return -1;
406 }
407
408 // Initialize ANSI color code generation
410
411 g_initialized = true;
412 g_connection_state = CONNECTION_STATE_DISCONNECTED;
413
414 return 0;
415}
416
417void client_cleanup(void) {
418 remote_render_settings = (acip_render_settings_t){0};
420
421 // Clean up crypto handshake context
422 crypto_handshake_destroy(&g_crypto_handshake_ctx);
423 memset(&g_crypto_handshake_ctx, 0, sizeof(g_crypto_handshake_ctx));
424
425 g_handshake_complete = false;
426 g_wasm_transport.crypto_ctx = NULL;
427 g_connection_state = CONNECTION_STATE_DISCONNECTED;
428 sodium_memzero(g_crypto_password, sizeof(g_crypto_password));
429 sodium_memzero(g_client_identity_key, sizeof(g_client_identity_key));
430 sodium_memzero(g_expected_server_identity_key, sizeof(g_expected_server_identity_key));
431 g_has_client_identity_key = false;
432 g_has_expected_server_identity_key = false;
433 // Keep module-wide options and buffer domains alive while browser renderers
434 // still reference them. Connection cleanup only resets authentication state.
435}
436
437// ============================================================================
438// Cryptography API
439// ============================================================================
440
446
447 if (!g_initialized) {
448 SET_ERRNO(ERROR_INVALID_STATE, "Client is not initialized");
449 return -1;
450 }
451
452 // A replacement handshake must not retain the previous connection's sequence state.
453 // Reset handshake context if previously used (reconnection / React Strict Mode remount)
454 if (g_crypto_handshake_ctx.state != CRYPTO_HANDSHAKE_DISABLED) {
455 crypto_handshake_destroy(&g_crypto_handshake_ctx);
456 memset(&g_crypto_handshake_ctx, 0, sizeof(g_crypto_handshake_ctx));
457 g_handshake_complete = false;
458 }
459
460 // Initialize crypto handshake context
461 asciichat_error_t result = crypto_handshake_init("wasm-client", &g_crypto_handshake_ctx, false /* is_server */);
462 if (result != ASCIICHAT_OK) {
463 return -1;
464 }
465 if (apply_client_crypto_options() != 0) {
466 crypto_handshake_destroy(&g_crypto_handshake_ctx);
467 memset(&g_crypto_handshake_ctx, 0, sizeof(g_crypto_handshake_ctx));
468 return -1;
469 }
470
471 g_connection_state = CONNECTION_STATE_DISCONNECTED;
472 return 0;
473}
474
481int client_set_server_address(const char *server_host, int server_port) {
482 if (!g_initialized) {
483 SET_ERRNO(ERROR_INVALID_STATE, "Client is not initialized");
484 return -1;
485 }
486
487 if (!server_host || !server_host[0] || server_port <= 0 || server_port > 65535) {
488 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client server address or port");
489 return -1;
490 }
491
492 // Set server IP and port in handshake context
493 SAFE_STRNCPY(g_crypto_handshake_ctx.server_ip, server_host, sizeof(g_crypto_handshake_ctx.server_ip));
494 g_crypto_handshake_ctx.server_port = server_port;
495
496 return 0;
497}
498
500 if (g_crypto_handshake_ctx.state == CRYPTO_HANDSHAKE_DISABLED) {
501 SET_ERRNO(ERROR_INVALID_STATE, "Client handshake is not initialized");
502 return NULL;
503 }
504 return g_crypto_handshake_ctx.crypto_ctx.public_key;
505}
506
514int client_handle_key_exchange_init(const uint8_t *packet, size_t packet_len) {
515
516 // Safety check: if handshake context is not in INIT state, reinitialize it
517 // This handles cases where previous handshakes weren't properly cleaned up
518 if (g_crypto_handshake_ctx.state != CRYPTO_HANDSHAKE_INIT) {
519
520 // Destroy and reset
521 if (g_crypto_handshake_ctx.state != CRYPTO_HANDSHAKE_DISABLED) {
522 crypto_handshake_destroy(&g_crypto_handshake_ctx);
523 }
524 memset(&g_crypto_handshake_ctx, 0, sizeof(g_crypto_handshake_ctx));
525
526 // Reinitialize to INIT state
527 asciichat_error_t init_result = crypto_handshake_init("wasm-client", &g_crypto_handshake_ctx, false);
528 if (init_result != ASCIICHAT_OK) {
529 return -1;
530 }
531 if (apply_client_crypto_options() != 0)
532 return -1;
533 }
534
535 if (!packet || packet_len == 0) {
536 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client handshake packet");
537 return -1;
538 }
539
540 // Extract packet type and payload
541 if (packet_len < sizeof(packet_header_t)) {
542 SET_ERRNO(ERROR_INVALID_PARAM, "Client handshake packet is shorter than its header");
543 return -1;
544 }
545
546 const packet_header_t *header = (const packet_header_t *)packet;
547 packet_type_t packet_type = ntohs(header->type);
548 const uint8_t *payload_src = packet + sizeof(packet_header_t);
549 size_t payload_len = packet_len - sizeof(packet_header_t);
550
551 // Allocate payload copy from buffer domain (crypto function takes ownership and frees it)
552 // The raw packet pointer from JS cannot be passed directly because the crypto
553 // handshake function calls buffer_release() on the payload when done.
554 uint8_t *payload = NULL;
555 if (payload_len > 0) {
556 payload = buffer_alloc(NULL, payload_len);
557 if (!payload) {
558 g_connection_state = CONNECTION_STATE_ERROR;
559 return -1;
560 }
561 memcpy(payload, payload_src, payload_len);
562 }
563
564 // Process key exchange using transport-abstracted handshake
565
566 asciichat_error_t result = crypto_handshake_client_key_exchange(&g_crypto_handshake_ctx, &g_wasm_transport, packet_type, payload, payload_len);
567
568 if (result != ASCIICHAT_OK) {
569 g_connection_state = CONNECTION_STATE_ERROR;
570 return -1;
571 }
572
573 g_connection_state = CONNECTION_STATE_HANDSHAKE;
574 return 0;
575}
576
584int client_handle_crypto_parameters(const uint8_t *packet, size_t packet_len) {
585
586 if (!packet || packet_len == 0) {
587 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client crypto parameters packet");
588 return -1;
589 }
590
591 // Discovery sends CRYPTO_PARAMETERS before KEY_EXCHANGE_INIT. Initialize
592 // here so the negotiated values survive the subsequent key exchange packet.
593 if (g_crypto_handshake_ctx.state != CRYPTO_HANDSHAKE_INIT) {
594 if (g_crypto_handshake_ctx.state != CRYPTO_HANDSHAKE_DISABLED) {
595 crypto_handshake_destroy(&g_crypto_handshake_ctx);
596 }
597 memset(&g_crypto_handshake_ctx, 0, sizeof(g_crypto_handshake_ctx));
598 if (crypto_handshake_init("wasm-client", &g_crypto_handshake_ctx, false) != ASCIICHAT_OK) {
599 return -1;
600 }
601 if (apply_client_crypto_options() != 0)
602 return -1;
603 }
604
605 // Extract packet type and payload
606 if (packet_len < sizeof(packet_header_t)) {
607 SET_ERRNO(ERROR_INVALID_PARAM, "Client crypto parameters packet is shorter than its header");
608 return -1;
609 }
610
611 const packet_header_t *header = (const packet_header_t *)packet;
612 packet_type_t packet_type = ntohs(header->type);
613 const uint8_t *payload_src = packet + sizeof(packet_header_t);
614 size_t payload_len = packet_len - sizeof(packet_header_t);
615
616 // Allocate payload copy from buffer domain
617 uint8_t *payload = NULL;
618 if (payload_len > 0) {
619 payload = buffer_alloc(NULL, payload_len);
620 if (!payload) {
621 g_connection_state = CONNECTION_STATE_ERROR;
622 return -1;
623 }
624 memcpy(payload, payload_src, payload_len);
625 }
626
627 // Process crypto parameters
628 asciichat_error_t result = crypto_handshake_set_parameters(&g_crypto_handshake_ctx, (const crypto_parameters_packet_t *)payload);
629
630 // Free payload
631 if (payload) {
632 buffer_release(payload);
633 }
634
635 if (result != ASCIICHAT_OK) {
636 g_connection_state = CONNECTION_STATE_ERROR;
637 return -1;
638 }
639
640 return 0;
641}
642
649int client_handle_auth_challenge(const uint8_t *packet, size_t packet_len) {
650
651 if (!packet || packet_len == 0) {
652 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client authentication challenge packet");
653 return -1;
654 }
655
656 // Extract packet type and payload
657 if (packet_len < sizeof(packet_header_t)) {
658 SET_ERRNO(ERROR_INVALID_PARAM, "Client authentication challenge is shorter than its header");
659 return -1;
660 }
661
662 const packet_header_t *header = (const packet_header_t *)packet;
663 packet_type_t packet_type = ntohs(header->type);
664 const uint8_t *payload_src = packet + sizeof(packet_header_t);
665 size_t payload_len = packet_len - sizeof(packet_header_t);
666
667 // Allocate payload copy from buffer domain (crypto function takes ownership and frees it)
668 uint8_t *payload = NULL;
669 if (payload_len > 0) {
670 payload = buffer_alloc(NULL, payload_len);
671 if (!payload) {
672 g_connection_state = CONNECTION_STATE_ERROR;
673 return -1;
674 }
675 memcpy(payload, payload_src, payload_len);
676 }
677
678 // Process auth challenge
679 asciichat_error_t result = crypto_handshake_client_auth_response(&g_crypto_handshake_ctx, &g_wasm_transport, packet_type, payload, payload_len);
680
681 if (result != ASCIICHAT_OK) {
682 g_connection_state = CONNECTION_STATE_ERROR;
683 return -1;
684 }
685
686 return 0;
687}
688
695int client_handle_handshake_complete(const uint8_t *packet, size_t packet_len) {
696
697 if (!packet || packet_len == 0) {
698 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client handshake completion packet");
699 return -1;
700 }
701
702 // Extract packet type and payload
703 if (packet_len < sizeof(packet_header_t)) {
704 SET_ERRNO(ERROR_INVALID_PARAM, "Client handshake completion is shorter than its header");
705 return -1;
706 }
707
708 const packet_header_t *header = (const packet_header_t *)packet;
709 packet_type_t packet_type = ntohs(header->type);
710 const uint8_t *payload_src = packet + sizeof(packet_header_t);
711 size_t payload_len = packet_len - sizeof(packet_header_t);
712
713 // Allocate payload copy from buffer domain (crypto function takes ownership and frees it)
714 uint8_t *payload = NULL;
715 if (payload_len > 0) {
716 payload = buffer_alloc(NULL, payload_len);
717 if (!payload) {
718 g_connection_state = CONNECTION_STATE_ERROR;
719 return -1;
720 }
721 memcpy(payload, payload_src, payload_len);
722 }
723
724 // Complete handshake (takes ownership of payload and will free it)
725 asciichat_error_t result = crypto_handshake_client_complete(&g_crypto_handshake_ctx, &g_wasm_transport, packet_type, payload, payload_len);
726
727 if (result != ASCIICHAT_OK) {
728 g_connection_state = CONNECTION_STATE_ERROR;
729 return -1;
730 }
731
732 g_handshake_complete = true;
733 g_connection_state = CONNECTION_STATE_CONNECTED;
734
735 // Update transport with the crypto context so ACIP can encrypt/decrypt packets
736 g_wasm_transport.crypto_ctx = &g_crypto_handshake_ctx.crypto_ctx;
737
738 return 0;
739}
740
741// ============================================================================
742// Packet Processing API
743// ============================================================================
744
754int client_encrypt_packet(const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t ciphertext_size, size_t *out_len) {
755 if (!g_handshake_complete) {
756 SET_ERRNO(ERROR_INVALID_STATE, "Client handshake is not complete");
757 return -1;
758 }
759
760 const size_t overhead = 32 + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
761 if (!plaintext || !ciphertext || !out_len || plaintext_len > SIZE_MAX - overhead || ciphertext_size < plaintext_len + overhead) {
762 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client encryption buffers or lengths");
763 return -1;
764 }
765 uint8_t *wire = NULL;
766 size_t length = 0;
767 packet_header_t header;
768 if (plaintext_len < sizeof(header)) {
769 SET_ERRNO(ERROR_INVALID_PARAM, "Client plaintext packet is shorter than its header");
770 return -1;
771 }
772 memcpy(&header, plaintext, sizeof(header));
774 asciichat_error_t result = acip_security_seal(&g_crypto_handshake_ctx.crypto_ctx, channel, ACIP_DELIVERY_RELIABLE, plaintext, plaintext_len, &wire, &length);
775 if (result == ASCIICHAT_OK) {
776 *out_len = length - sizeof(packet_header_t);
777 memcpy(ciphertext, wire + sizeof(packet_header_t), *out_len);
778 }
779 buffer_release(wire);
780 return result == ASCIICHAT_OK ? 0 : -1;
781}
782
792int client_decrypt_packet(const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t plaintext_size, size_t *out_len) {
793 if (!g_handshake_complete) {
794 SET_ERRNO(ERROR_INVALID_STATE, "Client handshake is not complete");
795 return -1;
796 }
797
798 if (!ciphertext || !plaintext || !out_len || plaintext_size < ciphertext_len) {
799 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client decryption buffers or lengths");
800 return -1;
801 }
802 *out_len = 0;
803 uint8_t *plain = NULL;
804 size_t length = 0;
805 uint64_t sequence;
806 bool consumed = false;
807 asciichat_error_t result =
808 acip_security_decrypt(&g_crypto_handshake_ctx.crypto_ctx, CRYPTO_RECORD_ANY_CHANNEL, ACIP_DELIVERY_RELIABLE, ciphertext, ciphertext_len, &plain, &length, &sequence, &consumed);
809 if (result == ASCIICHAT_OK && !consumed) {
810 memcpy(plaintext, plain, length);
811 *out_len = length;
812 }
813 buffer_release(plain);
814 return result == ASCIICHAT_OK ? 0 : -1;
815}
816
817// ============================================================================
818// Connection State API
819// ============================================================================
820
826 return (int)g_connection_state;
827}
828
829// ============================================================================
830// Opus Audio Codec API
831// ============================================================================
832
840int client_opus_encoder_init(int sample_rate, int channels, int bitrate) {
841 if (g_opus_encoder) {
842 opus_encoder_destroy(g_opus_encoder);
843 g_opus_encoder = NULL;
844 }
845
846 int error;
847 g_opus_encoder = opus_encoder_create(sample_rate, channels, bitrate >= 128000 ? OPUS_APPLICATION_AUDIO : OPUS_APPLICATION_VOIP, &error);
848 if (error != OPUS_OK || !g_opus_encoder) {
849 SET_ERRNO(ERROR_INVALID_PARAM, "Cannot initialize Opus encoder: %s", opus_strerror(error));
850 return -1;
851 }
852
853 // Set bitrate
854 if (opus_encoder_ctl(g_opus_encoder, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) {
855 opus_encoder_destroy(g_opus_encoder);
856 g_opus_encoder = NULL;
857 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus encoder bitrate: %d", bitrate);
858 return -1;
859 }
860
861 return 0;
862}
863
870int client_opus_decoder_init(int sample_rate, int channels) {
871 if (g_opus_decoder) {
872 opus_decoder_destroy(g_opus_decoder);
873 g_opus_decoder = NULL;
874 }
875
876 int error;
877 g_opus_decoder = opus_decoder_create(sample_rate, channels, &error);
878 if (error != OPUS_OK || !g_opus_decoder) {
879 SET_ERRNO(ERROR_INVALID_PARAM, "Cannot initialize Opus decoder: %s", opus_strerror(error));
880 return -1;
881 }
882
883 return 0;
884}
885
894int client_opus_encode(const int16_t *pcm_data, int frame_size, uint8_t *opus_data, int max_opus_bytes) {
895 if (!g_opus_encoder) {
896 SET_ERRNO(ERROR_INVALID_STATE, "Opus encoder is not initialized");
897 return -1;
898 }
899
900 if (!pcm_data || !opus_data || frame_size <= 0 || max_opus_bytes <= 0) {
901 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus encoder buffers or lengths");
902 return -1;
903 }
904
905 int encoded_bytes = opus_encode(g_opus_encoder, pcm_data, frame_size, opus_data, max_opus_bytes);
906 if (encoded_bytes < 0) {
907 SET_ERRNO(ERROR_INVALID_PARAM, "Opus encoding failed: %s", opus_strerror(encoded_bytes));
908 return -1;
909 }
910
911 return encoded_bytes;
912}
913
923int client_opus_decode(const uint8_t *opus_data, int opus_bytes, int16_t *pcm_data, int frame_size, int decode_fec) {
924 if (!g_opus_decoder) {
925 SET_ERRNO(ERROR_INVALID_STATE, "Opus decoder is not initialized");
926 return -1;
927 }
928
929 if ((!opus_data && opus_bytes > 0) || opus_bytes < 0 || !pcm_data || frame_size <= 0) {
930 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus decoder buffers or lengths");
931 return -1;
932 }
933
934 int decoded_samples = opus_decode(g_opus_decoder, opus_data, opus_bytes, pcm_data, frame_size, decode_fec);
935 if (decoded_samples < 0) {
936 SET_ERRNO(ERROR_INVALID_PARAM, "Opus decoding failed: %s", opus_strerror(decoded_samples));
937 return -1;
938 }
939
940 return decoded_samples;
941}
942
947 if (g_opus_encoder) {
948 opus_encoder_destroy(g_opus_encoder);
949 g_opus_encoder = NULL;
950 }
951}
952
957 if (g_opus_decoder) {
958 opus_decoder_destroy(g_opus_decoder);
959 g_opus_decoder = NULL;
960 }
961}
962
964 g_crypto_handshake_ctx.crypto_ctx.encrypt_data = ACIP_CRYPTO_HAS_ENCRYPT(mode);
965 return acds_handshake_begin(&g_wasm_transport, mode);
966}
967asciichat_error_t client_acds_handshake_receive(uint32_t type, const void *payload, size_t length) {
968 asciichat_error_t result = acds_handshake_receive(&g_crypto_handshake_ctx, &g_wasm_transport, type, payload, length);
969 if (result != ASCIICHAT_OK) {
970 g_connection_state = CONNECTION_STATE_ERROR;
971 return result;
972 }
973 if (g_crypto_handshake_ctx.state == CRYPTO_HANDSHAKE_READY) {
974 g_handshake_complete = true;
975 g_wasm_transport.crypto_ctx = &g_crypto_handshake_ctx.crypto_ctx;
976 g_connection_state = CONNECTION_STATE_CONNECTED;
977 } else {
978 g_connection_state = CONNECTION_STATE_HANDSHAKE;
979 }
980 return ASCIICHAT_OK;
981}
982
984int client_resolve_crypto_policy(bool secure, int encryption) {
985 crypto_policy_t policy = {
986 .transport_secure = secure,
987 .encrypt = encryption == 1,
988 .no_encrypt = encryption == 0,
989 .has_password = g_crypto_password[0] != '\0',
990 .has_identity = g_has_client_identity_key,
991 .has_verification_key = g_has_expected_server_identity_key,
992 };
993 uint8_t mode;
994 if (crypto_policy_resolve(&policy, &mode) != ASCIICHAT_OK)
995 return -1;
996 g_crypto_mode = mode;
997 return mode;
998}
999
1001 g_connection_state = CONNECTION_STATE_CONNECTING;
1002 g_version_received = false;
1003 g_crypto_handshake_ctx.crypto_ctx.encrypt_data = ACIP_CRYPTO_HAS_ENCRYPT(g_crypto_mode);
1004 return acds_handshake_begin(&g_wasm_transport, g_crypto_mode);
1005}
1006
1007asciichat_error_t client_handshake_receive(uint32_t type, const void *payload, size_t length) {
1008 if (!g_version_received && type != PACKET_TYPE_PROTOCOL_VERSION)
1009 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Expected server protocol version first");
1010 if (type == PACKET_TYPE_PROTOCOL_VERSION) {
1011 if (g_version_received)
1012 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Duplicate server protocol version");
1013 if (!payload || length != sizeof(protocol_version_packet_t))
1014 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid server protocol version");
1015 const protocol_version_packet_t *version = payload;
1016 if (NET_TO_HOST_U16(version->protocol_version) != PROTOCOL_VERSION_MAJOR || version->supports_encryption != g_crypto_mode)
1017 return SET_ERRNO(ERROR_CRYPTO, "Server rejected requested security policy");
1018 g_version_received = true;
1019 if (g_crypto_mode == ACIP_CRYPTO_NONE) {
1020 g_connection_state = CONNECTION_STATE_CONNECTED;
1021 return ASCIICHAT_OK;
1022 }
1030 caps.requires_verification = g_has_expected_server_identity_key;
1031 asciichat_error_t result = packet_send_via_transport(&g_wasm_transport, PACKET_TYPE_CRYPTO_CAPABILITIES, &caps, sizeof(caps), 0);
1032 if (result != ASCIICHAT_OK || !g_has_expected_server_identity_key)
1033 return result;
1034 return packet_send_via_transport(&g_wasm_transport, PACKET_TYPE_CRYPTO_CLIENT_HELLO, g_expected_server_identity_key, sizeof(g_expected_server_identity_key), 0);
1035 }
1036 if (type == PACKET_TYPE_CRYPTO_PARAMETERS) {
1037 if (!payload || length != sizeof(crypto_parameters_packet_t) || (((const crypto_parameters_packet_t *)payload)->selected_cipher != CIPHER_ALGO_NONE) != ACIP_CRYPTO_HAS_ENCRYPT(g_crypto_mode))
1038 return SET_ERRNO(ERROR_CRYPTO, "Server changed requested payload encryption");
1039 }
1040 return client_acds_handshake_receive(type, payload, length);
1041}
1042
1043/* Browser transport marshals framing, then invokes the SAME network content
1044 * contract as native before any terminal parser or recording receives bytes. */
1045asciichat_error_t client_validate_frame(const char *text, size_t length, uint32_t width, uint32_t height, uint32_t checksum, uint32_t flags, const void *metadata, size_t metadata_length) {
1047 return SET_ERRNO(ERROR_INVALID_FRAME, "Unknown browser frame flags");
1048 acip_render_settings_t settings;
1049 uint32_t effects = 0;
1050 bool contract = (flags & FRAME_FLAG_RENDER_CONTRACT) != 0;
1051 if (contract) {
1052 size_t consumed;
1053 asciichat_error_t decoded = frame_render_contract_decode(metadata, metadata_length, &settings, &effects, &consumed);
1054 if (decoded != ASCIICHAT_OK)
1055 return decoded;
1056 if (settings.revision && settings.revision == remote_render_settings.revision && remote_render_settings.palette[0] && remote_render_settings.caps.utf8_support &&
1057 strcmp(settings.palette, remote_render_settings.palette) != 0)
1058 return SET_ERRNO(ERROR_INVALID_FRAME, "Browser frame palette contradicts acknowledged revision");
1059 if (consumed != metadata_length)
1060 return SET_ERRNO(ERROR_INVALID_FRAME, "Trailing browser frame contract bytes");
1061 } else if (metadata_length) {
1062 return SET_ERRNO(ERROR_INVALID_FRAME, "Unexpected browser frame contract");
1063 }
1064 if (!text || !length || length > FRAME_RENDER_MAX_BYTES || asciichat_crc32(text, length) != checksum)
1065 return SET_ERRNO(ERROR_INVALID_FRAME, "Browser frame length/checksum mismatch");
1066 return session_frame_validate_contract(text, length, width, height, contract ? &settings : NULL, effects);
1067}
1068
1069asciichat_error_t client_decode_frame_data(const void *encoded, size_t encoded_length, void *output, size_t output_length) {
1070 if (!encoded || !output || !encoded_length || encoded_length > FRAME_RENDER_MAX_BYTES || !output_length || output_length > FRAME_RENDER_MAX_BYTES)
1071 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid browser frame decompression bounds");
1072 return decompress_data_exact(encoded, encoded_length, output, output_length);
1073}
1074
1076 return g_handshake_complete ? &g_crypto_handshake_ctx.crypto_ctx : NULL;
1077}
asciichat_error_t acds_handshake_begin(acip_transport_t *transport, uint8_t mode)
asciichat_error_t acds_handshake_receive(crypto_handshake_context_t *ctx, acip_transport_t *transport, packet_type_t type, const void *payload, size_t length)
ANSI escape sequence utilities and fast color code generation.
void errno_destroy(void)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
#define HAS_ERRNO(context)
bool browser_transport_is_connected(browser_transport_t *browser)
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
#define buffer_release(data)
Definition buffer.h:99
Platform initialization and static synchronization helpers.
📦 Network Packet Compression Utilities
Hardware-Accelerated CRC32 Checksum Computation.
asciichat_error_t crypto_handshake_init(const char *name, crypto_handshake_context_t *ctx, bool is_server)
Initialize named crypto handshake context.
void crypto_handshake_destroy(crypto_handshake_context_t *ctx)
Cleanup crypto handshake context with secure memory wiping.
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.
#define HOST_TO_NET_U16(val)
Definition endian.h:100
#define NET_TO_HOST_U16(val)
Definition endian.h:115
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t frame_render_contract_decode(const void *wire, size_t available, acip_render_settings_t *settings, uint32_t *effects, size_t *length)
asciichat_error_t session_frame_validate_contract(const char *text, size_t length, uint32_t width, uint32_t height, const acip_render_settings_t *settings, uint32_t effects)
void ansi_fast_init(void)
Definition ansi.c:226
struct OpusDecoder OpusDecoder
struct OpusEncoder OpusEncoder
@ OPUS_APPLICATION_AUDIO
General audio (optimized for music).
Definition opus.h:86
@ OPUS_APPLICATION_VOIP
Voice over IP (optimized for speech).
Definition opus.h:85
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t decompress_data_exact(const void *input, size_t input_size, void *output, size_t expected_size)
crypto_result_t
Cryptographic operation result codes.
crypto_result_t crypto_derive_password_key(crypto_context_t *ctx, const char *password)
Derive key from password using Argon2id.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_CRYPTO_KEY
Definition error_codes.h:98
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_FRAME
@ ERROR_CRYPTO
Definition error_codes.h:97
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:78
uint8_t ed25519[ED25519_PRIVATE_KEY_SIZE]
Definition key_types.h:104
key_type_t type
Definition key_types.h:102
bool use_gpg_agent
Definition key_types.h:108
bool use_ssh_agent
Definition key_types.h:107
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:109
asciichat_error_t parse_ssh_private_key(const char *key_path, private_key_t *key_out)
Parse SSH Ed25519 private key from file.
Definition ssh_keys.c:967
key_type_t type
Definition key_types.h:77
union private_key_t::@040247167361101132173265304166142033301213115260 key
@ KEY_TYPE_ED25519
Definition key_types.h:58
void log_init(const char *filename, log_level_t level, bool force_stderr, bool use_mmap)
Initialize the logging system.
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
asciichat_error_t log_set_format(const char *format_str, bool console_only)
Set a custom log format string.
#define AUTH_ALGO_ED25519
Ed25519 authentication (Edwards-curve signatures).
Definition packet.h:1140
#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 KEX_ALGO_X25519
X25519 key exchange (Curve25519).
Definition packet.h:1139
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1167
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
#define AUTH_ALGO_NONE
No authentication (plaintext mode).
Definition packet.h:1141
#define ACIP_CRYPTO_ENCRYPT
Encryption only (no identity verification).
Definition packet.h:1158
#define CIPHER_ALGO_NONE
No encryption (plaintext mode).
Definition packet.h:1143
#define CIPHER_ALGO_XSALSA20_POLY1305
XSalsa20-Poly1305 authenticated encryption.
Definition packet.h:1142
@ PACKET_TYPE_CRYPTO_CLIENT_HELLO
Client -> Server: Expected server key fingerprint for multi-key selection (UNENCRYPTED).
Definition packet.h:328
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
Definition packet.h:317
@ PACKET_TYPE_CRYPTO_PARAMETERS
Server -> Client: Chosen algorithms + data sizes (UNENCRYPTED).
Definition packet.h:338
@ PACKET_TYPE_CRYPTO_CAPABILITIES
Client -> Server: Supported crypto algorithms (UNENCRYPTED).
Definition packet.h:336
#define GET_OPTION(field)
Definition options.h:1333
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
asciichat_error_t options_init(int argc, char **argv)
Initialize options by parsing command-line arguments with unified mode detection.
Definition options.c:2291
#define OPT_LOG_TEMPLATE_DEFAULT_DEBUG
Default log format string - debug mode (verbose with thread name, file relative path,...
Definition options.h:373
#define PROTOCOL_VERSION_MAJOR
Major protocol version number.
#define FRAME_FLAG_RENDER_CONTRACT
Explicit versioned render contract precedes encoded ASCII text.
#define FRAME_FLAG_IS_COMPRESSED
Frame data is compressed.
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
Definition crc32.h:151
int socket_t
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Client-side handshake functions.
Common declarations and data structures for cryptographic handshake.
Transport abstraction layer for ACIP protocol.
acip_transport_type_t
Transport method table (virtual function table).
@ ACIP_TRANSPORT_WEBSOCKET
WebSocket over TCP.
int platform_init(void)
Definition init.c:9
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 acip_render_settings_prepare(acip_render_settings_t *candidate, const options_t *values)
asciichat_error_t acip_settings_decode(const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length)
void acip_render_settings_options(const acip_render_settings_t *state, options_t *values)
#define WEB_CLIENT_CRYPTO_PASSWORD_BUFFER_SIZE
📝 Logging API with multiple log levels and terminal output control
Magic number validation constants and macros.
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_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 openpgp_parse_armored_seckey(const char *armored_text, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE], uint8_t ed25519_sk[ED25519_SEED_SIZE])
Definition openpgp.c:733
asciichat_error_t openpgp_parse_binary_pubkey(const uint8_t *data, size_t data_len, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
Definition openpgp.c:280
asciichat_error_t openpgp_parse_binary_seckey(const uint8_t *data, size_t data_len, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE], uint8_t ed25519_sk[ED25519_SEED_SIZE])
Definition openpgp.c:702
asciichat_error_t openpgp_parse_armored_pubkey(const char *armored_text, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
Definition openpgp.c:307
OpenPGP (RFC 4880) packet format parser.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Opus audio codec wrapper for real-time encoding/decoding.
asciichat_error_t crypto_policy_resolve(const crypto_policy_t *policy, uint8_t *mode)
Definition policy.c:4
Protocol version, features, compression, and frame flag constants.
char * options_values_json(const options_t *values, bool remote_only)
Definition rcu.c:1001
asciichat_error_t options_prepare_json(const options_t *current, const char *json, size_t length, options_t *candidate, bool remote_only)
Definition rcu.c:977
#define CRYPTO_RECORD_ANY_CHANNEL
Definition record.h:15
#define FRAME_RENDER_MAX_BYTES
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
ACIP shared/bidirectional packet sending functions.
#define CRYPTO_NONCE_SIZE
Nonce size (XSalsa20).
Definition sizes.h:83
#define CRYPTO_MAC_SIZE
MAC size (Poly1305).
Definition sizes.h:89
Client-side H.265 media capture and encoding pipeline.
asciichat_error_t client_handshake_receive(uint32_t type, const void *payload, size_t length)
int client_handle_handshake_complete(const uint8_t *packet, size_t packet_len)
asciichat_error_t client_validate_frame(const char *text, size_t length, uint32_t width, uint32_t height, uint32_t checksum, uint32_t flags, const void *metadata, size_t metadata_length)
crypto_context_t * client_security_context(void)
const uint8_t * client_public_key_bytes(void)
int client_opus_decoder_init(int sample_rate, int channels)
int client_resolve_crypto_policy(bool secure, int encryption)
asciichat_error_t client_decode_frame_data(const void *encoded, size_t encoded_length, void *output, size_t output_length)
void client_cleanup(void)
int client_set_server_address(const char *server_host, int server_port)
connection_state_t
@ CONNECTION_STATE_ERROR
@ CONNECTION_STATE_DISCONNECTED
@ CONNECTION_STATE_CONNECTED
@ CONNECTION_STATE_HANDSHAKE
@ CONNECTION_STATE_CONNECTING
EM_JS(void, js_send_raw_packet,(const uint8_t *packet_data, size_t packet_len), { if(!Module.sendPacketCallback) { console.error('[WASM] sendPacketCallback not registered - cannot send packet');return;} const packetArray=new Uint8Array(HEAPU8.buffer, packet_data, packet_len);const packetCopy=new Uint8Array(packetArray);Module.sendPacketCallback(packetCopy);})
int client_parse_gpg_private_key(const char *armored_key, uint8_t *secret_key_out)
int client_get_connection_state(void)
int client_configure_crypto_options(const char *password, const uint8_t *identity_secret_key, const uint8_t *expected_server_public_key)
int client_parse_gpg_public_key_binary(const uint8_t *key_data, size_t key_data_len, uint8_t *public_key_out)
int client_init(void)
int client_generate_keypair(void)
asciichat_error_t client_acds_handshake_begin(uint8_t mode)
void client_opus_decoder_cleanup(void)
asciichat_error_t client_settings_receive(const void *payload, size_t length)
void client_opus_encoder_cleanup(void)
int client_handle_auth_challenge(const uint8_t *packet, size_t packet_len)
asciichat_error_t client_acds_handshake_receive(uint32_t type, const void *payload, size_t length)
int client_parse_gpg_public_key(const char *armored_key, uint8_t *public_key_out)
int client_decrypt_packet(const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t plaintext_size, size_t *out_len)
int client_parse_ssh_private_key(const char *key_path, uint8_t *secret_key_out)
int client_opus_encode(const int16_t *pcm_data, int frame_size, uint8_t *opus_data, int max_opus_bytes)
char * client_settings_values(void)
int client_parse_ssh_public_key(const char *key_line, uint8_t *public_key_out)
int client_opus_decode(const uint8_t *opus_data, int opus_bytes, int16_t *pcm_data, int frame_size, int decode_fec)
int client_encrypt_packet(const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t ciphertext_size, size_t *out_len)
int client_handle_crypto_parameters(const uint8_t *packet, size_t packet_len)
int client_opus_encoder_init(int sample_rate, int channels, int bitrate)
int client_get_crypto_error_message(char *output, size_t output_size)
int client_handle_key_exchange_init(const uint8_t *packet, size_t packet_len)
asciichat_error_t client_handshake_begin(void)
int client_parse_gpg_private_key_binary(const uint8_t *key_data, size_t key_data_len, uint8_t *secret_key_out)
asciichat_error_t parse_ssh_ed25519_line(const char *line, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
Definition ssh_keys.c:165
🔤 String Manipulation and Shell Escaping Utilities
acip render settings keeps the values that move through the subsystem: caps, width,...
acip settings header keeps the values that move through the subsystem: schema, request,...
Transport method table (virtual function table).
Crypto capabilities packet structure (Packet Type 14).
Definition packet.h:1027
uint8_t preferred_auth
Preferred authentication algorithm (AUTH_ALGO_*).
Definition packet.h:1039
uint16_t supported_auth_algorithms
Supported authentication algorithms bitmask (AUTH_ALGO_*).
Definition packet.h:1031
uint16_t supported_kex_algorithms
Supported key exchange algorithms bitmask (KEX_ALGO_*).
Definition packet.h:1029
uint8_t preferred_cipher
Preferred cipher algorithm (CIPHER_ALGO_*).
Definition packet.h:1041
uint16_t supported_cipher_algorithms
Supported cipher algorithms bitmask (CIPHER_ALGO_*).
Definition packet.h:1033
uint8_t preferred_kex
Preferred key exchange algorithm (KEX_ALGO_*).
Definition packet.h:1037
uint8_t requires_verification
Server verification requirement flag (1=required, 0=optional).
Definition packet.h:1035
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
Cryptographic handshake context structure.
Crypto parameters packet structure (Packet Type 15).
Definition packet.h:1056
The useful part of crypto policy is in transport_secure, encrypt, no_encrypt, no_auth: together they ...
Definition policy.h:17
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
Private identity-key material used for server authentication and signing.
Definition key_types.h:101
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
acip_traffic_class_t acip_packet_traffic_class(uint16_t type)
Definition traffic.c:4
acip_traffic_class_t
Definition traffic.h:9
@ ACIP_DELIVERY_RELIABLE
Definition traffic.h:16
📊 String Formatting Utilities