Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.}

<table>
<tr>
<td>Description</td>
Expand Down Expand Up @@ -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.}

<table>
<tr>
<td>Description</td>
Expand Down
11 changes: 10 additions & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
![Theme Selection](images/split-themes.png)
Expand All @@ -706,7 +707,9 @@ by default. You may replace *localhost* with your internal ip address.
![Configuration](images/configuration-search.png)
4. Find Moonlight clients and other tools for Sunshine in the `Featured Apps` tab.
![Featured Apps](images/featured-apps.png)
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
Expand Down Expand Up @@ -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.
28 changes: 25 additions & 3 deletions src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/confighttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
80 changes: 80 additions & 0 deletions src/control_protocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @file src/control_protocol.h
* @brief Bounded parsing of GameStream control envelopes before decryption.
*/
#pragma once

// standard includes
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string_view>
#include <utility>

// lib includes
#include <boost/endian/conversion.hpp>

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<encrypted_message_t> 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<const unsigned char *>(payload.data());
const auto length = boost::endian::endian_load<std::uint16_t, 2, boost::endian::order::little>(bytes);
if (length < minimum_length || length != payload.size() - sizeof(length)) {
return std::nullopt;
}
const auto sequence = boost::endian::endian_load<std::uint32_t, 4, boost::endian::order::little>(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<std::string_view> 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<std::uint32_t, 4, boost::endian::order::big>(reinterpret_cast<const unsigned char *>(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<std::pair<std::uint16_t, std::string_view>> 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<const unsigned char *>(plaintext.data());
const auto type = boost::endian::endian_load<std::uint16_t, 2, boost::endian::order::little>(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
36 changes: 19 additions & 17 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* @file src/crypto.cpp
* @brief Definitions for cryptography functions.
*/
// standard includes
#include <algorithm>
#include <limits>

// lib includes
#include <openssl/pem.h>
#include <openssl/rsa.h>
Expand Down Expand Up @@ -142,33 +146,31 @@ namespace crypto {
}

int gcm_t::decrypt(const std::string_view &tagged_cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv) {
plaintext.clear();
if (tagged_cipher.size() < tag_size || tagged_cipher.size() - tag_size > static_cast<std::size_t>(std::numeric_limits<int>::max() - 16) || key.size() != 16 || iv == nullptr || iv->empty() || iv->size() > static_cast<std::size_t>(std::numeric_limits<int>::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<std::uint8_t, tag_size> 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<std::size_t>(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<char *>(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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint8_t> &plaintext, aes_t *iv);
};
Expand Down
Loading