diff --git a/docs/configuration.md b/docs/configuration.md
index 6024df7cdb6..bfab9fabaf0 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -1784,6 +1784,9 @@ consent prompt for directory ACLs, so the Web UI shows setup steps for correctin
### lan_encryption_mode
+@note{Authenticated encrypted RTSP setup is always required. This setting controls audio/video encryption and
+cannot enable legacy plaintext RTSP setup.}
+
| Description |
@@ -1821,6 +1824,8 @@ consent prompt for directory ACLs, so the Web UI shows setup steps for correctin
### wan_encryption_mode
+@note{Authenticated encrypted RTSP setup is always required, independently of this audio/video encryption policy.}
+
| Description |
diff --git a/docs/getting_started.md b/docs/getting_started.md
index 4f1bb107ae7..337cf375590 100644
--- a/docs/getting_started.md
+++ b/docs/getting_started.md
@@ -697,6 +697,7 @@ by default. You may replace *localhost* with your internal ip address.
> [!CAUTION]
> If running for the first time, make sure to note the username and password that you created.
+> The `origin_web_ui_allowed` network restriction also applies to initial credential creation.
1. Change the web-ui to your desired theme, using the dropdown menu in the navbar.

@@ -706,7 +707,9 @@ by default. You may replace *localhost* with your internal ip address.

4. Find Moonlight clients and other tools for Sunshine in the `Featured Apps` tab.

-5. In Moonlight, you may need to add the PC manually.
+5. Use a Moonlight client that supports authenticated encrypted RTSP setup. Clients without this support cannot
+ start or resume a stream and receive an error asking them to update. This requirement applies even when
+ audio/video encryption is disabled. In Moonlight, you may need to add the PC manually.
6. When Moonlight requests for you insert the pin:
- Login to the web-ui
@@ -822,3 +825,9 @@ Tutorials and Guides are community generated. Want to contribute? Reach out to u
[latest-release]: https://github.com/SunshineStream/Sunshine/releases/latest
+
+### Revoking client access
+
+Unpairing or disabling a client revokes its active streams and pending launches. Other paired clients remain
+permitted to connect. Re-enabling or pairing a client again requires a new launch; it does not restore an
+old connection's authorization.
diff --git a/src/confighttp.cpp b/src/confighttp.cpp
index 7a5f0c69a69..34488a5e437 100644
--- a/src/confighttp.cpp
+++ b/src/confighttp.cpp
@@ -640,12 +640,12 @@ namespace confighttp {
}
/**
- * @brief Authenticate the user.
+ * @brief Enforce the Web UI remote-address policy, including initial setup.
* @param response The HTTP response object.
* @param request The HTTP request object.
- * @return True if the user is authenticated, false otherwise.
+ * @return True when the source address is allowed, false otherwise.
*/
- bool authenticate(const resp_https_t &response, const req_https_t &request) {
+ bool check_access(const resp_https_t &response, const req_https_t &request) {
auto address = net::addr_to_normalized_string(request->remote_endpoint().address());
if (const auto ip_type = net::from_address(address); ip_type > http::origin_web_ui_allowed) {
@@ -654,6 +654,20 @@ namespace confighttp {
return false;
}
+ return true;
+ }
+
+ /**
+ * @brief Authenticate credentials after enforcing the Web UI network policy.
+ * @param response The HTTP response object.
+ * @param request The HTTP request object.
+ * @return True when the source address and credentials are authorized.
+ */
+ bool authenticate(const resp_https_t &response, const req_https_t &request) {
+ if (!check_access(response, request)) {
+ return false;
+ }
+
// If credentials are shown, redirect the user to a /welcome page
if (config::sunshine.username.empty()) {
send_redirect(response, request, "/welcome");
@@ -1519,9 +1533,14 @@ namespace confighttp {
nlohmann::json output_tree;
const nlohmann::json input_tree = nlohmann::json::parse(ss);
const std::string uuid = input_tree.value("uuid", "");
+ const auto cert = nvhttp::get_cert_by_uuid(uuid);
const bool removed = nvhttp::unpair_client(uuid);
output_tree["status"] = removed;
+ if (removed && !cert.empty()) {
+ rtsp_stream::terminate_sessions_by_cert(cert);
+ }
+
if (removed && nvhttp::get_all_clients().empty()) {
proc::proc.terminate();
}
@@ -1829,6 +1848,9 @@ namespace confighttp {
* @api_examples{/api/password|:| POST|:| {"currentUsername":"admin","currentPassword":"admin","newUsername":"admin","newPassword":"admin","confirmNewPassword":"admin"}}
*/
void savePassword(const resp_https_t &response, const req_https_t &request) {
+ if (!check_access(response, request)) {
+ return;
+ }
if (!check_content_type(response, request, "application/json")) {
return;
}
diff --git a/src/confighttp.h b/src/confighttp.h
index fb379312436..c1bfcf8beab 100644
--- a/src/confighttp.h
+++ b/src/confighttp.h
@@ -40,6 +40,8 @@ namespace confighttp {
void send_response(const resp_https_t &response, const nlohmann::json &output_tree);
void send_unauthorized(const resp_https_t &response, const req_https_t &request);
void send_redirect(const resp_https_t &response, const req_https_t &request, const char *path);
+ bool check_access(const resp_https_t &response, const req_https_t &request);
+ void savePassword(const resp_https_t &response, const req_https_t &request);
bool authenticate(const resp_https_t &response, const req_https_t &request);
void not_found(const resp_https_t &response, const std::string &error_message = "Not Found");
void bad_request(const resp_https_t &response, const std::string &error_message = "Bad Request");
diff --git a/src/control_protocol.h b/src/control_protocol.h
new file mode 100644
index 00000000000..4e3fe5f398d
--- /dev/null
+++ b/src/control_protocol.h
@@ -0,0 +1,80 @@
+/**
+ * @file src/control_protocol.h
+ * @brief Bounded parsing of GameStream control envelopes before decryption.
+ */
+#pragma once
+
+// standard includes
+#include
+#include
+#include
+#include
+#include
+
+// lib includes
+#include
+
+namespace stream::control {
+ constexpr std::size_t TAG_SIZE = 16; ///< AES-GCM tag size on the control wire protocol.
+
+ /**
+ * @brief Validated encrypted envelope excluding the outer message type.
+ */
+ struct encrypted_message_t {
+ std::uint32_t sequence; ///< Sequence used to construct the message IV.
+ std::string_view tagged_cipher; ///< Bounded view of the authentication tag and ciphertext.
+ };
+
+ /**
+ * @brief Validate an encrypted envelope before reading its sequence or ciphertext.
+ * @param payload Wire bytes after the two-byte outer message type.
+ * @return Parsed envelope, or no value for an incomplete or inconsistent length.
+ */
+ inline std::optional parse_encrypted(std::string_view payload) {
+ constexpr std::size_t header_size = sizeof(std::uint16_t) + sizeof(std::uint32_t);
+ constexpr std::size_t minimum_length = sizeof(std::uint32_t) + TAG_SIZE + 4;
+ if (payload.size() < header_size) {
+ return std::nullopt;
+ }
+ const auto *bytes = reinterpret_cast(payload.data());
+ const auto length = boost::endian::endian_load(bytes);
+ if (length < minimum_length || length != payload.size() - sizeof(length)) {
+ return std::nullopt;
+ }
+ const auto sequence = boost::endian::endian_load(bytes + sizeof(length));
+ return encrypted_message_t {sequence, payload.substr(header_size)};
+ }
+
+ /**
+ * @brief Validate the big-endian length prefix of legacy encrypted input.
+ * @param payload Wire bytes after the outer message type.
+ * @return Bounded tag and ciphertext, or no value for an invalid length.
+ */
+ inline std::optional parse_legacy_input(std::string_view payload) {
+ constexpr auto header_size = sizeof(std::uint32_t);
+ if (payload.size() < header_size) {
+ return std::nullopt;
+ }
+ if (const auto length = boost::endian::endian_load(reinterpret_cast(payload.data())); length < TAG_SIZE || length != payload.size() - header_size) {
+ return std::nullopt;
+ }
+ return payload.substr(header_size);
+ }
+
+ /**
+ * @brief Read a complete decrypted secondary control header.
+ * @param plaintext Complete authenticated plaintext including its secondary header.
+ * @return Message type and actual authenticated payload, or no value for a short header.
+ */
+ inline std::optional> parse_plaintext(std::string_view plaintext) {
+ constexpr std::size_t header_size = 4;
+ if (plaintext.size() < header_size) {
+ return std::nullopt;
+ }
+ const auto *bytes = reinterpret_cast(plaintext.data());
+ const auto type = boost::endian::endian_load(bytes);
+ // Preserve legacy header semantics: dispatch is bounded by authenticated bytes,
+ // not by the advisory secondary payloadLength field.
+ return std::pair {type, plaintext.substr(header_size)};
+ }
+} // namespace stream::control
diff --git a/src/crypto.cpp b/src/crypto.cpp
index 4837fa82103..9bd386f57b1 100644
--- a/src/crypto.cpp
+++ b/src/crypto.cpp
@@ -2,6 +2,10 @@
* @file src/crypto.cpp
* @brief Definitions for cryptography functions.
*/
+// standard includes
+#include
+#include
+
// lib includes
#include
#include
@@ -142,33 +146,31 @@ namespace crypto {
}
int gcm_t::decrypt(const std::string_view &tagged_cipher, std::vector &plaintext, aes_t *iv) {
+ plaintext.clear();
+ if (tagged_cipher.size() < tag_size || tagged_cipher.size() - tag_size > static_cast(std::numeric_limits::max() - 16) || key.size() != 16 || iv == nullptr || iv->empty() || iv->size() > static_cast(std::numeric_limits::max())) {
+ return -1;
+ }
if (!decrypt_ctx && init_decrypt_gcm(decrypt_ctx, &key, iv, padding)) {
+ decrypt_ctx.reset();
return -1;
}
- // Calling with cipher == nullptr results in a parameter change
- // without requiring a reallocation of the internal cipher ctx.
+ // Reset message state while retaining the cipher context and negotiated key.
if (EVP_DecryptInit_ex(decrypt_ctx.get(), nullptr, nullptr, nullptr, iv->data()) != 1) {
- return false;
+ return -1;
}
- auto cipher = tagged_cipher.substr(tag_size);
- auto tag = tagged_cipher.substr(0, tag_size);
-
- plaintext.resize(round_to_pkcs7_padded(cipher.size()));
+ const auto cipher = tagged_cipher.substr(tag_size);
+ // OpenSSL accepts a mutable tag buffer, so retain a writable copy.
+ std::array tag;
+ std::copy_n(tagged_cipher.begin(), tag.size(), tag.begin());
+ // Reserve a writable output byte even for an authenticated empty message.
+ plaintext.resize(std::max(16, round_to_pkcs7_padded(cipher.size())));
int final_outlen;
int update_outlen;
-
- if (EVP_DecryptUpdate(decrypt_ctx.get(), plaintext.data(), &update_outlen, (const std::uint8_t *) cipher.data(), (int) cipher.size()) != 1) {
- return -1;
- }
-
- if (EVP_CIPHER_CTX_ctrl(decrypt_ctx.get(), EVP_CTRL_GCM_SET_TAG, (int) tag.size(), const_cast(tag.data())) != 1) {
- return -1;
- }
-
- if (EVP_DecryptFinal_ex(decrypt_ctx.get(), plaintext.data() + update_outlen, &final_outlen) != 1) {
+ if (EVP_DecryptUpdate(decrypt_ctx.get(), plaintext.data(), &update_outlen, (const std::uint8_t *) cipher.data(), (int) cipher.size()) != 1 || EVP_CIPHER_CTX_ctrl(decrypt_ctx.get(), EVP_CTRL_GCM_SET_TAG, (int) tag.size(), tag.data()) != 1 || EVP_DecryptFinal_ex(decrypt_ctx.get(), plaintext.data() + update_outlen, &final_outlen) != 1) {
+ plaintext.clear();
return -1;
}
diff --git a/src/crypto.h b/src/crypto.h
index 98be726c14e..c075662c0e2 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -320,12 +320,12 @@ namespace crypto {
int encrypt(const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
/**
- * @brief Decrypt ciphertext into the supplied plaintext buffer.
+ * @brief Authenticate and decrypt a complete tag-prefixed AES-GCM message.
*
- * @param cipher Ciphertext bytes to decrypt or output buffer for encryption.
- * @param plaintext Plaintext bytes to encrypt.
- * @param iv Initialization vector for the cipher operation.
- * @return Number of bytes written, signature bytes, or an error status depending on the overload.
+ * @param cipher Complete authentication tag followed by ciphertext.
+ * @param plaintext Receives authenticated plaintext; empty on failure.
+ * @param iv Nonempty initialization vector for the cipher operation.
+ * @return Zero on success, or -1 for invalid input or authentication failure.
*/
int decrypt(const std::string_view &cipher, std::vector &plaintext, aes_t *iv);
};
diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp
index 7601cb89690..36e4f6842f9 100644
--- a/src/nvhttp.cpp
+++ b/src/nvhttp.cpp
@@ -11,6 +11,7 @@
#include
#include
#include
+#include