Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
124
133
138{
139 // QSharedDataPointer deletes d for us if necessary
140}
141
147{
148 d = other.d;
149 return *this;
150}
151
170{
171 if (d == other.d)
172 return true;
173 return d->peerCertificate == other.d->peerCertificate &&
174 d->peerCertificateChain == other.d->peerCertificateChain &&
175 d->localCertificateChain == other.d->localCertificateChain &&
176 d->privateKey == other.d->privateKey &&
177 d->sessionCipher == other.d->sessionCipher &&
178 d->sessionProtocol == other.d->sessionProtocol &&
179 d->preSharedKeyIdentityHint == other.d->preSharedKeyIdentityHint &&
180 d->ciphers == other.d->ciphers &&
181 d->ellipticCurves == other.d->ellipticCurves &&
182 d->ephemeralServerKey == other.d->ephemeralServerKey &&
183 d->dhParams == other.d->dhParams &&
184 d->caCertificates == other.d->caCertificates &&
185 d->protocol == other.d->protocol &&
186 d->peerVerifyMode == other.d->peerVerifyMode &&
187 d->peerVerifyDepth == other.d->peerVerifyDepth &&
188 d->allowRootCertOnDemandLoading == other.d->allowRootCertOnDemandLoading &&
189 d->backendConfig == other.d->backendConfig &&
190 d->sslOptions == other.d->sslOptions &&
191 d->sslSession == other.d->sslSession &&
192 d->sslSessionTicketLifeTimeHint == other.d->sslSessionTicketLifeTimeHint &&
193 d->nextAllowedProtocols == other.d->nextAllowedProtocols &&
194 d->nextNegotiatedProtocol == other.d->nextNegotiatedProtocol &&
195 d->nextProtocolNegotiationStatus == other.d->nextProtocolNegotiationStatus &&
196 d->dtlsCookieEnabled == other.d->dtlsCookieEnabled &&
197 d->ocspStaplingEnabled == other.d->ocspStaplingEnabled &&
198 d->reportFromCallback == other.d->reportFromCallback &&
199 d->missingCertIsFatal == other.d->missingCertIsFatal;
200}
201
248
258
272
288
304
305
318{
319 return d->peerVerifyDepth;
320}
321
334{
335 if (depth < 0) {
336 qCWarning(lcSsl,
337 "QSslConfiguration::setPeerVerifyDepth: cannot set negative depth of %d", depth);
338 return;
339 }
341}
342
350QList<QSslCertificate> QSslConfiguration::localCertificateChain() const
351{
352 return d->localCertificateChain;
353}
354
377void QSslConfiguration::setLocalCertificateChain(const QList<QSslCertificate> &localChain)
378{
379 d->localCertificateChain = localChain;
380}
381
394
412{
413 d->localCertificateChain = QList<QSslCertificate>();
414 d->localCertificateChain += certificate;
415}
416
449
477QList<QSslCertificate> QSslConfiguration::peerCertificateChain() const
478{
479 return d->peerCertificateChain;
480}
481
499
512
520{
521 return d->privateKey;
522}
523
541
561QList<QSslCipher> QSslConfiguration::ciphers() const
562{
563 return d->ciphers;
564}
565
576void QSslConfiguration::setCiphers(const QList<QSslCipher> &ciphers)
577{
578 d->ciphers = ciphers;
579}
580
598{
599 auto *p = d.data();
600 p->ciphers.clear();
601 const auto cipherNames = ciphers.split(u':', Qt::SkipEmptyParts);
602 for (const QString &cipherName : cipherNames) {
603 QSslCipher cipher(cipherName);
604 if (!cipher.isNull())
605 p->ciphers << cipher;
606 }
607}
608
622
632QList<QSslCertificate> QSslConfiguration::caCertificates() const
633{
634 return d->caCertificates;
635}
636
649void QSslConfiguration::setCaCertificates(const QList<QSslCertificate> &certificates)
650{
651 d->caCertificates = certificates;
653}
654
674{
675 QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax);
676 if (certs.isEmpty())
677 return false;
678
679 d->caCertificates += certs;
680 return true;
681}
682
698{
699 d->caCertificates += certificate;
701}
702
717void QSslConfiguration::addCaCertificates(const QList<QSslCertificate> &certificates)
718{
719 d->caCertificates += certificates;
721}
722
735{
736 // we are calling ensureInitialized() in the method below
738}
739
748{
749 d->sslOptions.setFlag(option, on);
750}
751
763
784
796{
798}
799
817
834
857QList<QSslEllipticCurve> QSslConfiguration::ellipticCurves() const
858{
859 return d->ellipticCurves;
860}
861
874void QSslConfiguration::setEllipticCurves(const QList<QSslEllipticCurve> &curves)
875{
876 d->ellipticCurves = curves;
877}
878
892
904
918
935
956
967QMap<QByteArray, QVariant> QSslConfiguration::backendConfiguration() const
968{
969 return d->backendConfig;
970}
971
994
1006void QSslConfiguration::setBackendConfiguration(const QMap<QByteArray, QVariant> &backendConfiguration)
1007{
1009}
1010
1029
1044void QSslConfiguration::setAllowedNextProtocols(const QList<QByteArray> &protocols)
1045{
1046 d->nextAllowedProtocols = protocols;
1047}
1048
1059{
1060 return d->nextAllowedProtocols;
1061}
1062
1078
1099
1111
1112#if QT_CONFIG(dtls) || defined(Q_QDOC)
1113
1120bool QSslConfiguration::dtlsCookieVerificationEnabled() const
1121{
1122 return d->dtlsCookieEnabled;
1123}
1124
1130void QSslConfiguration::setDtlsCookieVerificationEnabled(bool enable)
1131{
1133}
1134
1152QSslConfiguration QSslConfiguration::defaultDtlsConfiguration()
1153{
1155}
1156
1164void QSslConfiguration::setDefaultDtlsConfiguration(const QSslConfiguration &configuration)
1165{
1167}
1168
1169#endif // dtls
1170
1181{
1182#if QT_CONFIG(ocsp)
1184#else
1185 if (enabled)
1186 qCWarning(lcSsl, "Enabling OCSP-stapling requires the feature 'ocsp'");
1187#endif // ocsp
1188}
1189
1198{
1199 return d->ocspStaplingEnabled;
1200}
1201
1216
1240{
1241#if QT_CONFIG(openssl)
1242 d->reportFromCallback = interrupt;
1243#else
1244 Q_UNUSED(interrupt);
1245 qCWarning(lcSsl, "This operation requires OpenSSL as TLS backend");
1246#endif
1247}
1248
1263
1278{
1279#if QT_CONFIG(openssl)
1280 d->missingCertIsFatal = cannotRecover;
1281#else
1282 Q_UNUSED(cannotRecover);
1283 qCWarning(lcSsl, "Handling a missing certificate as a fatal error requires an OpenSSL backend");
1284#endif // openssl
1285}
1286
1290 return configuration.d->peerSessionShared;
1291 }
1292
\inmodule QtCore
Definition qbytearray.h:57
bool isNull() const noexcept
Returns true if this byte array is null; otherwise returns false.
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
void clear()
Definition qlist.h:434
bool isEmpty() const
Definition qmap.h:269
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
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:129
\inmodule QtCore
Definition qvariant.h:65
EncodingFormat
Describes supported encoding formats for certificates and keys.
Definition qssl.h:28
SslOption
Describes the options that can be used to control the details of SSL behaviour.
Definition qssl.h:73
@ SslOptionDisableSessionPersistence
Definition qssl.h:80
@ SslOptionDisableCompression
Definition qssl.h:76
@ SslOptionDisableLegacyRenegotiation
Definition qssl.h:78
@ SslOptionDisableEmptyFragments
Definition qssl.h:74
SslProtocol
Describes the protocol of the cipher.
Definition qssl.h:50
@ SecureProtocols
Definition qssl.h:55
Combined button and popup list for selecting options.
@ SkipEmptyParts
Definition qnamespace.h:128
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qCWarning(category,...)
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
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]