refactor: move SecureSocket::formatFingerprint -> SecureUtils std::string formatSSLFingerprint

based on 0e406d4918
         9d8e1faf59
         b793675ef8
This commit is contained in:
sithlord48
2025-01-28 20:13:02 -05:00
committed by Nick Bolton
parent 524c3b0e7b
commit a98f2d745e
6 changed files with 80 additions and 21 deletions

View File

@ -18,6 +18,8 @@ add_library(net STATIC
SecureSocket.h
SocketMultiplexer.cpp
SocketMultiplexer.h
SecureUtils.cpp
SecureUtils.h
TCPListenSocket.cpp
TCPListenSocket.h
TCPSocket.cpp

View File

@ -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<char *>(static_cast<void *>(tempFingerprint)), tempFingerprintLen);
formatFingerprint(fingerprint);
fingerprint = deskflow::formatSSLFingerprint(fingerprint);
LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str()));
std::string trustedServersFilename;

View File

@ -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);

View File

@ -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

24
src/lib/net/SecureUtils.h Normal file
View File

@ -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 <string>
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

View File

@ -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 <gtest/gtest.h>
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"
);
}