10960fb760265ff1d6be055506058a3c731a321b
[platform/core/security/key-manager.git] / src / manager / service / crypto-logic.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  *
16  * @file        crypto-logic.cpp
17  * @author      Sebastian Grabowski (s.grabowski@samsung.com)
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Crypto module implementation.
21  */
22
23 #include <iostream>
24 #include <fstream>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <openssl/evp.h>
29 #include <openssl/rand.h>
30
31 #include <ckm/ckm-error.h>
32
33 #include <dpl/log/log.h>
34
35 #include <base64.h>
36 #include <digest.h>
37 #include <crypto-logic.h>
38
39 #include <generic-backend/exception.h>
40 #include <sw-backend/internals.h>
41
42 namespace {
43
44 const static int AES_CBC_KEY_SIZE = 32;
45 const static int AES_GCM_TAG_SIZE = 16;
46
47 } // anonymous namespace
48
49 namespace CKM {
50
51 CryptoLogic::CryptoLogic() {}
52
53 CryptoLogic::CryptoLogic(CryptoLogic &&second) {
54     m_keyMap = std::move(second.m_keyMap);
55 }
56
57 CryptoLogic& CryptoLogic::operator=(CryptoLogic &&second) {
58     if (this == &second)
59         return *this;
60     m_keyMap = std::move(second.m_keyMap);
61     return *this;
62 }
63
64 bool CryptoLogic::haveKey(const Label &smackLabel)
65 {
66     return (m_keyMap.count(smackLabel) > 0);
67 }
68
69 void CryptoLogic::pushKey(const Label &smackLabel,
70                           const RawBuffer &applicationKey)
71 {
72     if (smackLabel.length() == 0) {
73         ThrowErr(Exc::InternalError, "Empty smack label.");
74     }
75     if (applicationKey.size() == 0) {
76         ThrowErr(Exc::InternalError, "Empty application key.");
77     }
78     if (haveKey(smackLabel)) {
79         ThrowErr(Exc::InternalError, "Application key for ", smackLabel,
80             "label already exists.");
81     }
82
83     m_keyMap[smackLabel] = applicationKey;
84 }
85
86 void CryptoLogic::removeKey(const Label &smackLabel)
87 {
88     m_keyMap.erase(smackLabel);
89 }
90
91 RawBuffer CryptoLogic::passwordToKey(
92     const Password &password,
93     const RawBuffer &salt,
94     size_t keySize) const
95 {
96     RawBuffer result(keySize);
97
98     if (1 != PKCS5_PBKDF2_HMAC_SHA1(
99                 password.c_str(),
100                 password.size(),
101                 salt.data(),
102                 salt.size(),
103                 1024,
104                 result.size(),
105                 result.data()))
106     {
107         ThrowErr(Exc::InternalError, "PCKS5_PKKDF_HMAC_SHA1 failed.");
108     }
109
110     return result;
111 }
112
113 RawBuffer CryptoLogic::generateRandIV() const {
114     RawBuffer civ(EVP_MAX_IV_LENGTH);
115
116     if (1 != RAND_bytes(civ.data(), civ.size())) {
117         ThrowErr(Exc::InternalError, "RAND_bytes failed to generate IV.");
118     }
119
120     return civ;
121 }
122
123 void CryptoLogic::encryptRow(const Password &password, DB::Row &row)
124 {
125     try {
126         DB::Row crow = row;
127         RawBuffer key;
128         RawBuffer result1;
129         RawBuffer result2;
130
131         crow.algorithmType = DBCMAlgType::AES_GCM_256;
132         crow.dataSize = crow.data.size();
133
134         if (crow.dataSize <= 0) {
135             ThrowErr(Exc::InternalError, "Invalid dataSize.");
136         }
137
138         if (!haveKey(row.ownerLabel)) {
139             ThrowErr(Exc::InternalError, "Missing application key for ",
140               row.ownerLabel, " label.");
141         }
142
143         if (crow.iv.empty()) {
144             crow.iv = generateRandIV();
145         }
146
147         key = m_keyMap[row.ownerLabel];
148         crow.encryptionScheme = ENCR_APPKEY;
149
150         auto dataPair = Crypto::SW::Internals::encryptDataAesGcm(key, crow.data, crow.iv, AES_GCM_TAG_SIZE);
151         crow.data = dataPair.first;
152
153         crow.tag = dataPair.second;
154
155         if (!password.empty()) {
156             key = passwordToKey(password, crow.iv, AES_CBC_KEY_SIZE);
157
158             crow.data = Crypto::SW::Internals::encryptDataAesCbc(key, crow.data, crow.iv);
159             crow.encryptionScheme |= ENCR_PASSWORD;
160         }
161
162         encBase64(crow.data);
163         crow.encryptionScheme |= ENCR_BASE64;
164         encBase64(crow.iv);
165
166         row = crow;
167     } catch(const CKM::Base64Encoder::Exception::Base &e) {
168         ThrowErr(Exc::InternalError, e.GetMessage());
169     } catch(const CKM::Base64Decoder::Exception::Base &e) {
170         ThrowErr(Exc::InternalError, e.GetMessage());
171     }
172 }
173
174 void CryptoLogic::decryptRow(const Password &password, DB::Row &row)
175 {
176     try {
177         DB::Row crow = row;
178         RawBuffer key;
179         RawBuffer digest, dataDigest;
180
181         if (row.algorithmType != DBCMAlgType::AES_GCM_256) {
182             ThrowErr(Exc::AuthenticationFailed, "Invalid algorithm type.");
183         }
184
185         if ((row.encryptionScheme & ENCR_PASSWORD) && password.empty()) {
186             ThrowErr(Exc::AuthenticationFailed,
187               "DB row is password protected, but given password is "
188               "empty.");
189         }
190
191         if ((row.encryptionScheme & ENCR_APPKEY) && !haveKey(row.ownerLabel)) {
192             ThrowErr(Exc::AuthenticationFailed, "Missing application key for ",
193               row.ownerLabel, " label.");
194         }
195
196         decBase64(crow.iv);
197         if (crow.encryptionScheme & ENCR_BASE64) {
198             decBase64(crow.data);
199         }
200
201         if (crow.encryptionScheme & ENCR_PASSWORD) {
202             key = passwordToKey(password, crow.iv, AES_CBC_KEY_SIZE);
203             crow.data = Crypto::SW::Internals::decryptDataAesCbc(key, crow.data, crow.iv);
204         }
205
206         if (crow.encryptionScheme & ENCR_APPKEY) {
207             key = m_keyMap[crow.ownerLabel];
208             crow.data = Crypto::SW::Internals::decryptDataAesGcm(key, crow.data, crow.iv, crow.tag);
209         }
210
211         if (static_cast<int>(crow.data.size()) < crow.dataSize) {
212             ThrowErr(Exc::AuthenticationFailed, "Decrypted row size mismatch");
213         }
214
215         if (static_cast<int>(crow.data.size()) > crow.dataSize) {
216             crow.data.resize(crow.dataSize);
217         }
218
219         row = crow;
220     } catch(const CKM::Base64Encoder::Exception::Base &e) {
221         ThrowErr(Exc::InternalError, e.GetMessage());
222     } catch(const CKM::Base64Decoder::Exception::Base &e) {
223         ThrowErr(Exc::InternalError, e.GetMessage());
224     } catch(const Exc::Exception &e) {
225         ThrowErr(Exc::AuthenticationFailed, e.message());
226     }
227 }
228
229 void CryptoLogic::encBase64(RawBuffer &data)
230 {
231     Base64Encoder benc;
232     RawBuffer encdata;
233
234     benc.append(data);
235     benc.finalize();
236     encdata = benc.get();
237
238     if (encdata.size() == 0) {
239         ThrowErr(Exc::InternalError, "Base64Encoder returned empty data.");
240     }
241
242     data = std::move(encdata);
243 }
244
245 void CryptoLogic::decBase64(RawBuffer &data)
246 {
247     Base64Decoder bdec;
248     RawBuffer decdata;
249
250     bdec.reset();
251     bdec.append(data);
252     if (!bdec.finalize()) {
253         ThrowErr(Exc::InternalError, "Failed in Base64Decoder.finalize.");
254     }
255
256     decdata = bdec.get();
257
258     if (decdata.size() == 0) {
259         ThrowErr(Exc::InternalError, "Base64Decoder returned empty data.");
260     }
261
262     data = std::move(decdata);
263 }
264
265 bool CryptoLogic::equalDigests(RawBuffer &dig1, RawBuffer &dig2)
266 {
267     unsigned int dlen = Digest().length();
268
269     if ((dig1.size() != dlen) || (dig2.size() != dlen))
270         return false;
271     return (dig1 == dig2);
272 }
273
274 } // namespace CKM
275