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/client/crypto.c
Go to the documentation of this file.
3#include "crypto.h"
4#include "server.h"
5#include <options/options.h>
12
13/* Security storage and initialization belong to the shared connection owner.
14 * This module retains CLI discovery identity verification. */
15asciichat_error_t client_crypto_init(client_connection_t *connection, const char *password) {
16 asciichat_error_t result = network_connection_prepare_security(connection->network, false, password);
17 if (result != ASCIICHAT_OK)
18 return result;
19 connection->crypto_ctx = network_connection_handshake(connection->network);
20 connection->crypto_mode = network_connection_crypto_mode(connection->network);
21 result = client_connection_checkpoint("crypto");
22 if (result != ASCIICHAT_OK)
23 return result;
24 // If --require-client-verify is set, perform ACDS session lookup for server identity
25 if (GET_OPTION(require_client_verify) && strlen(GET_OPTION(session_string)) > 0) {
26 log_debug("--require-client-verify enabled: performing ACDS session lookup for '%s'", GET_OPTION(session_string));
27
28 // Connect to the discovery service URL; an explicit URL port overrides the default.
29 acds_client_config_t acds_config;
31 char discovery_url[URL_BUFFER_SIZE];
32 const char *discovery_service = GET_OPTION(discovery_server);
33 if (GET_OPTION(discovery_port) != ACIP_DISCOVERY_DEFAULT_PORT) {
34 asciichat_error_t override_result = connection_endpoint_override_port(discovery_service, (uint16_t)GET_OPTION(discovery_port), discovery_url, sizeof(discovery_url));
35 if (override_result != ASCIICHAT_OK)
36 return SET_ERRNO(ERROR_USAGE, "Invalid --discovery-service URL for --discovery-service-port override");
37 discovery_service = discovery_url;
38 }
39 SAFE_STRNCPY(acds_config.server_address, discovery_service, sizeof(acds_config.server_address));
41 acds_config.timeout_ms = 5 * MS_PER_SEC_INT;
42 acds_config.identity_key = GET_OPTION(encrypt_key);
43
44 // ACDS key verification (optional in debug builds, only if --discovery-service-key is provided)
45 if (strlen(GET_OPTION(discovery_service_key)) > 0) {
46 connection_endpoint_t endpoint = {0};
47 asciichat_error_t endpoint_result = connection_endpoint_resolve(acds_config.server_address, acds_config.server_port, &endpoint);
48 if (endpoint_result != ASCIICHAT_OK)
49 return SET_ERRNO(ERROR_USAGE, "Invalid --discovery-service URL");
50 log_debug("Verifying ACDS server key for %s...", endpoint.host);
52 asciichat_error_t verify_result = discovery_keys_verify(endpoint.host, GET_OPTION(discovery_service_key), acds_pubkey);
53 if (verify_result != ASCIICHAT_OK) {
54 NOTICE(DANGER, "ACDS VERIFICATION FAILED", "ACDS key verification failed for %s", endpoint.host);
55 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Cannot verify discovery host identity");
56 }
57 log_debug("ACDS server key verified successfully");
58 }
59#ifndef NDEBUG
60 else {
61 log_debug("Skipping ACDS key verification (debug build, no --discovery-service-key provided)");
62 }
63#endif
64
65 acds_client_t acds_client;
66 asciichat_error_t acds_result = acds_client_connect(&acds_client, &acds_config);
67 if (acds_result != ASCIICHAT_OK) {
68 log_error("Failed to connect to ACDS server at %s", acds_config.server_address);
69 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Cannot verify discovery host identity");
70 }
71
72 // Perform SESSION_LOOKUP to get server's identity
73 acds_session_lookup_result_t lookup_result;
74 acds_result = acds_session_lookup(&acds_client, GET_OPTION(session_string), &lookup_result);
75 acds_client_disconnect(&acds_client);
76
77 if (acds_result != ASCIICHAT_OK || !lookup_result.found) {
78 log_error("ACDS session lookup failed for '%s': %s", GET_OPTION(session_string), lookup_result.found ? "session not found" : "lookup error");
79 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Cannot verify discovery host identity");
80 }
81
82 // Convert server's Ed25519 public key (32 bytes) to hex string
83 char server_key_hex[HEX_STRING_SIZE_32]; // 32 bytes * 2 + null terminator
84 for (size_t i = 0; i < 32; i++) {
85 SAFE_SNPRINTF(&server_key_hex[i * 2], 3, "%02x", lookup_result.host_pubkey[i]);
86 }
87 server_key_hex[64] = '\0';
88
89 // Set expected server key for verification during handshake
90 connection->crypto_ctx->verify_server_key = true;
91 SAFE_STRNCPY(connection->crypto_ctx->expected_server_key, server_key_hex, sizeof(connection->crypto_ctx->expected_server_key) - 1);
92 log_debug("ACDS session lookup succeeded - server identity will be verified");
93 log_debug("Expected server key (from ACDS): %s", server_key_hex);
94 }
95
96 return ASCIICHAT_OK;
97}
ACIP client-side protocol library and ACDS client connection API.
void acds_client_config_init_defaults(acds_client_config_t *config)
Initialize ACDS client configuration with defaults.
asciichat_error_t acds_session_lookup(acds_client_t *client, const char *name, acds_session_lookup_result_t *result)
Look up session by string.
asciichat_error_t acds_client_connect(acds_client_t *client, const acds_client_config_t *config)
Connect to ACDS server.
void acds_client_disconnect(acds_client_t *client)
Disconnect from ACDS server.
#define SET_ERRNO(code,...)
asciichat_error_t connection_endpoint_resolve(const char *input, uint16_t default_port, connection_endpoint_t *endpoint_out)
For connection endpoint, this is the live record that ties protocol, port, has_explicit_port together...
asciichat_error_t connection_endpoint_override_port(const char *input, uint16_t port, char *output, size_t output_size)
Shared endpoint normalization for TCP and WebSocket connections.
asciichat_error_t discovery_keys_verify(const char *acds_server, const char *key_spec, uint8_t pubkey_out[32])
Verify and load ACDS server public key.
Discovery Server Public Key Trust Management.
🔄 Network byte order conversion helpers
unsigned short uint16_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CRYPTO_AUTH
Definition error_codes.h:99
@ ERROR_USAGE
Definition error_codes.h:52
#define URL_BUFFER_SIZE
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define ACIP_DISCOVERY_DEFAULT_PORT
Discovery server default port (use OPT_ACDS_PORT_INT_DEFAULT from options.h).
Definition acds.h:1102
#define MS_PER_SEC_INT
Definition time.h:180
#define GET_OPTION(field)
Definition options.h:1333
Client-side handshake functions.
asciichat_error_t network_connection_prepare_security(network_connection_t *c, bool server, const char *password)
crypto_handshake_context_t * network_connection_handshake(network_connection_t *c)
uint8_t network_connection_crypto_mode(network_connection_t *c)
#define NOTICE(severity, title,...)
Definition notice.h:56
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:37
#define HEX_STRING_SIZE_32
Hex string size for 32-byte values (64 hex chars + null terminator).
Definition sizes.h:158
asciichat_error_t client_connection_checkpoint(const char *stage)
struct client_connection client_connection_t
asciichat_error_t client_crypto_init(client_connection_t *connection, const char *password)
ascii-chat Client Server Connection Management Interface
acds client config is intentionally a small, concrete record. Fields such as server_port,...
char server_address[256]
ACDS server address (e.g., "discovery.ascii.chat" or "127.0.0.1").
uint32_t timeout_ms
Connection timeout in milliseconds.
uint16_t server_port
ACDS server port (default: 27225).
const char * identity_key
Optional private identity key path, borrowed during connect.
ACDS client connection handle.
Session lookup result.
bool found
Session exists.
uint8_t host_pubkey[32]
Host's Ed25519 public key.
network_connection_t * network
crypto_handshake_context_t * crypto_ctx