| 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 |
#include "qssl_p.h" |
| 41 |
#include "qsslkey.h" |
| 42 |
#include "qsslkey_p.h" |
| 43 |
#include "qsslcertificate_p.h" |
| 44 |
|
| 45 |
#include <QtCore/qfunctions_winrt.h> |
| 46 |
|
| 47 |
#include <wrl.h> |
| 48 |
#include <windows.security.cryptography.h> |
| 49 |
#include <windows.security.cryptography.core.h> |
| 50 |
#include <windows.security.cryptography.certificates.h> |
| 51 |
#include <windows.storage.streams.h> |
| 52 |
#include <robuffer.h> |
| 53 |
|
| 54 |
using namespace Microsoft::WRL; |
| 55 |
using namespace Microsoft::WRL::Wrappers; |
| 56 |
using namespace ABI::Windows::Foundation; |
| 57 |
using namespace ABI::Windows::Security::Cryptography; |
| 58 |
using namespace ABI::Windows::Security::Cryptography::Certificates; |
| 59 |
using namespace ABI::Windows::Security::Cryptography::Core; |
| 60 |
using namespace ABI::Windows::Storage::Streams; |
| 61 |
|
| 62 |
QT_USE_NAMESPACE |
| 63 |
|
| 64 |
struct SslKeyGlobal |
| 65 |
{ |
| 66 |
SslKeyGlobal() |
| 67 |
{ |
| 68 |
HRESULT hr; |
| 69 |
hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Security_Cryptography_Core_CryptographicEngine).Get(), |
| 70 |
&engine); |
| 71 |
Q_ASSERT_SUCCEEDED(hr); |
| 72 |
|
| 73 |
ComPtr<ISymmetricKeyAlgorithmProviderStatics> keyProviderFactory; |
| 74 |
hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Security_Cryptography_Core_SymmetricKeyAlgorithmProvider).Get(), |
| 75 |
&keyProviderFactory); |
| 76 |
Q_ASSERT_SUCCEEDED(hr); |
| 77 |
hr = keyProviderFactory->OpenAlgorithm(HString::MakeReference(L"DES_CBC").Get(), |
| 78 |
&keyProviders[QSslKeyPrivate::DesCbc]); |
| 79 |
Q_ASSERT_SUCCEEDED(hr); |
| 80 |
hr = keyProviderFactory->OpenAlgorithm(HString::MakeReference(L"3DES_CBC").Get(), |
| 81 |
&keyProviders[QSslKeyPrivate::DesEde3Cbc]); |
| 82 |
Q_ASSERT_SUCCEEDED(hr); |
| 83 |
hr = keyProviderFactory->OpenAlgorithm(HString::MakeReference(L"RC2_CBC").Get(), |
| 84 |
&keyProviders[QSslKeyPrivate::Rc2Cbc]); |
| 85 |
Q_ASSERT_SUCCEEDED(hr); |
| 86 |
hr = keyProviderFactory->OpenAlgorithm(HString::MakeReference(L"AES_CBC").Get(), |
| 87 |
&keyProviders[QSslKeyPrivate::Aes128Cbc]); |
| 88 |
Q_ASSERT_SUCCEEDED(hr); |
| 89 |
hr = keyProviderFactory->OpenAlgorithm(HString::MakeReference(L"AES_CBC").Get(), |
| 90 |
&keyProviders[QSslKeyPrivate::Aes192Cbc]); |
| 91 |
Q_ASSERT_SUCCEEDED(hr); |
| 92 |
hr = keyProviderFactory->OpenAlgorithm(HString::MakeReference(L"AES_CBC").Get(), |
| 93 |
&keyProviders[QSslKeyPrivate::Aes256Cbc]); |
| 94 |
Q_ASSERT_SUCCEEDED(hr); |
| 95 |
|
| 96 |
hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Security_Cryptography_CryptographicBuffer).Get(), |
| 97 |
&bufferFactory); |
| 98 |
Q_ASSERT_SUCCEEDED(hr); |
| 99 |
} |
| 100 |
|
| 101 |
ComPtr<ICryptographicEngineStatics> engine; |
| 102 |
QHash<QSslKeyPrivate::Cipher, ComPtr<ISymmetricKeyAlgorithmProvider>> keyProviders; |
| 103 |
ComPtr<ICryptographicBufferStatics> bufferFactory; |
| 104 |
}; |
| 105 |
Q_GLOBAL_STATIC(SslKeyGlobal, g) |
| 106 |
|
| 107 |
static QByteArray doCrypt(QSslKeyPrivate::Cipher cipher, QByteArray data, const QByteArray &key, const QByteArray &iv, bool encrypt) |
| 108 |
{ |
| 109 |
HRESULT hr; |
| 110 |
|
| 111 |
ISymmetricKeyAlgorithmProvider *keyProvider = g->keyProviders[cipher].Get(); |
| 112 |
Q_ASSERT(keyProvider); |
| 113 |
|
| 114 |
ComPtr<IBuffer> keyBuffer; |
| 115 |
hr = g->bufferFactory->CreateFromByteArray(key.length(), (BYTE *)key.data(), &keyBuffer); |
| 116 |
Q_ASSERT_SUCCEEDED(hr); |
| 117 |
ComPtr<ICryptographicKey> cryptographicKey; |
| 118 |
hr = keyProvider->CreateSymmetricKey(keyBuffer.Get(), &cryptographicKey); |
| 119 |
Q_ASSERT_SUCCEEDED(hr); |
| 120 |
|
| 121 |
UINT32 blockLength; |
| 122 |
hr = keyProvider->get_BlockLength(&blockLength); |
| 123 |
Q_ASSERT_SUCCEEDED(hr); |
| 124 |
if (encrypt) { // Add padding |
| 125 |
const char padding = blockLength - data.length() % blockLength; |
| 126 |
data += QByteArray(padding, padding); |
| 127 |
} |
| 128 |
|
| 129 |
ComPtr<IBuffer> dataBuffer; |
| 130 |
hr = g->bufferFactory->CreateFromByteArray(data.length(), (BYTE *)data.data(), &dataBuffer); |
| 131 |
Q_ASSERT_SUCCEEDED(hr); |
| 132 |
ComPtr<IBuffer> ivBuffer; |
| 133 |
hr = g->bufferFactory->CreateFromByteArray(iv.length(), (BYTE *)iv.data(), &ivBuffer); |
| 134 |
Q_ASSERT_SUCCEEDED(hr); |
| 135 |
ComPtr<IBuffer> resultBuffer; |
| 136 |
hr = encrypt ? g->engine->Encrypt(cryptographicKey.Get(), dataBuffer.Get(), ivBuffer.Get(), &resultBuffer) |
| 137 |
: g->engine->Decrypt(cryptographicKey.Get(), dataBuffer.Get(), ivBuffer.Get(), &resultBuffer); |
| 138 |
Q_ASSERT_SUCCEEDED(hr); |
| 139 |
|
| 140 |
UINT32 resultLength; |
| 141 |
hr = resultBuffer->get_Length(&resultLength); |
| 142 |
Q_ASSERT_SUCCEEDED(hr); |
| 143 |
ComPtr<Windows::Storage::Streams::IBufferByteAccess> bufferAccess; |
| 144 |
hr = resultBuffer.As(&bufferAccess); |
| 145 |
Q_ASSERT_SUCCEEDED(hr); |
| 146 |
byte *resultData; |
| 147 |
hr = bufferAccess->Buffer(&resultData); |
| 148 |
Q_ASSERT_SUCCEEDED(hr); |
| 149 |
|
| 150 |
if (!encrypt) { // Remove padding |
| 151 |
const uchar padding = resultData[resultLength - 1]; |
| 152 |
if (padding > 0 && padding <= blockLength) |
| 153 |
resultLength -= padding; |
| 154 |
else |
| 155 |
qCWarning(lcSsl, "Invalid padding length of %u; decryption likely failed.", padding); |
| 156 |
} |
| 157 |
|
| 158 |
return QByteArray(reinterpret_cast<const char *>(resultData), resultLength); |
| 159 |
} |
| 160 |
|
| 161 |
QByteArray QSslKeyPrivate::decrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv) |
| 162 |
{ |
| 163 |
return doCrypt(cipher, data, key, iv, false); |
| 164 |
} |
| 165 |
|
| 166 |
QByteArray QSslKeyPrivate::encrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv) |
| 167 |
{ |
| 168 |
return doCrypt(cipher, data, key, iv, true); |
| 169 |
} |