Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Built-in certificates when assets/cacert.pem can't be read

Rafael Abud Ferreira12fc62b83e42
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 91fb71c..27ceece 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,7 +12,17 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(XCLOUD_PS5 "Build for the PS5 (payload SDK)" OFF)
# --- Portable core: auth, HTTPS, xCloud protocol -----------------------------
+# The CA bundle inside the binary too: used when assets/cacert.pem can't be read.
+set(XC_BUILTIN_CA "${CMAKE_CURRENT_BINARY_DIR}/generated/builtin_ca.cpp")
+add_custom_command(
+ OUTPUT "${XC_BUILTIN_CA}"
+ COMMAND ${CMAKE_COMMAND} -DIN=${CMAKE_CURRENT_SOURCE_DIR}/assets/cacert.pem -DOUT=${XC_BUILTIN_CA}
+ -DNAME=xc_builtin_ca_bundle -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/embed_file.cmake
+ DEPENDS assets/cacert.pem cmake/embed_file.cmake
+ COMMENT "Embedding assets/cacert.pem")
+
add_library(xcloud_core STATIC
+ ${XC_BUILTIN_CA}
src/util/json.cpp
src/util/log.cpp
src/net/http.cpp
diff --git a/cmake/embed_file.cmake b/cmake/embed_file.cmake
new file mode 100644
index 0000000..efc8a89
--- /dev/null
+++ b/cmake/embed_file.cmake
@@ -0,0 +1,6 @@
+# Writes a C++ file holding IN's bytes (plus a terminating NUL, as Mbed TLS's
+# PEM parser wants) as NAME[] and NAME_size: cmake -DIN=... -DOUT=... -DNAME=... -P
+file(READ "${IN}" hex HEX)
+string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," bytes "${hex}")
+string(REGEX REPLACE "(0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,0x..,)" "\\1\n" bytes "${bytes}")
+file(WRITE "${OUT}" "// Generated from ${IN} by cmake/embed_file.cmake: do not edit.\n#include <cstddef>\n\nextern const unsigned char ${NAME}[] = {\n${bytes}0x00};\nextern const std::size_t ${NAME}_size = sizeof(${NAME});\n")
diff --git a/src/app/autoplay.h b/src/app/autoplay.h
index a67a4f5..9653761 100644
--- a/src/app/autoplay.h
+++ b/src/app/autoplay.h
@@ -37,7 +37,7 @@ struct Autoplay {
bool consolesTab = false; // "consolestab": open My consoles, save consoles.ppm
bool consolesEmpty = false; // with "consolestab": as if none were found
bool settingsTest = false; // "settingstest": open the resolution list, save settings.ppm
- bool badCa = false; // "badca": a missing CA bundle, to see the TLS setup error
+ bool badCa = false; // "badca": a missing CA bundle: the built-in copy must be used
bool consoles = false; // "consoles": log the account's own consoles (xhome)
bool pad = false; // "pad": the physical pad stays in use, its buttons logged
bool detailTest = false; // open a game to buy far down the list, save its page
diff --git a/src/app/ps5_main.cpp b/src/app/ps5_main.cpp
index 91e7aa6..1d5b58f 100644
--- a/src/app/ps5_main.cpp
+++ b/src/app/ps5_main.cpp
@@ -201,6 +201,7 @@ int main(int argc, char** argv) {
platform::dataDir() + "/xcloud.log");
if (g_autoplay.badCa) XC_LOGI("AUTOPLAY END: TLS setup error shown");
} else {
+ if (g_autoplay.badCa) XC_LOGI("AUTOPLAY END: missing CA bundle, built-in copy %s", net::usingBuiltinCa() ? "used" : "NOT used");
if (g_autoplay.testCa) net::addTrustedCa(platform::dataDir() + "/test-ca.pem");
std::thread(worker).detach();
}
diff --git a/src/net/http.cpp b/src/net/http.cpp
index 049e4d7..4a9add7 100644
--- a/src/net/http.cpp
+++ b/src/net/http.cpp
@@ -36,6 +36,10 @@
#define MBEDTLS_ERR_NET_RECV_FAILED -0x004C
#endif
+// The CA bundle built in (cmake/embed_file.cmake from assets/cacert.pem).
+extern const unsigned char xc_builtin_ca_bundle[];
+extern const std::size_t xc_builtin_ca_bundle_size;
+
namespace xc::net {
namespace {
@@ -156,6 +160,12 @@ bool Url::parse(const std::string& url, Url& out) {
namespace {
std::string g_initError;
+bool g_builtinCa = false;
+}
+
+bool usingBuiltinCa() {
+ std::lock_guard<std::mutex> lock(g.mutex);
+ return g_builtinCa;
}
std::string tlsInitError() {
@@ -185,7 +195,17 @@ bool initTls(const std::string& caBundlePath) {
reinterpret_cast<const unsigned char*>(pers), std::strlen(pers));
if (rc != 0) return fail("random generator (system random source?): " + tlsError(rc));
rc = mbedtls_x509_crt_parse_file(&g.ca, caBundlePath.c_str());
- if (rc < 0) return fail("certificates " + caBundlePath + ": " + tlsError(rc));
+ if (rc < 0) {
+ // The file is missing or unreadable: the same bundle, built in. The
+ // install stays broken (other files may be missing too): logged.
+ XC_LOGW("TLS setup: certificates %s: %s; using the built-in copy (reinstall the app folder)",
+ caBundlePath.c_str(), tlsError(rc).c_str());
+ mbedtls_x509_crt_free(&g.ca);
+ mbedtls_x509_crt_init(&g.ca);
+ rc = mbedtls_x509_crt_parse(&g.ca, xc_builtin_ca_bundle, xc_builtin_ca_bundle_size);
+ if (rc < 0) return fail("certificates " + caBundlePath + " and the built-in copy: " + tlsError(rc));
+ g_builtinCa = true;
+ }
if (rc > 0) XC_LOGW("CA bundle: %d certificates could not be parsed (ignored)", rc);
g.ready = true;
return true;
diff --git a/src/net/http.h b/src/net/http.h
index 973f1d8..567159d 100644
--- a/src/net/http.h
+++ b/src/net/http.h
@@ -52,8 +52,12 @@ struct Url {
};
// Loads trusted roots from a PEM bundle. Must be called once before any
-// request; returns false when the file is missing or holds no certificates.
+// request. When the file can't be read (an incomplete copy of the app), the
+// copy built into the binary is used, and the log says so; false only when
+// neither works.
bool initTls(const std::string& caBundlePath);
+// True when initTls() fell back to the built-in copy.
+bool usingBuiltinCa();
// Trusts the certificates in this PEM file too (the update test's local
// server); false when it holds none.
bool addTrustedCa(const std::string& pemPath);
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index aa871d6..118d135 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -506,6 +506,13 @@ static void testControllerTuning() {
CHECK(triggerCommand(38, 0, 1, 2, true, 0) == still); // vibration off: only the weight
}
+static void testBuiltinCa() {
+ // A missing bundle (an incomplete copy of the app): the copy built in.
+ CHECK(xc::net::initTls("/nonexistent/cacert.pem"));
+ CHECK(xc::net::usingBuiltinCa());
+ xc::net::shutdownTls();
+}
+
static void testAutoDeband() {
using xc::app::debandForMbps;
CHECK(debandForMbps(0.3, 1) == -1); // starting or standing still: no measure
@@ -549,6 +556,7 @@ static void testAudioEncoder() {
}
int main() {
+ testBuiltinCa();
testAudioEncoder();
testAutoDeband();
testControllerTuning();