diff --git a/docs/features.md b/docs/features.md index 09df27eb38..687fdf663a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -242,6 +242,17 @@ not get a line. 2026-10-11. Issue [#19](https://github.com/obs-rust/obs-studio/issues/19), PRs [#162](https://github.com/obs-rust/obs-studio/pull/162), [#181](https://github.com/obs-rust/obs-studio/pull/181). +- **Encrypted streaming where the service supports it.** Twitch, Vimeo, + niconico, BoxCast, Bitmovin, Eventials and EventLive.pro stream over RTMPS + (port 443) instead of plain RTMP, on every server checked to accept it; + existing profiles switch too, with no setting to change. If the encrypted + connection cannot be made, the shared fallback warning ("Streaming + unencrypted (RTMP)") counts down three seconds and then streams to the same + server over plain RTMP, unless you cancel; the fallback is written to the + log and to stderr. Without trusted root certificates on the system, the + warning says so and suggests installing the system's CA certificates. A + server certificate that does not verify stops the start instead. Custom servers are unchanged. *All*, pending merge. Issue + [#2205](https://github.com/obs-rust/obs-studio/issues/2205). ### Sources and filters diff --git a/frontend/build/frontend-widgets.pch.hpp b/frontend/build/frontend-widgets.pch.hpp index ac0a024618..7b98bb0ebf 100644 --- a/frontend/build/frontend-widgets.pch.hpp +++ b/frontend/build/frontend-widgets.pch.hpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #if defined(_WIN32) || defined(WHATSNEW_ENABLED) diff --git a/frontend/build/source-sets.json b/frontend/build/source-sets.json index bd96883561..1a6d224aad 100644 --- a/frontend/build/source-sets.json +++ b/frontend/build/source-sets.json @@ -403,6 +403,7 @@ "utility/RemuxQueueModel.hpp", "utility/RemuxWorker.hpp", "utility/ResizeSignaler.hpp", + "utility/RtmpsFallback.hpp", "build/frontend.utility-3+.hpp.cpp", "utility/SceneRenameDelegate.hpp", "build/frontend.utility-screenshotobj+.hpp.cpp", diff --git a/frontend/data/locale/en-US.ini b/frontend/data/locale/en-US.ini index dc10b3fa52..4dbc1c0c70 100644 --- a/frontend/data/locale/en-US.ini +++ b/frontend/data/locale/en-US.ini @@ -446,6 +446,10 @@ Output.ConnectFail.InvalidStream="Could not access the specified channel or stre Output.ConnectFail.HdrDisabled="HDR output is currently disabled for this output." Output.ConnectFail.Error="An unexpected error occurred when trying to connect to the server. More information in the log file." Output.ConnectFail.Disconnected="Disconnected from server." +Output.RtmpsFallback.Title="Streaming unencrypted (RTMP)" +Output.RtmpsFallback.Message="OBS could not make an encrypted connection (RTMPS) to the server.\n\nOBS streams to %1 unencrypted instead. To stop the stream from starting, select Cancel; to stream encrypted, check that your network or firewall allows connections on port 443." +Output.RtmpsFallback.NoRootCerts="OBS could not find your system's trusted certificates, so it could not make a secure connection (RTMPS) to the server.\n\nOBS streams to %1 unencrypted over RTMP instead. To stream encrypted, install your system's CA certificates package and start the stream again; to stop the stream from starting, select Cancel." +Output.RtmpsFallback.Tray="Streaming unencrypted (RTMP): the encrypted connection failed." # output streaming-related messages Output.StreamEncodeError.Title="Encoding error" diff --git a/frontend/utility/RtmpsFallback.hpp b/frontend/utility/RtmpsFallback.hpp new file mode 100644 index 0000000000..dee411ce66 --- /dev/null +++ b/frontend/utility/RtmpsFallback.hpp @@ -0,0 +1,111 @@ +#pragma once + +#include +#include + +#include +#include + +#include +#include +#include +#include + +/* The RTMPS fallback (obs-rust/obs-studio#2205): a stream that rtmp-services + * upgraded to RTMPS fails to connect, and OBS offers to stream to the same + * server over plain RTMP. The warning is the shared fallback warning + * (obs_frontend_warn_fallback, docs/fallback-warning.md): logged at error + * level and on stderr, a dialog with a three-second countdown that accepts, + * and Cancel, which stops the start. */ +namespace RtmpsFallback { + +/* How often to ask: the shared warning shows its dialog once per id in an + * OBS session, and the id names the server, so each server asks once per + * session. A second failure to the same server only logs and continues over + * RTMP. */ +inline constexpr bool IdPerServer = true; + +/* With the main window hidden (OBS in the tray) the shared warning logs and + * accepts at once; OBS then shows a tray notice and waits HiddenCountdownMs + * before it restarts over RTMP. */ +inline constexpr bool CountdownWhenHidden = true; +inline constexpr int HiddenCountdownMs = 3000; + +/* Why the RTMPS connection failed, as the RTMP output's rtmp_tls_failure proc + * reports it. */ +enum class Cause { + Connection, /* timeout, refused, reset, other handshake failure */ + CertUnverified, /* "cert_unverified": the server's certificate */ + NoRootCerts, /* "no_root_certs": the system has no trusted roots */ +}; + +inline Cause ParseCause(const char *failure) +{ + if (!failure) { + return Cause::Connection; + } + if (strcmp(failure, "cert_unverified") == 0) { + return Cause::CertUnverified; + } + if (strcmp(failure, "no_root_certs") == 0) { + return Cause::NoRootCerts; + } + return Cause::Connection; +} + +inline constexpr const char *BaseId = "rtmp.unencrypted"; +inline constexpr const char *NoRootCertsId = "rtmp.unencrypted.nocerts"; + +/* The fallback warning's id for a stream to plainUrl. The missing root + * certificates have their own id, so that warning is shown once per server + * even when an ordinary failure was shown first. */ +inline std::string Id(const std::string &plainUrl, Cause cause = Cause::Connection) +{ + std::string base = cause == Cause::NoRootCerts ? NoRootCertsId : BaseId; + if (!IdPerServer || plainUrl.empty()) { + return base; + } + return base + ":" + plainUrl; +} + +/* The locale key of the warning's message (%1 is the plain RTMP URL). */ +inline const char *MessageKey(Cause cause) +{ + return cause == Cause::NoRootCerts ? "Output.RtmpsFallback.NoRootCerts" : "Output.RtmpsFallback.Message"; +} + +/* Whether a stream that stopped with code may retry over plain RTMP: only a + * connection failure of a service stream (multitrack video brings its own + * URLs), and never when the server's certificate did not verify. That is no + * network trouble, and plain RTMP would send the stream key unencrypted to a + * server that could not prove who it is. */ +inline bool Allowed(int code, bool multitrack, Cause cause) +{ + return code == OBS_OUTPUT_CONNECT_FAILED && !multitrack && cause != Cause::CertUnverified; +} + +using Answer = std::function; + +/* Raises the shared warning for a fallback to plainUrl. message is translated + * and has %1 for the URL. answer is called once, on the UI thread: true for + * the countdown or "Accept anyway" (restart over plain RTMP), false for + * Cancel or a closed dialog. */ +inline void Warn(const QString &title, const QString &message, const std::string &plainUrl, Cause cause, Answer answer) +{ + std::string id = Id(plainUrl, cause); + QByteArray t = title.toUtf8(); + QByteArray m = message.arg(QString::fromStdString(plainUrl)).toUtf8(); + auto *held = new Answer(std::move(answer)); + obs_frontend_warn_fallback( + id.c_str(), t.constData(), m.constData(), true, + [](void *param, bool accepted) { + Answer *a = static_cast(param); + if (*a) { + (*a)(accepted); + } + delete a; + }, + held); +} + +} // namespace RtmpsFallback diff --git a/frontend/widgets/OBSBasic.hpp b/frontend/widgets/OBSBasic.hpp index b8440528c7..6e5f0a0aaf 100644 --- a/frontend/widgets/OBSBasic.hpp +++ b/frontend/widgets/OBSBasic.hpp @@ -1400,6 +1400,16 @@ private slots: bool streamingStopping = false; bool streamingStarting = false; + /* RTMPS fallback (utility/RtmpsFallback.hpp): whether the next start + * keeps plain RTMP after "Accept anyway", and whether the stream being + * started takes its URLs from multitrack video instead of the service. */ + bool rtmpsFallbackRestart = false; + bool streamUsesMultitrack = false; + std::string RtmpsFallbackUrl(); + std::string StreamTlsFailure(); + void SetPlainRtmp(bool enabled); + void OfferRtmpsFallback(const std::string &plainUrl, const std::string &tlsFailure); + public slots: void DisplayStreamStartError(); void StartStreaming(); diff --git a/frontend/widgets/OBSBasic_Streaming.cpp.hpp b/frontend/widgets/OBSBasic_Streaming.cpp.hpp index d3fd8ffdfb..749ecd7009 100644 --- a/frontend/widgets/OBSBasic_Streaming.cpp.hpp +++ b/frontend/widgets/OBSBasic_Streaming.cpp.hpp @@ -69,6 +69,11 @@ void OBSBasic::StartStreaming() } } + /* A start the user asks for tries RTMPS again; only the restart after + * "Proceed anyway" keeps plain RTMP. */ + SetPlainRtmp(rtmpsFallbackRestart); + rtmpsFallbackRestart = false; + emit StreamingPreparing(); if (sysTrayStream) { @@ -92,6 +97,8 @@ void OBSBasic::StartStreaming() sysTrayStream->setText("Basic.Main.Connecting"); } + streamUsesMultitrack = outputHandler->multitrackVideoActive; + if (!outputHandler->StartStreaming(service)) { DisplayStreamStartError(); return; @@ -335,6 +342,21 @@ void OBSBasic::StreamingStop(int code, QString last_error) errorDescription = Str("Output.ConnectFail.Disconnected"); } + /* A service connection that rtmp-services upgraded to RTMPS can retry + * over plain RTMP; multitrack video brings its own URLs. */ + std::string rtmpsPlainUrl; + std::string rtmpsTlsFailure; + if (code == OBS_OUTPUT_CONNECT_FAILED && !streamUsesMultitrack) { + rtmpsTlsFailure = StreamTlsFailure(); + RtmpsFallback::Cause cause = RtmpsFallback::ParseCause(rtmpsTlsFailure.c_str()); + if (RtmpsFallback::Allowed(code, streamUsesMultitrack, cause)) { + rtmpsPlainUrl = RtmpsFallbackUrl(); + } else if (cause == RtmpsFallback::Cause::CertUnverified && !RtmpsFallbackUrl().empty()) { + blog(LOG_ERROR, "RTMPS: the server's certificate did not verify; " + "the stream is not started and does not fall back to RTMP"); + } + } + if (use_last_error && !last_error.isEmpty()) { dstr_printf(errorMessage, "%s\n\n%s", errorDescription, QT_TO_UTF8(last_error)); } else { @@ -368,6 +390,9 @@ void OBSBasic::StreamingStop(int code, QString last_error) : QTStr("Output.StreamEncodeError.Msg.LastError").arg(last_error); OBSMessageBox::information(this, QTStr("Output.StreamEncodeError.Title"), msg); + } else if (!rtmpsPlainUrl.empty()) { + OfferRtmpsFallback(rtmpsPlainUrl, rtmpsTlsFailure); + } else if (code != OBS_OUTPUT_SUCCESS && isVisible()) { OBSMessageBox::information(this, QTStr("Output.ConnectFail.Title"), QT_UTF8(errorMessage)); @@ -384,6 +409,81 @@ void OBSBasic::StreamingStop(int code, QString last_error) } } +std::string OBSBasic::RtmpsFallbackUrl() +{ + calldata_t cd = {0}; + const char *plain = nullptr; + std::string url; + + calldata_set_ptr(&cd, "service", GetService()); + if (proc_handler_call(obs_get_proc_handler(), "rtmp_services_rtmps_fallback", &cd) && + calldata_get_string(&cd, "plain_url", &plain) && plain) { + url = plain; + } + calldata_free(&cd); + return url; +} + +void OBSBasic::SetPlainRtmp(bool enabled) +{ + calldata_t cd = {0}; + calldata_set_ptr(&cd, "service", GetService()); + calldata_set_bool(&cd, "enabled", enabled); + proc_handler_call(obs_get_proc_handler(), "rtmp_services_set_plain_rtmp", &cd); + calldata_free(&cd); +} + +/* Why the stream output's last connect failed its TLS handshake, as + * obs-outputs' rtmp output reports it: "cert_unverified", "no_root_certs", + * or empty for anything else (also for outputs without the proc). */ +std::string OBSBasic::StreamTlsFailure() +{ + obs_output_t *output = outputHandler ? outputHandler->streamOutput.Get() : nullptr; + if (!output) { + return ""; + } + + calldata_t cd = {0}; + const char *failure = nullptr; + std::string result; + if (proc_handler_call(obs_output_get_proc_handler(output), "rtmp_tls_failure", &cd) && + calldata_get_string(&cd, "failure", &failure) && failure) { + result = failure; + } + calldata_free(&cd); + return result; +} + +/* The RTMPS connection failed: the shared fallback warning counts down three + * seconds and then OBS restarts the stream over plain RTMP, unless the user + * cancels. The warning logs the fallback. Without trusted root certificates + * its message says so. */ +void OBSBasic::OfferRtmpsFallback(const std::string &plainUrl, const std::string &tlsFailure) +{ + RtmpsFallback::Cause cause = RtmpsFallback::ParseCause(tlsFailure.c_str()); + auto restart = [this]() { + if (isClosing() || outputHandler->StreamingActive()) { + return; + } + rtmpsFallbackRestart = true; + QMetaObject::invokeMethod(this, &OBSBasic::StartStreaming, Qt::QueuedConnection); + }; + + RtmpsFallback::Warn(QTStr("Output.RtmpsFallback.Title"), QTStr(RtmpsFallback::MessageKey(cause)), plainUrl, + cause, [this, restart](bool accepted) { + if (!accepted) { + blog(LOG_INFO, "RTMPS fallback cancelled; the stream was not started"); + return; + } + if (RtmpsFallback::CountdownWhenHidden && !isVisible()) { + SysTrayNotify(QTStr("Output.RtmpsFallback.Tray"), QSystemTrayIcon::Warning); + QTimer::singleShot(RtmpsFallback::HiddenCountdownMs, this, restart); + return; + } + restart(); + }); +} + void OBSBasic::StreamActionTriggered() { if (outputHandler->StreamingActive()) { diff --git a/plugins/obs-outputs/rtmp-stream.c.h b/plugins/obs-outputs/rtmp-stream.c.h index e85383f1e1..f127dca52e 100644 --- a/plugins/obs-outputs/rtmp-stream.c.h +++ b/plugins/obs-outputs/rtmp-stream.c.h @@ -89,6 +89,19 @@ static inline bool disconnected(struct rtmp_stream *stream) return os_atomic_load_bool(&stream->disconnected); } +/* RTMP_AddStream copies the stream key; RTMP_Close frees that copy only for + * a reading connection or after a server redirect (RTMP_PUB_CLEAN), and + * RTMP_Init forgets it. Free it before either drops it. */ +static void free_rtmp_streams(RTMP *r) +{ + for (int i = 0; i < r->Link.nStreams; i++) { + free(r->Link.streams[i].playpath.av_val); + r->Link.streams[i].playpath.av_val = NULL; + } + r->Link.nStreams = 0; + r->Link.curStreamIdx = 0; +} + static void rtmp_stream_destroy(void *data) { struct rtmp_stream *stream = data; @@ -110,6 +123,7 @@ static void rtmp_stream_destroy(void *data) } } + free_rtmp_streams(&stream->rtmp); RTMP_TLS_Free(&stream->rtmp); free_packets(stream); dstr_free(&stream->path); @@ -140,6 +154,52 @@ static void rtmp_stream_destroy(void *data) bfree(stream); } +/* Why the TLS handshake of a failed connect failed (obs-rust/obs-studio#2205). + * + * The server's certificate did not verify: not trusted, expired, or issued + * for another host name (mbedTLS reports a host-name mismatch the same way). + * Unlike a timeout, refusal or reset this is no network trouble, so the + * frontend does not retry over plain RTMP. + * + * The system has no trusted root certificates (RTMP_TLS_LoadCerts loaded + * none, so nothing can verify): the frontend still falls back, and its + * warning names the cause. + * + * Any other failure, also one before the certificate is checked, is a + * connection failure. */ +static enum rtmp_tls_failure rtmp_tls_failure(RTMP *r) +{ + int code = r->last_error_code; + bool verify_failed = code == -0x2700; /* MBEDTLS_ERR_X509_CERT_VERIFY_FAILED */ + bool no_ca_chain = code == -0x7680; /* MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED */ + if (!verify_failed && !no_ca_chain) + return RTMP_TLS_FAILURE_NONE; + + bool has_roots = r->RTMP_TLS_ctx && r->RTMP_TLS_ctx->cacert && r->RTMP_TLS_ctx->cacert->raw.len > 0; + if (!has_roots) + return RTMP_TLS_FAILURE_NO_ROOT_CERTS; + return verify_failed ? RTMP_TLS_FAILURE_CERT_UNVERIFIED : RTMP_TLS_FAILURE_NONE; +} + +/* Output proc: why the last connect's TLS handshake failed, as + * "cert_unverified", "no_root_certs", or "" for anything else. */ +static void rtmp_stream_tls_failure_proc(void *data, calldata_t *cd) +{ + struct rtmp_stream *stream = data; + const char *failure = ""; + switch (stream->tls_failure) { + case RTMP_TLS_FAILURE_CERT_UNVERIFIED: + failure = "cert_unverified"; + break; + case RTMP_TLS_FAILURE_NO_ROOT_CERTS: + failure = "no_root_certs"; + break; + case RTMP_TLS_FAILURE_NONE: + break; + } + calldata_set_string(cd, "failure", failure); +} + static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) { struct rtmp_stream *stream = bzalloc(sizeof(struct rtmp_stream)); @@ -181,6 +241,9 @@ static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) goto fail; } + proc_handler_add(obs_output_get_proc_handler(output), "void rtmp_tls_failure(out string failure)", + rtmp_stream_tls_failure_proc, stream); + UNUSED_PARAMETER(settings); return stream; @@ -951,7 +1014,8 @@ static int try_connect(struct rtmp_stream *stream) info("Connecting to RTMP URL %s...", stream->path.array); - // free any existing RTMP TLS context + // free any existing RTMP TLS context and stream keys + free_rtmp_streams(&stream->rtmp); RTMP_TLS_Free(&stream->rtmp); RTMP_Init(&stream->rtmp); @@ -992,7 +1056,13 @@ static int try_connect(struct rtmp_stream *stream) rtmp_host_log_interface_type(stream); + stream->tls_failure = RTMP_TLS_FAILURE_NONE; if (!RTMP_Connect(&stream->rtmp, NULL)) { + stream->tls_failure = rtmp_tls_failure(&stream->rtmp); + if (stream->tls_failure == RTMP_TLS_FAILURE_CERT_UNVERIFIED) + warn("The server's TLS certificate did not verify; the stream is not started"); + else if (stream->tls_failure == RTMP_TLS_FAILURE_NO_ROOT_CERTS) + warn("No trusted root certificates were found, so the TLS connection could not be verified"); set_output_error(stream); return OBS_OUTPUT_CONNECT_FAILED; } diff --git a/plugins/obs-outputs/rtmp-stream.h b/plugins/obs-outputs/rtmp-stream.h index cd82ed39e0..a0ee0b0aff 100644 --- a/plugins/obs-outputs/rtmp-stream.h +++ b/plugins/obs-outputs/rtmp-stream.h @@ -53,6 +53,14 @@ struct dbr_interpolation_point { long bitrates[MAX_OUTPUT_VIDEO_ENCODERS]; }; +/* Why an RTMPS connect failed, for the frontend's RTMP fallback + * (obs-rust/obs-studio#2205). */ +enum rtmp_tls_failure { + RTMP_TLS_FAILURE_NONE, /* none, or a connection failure */ + RTMP_TLS_FAILURE_CERT_UNVERIFIED, /* the server's certificate */ + RTMP_TLS_FAILURE_NO_ROOT_CERTS, /* no trusted root certificates */ +}; + struct rtmp_stream { obs_output_t *output; @@ -125,6 +133,9 @@ struct rtmp_stream { RTMP rtmp; bool new_socket_loop; + + /* Why the last connect's TLS handshake failed (rtmp_tls_failure). */ + enum rtmp_tls_failure tls_failure; bool low_latency_mode; bool disable_send_window_optimization; bool socket_thread_active; diff --git a/plugins/rtmp-services/CMakeLists.txt b/plugins/rtmp-services/CMakeLists.txt index 48ceb0303b..99e16909b1 100644 --- a/plugins/rtmp-services/CMakeLists.txt +++ b/plugins/rtmp-services/CMakeLists.txt @@ -28,6 +28,7 @@ target_sources( service-specific/amazon-ivs.h service-specific/dacast.h service-specific/nimotv.h + service-specific/rtmps-upgrade.h service-specific/service-ingest.h service-specific/showroom.h service-specific/twitch.h diff --git a/plugins/rtmp-services/build/rtmp-services.core+.h.c b/plugins/rtmp-services/build/rtmp-services.core+.h.c index d42c2287ba..4fc18f2c82 100644 --- a/plugins/rtmp-services/build/rtmp-services.core+.h.c +++ b/plugins/rtmp-services/build/rtmp-services.core+.h.c @@ -8,6 +8,7 @@ #include "plugins/rtmp-services/service-specific/amazon-ivs.c.h" #include "plugins/rtmp-services/service-specific/dacast.c.h" #include "plugins/rtmp-services/service-specific/nimotv.c.h" +#include "plugins/rtmp-services/service-specific/rtmps-upgrade.c.h" #include "plugins/rtmp-services/service-specific/service-ingest.c.h" #include "plugins/rtmp-services/service-specific/showroom.c.h" #include "plugins/rtmp-services/service-specific/twitch.c.h" diff --git a/plugins/rtmp-services/build/rtmp-services.pch.h b/plugins/rtmp-services/build/rtmp-services.pch.h index 01fe428b7f..82e9243ab7 100644 --- a/plugins/rtmp-services/build/rtmp-services.pch.h +++ b/plugins/rtmp-services/build/rtmp-services.pch.h @@ -18,6 +18,7 @@ #include "plugins/rtmp-services/service-specific/showroom.h" #include "plugins/rtmp-services/service-specific/dacast.h" #include "plugins/rtmp-services/service-specific/amazon-ivs.h" +#include "plugins/rtmp-services/service-specific/rtmps-upgrade.h" /* rtmp-services-main.c */ #include diff --git a/plugins/rtmp-services/rtmp-common.c.h b/plugins/rtmp-services/rtmp-common.c.h index 90fdb71359..63ed68fd60 100644 --- a/plugins/rtmp-services/rtmp-common.c.h +++ b/plugins/rtmp-services/rtmp-common.c.h @@ -14,6 +14,14 @@ struct rtmp_common { char **audio_codecs; bool supports_additional_audio_track; + + /* The RTMPS URL handed out instead of a plain one (rtmps-upgrade.h), + * the plain URL it replaced (NULL when nothing was upgraded), and + * whether the frontend asked for the plain URL after a failed RTMPS + * connection. */ + struct dstr rtmps_url; + char *rtmps_fallback; + bool plain_rtmp; }; static const char *rtmp_common_getname(void *unused) @@ -192,6 +200,8 @@ static void rtmp_common_destroy(void *data) bfree(service->protocol); bfree(service->server); bfree(service->key); + dstr_free(&service->rtmps_url); + bfree(service->rtmps_fallback); bfree(service); } @@ -489,7 +499,8 @@ static void fill_servers(obs_property_t *servers_prop, json_t *service, const ch return; } - /* Assumption: Twitch should be RTMP only, so no RTMPS check */ + /* Twitch lists its plain RTMP servers; rtmp_common_url() connects to + * the ones in rtmps-upgrade.c.h over RTMPS. */ if (strcmp(name, "Twitch") == 0) { if (fill_twitch_servers(servers_prop)) return; @@ -810,7 +821,7 @@ static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings, o } } -static const char *rtmp_common_url(void *data) +static const char *rtmp_common_plain_url(void *data) { struct rtmp_common *service = data; @@ -869,6 +880,67 @@ static const char *rtmp_common_url(void *data) return service->server; } +/* Hands out the RTMPS form of a plain URL on a host known to accept it, unless + * the frontend asked for plain RTMP after the encrypted connection failed. */ +static const char *rtmp_common_url(void *data) +{ + struct rtmp_common *service = data; + const char *url = rtmp_common_plain_url(data); + struct dstr rtmps = {0}; + + bfree(service->rtmps_fallback); + service->rtmps_fallback = NULL; + + if (service->plain_rtmp || !obs_is_output_protocol_registered("RTMPS") || + !rtmps_upgrade_url(service->service, url, &rtmps)) { + dstr_free(&rtmps); + return url; + } + + /* The output may still hold the last URL; only replace it when it + * changed. */ + if (dstr_cmp(&service->rtmps_url, rtmps.array) != 0) + dstr_move(&service->rtmps_url, &rtmps); + dstr_free(&rtmps); + + service->rtmps_fallback = bstrdup(url); + return service->rtmps_url.array; +} + +static void rtmp_common_rtmps_fallback_proc(void *param, calldata_t *cd) +{ + UNUSED_PARAMETER(param); + obs_service_t *service = calldata_ptr(cd, "service"); + + if (!service || strcmp(obs_service_get_id(service), "rtmp_common") != 0) + return; + + struct rtmp_common *data = obs_obj_get_data(service); + rtmp_common_url(data); + calldata_set_string(cd, "plain_url", data->rtmps_fallback ? data->rtmps_fallback : ""); +} + +static void rtmp_common_set_plain_rtmp_proc(void *param, calldata_t *cd) +{ + UNUSED_PARAMETER(param); + obs_service_t *service = calldata_ptr(cd, "service"); + + if (!service || strcmp(obs_service_get_id(service), "rtmp_common") != 0) + return; + + struct rtmp_common *data = obs_obj_get_data(service); + data->plain_rtmp = calldata_bool(cd, "enabled"); +} + +void rtmp_common_register_procs(void) +{ + proc_handler_t *ph = obs_get_proc_handler(); + proc_handler_add(ph, "void rtmp_services_rtmps_fallback(in ptr service, out string plain_url)", + rtmp_common_rtmps_fallback_proc, NULL); + proc_handler_add(ph, "void rtmp_services_set_plain_rtmp(in ptr service, in bool enabled)", + rtmp_common_set_plain_rtmp_proc, NULL); +} + static const char *rtmp_common_key(void *data) { struct rtmp_common *service = data; diff --git a/plugins/rtmp-services/rtmp-services-main.c.h b/plugins/rtmp-services/rtmp-services-main.c.h index b407037203..bc1c9a3c2b 100644 --- a/plugins/rtmp-services/rtmp-services-main.c.h +++ b/plugins/rtmp-services/rtmp-services-main.c.h @@ -14,6 +14,7 @@ static const char *RTMP_SERVICES_URL = (const char *)SERVICES_URL; extern struct obs_service_info rtmp_common_service; extern struct obs_service_info rtmp_custom_service; +extern void rtmp_common_register_procs(void); static update_info_t *update_info = NULL; static struct dstr module_name = {0}; @@ -93,6 +94,7 @@ bool obs_module_load(void) proc_handler_t *ph = obs_get_proc_handler(); proc_handler_add(ph, "void twitch_ingests_refresh(int seconds)", refresh_callback, NULL); proc_handler_add(ph, "void amazon_ivs_ingests_refresh(int seconds)", amazon_ivs_refresh_callback, NULL); + rtmp_common_register_procs(); #if defined(ENABLE_SERVICE_UPDATES) char *local_dir = obs_module_file(""); diff --git a/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h b/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h new file mode 100644 index 0000000000..5065886999 --- /dev/null +++ b/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h @@ -0,0 +1,125 @@ +#include "plugins/rtmp-services/build/rtmp-services.pch.h" + +/* Ingest hosts that answered on port 443 with a valid certificate for the + * host name, completed the RTMP handshake and connect over TLS, and rejected a + * publish with a made-up stream key the same way as over plain RTMP. Checked + * on 2026-10-24 for every server of every built-in RTMP service and every + * server in Twitch's ingest list (obs-rust/obs-studio#2205) with + * tools/rtmps-endpoint-check.py. A host that is not here stays on plain RTMP. */ +static const struct { + const char *service; + const char *host; +} rtmps_hosts[] = { + {"Bitmovin", "live-input.bitmovin.com"}, + {"BoxCast", "rtmp.boxcast.com"}, + {"Eventials", "transmission.eventials.com"}, + {"EventLive.pro", "go.eventlive.pro"}, + {"niconico (ニコニコ生放送)", "liveorigin.dlive.nicovideo.jp"}, + {"Twitch", "apn10.contribute.live-video.net"}, + {"Twitch", "apn20.contribute.live-video.net"}, + {"Twitch", "aps10.contribute.live-video.net"}, + {"Twitch", "aps20.contribute.live-video.net"}, + {"Twitch", "aps30.contribute.live-video.net"}, + {"Twitch", "euc10.contribute.live-video.net"}, + {"Twitch", "eun10.contribute.live-video.net"}, + {"Twitch", "euw10.contribute.live-video.net"}, + {"Twitch", "euw30.contribute.live-video.net"}, + {"Twitch", "ingest.global-contribute.live-video.net"}, + {"Twitch", "live-ams.twitch.tv"}, + {"Twitch", "live-arn.twitch.tv"}, + {"Twitch", "live-atl.twitch.tv"}, + {"Twitch", "live-ber.twitch.tv"}, + {"Twitch", "live-cdg.twitch.tv"}, + {"Twitch", "live-cph.twitch.tv"}, + {"Twitch", "live-den.twitch.tv"}, + {"Twitch", "live-dfw.twitch.tv"}, + {"Twitch", "live-eze.twitch.tv"}, + {"Twitch", "live-fra.twitch.tv"}, + {"Twitch", "live-hel.twitch.tv"}, + {"Twitch", "live-hkg.twitch.tv"}, + {"Twitch", "live-hou.twitch.tv"}, + {"Twitch", "live-iad.twitch.tv"}, + {"Twitch", "live-jfk.twitch.tv"}, + {"Twitch", "live-lax.twitch.tv"}, + {"Twitch", "live-lhr.twitch.tv"}, + {"Twitch", "live-lim.twitch.tv"}, + {"Twitch", "live-lis.twitch.tv"}, + {"Twitch", "live-mad.twitch.tv"}, + {"Twitch", "live-mde.twitch.tv"}, + {"Twitch", "live-mia.twitch.tv"}, + {"Twitch", "live-mil.twitch.tv"}, + {"Twitch", "live-mrs.twitch.tv"}, + {"Twitch", "live-ord.twitch.tv"}, + {"Twitch", "live-osl.twitch.tv"}, + {"Twitch", "live-pdx.twitch.tv"}, + {"Twitch", "live-phx.twitch.tv"}, + {"Twitch", "live-prg.twitch.tv"}, + {"Twitch", "live-qro.twitch.tv"}, + {"Twitch", "live-rio.twitch.tv"}, + {"Twitch", "live-sao.twitch.tv"}, + {"Twitch", "live-scl.twitch.tv"}, + {"Twitch", "live-sea.twitch.tv"}, + {"Twitch", "live-sel.twitch.tv"}, + {"Twitch", "live-sfo.twitch.tv"}, + {"Twitch", "live-sin.twitch.tv"}, + {"Twitch", "live-sjc.twitch.tv"}, + {"Twitch", "live-slc.twitch.tv"}, + {"Twitch", "live-syd.twitch.tv"}, + {"Twitch", "live-tpe.twitch.tv"}, + {"Twitch", "live-tyo.twitch.tv"}, + {"Twitch", "live-vie.twitch.tv"}, + {"Twitch", "live-waw.twitch.tv"}, + {"Twitch", "live-ymq.twitch.tv"}, + {"Twitch", "live-yto.twitch.tv"}, + {"Twitch", "live.twitch.tv"}, + {"Twitch", "sae10.contribute.live-video.net"}, + {"Twitch", "use10.contribute.live-video.net"}, + {"Twitch", "use20.contribute.live-video.net"}, + {"Twitch", "usw20.contribute.live-video.net"}, + {"Vimeo", "rtmp.cloud.vimeo.com"}, +}; + +#define UPGRADE_FROM_SCHEME "rtmp://" +#define UPGRADE_FROM_PORT "1935" + +static bool rtmps_host_listed(const char *service, const char *host, size_t host_len) +{ + for (size_t i = 0; i < sizeof(rtmps_hosts) / sizeof(rtmps_hosts[0]); i++) { + if (strcmp(rtmps_hosts[i].service, service) == 0 && strlen(rtmps_hosts[i].host) == host_len && + astrcmpi_n(rtmps_hosts[i].host, host, host_len) == 0) + return true; + } + return false; +} + +bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out) +{ + if (!service || !url || astrcmpi_n(url, UPGRADE_FROM_SCHEME, sizeof(UPGRADE_FROM_SCHEME) - 1) != 0) + return false; + + const char *host = url + sizeof(UPGRADE_FROM_SCHEME) - 1; + size_t host_len = strcspn(host, ":/?"); + const char *rest = host + host_len; + + /* Only the default RTMP port maps to RTMPS on 443. */ + if (*rest == ':') { + const char *port = rest + 1; + size_t port_len = strcspn(port, "/?"); + if (port_len != sizeof(UPGRADE_FROM_PORT) - 1 || + strncmp(port, UPGRADE_FROM_PORT, sizeof(UPGRADE_FROM_PORT) - 1) != 0) + return false; + rest = port + port_len; + } + + if (!host_len || !rtmps_host_listed(service, host, host_len)) + return false; + + dstr_copy(out, "rtmps://"); + dstr_ncat(out, host, host_len); + dstr_cat(out, ":443"); + dstr_cat(out, rest); + return true; +} + +#undef UPGRADE_FROM_SCHEME +#undef UPGRADE_FROM_PORT diff --git a/plugins/rtmp-services/service-specific/rtmps-upgrade.h b/plugins/rtmp-services/service-specific/rtmps-upgrade.h new file mode 100644 index 0000000000..063f948d28 --- /dev/null +++ b/plugins/rtmp-services/service-specific/rtmps-upgrade.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +/* Built-in services whose plain RTMP ingest servers also accept RTMPS on port + * 443. rtmp-services connects to these over RTMPS and keeps the plain URL so + * the frontend can offer it if the encrypted connection fails. + * + * Writes the RTMPS form of url (same host and path, port 443) to out and + * returns true when url is a plain rtmp:// URL on the default port and its host + * is in the list for service. Returns false and leaves out untouched + * otherwise. */ +bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out); + +/* Registers the rtmp_services_rtmps_fallback and rtmp_services_set_plain_rtmp + * procedures on the global proc handler. Defined in rtmp-common.c, called from + * obs_module_load. */ +void rtmp_common_register_procs(void); diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index c172f09641..0124f840ef 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -2614,6 +2614,66 @@ } ] }, + "test_rtmps_cert_no_fallback": { + "sources": [ + "test/cmocka/test_rtmps_cert_no_fallback.c", + { + "path": "plugins/obs-outputs/rtmp-hevc.c.h", + "requires": "ENABLE_HEVC" + }, + "plugins/obs-outputs/flv-mux.c.h", + "plugins/obs-outputs/net-if.c.h", + "plugins/obs-outputs/net-if-host-posix.c.h", + "plugins/obs-outputs/rtmp-stream-host-posix.c.h", + "plugins/obs-outputs/rtmp-av1.c.h", + "plugins/obs-outputs/librtmp/amf.c", + "plugins/obs-outputs/librtmp/cencode.c", + "plugins/obs-outputs/librtmp/hashswf.c", + "plugins/obs-outputs/librtmp/log.c", + "plugins/obs-outputs/librtmp/md5.c", + "plugins/obs-outputs/librtmp/parseurl.c", + "plugins/obs-outputs/librtmp/rtmp.c" + ], + "requires": { + "all": [ + "HAVE_OBS_OUTPUTS", + { + "not": "OS_WINDOWS" + } + ] + }, + "includes": [ + "plugins/obs-outputs", + "shared/opts-parser", + "shared/bpm" + ], + "libraries": [ + "OBS::libobs", + "OBS::happy-eyeballs", + "MbedTLS::mbedtls", + "ZLIB::ZLIB", + "jansson::jansson", + "OBS::native-test-cmocka", + { + "name": "OBS::native-test-foundation", + "requires": "OS_MACOS" + }, + { + "name": "OBS::native-test-security", + "requires": "OS_MACOS" + } + ], + "definitions": [ + "USE_MBEDTLS", + "CRYPTO" + ], + "compile_options": [ + { + "name": "-Wno-comma", + "requires": "C_COMPILER_CLANG" + } + ] + }, "test_scene_audio_action_steps": { "sources": [ "test/cmocka/test_scene_audio_action_steps.c", @@ -5100,6 +5160,13 @@ ], "timeout_seconds": 30 }, + "test_rtmps_cert_no_fallback": { + "target": "test_rtmps_cert_no_fallback", + "command": [ + "${target}" + ], + "timeout_seconds": 60 + }, "test_scene_audio_action_steps": { "target": "test_scene_audio_action_steps", "command": [ diff --git a/test/cmocka/test_rtmps_cert_no_fallback.c b/test/cmocka/test_rtmps_cert_no_fallback.c new file mode 100644 index 0000000000..dde5548750 --- /dev/null +++ b/test/cmocka/test_rtmps_cert_no_fallback.c @@ -0,0 +1,434 @@ +/* An RTMPS stream whose server certificate does not verify must not fall + * back to plain RTMP (obs-rust/obs-studio#2205, docs/fallback-warning.md + * rule 7). The frontend retries a failed RTMPS start over RTMP only for a + * connection failure: timeout, refusal, reset, or a handshake that fails for + * another reason. A certificate that does not verify, also one issued for + * another host name, stops the start. A system without trusted root + * certificates still falls back, with a warning that names that cause. The + * RTMP output reports the cause through its "rtmp_tls_failure" proc: + * "cert_unverified", "no_root_certs", or "" for any other failure. + * + * The test builds the real rtmp-stream.c and librtmp into this executable and + * connects to a TLS listener on the loopback interface. The listener presents + * a certificate the test makes at run time, issued for another host. With + * that certificate trusted, only the host name is wrong; without it, the + * certificate is also untrusted. A listener that closes the connection during + * the handshake, and a port with no listener, are connection failures that + * still fall back. An empty trust store (the system's certificate directory + * has no certificates) and a missing one (it could not be read) are the + * no-root-certificates case. The output is a small fake, so no display or encoder is + * needed. */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "librtmp/rtmp.h" + +/* Route the libobs calls rtmp-stream.c makes on its output to the fakes + * below, and RTMP_Connect through a wrapper that can add a trusted + * certificate. */ +#define obs_output_get_name fake_output_get_name +#define obs_output_set_last_error fake_output_set_last_error +#define obs_output_end_data_capture fake_output_end_data_capture +#define obs_output_get_proc_handler fake_output_get_proc_handler +static int test_rtmp_connect(RTMP *r, RTMPPacket *cp); +#define RTMP_Connect test_rtmp_connect + +#include "rtmp-stream.c" + +#undef RTMP_Connect + +const char *obs_module_text(const char *lookup) +{ + return lookup; +} + +/* ------------------------------------------------------------------------- */ +/* fakes */ + +static int fake_output; /* only its address is used */ +static proc_handler_t *output_procs; + +const char *fake_output_get_name(const obs_output_t *output) +{ + (void)output; + return "cert test"; +} + +void fake_output_set_last_error(obs_output_t *output, const char *message) +{ + (void)output; + (void)message; +} + +void fake_output_end_data_capture(obs_output_t *output) +{ + (void)output; +} + +proc_handler_t *fake_output_get_proc_handler(const obs_output_t *output) +{ + (void)output; + return output_procs; +} + +/* A certificate the client trusts in addition to the system's, or NULL. */ +static mbedtls_x509_crt *trusted_extra; + +/* The client's trust store. */ +enum store { + STORE_SYSTEM, /* the system's root certificates */ + STORE_EMPTY, /* the certificate directory parsed, with nothing in it */ + STORE_MISSING, /* the certificates could not be loaded at all */ +}; +static enum store store = STORE_SYSTEM; + +static int test_rtmp_connect(RTMP *r, RTMPPacket *cp) +{ + tls_ctx *tls = r->RTMP_TLS_ctx; + if (tls && store == STORE_EMPTY && tls->cacert) { + mbedtls_x509_crt_free(tls->cacert); + mbedtls_x509_crt_init(tls->cacert); + } else if (tls && store == STORE_MISSING) { + /* What RTMP_TLS_LoadCerts leaves when it fails. */ + if (tls->cacert) { + mbedtls_x509_crt_free(tls->cacert); + free(tls->cacert); + tls->cacert = NULL; + } + mbedtls_ssl_conf_ca_chain(&tls->conf, NULL, NULL); + } + if (trusted_extra && tls) + mbedtls_ssl_conf_ca_chain(&tls->conf, trusted_extra, NULL); + return RTMP_Connect(r, cp); +} + +/* ------------------------------------------------------------------------- */ +/* the server's certificate */ + +static mbedtls_entropy_context entropy; +static mbedtls_ctr_drbg_context drbg; +static mbedtls_pk_context server_key; +static mbedtls_x509_crt server_cert; + +/* A self-signed certificate for host, with its key. */ +static void make_cert(const char *host) +{ + char name[128]; + unsigned char pem[4096]; + unsigned char serial[] = {0x01}; + mbedtls_x509write_cert w; + + mbedtls_pk_init(&server_key); + assert_int_equal(mbedtls_pk_setup(&server_key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)), 0); + assert_int_equal(mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(server_key), + mbedtls_ctr_drbg_random, &drbg), + 0); + + snprintf(name, sizeof(name), "CN=%s", host); + mbedtls_x509write_crt_init(&w); + mbedtls_x509write_crt_set_version(&w, MBEDTLS_X509_CRT_VERSION_3); + mbedtls_x509write_crt_set_md_alg(&w, MBEDTLS_MD_SHA256); + mbedtls_x509write_crt_set_subject_key(&w, &server_key); + mbedtls_x509write_crt_set_issuer_key(&w, &server_key); + assert_int_equal(mbedtls_x509write_crt_set_subject_name(&w, name), 0); + assert_int_equal(mbedtls_x509write_crt_set_issuer_name(&w, name), 0); + assert_int_equal(mbedtls_x509write_crt_set_serial_raw(&w, serial, sizeof(serial)), 0); + assert_int_equal(mbedtls_x509write_crt_set_validity(&w, "20200101000000", "20991231235959"), 0); + assert_int_equal(mbedtls_x509write_crt_set_basic_constraints(&w, 1, -1), 0); + assert_int_equal(mbedtls_x509write_crt_pem(&w, pem, sizeof(pem), mbedtls_ctr_drbg_random, &drbg), 0); + mbedtls_x509write_crt_free(&w); + + mbedtls_x509_crt_init(&server_cert); + assert_int_equal(mbedtls_x509_crt_parse(&server_cert, pem, strlen((char *)pem) + 1), 0); +} + +/* ------------------------------------------------------------------------- */ +/* the listener */ + +enum listener { + LISTENER_TLS, /* completes TCP and presents server_cert */ + LISTENER_RESET, /* completes TCP and closes during the handshake */ +}; + +struct server { + enum listener kind; + int fd; + int port; + pthread_t thread; +}; + +static void *serve(void *param) +{ + struct server *s = param; + int conn = accept(s->fd, NULL, NULL); + if (conn < 0) + return NULL; + + if (s->kind == LISTENER_TLS) { + mbedtls_ssl_config conf; + mbedtls_ssl_context ssl; + mbedtls_net_context net = {.fd = conn}; + + mbedtls_ssl_config_init(&conf); + mbedtls_ssl_init(&ssl); + mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT); + mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); + mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &drbg); + mbedtls_ssl_conf_own_cert(&conf, &server_cert, &server_key); + if (mbedtls_ssl_setup(&ssl, &conf) == 0) { + mbedtls_ssl_set_bio(&ssl, &net, mbedtls_net_send, mbedtls_net_recv, NULL); + /* Fails once the client rejects the certificate. */ + mbedtls_ssl_handshake(&ssl); + } + mbedtls_ssl_free(&ssl); + mbedtls_ssl_config_free(&conf); + } else { + char hello[16]; + (void)recv(conn, hello, sizeof(hello), 0); + } + close(conn); + return NULL; +} + +static void start_server(struct server *s, enum listener kind) +{ + struct sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)}; + socklen_t len = sizeof(addr); + + s->kind = kind; + s->fd = socket(AF_INET, SOCK_STREAM, 0); + assert_true(s->fd >= 0); + assert_int_equal(bind(s->fd, (struct sockaddr *)&addr, sizeof(addr)), 0); + assert_int_equal(listen(s->fd, 1), 0); + assert_int_equal(getsockname(s->fd, (struct sockaddr *)&addr, &len), 0); + s->port = ntohs(addr.sin_port); + assert_int_equal(pthread_create(&s->thread, NULL, serve, s), 0); +} + +static void stop_server(struct server *s) +{ + shutdown(s->fd, SHUT_RDWR); + pthread_join(s->thread, NULL); + close(s->fd); +} + +/* A port on the loopback interface with nothing listening. */ +static int closed_port(void) +{ + struct sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)}; + socklen_t len = sizeof(addr); + int fd = socket(AF_INET, SOCK_STREAM, 0); + assert_true(fd >= 0); + assert_int_equal(bind(fd, (struct sockaddr *)&addr, sizeof(addr)), 0); + assert_int_equal(getsockname(fd, (struct sockaddr *)&addr, &len), 0); + close(fd); + return ntohs(addr.sin_port); +} + +/* ------------------------------------------------------------------------- */ +/* connecting */ + +struct result { + int code; + bool proc_found; + char failure[32]; +}; + +/* Connects the RTMP output to rtmps://127.0.0.1:port/live and asks it, as + * the frontend does, whether the certificate failed to verify. */ +static struct result connect_to(int port) +{ + struct result r = {0}; + calldata_t cd = {0}; + + output_procs = proc_handler_create(); + struct rtmp_stream *stream = rtmp_stream_create(NULL, (obs_output_t *)&fake_output); + assert_non_null(stream); + dstr_printf(&stream->path, "rtmps://127.0.0.1:%d/live", port); + dstr_copy(&stream->key, "key"); + + r.code = try_connect(stream); + + r.proc_found = proc_handler_call(output_procs, "rtmp_tls_failure", &cd); + const char *failure = r.proc_found ? calldata_string(&cd, "failure") : NULL; + snprintf(r.failure, sizeof(r.failure), "%s", failure ? failure : "(none)"); + calldata_free(&cd); + + rtmp_stream_destroy(stream); + proc_handler_destroy(output_procs); + output_procs = NULL; + return r; +} + +/* The certificate is trusted but issued for another host: the host name does + * not match, so the start stops and does not fall back. */ +static void cert_for_other_host_does_not_fall_back(void **state) +{ + (void)state; + struct server s; + trusted_extra = &server_cert; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + trusted_extra = NULL; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, "cert_unverified"); +} + +/* The certificate is not trusted (and for another host): no fallback. */ +static void untrusted_cert_does_not_fall_back(void **state) +{ + (void)state; + struct server s; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, "cert_unverified"); +} + +/* The server closes the connection during the handshake: a connection + * failure, so the fallback stays open. */ +static void reset_handshake_still_falls_back(void **state) +{ + (void)state; + struct server s; + start_server(&s, LISTENER_RESET); + + struct result r = connect_to(s.port); + + stop_server(&s); + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, ""); +} + +/* Nothing listens: a refused connection, so the fallback stays open. */ +static void refused_connection_still_falls_back(void **state) +{ + (void)state; + struct result r = connect_to(closed_port()); + + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, ""); +} + +/* The system has no trusted root certificates: the start falls back, and the + * output names the cause so that the warning can say so. */ +static void empty_trust_store_reports_no_root_certs(void **state) +{ + (void)state; + struct server s; + store = STORE_EMPTY; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + store = STORE_SYSTEM; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, "no_root_certs"); +} + +static void missing_trust_store_reports_no_root_certs(void **state) +{ + (void)state; + struct server s; + store = STORE_MISSING; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + store = STORE_SYSTEM; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, "no_root_certs"); +} + +/* Without root certificates, a failure before the certificate is checked is + * still an ordinary connection failure. */ +static void empty_trust_store_reset_is_ordinary(void **state) +{ + (void)state; + struct server s; + store = STORE_EMPTY; + start_server(&s, LISTENER_RESET); + + struct result r = connect_to(s.port); + + stop_server(&s); + store = STORE_SYSTEM; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, ""); +} + +static int setup(void **state) +{ + (void)state; + const char *pers = "rtmps-cert-test"; + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&drbg); + assert_int_equal(mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, + strlen(pers)), + 0); + make_cert("other.example"); + return 0; +} + +static int teardown(void **state) +{ + (void)state; + mbedtls_x509_crt_free(&server_cert); + mbedtls_pk_free(&server_key); + mbedtls_ctr_drbg_free(&drbg); + mbedtls_entropy_free(&entropy); + return 0; +} + +int main(void) +{ + signal(SIGPIPE, SIG_IGN); + + const struct CMUnitTest tests[] = { + cmocka_unit_test(cert_for_other_host_does_not_fall_back), + cmocka_unit_test(untrusted_cert_does_not_fall_back), + cmocka_unit_test(reset_handshake_still_falls_back), + cmocka_unit_test(refused_connection_still_falls_back), + cmocka_unit_test(empty_trust_store_reports_no_root_certs), + cmocka_unit_test(missing_trust_store_reports_no_root_certs), + cmocka_unit_test(empty_trust_store_reset_is_ordinary), + }; + return cmocka_run_group_tests(tests, setup, teardown); +} diff --git a/test/cmocka/test_rtmps_fallback_warning.cpp b/test/cmocka/test_rtmps_fallback_warning.cpp new file mode 100644 index 0000000000..b747e6a10e --- /dev/null +++ b/test/cmocka/test_rtmps_fallback_warning.cpp @@ -0,0 +1,229 @@ +/* The RTMPS fallback (obs-rust/obs-studio#2205) through the shared fallback + * warning (obs_frontend_warn_fallback, docs/fallback-warning.md). When a + * stream that rtmp-services upgraded to RTMPS fails to connect, the frontend + * raises the warning with Cancel, its standard wording and the plain RTMP URL, + * and restarts over RTMP only when the answer is to continue. A certificate + * that did not verify never falls back; a system without trusted root + * certificates falls back with a message that says so. obs_frontend_warn_fallback is a fake + * here that records its arguments. */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +extern "C" { +#include +} + +#include "RtmpsFallback.hpp" + +static struct { + int calls; + std::string id; + std::string title; + std::string message; + bool can_cancel; + obs_frontend_fallback_cb cb; + void *param; +} raised; + +extern "C" void obs_frontend_warn_fallback(const char *id, const char *title, const char *message, bool can_cancel, + obs_frontend_fallback_cb cb, void *param) +{ + raised.calls++; + raised.id = id; + raised.title = title; + raised.message = message; + raised.can_cancel = can_cancel; + raised.cb = cb; + raised.param = param; +} + +static int reset(void **state) +{ + (void)state; + raised = {}; + return 0; +} + +static const char *url = "rtmp://ingest.example/app"; + +/* The warning has Cancel, the fallback's id, the title, and the message with + * the plain URL in it. Nothing is answered before the warning answers. */ +static void raises_shared_warning_with_cancel(void **state) +{ + (void)state; + int answers = 0; + RtmpsFallback::Warn("Streaming unencrypted (RTMP)", "OBS streams to %1 unencrypted.", url, + RtmpsFallback::Cause::Connection, [&](bool) { answers++; }); + + assert_int_equal(raised.calls, 1); + assert_true(raised.can_cancel); + assert_string_equal(raised.id.c_str(), "rtmp.unencrypted:rtmp://ingest.example/app"); + assert_string_equal(raised.title.c_str(), "Streaming unencrypted (RTMP)"); + assert_string_equal(raised.message.c_str(), "OBS streams to rtmp://ingest.example/app unencrypted."); + assert_non_null(raised.cb); + assert_int_equal(answers, 0); + + raised.cb(raised.param, true); + assert_int_equal(answers, 1); +} + +/* "Accept anyway" or the countdown: the answer is to continue over RTMP. */ +static void accept_passes_true(void **state) +{ + (void)state; + int answer = -1; + RtmpsFallback::Warn("T", "M %1", url, RtmpsFallback::Cause::Connection, + [&](bool accepted) { answer = accepted; }); + raised.cb(raised.param, true); + assert_int_equal(answer, 1); +} + +/* Cancel or a closed dialog: the start stops. */ +static void cancel_passes_false(void **state) +{ + (void)state; + int answer = -1; + RtmpsFallback::Warn("T", "M %1", url, RtmpsFallback::Cause::Connection, + [&](bool accepted) { answer = accepted; }); + raised.cb(raised.param, false); + assert_int_equal(answer, 0); +} + +/* Each server has its own id, so each server's first failure in a session + * shows the dialog. */ +static void id_names_the_server(void **state) +{ + (void)state; + if (RtmpsFallback::IdPerServer) { + assert_string_not_equal(RtmpsFallback::Id("rtmp://a.example/app").c_str(), + RtmpsFallback::Id("rtmp://b.example/app").c_str()); + } else { + assert_string_equal(RtmpsFallback::Id("rtmp://a.example/app").c_str(), "rtmp.unencrypted"); + } + assert_string_equal(RtmpsFallback::Id("").c_str(), "rtmp.unencrypted"); + assert_int_equal(strncmp(RtmpsFallback::Id(url).c_str(), "rtmp.unencrypted", 16), 0); +} + +/* Only a connection failure of a service stream falls back; a certificate + * that did not verify, multitrack video and other stop codes do not. */ +static void falls_back_only_on_connection_failure(void **state) +{ + (void)state; + using RtmpsFallback::Cause; + assert_true(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, Cause::Connection)); + assert_true(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, Cause::NoRootCerts)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, Cause::CertUnverified)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, true, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_BAD_PATH, false, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_INVALID_STREAM, false, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_DISCONNECTED, false, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_SUCCESS, false, Cause::Connection)); +} + +/* The RTMP output's rtmp_tls_failure strings map to the causes; anything + * else, or no answer, is an ordinary connection failure. */ +static void parses_the_outputs_cause(void **state) +{ + (void)state; + using RtmpsFallback::Cause; + assert_true(RtmpsFallback::ParseCause("cert_unverified") == Cause::CertUnverified); + assert_true(RtmpsFallback::ParseCause("no_root_certs") == Cause::NoRootCerts); + assert_true(RtmpsFallback::ParseCause("") == Cause::Connection); + assert_true(RtmpsFallback::ParseCause("other") == Cause::Connection); + assert_true(RtmpsFallback::ParseCause(nullptr) == Cause::Connection); +} + +/* No trusted root certificates: the fallback is offered with its own id and + * the message that names the cause. Other failures keep the normal message. */ +static void no_root_certs_warning_says_why(void **state) +{ + (void)state; + using RtmpsFallback::Cause; + assert_string_equal(RtmpsFallback::MessageKey(Cause::NoRootCerts), "Output.RtmpsFallback.NoRootCerts"); + assert_string_equal(RtmpsFallback::MessageKey(Cause::Connection), "Output.RtmpsFallback.Message"); + + int answer = -1; + RtmpsFallback::Warn("T", "No certificates; streaming to %1.", url, Cause::NoRootCerts, + [&](bool accepted) { answer = accepted; }); + assert_int_equal(raised.calls, 1); + assert_true(raised.can_cancel); + assert_string_equal(raised.id.c_str(), "rtmp.unencrypted.nocerts:rtmp://ingest.example/app"); + assert_string_equal(raised.message.c_str(), "No certificates; streaming to rtmp://ingest.example/app."); + raised.cb(raised.param, true); + assert_int_equal(answer, 1); + + assert_string_not_equal(RtmpsFallback::Id(url, Cause::NoRootCerts).c_str(), + RtmpsFallback::Id(url, Cause::Connection).c_str()); +} + +static std::string locale_value(const std::string &text, const char *key) +{ + std::istringstream in(text); + std::string line, prefix = std::string(key) + "=\""; + while (std::getline(in, line)) { + if (line.compare(0, prefix.size(), prefix) == 0 && line.size() > prefix.size()) { + return line.substr(prefix.size(), line.size() - prefix.size() - 1); + } + } + return ""; +} + +/* The strings follow docs/fallback-warning.md: the title "Streaming + * unencrypted (RTMP)", the term "unencrypted", and a message that names the + * URL. The buttons come from the shared warning, so the fallback has none. */ +static void wording_follows_the_standard(void **state) +{ + (void)state; + std::ifstream f(FRONTEND_LOCALE); + assert_true(f.good()); + std::stringstream ss; + ss << f.rdbuf(); + std::string text = ss.str(); + + assert_string_equal(locale_value(text, "Output.RtmpsFallback.Title").c_str(), "Streaming unencrypted (RTMP)"); + std::string message = locale_value(text, "Output.RtmpsFallback.Message"); + assert_non_null(strstr(message.c_str(), "unencrypted")); + assert_non_null(strstr(message.c_str(), "%1")); + assert_null(strstr(message.c_str(), "insecure")); + assert_null(strstr(message.c_str(), "unsafe")); + assert_string_equal(locale_value(text, "Output.RtmpsFallback.Proceed").c_str(), ""); + + /* No root certificates: what failed (the trusted certificates), what OBS + * does instead (unencrypted over RTMP), and what the user can do. */ + std::string nocerts = locale_value(text, "Output.RtmpsFallback.NoRootCerts"); + assert_false(nocerts.empty()); + assert_true(nocerts != message); + assert_non_null(strstr(nocerts.c_str(), "trusted certificates")); + assert_non_null(strstr(nocerts.c_str(), "unencrypted")); + assert_non_null(strstr(nocerts.c_str(), "CA certificates package")); + assert_non_null(strstr(nocerts.c_str(), "Cancel")); + assert_non_null(strstr(nocerts.c_str(), "%1")); + const char *what = strstr(nocerts.c_str(), "trusted certificates"); + const char *instead = strstr(nocerts.c_str(), "unencrypted"); + const char *todo = strstr(nocerts.c_str(), "CA certificates package"); + assert_true(what < instead && instead < todo); + assert_string_equal(locale_value(text, "FallbackWarning.AcceptAnyway").c_str(), "Accept anyway"); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup(raises_shared_warning_with_cancel, reset), + cmocka_unit_test_setup(accept_passes_true, reset), + cmocka_unit_test_setup(cancel_passes_false, reset), + cmocka_unit_test_setup(id_names_the_server, reset), + cmocka_unit_test_setup(falls_back_only_on_connection_failure, reset), + cmocka_unit_test_setup(parses_the_outputs_cause, reset), + cmocka_unit_test_setup(no_root_certs_warning_says_why, reset), + cmocka_unit_test_setup(wording_follows_the_standard, reset), + }; + return cmocka_run_group_tests(tests, nullptr, nullptr); +} diff --git a/test/cmocka/test_rtmps_upgrade.c b/test/cmocka/test_rtmps_upgrade.c new file mode 100644 index 0000000000..ff51f563aa --- /dev/null +++ b/test/cmocka/test_rtmps_upgrade.c @@ -0,0 +1,341 @@ +/* rtmp-services connects to built-in services over RTMPS when their ingest + * server is known to accept it (obs-rust/obs-studio#2205). The service still + * stores the plain rtmp:// server the user picked; the upgrade happens when the + * output asks for the URL, and the service keeps the plain URL so the frontend + * can offer it if the encrypted connection fails. + * + * The cases load the real rtmp-services module with a services.json written + * by the test. They need obs_startup(), which needs a display on Linux, so + * they skip without one (Tier 2 runs ctest under xvfb-run). */ + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#ifdef RTMP_SERVICES_MODULE_PATH +#include "rtmp-format-ver.h" +#endif + +/* An ingest server that accepts RTMPS on port 443 and is on the list. */ +#define LISTED_PLAIN "rtmp://live.twitch.tv/app" +#define LISTED_RTMPS "rtmps://live.twitch.tv:443/app" + +static bool module_up; + +static void quiet_log(int level, const char *format, va_list args, void *param) +{ + (void)level; + (void)format; + (void)args; + (void)param; +} + +static obs_service_t *make_service(const char *id, const char *service, const char *server) +{ + obs_data_t *settings = obs_data_create(); + if (service) + obs_data_set_string(settings, "service", service); + obs_data_set_string(settings, "server", server); + obs_data_set_string(settings, "key", "test"); + obs_service_t *s = obs_service_create(id, "test service", settings, NULL); + obs_data_release(settings); + return s; +} + +static const char *url_of(obs_service_t *s) +{ + return obs_service_get_connect_info(s, OBS_SERVICE_CONNECT_INFO_SERVER_URL); +} + +/* The plain URL the service would fall back to, or "" when it did not + * upgrade the URL (or the module has no fallback command). */ +static void fallback_of(obs_service_t *s, char *buf, size_t size) +{ + calldata_t cd = {0}; + const char *plain = NULL; + + buf[0] = '\0'; + calldata_set_ptr(&cd, "service", s); + if (proc_handler_call(obs_get_proc_handler(), "rtmp_services_rtmps_fallback", &cd) && + calldata_get_string(&cd, "plain_url", &plain) && plain) + snprintf(buf, size, "%s", plain); + calldata_free(&cd); +} + +static bool set_plain_rtmp(obs_service_t *s, bool enabled) +{ + calldata_t cd = {0}; + calldata_set_ptr(&cd, "service", s); + calldata_set_bool(&cd, "enabled", enabled); + bool ok = proc_handler_call(obs_get_proc_handler(), "rtmp_services_set_plain_rtmp", &cd); + calldata_free(&cd); + return ok; +} + +#define SKIP_WITHOUT_MODULE() \ + do { \ + if (!module_up) { \ + print_message("rtmp-services module not loaded, skipping\n"); \ + skip(); \ + } \ + } while (0) + +/* A built-in service on a listed host connects over RTMPS on port 443, with + * the same host and app path. */ +static void module_listed_server_connects_over_rtmps(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_PLAIN); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_RTMPS); + obs_service_release(s); +} + +/* The service keeps the plain URL so the frontend can offer it. */ +static void module_listed_server_reports_plain_fallback(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_PLAIN); + assert_non_null(s); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, LISTED_PLAIN); + obs_service_release(s); +} + +/* After the user accepts the fallback, the service hands out the plain URL + * until the frontend clears it for the next start. */ +static void module_plain_rtmp_after_fallback(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_PLAIN); + assert_non_null(s); + assert_true(set_plain_rtmp(s, true)); + assert_string_equal(url_of(s), LISTED_PLAIN); + assert_true(set_plain_rtmp(s, false)); + assert_string_equal(url_of(s), LISTED_RTMPS); + obs_service_release(s); +} + +/* A host that is not on the list stays on plain RTMP and has no fallback. */ +static void module_unlisted_host_stays_plain(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_common", "Twitch", "rtmp://127.0.0.1/app"); + assert_non_null(s); + assert_string_equal(url_of(s), "rtmp://127.0.0.1/app"); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, ""); + obs_service_release(s); +} + +/* The list is per service: a listed host under another service name stays + * plain. */ +static void module_unlisted_service_stays_plain(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Test Service", LISTED_PLAIN); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_PLAIN); + obs_service_release(s); +} + +/* Only the default RTMP port is mapped to 443. */ +static void module_other_port_stays_plain(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", "rtmp://live.twitch.tv:1936/app"); + assert_non_null(s); + assert_string_equal(url_of(s), "rtmp://live.twitch.tv:1936/app"); + obs_service_release(s); + + s = make_service("rtmp_common", "Twitch", "rtmp://live.twitch.tv:1935/app"); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_RTMPS); + obs_service_release(s); +} + +/* The host match ignores case, and the query string is kept. */ +static void module_host_case_and_query(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", "rtmp://LIVE.Twitch.TV/app?x=1"); + assert_non_null(s); + assert_string_equal(url_of(s), "rtmps://LIVE.Twitch.TV:443/app?x=1"); + obs_service_release(s); +} + +/* A server the user typed into a custom service is never changed. */ +static void module_custom_server_unchanged(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_custom", NULL, LISTED_PLAIN); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_PLAIN); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, ""); + obs_service_release(s); +} + +/* A server that is already rtmps:// is used as is, with no fallback. */ +static void module_rtmps_server_has_no_fallback(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_RTMPS); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_RTMPS); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, ""); + obs_service_release(s); +} + +#ifdef RTMP_SERVICES_MODULE_PATH +/* rtmp-services only upgrades when an output handles RTMPS; this stands in + * for obs-outputs' RTMP output and is never started. */ +static const char *output_name(void *type_data) +{ + (void)type_data; + return "RTMP stand-in"; +} + +static void *output_create(obs_data_t *settings, obs_output_t *output) +{ + (void)settings; + return output; +} + +static void output_destroy(void *data) +{ + (void)data; +} + +static bool output_start(void *data) +{ + (void)data; + return false; +} + +static void output_stop(void *data, uint64_t ts) +{ + (void)data; + (void)ts; +} + +static void output_encoded_packet(void *data, struct encoder_packet *packet) +{ + (void)data; + (void)packet; +} +#endif + +static int setup(void) +{ +#ifdef RTMP_SERVICES_MODULE_PATH + base_set_log_handler(quiet_log, NULL); + + char json[1024]; + snprintf(json, sizeof(json), + "{\"format_version\": %d, \"services\": [" + "{\"name\": \"Twitch\", \"common\": true, \"servers\": [" + "{\"name\": \"Default\", \"url\": \"" LISTED_PLAIN "\"}," + "{\"name\": \"Local\", \"url\": \"rtmp://127.0.0.1/app\"}]}," + "{\"name\": \"Test Service\", \"common\": true, \"servers\": [" + "{\"name\": \"Default\", \"url\": \"" LISTED_PLAIN "\"}]}]}", + RTMP_SERVICES_FORMAT_VERSION); + + if (os_mkdirs(SERVICES_DATA_DIR) == MKDIR_ERROR || + !os_quick_write_utf8_file(SERVICES_DATA_DIR "/services.json", json, strlen(json), false)) { + fprintf(stderr, "could not write " SERVICES_DATA_DIR "/services.json\n"); + return -1; + } + + /* The module config path is under a regular file, so rtmp-services + * has no config directory: it neither reads a cached services.json + * nor starts its service update download. */ + if (!obs_startup("en-US", SERVICES_DATA_DIR "/services.json", NULL)) { + fprintf(stderr, "cases skipped: obs_startup failed (no display?)\n"); + return 0; + } + + struct obs_output_info info = { + .id = "test_rtmp_output", + .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED | OBS_OUTPUT_SERVICE, + .get_name = output_name, + .create = output_create, + .destroy = output_destroy, + .start = output_start, + .stop = output_stop, + .encoded_packet = output_encoded_packet, + .encoded_video_codecs = "h264", + .encoded_audio_codecs = "aac", + .protocols = "RTMP;RTMPS", + }; + obs_register_output(&info); + + obs_module_t *module = NULL; + if (obs_open_module(&module, RTMP_SERVICES_MODULE_PATH, SERVICES_DATA_DIR) != MODULE_SUCCESS || + !obs_init_module(module)) { + fprintf(stderr, "could not load rtmp-services\n"); + return -1; + } + + module_up = true; +#else + (void)quiet_log; + fprintf(stderr, "cases skipped: rtmp-services is not a loadable module here\n"); +#endif + return 0; +} + +int main(void) +{ + if (setup() != 0) + return 1; + + const struct CMUnitTest tests[] = { + cmocka_unit_test(module_listed_server_connects_over_rtmps), + cmocka_unit_test(module_listed_server_reports_plain_fallback), + cmocka_unit_test(module_plain_rtmp_after_fallback), + cmocka_unit_test(module_unlisted_host_stays_plain), + cmocka_unit_test(module_unlisted_service_stays_plain), + cmocka_unit_test(module_other_port_stays_plain), + cmocka_unit_test(module_host_case_and_query), + cmocka_unit_test(module_custom_server_unchanged), + cmocka_unit_test(module_rtmps_server_has_no_fallback), + }; + int failed = cmocka_run_group_tests(tests, NULL, NULL); + + if (module_up) + obs_shutdown(); + return failed; +} diff --git a/test/cmocka/tests.d/test_rtmps_fallback_warning.cmake b/test/cmocka/tests.d/test_rtmps_fallback_warning.cmake new file mode 100644 index 0000000000..e03f4d32df --- /dev/null +++ b/test/cmocka/tests.d/test_rtmps_fallback_warning.cmake @@ -0,0 +1,26 @@ +# The RTMPS fallback (obs-rust/obs-studio#2205) raises the shared fallback +# warning (obs_frontend_warn_fallback, #2218) with the right id, wording and +# Cancel, passes its answer on, and never falls back when the server's +# certificate did not verify. The warning itself is a fake here; its dialog is +# tested in test_fallback_warning_dialog. Needs Qt Core only. +find_package(Qt6 QUIET COMPONENTS Core) +if(TARGET Qt6::Core AND NOT OS_WINDOWS) + add_executable( + test_rtmps_fallback_warning + test_rtmps_fallback_warning.cpp + "${CMAKE_SOURCE_DIR}/frontend/utility/RtmpsFallback.hpp" + ) + target_compile_definitions( + test_rtmps_fallback_warning + PRIVATE FRONTEND_LOCALE="${CMAKE_SOURCE_DIR}/frontend/data/locale/en-US.ini" + ) + target_include_directories( + test_rtmps_fallback_warning + PRIVATE ${CMOCKA_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}/frontend/api" "${CMAKE_SOURCE_DIR}/frontend/utility" + ) + target_link_libraries(test_rtmps_fallback_warning PRIVATE OBS::libobs Qt6::Core ${CMOCKA_LIBRARIES}) + add_dependencies(cmocka-tests test_rtmps_fallback_warning) + add_test(test_rtmps_fallback_warning ${CMAKE_CURRENT_BINARY_DIR}/test_rtmps_fallback_warning) + set_tests_properties(test_rtmps_fallback_warning PROPERTIES TIMEOUT 30) + cmocka_qt_runtime(test_rtmps_fallback_warning Qt6::Core) +endif() diff --git a/test/cmocka/tests.d/test_rtmps_upgrade.cmake b/test/cmocka/tests.d/test_rtmps_upgrade.cmake new file mode 100644 index 0000000000..04e5a9a636 --- /dev/null +++ b/test/cmocka/tests.d/test_rtmps_upgrade.cmake @@ -0,0 +1,21 @@ +# Built-in services connect over RTMPS when their ingest server is known to +# accept it, and keep the plain URL for the frontend's fallback +# (obs-rust/obs-studio#2205). The cases load the real rtmp-services module, so +# they only run on Linux, and obs_startup needs a display there (run under +# xvfb-run, as Tier 2 does). +add_executable(test_rtmps_upgrade test_rtmps_upgrade.c) +target_include_directories(test_rtmps_upgrade PRIVATE ${CMOCKA_INCLUDE_DIR}) +target_link_libraries(test_rtmps_upgrade PRIVATE OBS::libobs ${CMOCKA_LIBRARIES}) +if(OS_LINUX AND TARGET rtmp-services) + target_compile_definitions( + test_rtmps_upgrade + PRIVATE + RTMP_SERVICES_MODULE_PATH="$" + SERVICES_DATA_DIR="${CMAKE_CURRENT_BINARY_DIR}/test_rtmps_upgrade_data" + ) + target_include_directories(test_rtmps_upgrade PRIVATE "${CMAKE_SOURCE_DIR}/plugins/rtmp-services") + add_dependencies(test_rtmps_upgrade rtmp-services) +endif() +add_dependencies(cmocka-tests test_rtmps_upgrade) +add_test(NAME test_rtmps_upgrade COMMAND test_rtmps_upgrade) +set_tests_properties(test_rtmps_upgrade PROPERTIES TIMEOUT 60) diff --git a/tools/rtmps-endpoint-check.py b/tools/rtmps-endpoint-check.py new file mode 100755 index 0000000000..672a444e00 --- /dev/null +++ b/tools/rtmps-endpoint-check.py @@ -0,0 +1,330 @@ +#!/usr/bin/env -S uv run --script +# /// script +# requires-python = ">=3.10" +# /// +"""Check whether RTMP ingest servers also work over RTMPS, without an account. + +plugins/rtmp-services/service-specific/rtmps-upgrade.c.h lists the hosts that +pass. Rerun this when services.json or Twitch's ingest list changes, and add +a host to that list only when every server of its service passes. + +For each server URL this runs, over plain RTMP and over RTMPS (same host and +app path, port 443 unless the URL says otherwise): + DNS + TCP, TLS with certificate/hostname verification (RTMPS only), + RTMP handshake, connect(app), then publish with a fake key. +No media is ever sent. A server "matches" when RTMPS gets as far as plain RTMP +and the fake-key publish is answered the same way. + +Usage: + uv run tools/rtmps-endpoint-check.py plugins/rtmp-services/data/services.json \ + --twitch-feed --twitch-list > results.jsonl + +Each output line is one server: its verdict (pass, fail, check, untestable) +and how far plain RTMP and RTMPS got. +""" +import argparse, json, os, socket, ssl, struct, sys, time +from urllib.parse import urlsplit + +FAKE_KEY = "rtmps-endpoint-check-invalid-key" +TIMEOUT = 8 + + +# --- AMF0 ------------------------------------------------------------------- +def amf_enc(v): + if v is None: + return b"\x05" + if isinstance(v, bool): + return b"\x01" + (b"\x01" if v else b"\x00") + if isinstance(v, (int, float)): + return b"\x00" + struct.pack(">d", float(v)) + if isinstance(v, str): + b = v.encode() + return b"\x02" + struct.pack(">H", len(b)) + b + if isinstance(v, dict): + out = b"\x03" + for k, x in v.items(): + kb = k.encode() + out += struct.pack(">H", len(kb)) + kb + amf_enc(x) + return out + b"\x00\x00\x09" + raise TypeError(v) + + +def amf_dec(b, i=0): + t = b[i] + i += 1 + if t == 0x00: + return struct.unpack(">d", b[i:i + 8])[0], i + 8 + if t == 0x01: + return bool(b[i]), i + 1 + if t == 0x02: + n = struct.unpack(">H", b[i:i + 2])[0] + return b[i + 2:i + 2 + n].decode("utf-8", "replace"), i + 2 + n + if t in (0x03, 0x08): + if t == 0x08: + i += 4 + o = {} + while True: + n = struct.unpack(">H", b[i:i + 2])[0] + if n == 0 and b[i + 2] == 0x09: + return o, i + 3 + k = b[i + 2:i + 2 + n].decode("utf-8", "replace") + o[k], i = amf_dec(b, i + 2 + n) + if t in (0x05, 0x06): + return None, i + raise ValueError("amf type %d" % t) + + +def amf_all(b): + out, i = [], 0 + while i < len(b): + try: + v, i = amf_dec(b, i) + except Exception: + break + out.append(v) + return out + + +# --- RTMP chunk stream ------------------------------------------------------ +class Conn: + def __init__(self, sock): + self.s = sock + self.buf = b"" + self.in_chunk = 128 + self.prev = {} + + def recv_exact(self, n): + while len(self.buf) < n: + d = self.s.recv(65536) + if not d: + raise ConnectionError("closed by server") + self.buf += d + out, self.buf = self.buf[:n], self.buf[n:] + return out + + def send_msg(self, csid, mtype, payload, msid=0): + hdr = bytes([csid & 0x3F]) + b"\x00\x00\x00" + struct.pack(">I", len(payload))[1:] + bytes([mtype]) + struct.pack("> 6, b0 & 0x3F + if csid == 0: + csid = 64 + self.recv_exact(1)[0] + elif csid == 1: + x = self.recv_exact(2) + csid = 64 + x[0] + x[1] * 256 + p = dict(self.prev.get(csid, {"ts": 0, "len": 0, "type": 0, "msid": 0})) + if fmt <= 2: + ts = int.from_bytes(self.recv_exact(3), "big") + if fmt <= 1: + p["len"] = int.from_bytes(self.recv_exact(3), "big") + p["type"] = self.recv_exact(1)[0] + if fmt == 0: + p["msid"] = struct.unpack("I", have[:4])[0] & 0x7FFFFFFF + continue + return p["type"], have + + +def status_of(vals): + for v in vals: + if isinstance(v, dict) and "code" in v: + return v.get("code"), v.get("level"), str(v.get("description", ""))[:160] + return None, None, None + + +def attempt(host, port, app, tcurl, use_tls): # noqa: C901 + r = {"host": host, "port": port, "tls": use_tls, "stage": "dns"} + try: + socket.getaddrinfo(host, port) + r["stage"] = "tcp" + raw = socket.create_connection((host, port), timeout=TIMEOUT) + raw.settimeout(TIMEOUT) + s = raw + if use_tls: + r["stage"] = "tls" + s = ssl.create_default_context().wrap_socket(raw, server_hostname=host) + r["tls_version"] = s.version() + c = Conn(s) + r["stage"] = "handshake" + c1 = struct.pack(">I", 0) + b"\x00" * 4 + os.urandom(1528) + s.sendall(b"\x03" + c1) + s0 = c.recv_exact(1) + if s0 != b"\x03": + raise ValueError("S0=%s (not RTMP)" % s0.hex()) + s1 = c.recv_exact(1536) + c.recv_exact(1536) + s.sendall(s1) + r["stage"] = "connect" + c.invoke("connect", 1, {"app": app, "type": "nonprivate", "flashVer": "FMLE/3.0 (compatible; FMSc/1.0)", "tcUrl": tcurl}) + while True: + t, body = c.read_msg() + if t == 20: + vals = amf_all(body) + if vals and vals[0] in ("_result", "_error") and vals[1] == 1: + code, _, desc = status_of(vals) + r["connect"] = code or vals[0] + if vals[0] == "_error": + r["connect_desc"] = desc + return r + break + r["stage"] = "publish" + c.invoke("releaseStream", 2, None, FAKE_KEY) + c.invoke("FCPublish", 3, None, FAKE_KEY) + c.invoke("createStream", 4, None) + msid = None + deadline = time.time() + TIMEOUT + while time.time() < deadline: + t, body = c.read_msg() + if t != 20: + continue + vals = amf_all(body) + if not vals: + continue + if vals[0] == "_result" and vals[1] == 4 and msid is None: + msid = int(vals[3]) if len(vals) > 3 and isinstance(vals[3], float) else 1 + c.invoke("publish", 5, None, FAKE_KEY, "live", csid=4, msid=msid) + continue + if vals[0] == "_error" and vals[1] == 4: + r["publish"] = "createStream:_error" + return r + if vals[0] in ("onStatus", "onFCPublish"): + code, level, desc = status_of(vals) + if code and (vals[0] == "onStatus" or level == "error"): + r["publish"] = code + r["publish_desc"] = desc + return r + r["publish"] = "timeout" + except ConnectionError as e: + r["error"] = str(e) + if r["stage"] == "publish": + r["publish"] = "closed" + except ssl.SSLCertVerificationError as e: + r["error"] = "cert: " + (e.verify_message or str(e)) + except Exception as e: + r["error"] = "%s: %s" % (type(e).__name__, e) + finally: + try: + s.close() + except Exception: + pass + return r + + +def split(url): + u = urlsplit(url) + path = u.path.lstrip("/") + app = path.rstrip("/") + if u.query: + app += "?" + u.query + return u.hostname, u.port, app + + +def outcome(r): + if "publish" in r: + return "publish:" + r["publish"] + if "connect" in r: + return "connect:" + r["connect"] + return "fail@" + r["stage"] + + +def check(service, url): + host, port, app = split(url) + is_tls = url.lower().startswith("rtmps://") + plain_port = 1935 if is_tls else (port or 1935) + tls_port = port if is_tls and port else 443 + plain = attempt(host, plain_port, app, "rtmp://%s:%d/%s" % (host, plain_port, app), False) + time.sleep(1) + tls = attempt(host, tls_port, app, "rtmps://%s:%d/%s" % (host, tls_port, app), True) + po, to = outcome(plain), outcome(tls) + if to.startswith("publish:") and to != "publish:NetStream.Publish.Start" and (po == to or po.startswith("fail@")): + verdict = "pass" + elif to == "publish:NetStream.Publish.Start": + verdict = "check" # fake key accepted; can't tell what a real key does + elif po.startswith("fail@") and to.startswith("fail@"): + verdict = "untestable" + elif po.startswith("publish:") and to.startswith("publish:"): + verdict = "check" # both answer publish, but differently + else: + verdict = "fail" + return {"service": service, "url": url, "verdict": verdict, "plain": po, "rtmps": to, "plain_detail": plain, "rtmps_detail": tls} + + +def main(): # noqa: C901 + ap = argparse.ArgumentParser() + ap.add_argument("services") + ap.add_argument("--twitch-feed", action="store_true") + ap.add_argument("--extra", nargs="*", default=[], help="NAME=URL") + ap.add_argument("--only", nargs="*") + ap.add_argument("--jobs", type=int, default=8, help="services checked in parallel") + ap.add_argument("--twitch-list", action="store_true", help="with --twitch-feed, also check the Twitch servers in services.json") + a = ap.parse_args() + d = json.load(open(a.services)) + jobs = [] + for s in d["services"]: + if a.only and s["name"] not in a.only: + continue + if s["name"] == "Twitch" and a.twitch_feed and not a.twitch_list: + continue + for x in s.get("servers", []): + if x["url"].lower().startswith(("rtmp://", "rtmps://")): + jobs.append((s["name"], x["url"])) + if a.twitch_feed: + import urllib.request + feed = json.load(urllib.request.urlopen("https://ingest.twitch.tv/ingests", timeout=15)) + for i in feed["ingests"]: + jobs.append(("Twitch", i["url_template_secure"].replace("/{stream_key}", ""))) + # The built-in fallback when the list can't be loaded (twitch.c.h). + jobs.append(("Twitch", "rtmp://live.twitch.tv/app")) + for e in a.extra: + n, u = e.split("=", 1) + jobs.append((n, u)) + seen, by_service = set(), {} + for name, url in jobs: + key = (name, url.lower().replace("rtmps://", "rtmp://").split("?")[0].rstrip("/")) + if key in seen: + continue + seen.add(key) + by_service.setdefault(name, []).append(url) + + import threading + from concurrent.futures import ThreadPoolExecutor + lock = threading.Lock() + + def run_service(name): + # Each worker takes one provider and checks its servers in sequence. + for url in by_service[name]: + r = check(name, url) + with lock: + print(json.dumps(r, ensure_ascii=False), flush=True) + time.sleep(1) + + with ThreadPoolExecutor(max_workers=a.jobs) as ex: + list(ex.map(run_service, by_service)) + + +if __name__ == "__main__": + main()