refactor: use QCryptographicHash::Algorithm in place of Fingerprint::Type

This commit is contained in:
sithlord48 2025-11-24 21:01:03 -05:00 committed by Chris Rizzitello
parent 52e0daba0f
commit 2dfba73cfb
10 changed files with 65 additions and 73 deletions

View file

@ -152,7 +152,7 @@ MainWindow::MainWindow()
if (!TlsUtility::isCertValid()) { if (!TlsUtility::isCertValid()) {
generateCertificate(); generateCertificate();
} else { } else {
m_fingerprint = {Fingerprint::Type::SHA256, TlsUtility::certFingerprint()}; m_fingerprint = {QCryptographicHash::Sha256, TlsUtility::certFingerprint()};
} }
} }
} }
@ -803,7 +803,7 @@ void MainWindow::checkFingerprint(const QString &line)
const auto sha256Text = line.mid(midStart + msgLen).remove(':'); const auto sha256Text = line.mid(midStart + msgLen).remove(':');
const Fingerprint sha256 = {Fingerprint::Type::SHA256, QByteArray::fromHex(sha256Text.toLatin1())}; const Fingerprint sha256 = {QCryptographicHash::Sha256, QByteArray::fromHex(sha256Text.toLatin1())};
const bool isClient = m_coreProcess.mode() == CoreMode::Client; const bool isClient = m_coreProcess.mode() == CoreMode::Client;
if ((isClient && m_checkedServers.contains(sha256Text)) || (!isClient && m_checkedClients.contains(sha256Text))) { if ((isClient && m_checkedServers.contains(sha256Text)) || (!isClient && m_checkedClients.contains(sha256Text))) {
@ -1177,7 +1177,7 @@ bool MainWindow::generateCertificate()
return false; return false;
} }
m_fingerprint = {Fingerprint::Type::SHA256, TlsUtility::certFingerprint()}; m_fingerprint = {QCryptographicHash::Sha256, TlsUtility::certFingerprint()};
updateLocalFingerprint(); updateLocalFingerprint();
return true; return true;

View file

@ -22,7 +22,7 @@ FingerprintPreview::FingerprintPreview(
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
setLayout( setLayout(
fingerprint.type == Fingerprint::Type::SHA256 ? sha256Layout(fingerprint, titleText, hashMode) : emptyLayout() fingerprint.type == QCryptographicHash::Sha256 ? sha256Layout(fingerprint, titleText, hashMode) : emptyLayout()
); );
adjustSize(); adjustSize();
setFixedSize(size()); setFixedSize(size());

View file

@ -10,6 +10,7 @@ if(APPLE)
set(OPENSSL_USE_STATIC_LIBS TRUE) set(OPENSSL_USE_STATIC_LIBS TRUE)
endif() endif()
find_package(Qt6 ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Network)
find_package(OpenSSL ${REQUIRED_OPENSSL_VERSION} REQUIRED COMPONENTS SSL Crypto) find_package(OpenSSL ${REQUIRED_OPENSSL_VERSION} REQUIRED COMPONENTS SSL Crypto)
add_library(net STATIC add_library(net STATIC
@ -49,7 +50,7 @@ add_library(net STATIC
target_link_libraries( target_link_libraries(
net net
PUBLIC OpenSSL::SSL OpenSSL::Crypto common PUBLIC OpenSSL::SSL OpenSSL::Crypto Qt6::Network common
PRIVATE mt io) PRIVATE mt io)
if(WIN32) if(WIN32)

View file

@ -12,12 +12,9 @@
bool Fingerprint::isValid() const bool Fingerprint::isValid() const
{ {
switch (type) { switch (type) {
using enum Type; case QCryptographicHash::Sha1:
case Invalid:
return false;
case SHA1:
return data.length() == 20; return data.length() == 20;
case SHA256: case QCryptographicHash::Sha256:
return data.length() == 32; return data.length() == 32;
default: default:
return false; return false;
@ -54,7 +51,7 @@ Fingerprint Fingerprint::fromDbLine(const QString &line)
const bool wrongSize = line.size() != kSha1ExpectedSize; const bool wrongSize = line.size() != kSha1ExpectedSize;
if (bool badColonCount = line.count(':') != kSha1ColonCount; wrongSize || badColonCount) if (bool badColonCount = line.count(':') != kSha1ColonCount; wrongSize || badColonCount)
return result; return result;
result.type = Fingerprint::Type::SHA1; result.type = QCryptographicHash::Sha1;
auto l2 = line; auto l2 = line;
result.data = QByteArray::fromHex(l2.remove(':').toLatin1()); result.data = QByteArray::fromHex(l2.remove(':').toLatin1());
} }
@ -62,25 +59,25 @@ Fingerprint Fingerprint::fromDbLine(const QString &line)
return result; return result;
} }
Fingerprint::Type Fingerprint::typeFromString(const QString &type) QString Fingerprint::typeToString(QCryptographicHash::Algorithm type)
{
using enum Type;
const auto t = type.toLower();
if (t == m_type_sha1)
return SHA1;
if (t == m_type_sha256)
return SHA256;
return Invalid;
}
QString Fingerprint::typeToString(Fingerprint::Type type)
{ {
switch (type) { switch (type) {
case Type::SHA1: case QCryptographicHash::Sha1:
return m_type_sha1; return m_type_sha1;
case Type::SHA256: case QCryptographicHash::Sha256:
return m_type_sha256; return m_type_sha256;
default: default:
return m_type_invalid; return m_type_invalid;
} }
} }
QCryptographicHash::Algorithm Fingerprint::typeFromString(const QString &type)
{
using enum QCryptographicHash::Algorithm;
const auto t = type.toLower();
if (t == m_type_sha1)
return Sha1;
if (t == m_type_sha256)
return Sha256;
return Md4;
}

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <QByteArray> #include <QByteArray>
#include <QCryptographicHash>
#include <QObject> #include <QObject>
struct Fingerprint struct Fingerprint
@ -17,14 +18,9 @@ struct Fingerprint
inline static QString m_type_invalid = QStringLiteral("invalid"); inline static QString m_type_invalid = QStringLiteral("invalid");
public: public:
enum class Type // Since there is no "undefined" or "invalid" we will use MD4 the value of 0 as default.
{ // Any type that is not Sha1 or Sha256 will be considered invalid
Invalid, QCryptographicHash::Algorithm type = QCryptographicHash::Md4;
SHA1,
SHA256
};
Q_ENUM(Type)
Type type = Type::Invalid;
QByteArray data; QByteArray data;
bool isValid() const; bool isValid() const;
@ -32,6 +28,6 @@ public:
bool operator==(const Fingerprint &other) const = default; bool operator==(const Fingerprint &other) const = default;
QString toDbLine() const; QString toDbLine() const;
static Fingerprint fromDbLine(const QString &line); static Fingerprint fromDbLine(const QString &line);
static QString typeToString(Fingerprint::Type type); static QString typeToString(QCryptographicHash::Algorithm type);
static Fingerprint::Type typeFromString(const QString &type); static QCryptographicHash::Algorithm typeFromString(const QString &type);
}; };

View file

@ -628,7 +628,7 @@ void SecureSocket::disconnect()
bool SecureSocket::verifyCertFingerprint(const QString &FingerprintDatabasePath) const bool SecureSocket::verifyCertFingerprint(const QString &FingerprintDatabasePath) const
{ {
const auto cert = SSL_get_peer_certificate(m_ssl->m_ssl); const auto cert = SSL_get_peer_certificate(m_ssl->m_ssl);
const auto sha256 = deskflow::sslCertFingerprint(cert, Fingerprint::Type::SHA256); const auto sha256 = deskflow::sslCertFingerprint(cert, QCryptographicHash::Sha256);
if (cert) if (cert)
X509_free(cert); X509_free(cert);

View file

@ -22,12 +22,12 @@ namespace deskflow {
namespace { namespace {
const EVP_MD *digestForType(Fingerprint::Type type) const EVP_MD *digestForType(QCryptographicHash::Algorithm type)
{ {
switch (type) { switch (type) {
case Fingerprint::Type::SHA1: case QCryptographicHash::Sha1:
return EVP_sha1(); return EVP_sha1();
case Fingerprint::Type::SHA256: case QCryptographicHash::Sha256:
return EVP_sha256(); return EVP_sha256();
default: default:
break; break;
@ -45,7 +45,7 @@ QString formatSSLFingerprint(const QByteArray &fingerprint, bool enableSeparator
return fingerprint.toHex().toUpper(); return fingerprint.toHex().toUpper();
} }
Fingerprint sslCertFingerprint(const X509 *cert, Fingerprint::Type type) Fingerprint sslCertFingerprint(const X509 *cert, QCryptographicHash::Algorithm type)
{ {
if (!cert) { if (!cert) {
throw std::runtime_error("certificate is null"); throw std::runtime_error("certificate is null");
@ -62,7 +62,7 @@ Fingerprint sslCertFingerprint(const X509 *cert, Fingerprint::Type type)
return {type, digestArray}; return {type, digestArray};
} }
Fingerprint pemFileCertFingerprint(const std::string &path, Fingerprint::Type type) Fingerprint pemFileCertFingerprint(const std::string &path, QCryptographicHash::Algorithm type)
{ {
auto fp = fopenUtf8Path(path, "r"); auto fp = fopenUtf8Path(path, "r");
if (!fp) { if (!fp) {

View file

@ -24,9 +24,9 @@ QString formatSSLFingerprint(const QByteArray &fingerprint, bool enableSeparator
QString formatSSLFingerprintColumns(const QByteArray &fingerprint); QString formatSSLFingerprintColumns(const QByteArray &fingerprint);
Fingerprint sslCertFingerprint(const X509 *cert, Fingerprint::Type type); Fingerprint sslCertFingerprint(const X509 *cert, QCryptographicHash::Algorithm type);
Fingerprint pemFileCertFingerprint(const std::string &path, Fingerprint::Type type); Fingerprint pemFileCertFingerprint(const std::string &path, QCryptographicHash::Algorithm type);
void generatePemSelfSignedCert(const std::string &path, int keyLength = 2048); void generatePemSelfSignedCert(const std::string &path, int keyLength = 2048);

View file

@ -10,8 +10,6 @@
#include "net/Fingerprint.h" #include "net/Fingerprint.h"
#include "net/FingerprintDatabase.h" #include "net/FingerprintDatabase.h"
#include <sstream>
void FingerprintDatabaseTests::readFile() void FingerprintDatabaseTests::readFile()
{ {
QString data = R"( QString data = R"(
@ -27,7 +25,7 @@ AB:CD:EF:00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16
// Only one will be in our list as only one is valid // Only one will be in our list as only one is valid
QList<Fingerprint> expected = { QList<Fingerprint> expected = {
{Fingerprint::Type::SHA1, {QCryptographicHash::Algorithm::Sha1,
QByteArray::fromRawData("\xAB\xCD\xEF\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16", 20)} QByteArray::fromRawData("\xAB\xCD\xEF\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16", 20)}
}; };
@ -41,10 +39,10 @@ void FingerprintDatabaseTests::writeFile()
FingerprintDatabase db; FingerprintDatabase db;
db.addTrusted( db.addTrusted(
{Fingerprint::Type::SHA1, QByteArray::fromHex(QString("ABCDEF0001020304050607080910111213141516").toLatin1())} {QCryptographicHash::Sha1, QByteArray::fromHex(QString("ABCDEF0001020304050607080910111213141516").toLatin1())}
); );
db.addTrusted( db.addTrusted(
{Fingerprint::Type::SHA1, QByteArray::fromHex(QString("0001020304050607080910111213141516ABCDEF").toLatin1())} {QCryptographicHash::Sha1, QByteArray::fromHex(QString("0001020304050607080910111213141516ABCDEF").toLatin1())}
); );
db.writeStream(stream); db.writeStream(stream);
@ -56,7 +54,7 @@ v2:sha1:0001020304050607080910111213141516abcdef
void FingerprintDatabaseTests::clear() void FingerprintDatabaseTests::clear()
{ {
FingerprintDatabase db; FingerprintDatabase db;
db.addTrusted({Fingerprint::Type::SHA1, QByteArray::fromHex(QString("01020304ab").toLatin1())}); db.addTrusted({QCryptographicHash::Sha1, QByteArray::fromHex(QString("01020304ab").toLatin1())});
db.clear(); db.clear();
QVERIFY(db.fingerprints().empty()); QVERIFY(db.fingerprints().empty());
@ -64,9 +62,9 @@ void FingerprintDatabaseTests::clear()
void FingerprintDatabaseTests::trusted() void FingerprintDatabaseTests::trusted()
{ {
Fingerprint trusted1 = {Fingerprint::Type::SHA1, QByteArray::fromHex(QString("01020304ab").toLatin1())}; Fingerprint trusted1 = {QCryptographicHash::Sha1, QByteArray::fromHex(QString("01020304ab").toLatin1())};
Fingerprint trusted2 = {Fingerprint::Type::SHA1, QByteArray::fromHex(QString("03040506ab").toLatin1())}; Fingerprint trusted2 = {QCryptographicHash::Sha1, QByteArray::fromHex(QString("03040506ab").toLatin1())};
Fingerprint untrusted = {Fingerprint::Type::SHA1, QByteArray::fromHex(QString("01020304ac").toLatin1())}; Fingerprint untrusted = {QCryptographicHash::Sha1, QByteArray::fromHex(QString("01020304ac").toLatin1())};
FingerprintDatabase db; FingerprintDatabase db;

View file

@ -19,7 +19,7 @@ void FingerprintTests::test_isValid()
QVERIFY(!f.isValid()); QVERIFY(!f.isValid());
// SHA1 Tests // SHA1 Tests
f.type = Fingerprint::Type::SHA1; f.type = QCryptographicHash::Sha1;
// Invalid SHA1, no Data // Invalid SHA1, no Data
f.data.clear(); f.data.clear();
@ -38,7 +38,7 @@ void FingerprintTests::test_isValid()
QVERIFY(!f.isValid()); QVERIFY(!f.isValid());
// SHA256 Tests // SHA256 Tests
f.type = Fingerprint::Type::SHA256; f.type = QCryptographicHash::Sha256;
// Invalid SHA256, no Data // Invalid SHA256, no Data
f.data.clear(); f.data.clear();
@ -69,7 +69,7 @@ void FingerprintTests::test_toDbLine()
QVERIFY(f.toDbLine().isEmpty()); QVERIFY(f.toDbLine().isEmpty());
// Invalid SHA1, type w/o data // Invalid SHA1, type w/o data
f.type = Fingerprint::Type::SHA1; f.type = QCryptographicHash::Sha1;
f.data.clear(); f.data.clear();
QVERIFY(f.toDbLine().isEmpty()); QVERIFY(f.toDbLine().isEmpty());
@ -79,7 +79,7 @@ void FingerprintTests::test_toDbLine()
QCOMPARE(f.toDbLine(), expectedString); QCOMPARE(f.toDbLine(), expectedString);
// Valid Sha256 // Valid Sha256
f.type = Fingerprint::Type::SHA256; f.type = QCryptographicHash::Sha256;
f.data = f.data.fill('\x23', 32); f.data = f.data.fill('\x23', 32);
expectedString = QStringLiteral("v2:sha256:2323232323232323232323232323232323232323232323232323232323232323"); expectedString = QStringLiteral("v2:sha256:2323232323232323232323232323232323232323232323232323232323232323");
QCOMPARE(f.toDbLine(), expectedString); QCOMPARE(f.toDbLine(), expectedString);
@ -112,14 +112,14 @@ void FingerprintTests::test_fromDbLine()
QCOMPARE(actual, expected); QCOMPARE(actual, expected);
// Test V1 Only support Sha1 // Test V1 Only support Sha1
expected.type = Fingerprint::Type::SHA1; expected.type = QCryptographicHash::Sha1;
expected.data = expected.data =
QByteArray::fromRawData("\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23", 20); QByteArray::fromRawData("\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23", 20);
actual = Fingerprint::fromDbLine("23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23"); actual = Fingerprint::fromDbLine("23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23:23");
QCOMPARE(actual, expected); QCOMPARE(actual, expected);
// V1 does not support SHA256 // V1 does not support SHA256
expected.type = Fingerprint::Type::SHA256; expected.type = QCryptographicHash::Sha256;
expected.data = QByteArray::fromRawData( expected.data = QByteArray::fromRawData(
"\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23" "\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23"
"\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23", "\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23",
@ -131,7 +131,7 @@ void FingerprintTests::test_fromDbLine()
QCOMPARE_NE(actual, expected); QCOMPARE_NE(actual, expected);
// V2 SHA1 Test // V2 SHA1 Test
expected.type = Fingerprint::Type::SHA1; expected.type = QCryptographicHash::Sha1;
expected.data = expected.data =
QByteArray::fromRawData("\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23", 20); QByteArray::fromRawData("\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23", 20);
actual = Fingerprint::fromDbLine("v2:sha1:2323232323232323232323232323232323232323"); actual = Fingerprint::fromDbLine("v2:sha1:2323232323232323232323232323232323232323");
@ -142,7 +142,7 @@ void FingerprintTests::test_fromDbLine()
QCOMPARE_NE(actual, expected); QCOMPARE_NE(actual, expected);
// V2 SHA256 Test // V2 SHA256 Test
expected.type = Fingerprint::Type::SHA256; expected.type = QCryptographicHash::Sha256;
expected.data = QByteArray::fromRawData( expected.data = QByteArray::fromRawData(
"\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23" "\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23"
"\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23", "\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23\x23",
@ -160,29 +160,29 @@ void FingerprintTests::test_typeToString()
{ {
Fingerprint expected; Fingerprint expected;
expected.type = Fingerprint::Type::Invalid; expected.type = QCryptographicHash::Md4;
QCOMPARE(expected.type, Fingerprint::Type::Invalid); QCOMPARE(expected.type, QCryptographicHash::Md4);
QCOMPARE(Fingerprint::typeToString(expected.type), QStringLiteral("invalid")); QCOMPARE(Fingerprint::typeToString(expected.type), QStringLiteral("invalid"));
expected.type = Fingerprint::Type::SHA1; expected.type = QCryptographicHash::Sha1;
QCOMPARE(expected.type, Fingerprint::Type::SHA1); QCOMPARE(expected.type, QCryptographicHash::Sha1);
QCOMPARE(Fingerprint::typeToString(expected.type), QStringLiteral("sha1")); QCOMPARE(Fingerprint::typeToString(expected.type), QStringLiteral("sha1"));
expected.type = Fingerprint::Type::SHA256; expected.type = QCryptographicHash::Sha256;
QCOMPARE(expected.type, Fingerprint::Type::SHA256); QCOMPARE(expected.type, QCryptographicHash::Sha256);
QCOMPARE(Fingerprint::typeToString(expected.type), QStringLiteral("sha256")); QCOMPARE(Fingerprint::typeToString(expected.type), QStringLiteral("sha256"));
} }
void FingerprintTests::test_typeFromString() void FingerprintTests::test_typeFromString()
{ {
QCOMPARE(Fingerprint::Type::SHA1, Fingerprint::typeFromString("sha1")); QCOMPARE(QCryptographicHash::Sha1, Fingerprint::typeFromString("sha1"));
QCOMPARE(Fingerprint::Type::SHA1, Fingerprint::typeFromString("SHA1")); QCOMPARE(QCryptographicHash::Sha1, Fingerprint::typeFromString("SHA1"));
QCOMPARE(Fingerprint::Type::SHA256, Fingerprint::typeFromString("sha256")); QCOMPARE(QCryptographicHash::Sha256, Fingerprint::typeFromString("sha256"));
QCOMPARE(Fingerprint::Type::SHA256, Fingerprint::typeFromString("SHA256")); QCOMPARE(QCryptographicHash::Sha256, Fingerprint::typeFromString("SHA256"));
QCOMPARE(Fingerprint::Type::Invalid, Fingerprint::typeFromString("invalid")); QCOMPARE(QCryptographicHash::Md4, Fingerprint::typeFromString("invalid"));
QCOMPARE(Fingerprint::Type::Invalid, Fingerprint::typeFromString("")); QCOMPARE(QCryptographicHash::Md4, Fingerprint::typeFromString(""));
QCOMPARE(Fingerprint::Type::Invalid, Fingerprint::typeFromString("230p89jivon345")); QCOMPARE(QCryptographicHash::Md4, Fingerprint::typeFromString("230p89jivon345"));
} }
QTEST_MAIN(FingerprintTests) QTEST_MAIN(FingerprintTests)