PR #26328 opened by bonkbeats
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26328
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26328.patch

The use_srtp option was ignored by the SChannel backend, so the DTLS
handshake never offered or negotiated an SRTP protection profile. WebRTC
peers then reject the session right after the handshake (e.g. pion:
"DTLS Handshake completed and no SRTP Protection Profile was chosen"),
so the WHIP muxer could not work with SChannel.

Pass a SECBUFFER_SRTP_PROTECTION_PROFILES input buffer offering
SRTP_AES128_CM_HMAC_SHA1_80, the only profile supported by the SRTP code,
on every InitializeSecurityContext/AcceptSecurityContext call, including
the first one that generates the ClientHello. The profile IDs are stored
in network byte order, as SChannel copies them into the extension as-is.

Before exporting the keying material, check via SECPKG_ATTR_SRTP_PARAMETERS
that this profile was actually negotiated, instead of assuming it.

The SChannel DTLS configure check now also requires the SRTP types, so
DTLS is disabled on headers that lack them.

Signed-off-by: prateek rao <[email protected]>

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From fdd771daa84c65e36a3b28a6ebfb76fda81274f0 Mon Sep 17 00:00:00 2001
From: prateek rao <[email protected]>
Date: Tue, 24 Nov 2026 23:53:45 +0530
Subject: [PATCH] avformat/tls_schannel: add support for the DTLS use_srtp
 extension

The use_srtp option was ignored by the SChannel backend, so the DTLS
handshake never offered or negotiated an SRTP protection profile. WebRTC
peers then reject the session right after the handshake (e.g. pion:
"DTLS Handshake completed and no SRTP Protection Profile was chosen"),
so the WHIP muxer could not work with SChannel.

Pass a SECBUFFER_SRTP_PROTECTION_PROFILES input buffer offering
SRTP_AES128_CM_HMAC_SHA1_80, the only profile supported by the SRTP code,
on every InitializeSecurityContext/AcceptSecurityContext call, including
the first one that generates the ClientHello. The profile IDs are stored
in network byte order, as SChannel copies them into the extension as-is.

Before exporting the keying material, check via SECPKG_ATTR_SRTP_PARAMETERS
that this profile was actually negotiated, instead of assuming it.

The SChannel DTLS configure check now also requires the SRTP types, so
DTLS is disabled on headers that lack them.

Signed-off-by: prateek rao <[email protected]>
---
 configure                  |  2 +-
 libavformat/tls_schannel.c | 77 ++++++++++++++++++++++++++++++++++----
 2 files changed, 70 insertions(+), 9 deletions(-)

diff --git a/configure b/configure
index eed7a76aa92..a12717cae41 100755
--- a/configure
+++ b/configure
@@ -7595,7 +7595,7 @@ enabled schannel &&
     schannel_extralibs="-lsecur32 -lncrypt -lcrypt32" ||
         disable schannel
 
-enabled schannel && check_cc dtls_protocol "windows.h security.h schnlsp.h" 
"int i = SECPKG_ATTR_DTLS_MTU;" -DSECURITY_WIN32
+enabled schannel && check_cc dtls_protocol "windows.h security.h schnlsp.h" 
"int i = SECPKG_ATTR_DTLS_MTU + SECBUFFER_SRTP_PROTECTION_PROFILES + 
SECPKG_ATTR_SRTP_PARAMETERS; SEC_SRTP_PROTECTION_PROFILES p; 
SecPkgContext_SrtpParameters sp" -DSECURITY_WIN32
 
 $makeinfo --version > /dev/null 2>&1 && enable makeinfo_command || disable 
makeinfo_command makeinfo_html
 if enabled makeinfo_command; then
diff --git a/libavformat/tls_schannel.c b/libavformat/tls_schannel.c
index a789f500fb2..dd5162e62f7 100644
--- a/libavformat/tls_schannel.c
+++ b/libavformat/tls_schannel.c
@@ -23,6 +23,7 @@
 #include "config.h"
 #include "config_components.h"
 
+#include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
 #include "avformat.h"
 #include "internal.h"
@@ -45,6 +46,9 @@
 #define SECBUFFER_ALERT                17
 #endif
 
+/* SRTP_AES128_CM_HMAC_SHA1_80, RFC 5764 section 4.1.2 */
+#define SCHANNEL_SRTP_AES128_CM_HMAC_SHA1_80 0x0001
+
 /* This is the name used for the private key in the MS Keystore.
  * There is as of time of writing no way to use schannel without
  * persisting the private key. Which usually means the default MS
@@ -621,6 +625,10 @@ typedef struct TLSContext {
     int sspi_close_notify;
 
     int have_private_cert;
+
+#if CONFIG_DTLS_PROTOCOL
+    SEC_SRTP_PROTECTION_PROFILES srtp_profiles;
+#endif
 } TLSContext;
 
 int ff_tls_set_external_socket(URLContext *h, URLContext *sock)
@@ -650,6 +658,26 @@ int ff_dtls_export_materials(URLContext *h, char 
*dtls_srtp_materials, size_t ma
     if (!c->have_context)
         return AVERROR(EINVAL);
 
+#if CONFIG_DTLS_PROTOCOL
+    if (c->tls_shared.use_srtp) {
+        SecPkgContext_SrtpParameters srtp_params = { 0 };
+        int profile;
+
+        sspi_ret = QueryContextAttributes(&c->ctxt_handle, 
SECPKG_ATTR_SRTP_PARAMETERS, &srtp_params);
+        if (sspi_ret != SEC_E_OK) {
+            av_log(h, AV_LOG_ERROR, "Querying SRTP parameters failed: %lx\n", 
sspi_ret);
+            return AVERROR_EXTERNAL;
+        }
+
+        profile = AV_RB16(&srtp_params.ProtectionProfile);
+        if (profile != SCHANNEL_SRTP_AES128_CM_HMAC_SHA1_80) {
+            av_log(h, AV_LOG_ERROR, "Peer did not negotiate the 
SRTP_AES128_CM_HMAC_SHA1_80 "
+                   "DTLS-SRTP profile (got 0x%04x)\n", profile);
+            return AVERROR_EXTERNAL;
+        }
+    }
+#endif
+
     keying_info.cbLabel = strlen(dst) + 1;
     keying_info.pszLabel = (LPSTR)dst;
     keying_info.cbContextValue = 0;
@@ -698,6 +726,21 @@ static void init_sec_buffer_desc(SecBufferDesc *desc, 
SecBuffer *buffers,
     desc->cBuffers = buffer_count;
 }
 
+/* Append the SRTP protection profiles buffer for the use_srtp extension
+ * (RFC 5764) at buffers[count], if enabled. Returns the new buffer count. */
+static unsigned long add_srtp_buffer(TLSContext *c, SecBuffer *buffers,
+                                     unsigned long count)
+{
+#if CONFIG_DTLS_PROTOCOL
+    TLSShared *s = &c->tls_shared;
+
+    if (s->is_dtls && s->use_srtp)
+        init_sec_buffer(&buffers[count++], SECBUFFER_SRTP_PROTECTION_PROFILES,
+                        &c->srtp_profiles, sizeof(c->srtp_profiles));
+#endif
+    return count;
+}
+
 static int tls_process_send_buffer(URLContext *h)
 {
     TLSContext *c = h->priv_data;
@@ -825,8 +868,9 @@ static int tls_handshake_loop(URLContext *h, int initial)
     SECURITY_STATUS sspi_ret;
     SecBuffer outbuf[3] = { 0 };
     SecBufferDesc outbuf_desc;
-    SecBuffer inbuf[3];
+    SecBuffer inbuf[4];
     SecBufferDesc inbuf_desc;
+    unsigned long inbuf_count;
     struct sockaddr_storage recv_addr = { 0 };
     socklen_t recv_addr_len = 0;
     int i, ret = 0, read_data = initial;
@@ -883,12 +927,11 @@ static int tls_handshake_loop(URLContext *h, int initial)
         /* input buffers */
         init_sec_buffer(&inbuf[0], SECBUFFER_TOKEN, 
av_malloc(c->enc_buf_offset), c->enc_buf_offset);
         init_sec_buffer(&inbuf[1], SECBUFFER_EMPTY, NULL, 0);
-        if (s->listen && s->is_dtls) {
-            init_sec_buffer(&inbuf[2], SECBUFFER_EXTRA, &recv_addr, 
recv_addr_len);
-            init_sec_buffer_desc(&inbuf_desc, inbuf, 3);
-        } else {
-            init_sec_buffer_desc(&inbuf_desc, inbuf, 2);
-        }
+        inbuf_count = 2;
+        if (s->listen && s->is_dtls)
+            init_sec_buffer(&inbuf[inbuf_count++], SECBUFFER_EXTRA, 
&recv_addr, recv_addr_len);
+        inbuf_count = add_srtp_buffer(c, inbuf, inbuf_count);
+        init_sec_buffer_desc(&inbuf_desc, inbuf, inbuf_count);
 
         if (inbuf[0].pvBuffer == NULL) {
             av_log(h, AV_LOG_ERROR, "Failed to allocate input buffer\n");
@@ -1018,11 +1061,19 @@ static int tls_client_handshake(URLContext *h)
     TLSContext *c = h->priv_data;
     TLSShared *s = &c->tls_shared;
     URLContext *uc = s->is_dtls ? s->udp : s->tcp;
+    SecBuffer inbuf[1];
+    SecBufferDesc inbuf_desc;
+    unsigned long inbuf_count;
     SecBuffer outbuf;
     SecBufferDesc outbuf_desc;
     SECURITY_STATUS sspi_ret;
     int ret;
 
+    /* The ClientHello is generated here, so the use_srtp extension has to be
+     * requested on this first call already. */
+    inbuf_count = add_srtp_buffer(c, inbuf, 0);
+    init_sec_buffer_desc(&inbuf_desc, inbuf, inbuf_count);
+
     init_sec_buffer(&outbuf, SECBUFFER_EMPTY, NULL, 0);
     init_sec_buffer_desc(&outbuf_desc, &outbuf, 1);
 
@@ -1036,7 +1087,8 @@ static int tls_client_handshake(URLContext *h)
         c->request_flags |= ISC_REQ_USE_SUPPLIED_CREDS;
 
     sspi_ret = InitializeSecurityContext(&c->cred_handle, NULL, s->host, 
c->request_flags, 0, 0,
-                                         NULL, 0, &c->ctxt_handle, 
&outbuf_desc, &c->context_flags,
+                                         inbuf_count ? &inbuf_desc : NULL, 0, 
&c->ctxt_handle,
+                                         &outbuf_desc, &c->context_flags,
                                          &c->ctxt_timestamp);
     if (sspi_ret != SEC_I_CONTINUE_NEEDED) {
         av_log(h, AV_LOG_ERROR, "Unable to create initial security context 
(0x%lx)\n", sspi_ret);
@@ -1176,6 +1228,15 @@ static int tls_open(URLContext *h, const char *uri, int 
flags, AVDictionary **op
 #endif
     }
 
+#if CONFIG_DTLS_PROTOCOL
+    if (s->is_dtls && s->use_srtp) {
+        c->srtp_profiles.ProfilesSize = 
sizeof(c->srtp_profiles.ProfilesList[0]);
+        /* SChannel copies the profile IDs into the extension verbatim,
+         * so they have to be in network byte order. */
+        AV_WB16(&c->srtp_profiles.ProfilesList[0], 
SCHANNEL_SRTP_AES128_CM_HMAC_SHA1_80);
+    }
+#endif
+
     /* Get credential handle */
     sspi_ret = AcquireCredentialsHandle(NULL, (TCHAR *)UNISP_NAME,
                                         s->listen ? SECPKG_CRED_INBOUND : 
SECPKG_CRED_OUTBOUND,
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to