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->dtlsCookieEnabled == other.d->dtlsCookieEnabled &&
196 d->ocspStaplingEnabled == other.d->ocspStaplingEnabled &&
197 d->reportFromCallback == other.d->reportFromCallback &&
198 d->missingCertIsFatal == other.d->missingCertIsFatal;
199}
200
201/*!
202 \fn QSslConfiguration::operator!=(const QSslConfiguration &other) const
203
204 Returns \c true if this QSslConfiguration differs from \a other. Two
205 QSslConfiguration objects are considered different if any state or
206 setting is different.
207
208 \sa operator==()
209*/
210
211/*!
212 Returns \c true if this is a null QSslConfiguration object.
213
214 A QSslConfiguration object is null if it has been
215 default-constructed and no setter methods have been called.
216
217 \sa setProtocol(), setLocalCertificate(), setPrivateKey(),
218 setCiphers(), setCaCertificates()
219*/
220bool QSslConfiguration::isNull() const
221{
222 return (d->protocol == QSsl::SecureProtocols &&
223 d->peerVerifyMode == QSslSocket::AutoVerifyPeer &&
224 d->peerVerifyDepth == 0 &&
225 d->allowRootCertOnDemandLoading == true &&
226 d->caCertificates.size() == 0 &&
227 d->ciphers.size() == 0 &&
228 d->ellipticCurves.isEmpty() &&
229 d->ephemeralServerKey.isNull() &&
230 d->dhParams == QSslDiffieHellmanParameters::defaultParameters() &&
231 d->localCertificateChain.isEmpty() &&
232 d->privateKey.isNull() &&
233 d->peerCertificate.isNull() &&
234 d->peerCertificateChain.size() == 0 &&
235 d->backendConfig.isEmpty() &&
236 d->sslOptions == QSslConfigurationPrivate::defaultSslOptions &&
237 d->sslSession.isNull() &&
238 d->sslSessionTicketLifeTimeHint == -1 &&
239 d->preSharedKeyIdentityHint.isNull() &&
240 d->nextAllowedProtocols.isEmpty() &&
241 d->nextNegotiatedProtocol.isNull() &&
242 d->nextProtocolNegotiationStatus == QSslConfiguration::NextProtocolNegotiationNone &&
243 d->ocspStaplingEnabled == false &&
244 d->reportFromCallback == false &&
245 d->missingCertIsFatal == false);
246}
247
248/*!
249 Returns the protocol setting for this SSL configuration.
250
251 \sa setProtocol()
252*/
253QSsl::SslProtocol QSslConfiguration::protocol() const
254{
255 return d->protocol;
256}
257
258/*!
259 Sets the protocol setting for this configuration to be \a
260 protocol.
261
262 Setting the protocol once the connection has already been
263 established has no effect.
264
265 \sa protocol()
266*/
267void QSslConfiguration::setProtocol(QSsl::SslProtocol protocol)
268{
269 d->protocol = protocol;
270}
271
272/*!
273 Returns the verify mode. This mode decides whether QSslSocket should
274 request a certificate from the peer (i.e., the client requests a
275 certificate from the server, or a server requesting a certificate from the
276 client), and whether it should require that this certificate is valid.
277
278 The default mode is AutoVerifyPeer, which tells QSslSocket to use
279 VerifyPeer for clients, QueryPeer for servers.
280
281 \sa setPeerVerifyMode()
282*/
283QSslSocket::PeerVerifyMode QSslConfiguration::peerVerifyMode() const
284{
285 return d->peerVerifyMode;
286}
287
288/*!
289 Sets the verify mode to \a mode. This mode decides whether QSslSocket
290 should request a certificate from the peer (i.e., the client requests a
291 certificate from the server, or a server requesting a certificate from the
292 client), and whether it should require that this certificate is valid.
293
294 The default mode is AutoVerifyPeer, which tells QSslSocket to use
295 VerifyPeer for clients, QueryPeer for servers.
296
297 \sa peerVerifyMode()
298*/
299void QSslConfiguration::setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
300{
301 d->peerVerifyMode = mode;
302}
303
304
305/*!
306 Returns the maximum number of certificates in the peer's certificate chain
307 to be checked during the SSL handshake phase, or 0 (the default) if no
308 maximum depth has been set, indicating that the whole certificate chain
309 should be checked.
310
311 The certificates are checked in issuing order, starting with the peer's
312 own certificate, then its issuer's certificate, and so on.
313
314 \sa setPeerVerifyDepth(), peerVerifyMode()
315*/
316int QSslConfiguration::peerVerifyDepth() const
317{
318 return d->peerVerifyDepth;
319}
320
321/*!
322 Sets the maximum number of certificates in the peer's certificate chain to
323 be checked during the SSL handshake phase, to \a depth. Setting a depth of
324 0 means that no maximum depth is set, indicating that the whole
325 certificate chain should be checked.
326
327 The certificates are checked in issuing order, starting with the peer's
328 own certificate, then its issuer's certificate, and so on.
329
330 \sa peerVerifyDepth(), setPeerVerifyMode()
331*/
332void QSslConfiguration::setPeerVerifyDepth(int depth)
333{
334 if (depth < 0) {
335 qCWarning(lcSsl,
336 "QSslConfiguration::setPeerVerifyDepth: cannot set negative depth of %d", depth);
337 return;
338 }
339 d->peerVerifyDepth = depth;
340}
341
342/*!
343 Returns the certificate chain to be presented to the peer during
344 the SSL handshake process.
345
346 \sa localCertificate()
347 \since 5.1
348*/
349QList<QSslCertificate> QSslConfiguration::localCertificateChain() const
350{
351 return d->localCertificateChain;
352}
353
354/*!
355 Sets the certificate chain to be presented to the peer during the
356 SSL handshake to be \a localChain.
357
358 Setting the certificate chain once the connection has been
359 established has no effect.
360
361 A certificate is the means of identification used in the SSL
362 process. The local certificate is used by the remote end to verify
363 the local user's identity against its list of Certification
364 Authorities. In most cases, such as in HTTP web browsing, only
365 servers identify to the clients, so the client does not send a
366 certificate.
367
368 Unlike QSslConfiguration::setLocalCertificate() this method allows
369 you to specify any intermediate certificates required in order to
370 validate your certificate. The first item in the list must be the
371 leaf certificate.
372
373 \sa localCertificateChain()
374 \since 5.1
375 */
376void QSslConfiguration::setLocalCertificateChain(const QList<QSslCertificate> &localChain)
377{
378 d->localCertificateChain = localChain;
379}
380
381/*!
382 Returns the certificate to be presented to the peer during the SSL
383 handshake process.
384
385 \sa setLocalCertificate()
386*/
387QSslCertificate QSslConfiguration::localCertificate() const
388{
389 if (d->localCertificateChain.isEmpty())
390 return QSslCertificate();
391 return d->localCertificateChain[0];
392}
393
394/*!
395 Sets the certificate to be presented to the peer during SSL
396 handshake to be \a certificate.
397
398 Setting the certificate once the connection has been established
399 has no effect.
400
401 A certificate is the means of identification used in the SSL
402 process. The local certificate is used by the remote end to verify
403 the local user's identity against its list of Certification
404 Authorities. In most cases, such as in HTTP web browsing, only
405 servers identify to the clients, so the client does not send a
406 certificate.
407
408 \sa localCertificate()
409*/
410void QSslConfiguration::setLocalCertificate(const QSslCertificate &certificate)
411{
412 d->localCertificateChain = QList<QSslCertificate>();
413 d->localCertificateChain += certificate;
414}
415
416/*!
417 Returns the peer's digital certificate (i.e., the immediate
418 certificate of the host you are connected to), or a null
419 certificate, if the peer has not assigned a certificate.
420
421 The peer certificate is checked automatically during the
422 handshake phase, so this function is normally used to fetch
423 the certificate for display or for connection diagnostic
424 purposes. It contains information about the peer, including
425 its host name, the certificate issuer, and the peer's public
426 key.
427
428 Because the peer certificate is set during the handshake phase, it
429 is safe to access the peer certificate from a slot connected to
430 the QSslSocket::sslErrors() signal, QNetworkReply::sslErrors()
431 signal, or the QSslSocket::encrypted() signal.
432
433 If a null certificate is returned, it can mean the SSL handshake
434 failed, or it can mean the host you are connected to doesn't have
435 a certificate, or it can mean there is no connection.
436
437 If you want to check the peer's complete chain of certificates,
438 use peerCertificateChain() to get them all at once.
439
440 \sa peerCertificateChain(),
441 QSslSocket::sslErrors(), QSslSocket::ignoreSslErrors(),
442 QNetworkReply::sslErrors(), QNetworkReply::ignoreSslErrors()
443*/
444QSslCertificate QSslConfiguration::peerCertificate() const
445{
446 return d->peerCertificate;
447}
448
449/*!
450 Returns the peer's chain of digital certificates, starting with
451 the peer's immediate certificate and ending with the CA's
452 certificate.
453
454 Peer certificates are checked automatically during the handshake
455 phase. This function is normally used to fetch certificates for
456 display, or for performing connection diagnostics. Certificates
457 contain information about the peer and the certificate issuers,
458 including host name, issuer names, and issuer public keys.
459
460 Because the peer certificate is set during the handshake phase, it
461 is safe to access the peer certificate from a slot connected to
462 the QSslSocket::sslErrors() signal, QNetworkReply::sslErrors()
463 signal, or the QSslSocket::encrypted() signal.
464
465 If an empty list is returned, it can mean the SSL handshake
466 failed, or it can mean the host you are connected to doesn't have
467 a certificate, or it can mean there is no connection.
468
469 If you want to get only the peer's immediate certificate, use
470 peerCertificate().
471
472 \sa peerCertificate(),
473 QSslSocket::sslErrors(), QSslSocket::ignoreSslErrors(),
474 QNetworkReply::sslErrors(), QNetworkReply::ignoreSslErrors()
475*/
476QList<QSslCertificate> QSslConfiguration::peerCertificateChain() const
477{
478 return d->peerCertificateChain;
479}
480
481/*!
482 Returns the socket's cryptographic \l {QSslCipher} {cipher}, or a
483 null cipher if the connection isn't encrypted. The socket's cipher
484 for the session is set during the handshake phase. The cipher is
485 used to encrypt and decrypt data transmitted through the socket.
486
487 The SSL infrastructure also provides functions for setting the
488 ordered list of ciphers from which the handshake phase will
489 eventually select the session cipher. This ordered list must be in
490 place before the handshake phase begins.
491
492 \sa ciphers(), setCiphers(), supportedCiphers()
493*/
494QSslCipher QSslConfiguration::sessionCipher() const
495{
496 return d->sessionCipher;
497}
498
499/*!
500 Returns the socket's SSL/TLS protocol or UnknownProtocol if the
501 connection isn't encrypted. The socket's protocol for the session
502 is set during the handshake phase.
503
504 \sa protocol(), setProtocol()
505 \since 5.4
506*/
507QSsl::SslProtocol QSslConfiguration::sessionProtocol() const
508{
509 return d->sessionProtocol;
510}
511
512/*!
513 Returns the \l {QSslKey} {SSL key} assigned to this connection or
514 a null key if none has been assigned yet.
515
516 \sa setPrivateKey(), localCertificate()
517*/
518QSslKey QSslConfiguration::privateKey() const
519{
520 return d->privateKey;
521}
522
523/*!
524 Sets the connection's private \l {QSslKey} {key} to \a key. The
525 private key and the local \l {QSslCertificate} {certificate} are
526 used by clients and servers that must prove their identity to
527 SSL peers.
528
529 Both the key and the local certificate are required if you are
530 creating an SSL server socket. If you are creating an SSL client
531 socket, the key and local certificate are required if your client
532 must identify itself to an SSL server.
533
534 \sa privateKey(), setLocalCertificate()
535*/
536void QSslConfiguration::setPrivateKey(const QSslKey &key)
537{
538 d->privateKey = key;
539}
540
541/*!
542 Returns this connection's current cryptographic cipher suite. This
543 list is used during the handshake phase for choosing a
544 session cipher. The returned list of ciphers is ordered by
545 descending preference. (i.e., the first cipher in the list is the
546 most preferred cipher). The session cipher will be the first one
547 in the list that is also supported by the peer.
548
549 By default, the handshake phase can choose any of the ciphers
550 supported by this system's SSL libraries, which may vary from
551 system to system. The list of ciphers supported by this system's
552 SSL libraries is returned by supportedCiphers(). You can restrict
553 the list of ciphers used for choosing the session cipher for this
554 socket by calling setCiphers() with a subset of the supported
555 ciphers. You can revert to using the entire set by calling
556 setCiphers() with the list returned by supportedCiphers().
557
558 \sa setCiphers(), supportedCiphers()
559*/
560QList<QSslCipher> QSslConfiguration::ciphers() const
561{
562 return d->ciphers;
563}
564
565/*!
566 Sets the cryptographic cipher suite for this socket to \a ciphers,
567 which must contain a subset of the ciphers in the list returned by
568 supportedCiphers().
569
570 Restricting the cipher suite must be done before the handshake
571 phase, where the session cipher is chosen.
572
573 \sa ciphers(), supportedCiphers()
574*/
575void QSslConfiguration::setCiphers(const QList<QSslCipher> &ciphers)
576{
577 d->ciphers = ciphers;
578}
579
580/*!
581 \since 6.0
582
583 Sets the cryptographic cipher suite for this configuration to \a ciphers,
584 which is a colon-separated list of cipher suite names. The ciphers are listed
585 in order of preference, starting with the most preferred cipher.
586 Each cipher name in \a ciphers must be the name of a cipher in the
587 list returned by supportedCiphers(). Restricting the cipher suite
588 must be done before the handshake phase, where the session cipher
589 is chosen.
590
591 \note With the Schannel backend the order of the ciphers is ignored and Schannel
592 picks the most secure one during the handshake.
593
594 \sa ciphers()
595*/
596void QSslConfiguration::setCiphers(const QString &ciphers)
597{
598 auto *p = d.data();
599 p->ciphers.clear();
600 const auto cipherNames = ciphers.split(u':', Qt::SkipEmptyParts);
601 for (const QString &cipherName : cipherNames) {
602 QSslCipher cipher(cipherName);
603 if (!cipher.isNull())
604 p->ciphers << cipher;
605 }
606}
607
608/*!
609 \since 5.5
610
611 Returns the list of cryptographic ciphers supported by this
612 system. This list is set by the system's SSL libraries and may
613 vary from system to system.
614
615 \sa ciphers(), setCiphers()
616*/
617QList<QSslCipher> QSslConfiguration::supportedCiphers()
618{
619 return QSslSocketPrivate::supportedCiphers();
620}
621
622/*!
623 Returns this connection's CA certificate database. The CA certificate
624 database is used by the socket during the handshake phase to
625 validate the peer's certificate. It can be modified prior to the
626 handshake with setCaCertificates(), or with addCaCertificate() and
627 addCaCertificates().
628
629 \sa setCaCertificates(), addCaCertificate(), addCaCertificates()
630*/
631QList<QSslCertificate> QSslConfiguration::caCertificates() const
632{
633 return d->caCertificates;
634}
635
636/*!
637 Sets this socket's CA certificate database to be \a certificates.
638 The certificate database must be set prior to the SSL handshake.
639 The CA certificate database is used by the socket during the
640 handshake phase to validate the peer's certificate.
641
642 \note The default configuration uses the system CA certificate database. If
643 that is not available (as is commonly the case on iOS), the default database
644 is empty.
645
646 \sa caCertificates(), addCaCertificates(), addCaCertificate()
647*/
648void QSslConfiguration::setCaCertificates(const QList<QSslCertificate> &certificates)
649{
650 d->caCertificates = certificates;
651 d->allowRootCertOnDemandLoading = false;
652}
653
654/*!
655 \since 5.15
656
657 Searches all files in the \a path for certificates encoded in the
658 specified \a format and adds them to this socket's CA certificate
659 database. \a path must be a file or a pattern matching one or more
660 files, as specified by \a syntax. Returns \c true if one or more
661 certificates are added to the socket's CA certificate database;
662 otherwise returns \c false.
663
664 The CA certificate database is used by the socket during the
665 handshake phase to validate the peer's certificate.
666
667 For more precise control, use addCaCertificate().
668
669 \sa addCaCertificate(), QSslCertificate::fromPath()
670*/
671bool QSslConfiguration::addCaCertificates(const QString &path, QSsl::EncodingFormat format,
672 QSslCertificate::PatternSyntax syntax)
673{
674 QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax);
675 if (certs.isEmpty())
676 return false;
677
678 d->caCertificates += certs;
679 return true;
680}
681
682/*!
683 \since 5.15
684
685 Adds \a certificate to this configuration's CA certificate database.
686 The certificate database must be set prior to the SSL handshake.
687 The CA certificate database is used by the socket during the
688 handshake phase to validate the peer's certificate.
689
690 \note The default configuration uses the system CA certificate database. If
691 that is not available (as is commonly the case on iOS), the default database
692 is empty.
693
694 \sa caCertificates(), setCaCertificates(), addCaCertificates()
695*/
696void QSslConfiguration::addCaCertificate(const QSslCertificate &certificate)
697{
698 d->caCertificates += certificate;
699 d->allowRootCertOnDemandLoading = false;
700}
701
702/*!
703 \since 5.15
704
705 Adds \a certificates to this configuration's CA certificate database.
706 The certificate database must be set prior to the SSL handshake.
707 The CA certificate database is used by the socket during the
708 handshake phase to validate the peer's certificate.
709
710 \note The default configuration uses the system CA certificate database. If
711 that is not available (as is commonly the case on iOS), the default database
712 is empty.
713
714 \sa caCertificates(), setCaCertificates(), addCaCertificate()
715*/
716void QSslConfiguration::addCaCertificates(const QList<QSslCertificate> &certificates)
717{
718 d->caCertificates += certificates;
719 d->allowRootCertOnDemandLoading = false;
720}
721
722/*!
723 \since 5.5
724
725 This function provides the CA certificate database
726 provided by the operating system. The CA certificate database
727 returned by this function is used to initialize the database
728 returned by caCertificates() on the default QSslConfiguration.
729
730 \sa caCertificates(), setCaCertificates(), defaultConfiguration(),
731 addCaCertificate(), addCaCertificates()
732*/
733QList<QSslCertificate> QSslConfiguration::systemCaCertificates()
734{
735 // we are calling ensureInitialized() in the method below
736 return QSslSocketPrivate::systemCaCertificates();
737}
738
739/*!
740 Enables or disables an SSL compatibility \a option. If \a on
741 is true, the \a option is enabled. If \a on is false, the
742 \a option is disabled.
743
744 \sa testSslOption()
745*/
746void QSslConfiguration::setSslOption(QSsl::SslOption option, bool on)
747{
748 d->sslOptions.setFlag(option, on);
749}
750
751/*!
752 \since 4.8
753
754 Returns \c true if the specified SSL compatibility \a option is enabled.
755
756 \sa setSslOption()
757*/
758bool QSslConfiguration::testSslOption(QSsl::SslOption option) const
759{
760 return d->sslOptions & option;
761}
762
763/*!
764 \since 5.2
765
766 If QSsl::SslOptionDisableSessionPersistence was turned off, this
767 function returns the session ticket used in the SSL handshake in ASN.1
768 format, suitable to e.g. be persisted to disk. If no session ticket was
769 used or QSsl::SslOptionDisableSessionPersistence was not turned off,
770 this function returns an empty QByteArray.
771
772 \note When persisting the session ticket to disk or similar, be
773 careful not to expose the session to a potential attacker, as
774 knowledge of the session allows for eavesdropping on data
775 encrypted with the session parameters.
776
777 \sa setSessionTicket(), QSsl::SslOptionDisableSessionPersistence, setSslOption(), QSslSocket::newSessionTicketReceived()
778 */
779QByteArray QSslConfiguration::sessionTicket() const
780{
781 return d->sslSession;
782}
783
784/*!
785 \since 5.2
786
787 Sets the session ticket to be used in an SSL handshake.
788 QSsl::SslOptionDisableSessionPersistence must be turned off
789 for this to work, and \a sessionTicket must be in ASN.1 format
790 as returned by sessionTicket().
791
792 \sa sessionTicket(), QSsl::SslOptionDisableSessionPersistence, setSslOption(), QSslSocket::newSessionTicketReceived()
793 */
794void QSslConfiguration::setSessionTicket(const QByteArray &sessionTicket)
795{
796 d->sslSession = sessionTicket;
797}
798
799/*!
800 \since 5.2
801
802 If QSsl::SslOptionDisableSessionPersistence was turned off, this
803 function returns the session ticket life time hint sent by the
804 server (which might be 0).
805 If the server did not send a session ticket (e.g. when
806 resuming a session or when the server does not support it) or
807 QSsl::SslOptionDisableSessionPersistence was not turned off,
808 this function returns -1.
809
810 \sa sessionTicket(), QSsl::SslOptionDisableSessionPersistence, setSslOption(), QSslSocket::newSessionTicketReceived()
811 */
812int QSslConfiguration::sessionTicketLifeTimeHint() const
813{
814 return d->sslSessionTicketLifeTimeHint;
815}
816
817/*!
818 \since 5.7
819
820 Returns the ephemeral server key used for cipher algorithms
821 with forward secrecy, e.g. DHE-RSA-AES128-SHA.
822
823 The ephemeral key is only available when running in client mode, i.e.
824 QSslSocket::SslClientMode. When running in server mode or using a
825 cipher algorithm without forward secrecy a null key is returned.
826 The ephemeral server key will be set before emitting the encrypted()
827 signal.
828 */
829QSslKey QSslConfiguration::ephemeralServerKey() const
830{
831 return d->ephemeralServerKey;
832}
833
834/*!
835 \since 5.5
836
837 Returns this connection's current list of elliptic curves. This
838 list is used during the handshake phase for choosing an
839 elliptic curve (when using an elliptic curve cipher).
840 The returned list of curves is ordered by descending preference
841 (i.e., the first curve in the list is the most preferred one).
842
843 By default, the handshake phase can choose any of the curves
844 supported by this system's SSL libraries, which may vary from
845 system to system. The list of curves supported by this system's
846 SSL libraries is returned by QSslSocket::supportedEllipticCurves().
847
848 You can restrict the list of curves used for choosing the session cipher
849 for this socket by calling setEllipticCurves() with a subset of the
850 supported ciphers. You can revert to using the entire set by calling
851 setEllipticCurves() with the list returned by
852 QSslSocket::supportedEllipticCurves().
853
854 \sa setEllipticCurves
855 */
856QList<QSslEllipticCurve> QSslConfiguration::ellipticCurves() const
857{
858 return d->ellipticCurves;
859}
860
861/*!
862 \since 5.5
863
864 Sets the list of elliptic curves to be used by this socket to \a curves,
865 which must contain a subset of the curves in the list returned by
866 supportedEllipticCurves().
867
868 Restricting the elliptic curves must be done before the handshake
869 phase, where the session cipher is chosen.
870
871 \sa ellipticCurves
872 */
873void QSslConfiguration::setEllipticCurves(const QList<QSslEllipticCurve> &curves)
874{
875 d->ellipticCurves = curves;
876}
877
878/*!
879 \since 5.5
880
881 Returns the list of elliptic curves supported by this
882 system. This list is set by the system's SSL libraries and may
883 vary from system to system.
884
885 \sa ellipticCurves(), setEllipticCurves()
886*/
887QList<QSslEllipticCurve> QSslConfiguration::supportedEllipticCurves()
888{
889 return QSslSocketPrivate::supportedEllipticCurves();
890}
891
892/*!
893 \since 5.8
894
895 Returns the identity hint.
896
897 \sa setPreSharedKeyIdentityHint()
898*/
899QByteArray QSslConfiguration::preSharedKeyIdentityHint() const
900{
901 return d->preSharedKeyIdentityHint;
902}
903
904/*!
905 \since 5.8
906
907 Sets the identity hint for a preshared key authentication to \a hint. This will
908 affect the next initiated handshake; calling this function on an already-encrypted
909 socket will not affect the socket's identity hint.
910
911 The identity hint is used in QSslSocket::SslServerMode only!
912*/
913void QSslConfiguration::setPreSharedKeyIdentityHint(const QByteArray &hint)
914{
915 d->preSharedKeyIdentityHint = hint;
916}
917
918/*!
919 \since 5.8
920
921 Retrieves the current set of Diffie-Hellman parameters.
922
923 If no Diffie-Hellman parameters have been set, the QSslConfiguration object
924 defaults to using the 2048-bit MODP group from RFC 3526.
925
926 \note The default parameters may change in future Qt versions.
927 Please check the documentation of the \e{exact Qt version} that you
928 are using in order to know what defaults that version uses.
929 */
930QSslDiffieHellmanParameters QSslConfiguration::diffieHellmanParameters() const
931{
932 return d->dhParams;
933}
934
935/*!
936 \since 5.8
937
938 Sets a custom set of Diffie-Hellman parameters to be used by this socket when functioning as
939 a server to \a dhparams.
940
941 If no Diffie-Hellman parameters have been set, the QSslConfiguration object
942 defaults to using the 2048-bit MODP group from RFC 3526.
943
944 Since 6.7 you can provide an empty Diffie-Hellman parameter to use auto selection
945 (see SSL_CTX_set_dh_auto of openssl) if the tls backend supports it.
946
947 \note The default parameters may change in future Qt versions.
948 Please check the documentation of the \e{exact Qt version} that you
949 are using in order to know what defaults that version uses.
950 */
951void QSslConfiguration::setDiffieHellmanParameters(const QSslDiffieHellmanParameters &dhparams)
952{
953 d->dhParams = dhparams;
954}
955
956/*!
957 \since 5.11
958
959 Returns the backend-specific configuration.
960
961 Only options set by setBackendConfigurationOption() or setBackendConfiguration() will be
962 returned. The internal standard configuration of the backend is not reported.
963
964 \sa setBackendConfigurationOption(), setBackendConfiguration()
965 */
966QMap<QByteArray, QVariant> QSslConfiguration::backendConfiguration() const
967{
968 return d->backendConfig;
969}
970
971/*!
972 \since 5.11
973
974 Sets the option \a name in the backend-specific configuration to \a value.
975
976 Options supported by the OpenSSL (>= 1.0.2) backend are available in the \l
977 {https://www.openssl.org/docs/manmaster/man3/SSL_CONF_cmd.html#SUPPORTED-CONFIGURATION-FILE-COMMANDS}
978 {supported configuration file commands} documentation. The expected type for
979 the \a value parameter is a QByteArray for all options. The \l
980 {https://www.openssl.org/docs/manmaster/man3/SSL_CONF_cmd.html#EXAMPLES}{examples}
981 show how to use some of the options.
982
983 \note The backend-specific configuration will be applied after the general
984 configuration. Using the backend-specific configuration to set a general
985 configuration option again will overwrite the general configuration option.
986
987 \sa backendConfiguration(), setBackendConfiguration()
988 */
989void QSslConfiguration::setBackendConfigurationOption(const QByteArray &name, const QVariant &value)
990{
991 d->backendConfig[name] = value;
992}
993
994/*!
995 \since 5.11
996
997 Sets or clears the backend-specific configuration.
998
999 Without a \a backendConfiguration parameter this function will clear the
1000 backend-specific configuration. More information about the supported
1001 options is available in the documentation of setBackendConfigurationOption().
1002
1003 \sa backendConfiguration(), setBackendConfigurationOption()
1004 */
1005void QSslConfiguration::setBackendConfiguration(const QMap<QByteArray, QVariant> &backendConfiguration)
1006{
1007 d->backendConfig = backendConfiguration;
1008}
1009
1010/*!
1011 \since 5.3
1012
1013 This function returns the protocol negotiated with the server
1014 if the Next Protocol Negotiation (NPN) or Application-Layer Protocol
1015 Negotiation (ALPN) TLS extension was enabled.
1016 In order for the NPN/ALPN extension to be enabled, setAllowedNextProtocols()
1017 needs to be called explicitly before connecting to the server.
1018
1019 If no protocol could be negotiated or the extension was not enabled,
1020 this function returns a QByteArray which is null.
1021
1022 \sa setAllowedNextProtocols(), nextProtocolNegotiationStatus()
1023 */
1024QByteArray QSslConfiguration::nextNegotiatedProtocol() const
1025{
1026 return d->nextNegotiatedProtocol;
1027}
1028
1029/*!
1030 \since 5.3
1031
1032 This function sets the allowed \a protocols to be negotiated with the
1033 server through the Next Protocol Negotiation (NPN) or Application-Layer
1034 Protocol Negotiation (ALPN) TLS extension; each
1035 element in \a protocols must define one allowed protocol.
1036 The function must be called explicitly before connecting to send the NPN/ALPN
1037 extension in the SSL handshake.
1038 Whether or not the negotiation succeeded can be queried through
1039 nextProtocolNegotiationStatus().
1040
1041 \sa nextNegotiatedProtocol(), nextProtocolNegotiationStatus(), allowedNextProtocols(), QSslConfiguration::NextProtocolHttp1_1
1042 */
1043void QSslConfiguration::setAllowedNextProtocols(const QList<QByteArray> &protocols)
1044{
1045 d->nextAllowedProtocols = protocols;
1046}
1047
1048/*!
1049 \since 5.3
1050
1051 This function returns the allowed protocols to be negotiated with the
1052 server through the Next Protocol Negotiation (NPN) or Application-Layer
1053 Protocol Negotiation (ALPN) TLS extension, as set by setAllowedNextProtocols().
1054
1055 \sa nextNegotiatedProtocol(), nextProtocolNegotiationStatus(), setAllowedNextProtocols(), QSslConfiguration::NextProtocolHttp1_1
1056 */
1057QList<QByteArray> QSslConfiguration::allowedNextProtocols() const
1058{
1059 return d->nextAllowedProtocols;
1060}
1061
1062/*!
1063 \since 5.3
1064
1065 This function returns the status of the Next Protocol Negotiation (NPN)
1066 or Application-Layer Protocol Negotiation (ALPN).
1067 If the feature has not been enabled through setAllowedNextProtocols(),
1068 this function returns NextProtocolNegotiationNone.
1069 The status will be set before emitting the encrypted() signal.
1070
1071 \sa setAllowedNextProtocols(), allowedNextProtocols(), nextNegotiatedProtocol(), QSslConfiguration::NextProtocolNegotiationStatus
1072 */
1073QSslConfiguration::NextProtocolNegotiationStatus QSslConfiguration::nextProtocolNegotiationStatus() const
1074{
1075 return d->nextProtocolNegotiationStatus;
1076}
1077
1078/*!
1079 Returns the default SSL configuration to be used in new SSL
1080 connections.
1081
1082 The default SSL configuration consists of:
1083
1084 \list
1085 \li no local certificate and no private key
1086 \li protocol \l{QSsl::SecureProtocols}{SecureProtocols}
1087 \li the system's default CA certificate list
1088 \li the cipher list equal to the list of the SSL libraries'
1089 supported SSL ciphers that are 128 bits or more
1090 \endlist
1091
1092 \sa supportedCiphers(), setDefaultConfiguration()
1093*/
1094QSslConfiguration QSslConfiguration::defaultConfiguration()
1095{
1096 return QSslConfigurationPrivate::defaultConfiguration();
1097}
1098
1099/*!
1100 Sets the default SSL configuration to be used in new SSL
1101 connections to be \a configuration. Existing connections are not
1102 affected by this call.
1103
1104 \sa supportedCiphers(), defaultConfiguration()
1105*/
1106void QSslConfiguration::setDefaultConfiguration(const QSslConfiguration &configuration)
1107{
1108 QSslConfigurationPrivate::setDefaultConfiguration(configuration);
1109}
1110
1111#if QT_CONFIG(dtls) || defined(Q_QDOC)
1112
1113/*!
1114 This function returns true if DTLS cookie verification was enabled on a
1115 server-side socket.
1116
1117 \sa setDtlsCookieVerificationEnabled()
1118 */
1119bool QSslConfiguration::dtlsCookieVerificationEnabled() const
1120{
1121 return d->dtlsCookieEnabled;
1122}
1123
1124/*!
1125 This function enables DTLS cookie verification when \a enable is true.
1126
1127 \sa dtlsCookieVerificationEnabled()
1128 */
1129void QSslConfiguration::setDtlsCookieVerificationEnabled(bool enable)
1130{
1131 d->dtlsCookieEnabled = enable;
1132}
1133
1134/*!
1135 Returns the default DTLS configuration to be used in new DTLS
1136 connections.
1137
1138 The default DTLS configuration consists of:
1139
1140 \list
1141 \li no local certificate and no private key
1142 \li protocol DtlsV1_2OrLater
1143 \li the system's default CA certificate list
1144 \li the cipher list equal to the list of the SSL libraries'
1145 supported TLS 1.2 ciphers that use 128 or more secret bits
1146 for the cipher.
1147 \endlist
1148
1149 \sa setDefaultDtlsConfiguration()
1150*/
1151QSslConfiguration QSslConfiguration::defaultDtlsConfiguration()
1152{
1153 return QSslConfigurationPrivate::defaultDtlsConfiguration();
1154}
1155
1156/*!
1157 Sets the default DTLS configuration to be used in new DTLS
1158 connections to be \a configuration. Existing connections are not
1159 affected by this call.
1160
1161 \sa defaultDtlsConfiguration()
1162*/
1163void QSslConfiguration::setDefaultDtlsConfiguration(const QSslConfiguration &configuration)
1164{
1165 QSslConfigurationPrivate::setDefaultDtlsConfiguration(configuration);
1166}
1167
1168#endif // dtls
1169
1170/*!
1171 \since 5.13
1172 If \a enabled is true, client QSslSocket will send a certificate status request
1173 to its peer when initiating a handshake. During the handshake QSslSocket will
1174 verify the server's response. This value must be set before the handshake
1175 starts.
1176
1177 \sa ocspStaplingEnabled()
1178*/
1179void QSslConfiguration::setOcspStaplingEnabled(bool enabled)
1180{
1181#if QT_CONFIG(ocsp)
1182 d->ocspStaplingEnabled = enabled;
1183#else
1184 if (enabled)
1185 qCWarning(lcSsl, "Enabling OCSP-stapling requires the feature 'ocsp'");
1186#endif // ocsp
1187}
1188
1189/*!
1190 \since 5.13
1191 Returns true if OCSP stapling was enabled by setOCSPStaplingEnabled(),
1192 otherwise false (which is the default value).
1193
1194 \sa setOcspStaplingEnabled()
1195*/
1196bool QSslConfiguration::ocspStaplingEnabled() const
1197{
1198 return d->ocspStaplingEnabled;
1199}
1200
1201/*!
1202 \since 6.0
1203
1204 Returns true if a verification callback will emit QSslSocket::handshakeInterruptedOnError()
1205 early, before concluding the handshake.
1206
1207 \note This function always returns false for all backends but OpenSSL.
1208
1209 \sa setHandshakeMustInterruptOnError(), QSslSocket::handshakeInterruptedOnError(), QSslSocket::continueInterruptedHandshake()
1210*/
1211bool QSslConfiguration::handshakeMustInterruptOnError() const
1212{
1213 return d->reportFromCallback;
1214}
1215
1216/*!
1217 \since 6.0
1218
1219 If \a interrupt is true and the underlying backend supports this option,
1220 errors found during certificate verification are reported immediately
1221 by emitting QSslSocket::handshakeInterruptedOnError(). This allows
1222 to stop the unfinished handshake and send a proper alert message to
1223 a peer. No special action is required from the application in this case.
1224 QSslSocket will close the connection after sending the alert message.
1225 If the application after inspecting the error wants to continue the
1226 handshake, it must call QSslSocket::continueInterruptedHandshake()
1227 from its slot function. The signal-slot connection must be direct.
1228
1229 \note When interrupting handshake is enabled, errors that would otherwise
1230 be reported by QSslSocket::peerVerifyError() are instead only reported by
1231 QSslSocket::handshakeInterruptedOnError().
1232 \note Even if the handshake was continued, these errors will be
1233 reported when emitting QSslSocket::sslErrors() signal (and thus must
1234 be ignored in the corresponding function slot).
1235
1236 \sa handshakeMustInterruptOnError(), QSslSocket::handshakeInterruptedOnError(), QSslSocket::continueInterruptedHandshake()
1237*/
1238void QSslConfiguration::setHandshakeMustInterruptOnError(bool interrupt)
1239{
1240#if QT_CONFIG(openssl)
1241 d->reportFromCallback = interrupt;
1242#else
1243 Q_UNUSED(interrupt);
1244 qCWarning(lcSsl, "This operation requires OpenSSL as TLS backend");
1245#endif
1246}
1247
1248/*!
1249 \since 6.0
1250
1251 Returns true if errors with code QSslError::NoPeerCertificate
1252 cannot be ignored.
1253
1254 \note Always returns false for all TLS backends but OpenSSL.
1255
1256 \sa QSslSocket::ignoreSslErrors(), setMissingCertificateIsFatal()
1257*/
1258bool QSslConfiguration::missingCertificateIsFatal() const
1259{
1260 return d->missingCertIsFatal;
1261}
1262
1263/*!
1264 \since 6.0
1265
1266 If \a cannotRecover is true, and verification mode in use is
1267 QSslSocket::VerifyPeer or QSslSocket::AutoVerifyPeer (for a
1268 client-side socket), the missing peer's certificate would be
1269 treated as an unrecoverable error that cannot be ignored. A proper
1270 alert message will be sent to the peer before closing the connection.
1271
1272 \note Only available if Qt was configured and built with OpenSSL backend.
1273
1274 \sa QSslSocket::ignoreSslErrors(), QSslSocket::PeerVerifyMode, missingCertificateIsFatal()
1275*/
1276void QSslConfiguration::setMissingCertificateIsFatal(bool cannotRecover)
1277{
1278#if QT_CONFIG(openssl)
1279 d->missingCertIsFatal = cannotRecover;
1280#else
1281 Q_UNUSED(cannotRecover);
1282 qCWarning(lcSsl, "Handling a missing certificate as a fatal error requires an OpenSSL backend");
1283#endif // openssl
1284}
1285
1286/*! \internal
1287*/
1288bool QSslConfigurationPrivate::peerSessionWasShared(const QSslConfiguration &configuration) {
1289 return configuration.d->peerSessionShared;
1290 }
1291
1292QT_END_NAMESPACE