1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 
41 #ifndef QSSLKEY_OPENSSL_P_H
42 #define QSSLKEY_OPENSSL_P_H
43 
44 //
45 //  W A R N I N G
46 //  -------------
47 //
48 // This file is not part of the Qt API.  It exists for the convenience
49 // of qsslcertificate.cpp.  This header file may change from version to version
50 // without notice, or even be removed.
51 //
52 // We mean it.
53 //
54 
55 #include <QtNetwork/private/qtnetworkglobal_p.h>
56 #include "qsslkey.h"
57 #include "qsslsocket_p.h" // includes wincrypt.h
58 
59 #ifndef QT_NO_OPENSSL
60 #include <openssl/rsa.h>
61 #include <openssl/dsa.h>
62 #endif
63 
64 QT_BEGIN_NAMESPACE
65 
66 class QSslKeyPrivate
67 {
68 public:
QSslKeyPrivate()69     inline QSslKeyPrivate()
70         : algorithm(QSsl::Opaque)
71         , opaque(nullptr)
72     {
73         clear(false);
74     }
75 
~QSslKeyPrivate()76     inline ~QSslKeyPrivate()
77     { clear(); }
78 
79     void clear(bool deep = true);
80 
81 #ifndef QT_NO_OPENSSL
82     bool fromEVP_PKEY(EVP_PKEY *pkey);
83 #endif
84     void decodeDer(const QByteArray &der, const QByteArray &passPhrase = {}, bool deepClear = true);
85     void decodePem(const QByteArray &pem, const QByteArray &passPhrase, bool deepClear = true);
86     QByteArray pemHeader() const;
87     QByteArray pemFooter() const;
88     QByteArray pemFromDer(const QByteArray &der, const QMap<QByteArray, QByteArray> &headers) const;
89     QByteArray derFromPem(const QByteArray &pem, QMap<QByteArray, QByteArray> *headers) const;
90 
91     int length() const;
92     QByteArray toPem(const QByteArray &passPhrase) const;
93     Qt::HANDLE handle() const;
94 
95     bool isEncryptedPkcs8(const QByteArray &der) const;
96 #if !QT_CONFIG(openssl)
97     QByteArray decryptPkcs8(const QByteArray &encrypted, const QByteArray &passPhrase);
98     bool isPkcs8 = false;
99 #endif
100 
101     bool isNull;
102     QSsl::KeyType type;
103     QSsl::KeyAlgorithm algorithm;
104 
105     enum Cipher {
106         DesCbc,
107         DesEde3Cbc,
108         Rc2Cbc,
109         Aes128Cbc,
110         Aes192Cbc,
111         Aes256Cbc
112     };
113 
114     Q_AUTOTEST_EXPORT static QByteArray decrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv);
115     Q_AUTOTEST_EXPORT static QByteArray encrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv);
116 
117 #ifndef QT_NO_OPENSSL
118     union {
119         EVP_PKEY *opaque;
120         RSA *rsa;
121         DSA *dsa;
122         DH *dh;
123 #ifndef OPENSSL_NO_EC
124         EC_KEY *ec;
125 #endif
126     };
127 #else
128     Qt::HANDLE opaque;
129     QByteArray derData;
130     int keyLength;
131 #endif
132 
133     QAtomicInt ref;
134 
135 private:
136     Q_DISABLE_COPY_MOVE(QSslKeyPrivate)
137 };
138 
139 QT_END_NAMESPACE
140 
141 #endif // QSSLKEY_OPENSSL_P_H
142