Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlsslconfiguration.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtCore/qfile.h>
6#include <QtNetwork/qsslcipher.h>
8#include <array>
9
10QT_BEGIN_NAMESPACE
11
12// Keep in sync with QSsl::SslOption!
13static constexpr std::array<QSsl::SslOption, 8> SslOptions = {
14 QSsl::SslOptionDisableEmptyFragments,
15 QSsl::SslOptionDisableSessionTickets,
16 QSsl::SslOptionDisableCompression,
17 QSsl::SslOptionDisableServerNameIndication,
18 QSsl::SslOptionDisableLegacyRenegotiation,
19 QSsl::SslOptionDisableSessionSharing,
20 QSsl::SslOptionDisableSessionPersistence,
21 QSsl::SslOptionDisableServerCipherPreference
22};
23
24QString QQmlSslConfiguration::ciphers() const
25{
26 return m_ciphers;
27}
28
29#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
30QList<QSsl::SslOption> QQmlSslConfiguration::sslOptions() const
31{
32 return m_sslOptions;
33}
34#endif // QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
35
36QSsl::SslProtocol QQmlSslConfiguration::protocol() const
37{
38 return m_configuration.protocol();
39}
40
41QSslSocket::PeerVerifyMode QQmlSslConfiguration::peerVerifyMode() const
42{
43 return m_configuration.peerVerifyMode();
44}
45
46int QQmlSslConfiguration::peerVerifyDepth() const
47{
48 return m_configuration.peerVerifyDepth();
49}
50
51QByteArray QQmlSslConfiguration::sessionTicket() const
52{
53 return m_configuration.sessionTicket();
54}
55
56QSsl::SslOptions QQmlSslConfiguration::sslOptionFlags() const
57{
58 QSsl::SslOptions options{};
59 for (auto opt : SslOptions) {
60 if (m_configuration.testSslOption(opt))
61 options |= opt;
62 }
63 return options;
64}
65
66QSslConfiguration const QQmlSslConfiguration::configuration()
67{
68 return m_configuration;
69}
70
71void QQmlSslConfiguration::setCertificateFiles(const QStringList &certificateFiles)
72{
73 if (m_certificateFiles == certificateFiles)
74 return;
75
76 m_certificateFiles = certificateFiles;
77 QList<QSslCertificate> certificates;
78 for (const QString &fileName: m_certificateFiles) {
79 QFile certificateFile(fileName);
80 if (certificateFile.open(QIODevice::ReadOnly)) {
81 QByteArray cert = certificateFile.readAll();
82 certificates.append(QSslCertificate(cert));
83 } else {
84 qWarning() << "File: " << fileName << "is not found. It will be skipped.";
85 }
86 }
87
88 if (!certificates.isEmpty())
89 m_configuration.setCaCertificates(certificates);
90 else
91 qWarning() << "No certificates loaded.";
92}
93
94void QQmlSslConfiguration::setProtocol(QSsl::SslProtocol protocol)
95{
96 if (m_configuration.protocol() == protocol)
97 return;
98
99 m_configuration.setProtocol(protocol);
100}
101
102void QQmlSslConfiguration::setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
103{
104 if (m_configuration.peerVerifyMode() == mode)
105 return;
106
107 m_configuration.setPeerVerifyMode(mode);
108}
109
110void QQmlSslConfiguration::setPeerVerifyDepth(int depth)
111{
112 if (m_configuration.peerVerifyDepth() == depth)
113 return;
114
115 m_configuration.setPeerVerifyDepth(depth);
116}
117
118void QQmlSslConfiguration::setCiphers(const QString &ciphers)
119{
120 if (ciphers == m_ciphers)
121 return;
122
123 m_ciphers = ciphers;
124 m_configuration.setCiphers(ciphers); // split(":") is used inside
125}
126
127#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
128void QQmlSslConfiguration::setSslOptions(const QList<QSsl::SslOption> &options)
129{
130 if (m_sslOptions == options)
131 return;
132
133 m_sslOptions = options;
134 for (QSsl::SslOption option: m_sslOptions)
135 m_configuration.setSslOption(option, true);
136}
137#endif // QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
138
139void QQmlSslConfiguration::setSessionTicket(const QByteArray &sessionTicket)
140{
141 if (m_configuration.sessionTicket() == sessionTicket)
142 return;
143
144 m_configuration.setSessionTicket(sessionTicket);
145}
146
147void QQmlSslConfiguration::setSslOptionFlags(QSsl::SslOptions options)
148{
149 for (auto opt : SslOptions)
150 m_configuration.setSslOption(opt, options & opt);
151}
152
153void QQmlSslConfiguration::setPrivateKey(const QQmlSslKey &privateKey)
154{
155 m_configuration.setPrivateKey(privateKey.getSslKey());
156}
157
158#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
159void QQmlSslConfiguration::setSslOptionsList(const QSslConfiguration &configuration)
160{
161 Q_ASSERT(m_sslOptions.isEmpty());
162 for (QSsl::SslOption option: SslOptions) {
163 if (configuration.testSslOption(option))
164 m_sslOptions.append(option);
165 }
166}
167#endif
168
169void QQmlSslConfiguration::setCiphersList(const QSslConfiguration &configuration)
170{
171 Q_ASSERT(m_ciphers.isEmpty());
172 QList<QSslCipher> ciphers = configuration.ciphers();
173 for (int i = 0; i < ciphers.size(); ++i) {
174 if (i != 0) {
175 m_ciphers += QString::fromUtf8(":");
176 }
177 m_ciphers += ciphers[i].name();
178 }
179}
180
181QQmlSslDefaultConfiguration::QQmlSslDefaultConfiguration()
182 : QQmlSslConfiguration()
183{
184 m_configuration = QSslConfiguration::defaultConfiguration();
185#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
186 setSslOptionsList(m_configuration);
187#endif
188 setCiphersList(m_configuration);
189}
190
191QQmlSslDefaultDtlsConfiguration::QQmlSslDefaultDtlsConfiguration()
192 : QQmlSslConfiguration()
193{
194#if QT_CONFIG(dtls)
195 m_configuration = QSslConfiguration::defaultDtlsConfiguration();
196#else
197 qWarning() << "No dtls support enabled";
198 m_configuration = QSslConfiguration::defaultConfiguration();
199#endif // QT_CONFIG(dtls)
200#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
201 setSslOptionsList(m_configuration);
202#endif
203 setCiphersList(m_configuration);
204}
205
206QT_END_NAMESPACE