1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the QtNetwork module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 
42 #ifndef QSSLCONTEXT_OPENSSL_P_H
43 #define QSSLCONTEXT_OPENSSL_P_H
44 
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include <QtNetwork/private/qtnetworkglobal_p.h>
57 #include <QtCore/qvariant.h>
58 #include <QtNetwork/qsslcertificate.h>
59 #include <QtNetwork/qsslconfiguration.h>
60 #include <openssl/ssl.h>
61 
62 QT_BEGIN_NAMESPACE
63 
64 #ifndef QT_NO_SSL
65 
66 class QSslContextPrivate;
67 
68 class QSslContext
69 {
70 public:
71 
72     ~QSslContext();
73 
74     static QSslContext* fromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration,
75                                           bool allowRootCertOnDemandLoading);
76     static QSharedPointer<QSslContext> sharedFromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration,
77                                                                bool allowRootCertOnDemandLoading);
78 
79     QSslError::SslError error() const;
80     QString errorString() const;
81 
82     SSL* createSsl();
83     bool cacheSession(SSL*); // should be called when handshake completed
84 
85     QByteArray sessionASN1() const;
86     void setSessionASN1(const QByteArray &sessionASN1);
87     int sessionTicketLifeTimeHint() const;
88 
89 #ifndef OPENSSL_NO_NEXTPROTONEG
90     // must be public because we want to use it from an OpenSSL callback
91     struct NPNContext {
NPNContextNPNContext92         NPNContext() : data(nullptr),
93             len(0),
94             status(QSslConfiguration::NextProtocolNegotiationNone)
95         { }
96         unsigned char *data;
97         unsigned short len;
98         QSslConfiguration::NextProtocolNegotiationStatus status;
99     };
100     NPNContext npnContext() const;
101 #endif // !OPENSSL_NO_NEXTPROTONEG
102 
103 protected:
104     QSslContext();
105     friend class QSharedPointer<QSslContext>;
106 
107 private:
108     static void initSslContext(QSslContext* sslContext, QSslSocket::SslMode mode, const QSslConfiguration &configuration,
109                                bool allowRootCertOnDemandLoading);
110     static void applyBackendConfig(QSslContext *sslContext);
111 
112 private:
113     SSL_CTX* ctx;
114     EVP_PKEY *pkey;
115     SSL_SESSION *session;
116     QByteArray m_sessionASN1;
117     int m_sessionTicketLifeTimeHint;
118     QSslError::SslError errorCode;
119     QString errorStr;
120     QSslConfiguration sslConfiguration;
121 #ifndef OPENSSL_NO_NEXTPROTONEG
122     QByteArray m_supportedNPNVersions;
123     NPNContext m_npnContext;
124 #endif // !OPENSSL_NO_NEXTPROTONEG
125 };
126 
127 #endif // QT_NO_SSL
128 
129 QT_END_NAMESPACE
130 
131 #endif // QSSLCONTEXT_OPENSSL_P_H
132