feat!: Add support for FingerPrintDatabase

This commit is contained in:
Vamshi Maskuri
2025-01-28 23:13:46 +05:30
committed by Nick Bolton
parent 7d79a4d729
commit aaa64e986e
5 changed files with 266 additions and 1 deletions

View File

@ -5,6 +5,10 @@
add_library(net STATIC
FingerprintTypes.h
FingerprintData.cpp
FingerprintData.h
FingerprintDatabase.cpp
FingerprintDatabase.h
IDataSocket.cpp
IDataSocket.h
IListenSocket.h
@ -57,5 +61,6 @@ if(WIN32)
target_link_libraries(
net
PUBLIC OpenSSL::applink
PRIVATE Crypt32 ws2_32 OpenSSL::applink)
PRIVATE Crypt32 ws2_32 OpenSSL::applink
)
endif()

View File

@ -0,0 +1,48 @@
/*
* 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 "FingerprintDatabase.h"
#include "io/filesystem.h"
#include <algorithm>
#include <fstream>
namespace deskflow {
bool FingerprintData::operator==(const FingerprintData &other) const
{
return algorithm == other.algorithm && data == other.data;
}
const char *fingerprintTypeToString(FingerprintType type)
{
switch (type) {
case FingerprintType::Invalid:
return "invalid";
case FingerprintType::SHA1:
return "sha1";
case FingerprintType::SHA256:
return "sha256";
default:
break;
}
return "invalid";
}
FingerprintType fingerprintTypeFromString(const std::string &type)
{
if (type == "sha1")
return FingerprintType::SHA1;
if (type == "sha256")
return FingerprintType::SHA256;
return FingerprintType::Invalid;
}
} // namespace deskflow

View File

@ -0,0 +1,34 @@
/*
* 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 "FingerprintTypes.h"
#include <cstdint>
#include <string>
#include <vector>
namespace deskflow {
struct FingerprintData
{
std::string algorithm;
std::vector<std::uint8_t> data;
bool valid() const
{
return !algorithm.empty();
}
bool operator==(const FingerprintData &other) const;
};
const char *fingerprintTypeToString(FingerprintType type);
FingerprintType fingerprintTypeFromString(const std::string &type);
} // namespace deskflow

View File

@ -0,0 +1,133 @@
/*
* 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 "FingerprintDatabase.h"
#include "base/String.h"
#include "io/filesystem.h"
#include <algorithm>
#include <fstream>
namespace deskflow {
void FingerprintDatabase::read(const fs::path &path)
{
std::ifstream file;
openUtf8Path(file, path, std::ios_base::in);
readStream(file);
}
void FingerprintDatabase::write(const fs::path &path)
{
std::ofstream file;
openUtf8Path(file, path, std::ios_base::out);
writeStream(file);
}
void FingerprintDatabase::readStream(std::istream &stream)
{
if (!stream.good()) {
return;
}
std::string line;
while (std::getline(stream, line)) {
if (line.empty()) {
continue;
}
auto fingerprint = parseDbLine(line);
if (!fingerprint.valid()) {
continue;
}
m_fingerprints.push_back(fingerprint);
}
}
void FingerprintDatabase::writeStream(std::ostream &stream)
{
if (!stream.good()) {
return;
}
for (const auto &fingerprint : m_fingerprints) {
stream << toDbLine(fingerprint) << "\n";
}
}
void FingerprintDatabase::clear()
{
m_fingerprints.clear();
}
void FingerprintDatabase::addTrusted(const FingerprintData &fingerprint)
{
if (isTrusted(fingerprint)) {
return;
}
m_fingerprints.push_back(fingerprint);
}
bool FingerprintDatabase::isTrusted(const FingerprintData &fingerprint)
{
auto found = std::find(m_fingerprints.begin(), m_fingerprints.end(), fingerprint);
return found != m_fingerprints.end();
}
FingerprintData FingerprintDatabase::parseDbLine(const std::string &line)
{
const auto kSha1ColonCount = 19;
const auto kSha1HexCharCount = 40;
const auto kSha1ExpectedSize = kSha1HexCharCount + kSha1ColonCount;
FingerprintData result;
// legacy v1 certificate handling
if (std::count(line.begin(), line.end(), ':') == kSha1ColonCount && line.size() == kSha1ExpectedSize) {
auto data = string::fromHex(line);
if (data.empty()) {
return result;
}
result.algorithm = fingerprintTypeToString(FingerprintType::SHA1);
result.data = data;
return result;
}
auto versionEndPos = line.find(':');
if (versionEndPos == std::string::npos) {
return result;
}
if (line.substr(0, versionEndPos) != "v2") {
return result;
}
auto algoStartPos = versionEndPos + 1;
auto algoEndPos = line.find(':', algoStartPos);
if (algoEndPos == std::string::npos) {
return result;
}
auto algorithm = line.substr(algoStartPos, algoEndPos - algoStartPos);
auto data = string::fromHex(line.substr(algoEndPos + 1));
if (data.empty()) {
return result;
}
result.algorithm = algorithm;
result.data = data;
return result;
}
std::string FingerprintDatabase::toDbLine(const FingerprintData &fingerprint)
{
std::string fingerprintStr(fingerprint.data.begin(), fingerprint.data.end());
return "v2:" + fingerprint.algorithm + ":" + string::toHex(fingerprintStr, 2);
}
} // namespace deskflow

View File

@ -0,0 +1,45 @@
/*
* 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 "FingerprintData.h"
#include "io/filesystem.h"
#include <iosfwd>
#include <string>
#include <vector>
namespace deskflow {
class FingerprintDatabase
{
public:
void read(const fs::path &path);
void write(const fs::path &path);
void readStream(std::istream &stream);
void writeStream(std::ostream &stream);
void clear();
void addTrusted(const FingerprintData &fingerprint);
bool isTrusted(const FingerprintData &fingerprint);
const std::vector<FingerprintData> &fingerprints() const
{
return m_fingerprints;
}
static FingerprintData parseDbLine(const std::string &line);
static std::string toDbLine(const FingerprintData &fingerprint);
private:
std::vector<FingerprintData> m_fingerprints;
};
} // namespace deskflow