From d16792e9430a9afcf59e0fcb0cc811606c129a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 17 Aug 2021 09:23:36 +0200 Subject: [PATCH 1/2] Prevent unspecific warnings during certificate loading --- libnymea-core/servermanager.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libnymea-core/servermanager.cpp b/libnymea-core/servermanager.cpp index f79e913c..45a57bd4 100644 --- a/libnymea-core/servermanager.cpp +++ b/libnymea-core/servermanager.cpp @@ -367,19 +367,31 @@ void ServerManager::unregisterZeroConfService(const QString &configId, const QSt bool ServerManager::loadCertificate(const QString &certificateKeyFileName, const QString &certificateFileName) { QFile certificateKeyFile(certificateKeyFileName); + if (!certificateKeyFile.exists() || certificateKeyFileName.isEmpty()) { + qCWarning(dcServerManager()) << "Could not load certificate key file" << certificateKeyFile.fileName() << "because the file does not exist."; + return false; + } + if (!certificateKeyFile.open(QIODevice::ReadOnly)) { qCWarning(dcServerManager()) << "Could not open" << certificateKeyFile.fileName() << ":" << certificateKeyFile.errorString(); return false; } + m_certificateKey = QSslKey(certificateKeyFile.readAll(), QSsl::Rsa); qCDebug(dcServerManager()) << "Loaded private certificate key " << certificateKeyFileName; certificateKeyFile.close(); QFile certificateFile(certificateFileName); + if (!certificateFile.exists() || certificateFileName.isEmpty()) { + qCWarning(dcServerManager()) << "Could not load certificate file" << certificateFile.fileName() << "because the file does not exist."; + return false; + } + if (!certificateFile.open(QIODevice::ReadOnly)) { qCWarning(dcServerManager()) << "Could not open" << certificateFile.fileName() << ":" << certificateFile.errorString(); return false; } + m_certificate = QSslCertificate(certificateFile.readAll()); qCDebug(dcServerManager()) << "Loaded certificate file " << certificateFileName; certificateFile.close(); From a5c439008dc32253df074739a8ca3b5ff6b9f6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 17 Aug 2021 09:31:34 +0200 Subject: [PATCH 2/2] Update certificate loading mechanism --- libnymea-core/servermanager.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libnymea-core/servermanager.cpp b/libnymea-core/servermanager.cpp index 45a57bd4..dabdc32c 100644 --- a/libnymea-core/servermanager.cpp +++ b/libnymea-core/servermanager.cpp @@ -83,12 +83,12 @@ ServerManager::ServerManager(Platform *platform, NymeaConfiguration *configurati QString fallbackKeyFileName = NymeaSettings::storagePath() + "/certs/nymead-certificate.key"; bool certsLoaded = false; - if (loadCertificate(configKeyFileName, configCertificateFileName)) { - qCDebug(dcServerManager()) << "Using SSL certificate:" << configKeyFileName; + if (!configKeyFileName.isEmpty() && !configCertificateFileName.isEmpty() && loadCertificate(configKeyFileName, configCertificateFileName)) { + qCDebug(dcServerManager()) << "Using SSL certificate:" << configCertificateFileName; certsLoaded = true; - } else if (loadCertificate(fallbackKeyFileName, fallbackCertificateFileName)) { + } else if (!fallbackKeyFileName.isEmpty() && !fallbackCertificateFileName.isEmpty() && loadCertificate(fallbackKeyFileName, fallbackCertificateFileName)) { certsLoaded = true; - qCWarning(dcServerManager()) << "Using fallback self-signed SSL certificate:" << fallbackCertificateFileName; + qCDebug(dcServerManager()) << "Using fallback self-signed SSL certificate:" << fallbackCertificateFileName; } else { qCDebug(dcServerManager()) << "Generating self signed certificates..."; CertificateGenerator::generate(fallbackCertificateFileName, fallbackKeyFileName); @@ -367,7 +367,7 @@ void ServerManager::unregisterZeroConfService(const QString &configId, const QSt bool ServerManager::loadCertificate(const QString &certificateKeyFileName, const QString &certificateFileName) { QFile certificateKeyFile(certificateKeyFileName); - if (!certificateKeyFile.exists() || certificateKeyFileName.isEmpty()) { + if (!certificateKeyFile.exists()) { qCWarning(dcServerManager()) << "Could not load certificate key file" << certificateKeyFile.fileName() << "because the file does not exist."; return false; } @@ -382,7 +382,7 @@ bool ServerManager::loadCertificate(const QString &certificateKeyFileName, const certificateKeyFile.close(); QFile certificateFile(certificateFileName); - if (!certificateFile.exists() || certificateFileName.isEmpty()) { + if (!certificateFile.exists()) { qCWarning(dcServerManager()) << "Could not load certificate file" << certificateFile.fileName() << "because the file does not exist."; return false; }