diff --git a/src/lib/base/String.cpp b/src/lib/base/String.cpp index f71fb952e..0c577aabb 100644 --- a/src/lib/base/String.cpp +++ b/src/lib/base/String.cpp @@ -178,6 +178,17 @@ std::string toHex(const std::string &subject, int width, const char fill) return ss.str(); } +std::string toHex(const std::vector &input, int width, const char fill) +{ + std::stringstream ss; + ss << std::hex; + for (unsigned int i = 0; i < input.size(); i++) { + ss << std::setw(width) << std::setfill(fill) << static_cast(input[i]); + } + + return ss.str(); +} + // clang-format off int fromHexChar(char c) { diff --git a/src/lib/base/String.h b/src/lib/base/String.h index 993ece3b4..74fbdd538 100644 --- a/src/lib/base/String.h +++ b/src/lib/base/String.h @@ -65,6 +65,15 @@ Return a new hexString */ std::string toHex(const std::string &subject, int width, const char fill = '0'); +/** + * @brief toHex Convert each value in input into a hex string + * @param input vector of uint8_t + * @param width + * @param fill fill character 0 is default + * @return a hex string + */ +std::string toHex(const std::vector &input, int width, const char fill = '0'); + /** * @brief fromHexChar Convert a single char to its hexidecmal value * @param c input char 0-F diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index ab30488d9..2f5e70f5c 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -627,8 +627,11 @@ bool SecureSocket::verifyCertFingerprint() } // format fingerprint into hexdecimal format with colon separator - std::string fingerprint(static_cast(static_cast(tempFingerprint)), tempFingerprintLen); - fingerprint = deskflow::formatSSLFingerprint(fingerprint); + std::vector fingerprint_raw; + fingerprint_raw.assign( + reinterpret_cast(tempFingerprint), reinterpret_cast(tempFingerprint) + tempFingerprintLen + ); + auto fingerprint = deskflow::formatSSLFingerprint(fingerprint_raw); LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str())); std::string trustedServersFilename; diff --git a/src/lib/net/SecureUtils.cpp b/src/lib/net/SecureUtils.cpp index 20522b7d9..c9b1819c6 100644 --- a/src/lib/net/SecureUtils.cpp +++ b/src/lib/net/SecureUtils.cpp @@ -10,12 +10,9 @@ namespace deskflow { -std::string formatSSLFingerprint(const std::string &fingerprint, bool convertToHex, bool enableSeparators) +std::string formatSSLFingerprint(const std::vector &fingerprint, bool enableSeparators) { - std::string result = fingerprint; - - if (convertToHex) - result = deskflow::string::toHex(fingerprint, 2); + std::string result = deskflow::string::toHex(fingerprint, 2); deskflow::string::uppercase(result); diff --git a/src/lib/net/SecureUtils.h b/src/lib/net/SecureUtils.h index 9b1493e80..5b998c69c 100644 --- a/src/lib/net/SecureUtils.h +++ b/src/lib/net/SecureUtils.h @@ -7,18 +7,18 @@ #pragma once +#include #include +#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); +std::string formatSSLFingerprint(const std::vector &fingerprint, bool enableSeparators = true); } // namespace deskflow diff --git a/src/test/unittests/base/StringTests.cpp b/src/test/unittests/base/StringTests.cpp index 29ceee416..d79317600 100644 --- a/src/test/unittests/base/StringTests.cpp +++ b/src/test/unittests/base/StringTests.cpp @@ -1,5 +1,6 @@ /* * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers * SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd. * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception */ @@ -48,6 +49,14 @@ TEST(StringTests, toHex_plaintext_hexString) EXPECT_EQ("666f6f626172", string::toHex("foobar", 2)); } +TEST(StringTests, toHex_vector_uint8_t_hexString) +{ + std::vector subject{'f', 'o', 'o', 'b', 'a', 'r'}; + int width = 2; + + EXPECT_EQ("666f6f626172", string::toHex(subject, width)); +} + TEST(StringTests, fromHexChar_plaintext_hexString) { EXPECT_EQ(-1, string::fromHexChar('z')); diff --git a/src/test/unittests/net/SecureUtilsTests.cpp b/src/test/unittests/net/SecureUtilsTests.cpp index a9ed19477..027503087 100644 --- a/src/test/unittests/net/SecureUtilsTests.cpp +++ b/src/test/unittests/net/SecureUtilsTests.cpp @@ -11,11 +11,11 @@ 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"; + std::vector fingerprint = {40, 253, 10, 152, 138, 14, 161, 108, 215, 232, 108, 167, 238, 88, 65, 113, + 202, 178, 142, 73, 37, 148, 144, 37, 38, 5, 141, 175, 99, 237, 46, 48}; 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" + deskflow::formatSSLFingerprint(fingerprint, 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" ); }