ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
security_options.c
Go to the documentation of this file.
4#include <string.h>
5
6asciichat_error_t acip_security_init_options(acip_security_t *security, bool server, bool transport_secure,
7 const char *password_override) {
8 if (!security)
9 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing peer security state");
10 memset(security, 0, sizeof(*security));
11 const options_t *options = options_get();
12 if (!options)
13 return SET_ERRNO(ERROR_INVALID_STATE, "Peer security requires initialized options");
14 const char *password = password_override ? password_override : options->password;
15 crypto_policy_t policy = {
16 .transport_secure = transport_secure,
17 .encrypt = options->encrypt_enabled,
18 .no_encrypt = options->no_encrypt,
19 .no_auth = options->no_auth,
20 .has_password = password[0] != '\0',
21 .has_identity = options->encrypt_key[0] != '\0',
22 .has_verification_key = server ? options->client_keys[0] != '\0' : options->server_key[0] != '\0',
23 };
24 asciichat_error_t result = crypto_policy_resolve(&policy, &security->mode);
25 security->forbid_encryption = policy.no_encrypt;
26 security->require_encryption = policy.encrypt;
27 if (result != ASCIICHAT_OK)
28 return result;
29 crypto_handshake_context_t *ctx = &security->handshake;
30 result = policy.has_password ? crypto_handshake_init_with_password("webrtc_peer", ctx, server, password)
31 : crypto_handshake_init("webrtc_peer", ctx, server);
32 if (result != ASCIICHAT_OK)
33 return result;
34 ctx->require_client_auth = server && options->require_server_verify;
36 if (policy.has_identity) {
37 private_key_t *private_key = server ? &ctx->server_private_key : &ctx->client_private_key;
38 public_key_t *public_key = server ? &ctx->server_public_key : &ctx->client_public_key;
39 result = parse_private_key(options->encrypt_key, private_key);
40 if (result != ASCIICHAT_OK)
41 goto failed;
42 public_key->type = KEY_TYPE_ED25519;
43 memcpy(public_key->key, private_key->public_key, ED25519_PUBLIC_KEY_SIZE);
44 if (!server && strncmp(options->encrypt_key, "gpg:", 4) == 0)
45 SAFE_STRNCPY(ctx->client_gpg_key_id, options->encrypt_key + 4, sizeof(ctx->client_gpg_key_id));
46 }
47 if (server && policy.has_verification_key) {
49 if (!ctx->client_whitelist) {
50 result = SET_ERRNO(ERROR_MEMORY, "Cannot allocate WebRTC client key list");
51 goto failed;
52 }
54 if (result != ASCIICHAT_OK)
55 goto failed;
56 if (!ctx->num_whitelisted_clients) {
57 result = SET_ERRNO(ERROR_CRYPTO_AUTH, "WebRTC client key list is empty");
58 goto failed;
59 }
60 ctx->require_client_auth = true;
61 } else if (!server && policy.has_verification_key) {
62 ctx->verify_server_key = true;
64 SAFE_STRNCPY(ctx->server_hostname, options->session_string, sizeof(ctx->server_hostname));
65 }
66 return ASCIICHAT_OK;
67failed:
68 acip_security_cleanup(security);
69 return result;
70}
#define SET_ERRNO(code,...)
asciichat_error_t crypto_handshake_init(const char *name, crypto_handshake_context_t *ctx, bool is_server)
Initialize named crypto handshake context.
asciichat_error_t crypto_handshake_init_with_password(const char *name, crypto_handshake_context_t *ctx, bool is_server, const char *password)
Initialize named crypto handshake context with password authentication.
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:423
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:51
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_CRYPTO_AUTH
Definition error_codes.h:95
@ ERROR_INVALID_PARAM
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:74
asciichat_error_t parse_private_key(const char *key_path, private_key_t *key_out)
Parse SSH private key from file.
Definition keys.c:219
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:411
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:105
key_type_t type
Definition key_types.h:73
@ KEY_TYPE_ED25519
Definition key_types.h:54
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-128).
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1148
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:511
struct acip_security acip_security_t
acip security is intentionally a small, concrete record. Fields such as handshake,...
void acip_security_cleanup(acip_security_t *security)
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
asciichat_error_t crypto_policy_resolve(const crypto_policy_t *policy, uint8_t *mode)
Definition policy.c:4
asciichat_error_t acip_security_init_options(acip_security_t *security, bool server, bool transport_secure, const char *password_override)
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:33
🔤 String Manipulation and Shell Escaping Utilities
crypto_handshake_context_t handshake
Cryptographic handshake context structure.
The useful part of crypto policy is in transport_secure, encrypt, no_encrypt, no_auth: together they ...
Definition policy.h:12
bool no_encrypt
Definition policy.h:15
bool has_password
Definition policy.h:17
bool has_identity
Definition policy.h:18
bool has_verification_key
Definition policy.h:19
bool encrypt_enabled
Enable encryption.
Definition options.h:1178
char password[256]
Password string.
Definition options.h:1180
char session_string[(38+1)]
Definition options.h:1026
bool no_encrypt
Disable encryption (opt-out).
Definition options.h:1182
bool no_auth
Disable authentication layer (–no-auth).
Definition options.h:1183
char server_key[256]
Expected server public key (client).
Definition options.h:1184
bool require_server_verify
Server: only accept clients who verified via ACDS.
Definition options.h:1198
char encrypt_key[256]
SSH/GPG key file path (first –key flag, kept for compatibility).
Definition options.h:1179
char client_keys[256]
Allowed client keys (server).
Definition options.h:1185
Private identity-key material used for server authentication and signing.
Definition key_types.h:97
The normalized public identity used by ascii-chat's crypto and key-management code.
Definition key_types.h:72