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()) {
generateCertificate();
} 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 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;
if ((isClient && m_checkedServers.contains(sha256Text)) || (!isClient && m_checkedClients.contains(sha256Text))) {
@ -1177,7 +1177,7 @@ bool MainWindow::generateCertificate()
return false;
}
m_fingerprint = {Fingerprint::Type::SHA256, TlsUtility::certFingerprint()};
m_fingerprint = {QCryptographicHash::Sha256, TlsUtility::certFingerprint()};
updateLocalFingerprint();
return true;

View File

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

View File

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

View File

@ -12,12 +12,9 @@
bool Fingerprint::isValid() const
{
switch (type) {
using enum Type;
case Invalid:
return false;
case SHA1:
case QCryptographicHash::Sha1:
return data.length() == 20;
case SHA256:
case QCryptographicHash::Sha256:
return data.length() == 32;
default:
return false;
@ -54,7 +51,7 @@ Fingerprint Fingerprint::fromDbLine(const QString &line)
const bool wrongSize = line.size() != kSha1ExpectedSize;
if (bool badColonCount = line.count(':') != kSha1ColonCount; wrongSize || badColonCount)
return result;
result.type = Fingerprint::Type::SHA1;
result.type = QCryptographicHash::Sha1;
auto l2 = line;
result.data = QByteArray::fromHex(l2.remove(':').toLatin1());
}
@ -62,25 +59,25 @@ Fingerprint Fingerprint::fromDbLine(const QString &line)
return result;
}
Fingerprint::Type Fingerprint::typeFromString(const QString &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)
QString Fingerprint::typeToString(QCryptographicHash::Algorithm type)
{
switch (type) {
case Type::SHA1:
case QCryptographicHash::Sha1:
return m_type_sha1;
case Type::SHA256:
case QCryptographicHash::Sha256:
return m_type_sha256;
default:
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
#include <QByteArray>
#include <QCryptographicHash>
#include <QObject>
struct Fingerprint
@ -17,14 +18,9 @@ struct Fingerprint
inline static QString m_type_invalid = QStringLiteral("invalid");
public:
enum class Type
{
Invalid,
SHA1,
SHA256
};
Q_ENUM(Type)
Type type = Type::Invalid;
// 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
QCryptographicHash::Algorithm type = QCryptographicHash::Md4;
QByteArray data;
bool isValid() const;
@ -32,6 +28,6 @@ public:
bool operator==(const Fingerprint &other) const = default;
QString toDbLine() const;
static Fingerprint fromDbLine(const QString &line);
static QString typeToString(Fingerprint::Type type);
static Fingerprint::Type typeFromString(const QString &type);
static QString typeToString(QCryptographicHash::Algorithm type);
static QCryptographicHash::Algorithm typeFromString(const QString &type);
};

View File

@ -628,7 +628,7 @@ void SecureSocket::disconnect()
bool SecureSocket::verifyCertFingerprint(const QString &FingerprintDatabasePath) const
{
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)
X509_free(cert);

View File

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

View File

@ -24,9 +24,9 @@ QString formatSSLFingerprint(const QByteArray &fingerprint, bool enableSeparator
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);

View File

@ -10,8 +10,6 @@
#include "net/Fingerprint.h"
#include "net/FingerprintDatabase.h"
#include <sstream>
void FingerprintDatabaseTests::readFile()
{
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
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)}
};
@ -41,10 +39,10 @@ void FingerprintDatabaseTests::writeFile()
FingerprintDatabase db;
db.addTrusted(
{Fingerprint::Type::SHA1, QByteArray::fromHex(QString("ABCDEF0001020304050607080910111213141516").toLatin1())}
{QCryptographicHash::Sha1, QByteArray::fromHex(QString("ABCDEF0001020304050607080910111213141516").toLatin1())}
);
db.addTrusted(
{Fingerprint::Type::SHA1, QByteArray::fromHex(QString("0001020304050607080910111213141516ABCDEF").toLatin1())}
{QCryptographicHash::Sha1, QByteArray::fromHex(QString("0001020304050607080910111213141516ABCDEF").toLatin1())}
);
db.writeStream(stream);
@ -56,7 +54,7 @@ v2:sha1:0001020304050607080910111213141516abcdef
void FingerprintDatabaseTests::clear()
{
FingerprintDatabase db;
db.addTrusted({Fingerprint::Type::SHA1, QByteArray::fromHex(QString("01020304ab").toLatin1())});
db.addTrusted({QCryptographicHash::Sha1, QByteArray::fromHex(QString("01020304ab").toLatin1())});
db.clear();
QVERIFY(db.fingerprints().empty());
@ -64,9 +62,9 @@ void FingerprintDatabaseTests::clear()
void FingerprintDatabaseTests::trusted()
{
Fingerprint trusted1 = {Fingerprint::Type::SHA1, QByteArray::fromHex(QString("01020304ab").toLatin1())};
Fingerprint trusted2 = {Fingerprint::Type::SHA1, QByteArray::fromHex(QString("03040506ab").toLatin1())};
Fingerprint untrusted = {Fingerprint::Type::SHA1, QByteArray::fromHex(QString("01020304ac").toLatin1())};
Fingerprint trusted1 = {QCryptographicHash::Sha1, QByteArray::fromHex(QString("01020304ab").toLatin1())};
Fingerprint trusted2 = {QCryptographicHash::Sha1, QByteArray::fromHex(QString("03040506ab").toLatin1())};
Fingerprint untrusted = {QCryptographicHash::Sha1, QByteArray::fromHex(QString("01020304ac").toLatin1())};
FingerprintDatabase db;

View File

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