From 4a7bd0874ab12f8963a4b7d2ce21ab65fe9ac16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=A3o=20Gomes=20Viana?= Date: Sun, 11 Oct 2026 16:18:36 +0200 Subject: [PATCH] sdp-types: read an SRTP suite by its whole name The suite of an a=crypto attribute used to be matched by trying each known name as a prefix. A suite whose name starts with a known one, such as AEAD_AES_256_GCM_8, matched AEAD_AES_256_GCM and left "_8" behind. The attribute then failed to parse, and with it the whole session description. The branch for unknown suites never ran for such a name. Now the suite parser reads the whole name first and then compares it with the known names. AEAD_AES_256_GCM_8 parses as SrtpSuite::Ext, and the parser reads the rest of the description. This change replaces the alt over the known names in SrtpSuite::parse with one take_while1 followed by a match on the name. It adds three tests: a known suite, a suite whose name starts with a known one, and a complete crypto attribute with such a suite. The change edits only the sdp-types crate, and the SrtpSuite type is unchanged. A SIP trunk offered AEAD_AES_256_GCM_8 in its second crypto attribute, after a suite the library knows. A SIP endpoint built on ezk could not read that offer, so the call failed, although the endpoint supports the suite offered first. Offers that list suites unknown to the library now parse. An application can then pick a suite it supports from the offer. Test: cargo test -p ezk-sdp-types --all-features (80 passed) Test: the two new tests of the longer suite failed on the old parser Test: cargo clippy -p ezk-sdp-types --all-features --all-targets Test: cargo fmt -p ezk-sdp-types --check --- media/sdp-types/src/attributes/crypto.rs | 51 ++++++++++++++++++++---- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/media/sdp-types/src/attributes/crypto.rs b/media/sdp-types/src/attributes/crypto.rs index e4d8f0cf..b6c549c7 100644 --- a/media/sdp-types/src/attributes/crypto.rs +++ b/media/sdp-types/src/attributes/crypto.rs @@ -97,14 +97,14 @@ macro_rules! suite { move |i| { context( "parsing srtp suite", - alt(( - $( - map(tag(stringify!($suite)), |_| Self::$suite), - )* - map(take_while1(is_alphanumeric_or_underscore), move |suite| { - Self::Ext(BytesStr::from_parse(src, suite)) - }), - )), + // The whole name is read before it is matched, so a known suite + // never matches the start of a longer one such as AEAD_AES_256_GCM_8. + map(take_while1(is_alphanumeric_or_underscore), move |suite: &str| { + match suite { + $(stringify!($suite) => Self::$suite,)* + _ => Self::Ext(BytesStr::from_parse(src, suite)), + } + }), )(i) } } @@ -361,6 +361,41 @@ fn number(i: &str) -> IResult<&str, u32> { mod tests { use super::*; + #[test] + fn srtp_suite_known() { + let i = BytesStr::from_static("AEAD_AES_256_GCM"); + let (rem, suite) = SrtpSuite::parse(i.as_ref())(&i).unwrap(); + + assert!(rem.is_empty(), "rem is not empty: {rem:?}"); + assert_eq!(suite, SrtpSuite::AEAD_AES_256_GCM); + } + + #[test] + fn srtp_suite_starting_with_a_known_suite() { + let i = BytesStr::from_static("AEAD_AES_256_GCM_8"); + let (rem, suite) = SrtpSuite::parse(i.as_ref())(&i).unwrap(); + + assert!(rem.is_empty(), "rem is not empty: {rem:?}"); + let SrtpSuite::Ext(ext) = &suite else { + panic!("expected Ext got {suite:?}") + }; + assert_eq!(ext.as_str(), "AEAD_AES_256_GCM_8"); + assert_eq!(suite.to_string(), "AEAD_AES_256_GCM_8"); + } + + #[test] + fn srtp_crypto_with_a_suite_starting_with_a_known_suite() { + let i = BytesStr::from_static( + "2 AEAD_AES_256_GCM_8 inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj", + ); + let (rem, crypto) = SrtpCrypto::parse(i.as_ref(), &i).unwrap(); + + assert!(rem.is_empty(), "rem is not empty: {rem:?}"); + assert_eq!(crypto.tag, 2); + assert_eq!(crypto.suite.as_str(), "AEAD_AES_256_GCM_8"); + assert_eq!(crypto.keys.len(), 1); + } + #[test] fn srtp_session_param_kdr() { let i = BytesStr::from_static("KDR=5");