Merge commit from fork

`switch_core_media_add_crypto()` parses the SDP `a=crypto` keysalt and
decodes it into a fixed-size key buffer. Enforce the buffer contract at
the call site:

- Reject a zero-length, negative, or over-long keysalt (the length
  check now has both a lower bound and an upper bound against the copy
  buffer).
- Copy the keysalt token into a NUL-terminated buffer and decode from
  that, so `switch_b64_decode` stops at the token instead of reading on
  into the following key material (it consumes input to the NUL and
  skips non-base64 bytes).
- Pass the destination buffer size as the decode bound rather than the
  parsed token length.
- Require the decoded length to cover the crypto suite's key+salt so the
  subsequent copy cannot read past the decoded bytes.

Add `test_add_crypto_keysalt_bounds`, a table-driven test covering the
accepted and rejected keysalt shapes across suites (AES-128/192/256),
including RFC 4568 lifetime/MKI and multi-key lines.
This commit is contained in:
Dmitry Verenitsin authored and GitHub committed 2026-08-08 19:47:47 +03:00
1 parent 6f47b5c38c
commit 86b10358e2
3 files changed
+162 -2

No files matched your search

+17 -2
View File
@@ -1240,6 +1240,7 @@ static const char* switch_core_media_crypto_find_key_material_candidate_end(cons
SWITCH_DECLARE(switch_status_t) switch_core_media_add_crypto(switch_core_session_t *session, switch_secure_settings_t *ssec, switch_rtp_crypto_direction_t direction)
{
unsigned char key[SWITCH_RTP_MAX_CRYPTO_LEN];
char keysalt_b64[SWITCH_RTP_MAX_CRYPTO_LEN + 1]; /* NUL-terminated copy of one key's base64, isolated from following key material */
switch_rtp_crypto_key_type_t type;
const char *p, *delimit;
@@ -1247,6 +1248,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_add_crypto(switch_core_session
const char *key_material_end = NULL; /* begin and end of the current key material candidate */
int method_len;
int keysalt_len;
switch_size_t decoded_len;
const char *opts;
uint32_t opt_field; /* LIFETIME or MKI */
@@ -1341,11 +1343,24 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_add_crypto(switch_core_session
keysalt_len = key_material_end - p;
}
if (keysalt_len > sizeof(key)) {
if (keysalt_len <= 0 || (size_t) keysalt_len >= sizeof(keysalt_b64)) {
goto bad_keysalt_len;
}
switch_b64_decode(p, (char *) key, keysalt_len);
/* switch_b64_decode consumes its input up to the NUL terminator and ignores
non-base64 bytes, so isolate this key's base64 in a terminated buffer to keep
the decode from spilling into the following key material. */
memcpy(keysalt_b64, p, keysalt_len);
keysalt_b64[keysalt_len] = '\0';
decoded_len = switch_b64_decode(keysalt_b64, (char *) key, sizeof(key));
/* switch_b64_decode returns the decoded byte count plus the trailing NUL it appends.
Require at least the suite's key+salt length so the copy below cannot read past the
decoded bytes into uninitialized stack. */
if (decoded_len <= (switch_size_t) SUITES[type].keysalt_len) {
goto bad_keysalt_len;
}
if (!multiple_keys) { /* First key becomes default (used in case no MKI is found). */
if (direction == SWITCH_RTP_CRYPTO_SEND) {