Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsslconfiguration.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2014 BlackBerry Limited. All rights reserved.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qssl_p.h"
6#include "qsslconfiguration.h"
8#include "qsslsocket.h"
9#include "qsslsocket_p.h"
10#include "qmutex.h"
11#include "qdebug.h"
12
14
16
21
22const char QSslConfiguration::ALPNProtocolHTTP2[] = "h2";
23const char QSslConfiguration::NextProtocolHttp1_1[] = "http/1.1";
24
116{
117}
118
124 : d(other.d)
125{
126}
127
132{
133 // QSharedDataPointer deletes d for us if necessary
134}
135
141{
142 d = other.d;
143 return *this;
144}
145
164{
165 if (d == other.d)
166 return true;
167 return d->peerCertificate == other.d->peerCertificate &&
168 d->peerCertificateChain == other.d->peerCertificateChain &&
169 d->localCertificateChain == other.d->localCertificateChain &&
170 d->privateKey == other.d->privateKey &&
171 d->sessionCipher == other.d->sessionCipher &&
172 d->sessionProtocol == other.d->sessionProtocol &&
173 d->preSharedKeyIdentityHint == other.d->preSharedKeyIdentityHint &&
174 d->ciphers == other.d->ciphers &&
175 d->ellipticCurves == other.d->ellipticCurves &&
176 d->ephemeralServerKey == other.d->ephemeralServerKey &&
177 d->dhParams == other.d->dhParams &&
178 d->caCertificates == other.d->caCertificates &&
179 d->protocol == other.d->protocol &&
180 d->peerVerifyMode == other.d->peerVerifyMode &&
181 d->peerVerifyDepth == other.d->peerVerifyDepth &&
182 d->allowRootCertOnDemandLoading == other.d->allowRootCertOnDemandLoading &&
183 d->backendConfig == other.d->backendConfig &&
184 d->sslOptions == other.d->sslOptions &&
185 d->sslSession == other.d->sslSession &&
186 d->sslSessionTicketLifeTimeHint == other.d->sslSessionTicketLifeTimeHint &&
187 d->nextAllowedProtocols == other.d->nextAllowedProtocols &&
188 d->nextNegotiatedProtocol == other.d->nextNegotiatedProtocol &&
189 d->nextProtocolNegotiationStatus == other.d->nextProtocolNegotiationStatus &&
190 d->dtlsCookieEnabled == other.d->dtlsCookieEnabled &&
191 d->ocspStaplingEnabled == other.d->ocspStaplingEnabled &&
192 d->reportFromCallback == other.d->reportFromCallback &&
193 d->missingCertIsFatal == other.d->missingCertIsFatal;
194}
195
216{
217 return (d->protocol == QSsl::SecureProtocols &&
219 d->peerVerifyDepth == 0 &&
220 d->allowRootCertOnDemandLoading == true &&
221 d->caCertificates.size() == 0 &&
222 d->ciphers.size() == 0 &&
223 d->ellipticCurves.isEmpty() &&
227 d->privateKey.isNull() &&
228 d->peerCertificate.isNull() &&
229 d->peerCertificateChain.size() == 0 &&
230 d->backendConfig.isEmpty() &&
232 d->sslSession.isNull() &&
238 d->ocspStaplingEnabled == false &&
239 d->reportFromCallback == false &&
240 d->missingCertIsFatal == false);
241}
242
249{
250 return d->protocol;
251}
252
263{
264 d->protocol = protocol;
265}
266
279{
280 return d->peerVerifyMode;
281}
282
295{
296 d->peerVerifyMode = mode;
297}
298
299
312{
313 return d->peerVerifyDepth;
314}
315
328{
329 if (depth < 0) {
330 qCWarning(lcSsl,
331 "QSslConfiguration::setPeerVerifyDepth: cannot set negative depth of %d", depth);
332 return;
333 }
335}
336
345{
346 return d->localCertificateChain;
347}
348
372{
373 d->localCertificateChain = localChain;
374}
375
383{
385 return QSslCertificate();
386 return d->localCertificateChain[0];
387}
388
406{
408 d->localCertificateChain += certificate;
409}
410
440{
441 return d->peerCertificate;
442}
443
472{
473 return d->peerCertificateChain;
474}
475
490{
491 return d->sessionCipher;
492}
493
503{
504 return d->sessionProtocol;
505}
506
514{
515 return d->privateKey;
516}
517
532{
533 d->privateKey = key;
534}
535
556{
557 return d->ciphers;
558}
559
571{
572 d->ciphers = ciphers;
573}
574
595{
596 auto *p = d.data();
597 p->ciphers.clear();
598 const auto cipherNames = ciphers.split(u':', Qt::SkipEmptyParts);
599 for (const QString &cipherName : cipherNames) {
600 QSslCipher cipher(cipherName);
601 if (!cipher.isNull())
602 p->ciphers << cipher;
603 }
604}
605
616{
618}
619
630{
631 return d->caCertificates;
632}
633
647{
648 d->caCertificates = certificates;
650}
651
671{
673 if (certs.isEmpty())
674 return false;
675
676 d->caCertificates += certs;
677 return true;
678}
679
695{
696 d->caCertificates += certificate;
698}
699
715{
716 d->caCertificates += certificates;
718}
719
732{
733 // we are calling ensureInitialized() in the method below
735}
736
745{
746 d->sslOptions.setFlag(option, on);
747}
748
757{
758 return d->sslOptions & option;
759}
760
778{
779 return d->sslSession;
780}
781
793{
795}
796
811{
813}
814
828{
829 return d->ephemeralServerKey;
830}
831
855{
856 return d->ellipticCurves;
857}
858
872{
873 d->ellipticCurves = curves;
874}
875
886{
888}
889
898{
899 return d->preSharedKeyIdentityHint;
900}
901
912{
914}
915
929{
930 return d->dhParams;
931}
932
947{
948 d->dhParams = dhparams;
949}
950
962{
963 return d->backendConfig;
964}
965
985{
987}
988
1001{
1003}
1004
1020{
1021 return d->nextNegotiatedProtocol;
1022}
1023
1039{
1040 d->nextAllowedProtocols = protocols;
1041}
1042
1053{
1054 return d->nextAllowedProtocols;
1055}
1056
1069{
1071}
1072
1090{
1092}
1093
1102{
1104}
1105
1106#if QT_CONFIG(dtls) || defined(Q_QDOC)
1107
1114bool QSslConfiguration::dtlsCookieVerificationEnabled() const
1115{
1116 return d->dtlsCookieEnabled;
1117}
1118
1124void QSslConfiguration::setDtlsCookieVerificationEnabled(bool enable)
1125{
1127}
1128
1146QSslConfiguration QSslConfiguration::defaultDtlsConfiguration()
1147{
1149}
1150
1158void QSslConfiguration::setDefaultDtlsConfiguration(const QSslConfiguration &configuration)
1159{
1161}
1162
1163#endif // dtls
1164
1175{
1176#if QT_CONFIG(ocsp)
1178#else
1179 if (enabled)
1180 qCWarning(lcSsl, "Enabling OCSP-stapling requires the feature 'ocsp'");
1181#endif // ocsp
1182}
1183
1192{
1193 return d->ocspStaplingEnabled;
1194}
1195
1207{
1208 return d->reportFromCallback;
1209}
1210
1234{
1235#if QT_CONFIG(openssl)
1236 d->reportFromCallback = interrupt;
1237#else
1238 Q_UNUSED(interrupt);
1239 qCWarning(lcSsl, "This operation requires OpenSSL as TLS backend");
1240#endif
1241}
1242
1254{
1255 return d->missingCertIsFatal;
1256}
1257
1272{
1273#if QT_CONFIG(openssl)
1274 d->missingCertIsFatal = cannotRecover;
1275#else
1276 Q_UNUSED(cannotRecover);
1277 qCWarning(lcSsl, "Handling a missing certificate as a fatal error requires an OpenSSL backend");
1278#endif // openssl
1279}
1280
1284 return configuration.d->peerSessionShared;
1285 }
1286
\inmodule QtCore
Definition qbytearray.h:57
bool isNull() const noexcept
Returns true if this byte array is null; otherwise returns false.
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
void clear()
Definition qlist.h:417
Definition qmap.h:186
bool isEmpty() const
Definition qmap.h:268
T * data()
Returns a pointer to the shared data object.
Definition qshareddata.h:47
The QSslCertificate class provides a convenient API for an X509 certificate.
static QList< QSslCertificate > fromPath(const QString &path, QSsl::EncodingFormat format=QSsl::Pem, PatternSyntax syntax=PatternSyntax::FixedString)
bool isNull() const
Returns true if this is a null certificate (i.e., a certificate with no contents); otherwise returns ...
The QSslCipher class represents an SSL cryptographic cipher.
Definition qsslcipher.h:22
bool isNull() const
Returns true if this is a null cipher; otherwise returns false.
static Q_AUTOTEST_EXPORT bool peerSessionWasShared(const QSslConfiguration &configuration)
QSslSocket::PeerVerifyMode peerVerifyMode
QList< QSslEllipticCurve > ellipticCurves
QSslConfiguration::NextProtocolNegotiationStatus nextProtocolNegotiationStatus
QList< QSslCertificate > caCertificates
static QSslConfiguration defaultConfiguration()
static void setDefaultDtlsConfiguration(const QSslConfiguration &configuration)
static const QSsl::SslOptions defaultSslOptions
QMap< QByteArray, QVariant > backendConfig
QList< QSslCertificate > peerCertificateChain
static void setDefaultConfiguration(const QSslConfiguration &configuration)
QList< QByteArray > nextAllowedProtocols
QList< QSslCipher > ciphers
QSsl::SslProtocol sessionProtocol
QList< QSslCertificate > localCertificateChain
QSslDiffieHellmanParameters dhParams
static QSslConfiguration defaultDtlsConfiguration()
The QSslConfiguration class holds the configuration and state of an SSL connection.
QList< QByteArray > allowedNextProtocols() const
QSslCertificate localCertificate() const
Returns the certificate to be presented to the peer during the SSL handshake process.
bool testSslOption(QSsl::SslOption option) const
void setSessionTicket(const QByteArray &sessionTicket)
void setEllipticCurves(const QList< QSslEllipticCurve > &curves)
QList< QSslCertificate > caCertificates() const
Returns this connection's CA certificate database.
void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
Sets the verify mode to mode.
bool ocspStaplingEnabled() const
QList< QSslCertificate > localCertificateChain() const
Returns the certificate chain to be presented to the peer during the SSL handshake process.
QSslConfiguration & operator=(QSslConfiguration &&other) noexcept
void setMissingCertificateIsFatal(bool cannotRecover)
void setBackendConfigurationOption(const QByteArray &name, const QVariant &value)
QSslSocket::PeerVerifyMode peerVerifyMode() const
Returns the verify mode.
QSsl::SslProtocol protocol() const
Returns the protocol setting for this SSL configuration.
bool isNull() const
Returns true if this is a null QSslConfiguration object.
static const char ALPNProtocolHTTP2[]
QSslKey ephemeralServerKey() const
void setHandshakeMustInterruptOnError(bool interrupt)
QSslDiffieHellmanParameters diffieHellmanParameters() const
QSslConfiguration()
\variable QSslConfiguration::NextProtocolHttp1_1
QByteArray preSharedKeyIdentityHint() const
int sessionTicketLifeTimeHint() const
QSsl::SslProtocol sessionProtocol() const
Returns the socket's SSL/TLS protocol or UnknownProtocol if the connection isn't encrypted.
int peerVerifyDepth() const
Returns the maximum number of certificates in the peer's certificate chain to be checked during the S...
static void setDefaultConfiguration(const QSslConfiguration &configuration)
Sets the default SSL configuration to be used in new SSL connections to be configuration.
void setDiffieHellmanParameters(const QSslDiffieHellmanParameters &dhparams)
bool operator==(const QSslConfiguration &other) const
Returns true if this QSslConfiguration object is equal to other.
QSslCipher sessionCipher() const
Returns the socket's cryptographic \l {QSslCipher} {cipher}, or a null cipher if the connection isn't...
QByteArray nextNegotiatedProtocol() const
QSslKey privateKey() const
Returns the \l {QSslKey} {SSL key} assigned to this connection or a null key if none has been assigne...
void addCaCertificate(const QSslCertificate &certificate)
QList< QSslEllipticCurve > ellipticCurves() const
void setSslOption(QSsl::SslOption option, bool on)
Enables or disables an SSL compatibility option.
QList< QSslCipher > ciphers() const
Returns this connection's current cryptographic cipher suite.
void setBackendConfiguration(const QMap< QByteArray, QVariant > &backendConfiguration=QMap< QByteArray, QVariant >())
QByteArray sessionTicket() const
void setLocalCertificate(const QSslCertificate &certificate)
Sets the certificate to be presented to the peer during SSL handshake to be certificate.
QList< QSslCertificate > peerCertificateChain() const
Returns the peer's chain of digital certificates, starting with the peer's immediate certificate and ...
bool handshakeMustInterruptOnError() const
void setPreSharedKeyIdentityHint(const QByteArray &hint)
bool missingCertificateIsFatal() const
static QList< QSslCipher > supportedCiphers()
~QSslConfiguration()
Releases any resources held by QSslConfiguration.
bool addCaCertificates(const QString &path, QSsl::EncodingFormat format=QSsl::Pem, QSslCertificate::PatternSyntax syntax=QSslCertificate::PatternSyntax::FixedString)
QMap< QByteArray, QVariant > backendConfiguration() const
static QList< QSslCertificate > systemCaCertificates()
void setPrivateKey(const QSslKey &key)
Sets the connection's private \l {QSslKey} {key} to key.
void setAllowedNextProtocols(const QList< QByteArray > &protocols)
void setLocalCertificateChain(const QList< QSslCertificate > &localChain)
Sets the certificate chain to be presented to the peer during the SSL handshake to be localChain.
QSslCertificate peerCertificate() const
Returns the peer's digital certificate (i.e., the immediate certificate of the host you are connected...
static const char NextProtocolHttp1_1[]
void setCiphers(const QList< QSslCipher > &ciphers)
Sets the cryptographic cipher suite for this socket to ciphers, which must contain a subset of the ci...
void setPeerVerifyDepth(int depth)
Sets the maximum number of certificates in the peer's certificate chain to be checked during the SSL ...
static QSslConfiguration defaultConfiguration()
Returns the default SSL configuration to be used in new SSL connections.
void setOcspStaplingEnabled(bool enable)
NextProtocolNegotiationStatus
Describes the status of the Next Protocol Negotiation (NPN) or Application-Layer Protocol Negotiation...
static QList< QSslEllipticCurve > supportedEllipticCurves()
void setCaCertificates(const QList< QSslCertificate > &certificates)
Sets this socket's CA certificate database to be certificates.
void setProtocol(QSsl::SslProtocol protocol)
Sets the protocol setting for this configuration to be protocol.
NextProtocolNegotiationStatus nextProtocolNegotiationStatus() const
The QSslDiffieHellmanParameters class provides an interface for Diffie-Hellman parameters for servers...
static QSslDiffieHellmanParameters defaultParameters()
Returns the default QSslDiffieHellmanParameters used by QSslSocket.
The QSslKey class provides an interface for private and public keys.
Definition qsslkey.h:23
bool isNull() const
Returns true if this is a null key; otherwise false.
static QList< QSslCipher > supportedCiphers()
static QList< QSslEllipticCurve > supportedEllipticCurves()
static QList< QSslCertificate > systemCaCertificates()
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qvariant.h:64
EncodingFormat
Describes supported encoding formats for certificates and keys.
Definition qssl.h:24
SslOption
Describes the options that can be used to control the details of SSL behaviour.
Definition qssl.h:65
@ SslOptionDisableSessionPersistence
Definition qssl.h:72
@ SslOptionDisableCompression
Definition qssl.h:68
@ SslOptionDisableLegacyRenegotiation
Definition qssl.h:70
@ SslOptionDisableEmptyFragments
Definition qssl.h:66
SslProtocol
Describes the protocol of the cipher.
Definition qssl.h:43
@ SecureProtocols
Definition qssl.h:48
Combined button and popup list for selecting options.
@ SkipEmptyParts
Definition qnamespace.h:127
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qCWarning(category,...)
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1369
GLint GLenum GLsizei GLsizei GLsizei depth
GLenum mode
GLuint64 key
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLboolean enable
GLuint name
GLint GLsizei GLsizei GLenum format
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
GLuint GLenum option
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define Q_UNUSED(x)
#define enabled
QSharedPointer< T > other(t)
[5]
const auto certs
[1]