From 342b07ee4c6ccb1063cfc8e6458eea8f510178c3 Mon Sep 17 00:00:00 2001 From: Andrey Konoplyankin Date: Sun, 25 Apr 2021 17:29:01 +0300 Subject: [PATCH 1/2] Refactor Wake Up/Find PC features --- src/main.cpp | 2 +- src/streaming/GameStreamClient.cpp | 41 ++++--- src/streaming/GameStreamClient.hpp | 5 + src/streaming/WakeOnLanManager.cpp | 166 +++++++++++++++++++---------- src/streaming/WakeOnLanManager.hpp | 4 +- src/ui/windows/MainWindow.cpp | 34 +++--- 6 files changed, 164 insertions(+), 88 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a47f401..274798d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,7 +74,7 @@ int main(int argc, const char * argv[]) { GamepadController::instance().init(new SwitchGamepadFrontend()); nanogui::init(); - nanogui::ref app = new Application(Size(m_width, m_height), Size(m_fb_width, m_fb_height)); + nanogui::ref app = new Application({ (float)m_width, (float)m_height }, { (float)m_fb_width, (float)m_fb_height }); nanogui::setup(1.0 / 15.0); diff --git a/src/streaming/GameStreamClient.cpp b/src/streaming/GameStreamClient.cpp index 81b8eec..382f23e 100644 --- a/src/streaming/GameStreamClient.cpp +++ b/src/streaming/GameStreamClient.cpp @@ -10,12 +10,14 @@ #include #include #include + +#if defined(__linux) || defined(__APPLE__) #include #include #include #include -#ifdef __SWITCH__ +#elif defined(__SWITCH__) #include #endif @@ -83,14 +85,9 @@ void GameStreamClient::stop() { #endif } -std::vector GameStreamClient::host_addresses_for_find() { - std::vector addresses; +static uint32_t get_my_ip_address() { uint32_t address = 0; - - #ifdef __SWITCH__ - Result result = nifmGetCurrentIpAddress(&address); - bool isSucceed = R_SUCCEEDED(result); - #else +#if defined(__linux) || defined(__APPLE__) struct ifreq ifr; ifr.ifr_addr.sa_family = AF_INET; strncpy(ifr.ifr_name, "en0", IFNAMSIZ - 1); @@ -100,9 +97,17 @@ std::vector GameStreamClient::host_addresses_for_find() { close(fd); address = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; +#elif defined(__SWITCH__) + nifmGetCurrentIpAddress(&address); +#endif + return address; +} + +std::vector GameStreamClient::host_addresses_for_find() { + std::vector addresses; - bool isSucceed = true; - #endif + uint32_t address = get_my_ip_address(); + bool isSucceed = address != 0; if (isSucceed) { int a = address & 0xFF; @@ -120,6 +125,10 @@ std::vector GameStreamClient::host_addresses_for_find() { return addresses; } +bool GameStreamClient::can_find_host() { + return get_my_ip_address() != 0; +} + void GameStreamClient::find_host(ServerCallback callback) { perform_async([this, callback] { auto addresses = host_addresses_for_find(); @@ -152,13 +161,19 @@ void GameStreamClient::find_host(ServerCallback callback) { }); } +bool GameStreamClient::can_wake_up_host(const Host &host) { + return WakeOnLanManager::instance().can_wake_up_host(host); +} + void GameStreamClient::wake_up_host(const Host &host, ServerCallback callback) { perform_async([this, host, callback] { - if (WakeOnLanManager::instance().wake_up_host(host)) { + auto result = WakeOnLanManager::instance().wake_up_host(host); + + if (result.isSuccess()) { usleep(5'000'000); - nanogui::async([callback] { callback(GSResult::success(true)); }); + nanogui::async([callback, result] { callback(result); }); } else { - nanogui::async([callback] { callback(GSResult::failure("Wake up failed...")); }); + nanogui::async([callback, result] { callback(result); }); } }); } diff --git a/src/streaming/GameStreamClient.hpp b/src/streaming/GameStreamClient.hpp index ee48cb8..b50fd28 100644 --- a/src/streaming/GameStreamClient.hpp +++ b/src/streaming/GameStreamClient.hpp @@ -67,8 +67,13 @@ public: void stop(); std::vector host_addresses_for_find(); + + bool can_find_host(); void find_host(ServerCallback callback); + + bool can_wake_up_host(const Host &host); void wake_up_host(const Host &host, ServerCallback callback); + void connect(const std::string &address, ServerCallback callback); void pair(const std::string &address, const std::string &pin, ServerCallback callback); void applist(const std::string &address, ServerCallback callback); diff --git a/src/streaming/WakeOnLanManager.cpp b/src/streaming/WakeOnLanManager.cpp index 431ff40..54fa940 100644 --- a/src/streaming/WakeOnLanManager.cpp +++ b/src/streaming/WakeOnLanManager.cpp @@ -2,26 +2,28 @@ #include "Data.hpp" #include "Logger.hpp" #include "Settings.hpp" -#include -#include -#include -#include +#include #include #include -static const int numPorts = 5; -static const int ports[numPorts] = {7, 9, 47998, 47999, 48000}; +#if defined(__linux) || defined(__APPLE__) || defined(__SWITCH__) +#define UNIX_SOCKS +#include +#include +#include +#include +#include -static void populate_address(struct sockaddr_storage* addr, unsigned short port) { - if (addr->ss_family == AF_INET) { - struct sockaddr_in *sin = (struct sockaddr_in*)addr; - sin->sin_port = htons(port); - } - else if (addr->ss_family == AF_INET6) { - struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addr; - sin6->sin6_port = htons(port); - } -} +#elif defined(_WIN32) +#define WIN32_SOCKS + +#include +#include +#include + +#endif + +#if defined(UNIX_SOCKS) || defined(WIN32_SOCKS) static Data mac_string_to_bytes(std::string mac) { std::string str = mac; @@ -51,49 +53,99 @@ static Data create_payload(const Host &host) { } return payload; } +#endif -bool WakeOnLanManager::wake_up_host(const Host &host) { - bool result = false; +#if defined(UNIX_SOCKS) +GSResult send_packet_unix(const Host &host, const Data &payload) { + struct sockaddr_in udpClient, udpServer; + int broadcast = 1; + + int udpSocket = socket(AF_INET, SOCK_DGRAM, 0); + if (udpSocket == -1) { + Logger::error("WakeOnLanManager", "An error was encountered creating the UDP socket: '%s'", strerror(errno)); + return GSResult::failure("An error was encountered creating the UDP socket: " + std::string(strerror(errno))); + } + + int setsock_result = setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast); + if (setsock_result == -1) { + Logger::error("WakeOnLanManager", "Failed to set socket options: '%s'", strerror(errno)); + return GSResult::failure("Failed to set socket options: " + std::string(strerror(errno))); + } + + // Set parameters + udpClient.sin_family = AF_INET; + udpClient.sin_addr.s_addr = INADDR_ANY; + udpClient.sin_port = 0; + // Bind socket + int bind_result = bind(udpSocket, (struct sockaddr*) &udpClient, sizeof(udpClient)); + if (bind_result == -1) { + Logger::error("WakeOnLanManager", "Failed to bind socket: '%s'", strerror(errno)); + return GSResult::failure("Failed to bind socket: " + std::string(strerror(errno))); + } + + // Set server end point (the broadcast addres) + udpServer.sin_family = AF_INET; + udpServer.sin_addr.s_addr = inet_addr(host.address.c_str()); + udpServer.sin_port = htons(9); + + // Send the packet + ssize_t result = sendto(udpSocket, payload.bytes(), sizeof(unsigned char) * 102, 0, (struct sockaddr*) &udpServer, sizeof(udpServer)); + if (result == -1) { + Logger::error("WakeOnLanManager", "Failed to send magic packet to socket: '%s'", strerror(errno)); + return GSResult::failure("Failed to send magic packet to socket: " + std::string(strerror(errno))); + } + return GSResult::success(true); +} +#elif defined(_WIN32) +GSResult send_packet_win32(const Host &host, const Data &payload) { + struct sockaddr_in udpClient, udpServer; + int broadcast = 1; + + WSADATA data; + SOCKET udpSocket; + + // Setup broadcast socket + WSAStartup(MAKEWORD(2, 2), &data); + udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast, sizeof(broadcast)) == -1){ + return GSResult::failure("Failed to setup a broadcast socket"); + } + + // Set parameters + udpClient.sin_family = AF_INET; + udpClient.sin_addr.s_addr = INADDR_ANY; + udpClient.sin_port = htons(0); + // Bind socket + bind(udpSocket, (struct sockaddr*) &udpClient, sizeof(udpClient)); + + // Set server end point (the broadcast addres) + udpServer.sin_family = AF_INET; + udpServer.sin_addr.s_addr = inet_addr(host.address.c_str()); + udpServer.sin_port = htons(9); + + // Send the packet + sendto(udpSocket, payload.bytes(), sizeof(unsigned char) * 102, 0, (struct sockaddr*) &udpServer, sizeof(udpServer)); + return GSResult::success(true); +} +#endif + +bool WakeOnLanManager::can_wake_up_host(const Host &host) { +#if defined(UNIX_SOCKS) || defined(WIN32_SOCKS) + return true; +#else + return false; +#endif +} + +GSResult WakeOnLanManager::wake_up_host(const Host &host) { Data payload = create_payload(host); - struct addrinfo hints, *res, *curr; +#if defined(UNIX_SOCKS) + return send_packet_unix(host, payload); +#elif defined(_WIN32) + return send_packet_win32(host, payload); +#endif - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_ADDRCONFIG; - - if (getaddrinfo(host.address.c_str(), NULL, &hints, &res) != 0 || res == NULL) { - // Failed to resolve address - Logger::error("WakeOnLanManager", "Failed to resolve WOL address"); - return result; - } - - // Try all addresses that this DNS name resolves to. We have - // to create a new socket each time because the addresses - // may be different address families. - for (curr = res; curr != NULL; curr = curr->ai_next) { - int wol_socket = socket(curr->ai_family, SOCK_DGRAM, IPPROTO_UDP); - if (wol_socket < 0) { - Logger::error("WakeOnLanManager", "Failed to create WOL socket"); - continue; - } - - int val = 1; - setsockopt(wol_socket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)); - - struct sockaddr_storage addr; - memset(&addr, 0, sizeof(addr)); - memcpy(&addr, curr->ai_addr, curr->ai_addrlen); - - for (int j = 0; j < numPorts; j++) { - populate_address(&addr, ports[j]); - long err = sendto(wol_socket, payload.bytes(), payload.size(), 0, (struct sockaddr*)&addr, curr->ai_addrlen); - result = result || err > 0; - Logger::info("WakeOnLanManager", "Sending WOL packet returned: %ld", err); - } - - close(wol_socket); - } - freeaddrinfo(res); - return result; + return GSResult::failure("Wake up host not supported..."); } diff --git a/src/streaming/WakeOnLanManager.hpp b/src/streaming/WakeOnLanManager.hpp index 307a010..033195b 100644 --- a/src/streaming/WakeOnLanManager.hpp +++ b/src/streaming/WakeOnLanManager.hpp @@ -1,11 +1,13 @@ #include "Singleton.hpp" +#include "GameStreamClient.hpp" #include struct Host; class WakeOnLanManager: public Singleton { private: - bool wake_up_host(const Host &host); + bool can_wake_up_host(const Host &host); + GSResult wake_up_host(const Host &host); friend class GameStreamClient; }; diff --git a/src/ui/windows/MainWindow.cpp b/src/ui/windows/MainWindow.cpp index 5f62b2b..b8e782b 100644 --- a/src/ui/windows/MainWindow.cpp +++ b/src/ui/windows/MainWindow.cpp @@ -63,7 +63,7 @@ void MainWindow::reload() { } else { auto alert = screen()->add("Error", "Innactive host...", false); - if (!button->host().mac.empty()) { + if (!button->host().mac.empty() && GameStreamClient::instance().can_wake_up_host(button->host())) { alert->add_button("Wake Up", [this, button] { wake_up_host(button->host()); }); @@ -78,21 +78,23 @@ void MainWindow::reload() { }); } - auto find_button = button_container->add(AddHostButtonTypeFind); - find_button->set_fixed_size({ 200, 200 }); - find_button->set_callback([this] { - auto addresses = GameStreamClient::instance().host_addresses_for_find(); - - if (addresses.empty()) { - screen()->add("Error", "Can't obtain IP address..."); - } else { - auto alert = screen()->add("Find Host", "Search Host PC in the same network as yours Switch by evalute IP addresses from " + addresses.front() + " to " + addresses.back() + ".\nPlease check your PC and Switch network before tap on a Find.", false); - alert->add_button("Find", [this] { - find_host(); - }); - alert->add_button("Cancel"); - } - }); + if (GameStreamClient::instance().can_find_host()) { + auto find_button = button_container->add(AddHostButtonTypeFind); + find_button->set_fixed_size({ 200, 200 }); + find_button->set_callback([this] { + auto addresses = GameStreamClient::instance().host_addresses_for_find(); + + if (addresses.empty()) { + screen()->add("Error", "Can't obtain IP address..."); + } else { + auto alert = screen()->add("Find Host", "Search Host PC in the same network as yours Switch by evalute IP addresses from " + addresses.front() + " to " + addresses.back() + ".\nPlease check your PC and Switch network before tap on a Find.", false); + alert->add_button("Find", [this] { + find_host(); + }); + alert->add_button("Cancel"); + } + }); + } auto add_button = button_container->add(AddHostButtonTypeAdd); add_button->set_fixed_size({ 200, 200 }); From 0611bf28ed714431f6fe1874a63200ed028217da Mon Sep 17 00:00:00 2001 From: Andrey Konoplyankin Date: Sun, 25 Apr 2021 17:35:18 +0300 Subject: [PATCH 2/2] Update moonlight-common-c; Remove mbedtls_to_openssl_wrapper, use build in MBEDTLS crypto from moonlight-common-c --- Makefile | 5 +- moonlight.xcodeproj/project.pbxproj | 12 ++- src/switch/mbedtls_to_openssl_wrapper.cpp | 121 ---------------------- third_party/moonlight-common-c | 2 +- 4 files changed, 12 insertions(+), 128 deletions(-) delete mode 100644 src/switch/mbedtls_to_openssl_wrapper.cpp diff --git a/Makefile b/Makefile index 99a44ec..63b8225 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,8 @@ M_INCLUDES := \ -I$(TOPDIR)/third_party/nanogui/ext/nanovg/src DEFINES := -DNANOGUI_USE_OPENGL -DNVG_STB_IMAGE_IMPLEMENTATION -DNANOGUI_NO_GLFW \ - -DHAS_SOCKLEN_T -DHAS_POLL -DHAS_FCNTL -DUSE_MBEDTLS_CRYPTO -DMOONLIGHT_VERSION=\"$(MOONLIGHT_VERSION)\" + -DUSE_MBEDTLS -DHAS_SOCKLEN_T -DHAS_POLL -DHAS_FCNTL -DUSE_MBEDTLS_CRYPTO \ + -DMOONLIGHT_VERSION=\"$(MOONLIGHT_VERSION)\" CFLAGS := -g -Wall -fcompare-debug-second -O2 -ffunction-sections $(ARCH) $(DEFINES) $(INCLUDE) $(M_INCLUDES) -D__SWITCH__ CXXFLAGS := $(CFLAGS) -std=gnu++17 @@ -118,7 +119,6 @@ MOONLIGHT_LIBRETRO_CXX_SOURCES = \ GLVideoRenderer.cpp \ Data.cpp \ MbedTLSCryptoManager.cpp \ - mbedtls_to_openssl_wrapper.cpp \ AudrenAudioRenderer.cpp \ BoxArtManager.cpp \ Logger.cpp \ @@ -157,6 +157,7 @@ MOONLIGHT_COMMON_C_SOURCES = \ Misc.c \ Platform.c \ PlatformSockets.c \ + PlatformCrypto.c \ RtpFecQueue.c \ RtpReorderQueue.c \ RtspConnection.c \ diff --git a/moonlight.xcodeproj/project.pbxproj b/moonlight.xcodeproj/project.pbxproj index 2cc424f..5030686 100644 --- a/moonlight.xcodeproj/project.pbxproj +++ b/moonlight.xcodeproj/project.pbxproj @@ -29,8 +29,8 @@ 28896597262C88C700139ABE /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 28896596262C88C700139ABE /* CoreMedia.framework */; }; 28896599262C88D000139ABE /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 28896598262C88D000139ABE /* CoreVideo.framework */; }; 2889659D262C88DD00139ABE /* VideoToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2889659C262C88DC00139ABE /* VideoToolbox.framework */; }; - 288965AF262C89D800139ABE /* mbedtls_to_openssl_wrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 36F16474247473A300D70AD9 /* mbedtls_to_openssl_wrapper.cpp */; }; 28AD4A752606120A009314C6 /* glad.c in Sources */ = {isa = PBXBuildFile; fileRef = 28AD4A722606120A009314C6 /* glad.c */; }; + 28EE5DAC2635B49E00C9C623 /* PlatformCrypto.c in Sources */ = {isa = PBXBuildFile; fileRef = 28EE5DAA2635B49E00C9C623 /* PlatformCrypto.c */; }; 3602C3B7245D903000368900 /* HostButton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3602C3B5245D903000368900 /* HostButton.cpp */; }; 3602C3BA245DB3C800368900 /* AppListWindow.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3602C3B8245DB3C800368900 /* AppListWindow.cpp */; }; 3602C3BD245DBA9100368900 /* AppButton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3602C3BB245DBA9100368900 /* AppButton.cpp */; }; @@ -170,6 +170,8 @@ 28AD4A712606120A009314C6 /* glad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = glad.h; sourceTree = ""; }; 28AD4A722606120A009314C6 /* glad.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = glad.c; sourceTree = ""; }; 28AD4A742606120A009314C6 /* khrplatform.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = khrplatform.h; sourceTree = ""; }; + 28EE5DAA2635B49E00C9C623 /* PlatformCrypto.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = PlatformCrypto.c; sourceTree = ""; }; + 28EE5DAB2635B49E00C9C623 /* PlatformCrypto.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlatformCrypto.h; sourceTree = ""; }; 3602C3B5245D903000368900 /* HostButton.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = HostButton.cpp; sourceTree = ""; }; 3602C3B6245D903000368900 /* HostButton.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = HostButton.hpp; sourceTree = ""; }; 3602C3B8245DB3C800368900 /* AppListWindow.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AppListWindow.cpp; sourceTree = ""; }; @@ -392,7 +394,6 @@ 36F1646F2474736E00D70AD9 /* switch_wrapper.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = switch_wrapper.c; sourceTree = ""; }; 36F164712474736E00D70AD9 /* evp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = evp.h; sourceTree = ""; }; 36F164722474736E00D70AD9 /* rand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rand.h; sourceTree = ""; }; - 36F16474247473A300D70AD9 /* mbedtls_to_openssl_wrapper.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = mbedtls_to_openssl_wrapper.cpp; sourceTree = ""; }; 36F16476247481F200D70AD9 /* AudrenAudioRenderer.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AudrenAudioRenderer.cpp; sourceTree = ""; }; 36F16477247481F200D70AD9 /* AudrenAudioRenderer.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = AudrenAudioRenderer.hpp; sourceTree = ""; }; /* End PBXFileReference section */ @@ -732,6 +733,8 @@ isa = PBXGroup; children = ( 3652F04E245C292B001FABF3 /* Limelight.h */, + 28EE5DAA2635B49E00C9C623 /* PlatformCrypto.c */, + 28EE5DAB2635B49E00C9C623 /* PlatformCrypto.h */, 3652F047245C292B001FABF3 /* PlatformThreads.h */, 3652F048245C292B001FABF3 /* Input.h */, 3652F049245C292B001FABF3 /* ControlStream.c */, @@ -952,7 +955,6 @@ children = ( 36F164702474736E00D70AD9 /* openssl */, 36F1646F2474736E00D70AD9 /* switch_wrapper.c */, - 36F16474247473A300D70AD9 /* mbedtls_to_openssl_wrapper.cpp */, ); path = switch; sourceTree = ""; @@ -1148,11 +1150,11 @@ 3652EFD0245B3B00001FABF3 /* vscrollpanel.cpp in Sources */, 3652F06D245C292B001FABF3 /* win32.c in Sources */, 3652F002245B6961001FABF3 /* AddHostWindow.cpp in Sources */, - 288965AF262C89D800139ABE /* mbedtls_to_openssl_wrapper.cpp in Sources */, 3652EFF8245B4EE2001FABF3 /* Application.cpp in Sources */, 3652F07E245C292B001FABF3 /* AudioStream.c in Sources */, 3652F076245C292B001FABF3 /* Connection.c in Sources */, 3652EFE4245B3B00001FABF3 /* combobox.cpp in Sources */, + 28EE5DAC2635B49E00C9C623 /* PlatformCrypto.c in Sources */, 3652EFF2245B3B00001FABF3 /* textbox.cpp in Sources */, 3652F074245C292B001FABF3 /* Platform.c in Sources */, 3661D2FF2469E0C00060EE24 /* GLVideoRenderer.cpp in Sources */, @@ -1271,6 +1273,7 @@ "-DHAVE_PULSE", "-DUSE_MBEDTLS_CRYPTO", "-DMOONLIGHT_VERSION=\\\"1.1.0\\\"", + "-DUSE_MBEDTLS", ); OTHER_LDFLAGS = ( "-lglfw", @@ -1364,6 +1367,7 @@ "-DHAVE_PULSE", "-DUSE_MBEDTLS_CRYPTO", "-DMOONLIGHT_VERSION=\\\"1.1.0\\\"", + "-DUSE_MBEDTLS", ); OTHER_LDFLAGS = ( "-lglfw", diff --git a/src/switch/mbedtls_to_openssl_wrapper.cpp b/src/switch/mbedtls_to_openssl_wrapper.cpp deleted file mode 100644 index ce910c5..0000000 --- a/src/switch/mbedtls_to_openssl_wrapper.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include -#include -#include -#include -#include "Data.hpp" - -struct MBED_CIPHER_CTX { - mbedtls_gcm_context ctx; - int iv_len; - const unsigned char *iv; - unsigned char* tag; -}; - -const EVP_CIPHER *EVP_aes_128_gcm(void) { - return NULL; -} - -const EVP_CIPHER *EVP_aes_128_cbc(void) { - return NULL; -} - -void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) { - if (ctx) { - mbedtls_gcm_init(&ctx->ctx); - ctx->iv_len = 0; - ctx->iv = NULL; - ctx->tag = NULL; - } -} - -int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *ctx) { - if (ctx) { - mbedtls_gcm_free(&ctx->ctx); - - if (ctx->tag) { - free(ctx->tag); - ctx->tag = NULL; - } - } - return 1; -} - -EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) { - EVP_CIPHER_CTX* ctx = (EVP_CIPHER_CTX *)malloc(sizeof(EVP_CIPHER_CTX)); - EVP_CIPHER_CTX_init(ctx); - return ctx; -} - -void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) { - if (ctx && ctx->tag) { - free(ctx->tag); - } -} - -int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) { - if (ctx && type == EVP_CTRL_GCM_SET_IVLEN) { - ctx->iv_len = arg; - } else if (ctx && type == EVP_CTRL_GCM_GET_TAG) { - unsigned char *tag = (unsigned char*)ptr; - - for (int i = 0; i < arg; i++) { - tag[i] = ctx->tag[i]; - } - } - return 1; -} - -int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv) { - if (ctx && key != NULL && iv != NULL) { - mbedtls_gcm_setkey(&ctx->ctx, MBEDTLS_CIPHER_ID_AES, key, ctx->iv_len * 8); - mbedtls_gcm_starts(&ctx->ctx, MBEDTLS_GCM_ENCRYPT, iv, ctx->iv_len, NULL, 0); - ctx->iv = iv; - } - return 1; -} - -int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl) { - if (ctx) { - ctx->tag = (unsigned char*)malloc(ctx->iv_len); - - mbedtls_gcm_crypt_and_tag(&ctx->ctx, MBEDTLS_GCM_ENCRYPT, inl, ctx->iv, ctx->iv_len, NULL, 0, in, out, ctx->iv_len, ctx->tag); - *outl = inl; - } - return 1; -} - -int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) { - return 1; -} - -// TODO: This is correct? -int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv) { - if (ctx && key != NULL && iv != NULL) { - mbedtls_gcm_setkey(&ctx->ctx, MBEDTLS_CIPHER_ID_AES, key, ctx->iv_len * 8); - mbedtls_gcm_starts(&ctx->ctx, MBEDTLS_GCM_DECRYPT, iv, ctx->iv_len, NULL, 0); - ctx->iv = iv; - } - return 1; -} - -int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl) { - if (ctx) { - ctx->tag = (unsigned char*)malloc(ctx->iv_len); - - mbedtls_gcm_crypt_and_tag(&ctx->ctx, MBEDTLS_GCM_DECRYPT, inl, ctx->iv, ctx->iv_len, NULL, 0, in, out, ctx->iv_len, ctx->tag); - *outl = inl; - } - return 1; -} - -int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl) { - return 1; -} - -int RAND_bytes(unsigned char *buf, int num) { - Data rand = Data::random_bytes(num); - for (int i = 0; i < num; i++) { - buf[i] = rand.bytes()[i]; - } - return 1; -} diff --git a/third_party/moonlight-common-c b/third_party/moonlight-common-c index 5782246..13041e0 160000 --- a/third_party/moonlight-common-c +++ b/third_party/moonlight-common-c @@ -1 +1 @@ -Subproject commit 5782246b30bfc11e659cf53b498886a924aa97d1 +Subproject commit 13041e0323685ff1b2ccade347cb1de850286d23