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

# Summary of changes

With WHIP in the default (passive) DTLS role on an SChannel build, the DTLS 
handshake always failed with `0x80090331` (SEC_E_ALGORITHM_MISMATCH).

I dumped the datagram SChannel was handed: it was not a ClientHello but the 
peer's **STUN Binding Request** (104 bytes, magic cookie `2112a442`, USERNAME / 
ICE-CONTROLLED / MESSAGE-INTEGRITY / FINGERPRINT). WebRTC multiplexes STUN, 
DTLS and SRTP on one UDP port, and `tls_handshake_loop()` passes every datagram 
it reads to `AcceptSecurityContext()`/`InitializeSecurityContext()`, so one 
STUN packet fails the whole handshake. SChannel never saw the real ClientHello.

This patch silently discards datagrams that aren't DTLS records 
(`ff_is_dtls_packet()`) in the handshake loop, as RFC 6347 ยง4.1.2.7 recommends 
for invalid records.

### Testing

Windows 11, MSYS2 UCRT64, gcc 16.2, MediaMTX v1.21.4 on localhost, default 
(passive) mode. Since WHIP on SChannel also needs `use_srtp` (#26328), the 
end-to-end runs used this patch on top of #26328:

| Build | Result |
|---|---|
| without this patch | handshake fails every time with `0x80090331` |
| with this patch, 20 runs | 20/20 OK; the STUN datagram was skipped in every 
run (`Ignoring non-DTLS datagram of 104 bytes`) |
| with this patch, 10 s publish | server: `stream is available and online, 2 
tracks (Opus, H264)`, 300 frames, exit 0 |
| active mode (`-whip_flags dtls_active`), 30 runs | 30/30 OK (unchanged) |
| this patch **alone** (without #26328), passive | STUN skipped, SChannel now 
processes the ClientHello and replies; pion then aborts with `SRTP support was 
requested but server did not respond with use_srtp`, which #26328 fixes |

FATE: `make fate` (without the sample suite) passes, 3274 tests, on this patch 
alone (Windows 11 / MSYS2 UCRT64 / gcc 16.2).

### Notes

- This might also explain the occasional `0x80090308` (SEC_E_INVALID_TOKEN) I 
mentioned in #26328 for active mode, if a STUN packet arrives mid-handshake. I 
couldn't reproduce that failure today (30/30 without this patch too), so I'm 
not claiming it's fixed.
- The passive handshake takes ~1.1 s, because `rtc.c` already consumed the 
peer's first ClientHello before the DTLS handshake starts, so SChannel waits 
for the retransmission. That's separate from this fix.

<!--
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 fb376bb596e934cfba07d2dd747146b05062971c Mon Sep 17 00:00:00 2001
From: prateek rao <[email protected]>
Date: Wed, 25 Nov 2026 02:28:00 +0530
Subject: [PATCH] avformat/tls_schannel: ignore non-DTLS datagrams during DTLS
 handshake

When the UDP socket is shared with other protocols, as in WebRTC where
STUN, DTLS and SRTP are multiplexed on one port, the handshake loop can
read a non-DTLS datagram and pass it to AcceptSecurityContext() or
InitializeSecurityContext(), which then fails the whole handshake.

With WHIP in the default (passive) DTLS role, the first datagram read
is typically the peer's STUN binding request, so the handshake always
failed with SEC_E_ALGORITHM_MISMATCH.

Silently discard datagrams that are not DTLS records, as RFC 6347
section 4.1.2.7 recommends for invalid records.

Signed-off-by: prateek rao <[email protected]>
---
 libavformat/tls_schannel.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/libavformat/tls_schannel.c b/libavformat/tls_schannel.c
index a789f500fb2..8822302582b 100644
--- a/libavformat/tls_schannel.c
+++ b/libavformat/tls_schannel.c
@@ -865,6 +865,13 @@ static int tls_handshake_loop(URLContext *h, int initial)
                 av_log(h, AV_LOG_ERROR, "Failed to read handshake response\n");
                 goto fail;
             }
+            /* The UDP socket may be shared with other protocols (e.g. STUN for
+             * WebRTC). Silently discard datagrams that are not DTLS records,
+             * as required by RFC 6347 section 4.1.2.7. */
+            if (s->is_dtls && !ff_is_dtls_packet(c->enc_buf + 
c->enc_buf_offset, ret)) {
+                av_log(h, AV_LOG_TRACE, "Ignoring non-DTLS datagram of %d 
bytes\n", ret);
+                continue;
+            }
             c->enc_buf_offset += ret;
             if (s->is_dtls && !recv_addr_len) {
                 ff_udp_get_last_recv_addr(uc, &recv_addr, &recv_addr_len);
-- 
2.54.1

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

Reply via email to