refactor: TlsUtility a collection of methods in a namespace

This commit is contained in:
sithlord48 2025-11-22 12:47:33 -05:00 committed by Nick Bolton
parent bd4db268f1
commit 5736566a79
7 changed files with 137 additions and 168 deletions

View file

@ -15,82 +15,77 @@
#include <QSslKey>
#include <QString>
namespace deskflow::gui {
namespace deskflow::gui::TlsUtility {
TlsUtility::TlsUtility(QObject *parent) : QObject(parent)
{
// do nothing
}
bool TlsUtility::isEnabled()
bool isEnabled()
{
return Settings::value(Settings::Security::TlsEnabled).toBool();
}
bool TlsUtility::isCertValid(const QString &certPath)
bool 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);
qDebug() << QObject::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);
qDebug() << QObject::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);
qDebug() << QObject::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");
qDebug() << QObject::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);
qDebug() << QObject::tr("failed to read RSA or DSA key from certificate file: %1").arg(certPath);
return false;
}
return true;
}
int TlsUtility::getCertKeyLength(const QString &certPath)
int getCertKeyLength(const QString &certPath)
{
return deskflow::getCertLength(certPath.toStdString());
}
QByteArray TlsUtility::certFingerprint(const QString &certPath)
QByteArray certFingerprint(const QString &certPath)
{
QByteArray fingerprint;
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);
qDebug() << QObject::tr("failed to read key from certificate file: %1").arg(certPath);
return fingerprint;
}
const auto cert = certs.first();
if (cert.isNull()) {
//: %1 will be replaced by the certificate path
qWarning() << tr("failed to parse certificate file: %1").arg(certPath);
qWarning() << QObject::tr("failed to parse certificate file: %1").arg(certPath);
return fingerprint;
}
return cert.digest(QCryptographicHash::Sha256);
}
bool TlsUtility::generateCertificate()
bool generateCertificate()
{
qDebug(
"generating tls certificate, "
@ -116,4 +111,4 @@ bool TlsUtility::generateCertificate()
return true;
}
} // namespace deskflow::gui
} // namespace deskflow::gui::TlsUtility

View file

@ -9,47 +9,36 @@
#include <common/Settings.h>
#include <QObject>
namespace deskflow::gui::TlsUtility {
namespace deskflow::gui {
bool generateCertificate();
class TlsUtility : public QObject
{
Q_OBJECT
/**
* @brief Checks the settings values Settings::Security::TlsEnabled
* @return true when tls is enabled
*/
bool isEnabled();
public:
explicit TlsUtility(QObject *parent = nullptr);
/**
* @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
*/
bool isCertValid(const QString &certPath = Settings::value(Settings::Security::Certificate).toString());
static bool generateCertificate();
/**
* @brief Get the lenght of a key
* @param certPath path of the file to check, when unset will use the value of Settings::Security::Certificate
* @return the bitsize of the key
*/
int getCertKeyLength(const QString &certPath = Settings::value(Settings::Security::Certificate).toString());
/**
* @brief Checks the settings values Settings::Security::TlsEnabled
* @return true when tls is enabled
*/
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());
/**
* @brief Get the lenght of a key
* @param certPath path of the file to check, when unset will use the value of Settings::Security::Certificate
* @return the bitsize of the key
*/
static int getCertKeyLength(const QString &certPath = Settings::value(Settings::Security::Certificate).toString());
/**
* @brief get the SHA256 fingerprint of a certificatefile.
* @param certPath path of the file to fingerprint, when not set uses the vaule of Settings::Security::Certificate
* @return A QByteArray of the SHA256 fingerprint of the file or empty array if invalid file is passed
*/
// clang-format off
static QByteArray certFingerprint(const QString &certPath = Settings::value(Settings::Security::Certificate).toString());
// clang-format on
};
} // namespace deskflow::gui
/**
* @brief get the SHA256 fingerprint of a certificatefile.
* @param certPath path of the file to fingerprint, when not set uses the vaule of Settings::Security::Certificate
* @return A QByteArray of the SHA256 fingerprint of the file or empty array if invalid file is passed
*/
// clang-format off
QByteArray certFingerprint(const QString &certPath = Settings::value(Settings::Security::Certificate).toString());
// clang-format on
} // namespace deskflow::gui::TlsUtility

View file

@ -711,6 +711,25 @@ Nombres válidos:
<source>&lt;p&gt;There was a problem finding the %1 background service (daemon).&lt;/p&gt;&lt;p&gt;The background service makes %1 work with UAC prompts and the login screen.&lt;/p&gt;&lt;p&gt;If don&apos;t want to use the background service and intentionally stopped it, you can prevent it&apos;s use by disabling this feature.&lt;/p&gt;&lt;p&gt;If you did not stop the background service intentionally, there may be a problem with it. Please retry or try restarting the %1 service from the Windows services program.&lt;/p&gt;</source>
<translation type="unfinished">&lt;p&gt;Hubo un problema al encontrar el servicio en segundo plano %1 (daemon).&lt;/p&gt;&lt;p&gt;El servicio en segundo plano hace que %1 funcione con los avisos de UAC y la pantalla de inicio de sesión.&lt;/p&gt;&lt;p&gt;Si no desea usar el servicio en segundo plano y lo detuvo intencionalmente, puede evitar su uso deshabilitando esta función.&lt;/p&gt;&lt;p&gt;Si no detuvo el servicio en segundo plano intencionalmente, es posible que haya un problema. Vuelva a intentarlo o reinicie el servicio %1 desde el programa de servicios de Windows.&lt;/p&gt;</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>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>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>ScreenSettingsDialog</name>
@ -1265,28 +1284,6 @@ Al habilitar esta opción, se deshabilitará la interfaz gráfica de usuario (GU
<translation type="unfinished">&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Requiere el paquete wl-clipboard&lt;/p&gt;&lt;p&gt;Al usar wl-clipboard v2.2.1, existe un error que provoca la pérdida del foco y que puede dificultar el uso de Deskflow. Este error se ha corregido al usar la rama principal de wl-clipboard, a menos que su Compositor no sea compatible con el protocolo wlroots-data-control.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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

@ -711,6 +711,25 @@ Nomi validi:
<source>&lt;p&gt;There was a problem finding the %1 background service (daemon).&lt;/p&gt;&lt;p&gt;The background service makes %1 work with UAC prompts and the login screen.&lt;/p&gt;&lt;p&gt;If don&apos;t want to use the background service and intentionally stopped it, you can prevent it&apos;s use by disabling this feature.&lt;/p&gt;&lt;p&gt;If you did not stop the background service intentionally, there may be a problem with it. Please retry or try restarting the %1 service from the Windows services program.&lt;/p&gt;</source>
<translation>&lt;p&gt;Si è verificato un problema durante la ricerca del servizio in background %1 (daemon).&lt;/p&gt;&lt;p&gt;Il servizio in background consente a %1 di funzionare con i prompt UAC e la schermata di accesso.&lt;/p&gt;&lt;p&gt;Se non desideri utilizzare il servizio in background e lo hai interrotto intenzionalmente, puoi impedirne l&apos;uso disabilitando questa funzionalità.&lt;/p&gt;&lt;p&gt;Se non hai interrotto intenzionalmente il servizio in background, potrebbe esserci un problema. Riprova o prova a riavviare il servizio %1 dal programma dei servizi di Windows.&lt;/p&gt;</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>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>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>ScreenSettingsDialog</name>
@ -1265,28 +1284,6 @@ L&apos;abilitazione di questa impostazione disabiliterà l&apos;interfaccia graf
<translation>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Richiede il pacchetto wl-clipboard&lt;/p&gt;&lt;p&gt;Quando si utilizza wl-clipboard v2.2.1, si verifica un bug di furto del focus che potrebbe rendere Deskflow più difficile da usare. Questo problema è stato risolto quando si utilizza il ramo master di wl-clipboard, a meno che il proprio compositore non supporti il ​​protocollo wlroots-data-control.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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

@ -712,6 +712,25 @@ Valid names:
<source>&lt;p&gt;There was a problem finding the %1 background service (daemon).&lt;/p&gt;&lt;p&gt;The background service makes %1 work with UAC prompts and the login screen.&lt;/p&gt;&lt;p&gt;If don&apos;t want to use the background service and intentionally stopped it, you can prevent it&apos;s use by disabling this feature.&lt;/p&gt;&lt;p&gt;If you did not stop the background service intentionally, there may be a problem with it. Please retry or try restarting the %1 service from the Windows services program.&lt;/p&gt;</source>
<translation>&lt;p&gt;%1 バックグラウンドサービス(デーモン)が検出できませんでした。&lt;/p&gt;&lt;p&gt;バックグラウンドサービスは %1 がUACプロンプトとログイン画面で動作するために必要です。&lt;/p&gt;&lt;p&gt;もしバックグラウンドサービスが不要で意図的に停止したのであれば、この機能を無効化することで利用を止めることができます。&lt;/p&gt;&lt;p&gt;もし意図的に止めたのでなければ、何か問題があるかもしれません。再試行するか、%1 サービスをWindowsのサービス設定から再起動してみてください。&lt;/p&gt;</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>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>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>ScreenSettingsDialog</name>
@ -1266,28 +1285,6 @@ Enabling this setting will disable the server config GUI.</source>
<translation>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;wl-clipboard パッケージが必要です。&lt;/p&gt;&lt;p&gt;wl-clipboard v2.2.1 を使用すると、フォーカス盗用のバグにより Deskflow の使い勝手が悪くなる可能性があります。この問題は wl-clipboard のマスターブランチで修正されていますが、使用しているコンポジターが wlroots-data-control プロトコルに対応している必要があります。&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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

@ -714,6 +714,25 @@ Valid names:
<source>&lt;p&gt;There was a problem finding the %1 background service (daemon).&lt;/p&gt;&lt;p&gt;The background service makes %1 work with UAC prompts and the login screen.&lt;/p&gt;&lt;p&gt;If don&apos;t want to use the background service and intentionally stopped it, you can prevent it&apos;s use by disabling this feature.&lt;/p&gt;&lt;p&gt;If you did not stop the background service intentionally, there may be a problem with it. Please retry or try restarting the %1 service from the Windows services program.&lt;/p&gt;</source>
<translation>&lt;p&gt;Возникла проблема нахождения %1 фоновым процессом (daemon).&lt;/p&gt;&lt;p&gt;Сервис %1 работает с UAC промтом и экраном входа.&lt;/p&gt;&lt;p&gt;Если Вы не хотите использовать сервис в фоновом режиме и намеренно останавливать его, Вы можете предотвратить его использование после отключения опции.&lt;/p&gt;&lt;p&gt;Если Вы не останавливали сервис намеренно, проблема может заключаться нем. Попробуйте перезапустить сервис %1 из программы Windows services.&lt;/p&gt;</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>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>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>ScreenSettingsDialog</name>
@ -1270,28 +1289,6 @@ Enabling this setting will disable the server config GUI.</source>
<translation>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Для этого необходим пакет wl-clipboard.&lt;/p&gt;&lt;p&gt;Когда ты используешь wl-clipboard v2.2.1. Возникает ошибка перехвата фокуса, которая мешает использовать deskflowю. Это фиксется если использовать wl-clipboard из ветки master, если только ваш Compositor не поддерживает протокол wlroots-data-control.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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

@ -712,6 +712,25 @@ Valid names:
<source>&lt;p&gt;There was a problem finding the %1 background service (daemon).&lt;/p&gt;&lt;p&gt;The background service makes %1 work with UAC prompts and the login screen.&lt;/p&gt;&lt;p&gt;If don&apos;t want to use the background service and intentionally stopped it, you can prevent it&apos;s use by disabling this feature.&lt;/p&gt;&lt;p&gt;If you did not stop the background service intentionally, there may be a problem with it. Please retry or try restarting the %1 service from the Windows services program.&lt;/p&gt;</source>
<translation type="unfinished">&lt;p&gt;查找 %1 后台服务(守护进程)时出现问题。&lt;/p&gt;&lt;p&gt;%1 需后台服务来与 UAC 提示和登录屏幕配合工作。&lt;/p&gt;&lt;p&gt;如果您不想使用后台服务并已将其停止,可以通过禁用此功能来阻止使用它。&lt;/p&gt;&lt;p&gt;如果您并非有意停止后台服务,则可能存在问题。请重试,或尝试从 Windows 服务程序中重启 %1 服务。&lt;/p&gt;</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>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>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>ScreenSettingsDialog</name>
@ -1266,28 +1285,6 @@ Enabling this setting will disable the server config GUI.</source>
<translation>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;需要 wl-clipboard 包&lt;/p&gt;&lt;p&gt;使用 wl-clipboard v2.2.1 时存在一个焦点抢夺 Bug,可能导致 Deskflow 使用不便。该问题已在 wl-clipboard 的 master 分支中修复,除非您的合成器不支持 wlroots-data-control 协议。&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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>