diff --git a/CMakeLists.txt b/CMakeLists.txt index 6472aed..5860957 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ if(POLICY CMP0168) endif() cmake_policy(SET CMP0168 "${CMAKE_POLICY_DEFAULT_CMP0168}") endif() -project(Venture VERSION 0.2.5.5 LANGUAGES C CXX) +project(Venture VERSION 0.2.5.6 LANGUAGES C CXX) # All owned generators share this interpreter and its imported target. find_package(Python3 COMPONENTS Interpreter REQUIRED) diff --git a/README.md b/README.md index 0a1e608..eff73a3 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ for streaming tools that key a color can be chosen instead. It is on by default; ### Something wrong? -If a game crashes, click **Report issue** on the banner that appears. Otherwise, use **Upload debug report** in Settings. Either way, Venture collects the launcher and game logs from your last launch into one debug report, uploads it as an unlisted paste that expires after 90 days, and gives you a link to copy. If the upload fails, attach the report file instead (**Show file**). The report starts with what happened in the last run and its likely cause; `venture-cli report [GAME] --upload` writes the same from a terminal. +If a game crashes, click **Report issue** on the banner that appears. Otherwise, use **Upload debug report** in Settings. Either way, Venture collects the launcher and game logs from your last launch into one debug report, uploads it as an encrypted paste on a PrivateBin server (the key stays in the link, so only people you give it to can read it) that expires after 30 days, and gives you a link to copy. If the upload fails, attach the report file instead (**Show file**). The report starts with what happened in the last run and its likely cause; `venture-cli report [GAME] --upload` writes the same from a terminal. [Open an issue](https://github.com/DeliciousMeatPop/Venture-UnknownSourceEdition/issues/new/choose) with the game, what happened, how to make it happen again and the debug report link. To watch a game's output live, turn on **Show runtime console** in Settings. Problems that also happen in upstream Venture are best reported [upstream](https://github.com/Villagers654/Venture/issues). diff --git a/i18n/venture.pot b/i18n/venture.pot index ae94552..ebf236e 100644 --- a/i18n/venture.pot +++ b/i18n/venture.pot @@ -42,7 +42,7 @@ msgid "" " (the most recent one by default): what happened,\n" " its likely cause, the system setup and the logs,\n" " with personal details removed; --upload also\n" -" shares it as a link (dpaste.com, 90 days)\n" +" shares it as a link (encrypted, 30 days)\n" "\n" "Add-ons\n" " addons GAME [--as ACCOUNT] List a game's add-ons (as the owning account)\n" @@ -132,7 +132,7 @@ msgstr "" #. Sidebar pages. #: src/launcher/ui/views.cpp:559 -#: src/launcher/ui/views.cpp:1905 +#: src/launcher/ui/views.cpp:1911 msgid "Library" msgstr "" @@ -149,7 +149,7 @@ msgid "Controls" msgstr "" #: src/launcher/ui/views.cpp:563 -#: src/launcher/ui/views.cpp:1871 +#: src/launcher/ui/views.cpp:1877 msgid "Settings" msgstr "" @@ -195,13 +195,13 @@ msgstr "" #: src/launcher/ui/views.cpp:841 #: src/launcher/ui/views.cpp:937 -#: src/launcher/ui/views.cpp:1308 +#: src/launcher/ui/views.cpp:1314 msgid "Unhide" msgstr "" #: src/launcher/ui/views.cpp:841 #: src/launcher/ui/views.cpp:937 -#: src/launcher/ui/views.cpp:1308 +#: src/launcher/ui/views.cpp:1314 msgid "Hide from library" msgstr "" @@ -211,52 +211,52 @@ msgstr "" msgid "Hidden" msgstr "" -#: src/launcher/ui/views.cpp:1298 +#: src/launcher/ui/views.cpp:1304 msgid "Link to store page…" msgstr "" -#: src/launcher/ui/views.cpp:1303 +#: src/launcher/ui/views.cpp:1309 msgid "Remove store link" msgstr "" -#: src/launcher/ui/views.cpp:1881 +#: src/launcher/ui/views.cpp:1887 msgid "Appearance" msgstr "" -#: src/launcher/ui/views.cpp:1889 +#: src/launcher/ui/views.cpp:1895 msgid "Interface size" msgstr "" -#: src/launcher/ui/views.cpp:1948 +#: src/launcher/ui/views.cpp:1954 msgid "Look up imported games online" msgstr "" -#: src/launcher/ui/views.cpp:1963 +#: src/launcher/ui/views.cpp:1969 msgid "Finds an imported game's store page on OculusDB. Only its package name is sent." msgstr "" -#: src/launcher/ui/views.cpp:1982 -#: src/launcher/ui/views.cpp:2532 +#: src/launcher/ui/views.cpp:1988 +#: src/launcher/ui/views.cpp:2538 msgid "Display" msgstr "" -#: src/launcher/ui/views.cpp:1989 -#: src/launcher/ui/views.cpp:2537 +#: src/launcher/ui/views.cpp:1995 +#: src/launcher/ui/views.cpp:2543 msgid "Runtime" msgstr "" #. Choices for "Compress textures". -#: src/launcher/ui/views.cpp:2084 -#: src/launcher/ui/views.cpp:2686 +#: src/launcher/ui/views.cpp:2090 +#: src/launcher/ui/views.cpp:2692 msgid "Always" msgstr "" #. Choices for "Compress textures". #. Choices for "Match game resolution". -#: src/launcher/ui/views.cpp:2084 -#: src/launcher/ui/views.cpp:2200 -#: src/launcher/ui/views.cpp:2417 -#: src/launcher/ui/views.cpp:2686 +#: src/launcher/ui/views.cpp:2090 +#: src/launcher/ui/views.cpp:2206 +#: src/launcher/ui/views.cpp:2423 +#: src/launcher/ui/views.cpp:2692 msgid "Automatic" msgstr "" @@ -264,265 +264,265 @@ msgstr "" #. Choices for "Match game resolution". #. Choices for "Audio on PC too". #. Choices of a troubleshooting setting, after its default. -#: src/launcher/ui/views.cpp:2084 -#: src/launcher/ui/views.cpp:2417 -#: src/launcher/ui/views.cpp:2512 -#: src/launcher/ui/views.cpp:2641 -#: src/launcher/ui/views.cpp:2686 +#: src/launcher/ui/views.cpp:2090 +#: src/launcher/ui/views.cpp:2423 +#: src/launcher/ui/views.cpp:2518 +#: src/launcher/ui/views.cpp:2647 +#: src/launcher/ui/views.cpp:2692 msgid "Off" msgstr "" #. Settings row: store game textures compressed, in less GPU memory. -#: src/launcher/ui/views.cpp:2087 -#: src/launcher/ui/views.cpp:2687 +#: src/launcher/ui/views.cpp:2093 +#: src/launcher/ui/views.cpp:2693 msgid "Compress textures" msgstr "" #. Settings row: GPU memory in use before new textures are compressed. -#: src/launcher/ui/views.cpp:2113 -#: src/launcher/ui/views.cpp:2714 +#: src/launcher/ui/views.cpp:2119 +#: src/launcher/ui/views.cpp:2720 msgid "Compression threshold" msgstr "" #. Faint link under a game's settings that opens its troubleshooting switches. -#: src/launcher/ui/views.cpp:2138 -#: src/launcher/ui/views.cpp:2570 +#: src/launcher/ui/views.cpp:2144 +#: src/launcher/ui/views.cpp:2576 msgid "Troubleshooting" msgstr "" -#: src/launcher/ui/views.cpp:2169 +#: src/launcher/ui/views.cpp:2175 msgid "About" msgstr "" -#: src/launcher/ui/views.cpp:2208 +#: src/launcher/ui/views.cpp:2214 msgid "Language" msgstr "" #. Settings row: make games render at the resolution above although they scale it up themselves. -#: src/launcher/ui/views.cpp:2409 +#: src/launcher/ui/views.cpp:2415 msgid "Match game resolution" msgstr "" -#: src/launcher/ui/views.cpp:2420 -#: src/launcher/ui/views.cpp:2515 -#: src/launcher/ui/views.cpp:2686 -#: src/launcher/ui/views.cpp:2711 +#: src/launcher/ui/views.cpp:2426 +#: src/launcher/ui/views.cpp:2521 +#: src/launcher/ui/views.cpp:2692 +#: src/launcher/ui/views.cpp:2717 msgid "Use global setting" msgstr "" -#: src/launcher/ui/views.cpp:2431 +#: src/launcher/ui/views.cpp:2437 msgid "Many Quest games render larger than the size they are asked for. Automatic learns how much and asks for less, so they render at the resolution above instead of wasting GPU time." msgstr "" #. Settings row: also play the game's sound on the PC's speakers or headphones. -#: src/launcher/ui/views.cpp:2504 +#: src/launcher/ui/views.cpp:2510 msgid "Audio on PC too" msgstr "" #. Choices for "Audio on PC too". #. Choices of a troubleshooting setting, after its default. -#: src/launcher/ui/views.cpp:2512 -#: src/launcher/ui/views.cpp:2641 +#: src/launcher/ui/views.cpp:2518 +#: src/launcher/ui/views.cpp:2647 msgid "On" msgstr "" #. Faint link under a game's settings that opens its troubleshooting switches. -#: src/launcher/ui/views.cpp:2570 +#: src/launcher/ui/views.cpp:2576 msgid "Troubleshooting settings changed" msgstr "" #. Warning before a game's troubleshooting switches are shown. #. Title of a game's troubleshooting switches. -#: src/launcher/ui/views.cpp:2583 -#: src/launcher/ui/views.cpp:2595 +#: src/launcher/ui/views.cpp:2589 +#: src/launcher/ui/views.cpp:2601 msgid "Troubleshooting settings" msgstr "" -#: src/launcher/ui/views.cpp:2584 +#: src/launcher/ui/views.cpp:2590 msgid "These change how Venture runs this game. A wrong choice can make it look broken or crash. Change them only when asked to in a bug report." msgstr "" -#: src/launcher/ui/views.cpp:2587 +#: src/launcher/ui/views.cpp:2593 msgid "Show them" msgstr "" -#: src/launcher/ui/views.cpp:2587 -#: src/launcher/ui/views.cpp:3561 -#: src/launcher/ui/views.cpp:3583 -#: src/launcher/ui/views.cpp:3649 -#: src/launcher/ui/views.cpp:3710 -#: src/launcher/ui/views.cpp:3711 +#: src/launcher/ui/views.cpp:2593 +#: src/launcher/ui/views.cpp:3567 +#: src/launcher/ui/views.cpp:3589 +#: src/launcher/ui/views.cpp:3655 +#: src/launcher/ui/views.cpp:3716 +#: src/launcher/ui/views.cpp:3717 msgid "Cancel" msgstr "" #. Advanced setting: the GPU checks that a game's reads stay inside its buffers and images. -#: src/launcher/ui/views.cpp:2602 +#: src/launcher/ui/views.cpp:2608 msgid "GPU bounds checks" msgstr "" -#: src/launcher/ui/views.cpp:2603 +#: src/launcher/ui/views.cpp:2609 msgid "Stops a game's out-of-range GPU reads, which crash PC graphics drivers. Off can fix effects that look wrong in some games, but the game may crash." msgstr "" -#: src/launcher/ui/views.cpp:2605 -#: src/launcher/ui/views.cpp:2610 -#: src/launcher/ui/views.cpp:2615 -#: src/launcher/ui/views.cpp:2620 -#: src/launcher/ui/views.cpp:2630 +#: src/launcher/ui/views.cpp:2611 +#: src/launcher/ui/views.cpp:2616 +#: src/launcher/ui/views.cpp:2621 +#: src/launcher/ui/views.cpp:2626 +#: src/launcher/ui/views.cpp:2636 msgid "Default (On)" msgstr "" #. Advanced setting: new descriptor sets start with empty descriptors. -#: src/launcher/ui/views.cpp:2607 +#: src/launcher/ui/views.cpp:2613 msgid "Start new resource slots empty" msgstr "" -#: src/launcher/ui/views.cpp:2608 +#: src/launcher/ui/views.cpp:2614 msgid "Fills the shader resource slots a game has not written yet with empty ones, as a Quest does. Prevents GPU crashes on AMD graphics cards." msgstr "" #. Advanced setting: games may bind empty (null) shader resources. -#: src/launcher/ui/views.cpp:2612 +#: src/launcher/ui/views.cpp:2618 msgid "Allow empty resources" msgstr "" -#: src/launcher/ui/views.cpp:2613 +#: src/launcher/ui/views.cpp:2619 msgid "Lets a game use empty shader resources, as a Quest does. Turn off only to test a graphics driver problem." msgstr "" #. Advanced setting: shader variables start at zero. -#: src/launcher/ui/views.cpp:2617 +#: src/launcher/ui/views.cpp:2623 msgid "Zero shader variables" msgstr "" -#: src/launcher/ui/views.cpp:2618 +#: src/launcher/ui/views.cpp:2624 msgid "Starts shader variables at zero, as a Quest's driver does. Prevents speckles or flicker in games that read a variable before setting it." msgstr "" #. Advanced setting: render pass inputs are read as ordinary images, one layer per eye. -#: src/launcher/ui/views.cpp:2622 +#: src/launcher/ui/views.cpp:2628 msgid "Read pass inputs as images" msgstr "" -#: src/launcher/ui/views.cpp:2623 +#: src/launcher/ui/views.cpp:2629 msgid "Reads render pass inputs as ordinary images, one layer per eye. Automatic turns it on for AMD graphics cards, whose driver otherwise shows the left eye's picture in the right eye." msgstr "" -#: src/launcher/ui/views.cpp:2625 +#: src/launcher/ui/views.cpp:2631 msgid "Default (Automatic)" msgstr "" #. Advanced setting: games are offered the Quest's foveated rendering interface. -#: src/launcher/ui/views.cpp:2627 +#: src/launcher/ui/views.cpp:2633 msgid "Foveation support" msgstr "" -#: src/launcher/ui/views.cpp:2628 +#: src/launcher/ui/views.cpp:2634 msgid "Offers games the Quest's foveated rendering interface; the PC still renders every pixel. Off can help a game that shows nothing or flickers." msgstr "" -#: src/launcher/ui/views.cpp:2655 +#: src/launcher/ui/views.cpp:2661 msgid "Done" msgstr "" -#: src/launcher/ui/views.cpp:2656 -#: src/launcher/ui/views.cpp:2657 +#: src/launcher/ui/views.cpp:2662 +#: src/launcher/ui/views.cpp:2663 msgid "Reset to defaults" msgstr "" -#: src/launcher/ui/views.cpp:3559 +#: src/launcher/ui/views.cpp:3565 #, python-brace-format msgid "Uninstall {0}?" msgstr "" -#: src/launcher/ui/views.cpp:3559 +#: src/launcher/ui/views.cpp:3565 msgid "Uninstall this game?" msgstr "" -#: src/launcher/ui/views.cpp:3560 +#: src/launcher/ui/views.cpp:3566 msgid "Its files are removed from this PC. Saves are kept, and you can install it again from your library." msgstr "" -#: src/launcher/ui/views.cpp:3561 +#: src/launcher/ui/views.cpp:3567 msgid "Uninstall" msgstr "" #. Warning when Compress textures changes from Always to Automatic or Off. -#: src/launcher/ui/views.cpp:3579 +#: src/launcher/ui/views.cpp:3585 msgid "Disable texture compression?" msgstr "" -#: src/launcher/ui/views.cpp:3580 +#: src/launcher/ui/views.cpp:3586 msgid "This can increase video memory use by up to 4 times. It isn't recommended on GPUs with less than 20 GB of video memory." msgstr "" -#: src/launcher/ui/views.cpp:3583 +#: src/launcher/ui/views.cpp:3589 msgid "Change anyway" msgstr "" #. Shown at start in the Linux Flatpak when SteamVR is the active OpenXR runtime. -#: src/launcher/ui/views.cpp:3606 +#: src/launcher/ui/views.cpp:3612 msgid "SteamVR does not work with the Flatpak" msgstr "" -#: src/launcher/ui/views.cpp:3607 +#: src/launcher/ui/views.cpp:3613 msgid "SteamVR is your OpenXR runtime, and the Flatpak sandbox cannot reach SteamVR, so games will not start. Use the Venture AppImage, tarball or your distribution's package with SteamVR, or use WiVRn or Monado with the Flatpak." msgstr "" -#: src/launcher/ui/views.cpp:3610 +#: src/launcher/ui/views.cpp:3616 msgid "OK" msgstr "" -#: src/launcher/ui/views.cpp:3645 +#: src/launcher/ui/views.cpp:3651 #, python-brace-format msgid "Send the saves of {0} to the headset?" msgstr "" -#: src/launcher/ui/views.cpp:3646 +#: src/launcher/ui/views.cpp:3652 msgid "Send this game's saves to the headset?" msgstr "" -#: src/launcher/ui/views.cpp:3647 +#: src/launcher/ui/views.cpp:3653 msgid "This closes the game on the headset and replaces its saves with Venture's copy. The headset's current saves are backed up on this computer first." msgstr "" -#: src/launcher/ui/views.cpp:3649 +#: src/launcher/ui/views.cpp:3655 msgid "Send to headset" msgstr "" -#: src/launcher/ui/views.cpp:3680 +#: src/launcher/ui/views.cpp:3686 msgid "Link to store page" msgstr "" #. {0} is an Android package name such as com.studio.game. -#: src/launcher/ui/views.cpp:3682 +#: src/launcher/ui/views.cpp:3688 #, python-brace-format msgid "Paste the game's Meta store link or its ID. Its name, description and art then come from the store. The store page must be for {0}." msgstr "" -#: src/launcher/ui/views.cpp:3695 +#: src/launcher/ui/views.cpp:3701 msgid "Checking the store page…" msgstr "" #. {0} is a game's store name, {1} and {2} Android package names. -#: src/launcher/ui/views.cpp:3701 +#: src/launcher/ui/views.cpp:3707 #, python-brace-format msgid "This store page is for {0} ({1}), not {2}. Link it anyway only for a modified build or another edition of the same game." msgstr "" -#: src/launcher/ui/views.cpp:3710 +#: src/launcher/ui/views.cpp:3716 msgid "Link anyway" msgstr "" -#: src/launcher/ui/views.cpp:3711 +#: src/launcher/ui/views.cpp:3717 msgid "Link" msgstr "" #. Controls page checkbox. The grip is the controller's side button held to grab. -#: src/launcher/ui/views.cpp:5182 +#: src/launcher/ui/views.cpp:5188 msgid "Grip toggle" msgstr "" -#: src/launcher/ui/views.cpp:5186 +#: src/launcher/ui/views.cpp:5192 msgid "Press once to hold, again to let go" msgstr "" diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 6380270..1b8a5a1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(venture_common STATIC controls.cpp digest.cpp + privatebin.cpp environment.cpp log.cpp paths.cpp @@ -46,12 +47,12 @@ if(VENTURE_BUILD_LAUNCHER) target_link_libraries(venture_common PRIVATE webpdecoder) endif() if(WIN32) - target_sources(venture_common PRIVATE platform/win32/digest.cpp platform/win32/paths.cpp platform/win32/secret_store.cpp platform/win32/process.cpp platform/win32/file_ops.cpp platform/win32/pipe.cpp platform/win32/openxr_layers.cpp platform/win32/system_fonts.cpp) + target_sources(venture_common PRIVATE platform/win32/digest.cpp platform/win32/crypto.cpp platform/win32/paths.cpp platform/win32/secret_store.cpp platform/win32/process.cpp platform/win32/file_ops.cpp platform/win32/pipe.cpp platform/win32/openxr_layers.cpp platform/win32/system_fonts.cpp) target_link_libraries(venture_common PRIVATE crypt32 bcrypt shell32 ole32 uuid advapi32) else() find_package(OpenSSL REQUIRED) target_link_libraries(venture_common PRIVATE OpenSSL::Crypto) - target_sources(venture_common PRIVATE platform/posix/digest.cpp platform/posix/paths.cpp platform/posix/secret_store.cpp platform/posix/process.cpp platform/posix/file_ops.cpp platform/posix/pipe.cpp platform/posix/dbus.cpp platform/posix/openxr_layers.cpp platform/posix/system_fonts.cpp) + target_sources(venture_common PRIVATE platform/posix/digest.cpp platform/posix/crypto.cpp platform/posix/paths.cpp platform/posix/secret_store.cpp platform/posix/process.cpp platform/posix/file_ops.cpp platform/posix/pipe.cpp platform/posix/dbus.cpp platform/posix/openxr_layers.cpp platform/posix/system_fonts.cpp) target_link_libraries(venture_common PRIVATE ${CMAKE_DL_LIBS}) find_package(PkgConfig QUIET) if(PkgConfig_FOUND) diff --git a/src/common/crypto.h b/src/common/crypto.h new file mode 100644 index 0000000..fd67453 --- /dev/null +++ b/src/common/crypto.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once +#include +#include +#include +#include + +namespace venture::crypto { +using Bytes = std::vector; +// The operating system's providers (Windows CNG, OpenSSL elsewhere). Failures throw std::runtime_error. +Bytes random_bytes(std::size_t count); +Bytes pbkdf2_sha256(std::span password, std::span salt, + std::uint32_t iterations, std::size_t length); +// AES-256-GCM with a 12-byte nonce and a 16-byte tag. Encryption returns the ciphertext followed by +// the tag; decryption takes that form and throws when the tag does not verify. +Bytes aes256_gcm_encrypt(std::span key, std::span nonce, + std::span aad, std::span plaintext); +Bytes aes256_gcm_decrypt(std::span key, std::span nonce, + std::span aad, std::span ciphertext_and_tag); +} // namespace venture::crypto diff --git a/src/common/http/client.cpp b/src/common/http/client.cpp index 857fde3..880a877 100644 --- a/src/common/http/client.cpp +++ b/src/common/http/client.cpp @@ -239,7 +239,8 @@ bool redirected(long status) { } Response memory(std::string_view url, const Fields *fields, size_t limit, std::stop_token stop, const Fields &extra_headers = {}, - bool loopback = false) { + bool loopback = false, const std::string *raw_body = nullptr, + std::string_view content_type = {}) { auto h = handle(url, &stop); if (loopback) { curl_easy_setopt(h.get(), CURLOPT_PROTOCOLS_STR, "http"); @@ -251,7 +252,7 @@ Response memory(std::string_view url, const Fields *fields, size_t limit, size_t limit; } sink{&r.body, limit}; curl_easy_setopt(h.get(), CURLOPT_TIMEOUT, 40L); - if (!extra_headers.empty() && !fields) { + if (!extra_headers.empty() && !fields && !raw_body) { curl_easy_setopt(h.get(), CURLOPT_CONNECTTIMEOUT, 5L); curl_easy_setopt(h.get(), CURLOPT_TIMEOUT, 15L); } @@ -279,6 +280,11 @@ Response memory(std::string_view url, const Fields *fields, size_t limit, curl_easy_setopt(h.get(), CURLOPT_POSTFIELDS, body.c_str()); curl_easy_setopt(h.get(), CURLOPT_POSTFIELDSIZE_LARGE, curl_off_t(body.size())); + } else if (raw_body) { + // The caller's bytes as they are (curl does not copy them: raw_body outlives perform). + curl_easy_setopt(h.get(), CURLOPT_POSTFIELDS, raw_body->data()); + curl_easy_setopt(h.get(), CURLOPT_POSTFIELDSIZE_LARGE, + curl_off_t(raw_body->size())); } raw_headers = curl_slist_append(nullptr, "Accept: application/json"); if (!raw_headers) @@ -296,6 +302,15 @@ Response memory(std::string_view url, const Fields *fields, size_t limit, hs.release(); hs.reset(raw_headers = next); } + if (raw_body && !content_type.empty()) { + if (content_type.find_first_of("\r\n") != content_type.npos) + throw std::runtime_error("Invalid HTTP header."); + auto *next = curl_slist_append(raw_headers, ("Content-Type: " + std::string(content_type)).c_str()); + if (!next) + throw std::runtime_error("HTTP initialization failed."); + hs.release(); + hs.reset(raw_headers = next); + } curl_easy_setopt(h.get(), CURLOPT_HTTPHEADER, raw_headers); auto code = curl_easy_perform(h.get()); #ifdef _WIN32 @@ -430,6 +445,10 @@ Response post(std::string_view url, const Fields &fields, std::stop_token stop, const Fields &headers) { return memory(url, &fields, 16 * 1024 * 1024, stop, headers); } +Response post_body(std::string_view url, const std::string &body, std::string_view content_type, + std::stop_token stop, const Fields &headers) { + return memory(url, nullptr, 1024 * 1024, stop, headers, false, &body, content_type); +} Response get_meta(std::string_view url, const Fields &headers, std::stop_token stop) { auto u = parse_url(url); diff --git a/src/common/http/client.h b/src/common/http/client.h index 5980786..a08275f 100644 --- a/src/common/http/client.h +++ b/src/common/http/client.h @@ -32,6 +32,9 @@ bool secure_binary(std::string_view url); std::string with_delivery_token(std::string_view url, std::string_view token); Response post(std::string_view url, const Fields &fields, std::stop_token stop = {}, const Fields &headers = {}); +// POST with a raw body (JSON, say) and its content type; the reply is limited to 1 MiB. +Response post_body(std::string_view url, const std::string &body, std::string_view content_type, + std::stop_token stop = {}, const Fields &headers = {}); Response get_meta(std::string_view url, const Fields &headers, std::stop_token stop = {}); // Explicit test transport; only the numeric loopback address can receive diff --git a/src/common/platform/posix/crypto.cpp b/src/common/platform/posix/crypto.cpp new file mode 100644 index 0000000..c5268db --- /dev/null +++ b/src/common/platform/posix/crypto.cpp @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#include "common/crypto.h" +#include +#include +#include + +namespace venture::crypto { +namespace { +constexpr std::size_t kTag = 16, kNonce = 12, kKey = 32; +struct Context { + EVP_CIPHER_CTX* cipher = EVP_CIPHER_CTX_new(); + ~Context() { EVP_CIPHER_CTX_free(cipher); } +}; +// One AES-256-GCM pass; `input` is the plaintext, or the ciphertext followed by its tag. +Bytes gcm(bool encrypt, std::span key, std::span nonce, + std::span aad, std::span input) { + if (key.size() != kKey || nonce.size() != kNonce) throw std::invalid_argument("bad AES-GCM key or nonce size"); + if (!encrypt && input.size() < kTag) throw std::runtime_error("AES-GCM input is shorter than its tag"); + Context ctx; + if (!ctx.cipher) throw std::runtime_error("cannot create AES-GCM context"); + const int mode = encrypt ? 1 : 0; + if (EVP_CipherInit_ex(ctx.cipher, EVP_aes_256_gcm(), nullptr, nullptr, nullptr, mode) != 1 || + EVP_CIPHER_CTX_ctrl(ctx.cipher, EVP_CTRL_GCM_SET_IVLEN, int(kNonce), nullptr) != 1 || + EVP_CipherInit_ex(ctx.cipher, nullptr, nullptr, key.data(), nonce.data(), mode) != 1) + throw std::runtime_error("cannot initialize AES-GCM"); + int length = 0; + if (!aad.empty() && EVP_CipherUpdate(ctx.cipher, nullptr, &length, aad.data(), int(aad.size())) != 1) + throw std::runtime_error("AES-GCM authenticated data failed"); + const auto body = encrypt ? input : input.first(input.size() - kTag); + Bytes out(body.size() + (encrypt ? kTag : 0)); + int total = 0; + if (!body.empty()) { + if (EVP_CipherUpdate(ctx.cipher, out.data(), &length, body.data(), int(body.size())) != 1) + throw std::runtime_error("AES-GCM failed"); + total = length; + } + if (encrypt) { + if (EVP_EncryptFinal_ex(ctx.cipher, out.data() + total, &length) != 1) + throw std::runtime_error("AES-GCM failed"); + total += length; + if (EVP_CIPHER_CTX_ctrl(ctx.cipher, EVP_CTRL_GCM_GET_TAG, int(kTag), out.data() + total) != 1) + throw std::runtime_error("AES-GCM tag failed"); + out.resize(std::size_t(total) + kTag); + } else { + Bytes tag(input.end() - kTag, input.end()); + if (EVP_CIPHER_CTX_ctrl(ctx.cipher, EVP_CTRL_GCM_SET_TAG, int(kTag), tag.data()) != 1) + throw std::runtime_error("AES-GCM tag failed"); + if (EVP_DecryptFinal_ex(ctx.cipher, out.data() + total, &length) != 1) + throw std::runtime_error("AES-GCM authentication failed"); + out.resize(std::size_t(total + length)); + } + return out; +} +} // namespace +Bytes random_bytes(std::size_t count) { + Bytes out(count); + if (count && RAND_bytes(out.data(), int(count)) != 1) throw std::runtime_error("cannot get random bytes"); + return out; +} +Bytes pbkdf2_sha256(std::span password, std::span salt, + std::uint32_t iterations, std::size_t length) { + Bytes out(length); + if (PKCS5_PBKDF2_HMAC(reinterpret_cast(password.data()), int(password.size()), salt.data(), + int(salt.size()), int(iterations), EVP_sha256(), int(length), out.data()) != 1) + throw std::runtime_error("PBKDF2 failed"); + return out; +} +Bytes aes256_gcm_encrypt(std::span key, std::span nonce, + std::span aad, std::span plaintext) { + return gcm(true, key, nonce, aad, plaintext); +} +Bytes aes256_gcm_decrypt(std::span key, std::span nonce, + std::span aad, std::span ciphertext_and_tag) { + return gcm(false, key, nonce, aad, ciphertext_and_tag); +} +} // namespace venture::crypto diff --git a/src/common/platform/win32/crypto.cpp b/src/common/platform/win32/crypto.cpp new file mode 100644 index 0000000..3ed37ab --- /dev/null +++ b/src/common/platform/win32/crypto.cpp @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#include "common/crypto.h" +#include +#include +#include +#include + +namespace venture::crypto { +namespace { +constexpr std::size_t kTag = 16, kNonce = 12, kKey = 32; +constexpr NTSTATUS kAuthTagMismatch = static_cast(0xC000A002L); + +ULONG checked(std::size_t size) { + if (size > std::numeric_limits::max()) throw std::runtime_error("crypto input is too large"); + return static_cast(size); +} +// A pointer a CNG call accepts for an empty span. +PUCHAR bytes(std::span data) { + static UCHAR none = 0; + return data.empty() ? &none : const_cast(data.data()); +} +struct Algorithm { + BCRYPT_ALG_HANDLE handle = nullptr; + Algorithm(LPCWSTR name, ULONG flags) { + if (BCryptOpenAlgorithmProvider(&handle, name, nullptr, flags) < 0) + throw std::runtime_error("cannot open crypto provider"); + } + ~Algorithm() { if (handle) BCryptCloseAlgorithmProvider(handle, 0); } + Algorithm(const Algorithm&) = delete; + Algorithm& operator=(const Algorithm&) = delete; +}; +struct Key { + BCRYPT_KEY_HANDLE handle = nullptr; + ~Key() { if (handle) BCryptDestroyKey(handle); } +}; +Bytes gcm(bool encrypt, std::span key, std::span nonce, + std::span aad, std::span input) { + if (key.size() != kKey || nonce.size() != kNonce) throw std::invalid_argument("bad AES-GCM key or nonce size"); + if (!encrypt && input.size() < kTag) throw std::runtime_error("AES-GCM input is shorter than its tag"); + Algorithm algorithm(BCRYPT_AES_ALGORITHM, 0); + if (BCryptSetProperty(algorithm.handle, BCRYPT_CHAINING_MODE, reinterpret_cast(const_cast(BCRYPT_CHAIN_MODE_GCM)), + sizeof(BCRYPT_CHAIN_MODE_GCM), 0) < 0) + throw std::runtime_error("cannot select AES-GCM"); + Key aes; + if (BCryptGenerateSymmetricKey(algorithm.handle, &aes.handle, nullptr, 0, bytes(key), checked(key.size()), 0) < 0) + throw std::runtime_error("cannot create AES key"); + const auto body = encrypt ? input : input.first(input.size() - kTag); + Bytes out(body.size() + (encrypt ? kTag : 0)); + Bytes tag(kTag); + if (!encrypt) tag.assign(input.end() - kTag, input.end()); + BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info; + BCRYPT_INIT_AUTH_MODE_INFO(info); + info.pbNonce = bytes(nonce); + info.cbNonce = checked(nonce.size()); + info.pbAuthData = aad.empty() ? nullptr : bytes(aad); + info.cbAuthData = checked(aad.size()); + info.pbTag = tag.data(); + info.cbTag = static_cast(kTag); + ULONG written = 0; + if (encrypt) { + UCHAR empty = 0; + NTSTATUS status = BCryptEncrypt(aes.handle, body.empty() ? &empty : bytes(body), checked(body.size()), &info, + nullptr, 0, out.data(), checked(body.size()), &written, 0); + if (status < 0) throw std::runtime_error("AES-GCM failed"); + out.resize(written); + out.insert(out.end(), tag.begin(), tag.end()); + } else { + UCHAR empty = 0; + NTSTATUS status = BCryptDecrypt(aes.handle, body.empty() ? &empty : bytes(body), checked(body.size()), &info, + nullptr, 0, out.data(), checked(out.size()), &written, 0); + if (status == kAuthTagMismatch) throw std::runtime_error("AES-GCM authentication failed"); + if (status < 0) throw std::runtime_error("AES-GCM failed"); + out.resize(written); + } + return out; +} +} // namespace +Bytes random_bytes(std::size_t count) { + Bytes out(count); + if (count && BCryptGenRandom(nullptr, out.data(), checked(count), BCRYPT_USE_SYSTEM_PREFERRED_RNG) < 0) + throw std::runtime_error("cannot get random bytes"); + return out; +} +Bytes pbkdf2_sha256(std::span password, std::span salt, + std::uint32_t iterations, std::size_t length) { + Algorithm hmac(BCRYPT_SHA256_ALGORITHM, BCRYPT_ALG_HANDLE_HMAC_FLAG); + Bytes out(length); + if (BCryptDeriveKeyPBKDF2(hmac.handle, bytes(password), checked(password.size()), bytes(salt), checked(salt.size()), + iterations, out.data(), checked(length), 0) < 0) + throw std::runtime_error("PBKDF2 failed"); + return out; +} +Bytes aes256_gcm_encrypt(std::span key, std::span nonce, + std::span aad, std::span plaintext) { + return gcm(true, key, nonce, aad, plaintext); +} +Bytes aes256_gcm_decrypt(std::span key, std::span nonce, + std::span aad, std::span ciphertext_and_tag) { + return gcm(false, key, nonce, aad, ciphertext_and_tag); +} +} // namespace venture::crypto diff --git a/src/common/privatebin.cpp b/src/common/privatebin.cpp new file mode 100644 index 0000000..daef6c1 --- /dev/null +++ b/src/common/privatebin.cpp @@ -0,0 +1,191 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#include "common/privatebin.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common/crypto.h" +#include "common/http/client.h" + +namespace venture::privatebin { +namespace { +using Json = nlohmann::json; +using crypto::Bytes; +constexpr std::uint32_t kIterations = 100000; +constexpr std::size_t kKeyBytes = 32, kSaltBytes = 8, kNonceBytes = 12; +constexpr char kBase58[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; +constexpr char kBase64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +Bytes base64_decode(std::string_view text) { + Bytes out; + std::uint32_t buffer = 0; + int bits = 0; + for (char c : text) { + if (c == '=') break; + const char *at = std::strchr(kBase64, c); + if (!at || !c) throw std::runtime_error("invalid base64"); + buffer = (buffer << 6) | std::uint32_t(at - kBase64); + bits += 6; + if (bits >= 8) { + bits -= 8; + out.push_back(std::uint8_t((buffer >> bits) & 0xff)); + } + } + return out; +} +Bytes base58_decode(std::string_view text) { + Bytes out; // big-endian, built by multiplying the number so far by 58 + std::size_t zeros = 0; + while (zeros < text.size() && text[zeros] == '1') ++zeros; + for (char c : text) { + const char *at = std::strchr(kBase58, c); + if (!at || !c) throw std::runtime_error("invalid base58"); + std::uint32_t carry = std::uint32_t(at - kBase58); + for (auto it = out.rbegin(); it != out.rend(); ++it) { + carry += std::uint32_t(*it) * 58; + *it = std::uint8_t(carry & 0xff); + carry >>= 8; + } + while (carry) { + out.insert(out.begin(), std::uint8_t(carry & 0xff)); + carry >>= 8; + } + } + while (!out.empty() && out.front() == 0) out.erase(out.begin()); + out.insert(out.begin(), zeros, std::uint8_t(0)); + return out; +} +// The deflate stream PrivateBin uses: raw, with no zlib header. +Bytes deflate_raw(std::string_view text) { + size_t size = 0; + const int flags = int(tdefl_create_comp_flags_from_zip_params(MZ_DEFAULT_LEVEL, -MZ_DEFAULT_WINDOW_BITS, + MZ_DEFAULT_STRATEGY)); + std::unique_ptr out(tdefl_compress_mem_to_heap(text.data(), text.size(), &size, flags), + mz_free); + if (!out) throw std::runtime_error("compression failed"); + const auto *begin = static_cast(out.get()); + return Bytes(begin, begin + size); +} +std::string inflate_raw(std::span data) { + size_t size = 0; + std::unique_ptr out(tinfl_decompress_mem_to_heap(data.data(), data.size(), &size, 0), + mz_free); + if (!out) throw std::runtime_error("decompression failed"); + return std::string(static_cast(out.get()), size); +} +// The authenticated data: what JSON.stringify gives PrivateBin's page, compact and in this order. +Json adata_for(std::span nonce, std::span salt) { + return Json::array({Json::array({base64(nonce), base64(salt), kIterations, 256, 128, "aes", "gcm", "zlib"}), + "plaintext", 0, 0}); +} +Bytes derive(std::span key, std::span salt) { + return crypto::pbkdf2_sha256(key, salt, kIterations, kKeyBytes); +} +// A server's short reason, safe to show. +std::string reason(const Json &reply) { + std::string text = reply.is_object() && reply.contains("message") && reply["message"].is_string() + ? reply["message"].get() : std::string(); + std::string clean; + for (unsigned char c : text.substr(0, 120)) clean += (c >= 0x20 && c < 0x7f) ? char(c) : '?'; + return clean; +} +bool valid_id(std::string_view id) { + return !id.empty() && id.size() <= 64 && + std::all_of(id.begin(), id.end(), [](unsigned char c) { return std::isalnum(c) != 0; }); +} +} // namespace + +std::string base64(std::span bytes) { + std::string out; + std::uint32_t buffer = 0; + int bits = 0; + for (auto byte : bytes) { + buffer = (buffer << 8) | byte; + bits += 8; + while (bits >= 6) { + bits -= 6; + out += kBase64[(buffer >> bits) & 63]; + } + } + if (bits) out += kBase64[(buffer << (6 - bits)) & 63]; + while (out.size() % 4) out += '='; + return out; +} +std::string base58(std::span bytes) { + std::size_t zeros = 0; + while (zeros < bytes.size() && bytes[zeros] == 0) ++zeros; + std::vector digits; // little-endian base-58 digits + for (auto byte : bytes) { + std::uint32_t carry = byte; + for (auto &digit : digits) { + carry += std::uint32_t(digit) << 8; + digit = std::uint8_t(carry % 58); + carry /= 58; + } + while (carry) { + digits.push_back(std::uint8_t(carry % 58)); + carry /= 58; + } + } + std::string out(zeros, '1'); + for (auto it = digits.rbegin(); it != digits.rend(); ++it) out += kBase58[*it]; + return out; +} + +Encrypted encrypt(std::string_view text, std::string_view expire, std::span key, + std::span salt, std::span nonce) { + if (key.size() != kKeyBytes || salt.size() != kSaltBytes || nonce.size() != kNonceBytes) + throw std::invalid_argument("bad key, salt or nonce size"); + // PrivateBin stores {"paste": text}; invalid UTF-8 in a log becomes U+FFFD instead of failing. + const std::string payload = Json{{"paste", text}}.dump(-1, ' ', false, Json::error_handler_t::replace); + const Json adata = adata_for(nonce, salt); + const std::string aad = adata.dump(); + const Bytes packed = deflate_raw(payload); + const Bytes sealed = crypto::aes256_gcm_encrypt(derive(key, salt), + nonce, std::span( + reinterpret_cast(aad.data()), aad.size()), + packed); + Json body{{"v", 2}, {"adata", adata}, {"ct", base64(sealed)}, {"meta", {{"expire", std::string(expire)}}}}; + return {body.dump(), base58(key)}; +} +Encrypted encrypt(std::string_view text, std::string_view expire) { + return encrypt(text, expire, crypto::random_bytes(kKeyBytes), crypto::random_bytes(kSaltBytes), + crypto::random_bytes(kNonceBytes)); +} +std::string decrypt(std::string_view body, std::string_view fragment) { + const Json request = Json::parse(body); + const Json &adata = request.at("adata"); + const Json &cipher = adata.at(0); + if (request.at("v") != 2 || cipher.at(2).get() != kIterations || cipher.at(3) != 256 || + cipher.at(4) != 128 || cipher.at(5) != "aes" || cipher.at(6) != "gcm" || cipher.at(7) != "zlib") + throw std::runtime_error("unsupported paste format"); + const Bytes key = base58_decode(fragment); + const Bytes salt = base64_decode(cipher.at(1).get()); + const Bytes nonce = base64_decode(cipher.at(0).get()); + const std::string aad = adata.dump(); + const Bytes packed = crypto::aes256_gcm_decrypt( + derive(key, salt), nonce, std::span(reinterpret_cast(aad.data()), aad.size()), + base64_decode(request.at("ct").get())); + return Json::parse(inflate_raw(packed)).at("paste").get(); +} + +std::string paste(std::string_view server, std::string_view text, std::string_view expire, std::stop_token stop) { + if (!server.starts_with("https://") || server.ends_with('/')) throw std::invalid_argument("bad PrivateBin address"); + const Encrypted sealed = encrypt(text, expire); + const auto reply_http = http::post_body(std::string(server) + "/", sealed.body, "application/json", stop, + {{"X-Requested-With", "JSONHttpRequest"}}); + Json reply = Json::parse(reply_http.body, nullptr, false); + if (reply_http.status != 200 || !reply.is_object() || reply.value("status", 1) != 0 || + !reply.contains("id") || !reply["id"].is_string() || !valid_id(reply["id"].get())) { + std::string why = reason(reply); + throw std::runtime_error(why.empty() ? "The upload service answered " + std::to_string(reply_http.status) + "." + : "The upload service refused it: " + why); + } + return std::string(server) + "/?" + reply["id"].get() + "#" + sealed.fragment; +} +} // namespace venture::privatebin diff --git a/src/common/privatebin.h b/src/common/privatebin.h new file mode 100644 index 0000000..7ce7d35 --- /dev/null +++ b/src/common/privatebin.h @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once +#include +#include +#include +#include +#include + +namespace venture::privatebin { +// A PrivateBin (https://privatebin.info, format version 2) client. The text is compressed and +// encrypted here with a random key that never leaves this computer: the server stores only +// ciphertext, and the key travels in the link's fragment (after the '#'), which browsers do not send. + +// What to POST and the link's key part. +struct Encrypted { + std::string body; // the JSON request body + std::string fragment; // base58 of the key, the part of the link after '#' +}; +// Compresses and encrypts `text`; `expire` is one of PrivateBin's lifetimes ("5min", "1hour", "1day", +// "1week", "1month", "1year", "never"). The key, salt and nonce are random; the overload taking them +// exists for tests (key 32 bytes, salt 8, nonce 12). +Encrypted encrypt(std::string_view text, std::string_view expire); +Encrypted encrypt(std::string_view text, std::string_view expire, std::span key, + std::span salt, std::span nonce); +// The inverse of encrypt, as PrivateBin's page does it: the text of a request body or paste, given +// the link's fragment. Throws when it does not verify. Exposed for tests. +std::string decrypt(std::string_view body, std::string_view fragment); + +// Uploads `text` to the PrivateBin at `server` (https, no trailing slash) and returns the link, +// key included. Throws with a short reason when the server refuses or cannot be reached. +std::string paste(std::string_view server, std::string_view text, std::string_view expire, + std::stop_token stop = {}); + +// Exposed for tests. +std::string base58(std::span bytes); +std::string base64(std::span bytes); +} // namespace venture::privatebin diff --git a/src/launcher/cli.cpp b/src/launcher/cli.cpp index 9c143bf..dbd66c1 100644 --- a/src/launcher/cli.cpp +++ b/src/launcher/cli.cpp @@ -66,7 +66,7 @@ Library (the most recent one by default): what happened, its likely cause, the system setup and the logs, with personal details removed; --upload also - shares it as a link (dpaste.com, 90 days) + shares it as a link (encrypted, 30 days) Add-ons addons GAME [--as ACCOUNT] List a game's add-ons (as the owning account) diff --git a/src/launcher/debug_report.cpp b/src/launcher/debug_report.cpp index d938e50..531144a 100644 --- a/src/launcher/debug_report.cpp +++ b/src/launcher/debug_report.cpp @@ -4,6 +4,7 @@ #include "common/http/client.h" #include "common/library/library.h" #include "common/openxr_layers.h" +#include "common/privatebin.h" #include "common/process.h" #include "common/saves_migration.h" #include "common/steam_library.h" @@ -44,7 +45,7 @@ namespace { constexpr size_t kRuntimeLog = 448 << 10, kSideLog = 128 << 10, kLauncherLog = 128 << 10; constexpr size_t kReportBudget = 900'000, kPasteLimit = 1'000'000; constexpr size_t kKeptReports = 10; -constexpr const char *kPasteDays = "90"; +constexpr const char *kPasteServer = "https://privatebin.etisbew.website", *kPasteExpire = "1month"; std::string read_file(const fs::path &file, size_t limit = 64 << 10) { std::ifstream in(file, std::ios::binary); @@ -1287,12 +1288,6 @@ std::string upload_debug_report(const fs::path &file, std::stop_token stop) { std::string text = read_file(file, kPasteLimit + 1); if (text.empty()) throw std::runtime_error("The report is empty."); if (text.size() > kPasteLimit) throw std::runtime_error("The report is too large to upload."); - auto r = http::post("https://dpaste.com/api/v2/", - {{"content", text}, {"title", paths::utf8(file.filename())}, {"expiry_days", kPasteDays}}, - stop); - std::string url = r.body.substr(0, r.body.find_first_of("\r\n")); - if (r.status != 201 || !url.starts_with("https://dpaste.com/")) - throw std::runtime_error(std::format("The upload service answered {}.", r.status)); - return url; + return privatebin::paste(kPasteServer, text, kPasteExpire, stop); } } // namespace venture::launcher diff --git a/src/launcher/debug_report.h b/src/launcher/debug_report.h index 51d1176..5944b7a 100644 --- a/src/launcher/debug_report.h +++ b/src/launcher/debug_report.h @@ -29,8 +29,9 @@ struct DebugReport { // The home folder, user and computer names, account details and anything // shaped like a token are removed; credentials and crash dumps are never read. fs::path write_debug_report(const DebugReport &request); -// Uploads a written report to dpaste.com as an unlisted paste that expires -// after 90 days and returns its link. Throws when it cannot. +// Uploads a written report to the PrivateBin server as an encrypted paste that +// expires after 30 days and returns its link (the key is only in the link). +// Throws when it cannot. std::string upload_debug_report(const fs::path &file, std::stop_token stop = {}); // Exposed for tests. std::string redact(std::string text, const std::vector &private_text); diff --git a/src/launcher/ui/views.cpp b/src/launcher/ui/views.cpp index 3e2c4b9..fbb745e 100644 --- a/src/launcher/ui/views.cpp +++ b/src/launcher/ui/views.cpp @@ -3965,7 +3965,7 @@ void App::draw_report() { "instead.", report_->error, paths::utf8(report_->file.filename()))); else dialog_text("Debug report uploaded", - "Put this link in a GitHub issue or our Discord with what happened. It stays readable for 90 days."); + "Put this link in a GitHub issue or our Discord with what happened. It stays readable for 30 days."); // The link, or the upload in progress, in a field-like box. if (!failed) { gap(16); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f31910b..7c8e52a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable(venture_tests controls_test.cpp paths_test.cpp digest_test.cpp + privatebin_test.cpp search_test.cpp auth_test.cpp sign_in_link_test.cpp diff --git a/tests/privatebin_test.cpp b/tests/privatebin_test.cpp new file mode 100644 index 0000000..5cda52a --- /dev/null +++ b/tests/privatebin_test.cpp @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#include "common/crypto.h" +#include "common/privatebin.h" +#include +#include +#include +using namespace venture; +namespace { +std::string hex(const crypto::Bytes &b) { + static const char *d = "0123456789abcdef"; + std::string s; + for (auto c : b) { s += d[c >> 4]; s += d[c & 15]; } + return s; +} +crypto::Bytes seq(int from, int n) { crypto::Bytes b(n); std::iota(b.begin(), b.end(), from); return b; } +std::span view(const std::string &s) { return {reinterpret_cast(s.data()), s.size()}; } +} // namespace +// Expected values were produced by Python's hashlib and the cryptography package. +TEST_CASE("PBKDF2-SHA256 and AES-256-GCM match known answers") { + auto pw = seq(0, 32), salt = seq(100, 8), nonce = seq(200, 12), key = seq(7, 32); + CHECK(hex(crypto::pbkdf2_sha256(pw, salt, 100000, 32)) == "30b1106c7a8413471fcc0d56191f9ca3c062b5486830bebb758d321e9ad689d2"); + std::string aad = R"([["x","y",100000,256,128,"aes","gcm","zlib"],"plaintext",0,0])", plain = "hello privatebin"; + auto sealed = crypto::aes256_gcm_encrypt(key, nonce, view(aad), view(plain)); + CHECK(hex(sealed) == "b4fd1597c0c93c7983e89141b792fe9fdcbce5fc9ac63d8e16aee1137716fb9f"); + CHECK(crypto::aes256_gcm_decrypt(key, nonce, view(aad), sealed) == crypto::Bytes(plain.begin(), plain.end())); + auto bad = sealed; bad.back() ^= 1; + CHECK_THROWS(crypto::aes256_gcm_decrypt(key, nonce, view(aad), bad)); + CHECK_THROWS(crypto::aes256_gcm_decrypt(key, nonce, view(plain), sealed)); + CHECK(crypto::random_bytes(16) != crypto::random_bytes(16)); +} +TEST_CASE("PrivateBin encodings and paste round trip") { + CHECK(privatebin::base64(crypto::Bytes{}) == ""); + CHECK(privatebin::base64(crypto::Bytes{'f'}) == "Zg=="); + CHECK(privatebin::base64(crypto::Bytes{'f', 'o'}) == "Zm8="); + CHECK(privatebin::base64(crypto::Bytes{'f', 'o', 'o'}) == "Zm9v"); + CHECK(privatebin::base58(crypto::Bytes{0, 0, 1}) == "112"); + CHECK(privatebin::base58(crypto::Bytes{'h', 'e', 'l', 'l', 'o'}) == "Cn8eVZg"); + std::string text = "report café "quoted" \ tab +"; + for (int i = 0; i < 2000; ++i) text += "12345.678 I [launch] repeated log line " + std::to_string(i) + " +"; + auto enc = privatebin::encrypt(text, "1month"); + CHECK(privatebin::decrypt(enc.body, enc.fragment) == text); + auto other = privatebin::encrypt(text, "1month"); + CHECK_THROWS(privatebin::decrypt(enc.body, other.fragment)); + auto odd = privatebin::encrypt("bad byte ÿþ here", "1day"); + CHECK(privatebin::decrypt(odd.body, odd.fragment).starts_with("bad byte")); +}