refactor: use vector<uint8_t> for keys

This commit is contained in:
sithlord48 2025-01-28 21:15:50 -05:00 committed by Nick Bolton
parent a98f2d745e
commit 39da277ead
7 changed files with 44 additions and 15 deletions

View file

@ -178,6 +178,17 @@ std::string toHex(const std::string &subject, int width, const char fill)
return ss.str(); return ss.str();
} }
std::string toHex(const std::vector<uint8_t> &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<int>(input[i]);
}
return ss.str();
}
// clang-format off // clang-format off
int fromHexChar(char c) int fromHexChar(char c)
{ {

View file

@ -65,6 +65,15 @@ Return a new hexString
*/ */
std::string toHex(const std::string &subject, int width, const char fill = '0'); 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<uint8_t> &input, int width, const char fill = '0');
/** /**
* @brief fromHexChar Convert a single char to its hexidecmal value * @brief fromHexChar Convert a single char to its hexidecmal value
* @param c input char 0-F * @param c input char 0-F

View file

@ -627,8 +627,11 @@ bool SecureSocket::verifyCertFingerprint()
} }
// format fingerprint into hexdecimal format with colon separator // format fingerprint into hexdecimal format with colon separator
std::string fingerprint(static_cast<char *>(static_cast<void *>(tempFingerprint)), tempFingerprintLen); std::vector<uint8_t> fingerprint_raw;
fingerprint = deskflow::formatSSLFingerprint(fingerprint); fingerprint_raw.assign(
reinterpret_cast<uint8_t *>(tempFingerprint), reinterpret_cast<uint8_t *>(tempFingerprint) + tempFingerprintLen
);
auto fingerprint = deskflow::formatSSLFingerprint(fingerprint_raw);
LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str())); LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str()));
std::string trustedServersFilename; std::string trustedServersFilename;

View file

@ -10,12 +10,9 @@
namespace deskflow { namespace deskflow {
std::string formatSSLFingerprint(const std::string &fingerprint, bool convertToHex, bool enableSeparators) std::string formatSSLFingerprint(const std::vector<uint8_t> &fingerprint, bool enableSeparators)
{ {
std::string result = fingerprint; std::string result = deskflow::string::toHex(fingerprint, 2);
if (convertToHex)
result = deskflow::string::toHex(fingerprint, 2);
deskflow::string::uppercase(result); deskflow::string::uppercase(result);

View file

@ -7,18 +7,18 @@
#pragma once #pragma once
#include <stdint.h>
#include <string> #include <string>
#include <vector>
namespace deskflow { namespace deskflow {
/**
/**
* @brief formatSSLFingerprint Format an ssl Fingerprint * @brief formatSSLFingerprint Format an ssl Fingerprint
* @param fingerprint input string * @param fingerprint input string
* @param convertToHex when true converts the string to a hex string
* @param enableSeparators insert : seperator every byte when true * @param enableSeparators insert : seperator every byte when true
* @return a Formated Fingerprint String * @return a Formated Fingerprint String
*/ */
std::string std::string formatSSLFingerprint(const std::vector<uint8_t> &fingerprint, bool enableSeparators = true);
formatSSLFingerprint(const std::string &fingerprint, bool convertToHex = true, bool enableSeparators = true);
} // namespace deskflow } // namespace deskflow

View file

@ -1,5 +1,6 @@
/* /*
* Deskflow -- mouse and keyboard sharing utility * Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd. * SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception * 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)); EXPECT_EQ("666f6f626172", string::toHex("foobar", 2));
} }
TEST(StringTests, toHex_vector_uint8_t_hexString)
{
std::vector<std::uint8_t> subject{'f', 'o', 'o', 'b', 'a', 'r'};
int width = 2;
EXPECT_EQ("666f6f626172", string::toHex(subject, width));
}
TEST(StringTests, fromHexChar_plaintext_hexString) TEST(StringTests, fromHexChar_plaintext_hexString)
{ {
EXPECT_EQ(-1, string::fromHexChar('z')); EXPECT_EQ(-1, string::fromHexChar('z'));

View file

@ -11,11 +11,11 @@
TEST(SecureUtilsTest, formatSSLFingerprints_fromHex_withSeperators) 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" std::vector<uint8_t> fingerprint = {40, 253, 10, 152, 138, 14, 161, 108, 215, 232, 108, 167, 238, 88, 65, 113,
"c\xED.0"; 202, 178, 142, 73, 37, 148, 144, 37, 38, 5, 141, 175, 99, 237, 46, 48};
ASSERT_EQ( ASSERT_EQ(
deskflow::formatSSLFingerprint(fingerprint, true, true), "28:FD:0A:98:8A:0E:A1:6C:D7:E8:6C:A7:EE:58:41:71:" deskflow::formatSSLFingerprint(fingerprint, true),
"CA:B2:8E:49:25:94:90:25:26:05:8D:AF:63:ED:2E:30" "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"
); );
} }