ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
identity.h 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 172 of file identity.c.

172 {
173 if (!path_out || path_size == 0) {
175 "path_out cannot be NULL and path_size must be > 0");
176 }
177
178 // Get platform-specific config directory (~/.config/ascii-chat/ or
179 // %APPDATA%\ascii-chat\‍)
180 char *config_dir = platform_get_config_dir();
181 if (!config_dir) {
182 return SET_ERRNO(ERROR_CONFIG, "Failed to get config directory");
183 }
184
185 // Append "acds_identity" to config directory
186 int written =
187 safe_snprintf(path_out, path_size, "%sacds_identity", config_dir);
188 SAFE_FREE(config_dir);
189
190 if (written < 0 || (size_t)written >= path_size) {
191 return SET_ERRNO(ERROR_CONFIG, "Path buffer too small");
192 }
193
194 return ASCIICHAT_OK;
195}
#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 150 of file identity.c.

152 {
153 if (!public_key || !fingerprint) {
154 log_error("NULL parameters");
155 return;
156 }
157
158 // Compute SHA256 hash of public key
159 uint8_t hash[crypto_hash_sha256_BYTES];
160 if (key_fingerprint_digest(public_key, hash) != ASCIICHAT_OK) {
161 fingerprint[0] = '\0';
162 return;
163 }
164
165 // Convert to hex string
166 for (int i = 0; i < 32; i++) {
167 safe_snprintf(&fingerprint[i * 2], 3, "%02x", hash[i]);
168 }
169 fingerprint[64] = '\0';
170}
unsigned char uint8_t
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
asciichat_error_t key_fingerprint_digest(const uint8_t public_key[ED25519_PUBLIC_KEY_SIZE], uint8_t digest[KEYMASK_DIGEST_SIZE])
Definition keymask.c:12

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 23 of file identity.c.

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

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 40 of file identity.c.

42 {
43 if (!path || !public_key || !secret_key) {
45 "path, public_key, and secret_key cannot be NULL");
46 }
47
48 // Open file for reading
49 FILE *fp = platform_fopen("file_stream", path, "rb");
50 if (!fp) {
51 if (errno == ENOENT) {
52 return SET_ERRNO(ERROR_CONFIG, "Identity file does not exist: %s", path);
53 }
54 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open identity file: %s",
55 path);
56 }
57
58 // Preserve the original raw libsodium identity format while accepting the
59 // OpenSSH Ed25519 private keys used by the rest of ascii-chat.
60 uint8_t raw_secret[crypto_sign_SECRETKEYBYTES];
61 const size_t raw_bytes = fread(raw_secret, 1, sizeof(raw_secret), fp);
62 const int trailing_byte = fgetc(fp);
63 fclose(fp);
64 if (raw_bytes == sizeof(raw_secret) && trailing_byte == EOF) {
65 uint8_t derived_public[crypto_sign_PUBLICKEYBYTES];
66 if (crypto_sign_ed25519_sk_to_pk(derived_public, raw_secret) != 0) {
67 sodium_memzero(raw_secret, sizeof(raw_secret));
68 return SET_ERRNO(
70 "Identity file contains an invalid Ed25519 secret key: %s", path);
71 }
72 memcpy(secret_key, raw_secret, sizeof(raw_secret));
73 memcpy(public_key, derived_public, sizeof(derived_public));
74 sodium_memzero(raw_secret, sizeof(raw_secret));
75 log_info("Loaded raw Ed25519 identity from %s", path);
76 return ASCIICHAT_OK;
77 }
78 sodium_memzero(raw_secret, sizeof(raw_secret));
79
80 private_key_t parsed_key = {0};
81 asciichat_error_t parse_result = parse_ssh_private_key(path, &parsed_key);
82 if (parse_result != ASCIICHAT_OK) {
83 sodium_memzero(&parsed_key, sizeof(parsed_key));
84 return parse_result;
85 }
86 if (parsed_key.type != KEY_TYPE_ED25519) {
87 sodium_memzero(&parsed_key, sizeof(parsed_key));
88 return SET_ERRNO(ERROR_CONFIG, "Discovery identity key must be Ed25519: %s",
89 path);
90 }
91
92 memcpy(secret_key, parsed_key.key.ed25519, crypto_sign_SECRETKEYBYTES);
93 memcpy(public_key, parsed_key.public_key, crypto_sign_PUBLICKEYBYTES);
94 sodium_memzero(&parsed_key, sizeof(parsed_key));
95 log_info("Loaded OpenSSH Ed25519 identity from %s", path);
96 return ASCIICHAT_OK;
97}
#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:116
key_type_t type
Definition key_types.h:114
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:124
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:1156
union private_key_t::@040247167361101132173265304166142033301213115260 key
@ KEY_TYPE_ED25519
Definition key_types.h:61
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
Private identity-key material used for server authentication and signing.
Definition key_types.h:113

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 100 of file identity.c.

102 {
103 if (!path || !public_key || !secret_key) {
105 "path, public_key, and secret_key cannot be NULL");
106 }
107
108 // Extract directory path and create all parent directories
109 char dir_path[PLATFORM_MAX_PATH_LENGTH];
110 SAFE_STRNCPY(dir_path, path, sizeof(dir_path));
111
112 // Find last directory separator
113 char *last_sep = strrchr(dir_path, '/');
114 if (!last_sep) {
115 last_sep = strrchr(dir_path, '\\');
116 }
117
118 if (last_sep) {
119 *last_sep = '\0';
120
121 // Create directory recursively (mkdir -p equivalent)
122 asciichat_error_t result = platform_mkdir_recursive(dir_path, 0700);
123 if (result != ASCIICHAT_OK) {
124 return result;
125 }
126 }
127
128 // Open file for writing (mode 0600 = owner read/write only)
129 int fd =
130 platform_open("identity_file", path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
131 if (fd < 0) {
132 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to create identity file: %s",
133 path);
134 }
135
136 // Write secret key (64 bytes)
137 ssize_t written = write(fd, secret_key, 64);
138 close(fd);
139
140 if (written != 64) {
141 return SET_ERRNO(ERROR_CONFIG,
142 "Failed to write identity file (wrote %zd/64 bytes): %s",
143 written, path);
144 }
145
146 log_info("Saved identity to %s", path);
147 return ASCIICHAT_OK;
148}
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)

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

Referenced by discovery_session_create().