74 lines
2.1 KiB
C++
74 lines
2.1 KiB
C++
/*
|
|
* Deskflow -- mouse and keyboard sharing utility
|
|
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
|
* SPDX-FileCopyrightText: (C) 2015 Symless Ltd.
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
|
*/
|
|
|
|
#include "TlsCertificate.h"
|
|
|
|
#include "common/constants.h"
|
|
#include "gui/core/CoreTool.h"
|
|
#include "net/FingerprintData.h"
|
|
#include "net/FingerprintDatabase.h"
|
|
#include "net/SecureUtils.h"
|
|
|
|
#include <QCoreApplication>
|
|
#include <QDir>
|
|
#include <QProcess>
|
|
|
|
TlsCertificate::TlsCertificate(QObject *parent) : QObject(parent)
|
|
{
|
|
}
|
|
|
|
bool TlsCertificate::generateCertificate(const QString &path, int keyLength)
|
|
{
|
|
qDebug("generating tls certificate: %s", qUtf8Printable(path));
|
|
|
|
QFileInfo info(path);
|
|
QDir dir(info.absolutePath());
|
|
if (!dir.exists() && !dir.mkpath(".")) {
|
|
qCritical("failed to create directory for tls certificate");
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
deskflow::generatePemSelfSignedCert(path.toStdString(), keyLength);
|
|
} catch (const std::exception &e) {
|
|
qCritical() << "failed to generate self-signed pem cert: " << e.what();
|
|
return false;
|
|
}
|
|
qDebug("tls certificate generated");
|
|
return generateFingerprint(path);
|
|
}
|
|
|
|
bool TlsCertificate::generateFingerprint(const QString &certificateFilename)
|
|
{
|
|
qDebug("generating tls fingerprint");
|
|
try {
|
|
auto fingerprint =
|
|
deskflow::pemFileCertFingerprint(certificateFilename.toStdString(), deskflow::FingerprintType::SHA1);
|
|
|
|
CoreTool coreTool;
|
|
QString profileDir = coreTool.getProfileDir();
|
|
|
|
auto localPath = QStringLiteral("%1/%2/%3").arg(profileDir, kSslDir, kFingerprintLocalFilename).toStdString();
|
|
|
|
const deskflow::FingerprintData data{"sha1", fingerprint};
|
|
|
|
deskflow::FingerprintDatabase db;
|
|
db.addTrusted(data);
|
|
db.write(localPath);
|
|
|
|
qDebug("tls fingerprint generated");
|
|
return true;
|
|
} catch (const std::exception &e) {
|
|
qCritical() << "failed to find tls fingerprint: " << e.what();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
int TlsCertificate::getCertKeyLength(const QString &path)
|
|
{
|
|
return deskflow::getCertLength(path.toStdString());
|
|
}
|