Debian Codegraph @

src/fdosecrets/objects/SessionCipher.cpp

raw
1 /*
2 * Copyright (C) 2019 Aetf <[email protected]>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "SessionCipher.h"
19
20 #include "config-keepassx.h"
21
22 #include "crypto/Random.h"
23 #include "crypto/SymmetricCipher.h"
24
25 #include <QDebug>
26 #include <botan/dh.h>
27
28 #ifdef WITH_XC_BOTAN3
29 #include <botan/dl_group.h>
30 #include <botan/pubkey.h>
31 #else
32 #include <botan/pk_ops.h>
33 #endif
34
35 namespace FdoSecrets
36 {
37 // XXX: remove the redundant definitions once we are at C++17
38 constexpr char PlainCipher::Algorithm[];
39 constexpr char DhIetf1024Sha256Aes128CbcPkcs7::Algorithm[];
40
41 DhIetf1024Sha256Aes128CbcPkcs7::DhIetf1024Sha256Aes128CbcPkcs7(const QByteArray& clientPublicKey)
42 {
43 try {
44 m_privateKey.reset(new Botan::DH_PrivateKey(*randomGen()->getRng(), Botan::DL_Group("modp/ietf/1024")));
45 m_valid = updateClientPublicKey(clientPublicKey);
46 } catch (std::exception& e) {
47 qCritical("Failed to derive DH key: %s", e.what());
48 m_privateKey.reset();
49 m_valid = false;
50 }
51 }
52
53 bool DhIetf1024Sha256Aes128CbcPkcs7::updateClientPublicKey(const QByteArray& clientPublicKey)
54 {
55 if (!m_privateKey) {
56 return false;
57 }
58
59 try {
60 Botan::secure_vector<uint8_t> salt(32, '\0');
61 #ifdef WITH_XC_BOTAN3
62 Botan::PK_Key_Agreement dhka(*m_privateKey, *randomGen()->getRng(), "HKDF(SHA-256)", "");
63 auto aesKey = dhka.derive_key(16,
64 reinterpret_cast<const uint8_t*>(clientPublicKey.constData()),
65 clientPublicKey.size(),
66 salt.data(),
67 salt.size());
68 m_aesKey = QByteArray(reinterpret_cast<const char*>(aesKey.begin()), aesKey.size());
69 #else
70 auto dhka = m_privateKey->create_key_agreement_op(*randomGen()->getRng(), "HKDF(SHA-256)", "");
71 auto aesKey = dhka->agree(16,
72 reinterpret_cast<const uint8_t*>(clientPublicKey.constData()),
73 clientPublicKey.size(),
74 salt.data(),
75 salt.size());
76 m_aesKey = QByteArray(reinterpret_cast<char*>(aesKey.data()), aesKey.size());
77 #endif
78 return true;
79 } catch (std::exception& e) {
80 qCritical("Failed to update client public key: %s", e.what());
81 return false;
82 }
83 }
84
85 Secret DhIetf1024Sha256Aes128CbcPkcs7::encrypt(const Secret& input)
86 {
87 Secret output = input;
88 output.parameters.clear();
89 output.value.clear();
90
91 SymmetricCipher encrypter;
92 auto IV = randomGen()->randomArray(SymmetricCipher::defaultIvSize(SymmetricCipher::Aes128_CBC));
93 if (!encrypter.init(SymmetricCipher::Aes128_CBC, SymmetricCipher::Encrypt, m_aesKey, IV)) {
94 qWarning() << "Error encrypt: " << encrypter.errorString();
95 return output;
96 }
97
98 output.parameters = IV;
99 output.value = input.value;
100 if (!encrypter.finish(output.value)) {
101 qWarning() << "Error encrypt: " << encrypter.errorString();
102 return output;
103 }
104
105 return output;
106 }
107
108 Secret DhIetf1024Sha256Aes128CbcPkcs7::decrypt(const Secret& input)
109 {
110 SymmetricCipher decrypter;
111 if (!decrypter.init(SymmetricCipher::Aes128_CBC, SymmetricCipher::Decrypt, m_aesKey, input.parameters)) {
112 qWarning() << "Error decoding: " << decrypter.errorString();
113 return input;
114 }
115
116 Secret output = input;
117 output.parameters.clear();
118 if (!decrypter.finish(output.value)) {
119 qWarning() << "Error decoding: " << decrypter.errorString();
120 return input;
121 }
122 return output;
123 }
124
125 bool DhIetf1024Sha256Aes128CbcPkcs7::isValid() const
126 {
127 return m_valid;
128 }
129
130 QVariant DhIetf1024Sha256Aes128CbcPkcs7::negotiationOutput() const
131 {
132 if (m_valid) {
133 auto pubkey = m_privateKey->public_value();
134 return QByteArray(reinterpret_cast<char*>(pubkey.data()), pubkey.size());
135 }
136 return {};
137 }
138 } // namespace FdoSecrets