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

Go to the source code of this file.

Data Structures

struct  ssh_private_key_parse_t

Macros

#define CRYPTO_SSH_SSH_KEYS_CIPHERNAME_BUFFER_SIZE   32
#define CRYPTO_SSH_SSH_KEYS_KDFNAME_BUFFER_SIZE   32
#define SSH_PASSWORD_INPUT_BUFFER_SIZE   1024
#define SSH_BCRYPT_SALT_SIZE   16

Functions

asciichat_error_t parse_ssh_ed25519_line (const char *line, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
asciichat_error_t parse_ssh_private_key (const char *key_path, private_key_t *key_out)
 Parse SSH Ed25519 private key from file.
asciichat_error_t ed25519_to_x25519_public (const uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE], uint8_t x25519_pk[X25519_KEY_SIZE])
asciichat_error_t ed25519_to_x25519_private (const uint8_t ed25519_sk[ED25519_PRIVATE_KEY_SIZE], uint8_t x25519_sk[X25519_KEY_SIZE])
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])
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)
SSH Key Parsing
asciichat_error_t validate_ssh_key_file (const char *key_path)
 Validate SSH key file permissions and format.

Macro Definition Documentation

◆ CRYPTO_SSH_SSH_KEYS_CIPHERNAME_BUFFER_SIZE

#define CRYPTO_SSH_SSH_KEYS_CIPHERNAME_BUFFER_SIZE   32

Definition at line 8 of file ssh_keys.c.

◆ CRYPTO_SSH_SSH_KEYS_KDFNAME_BUFFER_SIZE

#define CRYPTO_SSH_SSH_KEYS_KDFNAME_BUFFER_SIZE   32

Definition at line 9 of file ssh_keys.c.

◆ SSH_BCRYPT_SALT_SIZE

#define SSH_BCRYPT_SALT_SIZE   16

Definition at line 15 of file ssh_keys.c.

◆ SSH_PASSWORD_INPUT_BUFFER_SIZE

#define SSH_PASSWORD_INPUT_BUFFER_SIZE   1024

Definition at line 12 of file ssh_keys.c.

Function Documentation

◆ ed25519_sign_message()

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 at line 1109 of file ssh_keys.c.

1109 {
1110 if (!key || !message || !signature) {
1111 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters: key=%p, message=%p, signature=%p", key, message, signature);
1112 }
1113
1114 if (key->type != KEY_TYPE_ED25519) {
1115 return SET_ERRNO(ERROR_CRYPTO_KEY, "Key is not an Ed25519 key");
1116 }
1117
1118#ifndef EMSCRIPTEN_BUILD
1119 // If using GPG agent, delegate signing to GPG agent
1120 if (key->use_gpg_agent) {
1121 log_debug("Using GPG agent for Ed25519 signing (keygrip: %.40s)", key->gpg_keygrip);
1122
1123 // Connect to GPG agent
1124 int agent_sock = gpg_agent_connect();
1125 log_debug("GPG agent connection result: %d", agent_sock);
1126
1127 if (agent_sock < 0) {
1128 log_debug("GPG agent not available, falling back to gpg --detach-sign for signing");
1129
1130 // Extract GPG key ID from key comment ("GPG key <key_id>")
1131 const char *key_id_prefix = "GPG key ";
1132 const char *key_id = NULL;
1133 if (strncmp(key->key_comment, key_id_prefix, strlen(key_id_prefix)) == 0) {
1134 key_id = key->key_comment + strlen(key_id_prefix);
1135 }
1136
1137 log_debug("Extracted GPG key ID: %s (len=%zu)", key_id ? key_id : "NULL", key_id ? strlen(key_id) : 0);
1138
1139 if (!key_id || strlen(key_id) != 16) {
1140 return SET_ERRNO(ERROR_CRYPTO, "Cannot extract GPG key ID from comment for fallback signing");
1141 }
1142
1143 // Use gpg --detach-sign fallback
1144 log_debug("Calling gpg_sign_detached_ed25519 with key %s", key_id);
1145 int result = gpg_sign_detached_ed25519(key_id, message, message_len, signature);
1146 log_debug("gpg_sign_detached_ed25519 result: %d", result);
1147
1148 if (result != 0) {
1149 return SET_ERRNO(ERROR_CRYPTO, "GPG fallback signing failed (both agent and gpg command failed)");
1150 }
1151
1152 log_debug("Successfully signed message with gpg --detach-sign fallback (64 bytes)");
1153 return ASCIICHAT_OK;
1154 }
1155
1156 log_debug("Using GPG agent for signing");
1157
1158 // Sign the message using GPG agent
1159 size_t sig_len = 0;
1160 int result = gpg_agent_sign(agent_sock, key->gpg_keygrip, message, message_len, signature, &sig_len);
1161
1162 // Disconnect from agent
1163 gpg_agent_disconnect(agent_sock);
1164
1165 if (result != 0) {
1166 return SET_ERRNO(ERROR_CRYPTO, "GPG agent signing failed");
1167 }
1168
1169 if (sig_len != 64) {
1170 return SET_ERRNO(ERROR_CRYPTO, "GPG agent returned invalid signature length: %zu (expected 64)", sig_len);
1171 }
1172
1173 log_debug("Successfully signed message with GPG agent (64 bytes)");
1174 return ASCIICHAT_OK;
1175 }
1176
1177 // If using SSH agent, delegate signing to SSH agent
1178 if (key->use_ssh_agent) {
1179 log_debug("Using SSH agent for Ed25519 signing");
1180
1181 // Create a public_key_t from the stored public key
1182 public_key_t pub_key = {0};
1183 pub_key.type = KEY_TYPE_ED25519;
1184 memcpy(pub_key.key, key->public_key, ED25519_PUBLIC_KEY_SIZE);
1185
1186 // Sign using SSH agent
1187 asciichat_error_t result = ssh_agent_sign(&pub_key, message, message_len, signature);
1188 if (result != ASCIICHAT_OK) {
1189 return result; // Error already set by ssh_agent_sign
1190 }
1191
1192 log_debug("Successfully signed message with SSH agent (64 bytes)");
1193 return ASCIICHAT_OK;
1194 }
1195#else
1196 if (key->use_gpg_agent || key->use_ssh_agent) {
1197 return SET_ERRNO(ERROR_NOT_SUPPORTED, "External SSH and GPG agents are not available in the browser");
1198 }
1199#endif
1200
1201 // Sign the message with Ed25519 (in-memory key)
1202 if (crypto_sign_detached(signature, NULL, message, message_len, key->key.ed25519) != 0) {
1203 return SET_ERRNO(ERROR_CRYPTO, "Failed to sign message with Ed25519");
1204 }
1205
1206 return ASCIICHAT_OK;
1207}
#define SET_ERRNO(code,...)
int gpg_agent_connect(void)
Connect to gpg-agent.
Definition agent.c:103
int gpg_agent_sign(int handle_as_int, const char *keygrip, const uint8_t *message, size_t message_len, uint8_t *signature_out, size_t *signature_len_out)
Sign a message using GPG agent.
Definition agent.c:161
int gpg_sign_detached_ed25519(const char *key_id, const uint8_t *message, size_t message_len, uint8_t signature_out[ED25519_SIGNATURE_SIZE])
Sign message with GPG and extract raw Ed25519 signature.
Definition signing.c:144
void gpg_agent_disconnect(int handle_as_int)
Disconnect from gpg-agent.
Definition agent.c:153
asciichat_error_t ssh_agent_sign(const public_key_t *public_key, const uint8_t *message, size_t message_len, uint8_t signature[64])
Sign data using SSH agent with the specified public key.
Definition ssh_agent.c:260
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_CRYPTO_KEY
Definition error_codes.h:98
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CRYPTO
Definition error_codes.h:97
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:78
uint8_t ed25519[ED25519_PRIVATE_KEY_SIZE]
Definition key_types.h:104
key_type_t type
Definition key_types.h:102
bool use_gpg_agent
Definition key_types.h:108
bool use_ssh_agent
Definition key_types.h:107
char key_comment[MAX_COMMENT_LEN]
Definition key_types.h:110
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:109
char gpg_keygrip[MAX_GPG_KEYGRIP_LEN]
Definition key_types.h:111
key_type_t type
Definition key_types.h:77
union private_key_t::@040247167361101132173265304166142033301213115260 key
@ KEY_TYPE_ED25519
Definition key_types.h:58
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:37
The normalized public identity used by ascii-chat's crypto and key-management code.
Definition key_types.h:76

References ASCIICHAT_OK, private_key_t::ed25519, ED25519_PUBLIC_KEY_SIZE, ED25519_SIGNATURE_SIZE, ERROR_CRYPTO, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, gpg_agent_connect(), gpg_agent_disconnect(), gpg_agent_sign(), private_key_t::gpg_keygrip, gpg_sign_detached_ed25519(), private_key_t::key, public_key_t::key, private_key_t::key_comment, KEY_TYPE_ED25519, log_debug, private_key_t::public_key, SET_ERRNO, ssh_agent_sign(), private_key_t::type, public_key_t::type, private_key_t::use_gpg_agent, and private_key_t::use_ssh_agent.

Referenced by crypto_handshake_client_key_exchange(), and crypto_handshake_server_start().

◆ ed25519_to_x25519_private()

asciichat_error_t ed25519_to_x25519_private ( const uint8_t ed25519_sk[ED25519_PRIVATE_KEY_SIZE],
uint8_t x25519_sk[X25519_KEY_SIZE] )

Definition at line 1092 of file ssh_keys.c.

1092 {
1093 if (!ed25519_sk || !x25519_sk) {
1094 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters: ed25519_sk=%p, x25519_sk=%p", ed25519_sk, x25519_sk);
1095 }
1096
1097 // Convert Ed25519 private key to X25519 private key
1098 if (crypto_sign_ed25519_sk_to_curve25519(x25519_sk, ed25519_sk) != 0) {
1099 return SET_ERRNO(ERROR_CRYPTO_KEY, "Failed to convert Ed25519 private key to X25519");
1100 }
1101
1102 return ASCIICHAT_OK;
1103}

References ASCIICHAT_OK, ED25519_PRIVATE_KEY_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, SET_ERRNO, and X25519_KEY_SIZE.

Referenced by private_key_to_x25519().

◆ ed25519_to_x25519_public()

asciichat_error_t ed25519_to_x25519_public ( const uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE],
uint8_t x25519_pk[X25519_KEY_SIZE] )

Definition at line 1079 of file ssh_keys.c.

1079 {
1080 if (!ed25519_pk || !x25519_pk) {
1081 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters: ed25519_pk=%p, x25519_pk=%p", ed25519_pk, x25519_pk);
1082 }
1083
1084 // Convert Ed25519 public key to X25519 public key
1085 if (crypto_sign_ed25519_pk_to_curve25519(x25519_pk, ed25519_pk) != 0) {
1086 return SET_ERRNO(ERROR_CRYPTO_KEY, "Failed to convert Ed25519 public key to X25519");
1087 }
1088
1089 return ASCIICHAT_OK;
1090}

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, SET_ERRNO, and X25519_KEY_SIZE.

Referenced by public_key_to_x25519().

◆ ed25519_verify_signature()

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 at line 1209 of file ssh_keys.c.

1210 {
1211 if (!public_key || !message || !signature) {
1212 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters: public_key=%p, message=%p, signature=%p", public_key, message, signature);
1213 }
1214
1215 // Try standard Ed25519 verification first
1216 if (crypto_sign_verify_detached(signature, message, message_len, public_key) == 0) {
1217 return ASCIICHAT_OK;
1218 }
1219
1220#ifndef EMSCRIPTEN_BUILD
1221 // If standard verification fails, try GPG fallback (for GPG-signed messages)
1222 // Use provided gpg_key_id if available, otherwise check environment variable (for tests)
1223 const char *key_id_to_use = gpg_key_id;
1224 size_t key_id_len = key_id_to_use ? strlen(key_id_to_use) : 0;
1225 // Accept 8, 16, or 40 character GPG key IDs (short, long, or full fingerprint)
1226 bool valid_key_id = (key_id_len == 8 || key_id_len == 16 || key_id_len == 40);
1227
1228 if (!valid_key_id) {
1229 key_id_to_use = platform_getenv("TEST_GPG_KEY_ID");
1230 key_id_len = key_id_to_use ? strlen(key_id_to_use) : 0;
1231 valid_key_id = (key_id_len == 8 || key_id_len == 16 || key_id_len == 40);
1232 }
1233
1234 if (valid_key_id) {
1235 log_debug("Standard Ed25519 verification failed, trying GPG fallback with key %s", key_id_to_use);
1236 int gpg_result = gpg_verify_detached_ed25519(key_id_to_use, message, message_len, signature);
1237 if (gpg_result == 0) {
1238 log_debug("GPG verification succeeded");
1239 return ASCIICHAT_OK;
1240 }
1241 log_debug("GPG verification also failed");
1242 }
1243#endif
1244
1245 return SET_ERRNO(ERROR_CRYPTO, "Ed25519 signature verification failed (tried both libsodium and GPG)");
1246}
int gpg_verify_detached_ed25519(const char *key_id, const uint8_t *message, size_t message_len, const uint8_t signature[ED25519_SIGNATURE_SIZE])
Verify Ed25519 signature using GPG binary.
const char * platform_getenv(const char *name)
Definition system.c:45

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ED25519_SIGNATURE_SIZE, ERROR_CRYPTO, ERROR_INVALID_PARAM, gpg_verify_detached_ed25519(), log_debug, platform_getenv(), and SET_ERRNO.

Referenced by crypto_handshake_client_key_exchange(), crypto_handshake_server_auth_challenge(), and crypto_handshake_server_complete().

◆ parse_ssh_ed25519_line()

asciichat_error_t parse_ssh_ed25519_line ( const char * line,
uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE] )

Definition at line 165 of file ssh_keys.c.

165 {
166 if (!line || !ed25519_pk) {
167 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters: line=%p, ed25519_pk=%p", line, ed25519_pk);
168 }
169
170 // Use regex to parse and extract SSH public key components
171 char *base64_key = NULL, *comment = NULL;
172 if (!crypto_regex_match_public_key(line, &base64_key, &comment)) {
173 return SET_ERRNO(ERROR_CRYPTO_KEY, "SSH key line does not match expected format");
174 }
175
176 defer(SAFE_FREE(base64_key));
177 defer(SAFE_FREE(comment));
178
179 // Base64 decode
180 uint8_t *blob;
181 size_t blob_len;
182 if (base64_decode_ssh_key(base64_key, strlen(base64_key), &blob, &blob_len) != 0) {
183 return SET_ERRNO(ERROR_CRYPTO_KEY, "Failed to decode SSH key base64 data");
184 }
185
186 defer(SAFE_FREE(blob));
187
188 // Parse SSH key blob structure:
189 // [4 bytes: length of "ssh-ed25519"]
190 // [11 bytes: "ssh-ed25519"]
191 // [4 bytes: length of public key (32)]
192 // [32 bytes: Ed25519 public key]
193
194 if (blob_len < SSH_KEY_HEADER_SIZE) {
195 return SET_ERRNO(ERROR_CRYPTO_KEY, "SSH key blob too small: %zu bytes (expected at least %d)", blob_len, SSH_KEY_HEADER_SIZE);
196 }
197
198 // Extract Ed25519 public key (last 32 bytes)
199 memcpy(ed25519_pk, blob + blob_len - ED25519_PUBLIC_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE);
200
201 return ASCIICHAT_OK;
202}
#define SAFE_FREE(ptr)
unsigned char uint8_t
#define SSH_KEY_HEADER_SIZE
bool crypto_regex_match_public_key(const char *line, char **base64_key_out, char **comment_out)
Definition regex.c:175

References ASCIICHAT_OK, crypto_regex_match_public_key(), ED25519_PUBLIC_KEY_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, SAFE_FREE, SET_ERRNO, and SSH_KEY_HEADER_SIZE.

Referenced by browser_client_parse_ssh_public_key(), client_parse_ssh_public_key(), and parse_public_key().