ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
security_options.c
Go to the documentation of this file.
3#include <options/options.h>
4#include <string.h>
5
6asciichat_error_t acip_security_init_options(acip_security_t *security, bool server, bool transport_secure, const char *password_override) {
7 if (!security)
8 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing peer security state");
9 memset(security, 0, sizeof(*security));
10 const options_t *options = options_get();
11 if (!options)
12 return SET_ERRNO(ERROR_INVALID_STATE, "Peer security requires initialized options");
13 const char *password = password_override ? password_override : options->password;
14 crypto_policy_t policy = {
15 .transport_secure = transport_secure,
16 .encrypt = options->encrypt_enabled,
17 .no_encrypt = options->no_encrypt,
18 .no_auth = options->no_auth,
19 .has_password = password[0] != '\0',
20 .has_identity = options->encrypt_key[0] != '\0',
21 .has_verification_key = server ? options->client_keys[0] != '\0' : options->server_key[0] != '\0',
22 };
23 asciichat_error_t result = crypto_policy_resolve(&policy, &security->mode);
24 security->forbid_encryption = policy.no_encrypt;
25 security->require_encryption = policy.encrypt;
26 if (result != ASCIICHAT_OK)
27 return result;
28 crypto_handshake_context_t *ctx = &security->handshake;
29 result = policy.has_password ? crypto_handshake_init_with_password("webrtc_peer", ctx, server, password) : crypto_handshake_init("webrtc_peer", ctx, server);
30 if (result != ASCIICHAT_OK)
31 return result;
32 ctx->require_client_auth = server && options->require_server_verify;
34 if (policy.has_identity) {
35 private_key_t *private_key = server ? &ctx->server_private_key : &ctx->client_private_key;
36 public_key_t *public_key = server ? &ctx->server_public_key : &ctx->client_public_key;
37 result = parse_private_key(options->encrypt_key, private_key);
38 if (result != ASCIICHAT_OK)
39 goto failed;
40 public_key->type = KEY_TYPE_ED25519;
41 memcpy(public_key->key, private_key->public_key, ED25519_PUBLIC_KEY_SIZE);
42 if (!server && strncmp(options->encrypt_key, "gpg:", 4) == 0)
43 SAFE_STRNCPY(ctx->client_gpg_key_id, options->encrypt_key + 4, sizeof(ctx->client_gpg_key_id));
44 }
45 if (server && policy.has_verification_key) {
47 if (!ctx->client_whitelist) {
48 result = SET_ERRNO(ERROR_MEMORY, "Cannot allocate WebRTC client key list");
49 goto failed;
50 }
52 if (result != ASCIICHAT_OK)
53 goto failed;
54 if (!ctx->num_whitelisted_clients) {
55 result = SET_ERRNO(ERROR_CRYPTO_AUTH, "WebRTC client key list is empty");
56 goto failed;
57 }
58 ctx->require_client_auth = true;
59 } else if (!server && policy.has_verification_key) {
60 ctx->verify_server_key = true;
62 SAFE_STRNCPY(ctx->server_hostname, options->session_string, sizeof(ctx->server_hostname));
63 }
64 return ASCIICHAT_OK;
65failed:
66 acip_security_cleanup(security);
67 return result;
68}
#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)
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CRYPTO_AUTH
@ ERROR_INVALID_PARAM
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:85
asciichat_error_t parse_private_key(const char *key_path, private_key_t *key_out)
Parse SSH private key from file.
Definition keys.c:245
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:474
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:124
key_type_t type
Definition key_types.h:84
@ KEY_TYPE_ED25519
Definition key_types.h:61
#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:1171
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:513
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
struct acip_security acip_security_t
acip security is intentionally a small, concrete record. Fields such as handshake,...
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:38
🔤 String Manipulation and Shell Escaping Utilities
bool require_encryption
Definition security.h:22
uint8_t mode
Definition security.h:20
crypto_handshake_context_t handshake
Definition security.h:19
bool forbid_encryption
Definition security.h:21
Cryptographic handshake context structure.
The useful part of crypto policy is in transport_secure, encrypt, no_encrypt, no_auth: together they ...
Definition policy.h:19
bool no_encrypt
Definition policy.h:22
bool has_password
Definition policy.h:24
bool has_identity
Definition policy.h:25
bool has_verification_key
Definition policy.h:26
bool encrypt_enabled
Enable encryption.
Definition options.h:1171
char password[256]
Password string.
Definition options.h:1173
char session_string[(38+1)]
Definition options.h:1021
bool no_encrypt
Disable encryption (opt-out).
Definition options.h:1175
bool no_auth
Disable authentication layer (–no-auth).
Definition options.h:1176
char server_key[256]
Expected server public key (client).
Definition options.h:1177
bool require_server_verify
Server: only accept clients who verified via ACDS.
Definition options.h:1190
char encrypt_key[256]
SSH/GPG key file path (first –key flag, kept for compatibility).
Definition options.h:1172
char client_keys[256]
Allowed client keys (server).
Definition options.h:1178
Private identity-key material used for server authentication and signing.
Definition key_types.h:113
The normalized public identity used by ascii-chat's crypto and key-management code.
Definition key_types.h:83