From cd7e731cb730e912f512ea8b5821aa7b10f90b52 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Mon, 1 Nov 2021 02:52:37 +0200 Subject: [PATCH] lib: Switch to std::vector for fingerprint data --- src/lib/base/String.cpp | 6 +++--- src/lib/base/String.h | 2 +- src/lib/net/SecureSocket.cpp | 6 ++++-- src/lib/net/SecureUtils.cpp | 8 ++------ src/lib/net/SecureUtils.h | 5 +++-- src/test/global/TestUtils.cpp | 6 +++--- src/test/global/TestUtils.h | 4 ++-- src/test/unittests/base/StringTests.cpp | 2 +- src/test/unittests/net/SecureUtilsTests.cpp | 4 ++-- 9 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/lib/base/String.cpp b/src/lib/base/String.cpp index 19ee8f62..413ad449 100644 --- a/src/lib/base/String.cpp +++ b/src/lib/base/String.cpp @@ -221,12 +221,12 @@ removeFileExt(std::string filename) return filename.substr(0, dot); } -std::string to_hex(const std::string& subject, int width, const char fill) +std::string to_hex(const std::vector& subject, int width, const char fill) { std::stringstream ss; ss << std::hex; - for (unsigned int i = 0; i < subject.length(); i++) { - ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i]; + for (unsigned int i = 0; i < subject.size(); i++) { + ss << std::setw(width) << std::setfill(fill) << static_cast(subject[i]); } return ss.str(); diff --git a/src/lib/base/String.h b/src/lib/base/String.h index 4a2e43bf..9c5a53ba 100644 --- a/src/lib/base/String.h +++ b/src/lib/base/String.h @@ -75,7 +75,7 @@ std::string removeFileExt(std::string filename); /*! Convert each character in \c subject into hexdecimal form with \c width */ -std::string to_hex(const std::string& subject, int width, const char fill = '0'); +std::string to_hex(const std::vector& subject, int width, const char fill = '0'); /// Convert binary data from hexadecimal std::vector from_hex(const std::string& data); diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index 7186f249..39f2a1fd 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -674,8 +674,10 @@ SecureSocket::verifyCertFingerprint() } // format fingerprint into hexdecimal format with colon separator - std::string fingerprint(reinterpret_cast(tempFingerprint), tempFingerprintLen); - fingerprint = barrier::format_ssl_fingerprint(fingerprint); + std::vector fingerprint_raw; + fingerprint_raw.assign(reinterpret_cast(tempFingerprint), + reinterpret_cast(tempFingerprint) + tempFingerprintLen); + auto fingerprint = barrier::format_ssl_fingerprint(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 d45d694f..000c56ed 100644 --- a/src/lib/net/SecureUtils.cpp +++ b/src/lib/net/SecureUtils.cpp @@ -20,13 +20,9 @@ namespace barrier { -std::string format_ssl_fingerprint(const std::string& fingerprint, bool hex, bool separator) +std::string format_ssl_fingerprint(const std::vector& fingerprint, bool separator) { - std::string result = fingerprint; - if (hex) { - // to hexadecimal - result = barrier::string::to_hex(result, 2); - } + std::string result = barrier::string::to_hex(fingerprint, 2); // all uppercase barrier::string::uppercase(result); diff --git a/src/lib/net/SecureUtils.h b/src/lib/net/SecureUtils.h index 7dd680ed..50e944e1 100644 --- a/src/lib/net/SecureUtils.h +++ b/src/lib/net/SecureUtils.h @@ -19,11 +19,12 @@ #define BARRIER_LIB_NET_SECUREUTILS_H #include +#include namespace barrier { -std::string format_ssl_fingerprint(const std::string& fingerprint, - bool hex = true, bool separator = true); +std::string format_ssl_fingerprint(const std::vector& fingerprint, + bool separator = true); } // namespace barrier diff --git a/src/test/global/TestUtils.cpp b/src/test/global/TestUtils.cpp index 2aa7ca18..6a3193bf 100644 --- a/src/test/global/TestUtils.cpp +++ b/src/test/global/TestUtils.cpp @@ -20,18 +20,18 @@ namespace barrier { -std::string generate_pseudo_random_bytes(std::size_t seed, std::size_t size) +std::vector generate_pseudo_random_bytes(std::size_t seed, std::size_t size) { std::mt19937_64 engine{seed}; std::uniform_int_distribution dist{0, 255}; - std::vector bytes; + std::vector bytes; bytes.reserve(size); for (std::size_t i = 0; i < size; ++i) { bytes.push_back(dist(engine)); } - return std::string{bytes.data(), bytes.size()}; + return bytes; } } // namespace barrier diff --git a/src/test/global/TestUtils.h b/src/test/global/TestUtils.h index b27e2c6e..31050ece 100644 --- a/src/test/global/TestUtils.h +++ b/src/test/global/TestUtils.h @@ -19,11 +19,11 @@ #define BARRIER_TEST_GLOBAL_TEST_UTILS_H #include -#include +#include namespace barrier { -std::string generate_pseudo_random_bytes(std::size_t seed, std::size_t size); +std::vector generate_pseudo_random_bytes(std::size_t seed, std::size_t size); } // namespace barrier diff --git a/src/test/unittests/base/StringTests.cpp b/src/test/unittests/base/StringTests.cpp index c9c4732b..ad5d5157 100644 --- a/src/test/unittests/base/StringTests.cpp +++ b/src/test/unittests/base/StringTests.cpp @@ -56,7 +56,7 @@ TEST(StringTests, sprintf_formatWithArgument_formatedString) TEST(StringTests, toHex_plaintext_hexString) { - String subject = "foobar"; + std::vector subject{'f', 'o', 'o', 'b', 'a', 'r'}; int width = 2; EXPECT_EQ("666f6f626172", string::to_hex(subject, width)); diff --git a/src/test/unittests/net/SecureUtilsTests.cpp b/src/test/unittests/net/SecureUtilsTests.cpp index eb5ae498..c2394bf9 100644 --- a/src/test/unittests/net/SecureUtilsTests.cpp +++ b/src/test/unittests/net/SecureUtilsTests.cpp @@ -24,8 +24,8 @@ namespace barrier { TEST(SecureUtilsTest, FormatSslFingerprintHexWithSeparators) { - std::string fingerprint = generate_pseudo_random_bytes(0, 32); - ASSERT_EQ(format_ssl_fingerprint(fingerprint, true, true), + auto fingerprint = generate_pseudo_random_bytes(0, 32); + ASSERT_EQ(format_ssl_fingerprint(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"); }