diff --git a/src/base/http/server.cpp b/src/base/http/server.cpp index dd5759444..813c6a6d6 100644 --- a/src/base/http/server.cpp +++ b/src/base/http/server.cpp @@ -61,11 +61,27 @@ Server::~Server() } #ifndef QT_NO_OPENSSL -void Server::enableHttps(const QList &certificates, const QSslKey &key) +bool Server::setupHttps(const QByteArray &certificates, const QByteArray &key) { - m_certificates = certificates; - m_key = key; - m_https = true; + QSslKey sslKey(key, QSsl::Rsa); + if (sslKey.isNull()) + sslKey = QSslKey(key, QSsl::Ec); + + const QList certs = QSslCertificate::fromData(certificates); + const bool areCertsValid = !certs.empty() && std::all_of(certs.begin(), certs.end(), [](const QSslCertificate &c) { return !c.isNull(); }); + + if (!sslKey.isNull() && areCertsValid) + { + m_key = sslKey; + m_certificates = certs; + m_https = true; + return true; + } + else + { + disableHttps(); + return false; + } } void Server::disableHttps() diff --git a/src/base/http/server.h b/src/base/http/server.h index 7da8b5775..6fd5fc131 100644 --- a/src/base/http/server.h +++ b/src/base/http/server.h @@ -55,7 +55,7 @@ namespace Http ~Server(); #ifndef QT_NO_OPENSSL - void enableHttps(const QList &certificates, const QSslKey &key); + bool setupHttps(const QByteArray &certificates, const QByteArray &key); void disableHttps(); #endif diff --git a/src/webui/webui.cpp b/src/webui/webui.cpp index 564002ecf..903cb0418 100644 --- a/src/webui/webui.cpp +++ b/src/webui/webui.cpp @@ -74,14 +74,13 @@ void WebUI::init() #ifndef QT_NO_OPENSSL if (pref->isWebUiHttpsEnabled()) { - QList certs = QSslCertificate::fromData(pref->getWebUiHttpsCertificate()); - QSslKey key; - key = QSslKey(pref->getWebUiHttpsKey(), QSsl::Rsa); - bool certsIsNull = std::any_of(certs.begin(), certs.end(), [](QSslCertificate c) { return c.isNull(); }); - if (!certsIsNull && !certs.empty() && !key.isNull()) - m_httpServer->enableHttps(certs, key); + const QByteArray certs = pref->getWebUiHttpsCertificate(); + const QByteArray key = pref->getWebUiHttpsKey(); + bool success = m_httpServer->setupHttps(certs, key); + if (success) + logger->addMessage(tr("Web UI: https setup successful")); else - m_httpServer->disableHttps(); + logger->addMessage(tr("Web UI: https setup failed, fallback to http"), Log::CRITICAL); } else { m_httpServer->disableHttps();