10 #define LOC QString("SSL: ")
15 if (!QSslSocket::supportsSsl())
18 LOG(VB_HTTP, LOG_INFO,
LOC + QSslSocket::sslLibraryVersionString());
20 Config = QSslConfiguration::defaultConfiguration();
21 Config.setProtocol(QSsl::SecureProtocols);
22 Config.setSslOption(QSsl::SslOptionDisableLegacyRenegotiation,
true);
23 Config.setSslOption(QSsl::SslOptionDisableCompression,
true);
25 auto availableCiphers = QSslConfiguration::supportedCiphers();
26 QList<QSslCipher> secureCiphers;
27 for (
const auto & cipher : std::as_const(availableCiphers))
30 if (cipher.usedBits() < 128)
33 if (cipher.name().startsWith(
"RC4") ||
34 cipher.name().startsWith(
"EXP") ||
35 cipher.name().startsWith(
"ADH") ||
36 cipher.name().contains(
"NULL"))
39 secureCiphers.append(cipher);
41 Config.setCiphers(secureCiphers);
45 while (configdir.endsWith(
"/"))
47 configdir.append(QStringLiteral(
"/certificates/"));
50 if (hostKeyPath.isEmpty())
51 hostKeyPath = configdir +
"key.pem";
53 QFile hostKeyFile(hostKeyPath);
54 if (!hostKeyFile.exists() || !hostKeyFile.open(QIODevice::ReadOnly))
56 LOG(VB_GENERAL, LOG_WARNING,
LOC +
57 QString(
"SSL Host key file (%1) does not exist or is not readable").arg(hostKeyPath));
61 auto rawHostKey = hostKeyFile.readAll();
62 auto hostKey = QSslKey(rawHostKey, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
63 if (!hostKey.isNull())
65 Config.setPrivateKey(hostKey);
69 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Unable to load host key from file (%1)").arg(hostKeyPath));
74 if (hostCertPath.isEmpty())
75 hostCertPath = configdir +
"cert.pem";
77 QSslCertificate hostCert;
78 auto certList = QSslCertificate::fromPath(hostCertPath);
79 if (!certList.isEmpty())
80 hostCert = certList.first();
82 if (!hostCert.isNull())
84 if (hostCert.effectiveDate() > QDateTime::currentDateTime())
86 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Host certificate start date in future (%1)").arg(hostCertPath));
90 if (hostCert.expiryDate() < QDateTime::currentDateTime())
92 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Host certificate has expired (%1)").arg(hostCertPath));
96 Config.setLocalCertificate(hostCert);
100 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Unable to load host cert from file (%1)").arg(hostCertPath));
105 auto CACertList = QSslCertificate::fromPath(caCertPath);
106 if (!CACertList.isEmpty())
108 Config.setCaCertificates(CACertList);
110 else if (!caCertPath.isEmpty())
113 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Unable to load CA cert file (%1)").arg(caCertPath));
123 auto Encrypted = [](
const QSslSocket* SslSocket)
125 LOG(VB_HTTP, LOG_INFO,
LOC +
"Socket encrypted");
127 LOG(VB_HTTP, LOG_INFO,
LOC + QString(
"Cypher: %1").arg(SslSocket->sessionCipher().name()));
130 auto SSLErrors = [](
const QList<QSslError>& Errors)
132 for (
const auto &
error : Errors)
133 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"SslError: %1").arg(
error.errorString()));
136 QObject::connect(Socket, &QSslSocket::encrypted, [Encrypted, Socket] { Encrypted(Socket); } );
137 QObject::connect(Socket, qOverload<
const QList<QSslError> &>(&QSslSocket::sslErrors), SSLErrors);
138 Socket->setSslConfiguration(Config);
139 Socket->startServerEncryption();