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

Go to the source code of this file.

Macros

#define WEB_CLIENT_CRYPTO_PASSWORD_BUFFER_SIZE   256

Enumerations

enum  connection_state_t {
  CONNECTION_STATE_DISCONNECTED = 0 , CONNECTION_STATE_CONNECTING = 1 , CONNECTION_STATE_HANDSHAKE = 2 , CONNECTION_STATE_CONNECTED = 3 ,
  CONNECTION_STATE_ERROR = 4
}

Functions

asciichat_error_t client_settings_receive (const void *payload, size_t length)
char * client_settings_values (void)
 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_configure_crypto_options (const char *password, const uint8_t *identity_secret_key, const uint8_t *expected_server_public_key)
int client_parse_ssh_private_key (const char *key_path, uint8_t *secret_key_out)
int client_parse_ssh_public_key (const char *key_line, uint8_t *public_key_out)
int client_parse_gpg_public_key (const char *armored_key, uint8_t *public_key_out)
int client_parse_gpg_public_key_binary (const uint8_t *key_data, size_t key_data_len, uint8_t *public_key_out)
int client_parse_gpg_private_key (const char *armored_key, uint8_t *secret_key_out)
int client_parse_gpg_private_key_binary (const uint8_t *key_data, size_t key_data_len, uint8_t *secret_key_out)
int client_get_crypto_error_message (char *output, size_t output_size)
int client_init (void)
void client_cleanup (void)
int client_generate_keypair (void)
int client_set_server_address (const char *server_host, int server_port)
const uint8_t * client_public_key_bytes (void)
int client_handle_key_exchange_init (const uint8_t *packet, size_t packet_len)
int client_handle_crypto_parameters (const uint8_t *packet, size_t packet_len)
int client_handle_auth_challenge (const uint8_t *packet, size_t packet_len)
int client_handle_handshake_complete (const uint8_t *packet, size_t packet_len)
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_decrypt_packet (const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t plaintext_size, size_t *out_len)
int client_get_connection_state (void)
int client_opus_encoder_init (int sample_rate, int channels, int bitrate)
int client_opus_decoder_init (int sample_rate, int channels)
int client_opus_encode (const int16_t *pcm_data, int frame_size, uint8_t *opus_data, int max_opus_bytes)
int client_opus_decode (const uint8_t *opus_data, int opus_bytes, int16_t *pcm_data, int frame_size, int decode_fec)
void client_opus_encoder_cleanup (void)
void client_opus_decoder_cleanup (void)
asciichat_error_t client_acds_handshake_begin (uint8_t mode)
asciichat_error_t client_acds_handshake_receive (uint32_t type, const void *payload, size_t length)
int client_resolve_crypto_policy (bool secure, int encryption)
asciichat_error_t client_handshake_begin (void)
asciichat_error_t client_handshake_receive (uint32_t type, const void *payload, size_t length)
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)
asciichat_error_t client_decode_frame_data (const void *encoded, size_t encoded_length, void *output, size_t output_length)
crypto_context_t * client_security_context (void)

Macro Definition Documentation

◆ WEB_CLIENT_CRYPTO_PASSWORD_BUFFER_SIZE

#define WEB_CLIENT_CRYPTO_PASSWORD_BUFFER_SIZE   256

Definition at line 4 of file src/web/client.c.

Enumeration Type Documentation

◆ connection_state_t

Enumerator
CONNECTION_STATE_DISCONNECTED 
CONNECTION_STATE_CONNECTING 
CONNECTION_STATE_HANDSHAKE 
CONNECTION_STATE_CONNECTED 
CONNECTION_STATE_ERROR 

Definition at line 174 of file src/web/client.c.

Function Documentation

◆ client_acds_handshake_begin()

asciichat_error_t client_acds_handshake_begin ( uint8_t mode)

Definition at line 963 of file src/web/client.c.

963 {
964 g_crypto_handshake_ctx.crypto_ctx.encrypt_data = ACIP_CRYPTO_HAS_ENCRYPT(mode);
965 return acds_handshake_begin(&g_wasm_transport, mode);
966}
asciichat_error_t acds_handshake_begin(acip_transport_t *transport, uint8_t mode)
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1167

References acds_handshake_begin(), and ACIP_CRYPTO_HAS_ENCRYPT.

◆ client_acds_handshake_receive()

asciichat_error_t client_acds_handshake_receive ( uint32_t type,
const void * payload,
size_t length )

Definition at line 967 of file src/web/client.c.

967 {
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}
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)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50

References acds_handshake_receive(), ASCIICHAT_OK, CONNECTION_STATE_CONNECTED, CONNECTION_STATE_ERROR, CONNECTION_STATE_HANDSHAKE, and CRYPTO_HANDSHAKE_READY.

Referenced by client_handshake_receive().

◆ client_cleanup()

void client_cleanup ( void )

Definition at line 417 of file src/web/client.c.

417 {
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}
void errno_destroy(void)
void crypto_handshake_destroy(crypto_handshake_context_t *ctx)
Cleanup crypto handshake context with secure memory wiping.
acip render settings keeps the values that move through the subsystem: caps, width,...

References CONNECTION_STATE_DISCONNECTED, crypto_handshake_destroy(), and errno_destroy().

◆ client_configure_crypto_options()

int client_configure_crypto_options ( const char * password,
const uint8_t * identity_secret_key,
const uint8_t * expected_server_public_key )

Configure browser-provided authentication material for the next handshake.

Definition at line 211 of file src/web/client.c.

211 {
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}
#define SET_ERRNO(code,...)
#define SAFE_STRNCPY(dst, src, size)
unsigned char uint8_t
@ ERROR_INVALID_STATE
@ ERROR_INVALID_PARAM

References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, SAFE_STRNCPY, and SET_ERRNO.

◆ client_decode_frame_data()

asciichat_error_t client_decode_frame_data ( const void * encoded,
size_t encoded_length,
void * output,
size_t output_length )

Definition at line 1069 of file src/web/client.c.

1069 {
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}
asciichat_error_t decompress_data_exact(const void *input, size_t input_size, void *output, size_t expected_size)
@ ERROR_INVALID_FRAME
#define FRAME_RENDER_MAX_BYTES

References decompress_data_exact(), ERROR_INVALID_FRAME, FRAME_RENDER_MAX_BYTES, and SET_ERRNO.

◆ client_decrypt_packet()

int client_decrypt_packet ( const uint8_t * ciphertext,
size_t ciphertext_len,
uint8_t * plaintext,
size_t plaintext_size,
size_t * out_len )

Decrypt a ciphertext packet

Parameters
ciphertextInput ciphertext data
ciphertext_lenLength of ciphertext
plaintextOutput buffer (allocated by caller, must be at least ciphertext_len)
plaintext_sizeSize of plaintext buffer
out_lenOutput parameter for plaintext length
Returns
0 on success, -1 on error

Definition at line 792 of file src/web/client.c.

792 {
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}
#define buffer_release(data)
Definition buffer.h:99
unsigned long long uint64_t
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)
#define CRYPTO_RECORD_ANY_CHANNEL
Definition record.h:15
@ ACIP_DELIVERY_RELIABLE
Definition traffic.h:16

References ACIP_DELIVERY_RELIABLE, acip_security_decrypt(), ASCIICHAT_OK, buffer_release, CRYPTO_RECORD_ANY_CHANNEL, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

◆ client_encrypt_packet()

int client_encrypt_packet ( const uint8_t * plaintext,
size_t plaintext_len,
uint8_t * ciphertext,
size_t ciphertext_size,
size_t * out_len )

Encrypt a plaintext packet

Parameters
plaintextInput plaintext data
plaintext_lenLength of plaintext
ciphertextOutput buffer (allocated by caller, must be at least plaintext_len + crypto_aead_overhead)
ciphertext_sizeSize of ciphertext buffer
out_lenOutput parameter for ciphertext length
Returns
0 on success, -1 on error

Definition at line 754 of file src/web/client.c.

754 {
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}
#define NET_TO_HOST_U16(val)
Definition endian.h:115
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)
#define CRYPTO_NONCE_SIZE
Nonce size (XSalsa20).
Definition sizes.h:83
#define CRYPTO_MAC_SIZE
MAC size (Poly1305).
Definition sizes.h:89
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
acip_traffic_class_t acip_packet_traffic_class(uint16_t type)
Definition traffic.c:4
acip_traffic_class_t
Definition traffic.h:9

References ACIP_DELIVERY_RELIABLE, acip_packet_traffic_class(), acip_security_seal(), ASCIICHAT_OK, buffer_release, CRYPTO_MAC_SIZE, CRYPTO_NONCE_SIZE, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, NET_TO_HOST_U16, and SET_ERRNO.

◆ client_generate_keypair()

int client_generate_keypair ( void )

Generate client keypair for handshake

Returns
0 on success, -1 on error

Definition at line 445 of file src/web/client.c.

445 {
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}
asciichat_error_t crypto_handshake_init(const char *name, crypto_handshake_context_t *ctx, bool is_server)
Initialize named crypto handshake context.

References ASCIICHAT_OK, CONNECTION_STATE_DISCONNECTED, crypto_handshake_destroy(), CRYPTO_HANDSHAKE_DISABLED, crypto_handshake_init(), ERROR_INVALID_STATE, and SET_ERRNO.

◆ client_get_connection_state()

int client_get_connection_state ( void )

Get current connection state

Returns
Connection state enum value

Definition at line 825 of file src/web/client.c.

825 {
826 return (int)g_connection_state;
827}

◆ client_get_crypto_error_message()

int client_get_crypto_error_message ( char * output,
size_t output_size )

Copy the most recent C crypto error into a caller-provided buffer.

Definition at line 363 of file src/web/client.c.

363 {
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}
#define HAS_ERRNO(context)

References asciichat_error_context_t::context_message, ERROR_INVALID_PARAM, HAS_ERRNO, and SET_ERRNO.

◆ client_handle_auth_challenge()

int client_handle_auth_challenge ( const uint8_t * packet,
size_t packet_len )

Handle CRYPTO_AUTH_CHALLENGE packet from server

Parameters
packetRaw packet data including header
packet_lenTotal packet length
Returns
0 on success, -1 on error

Definition at line 649 of file src/web/client.c.

649 {
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}
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
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.

References ASCIICHAT_OK, buffer_alloc(), CONNECTION_STATE_ERROR, crypto_handshake_client_auth_response(), ERROR_INVALID_PARAM, and SET_ERRNO.

◆ client_handle_crypto_parameters()

int client_handle_crypto_parameters ( const uint8_t * packet,
size_t packet_len )

Handle CRYPTO_PARAMETERS packet from server Sets up the key size negotiation before KEY_EXCHANGE_INIT

Parameters
packetRaw packet data including header
packet_lenTotal packet length
Returns
0 on success, -1 on error

Definition at line 584 of file src/web/client.c.

584 {
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}
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.
Crypto parameters packet structure (Packet Type 15).
Definition packet.h:1056

References ASCIICHAT_OK, buffer_alloc(), buffer_release, CONNECTION_STATE_ERROR, crypto_handshake_destroy(), CRYPTO_HANDSHAKE_DISABLED, CRYPTO_HANDSHAKE_INIT, crypto_handshake_init(), crypto_handshake_set_parameters(), ERROR_INVALID_PARAM, and SET_ERRNO.

◆ client_handle_handshake_complete()

int client_handle_handshake_complete ( const uint8_t * packet,
size_t packet_len )

Handle CRYPTO_HANDSHAKE_COMPLETE packet from server

Parameters
packetRaw packet data including header
packet_lenTotal packet length
Returns
0 on success, -1 on error

Definition at line 695 of file src/web/client.c.

695 {
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}
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.

References ASCIICHAT_OK, buffer_alloc(), CONNECTION_STATE_CONNECTED, CONNECTION_STATE_ERROR, crypto_handshake_client_complete(), ERROR_INVALID_PARAM, and SET_ERRNO.

◆ client_handle_key_exchange_init()

int client_handle_key_exchange_init ( const uint8_t * packet,
size_t packet_len )

Handle CRYPTO_KEY_EXCHANGE_INIT packet from server This is the first step of the crypto handshake

Parameters
packetRaw packet data including header
packet_lenTotal packet length
Returns
0 on success, -1 on error

Definition at line 514 of file src/web/client.c.

514 {
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}
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.

References ASCIICHAT_OK, buffer_alloc(), CONNECTION_STATE_ERROR, CONNECTION_STATE_HANDSHAKE, crypto_handshake_client_key_exchange(), crypto_handshake_destroy(), CRYPTO_HANDSHAKE_DISABLED, CRYPTO_HANDSHAKE_INIT, crypto_handshake_init(), ERROR_INVALID_PARAM, and SET_ERRNO.

◆ client_handshake_begin()

asciichat_error_t client_handshake_begin ( void )

Definition at line 1000 of file src/web/client.c.

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

References acds_handshake_begin(), ACIP_CRYPTO_HAS_ENCRYPT, and CONNECTION_STATE_CONNECTING.

◆ client_handshake_receive()

asciichat_error_t client_handshake_receive ( uint32_t type,
const void * payload,
size_t length )

Definition at line 1007 of file src/web/client.c.

1007 {
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}
#define HOST_TO_NET_U16(val)
Definition endian.h:100
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ERROR_CRYPTO
Definition error_codes.h:97
#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 AUTH_ALGO_NONE
No authentication (plaintext mode).
Definition packet.h:1141
#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 PROTOCOL_VERSION_MAJOR
Major protocol version number.
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
asciichat_error_t client_acds_handshake_receive(uint32_t type, const void *payload, size_t length)
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
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_AUTH, ACIP_CRYPTO_HAS_ENCRYPT, ACIP_CRYPTO_NONE, ASCIICHAT_OK, AUTH_ALGO_ED25519, AUTH_ALGO_NONE, CIPHER_ALGO_NONE, CIPHER_ALGO_XSALSA20_POLY1305, client_acds_handshake_receive(), CONNECTION_STATE_CONNECTED, ERROR_CRYPTO, ERROR_NETWORK_PROTOCOL, HOST_TO_NET_U16, KEX_ALGO_X25519, NET_TO_HOST_U16, packet_send_via_transport(), PACKET_TYPE_CRYPTO_CAPABILITIES, PACKET_TYPE_CRYPTO_CLIENT_HELLO, PACKET_TYPE_CRYPTO_PARAMETERS, PACKET_TYPE_PROTOCOL_VERSION, crypto_capabilities_packet_t::preferred_auth, crypto_capabilities_packet_t::preferred_cipher, crypto_capabilities_packet_t::preferred_kex, protocol_version_packet_t::protocol_version, PROTOCOL_VERSION_MAJOR, crypto_capabilities_packet_t::requires_verification, SET_ERRNO, crypto_capabilities_packet_t::supported_auth_algorithms, crypto_capabilities_packet_t::supported_cipher_algorithms, crypto_capabilities_packet_t::supported_kex_algorithms, and protocol_version_packet_t::supports_encryption.

◆ client_init()

int client_init ( void )

Initialize client mode with native defaults.

Returns
0 on success, -1 on error

Definition at line 383 of file src/web/client.c.

383 {
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}
void ansi_fast_init(void)
Definition ansi.c:226
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 GET_OPTION(field)
Definition options.h:1333
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
int platform_init(void)
Definition init.c:9

References ansi_fast_init(), ASCIICHAT_OK, CONNECTION_STATE_DISCONNECTED, GET_OPTION, log_info, log_init(), log_set_format(), OPT_LOG_TEMPLATE_DEFAULT_DEBUG, options_init(), and platform_init().

◆ client_opus_decode()

int client_opus_decode ( const uint8_t * opus_data,
int opus_bytes,
int16_t * pcm_data,
int frame_size,
int decode_fec )

Decode Opus audio to PCM

Parameters
opus_dataInput Opus data
opus_bytesLength of Opus data
pcm_dataOutput buffer for PCM samples (Int16)
frame_sizeMaximum number of samples per channel
decode_fecWhether to use FEC
Returns
Number of samples decoded per channel, or -1 on error

Definition at line 923 of file src/web/client.c.

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

References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

◆ client_opus_decoder_cleanup()

void client_opus_decoder_cleanup ( void )

Cleanup Opus decoder

Definition at line 956 of file src/web/client.c.

956 {
957 if (g_opus_decoder) {
958 opus_decoder_destroy(g_opus_decoder);
959 g_opus_decoder = NULL;
960 }
961}

◆ client_opus_decoder_init()

int client_opus_decoder_init ( int sample_rate,
int channels )

Initialize Opus decoder

Parameters
sample_rateSample rate (8000, 16000, 24000, or 48000)
channelsNumber of channels (1=mono, 2=stereo)
Returns
0 on success, -1 on error

Definition at line 870 of file src/web/client.c.

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

References ERROR_INVALID_PARAM, and SET_ERRNO.

◆ client_opus_encode()

int client_opus_encode ( const int16_t * pcm_data,
int frame_size,
uint8_t * opus_data,
int max_opus_bytes )

Encode PCM audio to Opus

Parameters
pcm_dataInput PCM data (Int16 samples)
frame_sizeNumber of samples per channel
opus_dataOutput buffer for Opus data
max_opus_bytesMaximum size of output buffer
Returns
Number of bytes encoded, or -1 on error

Definition at line 894 of file src/web/client.c.

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

References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

◆ client_opus_encoder_cleanup()

void client_opus_encoder_cleanup ( void )

Cleanup Opus encoder

Definition at line 946 of file src/web/client.c.

946 {
947 if (g_opus_encoder) {
948 opus_encoder_destroy(g_opus_encoder);
949 g_opus_encoder = NULL;
950 }
951}

◆ client_opus_encoder_init()

int client_opus_encoder_init ( int sample_rate,
int channels,
int bitrate )

Initialize Opus encoder

Parameters
sample_rateSample rate (8000, 16000, 24000, or 48000)
channelsNumber of channels (1=mono, 2=stereo)
bitrateTarget bitrate in bits/sec
Returns
0 on success, -1 on error

Definition at line 840 of file src/web/client.c.

840 {
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}
@ 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

References ERROR_INVALID_PARAM, OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, and SET_ERRNO.

◆ client_parse_gpg_private_key()

int client_parse_gpg_private_key ( const char * armored_key,
uint8_t * secret_key_out )

Parse an unencrypted armored OpenPGP Ed25519 private key through the shared C parser.

Definition at line 303 of file src/web/client.c.

303 {
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}
@ ERROR_CRYPTO_KEY
Definition error_codes.h:98
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

References ASCIICHAT_OK, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, openpgp_parse_armored_seckey(), and SET_ERRNO.

◆ client_parse_gpg_private_key_binary()

int client_parse_gpg_private_key_binary ( const uint8_t * key_data,
size_t key_data_len,
uint8_t * secret_key_out )

Parse an unencrypted binary OpenPGP Ed25519 secret-key packet stream.

Definition at line 333 of file src/web/client.c.

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

References ASCIICHAT_OK, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, openpgp_parse_binary_seckey(), and SET_ERRNO.

◆ client_parse_gpg_public_key()

int client_parse_gpg_public_key ( const char * armored_key,
uint8_t * public_key_out )

Parse an armored OpenPGP Ed25519 public key through the shared C parser.

Definition at line 285 of file src/web/client.c.

285 {
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}
asciichat_error_t openpgp_parse_armored_pubkey(const char *armored_text, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
Definition openpgp.c:307

References ASCIICHAT_OK, ERROR_INVALID_PARAM, openpgp_parse_armored_pubkey(), and SET_ERRNO.

◆ client_parse_gpg_public_key_binary()

int client_parse_gpg_public_key_binary ( const uint8_t * key_data,
size_t key_data_len,
uint8_t * public_key_out )

Parse a binary OpenPGP Ed25519 public-key packet stream.

Definition at line 294 of file src/web/client.c.

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

References ASCIICHAT_OK, ERROR_INVALID_PARAM, openpgp_parse_binary_pubkey(), and SET_ERRNO.

◆ client_parse_ssh_private_key()

int client_parse_ssh_private_key ( const char * key_path,
uint8_t * secret_key_out )

Parse an OpenSSH Ed25519 private-key file through the shared SSH parser.

Definition at line 252 of file src/web/client.c.

252 {
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}
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
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
union private_key_t::@040247167361101132173265304166142033301213115260 key
@ KEY_TYPE_ED25519
Definition key_types.h:58
Private identity-key material used for server authentication and signing.
Definition key_types.h:101

References ASCIICHAT_OK, private_key_t::ed25519, ERROR_INVALID_PARAM, private_key_t::key, KEY_TYPE_ED25519, parse_ssh_private_key(), SET_ERRNO, private_key_t::type, private_key_t::use_gpg_agent, and private_key_t::use_ssh_agent.

◆ client_parse_ssh_public_key()

int client_parse_ssh_public_key ( const char * key_line,
uint8_t * public_key_out )

Parse an OpenSSH Ed25519 public-key line through the shared SSH parser.

Definition at line 276 of file src/web/client.c.

276 {
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}
asciichat_error_t parse_ssh_ed25519_line(const char *line, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
Definition ssh_keys.c:165

References ASCIICHAT_OK, ERROR_INVALID_PARAM, parse_ssh_ed25519_line(), and SET_ERRNO.

◆ client_public_key_bytes()

const uint8_t * client_public_key_bytes ( void )

Definition at line 499 of file src/web/client.c.

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

References CRYPTO_HANDSHAKE_DISABLED, ERROR_INVALID_STATE, and SET_ERRNO.

◆ client_resolve_crypto_policy()

int client_resolve_crypto_policy ( bool secure,
int encryption )

Resolve browser inputs through the same policy as native and mobile clients.

Definition at line 984 of file src/web/client.c.

984 {
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}
asciichat_error_t crypto_policy_resolve(const crypto_policy_t *policy, uint8_t *mode)
Definition policy.c:4
The useful part of crypto policy is in transport_secure, encrypt, no_encrypt, no_auth: together they ...
Definition policy.h:17

References ASCIICHAT_OK, and crypto_policy_resolve().

◆ client_security_context()

crypto_context_t * client_security_context ( void )

Definition at line 1075 of file src/web/client.c.

1075 {
1076 return g_handshake_complete ? &g_crypto_handshake_ctx.crypto_ctx : NULL;
1077}

◆ client_set_server_address()

int client_set_server_address ( const char * server_host,
int server_port )

Set server address for known_hosts verification

Parameters
server_hostServer hostname or IP address
server_portServer port number
Returns
0 on success, -1 on error

Definition at line 481 of file src/web/client.c.

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

References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, SAFE_STRNCPY, and SET_ERRNO.

◆ client_settings_receive()

asciichat_error_t client_settings_receive ( const void * payload,
size_t length )

Effective remote state is separate from this participant's saved preferences.

Definition at line 21 of file src/web/client.c.

21 {
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}
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
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)
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
acip settings header keeps the values that move through the subsystem: schema, request,...

References acip_render_settings_options(), acip_render_settings_prepare(), acip_settings_decode(), ASCIICHAT_OK, options_prepare_json(), SAFE_FREE, and SAFE_MALLOC.

◆ client_settings_values()

char * client_settings_values ( void )

Definition at line 41 of file src/web/client.c.

41 {
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}
char * options_values_json(const options_t *values, bool remote_only)
Definition rcu.c:1001

References acip_render_settings_options(), options_values_json(), SAFE_FREE, and SAFE_MALLOC.

◆ client_validate_frame()

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 )

Definition at line 1045 of file src/web/client.c.

1045 {
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}
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)
unsigned int uint32_t
#define FRAME_FLAG_RENDER_CONTRACT
Explicit versioned render contract precedes encoded ASCII text.
#define FRAME_FLAG_IS_COMPRESSED
Frame data is compressed.
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
Definition crc32.h:151

References asciichat_crc32, ASCIICHAT_OK, ERROR_INVALID_FRAME, FRAME_FLAG_IS_COMPRESSED, FRAME_FLAG_RENDER_CONTRACT, frame_render_contract_decode(), FRAME_RENDER_MAX_BYTES, height, acip_render_settings_t::palette, acip_render_settings_t::revision, session_frame_validate_contract(), SET_ERRNO, and width.

◆ EM_JS()

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);}  )