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

Go to the source code of this file.

Functions

asciichat_error_t acds_identity_generate (uint8_t public_key[32], uint8_t secret_key[64])
 Generate new Ed25519 keypair.
asciichat_error_t acds_identity_load (const char *path, uint8_t public_key[32], uint8_t secret_key[64])
 Load identity from file.
asciichat_error_t acds_identity_save (const char *path, const uint8_t public_key[32], const uint8_t secret_key[64])
 Save identity to file.
void acds_identity_fingerprint (const uint8_t public_key[32], char fingerprint[(32 *2+1)])
 Compute SHA256 fingerprint of public key.
asciichat_error_t acds_identity_default_path (char *path_out, size_t path_size)
 Get default identity file path for current platform.

Function Documentation

◆ acds_identity_default_path()

asciichat_error_t acds_identity_default_path ( char * path_out,
size_t path_size )

Get default identity file path for current platform.

Parameters
path_outOutput buffer for path (should be at least 256 bytes)
path_sizeSize of output buffer
Returns
ASCIICHAT_OK on success, error code otherwise

Returns:

  • Unix: ~/.config/ascii-chat/acds_identity
  • Windows: APPDATA%\ascii-chat\acds_identity

Definition at line 151 of file identity.c.

151 {
152 if (!path_out || path_size == 0) {
153 return SET_ERRNO(ERROR_INVALID_PARAM, "path_out cannot be NULL and path_size must be > 0");
154 }
155
156 // Get platform-specific config directory (~/.config/ascii-chat/ or %APPDATA%\ascii-chat\‍)
157 char *config_dir = platform_get_config_dir();
158 if (!config_dir) {
159 return SET_ERRNO(ERROR_CONFIG, "Failed to get config directory");
160 }
161
162 // Append "acds_identity" to config directory
163 int written = safe_snprintf(path_out, path_size, "%sacds_identity", config_dir);
164 SAFE_FREE(config_dir);
165
166 if (written < 0 || (size_t)written >= path_size) {
167 return SET_ERRNO(ERROR_CONFIG, "Path buffer too small");
168 }
169
170 return ASCIICHAT_OK;
171}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_INVALID_PARAM
char * platform_get_config_dir(void)
Definition util.c:116

References ASCIICHAT_OK, ERROR_CONFIG, ERROR_INVALID_PARAM, platform_get_config_dir(), SAFE_FREE, and SET_ERRNO.

Referenced by discovery_session_create().

◆ acds_identity_fingerprint()

void acds_identity_fingerprint ( const uint8_t public_key[32],
char fingerprint[(32 *2+1)] )

Compute SHA256 fingerprint of public key.

Parameters
public_key32-byte Ed25519 public key
fingerprintOutput buffer for 65 bytes (64 hex chars + null terminator)

Definition at line 131 of file identity.c.

131 {
132 if (!public_key || !fingerprint) {
133 log_error("NULL parameters");
134 return;
135 }
136
137 // Compute SHA256 hash of public key
138 uint8_t hash[crypto_hash_sha256_BYTES];
139 if (key_fingerprint_digest(public_key, hash) != ASCIICHAT_OK) {
140 fingerprint[0] = '\0';
141 return;
142 }
143
144 // Convert to hex string
145 for (int i = 0; i < 32; i++) {
146 safe_snprintf(&fingerprint[i * 2], 3, "%02x", hash[i]);
147 }
148 fingerprint[64] = '\0';
149}
unsigned char uint8_t
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
asciichat_error_t key_fingerprint_digest(const uint8_t public_key[ED25519_PUBLIC_KEY_SIZE], uint8_t digest[KEYMASK_DIGEST_SIZE])
Definition keymask.c:11

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, HEX_STRING_SIZE_32, key_fingerprint_digest(), and log_error.

Referenced by discovery_session_create().

◆ acds_identity_generate()

asciichat_error_t acds_identity_generate ( uint8_t public_key[32],
uint8_t secret_key[64] )

Generate new Ed25519 keypair.

Parameters
public_keyOutput buffer for 32-byte public key
secret_keyOutput buffer for 64-byte secret key
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 22 of file identity.c.

22 {
23 if (!public_key || !secret_key) {
24 return SET_ERRNO(ERROR_INVALID_PARAM, "public_key and secret_key cannot be NULL");
25 }
26
27 // Generate Ed25519 keypair using libsodium
28 if (crypto_sign_keypair(public_key, secret_key) != 0) {
29 return SET_ERRNO(ERROR_CRYPTO, "Failed to generate Ed25519 keypair");
30 }
31
32 log_debug("Generated new Ed25519 identity keypair");
33 return ASCIICHAT_OK;
34}
@ ERROR_CRYPTO
Definition error_codes.h:97
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589

References ASCIICHAT_OK, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, ERROR_CRYPTO, ERROR_INVALID_PARAM, log_debug, and SET_ERRNO.

Referenced by discovery_session_create().

◆ acds_identity_load()

asciichat_error_t acds_identity_load ( const char * path,
uint8_t public_key[32],
uint8_t secret_key[64] )

Load identity from file.

Parameters
pathPath to identity file (e.g., ~/.config/ascii-chat/acds_identity)
public_keyOutput buffer for 32-byte public key
secret_keyOutput buffer for 64-byte secret key
Returns
ASCIICHAT_OK on success, ERROR_CONFIG if file doesn't exist

Definition at line 36 of file identity.c.

36 {
37 if (!path || !public_key || !secret_key) {
38 return SET_ERRNO(ERROR_INVALID_PARAM, "path, public_key, and secret_key cannot be NULL");
39 }
40
41 // Open file for reading
42 FILE *fp = platform_fopen("file_stream", path, "rb");
43 if (!fp) {
44 if (errno == ENOENT) {
45 return SET_ERRNO(ERROR_CONFIG, "Identity file does not exist: %s", path);
46 }
47 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open identity file: %s", path);
48 }
49
50 // Preserve the original raw libsodium identity format while accepting the
51 // OpenSSH Ed25519 private keys used by the rest of ascii-chat.
52 uint8_t raw_secret[crypto_sign_SECRETKEYBYTES];
53 const size_t raw_bytes = fread(raw_secret, 1, sizeof(raw_secret), fp);
54 const int trailing_byte = fgetc(fp);
55 fclose(fp);
56 if (raw_bytes == sizeof(raw_secret) && trailing_byte == EOF) {
57 uint8_t derived_public[crypto_sign_PUBLICKEYBYTES];
58 if (crypto_sign_ed25519_sk_to_pk(derived_public, raw_secret) != 0) {
59 sodium_memzero(raw_secret, sizeof(raw_secret));
60 return SET_ERRNO(ERROR_CONFIG, "Identity file contains an invalid Ed25519 secret key: %s", path);
61 }
62 memcpy(secret_key, raw_secret, sizeof(raw_secret));
63 memcpy(public_key, derived_public, sizeof(derived_public));
64 sodium_memzero(raw_secret, sizeof(raw_secret));
65 log_info("Loaded raw Ed25519 identity from %s", path);
66 return ASCIICHAT_OK;
67 }
68 sodium_memzero(raw_secret, sizeof(raw_secret));
69
70 private_key_t parsed_key = {0};
71 asciichat_error_t parse_result = parse_ssh_private_key(path, &parsed_key);
72 if (parse_result != ASCIICHAT_OK) {
73 sodium_memzero(&parsed_key, sizeof(parsed_key));
74 return parse_result;
75 }
76 if (parsed_key.type != KEY_TYPE_ED25519) {
77 sodium_memzero(&parsed_key, sizeof(parsed_key));
78 return SET_ERRNO(ERROR_CONFIG, "Discovery identity key must be Ed25519: %s", path);
79 }
80
81 memcpy(secret_key, parsed_key.key.ed25519, crypto_sign_SECRETKEYBYTES);
82 memcpy(public_key, parsed_key.public_key, crypto_sign_PUBLICKEYBYTES);
83 sodium_memzero(&parsed_key, sizeof(parsed_key));
84 log_info("Loaded OpenSSH Ed25519 identity from %s", path);
85 return ASCIICHAT_OK;
86}
#define SET_ERRNO_SYS(code,...)
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Definition filesystem.c:18
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
uint8_t ed25519[ED25519_PRIVATE_KEY_SIZE]
Definition key_types.h:104
key_type_t type
Definition key_types.h:102
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:109
asciichat_error_t parse_ssh_private_key(const char *key_path, private_key_t *key_out)
Parse SSH Ed25519 private key from file.
Definition ssh_keys.c:967
union private_key_t::@040247167361101132173265304166142033301213115260 key
@ KEY_TYPE_ED25519
Definition key_types.h:58
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
Private identity-key material used for server authentication and signing.
Definition key_types.h:101

References ASCIICHAT_OK, private_key_t::ed25519, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, ERROR_CONFIG, ERROR_INVALID_PARAM, private_key_t::key, KEY_TYPE_ED25519, log_info, parse_ssh_private_key(), platform_fopen(), private_key_t::public_key, SET_ERRNO, SET_ERRNO_SYS, and private_key_t::type.

Referenced by discovery_session_create().

◆ acds_identity_save()

asciichat_error_t acds_identity_save ( const char * path,
const uint8_t public_key[32],
const uint8_t secret_key[64] )

Save identity to file.

Parameters
pathPath to identity file
public_key32-byte public key
secret_key64-byte secret key
Returns
ASCIICHAT_OK on success, error code otherwise

Definition at line 88 of file identity.c.

88 {
89 if (!path || !public_key || !secret_key) {
90 return SET_ERRNO(ERROR_INVALID_PARAM, "path, public_key, and secret_key cannot be NULL");
91 }
92
93 // Extract directory path and create all parent directories
94 char dir_path[PLATFORM_MAX_PATH_LENGTH];
95 SAFE_STRNCPY(dir_path, path, sizeof(dir_path));
96
97 // Find last directory separator
98 char *last_sep = strrchr(dir_path, '/');
99 if (!last_sep) {
100 last_sep = strrchr(dir_path, '\\');
101 }
102
103 if (last_sep) {
104 *last_sep = '\0';
105
106 // Create directory recursively (mkdir -p equivalent)
107 asciichat_error_t result = platform_mkdir_recursive(dir_path, 0700);
108 if (result != ASCIICHAT_OK) {
109 return result;
110 }
111 }
112
113 // Open file for writing (mode 0600 = owner read/write only)
114 int fd = platform_open("identity_file", path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
115 if (fd < 0) {
116 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to create identity file: %s", path);
117 }
118
119 // Write secret key (64 bytes)
120 ssize_t written = write(fd, secret_key, 64);
121 close(fd);
122
123 if (written != 64) {
124 return SET_ERRNO(ERROR_CONFIG, "Failed to write identity file (wrote %zd/64 bytes): %s", written, path);
125 }
126
127 log_info("Saved identity to %s", path);
128 return ASCIICHAT_OK;
129}
int platform_mkdir_recursive(const char *path, int mode)
Definition filesystem.c:36
int platform_open(const char *name, const char *pathname, int flags,...)
Definition filesystem.c:65
#define SAFE_STRNCPY(dst, src, size)
#define PLATFORM_MAX_PATH_LENGTH

References ASCIICHAT_OK, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, ERROR_CONFIG, ERROR_INVALID_PARAM, log_info, PLATFORM_MAX_PATH_LENGTH, platform_mkdir_recursive(), platform_open(), SAFE_STRNCPY, SET_ERRNO, and SET_ERRNO_SYS.

Referenced by discovery_session_create().