From b94982e5d27eefcc7307a28944b61255a5bd4ee5 Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Thu, 19 Nov 2026 14:12:33 +0300 Subject: [PATCH] [Core] SRTP: keep NSS initialized while SRTP is up libsrtp2's NSS backend shuts NSS down whenever the last SRTP stream goes away. Hold an idle SRTP session from switch_rtp_init() to switch_rtp_shutdown() so that it stays initialized. --- src/switch_rtp.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 74f07f5450..77265a773e 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -37,14 +37,6 @@ #endif #include #include -#undef PACKAGE_NAME -#undef PACKAGE_STRING -#undef PACKAGE_TARNAME -#undef PACKAGE_VERSION -#undef PACKAGE_BUGREPORT -#undef VERSION -#undef PACKAGE -#undef inline #include #include #include @@ -93,6 +85,11 @@ static switch_port_t END_PORT = RTP_END_PORT; static switch_mutex_t *port_lock = NULL; static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in); +/* libsrtp's NSS backend opens an NSS context per cipher, and NSS is fully shut down when the + * last one closes. This idle session keeps ciphers, and with them NSS, alive for as long as + * SRTP is initialized. */ +static srtp_t srtp_keepalive = NULL; + typedef char switch_rtp_hdr_size_check_t[sizeof(switch_rtp_hdr_t) == 12 ? 1 : -1]; @@ -1608,7 +1605,22 @@ SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool_t *pool) { srtp_err_status_t stat = srtp_init(); if (stat == srtp_err_status_ok) { + srtp_policy_t policy; + unsigned char key[SRTP_AES_ICM_128_KEY_LEN_WSALT] = { 0 }; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SRTP (%s) initialized.\n", srtp_get_version_string()); + + /* The session never protects anything, so the key does not matter. */ + memset(&policy, 0, sizeof(policy)); + srtp_crypto_policy_set_rtp_default(&policy.rtp); + srtp_crypto_policy_set_rtcp_default(&policy.rtcp); + policy.ssrc.type = ssrc_any_inbound; + policy.key = key; + + if ((stat = srtp_create(&srtp_keepalive, &policy))) { + srtp_keepalive = NULL; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Error creating the idle SRTP session (%d).\n", stat); + } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error initializing SRTP (%d).\n", stat); } @@ -2652,9 +2664,14 @@ SWITCH_DECLARE(void) switch_rtp_shutdown(void) switch_mutex_unlock(port_lock); #ifdef ENABLE_SRTP + if (srtp_keepalive) { + srtp_dealloc(srtp_keepalive); + srtp_keepalive = NULL; + } srtp_shutdown(); #endif switch_rtp_dtls_destroy(); + global_init = 0; } SWITCH_DECLARE(switch_port_t) switch_rtp_set_start_port(switch_port_t port)