From a98f2d745e0c5304457494b7386c3c42c27040a7 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Tue, 28 Jan 2025 20:13:02 -0500 Subject: [PATCH] refactor: move SecureSocket::formatFingerprint -> SecureUtils std::string formatSSLFingerprint based on https://github.com/debauchee/barrier/commit/0e406d491823bfc9dfed0fcc7934cdece8db7dd0 https://github.com/debauchee/barrier/commit/9d8e1faf59bbbc0360adc52b964d71a510f4e8a2 https://github.com/debauchee/barrier/commit/b793675ef8cbf7f69fe8ba7cbdf3689e7f60c657 --- src/lib/net/CMakeLists.txt | 2 ++ src/lib/net/SecureSocket.cpp | 22 ++------------- src/lib/net/SecureSocket.h | 1 - src/lib/net/SecureUtils.cpp | 31 +++++++++++++++++++++ src/lib/net/SecureUtils.h | 24 ++++++++++++++++ src/test/unittests/net/SecureUtilsTests.cpp | 21 ++++++++++++++ 6 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 src/lib/net/SecureUtils.cpp create mode 100644 src/lib/net/SecureUtils.h create mode 100644 src/test/unittests/net/SecureUtilsTests.cpp diff --git a/src/lib/net/CMakeLists.txt b/src/lib/net/CMakeLists.txt index 165b521e2..600fd1e87 100644 --- a/src/lib/net/CMakeLists.txt +++ b/src/lib/net/CMakeLists.txt @@ -18,6 +18,8 @@ add_library(net STATIC SecureSocket.h SocketMultiplexer.cpp SocketMultiplexer.h + SecureUtils.cpp + SecureUtils.h TCPListenSocket.cpp TCPListenSocket.h TCPSocket.cpp diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index a13ad5e02..ab30488d9 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -5,6 +5,7 @@ */ #include "SecureSocket.h" +#include "SecureUtils.h" #include "arch/XArch.h" #include "base/Log.h" @@ -610,25 +611,6 @@ void SecureSocket::disconnect() sendEvent(getEvents()->forIStream().inputShutdown()); } -void SecureSocket::formatFingerprint(std::string &fingerprint, bool hex, bool separator) -{ - if (hex) { - // to hexidecimal - fingerprint = deskflow::string::toHex(fingerprint, 2); - } - - // all uppercase - deskflow::string::uppercase(fingerprint); - - if (separator) { - // add colon to separate each 2 charactors - size_t separators = fingerprint.size() / 2; - for (size_t i = 1; i < separators; i++) { - fingerprint.insert(i * 3 - 1, ":"); - } - } -} - bool SecureSocket::verifyCertFingerprint() { // calculate received certificate fingerprint @@ -646,7 +628,7 @@ bool SecureSocket::verifyCertFingerprint() // format fingerprint into hexdecimal format with colon separator std::string fingerprint(static_cast(static_cast(tempFingerprint)), tempFingerprintLen); - formatFingerprint(fingerprint); + fingerprint = deskflow::formatSSLFingerprint(fingerprint); LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str())); std::string trustedServersFilename; diff --git a/src/lib/net/SecureSocket.h b/src/lib/net/SecureSocket.h index ddc91132a..556cbf5ba 100644 --- a/src/lib/net/SecureSocket.h +++ b/src/lib/net/SecureSocket.h @@ -67,7 +67,6 @@ private: bool showCertificate() const; void checkResult(int n, int &retry); void disconnect(); - void formatFingerprint(std::string &fingerprint, bool hex = true, bool separator = true); bool verifyCertFingerprint(); ISocketMultiplexerJob *serviceConnect(ISocketMultiplexerJob *, bool, bool, bool); diff --git a/src/lib/net/SecureUtils.cpp b/src/lib/net/SecureUtils.cpp new file mode 100644 index 000000000..20522b7d9 --- /dev/null +++ b/src/lib/net/SecureUtils.cpp @@ -0,0 +1,31 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers + * SPDX-FileCopyrightText: (C) 2021 Barrier Contributors + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "SecureUtils.h" +#include "base/String.h" + +namespace deskflow { + +std::string formatSSLFingerprint(const std::string &fingerprint, bool convertToHex, bool enableSeparators) +{ + std::string result = fingerprint; + + if (convertToHex) + result = deskflow::string::toHex(fingerprint, 2); + + deskflow::string::uppercase(result); + + if (enableSeparators) { + const auto usedSpaces = 3; + size_t separators = result.size() / 2; + for (size_t i = 1; i < separators; i++) + result.insert(i * usedSpaces - 1, ":"); + } + return result; +} + +} // namespace deskflow diff --git a/src/lib/net/SecureUtils.h b/src/lib/net/SecureUtils.h new file mode 100644 index 000000000..9b1493e80 --- /dev/null +++ b/src/lib/net/SecureUtils.h @@ -0,0 +1,24 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers + * SPDX-FileCopyrightText: (C) 2021 Barrier Contributors + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#pragma once + +#include + +namespace deskflow { +/** + + * @brief formatSSLFingerprint Format an ssl Fingerprint + * @param fingerprint input string + * @param convertToHex when true converts the string to a hex string + * @param enableSeparators insert : seperator every byte when true + * @return a Formated Fingerprint String + */ +std::string +formatSSLFingerprint(const std::string &fingerprint, bool convertToHex = true, bool enableSeparators = true); + +} // namespace deskflow diff --git a/src/test/unittests/net/SecureUtilsTests.cpp b/src/test/unittests/net/SecureUtilsTests.cpp new file mode 100644 index 000000000..a9ed19477 --- /dev/null +++ b/src/test/unittests/net/SecureUtilsTests.cpp @@ -0,0 +1,21 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers + * SPDX-FileCopyrightText: (C) 2021 Barrier Contributors + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "net/SecureUtils.h" + +#include + +TEST(SecureUtilsTest, formatSSLFingerprints_fromHex_withSeperators) +{ + std::string fingerprint = "(\xFD\n\x98\x8A\x0E\xA1l\xD7\xE8l\xA7\xEEXAq\xCA\xB2\x8EI%\x94\x90%&\x05\x8D\xAF" + "c\xED.0"; + + ASSERT_EQ( + deskflow::formatSSLFingerprint(fingerprint, true, true), "28:FD:0A:98:8A:0E:A1:6C:D7:E8:6C:A7:EE:58:41:71:" + "CA:B2:8E:49:25:94:90:25:26:05:8D:AF:63:ED:2E:30" + ); +}