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 File Reference

TURN server credential generation implementation. More...

Go to the source code of this file.

Functions

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.
bool turn_credentials_expired (const turn_credentials_t *credentials)
 Check if TURN credentials have expired.

Detailed Description

TURN server credential generation implementation.

Uses OpenBSD's public domain SHA1 implementation for HMAC-SHA1 credential generation (RFC 5766 TURN).

Definition in file turn_credentials.c.

Function Documentation

◆ turn_credentials_expired()

bool turn_credentials_expired ( const turn_credentials_t * credentials)

Check if TURN credentials have expired.

Parameters
credentialsCredentials to check
Returns
true if expired, false if still valid

Definition at line 186 of file turn_credentials.c.

186 {
187 if (!credentials) {
188 return true;
189 }
190
191 time_t now = time(NULL);
192 return now >= credentials->expires_at;
193}

References turn_credentials_t::expires_at.

◆ turn_generate_credentials()

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.

Creates TURN authentication credentials using HMAC-SHA1 with a shared secret. The username includes a timestamp for automatic expiration.

Parameters
session_idSession identifier (e.g., "swift-river-mountain")
secretShared secret configured on TURN server
validity_secondsHow long credentials remain valid (default: 86400 = 24 hours)
out_credentialsOutput credentials structure
Returns
ASCIICHAT_OK on success, error code on failure
Note
The secret must match the static-auth-secret configured on the TURN server

Example:

"swift-river-mountain",
"my-turn-secret",
86400, // 24 hours
&creds
);
if (result == ASCIICHAT_OK) {
printf("Username: %s\n", creds.username);
printf("Password: %s\n", creds.password);
}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
TURN server credentials (username + password).
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.

Definition at line 141 of file turn_credentials.c.

141 {
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}
#define SET_ERRNO(code,...)
unsigned char uint8_t
@ 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
#define SHA1_DIGEST_LENGTH
Definition sha1.h:21
uint8_t session_id[SESSION_ID_SIZE]

References ASCIICHAT_OK, ERROR_BUFFER_OVERFLOW, ERROR_CRYPTO, ERROR_INVALID_PARAM, turn_credentials_t::expires_at, log_debug, turn_credentials_t::password, session_id, SET_ERRNO, SHA1_DIGEST_LENGTH, and turn_credentials_t::username.