1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTLSBACKEND_P_H
5#define QTLSBACKEND_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19
20#include "qsslconfiguration.h"
21#include "qsslerror.h"
22#include "qssl_p.h"
23
24#if QT_CONFIG(dtls)
25#include "qdtls.h"
26#endif
27
28#include <QtNetwork/qsslcertificate.h>
29#include <QtNetwork/qsslcipher.h>
30#include <QtNetwork/qsslkey.h>
31#include <QtNetwork/qssl.h>
32
33#include <QtCore/qloggingcategory.h>
34#include <QtCore/qnamespace.h>
35#include <QtCore/qobject.h>
36#include <QtCore/qglobal.h>
37#include <QtCore/qstring.h>
38#include <QtCore/qlist.h>
39#include <QtCore/qmap.h>
40
41#include <memory>
42
43QT_BEGIN_NAMESPACE
44
45class QSslPreSharedKeyAuthenticator;
46class QSslSocketPrivate;
47class QHostAddress;
48class QSslContext;
49
50class QSslSocket;
51class QByteArray;
52class QSslCipher;
53class QUdpSocket;
54class QIODevice;
55class QSslError;
56class QSslKey;
57
58namespace QTlsPrivate {
59
60class Q_NETWORK_PRIVATE_EXPORT TlsKey {
61public:
62 virtual ~TlsKey();
63
64 using KeyType = QSsl::KeyType;
65 using KeyAlgorithm = QSsl::KeyAlgorithm;
66
67 virtual void decodeDer(KeyType type, KeyAlgorithm algorithm, const QByteArray &der,
68 const QByteArray &passPhrase, bool deepClear) = 0;
69 virtual void decodePem(KeyType type, KeyAlgorithm algorithm, const QByteArray &pem,
70 const QByteArray &passPhrase, bool deepClear) = 0;
71
72 virtual QByteArray toPem(const QByteArray &passPhrase) const = 0;
73 virtual QByteArray derFromPem(const QByteArray &pem, QMap<QByteArray, QByteArray> *headers) const = 0;
74 virtual QByteArray pemFromDer(const QByteArray &der, const QMap<QByteArray, QByteArray> &headers) const = 0;
75
76 virtual void fromHandle(Qt::HANDLE handle, KeyType type) = 0;
77 virtual Qt::HANDLE handle() const = 0;
78
79 virtual bool isNull() const = 0;
80 virtual KeyType type() const = 0;
81 virtual KeyAlgorithm algorithm() const = 0;
82 virtual int length() const = 0;
83
84 virtual void clear(bool deepClear) = 0;
85
86 virtual bool isPkcs8() const = 0;
87
88 virtual QByteArray decrypt(Cipher cipher, const QByteArray &data,
89 const QByteArray &passPhrase, const QByteArray &iv) const = 0;
90 virtual QByteArray encrypt(Cipher cipher, const QByteArray &data,
91 const QByteArray &key, const QByteArray &iv) const = 0;
92
93 QByteArray pemHeader() const;
94 QByteArray pemFooter() const;
95};
96
97class Q_NETWORK_PRIVATE_EXPORT X509Certificate
98{
99public:
100 virtual ~X509Certificate();
101
102 virtual bool isEqual(const X509Certificate &other) const = 0;
103 virtual bool isNull() const = 0;
104 virtual bool isSelfSigned() const = 0;
105 virtual QByteArray version() const = 0;
106 virtual QByteArray serialNumber() const = 0;
107 virtual QStringList issuerInfo(QSslCertificate::SubjectInfo subject) const = 0;
108 virtual QStringList issuerInfo(const QByteArray &attribute) const = 0;
109 virtual QStringList subjectInfo(QSslCertificate::SubjectInfo subject) const = 0;
110 virtual QStringList subjectInfo(const QByteArray &attribute) const = 0;
111
112 virtual QList<QByteArray> subjectInfoAttributes() const = 0;
113 virtual QList<QByteArray> issuerInfoAttributes() const = 0;
114 virtual QMultiMap<QSsl::AlternativeNameEntryType, QString> subjectAlternativeNames() const = 0;
115 virtual QDateTime effectiveDate() const = 0;
116 virtual QDateTime expiryDate() const = 0;
117
118 virtual TlsKey *publicKey() const;
119
120 // Extensions. Plugins do not expose internal representation
121 // and cannot rely on QSslCertificate's internals. Thus,
122 // we provide this information 'in pieces':
123 virtual qsizetype numberOfExtensions() const = 0;
124 virtual QString oidForExtension(qsizetype i) const = 0;
125 virtual QString nameForExtension(qsizetype i) const = 0;
126 virtual QVariant valueForExtension(qsizetype i) const = 0;
127 virtual bool isExtensionCritical(qsizetype i) const = 0;
128 virtual bool isExtensionSupported(qsizetype i) const = 0;
129
130 virtual QByteArray toPem() const = 0;
131 virtual QByteArray toDer() const = 0;
132 virtual QString toText() const = 0;
133
134 virtual Qt::HANDLE handle() const = 0;
135
136 virtual size_t hash(size_t seed) const noexcept = 0;
137};
138
139// TLSTODO: consider making those into virtuals in QTlsBackend. After all, we ask the backend
140// to return those pointers if the functionality is supported, but it's a bit odd to have
141// this level of indirection. They are not parts of the classes above because ...
142// you'd then have to ask backend to create a certificate to ... call those
143// functions on a certificate.
144using X509ChainVerifyPtr = QList<QSslError> (*)(const QList<QSslCertificate> &chain,
145 const QString &hostName);
146using X509PemReaderPtr = QList<QSslCertificate> (*)(const QByteArray &pem, int count);
147using X509DerReaderPtr = X509PemReaderPtr;
148using X509Pkcs12ReaderPtr = bool (*)(QIODevice *device, QSslKey *key, QSslCertificate *cert,
149 QList<QSslCertificate> *caCertificates,
150 const QByteArray &passPhrase);
151
152#if QT_CONFIG(ssl)
153// TLS over TCP. Handshake, encryption/decryption.
154class Q_NETWORK_PRIVATE_EXPORT TlsCryptograph : public QObject
155{
156public:
157 virtual ~TlsCryptograph();
158
159 virtual void init(QSslSocket *q, QSslSocketPrivate *d) = 0;
160 virtual void checkSettingSslContext(std::shared_ptr<QSslContext> tlsContext);
161 virtual std::shared_ptr<QSslContext> sslContext() const;
162
163 virtual QList<QSslError> tlsErrors() const = 0;
164
165 virtual void startClientEncryption() = 0;
166 virtual void startServerEncryption() = 0;
167 virtual void continueHandshake() = 0;
168 virtual void enableHandshakeContinuation();
169 virtual void disconnectFromHost() = 0;
170 virtual void disconnected() = 0;
171 virtual void cancelCAFetch();
172 virtual QSslCipher sessionCipher() const = 0;
173 virtual QSsl::SslProtocol sessionProtocol() const = 0;
174
175 virtual void transmit() = 0;
176 virtual bool hasUndecryptedData() const;
177 virtual QList<QOcspResponse> ocsps() const;
178
179 static bool isMatchingHostname(const QSslCertificate &cert, const QString &peerName);
180
181 void setErrorAndEmit(QSslSocketPrivate *d, QAbstractSocket::SocketError errorCode,
182 const QString &errorDescription) const;
183};
184#else
185class TlsCryptograph;
186#endif // QT_CONFIG(ssl)
187
188#if QT_CONFIG(dtls)
189
190class Q_NETWORK_PRIVATE_EXPORT DtlsBase
191{
192public:
193 virtual ~DtlsBase();
194
195 virtual void setDtlsError(QDtlsError code, const QString &description) = 0;
196
197 virtual QDtlsError error() const = 0;
198 virtual QString errorString() const = 0;
199
200 virtual void clearDtlsError() = 0;
201
202 virtual void setConfiguration(const QSslConfiguration &configuration) = 0;
203 virtual QSslConfiguration configuration() const = 0;
204
205 using GenParams = QDtlsClientVerifier::GeneratorParameters;
206 virtual bool setCookieGeneratorParameters(const GenParams &params) = 0;
207 virtual GenParams cookieGeneratorParameters() const = 0;
208};
209
210// DTLS cookie: generation and verification.
211class Q_NETWORK_EXPORT DtlsCookieVerifier : virtual public DtlsBase
212{
213public:
214 virtual bool verifyClient(QUdpSocket *socket, const QByteArray &dgram,
215 const QHostAddress &address, quint16 port) = 0;
216 virtual QByteArray verifiedHello() const = 0;
217};
218
219// TLS over UDP. Handshake, encryption/decryption.
220class Q_NETWORK_PRIVATE_EXPORT DtlsCryptograph : virtual public DtlsBase
221{
222public:
223
224 virtual QSslSocket::SslMode cryptographMode() const = 0;
225 virtual void setPeer(const QHostAddress &addr, quint16 port, const QString &name) = 0;
226 virtual QHostAddress peerAddress() const = 0;
227 virtual quint16 peerPort() const = 0;
228 virtual void setPeerVerificationName(const QString &name) = 0;
229 virtual QString peerVerificationName() const = 0;
230
231 virtual void setDtlsMtuHint(quint16 mtu) = 0;
232 virtual quint16 dtlsMtuHint() const = 0;
233
234 virtual QDtls::HandshakeState state() const = 0;
235 virtual bool isConnectionEncrypted() const = 0;
236
237 virtual bool startHandshake(QUdpSocket *socket, const QByteArray &dgram) = 0;
238 virtual bool handleTimeout(QUdpSocket *socket) = 0;
239 virtual bool continueHandshake(QUdpSocket *socket, const QByteArray &dgram) = 0;
240 virtual bool resumeHandshake(QUdpSocket *socket) = 0;
241 virtual void abortHandshake(QUdpSocket *socket) = 0;
242 virtual void sendShutdownAlert(QUdpSocket *socket) = 0;
243
244 virtual QList<QSslError> peerVerificationErrors() const = 0;
245 virtual void ignoreVerificationErrors(const QList<QSslError> &errorsToIgnore) = 0;
246
247 virtual QSslCipher dtlsSessionCipher() const = 0;
248 virtual QSsl::SslProtocol dtlsSessionProtocol() const = 0;
249
250 virtual qint64 writeDatagramEncrypted(QUdpSocket *socket, const QByteArray &dgram) = 0;
251 virtual QByteArray decryptDatagram(QUdpSocket *socket, const QByteArray &dgram) = 0;
252};
253
254#else
255
256class DtlsCookieVerifier;
257class DtlsCryptograph;
258
259#endif // QT_CONFIG(dtls)
260
261} // namespace QTlsPrivate
262
263// Factory, creating back-end specific implementations of
264// different entities QSslSocket is using.
265class Q_NETWORK_EXPORT QTlsBackend : public QObject
266{
267 Q_OBJECT
268public:
269 QTlsBackend();
270 ~QTlsBackend() override;
271
272 virtual bool isValid() const;
273 virtual long tlsLibraryVersionNumber() const;
274 virtual QString tlsLibraryVersionString() const;
275 virtual long tlsLibraryBuildVersionNumber() const;
276 virtual QString tlsLibraryBuildVersionString() const;
277 virtual void ensureInitialized() const;
278
279 virtual QString backendName() const = 0;
280 virtual QList<QSsl::SslProtocol> supportedProtocols() const = 0;
281 virtual QList<QSsl::SupportedFeature> supportedFeatures() const = 0;
282 virtual QList<QSsl::ImplementedClass> implementedClasses() const = 0;
283
284 // X509 and keys:
285 virtual QTlsPrivate::TlsKey *createKey() const;
286 virtual QTlsPrivate::X509Certificate *createCertificate() const;
287
288 virtual QList<QSslCertificate> systemCaCertificates() const;
289
290 // TLS and DTLS:
291 virtual QTlsPrivate::TlsCryptograph *createTlsCryptograph() const;
292 virtual QTlsPrivate::DtlsCryptograph *createDtlsCryptograph(class QDtls *qObject, int mode) const;
293 virtual QTlsPrivate::DtlsCookieVerifier *createDtlsCookieVerifier() const;
294
295 // TLSTODO - get rid of these function pointers, make them virtuals in
296 // the backend itself. X509 machinery:
297 virtual QTlsPrivate::X509ChainVerifyPtr X509Verifier() const;
298 virtual QTlsPrivate::X509PemReaderPtr X509PemReader() const;
299 virtual QTlsPrivate::X509DerReaderPtr X509DerReader() const;
300 virtual QTlsPrivate::X509Pkcs12ReaderPtr X509Pkcs12Reader() const;
301
302 // Elliptic curves:
303 virtual QList<int> ellipticCurvesIds() const;
304 virtual int curveIdFromShortName(const QString &name) const;
305 virtual int curveIdFromLongName(const QString &name) const;
306 virtual QString shortNameForId(int cid) const;
307 virtual QString longNameForId(int cid) const;
308 virtual bool isTlsNamedCurve(int cid) const;
309
310 // Note: int and not QSslDiffieHellmanParameter::Error - because this class and
311 // its enum are QT_CONFIG(ssl)-conditioned. But not QTlsBackend and
312 // its virtual functions. DH decoding:
313 virtual int dhParametersFromDer(const QByteArray &derData, QByteArray *data) const;
314 virtual int dhParametersFromPem(const QByteArray &pemData, QByteArray *data) const;
315
316 static QList<QString> availableBackendNames();
317 static QString defaultBackendName();
318 static QTlsBackend *findBackend(const QString &backendName);
319 static QTlsBackend *activeOrAnyBackend();
320
321 static QList<QSsl::SslProtocol> supportedProtocols(const QString &backendName);
322 static QList<QSsl::SupportedFeature> supportedFeatures(const QString &backendName);
323 static QList<QSsl::ImplementedClass> implementedClasses(const QString &backendName);
324
325 // Built-in, this is what Qt provides out of the box (depending on OS):
326 static constexpr const int nameIndexSchannel = 0;
327 static constexpr const int nameIndexSecureTransport = 1;
328 static constexpr const int nameIndexOpenSSL = 2;
329 static constexpr const int nameIndexCertOnly = 3;
330
331 static const QString builtinBackendNames[];
332
333 template<class DynamicType, class TLSObject>
334 static DynamicType *backend(const TLSObject &o)
335 {
336 return static_cast<DynamicType *>(o.d->backend.get());
337 }
338
339 static void resetBackend(QSslKey &key, QTlsPrivate::TlsKey *keyBackend);
340
341 static void setupClientPskAuth(QSslPreSharedKeyAuthenticator *auth, const char *hint,
342 int hintLength, unsigned maxIdentityLen, unsigned maxPskLen);
343 static void setupServerPskAuth(QSslPreSharedKeyAuthenticator *auth, const char *identity,
344 const QByteArray &identityHint, unsigned maxPskLen);
345#if QT_CONFIG(ssl)
346 static QSslCipher createCiphersuite(const QString &description, int bits, int supportedBits);
347 static QSslCipher createCiphersuite(const QString &suiteName, QSsl::SslProtocol protocol,
348 const QString &protocolString);
349 static QSslCipher createCipher(const QString &name, QSsl::SslProtocol protocol,
350 const QString &protocolString);
351
352 // Those statics are implemented using QSslSocketPrivate (which is not exported,
353 // unlike QTlsBackend).
354 static QList<QSslCipher> defaultCiphers();
355 static QList<QSslCipher> defaultDtlsCiphers();
356
357 static void setDefaultCiphers(const QList<QSslCipher> &ciphers);
358 static void setDefaultDtlsCiphers(const QList<QSslCipher> &ciphers);
359 static void setDefaultSupportedCiphers(const QList<QSslCipher> &ciphers);
360
361 static void resetDefaultEllipticCurves();
362
363 static void setDefaultCaCertificates(const QList<QSslCertificate> &certs);
364
365 // Many thanks to people who designed QSslConfiguration with hidden
366 // data-members, that sneakily set by some 'friend' classes, having
367 // some twisted logic.
368 static bool rootLoadingOnDemandAllowed(const QSslConfiguration &configuration);
369 static void storePeerCertificate(QSslConfiguration &configuration, const QSslCertificate &peerCert);
370 static void storePeerCertificateChain(QSslConfiguration &configuration,
371 const QList<QSslCertificate> &peerCertificateChain);
372 static void clearPeerCertificates(QSslConfiguration &configuration);
373 // And those are even worse, this is where we don't have the original configuration,
374 // and can have only a copy. So instead we go to d->privateConfiguration.someMember:
375 static void clearPeerCertificates(QSslSocketPrivate *d);
376 static void setPeerSessionShared(QSslSocketPrivate *d, bool shared);
377 static void setSessionAsn1(QSslSocketPrivate *d, const QByteArray &asn1);
378 static void setSessionLifetimeHint(QSslSocketPrivate *d, int hint);
379 using AlpnNegotiationStatus = QSslConfiguration::NextProtocolNegotiationStatus;
380 static void setAlpnStatus(QSslSocketPrivate *d, AlpnNegotiationStatus st);
381 static void setNegotiatedProtocol(QSslSocketPrivate *d, const QByteArray &protocol);
382 static void storePeerCertificate(QSslSocketPrivate *d, const QSslCertificate &peerCert);
383 static void storePeerCertificateChain(QSslSocketPrivate *d, const QList<QSslCertificate> &peerChain);
384 static void addTustedRoot(QSslSocketPrivate *d, const QSslCertificate &rootCert);// TODO: "addTrusted..."
385 // The next one - is a "very important" feature! Kidding ...
386 static void setEphemeralKey(QSslSocketPrivate *d, const QSslKey &key);
387
388 virtual void forceAutotestSecurityLevel();
389#endif // QT_CONFIG(ssl)
390
391 Q_DISABLE_COPY_MOVE(QTlsBackend)
392};
393
394#define QTlsBackend_iid "org.qt-project.Qt.QTlsBackend"
395Q_DECLARE_INTERFACE(QTlsBackend, QTlsBackend_iid);
396
397QT_END_NAMESPACE
398
399#endif // QTLSBACKEND_P_H
400

source code of qtbase/src/network/ssl/qtlsbackend_p.h