refactor: new static TlsUtiliy::isCertValid

This commit is contained in:
sithlord48
2025-11-22 10:06:20 -05:00
committed by Nick Bolton
parent 4216acdb3a
commit 91b9680643
8 changed files with 188 additions and 2 deletions

View File

@ -147,8 +147,13 @@ MainWindow::MainWindow()
this, kAppName,
tr("Your current TLS key is smaller than the minimum allowed size, A new key 2048-bit key will be generated.")
);
regenerateLocalFingerprints();
Settings::setValue(Settings::Security::KeySize, 2048);
}
if (!TlsUtility::isCertValid()) {
regenerateLocalFingerprints();
return;
}
if (!QFile::exists(Settings::tlsLocalDb())) {
regenerateLocalFingerprints();
return;
@ -371,7 +376,8 @@ void MainWindow::settingsChanged(const QString &key)
if ((key == Settings::Security::Certificate) || (key == Settings::Security::KeySize) ||
(key == Settings::Security::TlsEnabled) || (key == Settings::Security::CheckPeers)) {
if (TlsUtility::isEnabled() && !QFile::exists(Settings::value(Settings::Security::Certificate).toString())) {
if (TlsUtility::isEnabled() && !TlsUtility::isCertValid()) {
qWarning() << tr("invalid certificate, generating a new one");
m_tlsUtility.generateCertificate();
}
updateSecurityIcon(m_lblSecurityStatus->isVisible());

View File

@ -1,5 +1,6 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
@ -8,7 +9,10 @@
#include "TlsCertificate.h"
#include "common/Settings.h"
#include <QFile>
#include <QSslCertificate>
#include <QSslKey>
#include <QString>
namespace deskflow::gui {
@ -23,6 +27,43 @@ bool TlsUtility::isEnabled()
return Settings::value(Settings::Security::TlsEnabled).toBool();
}
bool TlsUtility::isCertValid(const QString &certPath)
{
const auto certs = QSslCertificate::fromPath(certPath);
if (certs.isEmpty()) {
//: %1 will be replaced by the certificate path
qDebug() << tr("failed to read key from certificate file: %1").arg(certPath);
return false;
}
const auto cert = certs.first();
if (cert.isNull()) {
//: %1 will be replaced by the certificate path
qDebug() << tr("failed to parse certificate file: %1").arg(certPath);
return false;
}
const auto key = cert.publicKey();
if (key.isNull()) {
//: %1 will be replaced by the certificate path
qDebug() << tr("failed to read key from certificate file: %1").arg(certPath);
return false;
}
if (key.length() != Settings::value(Settings::Security::KeySize).toInt()) {
qDebug() << tr("key detected is the incorrect size");
return false;
}
if (const auto type = key.algorithm(); (type != QSsl::Dsa || type != QSsl::Rsa)) {
//: %1 will be replaced by the certificate path
qDebug() << tr("failed to read RSA or DSA key from certificate file: %1").arg(certPath);
return false;
}
return true;
}
bool TlsUtility::generateCertificate() const
{
qDebug(

View File

@ -1,5 +1,6 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
@ -7,6 +8,7 @@
#pragma once
#include "TlsCertificate.h"
#include <common/Settings.h>
#include <QObject>
@ -28,6 +30,13 @@ public:
*/
static bool isEnabled();
/**
* @brief isCertValid
* @param certPath the path of the file to check, when not set uses Settings::Security::Certificate value
* @return true if the certificate is valid
*/
static bool isCertValid(const QString &certPath = Settings::value(Settings::Security::Certificate).toString());
private:
TlsCertificate m_certificate;
};

View File

@ -455,6 +455,10 @@ Do you want to connect to the server?
<source>%1 is starting...</source>
<translation type="unfinished">%1 está iniciando...</translation>
</message>
<message>
<source>invalid certificate, generating a new one</source>
<translation type="unfinished">certificado no válido, generando uno nuevo</translation>
</message>
<message>
<source>%1 will retry in a moment...</source>
<translation type="unfinished">%1 lo intentará nuevamente en un momento...</translation>
@ -1284,6 +1288,28 @@ Al habilitar esta opción, se deshabilitará la interfaz gráfica de usuario (GU
<translation type="unfinished">La clave pública en el archivo de clave del certificado predeterminado es demasiado pequeña</translation>
</message>
</context>
<context>
<name>deskflow::gui::TlsUtility</name>
<message>
<source>failed to parse certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">Error al analizar el archivo de certificado: %1</translation>
</message>
<message>
<source>failed to read key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">No se pudo leer la clave del archivo de certificado: %1</translation>
</message>
<message>
<source>key detected is the incorrect size</source>
<translation type="unfinished">La clave detectada es de tamaño incorrecto</translation>
</message>
<message>
<source>failed to read RSA or DSA key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">No se pudo leer la clave RSA o DSA del archivo de certificado: %1</translation>
</message>
</context>
<context>
<name>i18n</name>
<message>

View File

@ -463,6 +463,10 @@ Nomi validi:
<source>%1 is starting...</source>
<translation>%1 si sta avviando...</translation>
</message>
<message>
<source>invalid certificate, generating a new one</source>
<translation type="unfinished">certificato non valido, ne viene generato uno nuovo</translation>
</message>
<message>
<source>%1 will retry in a moment...</source>
<translation>%1 riproverà tra un momento...</translation>
@ -1284,6 +1288,28 @@ L&apos;abilitazione di questa impostazione disabiliterà l&apos;interfaccia graf
<translation>La chiave pubblica nel file della chiave del certificato predefinito è troppo piccola</translation>
</message>
</context>
<context>
<name>deskflow::gui::TlsUtility</name>
<message>
<source>failed to parse certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">impossibile analizzare il file del certificato: %1</translation>
</message>
<message>
<source>failed to read key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">impossibile leggere la chiave dal file del certificato: %1</translation>
</message>
<message>
<source>key detected is the incorrect size</source>
<translation type="unfinished">la chiave rilevata ha una dimensione errata</translation>
</message>
<message>
<source>failed to read RSA or DSA key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">impossibile leggere la chiave RSA o DSA dal file del certificato: %1</translation>
</message>
</context>
<context>
<name>i18n</name>
<message>

View File

@ -307,6 +307,10 @@ Do you want to connect to the server?
<source>Your current TLS key is smaller than the minimum allowed size, A new key 2048-bit key will be generated.</source>
<translation>TLS鍵長が許容される長さより小さいため2048</translation>
</message>
<message>
<source>invalid certificate, generating a new one</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>A new version v%1 is available</source>
<translation>(v%1)</translation>
@ -1285,6 +1289,28 @@ Enabling this setting will disable the server config GUI.</source>
<translation></translation>
</message>
</context>
<context>
<name>deskflow::gui::TlsUtility</name>
<message>
<source>failed to parse certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">: %1</translation>
</message>
<message>
<source>failed to read key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">: %1</translation>
</message>
<message>
<source>key detected is the incorrect size</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>failed to read RSA or DSA key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished"> RSA DSA : %1</translation>
</message>
</context>
<context>
<name>i18n</name>
<message>

View File

@ -309,6 +309,10 @@ Do you want to connect to the server?
<source>Your current TLS key is smaller than the minimum allowed size, A new key 2048-bit key will be generated.</source>
<translation>Ваш нынешний TLS ключ меньше чем минимальный разрешимый размер , Новый ключ 2048-бит ключ будет сгенерирован.</translation>
</message>
<message>
<source>invalid certificate, generating a new one</source>
<translation type="unfinished">недействительный сертификат, генерация нового</translation>
</message>
<message>
<source>A new version v%1 is available</source>
<translation>Новая версия %1 доступна</translation>
@ -1289,6 +1293,28 @@ Enabling this setting will disable the server config GUI.</source>
<translation>Публичный ключ в стандартном файле сертификата слишком мал</translation>
</message>
</context>
<context>
<name>deskflow::gui::TlsUtility</name>
<message>
<source>failed to parse certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">не удалось проанализировать файл сертификата: %1</translation>
</message>
<message>
<source>failed to read key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">не удалось прочитать ключ из файла сертификата: %1</translation>
</message>
<message>
<source>key detected is the incorrect size</source>
<translation type="unfinished">обнаружен ключ неправильного размера</translation>
</message>
<message>
<source>failed to read RSA or DSA key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">не удалось прочитать ключ RSA или DSA из файла сертификата: %1</translation>
</message>
</context>
<context>
<name>i18n</name>
<message>

View File

@ -307,6 +307,10 @@ Do you want to connect to the server?
<source>Your current TLS key is smaller than the minimum allowed size, A new key 2048-bit key will be generated.</source>
<translation> TLS 2048 </translation>
</message>
<message>
<source>invalid certificate, generating a new one</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>A new version v%1 is available</source>
<translation> v%1 </translation>
@ -1285,6 +1289,28 @@ Enabling this setting will disable the server config GUI.</source>
<translation></translation>
</message>
</context>
<context>
<name>deskflow::gui::TlsUtility</name>
<message>
<source>failed to parse certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">%1</translation>
</message>
<message>
<source>failed to read key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished">%1</translation>
</message>
<message>
<source>key detected is the incorrect size</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>failed to read RSA or DSA key from certificate file: %1</source>
<extracomment>%1 will be replaced by the certificate path</extracomment>
<translation type="unfinished"> RSA DSA %1</translation>
</message>
</context>
<context>
<name>i18n</name>
<message>