1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Jeremy Lainé <jeremy.laine@m4x.org>
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 "qsslkey.h"
41 #include "qsslkey_p.h"
42
43 #include <CommonCrypto/CommonCrypto.h>
44
45 #include <cstddef>
46
47 QT_BEGIN_NAMESPACE
48
wrapCCCrypt(CCOperation ccOp,QSslKeyPrivate::Cipher cipher,const QByteArray & data,const QByteArray & key,const QByteArray & iv)49 static QByteArray wrapCCCrypt(CCOperation ccOp,
50 QSslKeyPrivate::Cipher cipher,
51 const QByteArray &data,
52 const QByteArray &key, const QByteArray &iv)
53 {
54 int blockSize;
55 CCAlgorithm ccAlgorithm;
56 switch (cipher) {
57 case QSslKeyPrivate::DesCbc:
58 blockSize = kCCBlockSizeDES;
59 ccAlgorithm = kCCAlgorithmDES;
60 break;
61 case QSslKeyPrivate::DesEde3Cbc:
62 blockSize = kCCBlockSize3DES;
63 ccAlgorithm = kCCAlgorithm3DES;
64 break;
65 case QSslKeyPrivate::Rc2Cbc:
66 blockSize = kCCBlockSizeRC2;
67 ccAlgorithm = kCCAlgorithmRC2;
68 break;
69 case QSslKeyPrivate::Aes128Cbc:
70 case QSslKeyPrivate::Aes192Cbc:
71 case QSslKeyPrivate::Aes256Cbc:
72 blockSize = kCCBlockSizeAES128;
73 ccAlgorithm = kCCAlgorithmAES;
74 break;
75 }
76 size_t plainLength = 0;
77 QByteArray plain(data.size() + blockSize, 0);
78 CCCryptorStatus status = CCCrypt(
79 ccOp, ccAlgorithm, kCCOptionPKCS7Padding,
80 key.constData(), std::size_t(key.size()),
81 iv.constData(),
82 data.constData(), std::size_t(data.size()),
83 plain.data(), std::size_t(plain.size()), &plainLength);
84 if (status == kCCSuccess)
85 return plain.left(int(plainLength));
86 return QByteArray();
87 }
88
decrypt(Cipher cipher,const QByteArray & data,const QByteArray & key,const QByteArray & iv)89 QByteArray QSslKeyPrivate::decrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv)
90 {
91 return wrapCCCrypt(kCCDecrypt, cipher, data, key, iv);
92 }
93
encrypt(Cipher cipher,const QByteArray & data,const QByteArray & key,const QByteArray & iv)94 QByteArray QSslKeyPrivate::encrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv)
95 {
96 return wrapCCCrypt(kCCEncrypt, cipher, data, key, iv);
97 }
98
99 QT_END_NAMESPACE
100