ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
turn_credentials.c
Go to the documentation of this file.
1
8
11#include <ascii-chat/common.h>
12#include <ascii-chat/log/log.h>
14#include <stdio.h>
15#include <string.h>
16#include <time.h>
17
21static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
22
32static size_t base64_encode(const uint8_t *input, size_t input_len, char *output, size_t output_size) {
33 if (!input || !output || input_len == 0) {
34 return 0;
35 }
36
37 // Calculate required output size: ((n + 2) / 3) * 4 + 1 for null terminator
38 size_t required_size = ((input_len + 2) / 3) * 4 + 1;
39 if (output_size < required_size) {
40 return 0;
41 }
42
43 size_t i = 0;
44 size_t j = 0;
45
46 // Process input in 3-byte chunks
47 while (i + 2 < input_len) {
48 uint32_t triple = ((uint32_t)input[i] << 16) | ((uint32_t)input[i + 1] << 8) | (uint32_t)input[i + 2];
49
50 output[j++] = base64_table[(triple >> 18) & 0x3F];
51 output[j++] = base64_table[(triple >> 12) & 0x3F];
52 output[j++] = base64_table[(triple >> 6) & 0x3F];
53 output[j++] = base64_table[triple & 0x3F];
54
55 i += 3;
56 }
57
58 // Handle remaining bytes (1 or 2)
59 if (i < input_len) {
60 uint32_t triple = (uint32_t)input[i] << 16;
61 if (i + 1 < input_len) {
62 triple |= (uint32_t)input[i + 1] << 8;
63 }
64
65 output[j++] = base64_table[(triple >> 18) & 0x3F];
66 output[j++] = base64_table[(triple >> 12) & 0x3F];
67
68 if (i + 1 < input_len) {
69 output[j++] = base64_table[(triple >> 6) & 0x3F];
70 } else {
71 output[j++] = '=';
72 }
73 output[j++] = '=';
74 }
75
76 output[j] = '\0';
77 return j;
78}
79
94static asciichat_error_t hmac_sha1(const uint8_t *data, size_t data_len, const uint8_t *secret, size_t secret_len, uint8_t *output, unsigned int *output_len) {
95 if (!data || !secret || !output || !output_len) {
96 return SET_ERRNO(ERROR_INVALID_PARAM, "HMAC-SHA1: NULL parameter");
97 }
98
99 SHA1_CTX ctx;
102 uint8_t inner_hash[SHA1_DIGEST_LENGTH];
103
104 // Prepare key: pad or truncate to block length
106 memset(key, 0, sizeof(key));
107
108 if (secret_len > SHA1_BLOCK_LENGTH) {
109 // If key is longer than block size, hash it first
110 SHA1_CTX key_ctx;
111 SHA1Init(&key_ctx);
112 SHA1Update(&key_ctx, secret, secret_len);
113 SHA1Final(key, &key_ctx);
114 } else {
115 memcpy(key, secret, secret_len);
116 }
117
118 // Prepare ipad and opad (HMAC construction per RFC 2104)
119 for (int i = 0; i < SHA1_BLOCK_LENGTH; i++) {
120 ipad[i] = key[i] ^ 0x36;
121 opad[i] = key[i] ^ 0x5c;
122 }
123
124 // Compute inner hash: H(ipad || message)
125 SHA1Init(&ctx);
126 SHA1Update(&ctx, ipad, SHA1_BLOCK_LENGTH);
127 SHA1Update(&ctx, data, data_len);
128 SHA1Final(inner_hash, &ctx);
129
130 // Compute outer hash: H(opad || inner_hash)
131 SHA1Init(&ctx);
132 SHA1Update(&ctx, opad, SHA1_BLOCK_LENGTH);
133 SHA1Update(&ctx, inner_hash, SHA1_DIGEST_LENGTH);
134 SHA1Final(output, &ctx);
135
136 *output_len = SHA1_DIGEST_LENGTH;
137
138 return ASCIICHAT_OK;
139}
140
141asciichat_error_t turn_generate_credentials(const char *session_id, const char *secret, uint32_t validity_seconds, turn_credentials_t *out_credentials) {
142 if (!session_id || !secret || !out_credentials) {
143 return SET_ERRNO(ERROR_INVALID_PARAM, "TURN credentials: NULL parameter");
144 }
145
146 if (validity_seconds == 0) {
147 return SET_ERRNO(ERROR_INVALID_PARAM, "TURN credentials: validity_seconds must be > 0");
148 }
149
150 // Calculate expiration timestamp
151 time_t now = time(NULL);
152 time_t expires_at = now + (time_t)validity_seconds;
153
154 // Format username: "{timestamp}:{session_id}"
155 int username_len = safe_snprintf(out_credentials->username, sizeof(out_credentials->username), "%ld:%s", (long)expires_at, session_id);
156 if (username_len < 0 || (size_t)username_len >= sizeof(out_credentials->username)) {
157 return SET_ERRNO(ERROR_BUFFER_OVERFLOW, "TURN credentials: username too long");
158 }
159
160 // Compute HMAC-SHA1(secret, username)
161 uint8_t hmac_result[SHA1_DIGEST_LENGTH];
162 unsigned int hmac_len = 0;
163
164 asciichat_error_t result = hmac_sha1((const uint8_t *)out_credentials->username, (size_t)username_len, (const uint8_t *)secret, strlen(secret), hmac_result, &hmac_len);
165 if (result != ASCIICHAT_OK) {
166 return result;
167 }
168
169 if (hmac_len != SHA1_DIGEST_LENGTH) {
170 return SET_ERRNO(ERROR_CRYPTO, "TURN credentials: unexpected HMAC length %u (expected %u)", hmac_len, SHA1_DIGEST_LENGTH);
171 }
172
173 // Base64-encode the HMAC to get the password
174 size_t encoded_len = base64_encode(hmac_result, hmac_len, out_credentials->password, sizeof(out_credentials->password));
175 if (encoded_len == 0) {
176 return SET_ERRNO(ERROR_BUFFER_OVERFLOW, "TURN credentials: password encoding failed");
177 }
178
179 out_credentials->expires_at = expires_at;
180
181 log_debug("Generated TURN credentials: username=%s, expires_at=%ld", out_credentials->username, (long)expires_at);
182
183 return ASCIICHAT_OK;
184}
185
187 if (!credentials) {
188 return true;
189 }
190
191 time_t now = time(NULL);
192 return now >= credentials->expires_at;
193}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
unsigned int uint32_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CRYPTO
Definition error_codes.h:97
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
void SHA1Update(SHA1_CTX *context, const void *dataptr, unsigned int len)
Definition sha1.c:198
void SHA1Init(SHA1_CTX *context)
Definition sha1.c:186
void SHA1Final(unsigned char digest[20], SHA1_CTX *context)
Definition sha1.c:219
#define SHA1_BLOCK_LENGTH
Definition sha1.h:20
#define SHA1_DIGEST_LENGTH
Definition sha1.h:21
uint8_t session_id[SESSION_ID_SIZE]
🔤 String Manipulation and Shell Escaping Utilities
TURN server credentials (username + password).
⏱️ High-precision timing utilities using sokol_time.h and uthash
bool turn_credentials_expired(const turn_credentials_t *credentials)
Check if TURN credentials have expired.
asciichat_error_t turn_generate_credentials(const char *session_id, const char *secret, uint32_t validity_seconds, turn_credentials_t *out_credentials)
Generate time-limited TURN credentials.
TURN server credential generation for WebRTC.