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

Client-side handshake protocol implementation. More...

Go to the source code of this file.

Macros

#define HANDSHAKE_NONCE_PACKET_CAPACITY   256

Functions

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 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.

Detailed Description

Client-side handshake protocol implementation.

Definition in file lib/crypto/handshake/client.c.

Macro Definition Documentation

◆ HANDSHAKE_NONCE_PACKET_CAPACITY

#define HANDSHAKE_NONCE_PACKET_CAPACITY   256

Definition at line 8 of file lib/crypto/handshake/client.c.

Referenced by crypto_handshake_client_auth_response().

Function Documentation

◆ crypto_handshake_client_auth_response()

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.

Parameters
ctxHandshake context (must be in CRYPTO_HANDSHAKE_KEY_EXCHANGE state)
transportACIP transport to send on
packet_typePacket type received (should be PACKET_TYPE_CRYPTO_AUTH_CHALLENGE or PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE)
payloadReceived packet payload
payload_lenLength of received payload
Returns
ASCIICHAT_OK on success, error code on failure

Client processes server's AUTH_CHALLENGE packet and sends AUTH_RESPONSE. Generates HMAC bound to shared secret using password or client key.

Note
Packet must be received by ACIP handler before calling this function
State transition: CRYPTO_HANDSHAKE_KEY_EXCHANGE -> CRYPTO_HANDSHAKE_AUTHENTICATING

Definition at line 571 of file lib/crypto/handshake/client.c.

571 {
572 ASCII_OTEL_SCOPE(CRYPTO_HANDSHAKE_CLIENT_AUTH_RESPONSE);
573 if (!ctx || ctx->state != CRYPTO_HANDSHAKE_KEY_EXCHANGE) {
574 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid state: ctx=%p, state=%d", ctx, ctx ? ctx->state : -1);
575 }
576 if (!transport) {
577 return SET_ERRNO(ERROR_INVALID_PARAM, "transport is NULL");
578 }
579
580 // Note: Packet already received by ACIP handler
581 int result;
582
583 // If server sent HANDSHAKE_COMPLETE, authentication was skipped (client has
584 // no key)
585 if (packet_type == PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE) {
587 ctx->crypto_ctx.handshake_complete = true; // Mark crypto context as ready for rekeying
588 log_debug("Crypto handshake completed successfully (no authentication required)");
589 return ASCIICHAT_OK;
590 }
591
592 // If server sent AUTH_FAILED, client is not authorized
593 if (packet_type == PACKET_TYPE_CRYPTO_AUTH_FAILED) {
594 return SET_ERRNO(ERROR_CRYPTO, "Server rejected authentication - client key not authorized");
595 }
596
597 // Otherwise, verify packet type is AUTH_CHALLENGE
598 if (packet_type != PACKET_TYPE_CRYPTO_AUTH_CHALLENGE) {
600 "Expected AUTH_CHALLENGE, HANDSHAKE_COMPLETE, or AUTH_FAILED, "
601 "got packet type %d",
602 packet_type);
603 }
604
605 // Validate packet size using session parameters
607 if (validation_result != ASCIICHAT_OK) {
608 return validation_result;
609 }
610
611 // Parse auth requirement flags
612 uint8_t auth_flags = payload[0];
613
614 // Copy nonce to local buffer before freeing payload
615 // Use auth_challenge_size since that's what the server sent
616 // Note: auth_challenge_size is uint8_t (max 255), buffer is 256 bytes, so always sufficient
618 memcpy(nonce_buffer, payload + 1, ctx->crypto_ctx.auth_challenge_size);
619 const uint8_t *nonce = nonce_buffer;
620
621 log_debug("Server auth requirements: password=%s, client_key=%s", (auth_flags & AUTH_REQUIRE_PASSWORD) ? "required" : "no", (auth_flags & AUTH_REQUIRE_CLIENT_KEY) ? "required" : "no");
622
623 // Check if we can satisfy the server's authentication requirements
624 bool has_password = ctx->crypto_ctx.has_password;
625 bool has_client_key = (ctx->client_private_key.type == KEY_TYPE_ED25519);
626 bool password_required = (auth_flags & AUTH_REQUIRE_PASSWORD);
627 bool client_key_required = (auth_flags & AUTH_REQUIRE_CLIENT_KEY);
628
629 // Provide specific error messages based on what's missing
630 if (password_required && !has_password) {
631 if (client_key_required && !has_client_key) {
632 return SET_ERRNO(ERROR_CRYPTO, "Server requires both password and client key authentication. Please "
633 "provide --password and --key to authenticate");
634 }
635 // Prompt for password interactively
636 if (ctx->defer_password_prompt) {
638 return deferred_password_error("Server requires a password");
639 }
640 char prompted_password[PASSWORD_BUFFER_SIZE];
641 if (prompt_password("Server password required - please enter password:", prompted_password, sizeof(prompted_password)) != 0) {
642 return SET_ERRNO(ERROR_CRYPTO, "Failed to read password");
643 }
644
645 // Derive password key from prompted password
646 log_debug("Deriving key from prompted password");
647 crypto_result_t crypto_result = crypto_derive_password_key(&ctx->crypto_ctx, prompted_password);
648 sodium_memzero(prompted_password, sizeof(prompted_password));
649
650 if (crypto_result != CRYPTO_OK) {
651 return SET_ERRNO(ERROR_CRYPTO, "Failed to derive password key: %s", crypto_result_to_string(crypto_result));
652 }
653
654 // Mark that password auth is now available
655 ctx->crypto_ctx.has_password = true;
656 has_password = true; // Update flag for logic below
657 }
658
659 // Authentication response priority:
660 // NOTE: Identity verification happens during KEY_EXCHANGE phase, not
661 // AUTH_RESPONSE!
662 // 1. If server requires password → MUST send HMAC (hmac_size bytes), error if no password
663 // 2. Else if server requires identity (whitelist) → MUST send Ed25519 signature (signature_size bytes), error if no
664 // key
665 // 3. Else if client has password → send HMAC (optional password auth)
666 // 4. Else if client has SSH key → send Ed25519 signature (optional identity
667 // proof)
668 // 5. Else → no authentication available
669
670 // Clean up payload before any early returns
671 if (password_required) {
672 // Server requires password - HIGHEST PRIORITY
673 // (Identity was already verified in KEY_EXCHANGE phase if whitelist is
674 // enabled)
675 if (!has_password) {
676 return SET_ERRNO(ERROR_CRYPTO, "Server requires password authentication\n"
677 "Please provide --password for this server");
678 }
679
680 result = send_password_auth_response(ctx, transport, nonce, "required password");
681 if (result != ASCIICHAT_OK) {
682 SET_ERRNO(ERROR_NETWORK, "Failed to send password auth response");
683 return result;
684 }
685 } else if (client_key_required) {
686 // Server requires client key (whitelist) - SECOND PRIORITY
687 if (!has_client_key) {
688 return SET_ERRNO(ERROR_CRYPTO, "Server requires client key authentication (whitelist)\n"
689 "Please provide --key with your authorized Ed25519 key");
690 }
691
692 result = send_key_auth_response(ctx, transport, nonce, "required client key");
693 if (result != ASCIICHAT_OK) {
694 SET_ERRNO(ERROR_NETWORK, "Failed to send key auth response");
695 return result;
696 }
697 } else if (has_password) {
698 // No server requirements, but client has password → send HMAC + client
699 // nonce (optional)
700 result = send_password_auth_response(ctx, transport, nonce, "optional password");
701 if (result != ASCIICHAT_OK) {
702 SET_ERRNO(ERROR_NETWORK, "Failed to send password auth response");
703 return result;
704 }
705 } else if (has_client_key) {
706 // No server requirements, but client has SSH key → send Ed25519 signature +
707 // client nonce (optional)
708 result = send_key_auth_response(ctx, transport, nonce, "optional identity");
709 if (result != ASCIICHAT_OK) {
710 SET_ERRNO(ERROR_NETWORK, "Failed to send key auth response");
711 return result;
712 }
713 } else {
714 // No authentication method available
715 // Continue without authentication (server will decide if this is
716 // acceptable)
717 log_debug("No authentication credentials provided - continuing without "
718 "authentication");
719 }
720
722
723 return ASCIICHAT_OK;
724}
#define SET_ERRNO(code,...)
asciichat_error_t crypto_handshake_validate_packet_size(const crypto_handshake_context_t *ctx, uint16_t packet_type, size_t packet_size)
Validate crypto packet size based on session parameters.
unsigned char uint8_t
const char * crypto_result_to_string(crypto_result_t result)
Convert crypto result to human-readable string.
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_NETWORK
Definition error_codes.h:76
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:80
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CRYPTO
Definition error_codes.h:97
@ ERROR_INVALID_PARAM
key_type_t type
Definition key_types.h:102
@ KEY_TYPE_ED25519
Definition key_types.h:58
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
@ AUTH_FAIL_PASSWORD_REQUIRED
Server requires password but client didn't provide one.
Definition packet.h:814
@ PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE
Server -> Client: "encryption ready" (UNENCRYPTED).
Definition packet.h:352
@ PACKET_TYPE_CRYPTO_AUTH_FAILED
Server -> Client: "authentication failed" (UNENCRYPTED).
Definition packet.h:348
@ PACKET_TYPE_CRYPTO_AUTH_CHALLENGE
Server -> Client: {nonce[32]} (UNENCRYPTED).
Definition packet.h:344
int prompt_password(const char *prompt, char *password, size_t max_len)
Prompt the user for a password with secure input.
Definition password.c:15
#define AUTH_REQUIRE_PASSWORD
Server requires password authentication.
#define AUTH_REQUIRE_CLIENT_KEY
Server requires client key authentication (whitelist).
#define HANDSHAKE_NONCE_PACKET_CAPACITY
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
#define PASSWORD_BUFFER_SIZE
Password input buffer size (256 bytes).
Definition sizes.h:162

References ASCII_OTEL_SCOPE, ASCIICHAT_OK, crypto_context_t::auth_challenge_size, AUTH_FAIL_PASSWORD_REQUIRED, AUTH_REQUIRE_CLIENT_KEY, AUTH_REQUIRE_PASSWORD, crypto_handshake_context_t::client_private_key, crypto_handshake_context_t::crypto_ctx, crypto_derive_password_key(), CRYPTO_HANDSHAKE_AUTHENTICATING, CRYPTO_HANDSHAKE_KEY_EXCHANGE, CRYPTO_HANDSHAKE_READY, crypto_handshake_validate_packet_size(), CRYPTO_OK, crypto_result_to_string(), crypto_handshake_context_t::defer_password_prompt, ERROR_CRYPTO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_NETWORK, ERROR_NETWORK_PROTOCOL, crypto_context_t::handshake_complete, HANDSHAKE_NONCE_PACKET_CAPACITY, crypto_context_t::has_password, KEY_TYPE_ED25519, log_debug, PACKET_TYPE_CRYPTO_AUTH_CHALLENGE, PACKET_TYPE_CRYPTO_AUTH_FAILED, PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE, PASSWORD_BUFFER_SIZE, crypto_handshake_context_t::password_failure, prompt_password(), SET_ERRNO, crypto_handshake_context_t::state, and private_key_t::type.

Referenced by acds_handshake_receive(), acip_security_receive(), and client_handle_auth_challenge().

◆ crypto_handshake_client_complete()

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.

Parameters
ctxHandshake context (must be in CRYPTO_HANDSHAKE_AUTHENTICATING state)
transportACIP transport (unused, for consistency)
packet_typePacket type received (should be PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP or PACKET_TYPE_CRYPTO_AUTH_FAILED)
payloadReceived packet payload
payload_lenLength of received payload
Returns
ASCIICHAT_OK on success, error code on failure

Client processes server's SERVER_AUTH_RESP or AUTH_FAILED packet. After receiving, handshake is complete and encryption is ready.

Note
Packet must be received by ACIP handler before calling this function
State transition: CRYPTO_HANDSHAKE_AUTHENTICATING -> CRYPTO_HANDSHAKE_READY

Definition at line 726 of file lib/crypto/handshake/client.c.

726 {
727 ASCII_OTEL_SCOPE(CRYPTO_HANDSHAKE_CLIENT_COMPLETE);
728 // Accept both KEY_EXCHANGE and AUTHENTICATING states for simple mode compatibility
729 // In simple mode, server skips AUTH_CHALLENGE and sends HANDSHAKE_COMPLETE directly
731 SET_ERRNO(ERROR_INVALID_STATE, "Invalid state: ctx=%p, state=%d", ctx, ctx ? ctx->state : -1);
732 return ERROR_INVALID_STATE;
733 }
734 if (!transport) {
735 return SET_ERRNO(ERROR_INVALID_PARAM, "transport is NULL");
736 }
737
738 // Note: Packet already received by ACIP handler
739 (void)transport; // Unused parameter (this function only receives, doesn't send)
740
741 // Check packet type
742 if (packet_type == PACKET_TYPE_CRYPTO_AUTH_FAILED) {
743 // Parse the auth failure packet to get specific reasons
744 if (payload_len >= sizeof(auth_failure_packet_t)) {
745 auth_failure_packet_t *failure = (auth_failure_packet_t *)payload;
747 if (ctx->defer_password_prompt && failure->reason_flags && !(failure->reason_flags & ~password_flags)) {
748 ctx->password_failure = failure->reason_flags;
749 return deferred_password_error("Server rejected password");
750 }
751 NOTICE(DANGER, "AUTHENTICATION REJECTED", "%s%s%s%s%sConnection rejected.", failure->reason_flags & AUTH_FAIL_PASSWORD_INCORRECT ? "Incorrect password.\n" : "",
752 failure->reason_flags & AUTH_FAIL_PASSWORD_REQUIRED ? "Password required: use --password.\n" : "",
753 failure->reason_flags & AUTH_FAIL_CLIENT_KEY_REQUIRED ? "Client identity required: use --key.\n" : "",
754 failure->reason_flags & AUTH_FAIL_CLIENT_KEY_REJECTED ? "Client key is not authorized by the server.\n" : "",
755 failure->reason_flags & AUTH_FAIL_SIGNATURE_INVALID ? "Client signature verification failed.\n" : "");
756 SET_ERRNO(ERROR_CRYPTO_AUTH, "Server rejected authentication:");
757
759 SET_ERRNO(ERROR_CRYPTO_AUTH, " - Incorrect password");
760 }
762 SET_ERRNO(ERROR_CRYPTO_AUTH, " - Server requires a password (use --password)");
763 }
765 SET_ERRNO(ERROR_CRYPTO_AUTH, " - Server requires a whitelisted client key (use --key "
766 "with your SSH key)");
767 }
769 SET_ERRNO(ERROR_CRYPTO_AUTH, " - Your client key is not in the server's whitelist");
770 }
772 SET_ERRNO(ERROR_CRYPTO_AUTH, " - Client signature verification failed");
773 }
774
775 // Provide helpful guidance
778 SET_ERRNO(ERROR_CRYPTO_AUTH, "Hint: Server requires BOTH correct password AND "
779 "whitelisted key");
780 } else if (failure->reason_flags & AUTH_FAIL_PASSWORD_INCORRECT) {
781 SET_ERRNO(ERROR_CRYPTO_AUTH, "Hint: Check your password and try again");
782 } else if (failure->reason_flags & AUTH_FAIL_CLIENT_KEY_REQUIRED) {
783 SET_ERRNO(ERROR_CRYPTO_AUTH, "Hint: Provide your SSH key with --key ~/.ssh/id_ed25519");
784 } else if (failure->reason_flags & AUTH_FAIL_CLIENT_KEY_REJECTED) {
785 SET_ERRNO(ERROR_CRYPTO_AUTH, "Hint: Your key needs to be added to the server's whitelist");
786 }
787 }
788 } else {
789 NOTICE(DANGER, "AUTHENTICATION REJECTED", "Server rejected authentication (no details provided).\nConnection rejected.");
790 SET_ERRNO(ERROR_CRYPTO_AUTH, "Server rejected authentication (no details provided)");
791 }
793 "Server authentication failed - incorrect HMAC"); // Special code for
794 // auth failure - do
795 // not retry
796 }
797
798 // Handle no-auth flow: server sends HANDSHAKE_COMPLETE directly
799 if (packet_type == PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE) {
801 ctx->crypto_ctx.handshake_complete = true;
802 log_info("Handshake complete (no authentication required)");
803 return ASCIICHAT_OK;
804 }
805
806 // Handle with-auth flow: server sends SERVER_AUTH_RESP after authentication
807 if (packet_type != PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP) {
808 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Expected HANDSHAKE_COMPLETE, SERVER_AUTH_RESPONSE, or AUTH_FAILED, got packet type %d", packet_type);
809 }
810
811 // Verify server's HMAC for mutual authentication
812 // Use ctx->crypto_ctx.hmac_size (negotiated during handshake) rather than SERVER_AUTH_RESPONSE_SIZE constant
813 if (payload_len != ctx->crypto_ctx.hmac_size) {
814 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid SERVER_AUTH_RESPONSE size: %zu bytes (expected %u)", payload_len, ctx->crypto_ctx.hmac_size);
815 }
816
817 // Verify server's HMAC (binds to DH shared_secret to prevent MITM)
819 NOTICE(DANGER, "SERVER AUTHENTICATION FAILED", "The server returned an incorrect HMAC. This may indicate a man-in-the-middle attack.\nConnection rejected.");
821 "Server authentication failed - incorrect HMAC"); // Authentication
822 // failure - do not
823 // retry
824 }
826 ctx->crypto_ctx.handshake_complete = true;
827 log_info("Server authentication successful - mutual authentication complete");
828
829 return ASCIICHAT_OK;
830}
@ ERROR_CRYPTO_AUTH
Definition error_codes.h:99
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
@ AUTH_FAIL_PASSWORD_INCORRECT
Password verification failed (incorrect password).
Definition packet.h:816
@ AUTH_FAIL_CLIENT_KEY_REQUIRED
Server requires client key but client didn't provide one.
Definition packet.h:818
@ AUTH_FAIL_CLIENT_KEY_REJECTED
Client key not in whitelist (access denied).
Definition packet.h:820
@ AUTH_FAIL_SIGNATURE_INVALID
Client signature verification failed (invalid signature).
Definition packet.h:822
@ PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP
Server -> Client: {HMAC[32]} server proves knowledge (UNENCRYPTED).
Definition packet.h:350
bool crypto_verify_auth_response(const crypto_context_t *ctx, const uint8_t nonce[AUTH_CHALLENGE_SIZE], const uint8_t expected_hmac[HMAC_SHA256_SIZE])
#define NOTICE(severity, title,...)
Definition notice.h:56
Authentication failure packet structure.
Definition packet.h:833
uint8_t reason_flags
Bitmask of auth_failure_reason_t values indicating failure causes.
Definition packet.h:835

References ASCII_OTEL_SCOPE, ASCIICHAT_OK, AUTH_FAIL_CLIENT_KEY_REJECTED, AUTH_FAIL_CLIENT_KEY_REQUIRED, AUTH_FAIL_PASSWORD_INCORRECT, AUTH_FAIL_PASSWORD_REQUIRED, AUTH_FAIL_SIGNATURE_INVALID, crypto_handshake_context_t::client_challenge_nonce, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_AUTHENTICATING, CRYPTO_HANDSHAKE_KEY_EXCHANGE, CRYPTO_HANDSHAKE_READY, crypto_verify_auth_response(), crypto_handshake_context_t::defer_password_prompt, ERROR_CRYPTO_AUTH, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_NETWORK_PROTOCOL, crypto_context_t::handshake_complete, crypto_context_t::hmac_size, log_info, NOTICE, PACKET_TYPE_CRYPTO_AUTH_FAILED, PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE, PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP, crypto_handshake_context_t::password_failure, auth_failure_packet_t::reason_flags, SET_ERRNO, and crypto_handshake_context_t::state.

Referenced by acds_handshake_receive(), acip_security_receive(), and client_handle_handshake_complete().

◆ crypto_handshake_client_key_exchange()

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.

Parameters
ctxHandshake context (must be in CRYPTO_HANDSHAKE_INIT state)
transportACIP transport to send on
packet_typePacket type received (should be PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT)
payloadReceived packet payload
payload_lenLength of received payload
Returns
ASCIICHAT_OK on success, error code on failure

Client processes server's KEY_EXCHANGE_INIT packet and responds with KEY_EXCHANGE_RESP. Supports both simple and authenticated formats. Verifies server signature if present.

Note
Packet must be received by ACIP handler before calling this function
State transition: CRYPTO_HANDSHAKE_INIT -> CRYPTO_HANDSHAKE_KEY_EXCHANGE

Definition at line 47 of file lib/crypto/handshake/client.c.

47 {
48 ASCII_OTEL_SCOPE(CRYPTO_HANDSHAKE_CLIENT_KEY_EXCHANGE);
49 if (!ctx || ctx->state != CRYPTO_HANDSHAKE_INIT) {
50 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid state: ctx=%p, state=%d", (void *)ctx, ctx ? (int)ctx->state : -1);
51 }
52 if (!transport) {
53 return SET_ERRNO(ERROR_INVALID_PARAM, "transport is NULL");
54 }
55
56 // Note: Packet already received by ACIP handler
57 int result;
58
59 // Verify packet type
60 if (packet_type != PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT) {
61 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Expected KEY_EXCHANGE_INIT, got packet type %d", packet_type);
62 }
63
64 log_debug("CLIENT_KEY_EXCHANGE: Received packet with payload_len=%zu, kex_size=%u, auth_size=%u, sig_size=%u", payload_len, ctx->crypto_ctx.public_key_size, ctx->crypto_ctx.auth_public_key_size,
66
67 // Check payload size - only authenticated format supported
68 // Authenticated: public_key_size + auth_public_key_size + signature_size bytes
69 size_t expected_auth_size = ctx->crypto_ctx.public_key_size + ctx->crypto_ctx.auth_public_key_size + ctx->crypto_ctx.signature_size;
70
71 // Use the crypto context's public key size to ensure compatibility.
72 size_t key_size = sizeof(ctx->crypto_ctx.public_key);
73 SAFE_MALLOC_DEFER_FREE(server_ephemeral_key, key_size, uint8_t *);
74 if (!server_ephemeral_key) {
75 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate memory for server ephemeral key");
76 }
79
80 if (!server_identity_key || !server_signature) {
81 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate memory for server identity key or signature");
82 }
83
84 // Validate packet size using session parameters
86 if (validation_result != ASCIICHAT_OK) {
87 return validation_result;
88 }
89
90 // Check if server is using authenticated format (includes signature)
91 // Must have signature_size > 0 to distinguish from simple ephemeral-only format
92 // This covers both cases:
93 // 1. With identity key: ephemeral + identity + signature
94 // 2. Without identity key but with signature: ephemeral + 0 + signature
95 if (ctx->crypto_ctx.signature_size > 0 && payload_len == expected_auth_size) {
96 // Authenticated format:
97 // [ephemeral:public_key_size][identity:auth_public_key_size][signature:signature_size]
98 log_debug("Received authenticated KEY_EXCHANGE_INIT (%zu bytes)", expected_auth_size);
99 memcpy(server_ephemeral_key, payload, ctx->crypto_ctx.public_key_size);
100 memcpy(server_identity_key, payload + ctx->crypto_ctx.public_key_size, ctx->crypto_ctx.auth_public_key_size);
101 memcpy(server_signature, payload + ctx->crypto_ctx.public_key_size + ctx->crypto_ctx.auth_public_key_size, ctx->crypto_ctx.signature_size);
102
103 // Server is using client authentication
104 ctx->server_uses_client_auth = true;
105
106 // DEBUG: Print identity key received
107 char hex_id[HEX_STRING_SIZE_32];
108 for (int i = 0; i < ED25519_PUBLIC_KEY_SIZE; i++) {
109 safe_snprintf(hex_id + i * 2, 3, "%02x", server_identity_key[i]);
110 }
111 hex_id[HEX_STRING_SIZE_32 - 1] = '\0';
112 log_debug("Received identity key: %s", hex_id);
113
114 // DEBUG: Print ephemeral key and signature
115 char hex_eph[HEX_STRING_SIZE_32];
116 for (int i = 0; i < ED25519_PUBLIC_KEY_SIZE; i++) {
117 safe_snprintf(hex_eph + i * 2, 3, "%02x", server_ephemeral_key[i]);
118 }
119 hex_eph[HEX_STRING_SIZE_32 - 1] = '\0';
120 log_debug("Received ephemeral key: %s", hex_eph);
121
122 char hex_sig[HEX_STRING_SIZE_64];
123 for (int i = 0; i < ED25519_SIGNATURE_SIZE; i++) {
124 safe_snprintf(hex_sig + i * 2, 3, "%02x", server_signature[i]);
125 }
126 hex_sig[HEX_STRING_SIZE_64 - 1] = '\0';
127 log_debug("Received signature: %s", hex_sig);
128
129 // Verify signature: server identity signed the ephemeral key
130 log_debug("Verifying server's signature over ephemeral key (already logged above)");
131
132 // If client didn't specify --server-key, skip signature verification
133 // (client doesn't care about server identity verification)
134 if (!ctx->verify_server_key) {
135 log_info("Skipping server signature verification (no --server-key specified)");
136 NOTICE(DANGER, "UNVERIFIED SERVER IDENTITY", "Connection is encrypted but server identity is NOT verified (vulnerable to MITM)");
137 } else {
138 // Extract GPG key ID from expected_server_key if it's a GPG key (gpg:KEYID format)
139 const char *gpg_key_id = NULL;
140 if (ctx->expected_server_key[0] != '\0' && strncmp(ctx->expected_server_key, "gpg:", 4) == 0) {
141 const char *key_id_start = ctx->expected_server_key + 4;
142 size_t key_id_len = strlen(key_id_start);
143 // Accept 8, 16, or 40 character GPG key IDs (short, long, or full fingerprint)
144 if (key_id_len == 8 || key_id_len == 16 || key_id_len == 40) {
145 gpg_key_id = key_id_start;
146 log_debug("Using GPG key ID from --server-key for verification: %s", gpg_key_id);
147 }
148 }
149
150 if (ed25519_verify_signature(server_identity_key, server_ephemeral_key, ctx->crypto_ctx.public_key_size, server_signature, gpg_key_id) != 0) {
151 NOTICE(DANGER, "SERVER SIGNATURE INVALID",
152 "The server identity signature could not be verified.\n"
153 "Possible man-in-the-middle attack. Connection rejected.");
154 return SET_ERRNO(ERROR_CRYPTO, "Server signature verification FAILED - rejecting connection. "
155 "This indicates: Server's identity key does not "
156 "match its ephemeral key, Potential man-in-the-middle attack, "
157 "Corrupted or malicious server");
158 }
159 log_debug("Server signature verified successfully");
160 }
161
162 // Verify server identity against a browser-provided raw key before falling
163 // back to the native file, URL, and key-list parser.
165 if (sodium_memcmp(server_identity_key, ctx->expected_server_key_bytes, ED25519_PUBLIC_KEY_SIZE) != 0) {
166 NOTICE(DANGER, "SERVER IDENTITY KEY MISMATCH", "The server key does not match the browser verification key.\nConnection rejected.");
167 return SET_ERRNO(ERROR_CRYPTO_VERIFICATION, "Server identity key does not match the active browser verification key");
168 }
169 log_info("Server identity key verified against active browser key");
170 } else if (ctx->verify_server_key && strlen(ctx->expected_server_key) > 0) {
171 // Parse ALL expected server keys (github:/gitlab: may have multiple keys)
172 public_key_t expected_keys[MAX_CLIENTS];
173 size_t num_expected_keys = 0;
174 if (parse_public_keys(ctx->expected_server_key, expected_keys, &num_expected_keys, MAX_CLIENTS) != 0 || num_expected_keys == 0) {
175 return SET_ERRNO(ERROR_CONFIG,
176 "Failed to parse expected server key: %s. Check that "
177 "--server-key value is valid (ssh-ed25519 "
178 "format, github:username, or hex)",
180 }
181
182 // Compare server's IDENTITY key against ALL expected keys (match any one)
183 // This supports users with multiple SSH keys (e.g., different machines)
184 bool key_matched = false;
185 for (size_t i = 0; i < num_expected_keys; i++) {
186 if (sodium_memcmp(server_identity_key, expected_keys[i].key, ED25519_PUBLIC_KEY_SIZE) == 0) {
187 key_matched = true;
188 log_debug("Server identity key matched expected key %zu/%zu", i + 1, num_expected_keys);
189 break;
190 }
191 }
192
193 if (!key_matched) {
194 NOTICE(DANGER, "SERVER IDENTITY KEY MISMATCH",
195 "The server key does not match --server-key: %s.\n"
196 "Possible man-in-the-middle attack. Do not connect until the server identity is verified.",
198 return SET_ERRNO(ERROR_CRYPTO,
199 "Server identity key mismatch - potential MITM attack! "
200 "Expected key(s) from: %s (checked %zu keys), Server presented a different key "
201 "than specified with --server-key, DO NOT CONNECT to this "
202 "server - likely man-in-the-middle attack!",
203 ctx->expected_server_key, num_expected_keys);
204 }
205 log_info("Server identity key verified against --server-key (%zu key(s) checked)", num_expected_keys);
206 }
207
208 // Note: Server IP resolution now handled by caller (TCP transport layer)
209 // For WebSocket/WebRTC transports, server_ip should be set by the transport
210 // before handshake begins. TCP clients will use the legacy wrapper which
211 // handles this.
212 if (ctx->server_ip[0] == '\0') {
213 log_debug("Server IP not set - skipping known_hosts verification (non-TCP transport)");
214 } else {
215 log_debug("Server IP already set: %s", ctx->server_ip);
216 }
217
218 // Check known_hosts for this server (if we have server IP and port)
219 // Check if known_hosts verification should be skipped
220 bool skip_known_hosts = false;
221 const char *env_skip = platform_getenv("ASCII_CHAT_INSECURE_NO_HOST_IDENTITY_CHECK");
222 if (env_skip && strcmp(env_skip, STR_ONE) == 0) {
223 log_warn("Skipping known_hosts checking for authenticated connection (ASCII_CHAT_INSECURE_NO_HOST_IDENTITY_CHECK=1)");
224 skip_known_hosts = true;
225 }
226#ifndef NDEBUG
227 // In debug builds, also skip for Claude Code (LLM automation can't do interactive prompts)
228 else if (platform_getenv("CLAUDECODE")) {
229 log_warn("Skipping known_hosts checking (CLAUDECODE set in debug build)");
230 skip_known_hosts = true;
231 }
232#endif
233
234 if (!ctx->addressless_transport && !skip_known_hosts && ctx->server_ip[0] != '\0' && ctx->server_port > 0) {
236 bool has_stored_key = false;
237 asciichat_error_t known_host_result = check_known_host_with_key(ctx->server_ip, ctx->server_port, server_identity_key, stored_key, &has_stored_key);
238 if (known_host_result == ERROR_CRYPTO_VERIFICATION) {
239 // Key mismatch - MITM attack detected! Prompt user for confirmation
240 log_error("SECURITY: Server key does NOT match known_hosts entry!\n"
241 "This indicates a possible man-in-the-middle attack!");
242 if (!display_mitm_warning(ctx->server_ip, ctx->server_port, has_stored_key ? stored_key : NULL, server_identity_key)) {
243 // User declined to continue - ABORT connection for security
244
245 return SET_ERRNO(ERROR_CRYPTO_VERIFICATION, "SECURITY: Connection aborted - server key mismatch (possible MITM attack)");
246 }
247 // User accepted the risk - continue with connection
248 NOTICE(DANGER, "HOST KEY CHANGE ACCEPTED", "SECURITY WARNING: User accepted MITM risk - continuing with connection");
249 } else if (known_host_result == ASCIICHAT_OK) {
250 // Unknown host (first connection) - prompt user to verify fingerprint
251 if (!prompt_unknown_host(ctx->server_ip, ctx->server_port, server_identity_key)) {
252 // User declined to add host - ABORT connection
253
254 return SET_ERRNO(ERROR_CRYPTO, "User declined to verify unknown host");
255 }
256
257 // User accepted - add to known_hosts
258 if (add_known_host(ctx->server_ip, ctx->server_port, server_identity_key) != ASCIICHAT_OK) {
259 return SET_ERRNO(ERROR_CONFIG,
260 "CRITICAL SECURITY ERROR: Failed to create known_hosts "
261 "file! This is a security vulnerability - the "
262 "program cannot track known hosts. Please check file "
263 "permissions and ensure the program can write to: %s",
265 }
266 log_debug("Server host added to known_hosts successfully");
267 } else if (known_host_result == 1) {
268 // Key matches - connection is secure!
269 log_info("Server host key verified from known_hosts - connection secure");
270 } else {
271 // Unexpected error code from check_known_host
272
273 return SET_ERRNO(known_host_result, "SECURITY: known_hosts verification failed with error code %d", known_host_result);
274 }
275 }
276 public_key_t identity = {.type = KEY_TYPE_ED25519};
277 memcpy(identity.key, server_identity_key, sizeof(identity.key));
278 key_identity_announce(ctx->verify_server_key ? "VERIFIED SERVER PUBLIC IDENTITY" : "UNVERIFIED SERVER PUBLIC IDENTITY", &identity);
279 } else if (payload_len == ctx->crypto_ctx.public_key_size) {
280 if (ctx->verify_server_key) {
281 return SET_ERRNO(ERROR_CRYPTO_AUTH, "Server omitted the required identity key");
282 }
283 // Simple format: just ephemeral key (no identity key)
284 log_debug("Received simple KEY_EXCHANGE_INIT (%zu bytes) - server has no "
285 "identity key",
286 payload_len);
287 memcpy(server_ephemeral_key, payload, ctx->crypto_ctx.public_key_size);
288
289 // Clear identity key and signature for simple format
290 memset(server_identity_key, 0, ctx->crypto_ctx.auth_public_key_size);
291 memset(server_signature, 0, ctx->crypto_ctx.signature_size);
292
293 // Server is not using client authentication in simple mode
294 ctx->server_uses_client_auth = false;
295
296 log_debug("Received ephemeral key (simple format)");
297
298 if (!ctx->addressless_transport) {
299 // SECURITY: For servers without identity keys, we implement a different security model:
300 // 1. Verify IP address matches known_hosts entry
301 // 2. Always require user confirmation (no silent connections)
302 // 3. Store server fingerprint for future verification
303
304 if (ctx->server_ip[0] == '\0' || ctx->server_port <= 0) {
305 return SET_ERRNO(ERROR_CRYPTO, "Server IP or port not set, cannot check known_hosts");
306 }
307
308 // Check if this server was previously connected to (IP verification)
309 bool skip_known_hosts = false;
310 asciichat_error_t known_host_result = ASCIICHAT_OK;
311 const char *env_skip_known_hosts_checking = platform_getenv("ASCII_CHAT_INSECURE_NO_HOST_IDENTITY_CHECK");
312 if (env_skip_known_hosts_checking && strcmp(env_skip_known_hosts_checking, STR_ONE) == 0) {
313 log_warn("Skipping known_hosts checking. This is a security vulnerability.");
314 skip_known_hosts = true;
315 }
316#ifndef NDEBUG
317 // In debug builds, also skip for Claude Code (LLM automation can't do interactive prompts)
318 else if (platform_getenv("CLAUDECODE")) {
319 log_warn("Skipping known_hosts checking (CLAUDECODE set in debug build).");
320 skip_known_hosts = true;
321 }
322#endif
323 else {
324 known_host_result = check_known_host_no_identity(ctx->server_ip, ctx->server_port);
325 }
326
327 if (skip_known_hosts || known_host_result == 1) {
328 // Server IP is known and verified - allow connection without warnings
329 log_info("SECURITY: Server IP %s:%u is known (no-identity entry found) - connection verified", ctx->server_ip, ctx->server_port);
330 } else if (known_host_result == ASCIICHAT_OK) {
331 // Server IP is unknown - require user confirmation
332 NOTICE(DANGER, "UNVERIFIED SERVER IDENTITY",
333 "SECURITY: Unknown server IP %s:%u with no identity key\n"
334 "This connection is vulnerable to man-in-the-middle attacks\n"
335 "Anyone can intercept your connection and read your data",
336 ctx->server_ip, ctx->server_port);
337
339 return SET_ERRNO(ERROR_CRYPTO, "User declined to connect to unknown server without identity key");
340 }
341
342 // User accepted - add to known_hosts as no-identity entry
343 // For servers without identity keys, pass zero key to indicate no-identity
344 uint8_t zero_key[ZERO_KEY_SIZE] = {0};
345 if (add_known_host(ctx->server_ip, ctx->server_port, zero_key) != ASCIICHAT_OK) {
346 return SET_ERRNO(ERROR_CONFIG,
347 "CRITICAL SECURITY ERROR: Failed to create known_hosts "
348 "file! This is a security vulnerability - the "
349 "program cannot track known hosts. Please check file "
350 "permissions and ensure the program can write to: %s",
352 }
353 log_debug("Server host added to known_hosts successfully");
354 } else if (known_host_result == ERROR_CRYPTO_VERIFICATION) {
355 // Server previously had identity key but now has none - potential security issue
356 NOTICE(DANGER, "SERVER IDENTITY REMOVED", "SECURITY: Server previously had identity key but now has none - potential security issue");
357
358 return SET_ERRNO(ERROR_CRYPTO_VERIFICATION, "Server key configuration changed - potential security issue");
359 } else {
360 // Other error checking known_hosts (e.g., ERROR_INVALID_PARAM)
361
362 return SET_ERRNO(ERROR_CRYPTO, "Failed to verify server IP address");
363 }
364 }
365 } else {
367 "Invalid KEY_EXCHANGE_INIT size: %zu bytes (expected %zu or "
368 "%zu). This indicates: Protocol violation "
369 "or incompatible server version, Potential man-in-the-middle "
370 "attack, Network corruption",
371 payload_len, expected_auth_size, ctx->crypto_ctx.public_key_size);
372 // retry
373 }
374
375 // Set peer's public key (EPHEMERAL X25519) - this also derives the shared secret
376 crypto_result_t crypto_result = crypto_set_peer_public_key(&ctx->crypto_ctx, server_ephemeral_key);
377 if (crypto_result != CRYPTO_OK) {
378 return SET_ERRNO(ERROR_CRYPTO, "Failed to set peer public key and derive shared secret: %s", crypto_result_to_string(crypto_result));
379 }
380
381 // Determine if client has an identity key
382 bool client_has_identity_key = (ctx->client_private_key.type == KEY_TYPE_ED25519);
383
384 // Send authenticated response if server has identity key (auth_public_key_size > 0)
385 // OR if server requires client authentication (require_client_auth)
386 // Note: server_uses_client_auth is set when server has identity key, but we should
387 // send authenticated response when server has identity key regardless of client auth requirement
388 bool server_has_identity = (ctx->crypto_ctx.auth_public_key_size > 0 && ctx->crypto_ctx.signature_size > 0);
389 bool server_requires_auth = server_has_identity || ctx->require_client_auth;
390
391 if (server_requires_auth || client_has_identity_key) {
392 // Send authenticated packet:
393 // [ephemeral:kex_size][identity:auth_size][signature:sig_size][gpg_key_id_len:1][gpg_key_id:0-16]
394 // Use Ed25519 sizes since client has Ed25519 key
395 size_t ed25519_pubkey_size = ED25519_PUBLIC_KEY_SIZE; // Ed25519 public key is always 32 bytes
396 size_t ed25519_sig_size = ED25519_SIGNATURE_SIZE; // Ed25519 signature is always 64 bytes
397 size_t response_size = ctx->crypto_ctx.public_key_size + ed25519_pubkey_size + ed25519_sig_size;
398
399 // Check if client has a GPG key ID to send
400 uint8_t gpg_key_id_len = 0;
401 if (ctx->client_gpg_key_id[0] != '\0') {
402 gpg_key_id_len = (uint8_t)strlen(ctx->client_gpg_key_id);
403 if (gpg_key_id_len > 40) {
404 gpg_key_id_len = 40; // Truncate to max length (full fingerprint)
405 }
406 response_size += 1 + gpg_key_id_len; // 1 byte for length + key ID
407 } else {
408 response_size += 1; // Just the length byte (0)
409 }
410
411 SAFE_MALLOC_DEFER_FREE(key_response, response_size, uint8_t *);
412 defer(sodium_memzero(key_response, response_size));
413 size_t offset = 0;
414
415 // Copy ephemeral key
416 memcpy(key_response + offset, ctx->crypto_ctx.public_key,
417 ctx->crypto_ctx.public_key_size); // X25519 ephemeral for encryption
418 offset += ctx->crypto_ctx.public_key_size;
419
420 if (client_has_identity_key) {
421 // Client has identity key - send it with signature
422 memcpy(key_response + offset, ctx->client_private_key.public_key, ed25519_pubkey_size); // Ed25519 identity
423 offset += ed25519_pubkey_size;
424
425 // Sign ephemeral key with client identity key
426 if (ed25519_sign_message(&ctx->client_private_key, ctx->crypto_ctx.public_key, ctx->crypto_ctx.public_key_size, key_response + offset) != 0) {
427 return SET_ERRNO(ERROR_CRYPTO, "Failed to sign client ephemeral key");
428 }
429 offset += ed25519_sig_size;
430 } else {
431 // Client has no identity key - send null identity and null signature
432 memset(key_response + offset, 0, ed25519_pubkey_size); // Null identity
433 offset += ed25519_pubkey_size;
434 memset(key_response + offset, 0, ed25519_sig_size); // Null signature
435 offset += ed25519_sig_size;
436 }
437
438 // Append GPG key ID length
439 key_response[offset] = gpg_key_id_len;
440 offset += 1;
441
442 // Append GPG key ID if present
443 if (gpg_key_id_len > 0) {
444 memcpy(key_response + offset, ctx->client_gpg_key_id, gpg_key_id_len);
445 offset += gpg_key_id_len;
446 log_debug("Including client GPG key ID in KEY_EXCHANGE_RESPONSE: %.*s", gpg_key_id_len, ctx->client_gpg_key_id);
447 }
448
449 result = packet_send_via_transport(transport, PACKET_TYPE_CRYPTO_KEY_EXCHANGE_RESP, key_response, response_size, 0);
450 if (result != 0) {
451 return SET_ERRNO(ERROR_NETWORK, "Failed to send KEY_EXCHANGE_RESPONSE packet");
452 }
453
454 } else {
455 // Send X25519 encryption key only to server (no identity key)
456 // Format: [X25519 pubkey (kex_size)] = kex_size bytes total
458 if (result != 0) {
459 return SET_ERRNO(ERROR_NETWORK, "Failed to send KEY_EXCHANGE_RESPONSE packet");
460 }
461 }
462
464
465 return ASCIICHAT_OK;
466}
#define SAFE_MALLOC_DEFER_FREE(name, size, cast)
const char * get_known_hosts_path(void)
Get the path to the known_hosts file.
Definition known_hosts.c:57
crypto_result_t crypto_set_peer_public_key(crypto_context_t *ctx, const uint8_t *peer_public_key)
Set peer's public key and compute shared secret (step 2 of handshake).
bool prompt_unknown_host_no_identity(const char *server_ip, uint16_t port)
Interactive prompt for unknown host without identity key - returns true if user wants to continue,...
asciichat_error_t check_known_host_no_identity(const char *server_ip, uint16_t port)
Check known_hosts for servers without identity key (no-identity entries).
@ ERROR_CRYPTO_VERIFICATION
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_CONFIG
Definition error_codes.h:56
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:78
asciichat_error_t parse_public_keys(const char *input, public_key_t *keys_out, size_t *num_keys, size_t max_keys)
Parse all SSH/GPG public keys from any format (returns all keys).
Definition keys.c:402
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:109
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-128).
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
@ PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT
Server -> Client: {server_pubkey[32]} (UNENCRYPTED).
Definition packet.h:340
@ PACKET_TYPE_CRYPTO_KEY_EXCHANGE_RESP
Client -> Server: {client_pubkey[32]} (UNENCRYPTED).
Definition packet.h:342
#define STR_ONE
String literal: "1" (one).
bool prompt_unknown_host(const char *server_ip, uint16_t port, const uint8_t server_key[ED25519_PUBLIC_KEY_SIZE])
bool display_mitm_warning(const char *server_ip, uint16_t port, const uint8_t expected_key[ED25519_PUBLIC_KEY_SIZE], const uint8_t received_key[ED25519_PUBLIC_KEY_SIZE])
asciichat_error_t check_known_host_with_key(const char *server_ip, uint16_t port, const uint8_t server_key[ED25519_PUBLIC_KEY_SIZE], uint8_t expected_key[ED25519_PUBLIC_KEY_SIZE], bool *has_expected_key)
Definition known_hosts.c:90
asciichat_error_t add_known_host(const char *server_ip, uint16_t port, const uint8_t server_key[ED25519_PUBLIC_KEY_SIZE])
void key_identity_announce(const char *label, const public_key_t *key)
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
#define ED25519_SIGNATURE_SIZE
Ed25519 signature size in bytes.
Definition sizes.h:41
#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
#define ZERO_KEY_SIZE
Zero key array size (32 bytes, used for no-identity entries).
Definition sizes.h:164
#define HEX_STRING_SIZE_64
Hex string size for 64-byte values (128 hex chars + null terminator).
Definition sizes.h:160
asciichat_error_t ed25519_sign_message(const private_key_t *key, const uint8_t *message, size_t message_len, uint8_t signature[ED25519_SIGNATURE_SIZE])
Definition ssh_keys.c:1109
asciichat_error_t ed25519_verify_signature(const uint8_t public_key[ED25519_PUBLIC_KEY_SIZE], const uint8_t *message, size_t message_len, const uint8_t signature[ED25519_SIGNATURE_SIZE], const char *gpg_key_id)
Definition ssh_keys.c:1209
The normalized public identity used by ascii-chat's crypto and key-management code.
Definition key_types.h:76
const char * platform_getenv(const char *name)
Definition system.c:45

References add_known_host(), crypto_handshake_context_t::addressless_transport, ASCII_OTEL_SCOPE, ASCIICHAT_OK, crypto_context_t::auth_public_key_size, check_known_host_no_identity(), check_known_host_with_key(), crypto_handshake_context_t::client_gpg_key_id, crypto_handshake_context_t::client_private_key, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_INIT, CRYPTO_HANDSHAKE_KEY_EXCHANGE, crypto_handshake_validate_packet_size(), CRYPTO_OK, crypto_result_to_string(), crypto_set_peer_public_key(), display_mitm_warning(), ED25519_PUBLIC_KEY_SIZE, ed25519_sign_message(), ED25519_SIGNATURE_SIZE, ed25519_verify_signature(), ERROR_CONFIG, ERROR_CRYPTO, ERROR_CRYPTO_AUTH, ERROR_CRYPTO_VERIFICATION, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_NETWORK, ERROR_NETWORK_PROTOCOL, crypto_handshake_context_t::expected_server_key, crypto_handshake_context_t::expected_server_key_bytes, crypto_handshake_context_t::expected_server_key_bytes_set, get_known_hosts_path(), HEX_STRING_SIZE_32, HEX_STRING_SIZE_64, public_key_t::key, key_identity_announce(), KEY_TYPE_ED25519, log_debug, log_error, log_info, log_warn, MAX_CLIENTS, NOTICE, packet_send_via_transport(), PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT, PACKET_TYPE_CRYPTO_KEY_EXCHANGE_RESP, parse_public_keys(), platform_getenv(), prompt_unknown_host(), prompt_unknown_host_no_identity(), crypto_context_t::public_key, private_key_t::public_key, crypto_context_t::public_key_size, crypto_handshake_context_t::require_client_auth, SAFE_MALLOC_DEFER_FREE, crypto_handshake_context_t::server_ip, crypto_handshake_context_t::server_port, crypto_handshake_context_t::server_uses_client_auth, SET_ERRNO, crypto_context_t::signature_size, crypto_handshake_context_t::state, STR_ONE, private_key_t::type, crypto_handshake_context_t::verify_server_key, and ZERO_KEY_SIZE.

Referenced by acds_handshake_receive(), acip_security_receive(), and client_handle_key_exchange_init().