ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
prepare.c
Go to the documentation of this file.
5#include <string.h>
6
7_Static_assert(ACIP_TRAFFIC_CONTROL == CRYPTO_RECORD_CONTROL_CHANNEL, "Control channel IDs must match");
8
9asciichat_error_t acip_packet_plan(crypto_context_t *crypto, const socket_buffer_t *buffers, size_t count, acip_packet_plan_t *plan) {
10 if (!plan)
11 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP packet plan");
12 *plan = (acip_packet_plan_t){0};
13 if (!buffers || !count || count > SOCKET_IOV_MAX)
14 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ACIP packet vectors");
15 size_t size = 0;
16 for (size_t i = 0; i < count; ++i) {
17 if ((buffers[i].len && !buffers[i].data) || buffers[i].len > SOCKET_SEND_BUFFER_MAX - size)
18 return SET_ERRNO(ERROR_NETWORK_SIZE, "Invalid ACIP packet vector size");
19 size += buffers[i].len;
20 }
21 bool encrypt = false;
22 acip_send_options_t options = {.delivery = ACIP_DELIVERY_RELIABLE};
23 buffer_domain_t *domain = NULL;
24 if (buffers[0].len >= sizeof(packet_header_t)) {
25 packet_header_t header;
26 memcpy(&header, buffers[0].data, sizeof(header));
27 packet_type_t type = (packet_type_t)NET_TO_HOST_U16(header.type);
28 if (packet_is_handshake_type(type) || type == PACKET_TYPE_ERROR_MESSAGE)
29 domain = buffer_control_domain();
31 encrypt = crypto && crypto->encrypt_data && crypto_is_ready(crypto) && type != PACKET_TYPE_ENCRYPTED && !packet_is_initial_handshake_type(type);
32 } else {
33 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing ACIP packet header");
34 }
35 size_t capacity = 0;
36 crypto_record_t *records = NULL;
37 if (encrypt) {
38 if (size > CRYPTO_MAX_PLAINTEXT_SIZE - 24 || size > SOCKET_SEND_BUFFER_MAX - sizeof(packet_header_t) - CRYPTO_NONCE_SIZE - CRYPTO_MAC_SIZE)
39 return SET_ERRNO(ERROR_NETWORK_SIZE, "Encrypted ACIP packet too large");
40 crypto_session_t *session = NULL;
41 asciichat_error_t result = crypto_session_lock(crypto, &session);
42 if (result != ASCIICHAT_OK)
43 return result;
44 records = &session->records;
45 crypto_session_unlock(session);
46 capacity = size + 32 + crypto->nonce_size + crypto->mac_size;
47 }
48 *plan =
49 (acip_packet_plan_t){.buffers = buffers, .count = count, .plaintext_size = size, .ciphertext_capacity = capacity, .encrypt = encrypt, .domain = domain, .records = records, .options = options};
50 return ASCIICHAT_OK;
51}
52
53asciichat_error_t acip_packet_encrypt(crypto_context_t *crypto, const acip_packet_plan_t *plan, buffer_domain_t *domain, void *ciphertext, size_t capacity, packet_header_t *header, size_t *length) {
54 if (length)
55 *length = 0;
56 if (!crypto || !plan || !plan->encrypt || !plan->buffers || !plan->count || !ciphertext || !header || !length)
57 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid ACIP encryption arguments");
58 if (capacity < plan->ciphertext_capacity)
59 return SET_ERRNO(ERROR_NETWORK_SIZE, "ACIP ciphertext storage too small");
60 const uint8_t *plaintext = (const uint8_t *)plan->buffers[0].data;
61 uint8_t *gathered = NULL;
62 if (plan->count > 1) {
63 gathered = (uint8_t *)buffer_alloc(domain, plan->plaintext_size);
64 if (!gathered)
65 return SET_ERRNO(ERROR_MEMORY, "Cannot gather ACIP encryption input");
66 size_t offset = 0;
67 for (size_t i = 0; i < plan->count; ++i) {
68 if (plan->buffers[i].len)
69 memcpy(gathered + offset, plan->buffers[i].data, plan->buffers[i].len);
70 offset += plan->buffers[i].len;
71 }
72 plaintext = gathered;
73 }
74 size_t encrypted_length = 0;
75 crypto_session_t *session = NULL;
76 bool shared = !plan->records_locked && crypto && crypto->session && plan->records == &crypto->session->records;
77 asciichat_error_t encrypted = shared ? crypto_session_lock(crypto, &session) : ASCIICHAT_OK;
78 if (encrypted == ASCIICHAT_OK)
79 encrypted = plan->records ? crypto_record_seal_into_domain(plan->records, plan->options.traffic_class, plan->options.delivery, plaintext, plan->plaintext_size, (uint8_t *)ciphertext, capacity,
80 &encrypted_length, domain)
81 : SET_ERRNO(ERROR_INVALID_STATE, "Missing authenticated record context");
82 if (session)
83 crypto_session_unlock(session);
84 buffer_release(gathered);
85 if (encrypted != ASCIICHAT_OK)
86 return encrypted;
87 socket_buffer_t slice = {ciphertext, encrypted_length};
88 asciichat_error_t result = packet_header_build(header, PACKET_TYPE_ENCRYPTED, &slice, 1, 0);
89 if (result != ASCIICHAT_OK)
90 return result;
91 *length = encrypted_length;
92 return ASCIICHAT_OK;
93}
94
95static asciichat_error_t validate_record_packet(void *context, unsigned flow, unsigned delivery, const void *packet, size_t length) {
96 (void)context;
97 packet_header_t header;
98 asciichat_error_t result = packet_validate(packet, length, 0, &header);
99 if (result != ASCIICHAT_OK)
100 return result;
101 packet_type_t type = (packet_type_t)NET_TO_HOST_U16(header.type);
102 if (flow > ACIP_TRAFFIC_VISUAL || delivery > ACIP_DELIVERY_DATAGRAM || (delivery == ACIP_DELIVERY_DATAGRAM && flow == ACIP_TRAFFIC_CONTROL) || type == PACKET_TYPE_ENCRYPTED ||
103 packet_is_initial_handshake_type(type) || (type == PACKET_TYPE_CRYPTO_EPOCH_MARKER ? delivery != ACIP_DELIVERY_RELIABLE : acip_packet_traffic_class(type) != (acip_traffic_class_t)flow))
104 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid authenticated packet channel");
105 return ASCIICHAT_OK;
106}
107
108asciichat_error_t acip_packet_seal(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written) {
109 if (!output || !written || !records || !records->crypto)
110 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid packet encryption arguments");
111 *output = NULL;
112 *written = 0;
113 if ((unsigned)flow > ACIP_TRAFFIC_VISUAL || (unsigned)delivery > ACIP_DELIVERY_DATAGRAM || (flow == ACIP_TRAFFIC_CONTROL && delivery == ACIP_DELIVERY_DATAGRAM))
114 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid packet output channel");
115 asciichat_error_t result = validate_record_packet(NULL, flow, delivery, packet, length);
116 if (result != ASCIICHAT_OK)
117 return result;
118 socket_buffer_t slice = {packet, length};
119 if (length > CRYPTO_MAX_PLAINTEXT_SIZE - 24)
120 return SET_ERRNO(ERROR_NETWORK_SIZE, "Packet exceeds record plaintext capacity");
121 acip_packet_plan_t plan = {.buffers = &slice,
122 .count = 1,
123 .plaintext_size = length,
124 .encrypt = true,
125 .records = records,
126 .records_locked = true,
127 .options = {.traffic_class = flow, .delivery = delivery},
128 .ciphertext_capacity = length + 32 + records->crypto->nonce_size + records->crypto->mac_size};
129 uint8_t *wire = (uint8_t *)buffer_alloc(plan.domain, sizeof(packet_header_t) + plan.ciphertext_capacity);
130 if (!wire)
131 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate encrypted packet");
132 packet_header_t header;
133 size_t encrypted_length = 0;
134 result = acip_packet_encrypt(records->crypto, &plan, plan.domain, wire + sizeof(header), plan.ciphertext_capacity, &header, &encrypted_length);
135 if (result != ASCIICHAT_OK) {
136 buffer_release(wire);
137 return result;
138 }
139 memcpy(wire, &header, sizeof(header));
140 *output = wire;
141 *written = sizeof(header) + encrypted_length;
142 return ASCIICHAT_OK;
143}
144
145asciichat_error_t acip_packet_open_sequence(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written,
146 uint64_t *sequence) {
147 if (!output || !written)
148 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing decrypted packet output");
149 *output = NULL;
150 *written = 0;
151 packet_header_t header;
152 asciichat_error_t result = packet_validate(packet, length, 0, &header);
153 if (result != ASCIICHAT_OK)
154 return result;
155 if (NET_TO_HOST_U16(header.type) != PACKET_TYPE_ENCRYPTED)
156 return SET_ERRNO(ERROR_CRYPTO, "Expected encrypted packet");
157 return acip_packet_decrypt_payload(records, flow, delivery, (const uint8_t *)packet + sizeof(header), length - sizeof(header), output, written, sequence);
158}
159
160asciichat_error_t acip_packet_open(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written) {
161 return acip_packet_open_sequence(records, flow, delivery, packet, length, output, written, NULL);
162}
163
164asciichat_error_t acip_packet_decrypt_payload(crypto_record_t *records, unsigned flow, unsigned delivery, const void *payload, size_t length, uint8_t **output, size_t *written, uint64_t *sequence) {
165 return crypto_record_open_sequence(records, flow, delivery, payload, length, output, written, sequence, validate_record_packet, NULL);
166}
#define SET_ERRNO(code,...)
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:294
buffer_domain_t * buffer_control_domain(void)
Definition buffer.c:465
struct buffer_domain buffer_domain_t
Definition buffer.h:28
#define buffer_release(data)
Definition buffer.h:108
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned long long uint64_t
unsigned char uint8_t
bool crypto_is_ready(const crypto_context_t *ctx)
Check if key exchange is complete and ready for encryption.
#define CRYPTO_MAX_PLAINTEXT_SIZE
Maximum plaintext size (1MB).
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_CRYPTO
@ ERROR_INVALID_PARAM
asciichat_error_t packet_validate(const void *packet, size_t length, size_t limit, packet_header_t *output)
Definition packet.c:81
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
asciichat_error_t packet_header_build(packet_header_t *header, packet_type_t type, const socket_buffer_t *payload, size_t count, uint32_t client_id)
Definition packet.c:47
@ PACKET_TYPE_CRYPTO_EPOCH_MARKER
Definition packet.h:359
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion).
Definition packet.h:369
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
struct crypto_session crypto_session_t
For crypto session, this is the live record that ties owner, mutex, records, rotation together....
void crypto_session_unlock(crypto_session_t *session)
asciichat_error_t crypto_session_lock(crypto_context_t *crypto, crypto_session_t **output)
asciichat_error_t acip_packet_open(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written)
Definition prepare.c:160
asciichat_error_t acip_packet_decrypt_payload(crypto_record_t *records, unsigned flow, unsigned delivery, const void *payload, size_t length, uint8_t **output, size_t *written, uint64_t *sequence)
Definition prepare.c:164
asciichat_error_t acip_packet_plan(crypto_context_t *crypto, const socket_buffer_t *buffers, size_t count, acip_packet_plan_t *plan)
Definition prepare.c:9
asciichat_error_t acip_packet_encrypt(crypto_context_t *crypto, const acip_packet_plan_t *plan, buffer_domain_t *domain, void *ciphertext, size_t capacity, packet_header_t *header, size_t *length)
The useful part of acip packet plan is in buffers, count, plaintext_size, ciphertext_capacity: togeth...
Definition prepare.c:53
asciichat_error_t acip_packet_seal(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written)
Definition prepare.c:108
asciichat_error_t acip_packet_open_sequence(crypto_record_t *records, acip_traffic_class_t flow, acip_delivery_t delivery, const void *packet, size_t length, uint8_t **output, size_t *written, uint64_t *sequence)
Definition prepare.c:145
asciichat_error_t crypto_record_seal_into_domain(crypto_record_t *state, unsigned traffic_class, unsigned delivery, const void *packet, size_t length, uint8_t *wire, size_t capacity, size_t *out_length, buffer_domain_t *requested_domain)
Definition record.c:49
asciichat_error_t crypto_record_open_sequence(crypto_record_t *state, unsigned traffic_class, unsigned delivery, const void *packet, size_t length, uint8_t **out, size_t *out_length, uint64_t *sequence_out, crypto_record_validate_fn validate, void *context)
Definition record.c:107
struct crypto_record crypto_record_t
When code needs to work with crypto record, it keeps crypto, key_epoch, last_receive_epoch,...
#define CRYPTO_RECORD_CONTROL_CHANNEL
Definition record.h:12
#define CRYPTO_NONCE_SIZE
Nonce size (XSalsa20).
Definition sizes.h:84
#define CRYPTO_MAC_SIZE
MAC size (Poly1305).
Definition sizes.h:90
🔤 String Manipulation and Shell Escaping Utilities
size_t ciphertext_capacity
Definition prepare.h:24
crypto_record_t * records
Definition prepare.h:26
acip_send_options_t options
Definition prepare.h:28
const socket_buffer_t * buffers
Definition prepare.h:21
buffer_domain_t * domain
Borrowed control reserve for handshake/error traffic; NULL otherwise.
Definition prepare.h:29
size_t plaintext_size
Definition prepare.h:23
acip_delivery_t delivery
Definition traffic.h:22
acip_traffic_class_t traffic_class
Definition traffic.h:21
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
struct crypto_session * session
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
crypto_context_t * crypto
Definition record.h:27
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
acip_traffic_class_t acip_packet_traffic_class(uint16_t type)
Definition traffic.c:4
acip_traffic_class_t
Definition traffic.h:9
@ ACIP_TRAFFIC_CONTROL
Definition traffic.h:10
@ ACIP_TRAFFIC_VISUAL
Definition traffic.h:12
acip_delivery_t
Definition traffic.h:15
@ ACIP_DELIVERY_RELIABLE
Definition traffic.h:16
@ ACIP_DELIVERY_DATAGRAM
Definition traffic.h:17