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
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// Qt-Security score:significant reason:default
5
6#include "qssl_p.h"
9#include "qsslsocket.h"
10#include "qsslsocket_p.h"
11#include "qmutex.h"
12#include "qdebug.h"
13
15
16QT_IMPL_METATYPE_EXTERN(QSslConfiguration)
17
18const QSsl::SslOptions QSslConfigurationPrivate::defaultSslOptions = QSsl::SslOptionDisableEmptyFragments
19 |QSsl::SslOptionDisableLegacyRenegotiation
20 |QSsl::SslOptionDisableCompression
21 |QSsl::SslOptionDisableSessionPersistence;
22
23const char QSslConfiguration::ALPNProtocolHTTP2[] = "h2";
24const char QSslConfiguration::NextProtocolHttp1_1[] = "http/1.1";
25
26/*!
27 \class QSslConfiguration
28 \brief The QSslConfiguration class holds the configuration and state of an SSL connection.
29 \since 4.4
30
31 \reentrant
32 \inmodule QtNetwork
33 \ingroup network
34 \ingroup ssl
35 \ingroup shared
36
37 QSslConfiguration is used by Qt networking classes to relay
38 information about an open SSL connection and to allow the
39 application to control certain features of that connection.
40
41 The settings that QSslConfiguration currently supports are:
42
43 \list
44 \li The SSL/TLS protocol to be used
45 \li The certificate to be presented to the peer during connection
46 and its associated private key
47 \li The ciphers allowed to be used for encrypting the connection
48 \li The list of Certificate Authorities certificates that are
49 used to validate the peer's certificate
50 \endlist
51
52 These settings are applied only during the connection
53 handshake. Setting them after the connection has been established
54 has no effect.
55
56 The state that QSslConfiguration supports are:
57 \list
58 \li The certificate the peer presented during handshake, along
59 with the chain leading to a CA certificate
60 \li The cipher used to encrypt this session
61 \endlist
62
63 The state can only be obtained once the SSL connection starts, but
64 not necessarily before it's done. Some settings may change during
65 the course of the SSL connection without need to restart it (for
66 instance, the cipher can be changed over time).
67
68 State in QSslConfiguration objects cannot be changed.
69
70 QSslConfiguration can be used with QSslSocket and the Network
71 Access API.
72
73 Note that changing settings in QSslConfiguration is not enough to
74 change the settings in the related SSL connection. You must call
75 setSslConfiguration on a modified QSslConfiguration object to
76 achieve that. The following example illustrates how to change the
77 protocol to TLSv1_2 in a QSslSocket object:
78
79 \snippet code/src_network_ssl_qsslconfiguration.cpp 0
80
81 \sa QSsl::SslProtocol, QSslCertificate, QSslCipher, QSslKey,
82 QSslSocket, QNetworkAccessManager,
83 QSslSocket::sslConfiguration(), QSslSocket::setSslConfiguration()
84*/
85
86/*!
87 \enum QSslConfiguration::NextProtocolNegotiationStatus
88
89 Describes the status of the Next Protocol Negotiation (NPN) or
90 Application-Layer Protocol Negotiation (ALPN).
91
92 \value NextProtocolNegotiationNone No application protocol
93 has been negotiated (yet).
94
95 \value NextProtocolNegotiationNegotiated A next protocol
96 has been negotiated (see nextNegotiatedProtocol()).
97
98 \value NextProtocolNegotiationUnsupported The client and
99 server could not agree on a common next application protocol.
100*/
101
102/*!
103 \variable QSslConfiguration::NextProtocolHttp1_1
104 \brief The value used for negotiating HTTP 1.1 during the Next
105 Protocol Negotiation.
106*/
107
108/*!
109 \variable QSslConfiguration::ALPNProtocolHTTP2
110 \brief The value used for negotiating HTTP 2 during the Application-Layer
111 Protocol Negotiation.
112*/
113
114/*!
115 Constructs an empty SSL configuration. This configuration contains
116 no valid settings and the state will be empty. isNull() will
117 return true after this constructor is called.
118
119 Once any setter methods are called, isNull() will return false.
120*/
121QSslConfiguration::QSslConfiguration()
122 : d(new QSslConfigurationPrivate)
123{
124}
125
126/*!
127 Copies the configuration and state of \a other. If \a other is
128 null, this object will be null too.
129*/
130QSslConfiguration::QSslConfiguration(const QSslConfiguration &other)
131 : d(other.d)
132{
133}
134
135/*!
136 Releases any resources held by QSslConfiguration.
137*/
138QSslConfiguration::~QSslConfiguration()
139{
140 // QSharedDataPointer deletes d for us if necessary
141}
142
143/*!
144 Copies the configuration and state of \a other. If \a other is
145 null, this object will be null too.
146*/
147QSslConfiguration &QSslConfiguration::operator=(const QSslConfiguration &other)
148{
149 d = other.d;
150 return *this;
151}
152
153/*!
154 \fn void QSslConfiguration::swap(QSslConfiguration &other)
155 \since 5.0
156 \memberswap{SSL configuration instance}
157*/
158
159/*!
160 Returns \c true if this QSslConfiguration object is equal to \a
161 other.
162
163 Two QSslConfiguration objects are considered equal if they have
164 the exact same settings and state.
165
166 \sa operator!=()
167*/
168bool QSslConfiguration::operator==(const QSslConfiguration &other) const
169{
170 if (d == other.d)
171 return true;
172 return d->peerCertificate == other.d->peerCertificate &&
173 d->peerCertificateChain == other.d->peerCertificateChain &&
174 d->localCertificateChain == other.d->localCertificateChain &&
175 d->privateKey == other.d->privateKey &&
176 d->sessionCipher == other.d->sessionCipher &&
177 d->sessionProtocol == other.d->sessionProtocol &&
178 d->preSharedKeyIdentityHint == other.d->preSharedKeyIdentityHint &&
179 d->ciphers == other.d->ciphers &&
180 d->ellipticCurves == other.d->ellipticCurves &&
181 d->ephemeralServerKey == other.d->ephemeralServerKey &&
182 d->dhParams == other.d->dhParams &&
183 d->caCertificates == other.d->caCertificates &&
184 d->protocol == other.d->protocol &&
185 d->peerVerifyMode == other.d->peerVerifyMode &&
186 d->peerVerifyDepth == other.d->peerVerifyDepth &&
187 d->allowRootCertOnDemandLoading == other.d->allowRootCertOnDemandLoading &&
188 d->backendConfig == other.d->backendConfig &&
189 d->sslOptions == other.d->sslOptions &&
190 d->sslSession == other.d->sslSession &&
191 d->sslSessionTicketLifeTimeHint == other.d->sslSessionTicketLifeTimeHint &&
192 d->nextAllowedProtocols == other.d->nextAllowedProtocols &&
193 d->nextNegotiatedProtocol == other.d->nextNegotiatedProtocol &&
194 d->nextProtocolNegotiationStatus == other.d->nextProtocolNegotiationStatus &&
195 d->keyingMaterial == other.d->keyingMaterial &&
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
202/*!
203 \fn QSslConfiguration::operator!=(const QSslConfiguration &other) const
204
205 Returns \c true if this QSslConfiguration differs from \a other. Two
206 QSslConfiguration objects are considered different if any state or
207 setting is different.
208
209 \sa operator==()
210*/
211
212/*!
213 Returns \c true if this is a null QSslConfiguration object.
214
215 A QSslConfiguration object is null if it has been
216 default-constructed and no setter methods have been called.
217
218 \sa setProtocol(), setLocalCertificate(), setPrivateKey(),
219 setCiphers(), setCaCertificates()
220*/
221bool QSslConfiguration::isNull() const
222{
223 return (d->protocol == QSsl::SecureProtocols &&
224 d->peerVerifyMode == QSslSocket::AutoVerifyPeer &&
225 d->peerVerifyDepth == 0 &&
226 d->allowRootCertOnDemandLoading == true &&
227 d->caCertificates.size() == 0 &&
228 d->ciphers.size() == 0 &&
229 d->ellipticCurves.isEmpty() &&
230 d->ephemeralServerKey.isNull() &&
231 d->dhParams == QSslDiffieHellmanParameters::defaultParameters() &&
232 d->localCertificateChain.isEmpty() &&
233 d->privateKey.isNull() &&
234 d->peerCertificate.isNull() &&
235 d->peerCertificateChain.size() == 0 &&
236 d->backendConfig.isEmpty() &&
237 d->sslOptions == QSslConfigurationPrivate::defaultSslOptions &&
238 d->sslSession.isNull() &&
239 d->sslSessionTicketLifeTimeHint == -1 &&
240 d->preSharedKeyIdentityHint.isNull() &&
241 d->nextAllowedProtocols.isEmpty() &&
242 d->nextNegotiatedProtocol.isNull() &&
243 d->nextProtocolNegotiationStatus == QSslConfiguration::NextProtocolNegotiationNone &&
244 d->keyingMaterial.isEmpty() &&
245 d->ocspStaplingEnabled == false &&
246 d->reportFromCallback == false &&
247 d->missingCertIsFatal == false);
248}
249
250/*!
251 Returns the protocol setting for this SSL configuration.
252
253 \sa setProtocol()
254*/
255QSsl::SslProtocol QSslConfiguration::protocol() const
256{
257 return d->protocol;
258}
259
260/*!
261 Sets the protocol setting for this configuration to be \a
262 protocol.
263
264 Setting the protocol once the connection has already been
265 established has no effect.
266
267 \sa protocol()
268*/
269void QSslConfiguration::setProtocol(QSsl::SslProtocol protocol)
270{
271 d->protocol = protocol;
272}
273
274/*!
275 Returns the verify mode. This mode decides whether QSslSocket should
276 request a certificate from the peer (i.e., the client requests a
277 certificate from the server, or a server requesting a certificate from the
278 client), and whether it should require that this certificate is valid.
279
280 The default mode is AutoVerifyPeer, which tells QSslSocket to use
281 VerifyPeer for clients, QueryPeer for servers.
282
283 \sa setPeerVerifyMode()
284*/
285QSslSocket::PeerVerifyMode QSslConfiguration::peerVerifyMode() const
286{
287 return d->peerVerifyMode;
288}
289
290/*!
291 Sets the verify mode to \a mode. This mode decides whether QSslSocket
292 should request a certificate from the peer (i.e., the client requests a
293 certificate from the server, or a server requesting a certificate from the
294 client), and whether it should require that this certificate is valid.
295
296 The default mode is AutoVerifyPeer, which tells QSslSocket to use
297 VerifyPeer for clients, QueryPeer for servers.
298
299 \sa peerVerifyMode()
300*/
301void QSslConfiguration::setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
302{
303 d->peerVerifyMode = mode;
304}
305
306
307/*!
308 Returns the maximum number of certificates in the peer's certificate chain
309 to be checked during the SSL handshake phase, or 0 (the default) if no
310 maximum depth has been set, indicating that the whole certificate chain
311 should be checked.
312
313 The certificates are checked in issuing order, starting with the peer's
314 own certificate, then its issuer's certificate, and so on.
315
316 \sa setPeerVerifyDepth(), peerVerifyMode()
317*/
318int QSslConfiguration::peerVerifyDepth() const
319{
320 return d->peerVerifyDepth;
321}
322
323/*!
324 Sets the maximum number of certificates in the peer's certificate chain to
325 be checked during the SSL handshake phase, to \a depth. Setting a depth of
326 0 means that no maximum depth is set, indicating that the whole
327 certificate chain should be checked.
328
329 The certificates are checked in issuing order, starting with the peer's
330 own certificate, then its issuer's certificate, and so on.
331
332 \sa peerVerifyDepth(), setPeerVerifyMode()
333*/
334void QSslConfiguration::setPeerVerifyDepth(int depth)
335{
336 if (depth < 0) {
337 qCWarning(lcSsl,
338 "QSslConfiguration::setPeerVerifyDepth: cannot set negative depth of %d", depth);
339 return;
340 }
341 d->peerVerifyDepth = depth;
342}
343
344/*!
345 Returns the certificate chain to be presented to the peer during
346 the SSL handshake process.
347
348 \sa localCertificate()
349 \since 5.1
350*/
351QList<QSslCertificate> QSslConfiguration::localCertificateChain() const
352{
353 return d->localCertificateChain;
354}
355
356/*!
357 Sets the certificate chain to be presented to the peer during the
358 SSL handshake to be \a localChain.
359
360 Setting the certificate chain once the connection has been
361 established has no effect.
362
363 A certificate is the means of identification used in the SSL
364 process. The local certificate is used by the remote end to verify
365 the local user's identity against its list of Certification
366 Authorities. In most cases, such as in HTTP web browsing, only
367 servers identify to the clients, so the client does not send a
368 certificate.
369
370 Unlike QSslConfiguration::setLocalCertificate() this method allows
371 you to specify any intermediate certificates required in order to
372 validate your certificate. The first item in the list must be the
373 leaf certificate.
374
375 \sa localCertificateChain()
376 \since 5.1
377 */
378void QSslConfiguration::setLocalCertificateChain(const QList<QSslCertificate> &localChain)
379{
380 d->localCertificateChain = localChain;
381}
382
383/*!
384 Returns the certificate to be presented to the peer during the SSL
385 handshake process.
386
387 \sa setLocalCertificate()
388*/
389QSslCertificate QSslConfiguration::localCertificate() const
390{
391 if (d->localCertificateChain.isEmpty())
392 return QSslCertificate();
393 return d->localCertificateChain[0];
394}
395
396/*!
397 Sets the certificate to be presented to the peer during SSL
398 handshake to be \a certificate.
399
400 Setting the certificate once the connection has been established
401 has no effect.
402
403 A certificate is the means of identification used in the SSL
404 process. The local certificate is used by the remote end to verify
405 the local user's identity against its list of Certification
406 Authorities. In most cases, such as in HTTP web browsing, only
407 servers identify to the clients, so the client does not send a
408 certificate.
409
410 \sa localCertificate()
411*/
412void QSslConfiguration::setLocalCertificate(const QSslCertificate &certificate)
413{
414 d->localCertificateChain = QList<QSslCertificate>();
415 d->localCertificateChain += certificate;
416}
417
418/*!
419 Returns the peer's digital certificate (i.e., the immediate
420 certificate of the host you are connected to), or a null
421 certificate, if the peer has not assigned a certificate.
422
423 The peer certificate is checked automatically during the
424 handshake phase, so this function is normally used to fetch
425 the certificate for display or for connection diagnostic
426 purposes. It contains information about the peer, including
427 its host name, the certificate issuer, and the peer's public
428 key.
429
430 Because the peer certificate is set during the handshake phase, it
431 is safe to access the peer certificate from a slot connected to
432 the QSslSocket::sslErrors() signal, QNetworkReply::sslErrors()
433 signal, or the QSslSocket::encrypted() signal.
434
435 If a null certificate is returned, it can mean the SSL handshake
436 failed, or it can mean the host you are connected to doesn't have
437 a certificate, or it can mean there is no connection.
438
439 If you want to check the peer's complete chain of certificates,
440 use peerCertificateChain() to get them all at once.
441
442 \sa peerCertificateChain(),
443 QSslSocket::sslErrors(), QSslSocket::ignoreSslErrors(),
444 QNetworkReply::sslErrors(), QNetworkReply::ignoreSslErrors()
445*/
446QSslCertificate QSslConfiguration::peerCertificate() const
447{
448 return d->peerCertificate;
449}
450
451/*!
452 Returns the peer's chain of digital certificates, starting with
453 the peer's immediate certificate and ending with the CA's
454 certificate.
455
456 Peer certificates are checked automatically during the handshake
457 phase. This function is normally used to fetch certificates for
458 display, or for performing connection diagnostics. Certificates
459 contain information about the peer and the certificate issuers,
460 including host name, issuer names, and issuer public keys.
461
462 Because the peer certificate is set during the handshake phase, it
463 is safe to access the peer certificate from a slot connected to
464 the QSslSocket::sslErrors() signal, QNetworkReply::sslErrors()
465 signal, or the QSslSocket::encrypted() signal.
466
467 If an empty list is returned, it can mean the SSL handshake
468 failed, or it can mean the host you are connected to doesn't have
469 a certificate, or it can mean there is no connection.
470
471 If you want to get only the peer's immediate certificate, use
472 peerCertificate().
473
474 \sa peerCertificate(),
475 QSslSocket::sslErrors(), QSslSocket::ignoreSslErrors(),
476 QNetworkReply::sslErrors(), QNetworkReply::ignoreSslErrors()
477*/
478QList<QSslCertificate> QSslConfiguration::peerCertificateChain() const
479{
480 return d->peerCertificateChain;
481}
482
483/*!
484 Returns the socket's cryptographic \l {QSslCipher} {cipher}, or a
485 null cipher if the connection isn't encrypted. The socket's cipher
486 for the session is set during the handshake phase. The cipher is
487 used to encrypt and decrypt data transmitted through the socket.
488
489 The SSL infrastructure also provides functions for setting the
490 ordered list of ciphers from which the handshake phase will
491 eventually select the session cipher. This ordered list must be in
492 place before the handshake phase begins.
493
494 \sa ciphers(), setCiphers(), supportedCiphers()
495*/
496QSslCipher QSslConfiguration::sessionCipher() const
497{
498 return d->sessionCipher;
499}
500
501/*!
502 Returns the socket's SSL/TLS protocol or UnknownProtocol if the
503 connection isn't encrypted. The socket's protocol for the session
504 is set during the handshake phase.
505
506 \sa protocol(), setProtocol()
507 \since 5.4
508*/
509QSsl::SslProtocol QSslConfiguration::sessionProtocol() const
510{
511 return d->sessionProtocol;
512}
513
514/*!
515 Returns the \l {QSslKey} {SSL key} assigned to this connection or
516 a null key if none has been assigned yet.
517
518 \sa setPrivateKey(), localCertificate()
519*/
520QSslKey QSslConfiguration::privateKey() const
521{
522 return d->privateKey;
523}
524
525/*!
526 Sets the connection's private \l {QSslKey} {key} to \a key. The
527 private key and the local \l {QSslCertificate} {certificate} are
528 used by clients and servers that must prove their identity to
529 SSL peers.
530
531 Both the key and the local certificate are required if you are
532 creating an SSL server socket. If you are creating an SSL client
533 socket, the key and local certificate are required if your client
534 must identify itself to an SSL server.
535
536 \sa privateKey(), setLocalCertificate()
537*/
538void QSslConfiguration::setPrivateKey(const QSslKey &key)
539{
540 d->privateKey = key;
541}
542
543/*!
544 Returns this connection's current cryptographic cipher suite. This
545 list is used during the handshake phase for choosing a
546 session cipher. The returned list of ciphers is ordered by
547 descending preference. (i.e., the first cipher in the list is the
548 most preferred cipher). The session cipher will be the first one
549 in the list that is also supported by the peer.
550
551 By default, the handshake phase can choose any of the ciphers
552 supported by this system's SSL libraries, which may vary from
553 system to system. The list of ciphers supported by this system's
554 SSL libraries is returned by supportedCiphers(). You can restrict
555 the list of ciphers used for choosing the session cipher for this
556 socket by calling setCiphers() with a subset of the supported
557 ciphers. You can revert to using the entire set by calling
558 setCiphers() with the list returned by supportedCiphers().
559
560 \sa setCiphers(), supportedCiphers()
561*/
562QList<QSslCipher> QSslConfiguration::ciphers() const
563{
564 return d->ciphers;
565}
566
567/*!
568 Sets the cryptographic cipher suite for this socket to \a ciphers,
569 which must contain a subset of the ciphers in the list returned by
570 supportedCiphers().
571
572 Restricting the cipher suite must be done before the handshake
573 phase, where the session cipher is chosen.
574
575 \sa ciphers(), supportedCiphers()
576*/
577void QSslConfiguration::setCiphers(const QList<QSslCipher> &ciphers)
578{
579 d->ciphers = ciphers;
580}
581
582/*!
583 \since 6.0
584
585 Sets the cryptographic cipher suite for this configuration to \a ciphers,
586 which is a colon-separated list of cipher suite names. The ciphers are listed
587 in order of preference, starting with the most preferred cipher.
588 Each cipher name in \a ciphers must be the name of a cipher in the
589 list returned by supportedCiphers(). Restricting the cipher suite
590 must be done before the handshake phase, where the session cipher
591 is chosen.
592
593 \note With the Schannel backend the order of the ciphers is ignored and Schannel
594 picks the most secure one during the handshake.
595
596 \sa ciphers()
597*/
598void QSslConfiguration::setCiphers(const QString &ciphers)
599{
600 auto *p = d.data();
601 p->ciphers.clear();
602 const auto cipherNames = ciphers.split(u':', Qt::SkipEmptyParts);
603 for (const QString &cipherName : cipherNames) {
604 QSslCipher cipher(cipherName);
605 if (!cipher.isNull())
606 p->ciphers << cipher;
607 }
608}
609
610/*!
611 \since 5.5
612
613 Returns the list of cryptographic ciphers supported by this
614 system. This list is set by the system's SSL libraries and may
615 vary from system to system.
616
617 \sa ciphers(), setCiphers()
618*/
619QList<QSslCipher> QSslConfiguration::supportedCiphers()
620{
621 return QSslSocketPrivate::supportedCiphers();
622}
623
624/*!
625 Returns this connection's CA certificate database. The CA certificate
626 database is used by the socket during the handshake phase to
627 validate the peer's certificate. It can be modified prior to the
628 handshake with setCaCertificates(), or with addCaCertificate() and
629 addCaCertificates().
630
631 \sa setCaCertificates(), addCaCertificate(), addCaCertificates()
632*/
633QList<QSslCertificate> QSslConfiguration::caCertificates() const
634{
635 return d->caCertificates;
636}
637
638/*!
639 Sets this socket's CA certificate database to be \a certificates.
640 The certificate database must be set prior to the SSL handshake.
641 The CA certificate database is used by the socket during the
642 handshake phase to validate the peer's certificate.
643
644 \note The default configuration uses the system CA certificate database. If
645 that is not available (as is commonly the case on iOS), the default database
646 is empty.
647
648 \sa caCertificates(), addCaCertificates(), addCaCertificate()
649*/
650void QSslConfiguration::setCaCertificates(const QList<QSslCertificate> &certificates)
651{
652 d->caCertificates = certificates;
653 d->allowRootCertOnDemandLoading = false;
654}
655
656/*!
657 \since 5.15
658
659 Searches all files in the \a path for certificates encoded in the
660 specified \a format and adds them to this socket's CA certificate
661 database. \a path must be a file or a pattern matching one or more
662 files, as specified by \a syntax. Returns \c true if one or more
663 certificates are added to the socket's CA certificate database;
664 otherwise returns \c false.
665
666 The CA certificate database is used by the socket during the
667 handshake phase to validate the peer's certificate.
668
669 For more precise control, use addCaCertificate().
670
671 \sa addCaCertificate(), QSslCertificate::fromPath()
672*/
673bool QSslConfiguration::addCaCertificates(const QString &path, QSsl::EncodingFormat format,
674 QSslCertificate::PatternSyntax syntax)
675{
676 QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax);
677 if (certs.isEmpty())
678 return false;
679
680 d->caCertificates += certs;
681 return true;
682}
683
684/*!
685 \since 5.15
686
687 Adds \a certificate to this configuration's CA certificate database.
688 The certificate database must be set prior to the SSL handshake.
689 The CA certificate database is used by the socket during the
690 handshake phase to validate the peer's certificate.
691
692 \note The default configuration uses the system CA certificate database. If
693 that is not available (as is commonly the case on iOS), the default database
694 is empty.
695
696 \sa caCertificates(), setCaCertificates(), addCaCertificates()
697*/
698void QSslConfiguration::addCaCertificate(const QSslCertificate &certificate)
699{
700 d->caCertificates += certificate;
701 d->allowRootCertOnDemandLoading = false;
702}
703
704/*!
705 \since 5.15
706
707 Adds \a certificates to this configuration's CA certificate database.
708 The certificate database must be set prior to the SSL handshake.
709 The CA certificate database is used by the socket during the
710 handshake phase to validate the peer's certificate.
711
712 \note The default configuration uses the system CA certificate database. If
713 that is not available (as is commonly the case on iOS), the default database
714 is empty.
715
716 \sa caCertificates(), setCaCertificates(), addCaCertificate()
717*/
718void QSslConfiguration::addCaCertificates(const QList<QSslCertificate> &certificates)
719{
720 d->caCertificates += certificates;
721 d->allowRootCertOnDemandLoading = false;
722}
723
724/*!
725 \since 5.5
726
727 This function provides the CA certificate database
728 provided by the operating system. The CA certificate database
729 returned by this function is used to initialize the database
730 returned by caCertificates() on the default QSslConfiguration.
731
732 \sa caCertificates(), setCaCertificates(), defaultConfiguration(),
733 addCaCertificate(), addCaCertificates()
734*/
735QList<QSslCertificate> QSslConfiguration::systemCaCertificates()
736{
737 // we are calling ensureInitialized() in the method below
738 return QSslSocketPrivate::systemCaCertificates();
739}
740
741/*!
742 Enables or disables an SSL compatibility \a option. If \a on
743 is true, the \a option is enabled. If \a on is false, the
744 \a option is disabled.
745
746 \sa testSslOption()
747*/
748void QSslConfiguration::setSslOption(QSsl::SslOption option, bool on)
749{
750 d->sslOptions.setFlag(option, on);
751}
752
753/*!
754 \since 4.8
755
756 Returns \c true if the specified SSL compatibility \a option is enabled.
757
758 \sa setSslOption()
759*/
760bool QSslConfiguration::testSslOption(QSsl::SslOption option) const
761{
762 return d->sslOptions & option;
763}
764
765/*!
766 \since 5.2
767
768 If QSsl::SslOptionDisableSessionPersistence was turned off, this
769 function returns the session ticket used in the SSL handshake in ASN.1
770 format, suitable to e.g. be persisted to disk. If no session ticket was
771 used or QSsl::SslOptionDisableSessionPersistence was not turned off,
772 this function returns an empty QByteArray.
773
774 \note When persisting the session ticket to disk or similar, be
775 careful not to expose the session to a potential attacker, as
776 knowledge of the session allows for eavesdropping on data
777 encrypted with the session parameters.
778
779 \sa setSessionTicket(), QSsl::SslOptionDisableSessionPersistence, setSslOption(), QSslSocket::newSessionTicketReceived()
780 */
781QByteArray QSslConfiguration::sessionTicket() const
782{
783 return d->sslSession;
784}
785
786/*!
787 \since 5.2
788
789 Sets the session ticket to be used in an SSL handshake.
790 QSsl::SslOptionDisableSessionPersistence must be turned off
791 for this to work, and \a sessionTicket must be in ASN.1 format
792 as returned by sessionTicket().
793
794 \sa sessionTicket(), QSsl::SslOptionDisableSessionPersistence, setSslOption(), QSslSocket::newSessionTicketReceived()
795 */
796void QSslConfiguration::setSessionTicket(const QByteArray &sessionTicket)
797{
798 d->sslSession = sessionTicket;
799}
800
801/*!
802 \since 5.2
803
804 If QSsl::SslOptionDisableSessionPersistence was turned off, this
805 function returns the session ticket life time hint sent by the
806 server (which might be 0).
807 If the server did not send a session ticket (e.g. when
808 resuming a session or when the server does not support it) or
809 QSsl::SslOptionDisableSessionPersistence was not turned off,
810 this function returns -1.
811
812 \sa sessionTicket(), QSsl::SslOptionDisableSessionPersistence, setSslOption(), QSslSocket::newSessionTicketReceived()
813 */
814int QSslConfiguration::sessionTicketLifeTimeHint() const
815{
816 return d->sslSessionTicketLifeTimeHint;
817}
818
819/*!
820 \since 5.7
821
822 Returns the ephemeral server key used for cipher algorithms
823 with forward secrecy, e.g. DHE-RSA-AES128-SHA.
824
825 The ephemeral key is only available when running in client mode, i.e.
826 QSslSocket::SslClientMode. When running in server mode or using a
827 cipher algorithm without forward secrecy a null key is returned.
828 The ephemeral server key will be set before emitting the encrypted()
829 signal.
830 */
831QSslKey QSslConfiguration::ephemeralServerKey() const
832{
833 return d->ephemeralServerKey;
834}
835
836/*!
837 \since 5.5
838
839 Returns this connection's current list of elliptic curves. This
840 list is used during the handshake phase for choosing an
841 elliptic curve (when using an elliptic curve cipher).
842 The returned list of curves is ordered by descending preference
843 (i.e., the first curve in the list is the most preferred one).
844
845 By default, the handshake phase can choose any of the curves
846 supported by this system's SSL libraries, which may vary from
847 system to system. The list of curves supported by this system's
848 SSL libraries is returned by QSslSocket::supportedEllipticCurves().
849
850 You can restrict the list of curves used for choosing the session cipher
851 for this socket by calling setEllipticCurves() with a subset of the
852 supported ciphers. You can revert to using the entire set by calling
853 setEllipticCurves() with the list returned by
854 QSslSocket::supportedEllipticCurves().
855
856 \sa setEllipticCurves
857 */
858QList<QSslEllipticCurve> QSslConfiguration::ellipticCurves() const
859{
860 return d->ellipticCurves;
861}
862
863/*!
864 \since 5.5
865
866 Sets the list of elliptic curves to be used by this socket to \a curves,
867 which must contain a subset of the curves in the list returned by
868 supportedEllipticCurves().
869
870 Restricting the elliptic curves must be done before the handshake
871 phase, where the session cipher is chosen.
872
873 \sa ellipticCurves
874 */
875void QSslConfiguration::setEllipticCurves(const QList<QSslEllipticCurve> &curves)
876{
877 d->ellipticCurves = curves;
878}
879
880/*!
881 \since 5.5
882
883 Returns the list of elliptic curves supported by this
884 system. This list is set by the system's SSL libraries and may
885 vary from system to system.
886
887 \sa ellipticCurves(), setEllipticCurves()
888*/
889QList<QSslEllipticCurve> QSslConfiguration::supportedEllipticCurves()
890{
891 return QSslSocketPrivate::supportedEllipticCurves();
892}
893
894/*!
895 \since 5.8
896
897 Returns the identity hint.
898
899 \sa setPreSharedKeyIdentityHint()
900*/
901QByteArray QSslConfiguration::preSharedKeyIdentityHint() const
902{
903 return d->preSharedKeyIdentityHint;
904}
905
906/*!
907 \since 5.8
908
909 Sets the identity hint for a preshared key authentication to \a hint. This will
910 affect the next initiated handshake; calling this function on an already-encrypted
911 socket will not affect the socket's identity hint.
912
913 The identity hint is used in QSslSocket::SslServerMode only!
914*/
915void QSslConfiguration::setPreSharedKeyIdentityHint(const QByteArray &hint)
916{
917 d->preSharedKeyIdentityHint = hint;
918}
919
920/*!
921 \since 5.8
922
923 Retrieves the current set of Diffie-Hellman parameters.
924
925 If no Diffie-Hellman parameters have been set, the QSslConfiguration object
926 defaults to using the 2048-bit MODP group from RFC 3526.
927
928 \note The default parameters may change in future Qt versions.
929 Please check the documentation of the \e{exact Qt version} that you
930 are using in order to know what defaults that version uses.
931 */
932QSslDiffieHellmanParameters QSslConfiguration::diffieHellmanParameters() const
933{
934 return d->dhParams;
935}
936
937/*!
938 \since 5.8
939
940 Sets a custom set of Diffie-Hellman parameters to be used by this socket when functioning as
941 a server to \a dhparams.
942
943 If no Diffie-Hellman parameters have been set, the QSslConfiguration object
944 defaults to using the 2048-bit MODP group from RFC 3526.
945
946 Since 6.7 you can provide an empty Diffie-Hellman parameter to use auto selection
947 (see SSL_CTX_set_dh_auto of openssl) if the tls backend supports it.
948
949 \note The default parameters may change in future Qt versions.
950 Please check the documentation of the \e{exact Qt version} that you
951 are using in order to know what defaults that version uses.
952 */
953void QSslConfiguration::setDiffieHellmanParameters(const QSslDiffieHellmanParameters &dhparams)
954{
955 d->dhParams = dhparams;
956}
957
958/*!
959 \since 5.11
960
961 Returns the backend-specific configuration.
962
963 Only options set by setBackendConfigurationOption() or setBackendConfiguration() will be
964 returned. The internal standard configuration of the backend is not reported.
965
966 \sa setBackendConfigurationOption(), setBackendConfiguration()
967 */
968QMap<QByteArray, QVariant> QSslConfiguration::backendConfiguration() const
969{
970 return d->backendConfig;
971}
972
973/*!
974 \since 5.11
975
976 Sets the option \a name in the backend-specific configuration to \a value.
977
978 Options supported by the OpenSSL (>= 1.0.2) backend are available in the \l
979 {https://www.openssl.org/docs/manmaster/man3/SSL_CONF_cmd.html#SUPPORTED-CONFIGURATION-FILE-COMMANDS}
980 {supported configuration file commands} documentation. The expected type for
981 the \a value parameter is a QByteArray for all options. The \l
982 {https://www.openssl.org/docs/manmaster/man3/SSL_CONF_cmd.html#EXAMPLES}{examples}
983 show how to use some of the options.
984
985 \note The backend-specific configuration will be applied after the general
986 configuration. Using the backend-specific configuration to set a general
987 configuration option again will overwrite the general configuration option.
988
989 \sa backendConfiguration(), setBackendConfiguration()
990 */
991void QSslConfiguration::setBackendConfigurationOption(const QByteArray &name, const QVariant &value)
992{
993 d->backendConfig[name] = value;
994}
995
996/*!
997 \since 5.11
998
999 Sets or clears the backend-specific configuration.
1000
1001 Without a \a backendConfiguration parameter this function will clear the
1002 backend-specific configuration. More information about the supported
1003 options is available in the documentation of setBackendConfigurationOption().
1004
1005 \sa backendConfiguration(), setBackendConfigurationOption()
1006 */
1007void QSslConfiguration::setBackendConfiguration(const QMap<QByteArray, QVariant> &backendConfiguration)
1008{
1009 d->backendConfig = backendConfiguration;
1010}
1011
1012/*!
1013 \since 5.3
1014
1015 This function returns the protocol negotiated with the server
1016 if the Next Protocol Negotiation (NPN) or Application-Layer Protocol
1017 Negotiation (ALPN) TLS extension was enabled.
1018 In order for the NPN/ALPN extension to be enabled, setAllowedNextProtocols()
1019 needs to be called explicitly before connecting to the server.
1020
1021 If no protocol could be negotiated or the extension was not enabled,
1022 this function returns a QByteArray which is null.
1023
1024 \sa setAllowedNextProtocols(), nextProtocolNegotiationStatus()
1025 */
1026QByteArray QSslConfiguration::nextNegotiatedProtocol() const
1027{
1028 return d->nextNegotiatedProtocol;
1029}
1030
1031/*!
1032 \since 5.3
1033
1034 This function sets the allowed \a protocols to be negotiated with the
1035 server through the Next Protocol Negotiation (NPN) or Application-Layer
1036 Protocol Negotiation (ALPN) TLS extension; each
1037 element in \a protocols must define one allowed protocol.
1038 The function must be called explicitly before connecting to send the NPN/ALPN
1039 extension in the SSL handshake.
1040 Whether or not the negotiation succeeded can be queried through
1041 nextProtocolNegotiationStatus().
1042
1043 \sa nextNegotiatedProtocol(), nextProtocolNegotiationStatus(), allowedNextProtocols(), QSslConfiguration::NextProtocolHttp1_1
1044 */
1045void QSslConfiguration::setAllowedNextProtocols(const QList<QByteArray> &protocols)
1046{
1047 d->nextAllowedProtocols = protocols;
1048}
1049
1050/*!
1051 \since 5.3
1052
1053 This function returns the allowed protocols to be negotiated with the
1054 server through the Next Protocol Negotiation (NPN) or Application-Layer
1055 Protocol Negotiation (ALPN) TLS extension, as set by setAllowedNextProtocols().
1056
1057 \sa nextNegotiatedProtocol(), nextProtocolNegotiationStatus(), setAllowedNextProtocols(), QSslConfiguration::NextProtocolHttp1_1
1058 */
1059QList<QByteArray> QSslConfiguration::allowedNextProtocols() const
1060{
1061 return d->nextAllowedProtocols;
1062}
1063
1064/*!
1065 \since 5.3
1066
1067 This function returns the status of the Next Protocol Negotiation (NPN)
1068 or Application-Layer Protocol Negotiation (ALPN).
1069 If the feature has not been enabled through setAllowedNextProtocols(),
1070 this function returns NextProtocolNegotiationNone.
1071 The status will be set before emitting the encrypted() signal.
1072
1073 \sa setAllowedNextProtocols(), allowedNextProtocols(), nextNegotiatedProtocol(), QSslConfiguration::NextProtocolNegotiationStatus
1074 */
1075QSslConfiguration::NextProtocolNegotiationStatus QSslConfiguration::nextProtocolNegotiationStatus() const
1076{
1077 return d->nextProtocolNegotiationStatus;
1078}
1079
1080/*!
1081 Returns the default SSL configuration to be used in new SSL
1082 connections.
1083
1084 The default SSL configuration consists of:
1085
1086 \list
1087 \li no local certificate and no private key
1088 \li protocol \l{QSsl::SecureProtocols}{SecureProtocols}
1089 \li the system's default CA certificate list
1090 \li the cipher list equal to the list of the SSL libraries'
1091 supported SSL ciphers that are 128 bits or more
1092 \endlist
1093
1094 \sa supportedCiphers(), setDefaultConfiguration()
1095*/
1096QSslConfiguration QSslConfiguration::defaultConfiguration()
1097{
1098 return QSslConfigurationPrivate::defaultConfiguration();
1099}
1100
1101/*!
1102 Sets the default SSL configuration to be used in new SSL
1103 connections to be \a configuration. Existing connections are not
1104 affected by this call.
1105
1106 \sa supportedCiphers(), defaultConfiguration()
1107*/
1108void QSslConfiguration::setDefaultConfiguration(const QSslConfiguration &configuration)
1109{
1110 QSslConfigurationPrivate::setDefaultConfiguration(configuration);
1111}
1112
1113#if QT_CONFIG(dtls) || defined(Q_QDOC)
1114
1115/*!
1116 This function returns true if DTLS cookie verification was enabled on a
1117 server-side socket.
1118
1119 \sa setDtlsCookieVerificationEnabled()
1120 */
1121bool QSslConfiguration::dtlsCookieVerificationEnabled() const
1122{
1123 return d->dtlsCookieEnabled;
1124}
1125
1126/*!
1127 This function enables DTLS cookie verification when \a enable is true.
1128
1129 \sa dtlsCookieVerificationEnabled()
1130 */
1131void QSslConfiguration::setDtlsCookieVerificationEnabled(bool enable)
1132{
1133 d->dtlsCookieEnabled = enable;
1134}
1135
1136/*!
1137 Returns the default DTLS configuration to be used in new DTLS
1138 connections.
1139
1140 The default DTLS configuration consists of:
1141
1142 \list
1143 \li no local certificate and no private key
1144 \li protocol DtlsV1_2OrLater
1145 \li the system's default CA certificate list
1146 \li the cipher list equal to the list of the SSL libraries'
1147 supported TLS 1.2 ciphers that use 128 or more secret bits
1148 for the cipher.
1149 \endlist
1150
1151 \sa setDefaultDtlsConfiguration()
1152*/
1153QSslConfiguration QSslConfiguration::defaultDtlsConfiguration()
1154{
1155 return QSslConfigurationPrivate::defaultDtlsConfiguration();
1156}
1157
1158/*!
1159 Sets the default DTLS configuration to be used in new DTLS
1160 connections to be \a configuration. Existing connections are not
1161 affected by this call.
1162
1163 \sa defaultDtlsConfiguration()
1164*/
1165void QSslConfiguration::setDefaultDtlsConfiguration(const QSslConfiguration &configuration)
1166{
1167 QSslConfigurationPrivate::setDefaultDtlsConfiguration(configuration);
1168}
1169
1170#endif // dtls
1171
1172/*!
1173 \since 5.13
1174 If \a enabled is true, client QSslSocket will send a certificate status request
1175 to its peer when initiating a handshake. During the handshake QSslSocket will
1176 verify the server's response. This value must be set before the handshake
1177 starts.
1178
1179 \sa ocspStaplingEnabled()
1180*/
1181void QSslConfiguration::setOcspStaplingEnabled(bool enabled)
1182{
1183#if QT_CONFIG(ocsp)
1184 d->ocspStaplingEnabled = enabled;
1185#else
1186 if (enabled)
1187 qCWarning(lcSsl, "Enabling OCSP-stapling requires the feature 'ocsp'");
1188#endif // ocsp
1189}
1190
1191/*!
1192 \since 5.13
1193 Returns true if OCSP stapling was enabled by setOCSPStaplingEnabled(),
1194 otherwise false (which is the default value).
1195
1196 \sa setOcspStaplingEnabled()
1197*/
1198bool QSslConfiguration::ocspStaplingEnabled() const
1199{
1200 return d->ocspStaplingEnabled;
1201}
1202
1203/*!
1204 \since 6.0
1205
1206 Returns true if a verification callback will emit QSslSocket::handshakeInterruptedOnError()
1207 early, before concluding the handshake.
1208
1209 \note This function always returns false for all backends but OpenSSL.
1210
1211 \sa setHandshakeMustInterruptOnError(), QSslSocket::handshakeInterruptedOnError(), QSslSocket::continueInterruptedHandshake()
1212*/
1213bool QSslConfiguration::handshakeMustInterruptOnError() const
1214{
1215 return d->reportFromCallback;
1216}
1217
1218/*!
1219 \since 6.0
1220
1221 If \a interrupt is true and the underlying backend supports this option,
1222 errors found during certificate verification are reported immediately
1223 by emitting QSslSocket::handshakeInterruptedOnError(). This allows
1224 to stop the unfinished handshake and send a proper alert message to
1225 a peer. No special action is required from the application in this case.
1226 QSslSocket will close the connection after sending the alert message.
1227 If the application after inspecting the error wants to continue the
1228 handshake, it must call QSslSocket::continueInterruptedHandshake()
1229 from its slot function. The signal-slot connection must be direct.
1230
1231 \note When interrupting handshake is enabled, errors that would otherwise
1232 be reported by QSslSocket::peerVerifyError() are instead only reported by
1233 QSslSocket::handshakeInterruptedOnError().
1234 \note Even if the handshake was continued, these errors will be
1235 reported when emitting QSslSocket::sslErrors() signal (and thus must
1236 be ignored in the corresponding function slot).
1237
1238 \sa handshakeMustInterruptOnError(), QSslSocket::handshakeInterruptedOnError(), QSslSocket::continueInterruptedHandshake()
1239*/
1240void QSslConfiguration::setHandshakeMustInterruptOnError(bool interrupt)
1241{
1242#if QT_CONFIG(openssl)
1243 d->reportFromCallback = interrupt;
1244#else
1245 Q_UNUSED(interrupt);
1246 qCWarning(lcSsl, "This operation requires OpenSSL as TLS backend");
1247#endif
1248}
1249
1250/*!
1251 \since 6.12
1252
1253 Returns the keying material configuration for this SSL connection.
1254
1255 Keying material allows an application to derive additional
1256 cryptographic material from an established TLS session, using the
1257 TLS exporter mechanism as defined in RFC 5705 and RFC 8446.
1258
1259 Each entry in the returned list describes a request for exported
1260 keying material, including the exporter label, optional context,
1261 and the desired length of the exported data. The actual keying
1262 material becomes available only after a successful handshake.
1263
1264 Exported keying material is bound to the specific TLS session and
1265 cryptographically independent from the session keys used for
1266 encryption. It can be used for application-specific purposes such
1267 as channel binding or key derivation for protocols layered on top
1268 of TLS.
1269
1270 If no keying material configuration was set, this function returns
1271 an empty list.
1272
1273 \note The availability of keying material depends on the TLS
1274 backend. Currently, this feature is supported only when using
1275 OpenSSL.
1276
1277 \warning Exported keying material must be handled with care.
1278 Improper use may compromise the security of the TLS
1279 session or higher-level protocols.
1280
1281 \sa setKeyingMaterial()
1282*/
1283QList<QSslKeyingMaterial> QSslConfiguration::keyingMaterial() const
1284{
1285 return d->keyingMaterial;
1286}
1287
1288/*!
1289 \since 6.12
1290
1291 Returns the keying material configuration for this SSL connection
1292 of given object.
1293
1294 \sa keyingMaterial()
1295*/
1296std::optional<QSslKeyingMaterial>
1297QSslConfiguration::keyingMaterial(const QSslKeyingMaterial &material) const
1298{
1299 for (const auto &entry : std::as_const(d->keyingMaterial)) {
1300 if (entry.label() == material.label() &&
1301 entry.context() == material.context() &&
1302 entry.size() == material.size()) {
1303 return entry;
1304 }
1305 }
1306
1307 return std::nullopt;
1308}
1309
1310/*!
1311 \since 6.12
1312
1313 Sets the keying material configuration for this SSL connection to
1314 \a keyMaterial.
1315
1316 This enables the TLS exporter mechanism, allowing the application
1317 to derive additional cryptographic material from the negotiated TLS
1318 session. The exporter is defined in RFC 5705 and RFC 8446.
1319
1320 The configuration must be set before the TLS handshake starts.
1321 Calling this function on an already encrypted connection has no
1322 effect.
1323
1324 Each QSslKeyingMaterial entry specifies the exporter label, optional
1325 context value, and the length of the keying material to be derived.
1326 After a successful handshake, the requested keying material can be
1327 retrieved from the SSL connection.
1328
1329 Exported keying material is unique to the TLS session and can be
1330 safely used to derive keys for application-level protocols that
1331 require cryptographic binding to the underlying TLS channel.
1332
1333 \note This feature is backend-specific and currently requires
1334 OpenSSL support.
1335
1336 \warning Applications are responsible for defining unique and
1337 collision-resistant exporter labels. Reusing labels or
1338 contexts across different purposes may lead to subtle
1339 security vulnerabilities.
1340
1341 \sa keyingMaterial()
1342*/
1343void QSslConfiguration::setKeyingMaterial(const QList<QSslKeyingMaterial> &keyMaterial)
1344{
1345 d->keyingMaterial = keyMaterial;
1346}
1347
1348/*!
1349 \since 6.0
1350
1351 Returns true if errors with code QSslError::NoPeerCertificate
1352 cannot be ignored.
1353
1354 \note Always returns false for all TLS backends but OpenSSL.
1355
1356 \sa QSslSocket::ignoreSslErrors(), setMissingCertificateIsFatal()
1357*/
1358bool QSslConfiguration::missingCertificateIsFatal() const
1359{
1360 return d->missingCertIsFatal;
1361}
1362
1363/*!
1364 \since 6.0
1365
1366 If \a cannotRecover is true, and verification mode in use is
1367 QSslSocket::VerifyPeer or QSslSocket::AutoVerifyPeer (for a
1368 client-side socket), the missing peer's certificate would be
1369 treated as an unrecoverable error that cannot be ignored. A proper
1370 alert message will be sent to the peer before closing the connection.
1371
1372 \note Only available if Qt was configured and built with OpenSSL backend.
1373
1374 \sa QSslSocket::ignoreSslErrors(), QSslSocket::PeerVerifyMode, missingCertificateIsFatal()
1375*/
1376void QSslConfiguration::setMissingCertificateIsFatal(bool cannotRecover)
1377{
1378#if QT_CONFIG(openssl)
1379 d->missingCertIsFatal = cannotRecover;
1380#else
1381 Q_UNUSED(cannotRecover);
1382 qCWarning(lcSsl, "Handling a missing certificate as a fatal error requires an OpenSSL backend");
1383#endif // openssl
1384}
1385
1386/*! \internal
1387*/
1388bool QSslConfigurationPrivate::peerSessionWasShared(const QSslConfiguration &configuration) {
1389 return configuration.d->peerSessionShared;
1390 }
1391
1392QT_END_NAMESPACE
Combined button and popup list for selecting options.