lib: Switch to std::vector<std::uint8_t> for fingerprint data

This commit is contained in:
Povilas Kanapickas 2021-11-01 02:52:37 +02:00
parent ef08470286
commit cd7e731cb7
9 changed files with 21 additions and 22 deletions

View File

@ -221,12 +221,12 @@ removeFileExt(std::string filename)
return filename.substr(0, dot); 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<std::uint8_t>& subject, int width, const char fill)
{ {
std::stringstream ss; std::stringstream ss;
ss << std::hex; ss << std::hex;
for (unsigned int i = 0; i < subject.length(); i++) { for (unsigned int i = 0; i < subject.size(); i++) {
ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i]; ss << std::setw(width) << std::setfill(fill) << static_cast<int>(subject[i]);
} }
return ss.str(); return ss.str();

View File

@ -75,7 +75,7 @@ std::string removeFileExt(std::string filename);
/*! /*!
Convert each character in \c subject into hexdecimal form with \c width 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<std::uint8_t>& subject, int width, const char fill = '0');
/// Convert binary data from hexadecimal /// Convert binary data from hexadecimal
std::vector<std::uint8_t> from_hex(const std::string& data); std::vector<std::uint8_t> from_hex(const std::string& data);

View File

@ -674,8 +674,10 @@ SecureSocket::verifyCertFingerprint()
} }
// format fingerprint into hexdecimal format with colon separator // format fingerprint into hexdecimal format with colon separator
std::string fingerprint(reinterpret_cast<char*>(tempFingerprint), tempFingerprintLen); std::vector<std::uint8_t> fingerprint_raw;
fingerprint = barrier::format_ssl_fingerprint(fingerprint); fingerprint_raw.assign(reinterpret_cast<std::uint8_t*>(tempFingerprint),
reinterpret_cast<std::uint8_t*>(tempFingerprint) + tempFingerprintLen);
auto fingerprint = barrier::format_ssl_fingerprint(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

@ -20,13 +20,9 @@
namespace barrier { namespace barrier {
std::string format_ssl_fingerprint(const std::string& fingerprint, bool hex, bool separator) std::string format_ssl_fingerprint(const std::vector<uint8_t>& fingerprint, bool separator)
{ {
std::string result = fingerprint; std::string result = barrier::string::to_hex(fingerprint, 2);
if (hex) {
// to hexadecimal
result = barrier::string::to_hex(result, 2);
}
// all uppercase // all uppercase
barrier::string::uppercase(result); barrier::string::uppercase(result);

View File

@ -19,11 +19,12 @@
#define BARRIER_LIB_NET_SECUREUTILS_H #define BARRIER_LIB_NET_SECUREUTILS_H
#include <string> #include <string>
#include <vector>
namespace barrier { namespace barrier {
std::string format_ssl_fingerprint(const std::string& fingerprint, std::string format_ssl_fingerprint(const std::vector<std::uint8_t>& fingerprint,
bool hex = true, bool separator = true); bool separator = true);
} // namespace barrier } // namespace barrier

View File

@ -20,18 +20,18 @@
namespace barrier { namespace barrier {
std::string generate_pseudo_random_bytes(std::size_t seed, std::size_t size) std::vector<std::uint8_t> generate_pseudo_random_bytes(std::size_t seed, std::size_t size)
{ {
std::mt19937_64 engine{seed}; std::mt19937_64 engine{seed};
std::uniform_int_distribution<int> dist{0, 255}; std::uniform_int_distribution<int> dist{0, 255};
std::vector<char> bytes; std::vector<std::uint8_t> bytes;
bytes.reserve(size); bytes.reserve(size);
for (std::size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
bytes.push_back(dist(engine)); bytes.push_back(dist(engine));
} }
return std::string{bytes.data(), bytes.size()}; return bytes;
} }
} // namespace barrier } // namespace barrier

View File

@ -19,11 +19,11 @@
#define BARRIER_TEST_GLOBAL_TEST_UTILS_H #define BARRIER_TEST_GLOBAL_TEST_UTILS_H
#include <cstdint> #include <cstdint>
#include <string> #include <vector>
namespace barrier { namespace barrier {
std::string generate_pseudo_random_bytes(std::size_t seed, std::size_t size); std::vector<std::uint8_t> generate_pseudo_random_bytes(std::size_t seed, std::size_t size);
} // namespace barrier } // namespace barrier

View File

@ -56,7 +56,7 @@ TEST(StringTests, sprintf_formatWithArgument_formatedString)
TEST(StringTests, toHex_plaintext_hexString) TEST(StringTests, toHex_plaintext_hexString)
{ {
String subject = "foobar"; std::vector<std::uint8_t> subject{'f', 'o', 'o', 'b', 'a', 'r'};
int width = 2; int width = 2;
EXPECT_EQ("666f6f626172", string::to_hex(subject, width)); EXPECT_EQ("666f6f626172", string::to_hex(subject, width));

View File

@ -24,8 +24,8 @@ namespace barrier {
TEST(SecureUtilsTest, FormatSslFingerprintHexWithSeparators) TEST(SecureUtilsTest, FormatSslFingerprintHexWithSeparators)
{ {
std::string fingerprint = generate_pseudo_random_bytes(0, 32); auto fingerprint = generate_pseudo_random_bytes(0, 32);
ASSERT_EQ(format_ssl_fingerprint(fingerprint, true, true), ASSERT_EQ(format_ssl_fingerprint(fingerprint, true),
"28:FD:0A:98:8A:0E:A1:6C:D7:E8:6C:A7:EE:58:41:71:" "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"); "CA:B2:8E:49:25:94:90:25:26:05:8D:AF:63:ED:2E:30");
} }