| 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 QTLS_OPENSSL_P_H |
| 5 | #define QTLS_OPENSSL_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 "qtlsbackend_openssl_p.h" |
| 21 | #include "qsslcontext_openssl_p.h" |
| 22 | #include "qopenssl_p.h" |
| 23 | |
| 24 | #include <QtNetwork/qsslcertificate.h> |
| 25 | #include <QtNetwork/qocspresponse.h> |
| 26 | |
| 27 | #include <QtCore/qsharedpointer.h> |
| 28 | #include <QtCore/qbytearray.h> |
| 29 | #include <QtCore/qglobal.h> |
| 30 | #include <QtCore/qlist.h> |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | namespace QTlsPrivate { |
| 35 | |
| 36 | class TlsCryptographOpenSSL : public TlsCryptograph |
| 37 | { |
| 38 | public: |
| 39 | enum ExDataOffset { |
| 40 | errorOffsetInExData = 1, |
| 41 | socketOffsetInExData = 2 |
| 42 | }; |
| 43 | |
| 44 | ~TlsCryptographOpenSSL(); |
| 45 | |
| 46 | void init(QSslSocket *qObj, QSslSocketPrivate *dObj) override; |
| 47 | void checkSettingSslContext(std::shared_ptr<QSslContext> tlsContext) override; |
| 48 | std::shared_ptr<QSslContext> sslContext() const override; |
| 49 | |
| 50 | QList<QSslError> tlsErrors() const override; |
| 51 | |
| 52 | void startClientEncryption() override; |
| 53 | void startServerEncryption() override; |
| 54 | bool startHandshake(); |
| 55 | void enableHandshakeContinuation() override; |
| 56 | void cancelCAFetch() override; |
| 57 | void continueHandshake() override; |
| 58 | void transmit() override; |
| 59 | void disconnectFromHost() override; |
| 60 | void disconnected() override; |
| 61 | QSslCipher sessionCipher() const override; |
| 62 | QSsl::SslProtocol sessionProtocol() const override; |
| 63 | QList<QOcspResponse> ocsps() const override; |
| 64 | |
| 65 | bool checkSslErrors(); |
| 66 | int handleNewSessionTicket(SSL *connection); |
| 67 | |
| 68 | void alertMessageSent(int encoded); |
| 69 | void alertMessageReceived(int encoded); |
| 70 | |
| 71 | int emitErrorFromCallback(X509_STORE_CTX *ctx); |
| 72 | void trySendFatalAlert(); |
| 73 | |
| 74 | #if QT_CONFIG(ocsp) |
| 75 | bool checkOcspStatus(); |
| 76 | #endif |
| 77 | |
| 78 | QSslSocket *q = nullptr; |
| 79 | QSslSocketPrivate *d = nullptr; |
| 80 | |
| 81 | void storePeerCertificates(); |
| 82 | |
| 83 | unsigned pskClientTlsCallback(const char *hint, char *identity, unsigned max_identity_len, |
| 84 | unsigned char *psk, unsigned max_psk_len); |
| 85 | unsigned pskServerTlsCallback(const char *identity, unsigned char *psk, |
| 86 | unsigned max_psk_len); |
| 87 | |
| 88 | bool isInSslRead() const; |
| 89 | void setRenegotiated(bool renegotiated); |
| 90 | |
| 91 | #ifdef Q_OS_WIN |
| 92 | void fetchCaRootForCert(const QSslCertificate &cert); |
| 93 | void caRootLoaded(QSslCertificate certificate, QSslCertificate trustedRoot); |
| 94 | #endif |
| 95 | |
| 96 | QByteArray ocspResponseDer; |
| 97 | private: |
| 98 | // TLSTODO: names were preserved, to make comparison |
| 99 | // easier (see qsslsocket_openssl.cpp, while it exists). |
| 100 | bool initSslContext(); |
| 101 | void destroySslContext(); |
| 102 | |
| 103 | std::shared_ptr<QSslContext> sslContextPointer; |
| 104 | SSL *ssl = nullptr; // TLSTODO: RAII. |
| 105 | |
| 106 | QList<QSslErrorEntry> errorList; |
| 107 | QList<QSslError> sslErrors; |
| 108 | |
| 109 | BIO *readBio = nullptr; |
| 110 | BIO *writeBio = nullptr; |
| 111 | |
| 112 | QList<QOcspResponse> ocspResponses; |
| 113 | |
| 114 | // This description will go to setErrorAndEmit(SslHandshakeError, ocspErrorDescription) |
| 115 | QString ocspErrorDescription; |
| 116 | // These will go to sslErrors() |
| 117 | QList<QSslError> ocspErrors; |
| 118 | |
| 119 | bool = false; |
| 120 | bool handshakeInterrupted = false; |
| 121 | |
| 122 | bool fetchAuthorityInformation = false; |
| 123 | std::optional<QSslCertificate> caToFetch; |
| 124 | |
| 125 | bool inSetAndEmitError = false; |
| 126 | bool pendingFatalAlert = false; |
| 127 | bool errorsReportedFromCallback = false; |
| 128 | |
| 129 | bool shutdown = false; |
| 130 | |
| 131 | bool inSslRead = false; |
| 132 | bool renegotiated = false; |
| 133 | }; |
| 134 | |
| 135 | } // namespace QTlsPrivate |
| 136 | |
| 137 | QT_END_NAMESPACE |
| 138 | |
| 139 | #endif // QTLS_OPENSSL_P_H |
| 140 | |
| 141 | |