2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
22 #include <dpl/serialization.h>
23 #include <dpl/log/log.h>
25 #include <ckm/ckm-error.h>
26 #include <ckm/ckm-type.h>
27 #include <key-provider.h>
28 #include <file-system.h>
30 #include <ckm-logic.h>
34 int retCode = FileSystem::init();
35 // TODO what can I do when init went wrong? exit(-1) ??
37 LogError("Fatal error in FileSystem::init()");
41 CKMLogic::~CKMLogic(){}
43 RawBuffer CKMLogic::unlockUserKey(uid_t user, const std::string &password) {
44 // TODO try catch for all errors that should be supported by error code
45 int retCode = KEY_MANAGER_API_SUCCESS;
47 UserData &handle = m_userDataMap[user];
49 if (!(handle.keyProvider.isInitialized())) {
50 auto &handle = m_userDataMap[user];
53 auto wrappedDomainKEK = fs.getDomainKEK();
55 if (wrappedDomainKEK.empty()) {
56 wrappedDomainKEK = KeyProvider::generateDomainKEK(std::to_string(user), password);
57 fs.saveDomainKEK(wrappedDomainKEK);
60 handle.keyProvider = KeyProvider(wrappedDomainKEK, password);
61 handle.database = DBCrypto(fs.getDBPath(), handle.keyProvider.getDomainKEK());
64 MessageBuffer response;
65 Serialization::Serialize(response, retCode);
66 return response.Pop();
69 RawBuffer CKMLogic::lockUserKey(uid_t user) {
70 int retCode = KEY_MANAGER_API_SUCCESS;
71 // TODO try catch for all errors that should be supported by error code
72 m_userDataMap.erase(user);
74 MessageBuffer response;
75 Serialization::Serialize(response, retCode);
76 return response.Pop();
79 RawBuffer CKMLogic::removeUserData(uid_t user) {
80 int retCode = KEY_MANAGER_API_SUCCESS;
81 // TODO try catch for all errors that should be supported by error code
82 m_userDataMap.erase(user);
87 MessageBuffer response;
88 Serialization::Serialize(response, retCode);
89 return response.Pop();
92 RawBuffer CKMLogic::changeUserPassword(
94 const std::string &oldPassword,
95 const std::string &newPassword)
97 int retCode = KEY_MANAGER_API_SUCCESS;
100 auto wrappedDomainKEK = fs.getDomainKEK();
101 if (wrappedDomainKEK.empty()) {
102 retCode = KEY_MANAGER_API_ERROR_BAD_REQUEST;
104 wrappedDomainKEK = KeyProvider::reencrypt(wrappedDomainKEK, oldPassword, newPassword);
105 fs.saveDomainKEK(wrappedDomainKEK);
107 MessageBuffer response;
108 Serialization::Serialize(response, retCode);
109 return response.Pop();
112 RawBuffer CKMLogic::resetUserPassword(
114 const std::string &newPassword)
116 int retCode = KEY_MANAGER_API_SUCCESS;
118 if (0 == m_userDataMap.count(user)) {
119 retCode = KEY_MANAGER_API_ERROR_BAD_REQUEST;
121 auto &handler = m_userDataMap[user];
122 auto wrappedDomainKEK = handler.keyProvider.getDomainKEK(newPassword);
124 fs.saveDomainKEK(wrappedDomainKEK);
127 MessageBuffer response;
128 Serialization::Serialize(response, retCode);
129 return response.Pop();
132 RawBuffer CKMLogic::saveData(
137 const RawBuffer &key,
138 const PolicySerializable &policy)
140 int retCode = KEY_MANAGER_API_SUCCESS;
142 if (0 == m_userDataMap.count(cred.uid)) {
143 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
145 RawBuffer iv(10,'c');
159 auto &handler = m_userDataMap[cred.uid];
160 retCode = handler.database.saveDBRow(row);
163 MessageBuffer response;
164 Serialization::Serialize(response, static_cast<int>(LogicCommand::SAVE));
165 Serialization::Serialize(response, commandId);
166 Serialization::Serialize(response, retCode);
167 Serialization::Serialize(response, static_cast<int>(dataType));
169 return response.Pop();
172 RawBuffer CKMLogic::removeData(
181 int retCode = KEY_MANAGER_API_SUCCESS;
183 if (0 == m_userDataMap.count(cred.uid)) {
184 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
187 auto &handler = m_userDataMap[cred.uid];
191 MessageBuffer response;
192 Serialization::Serialize(response, static_cast<int>(LogicCommand::REMOVE));
193 Serialization::Serialize(response, commandId);
194 Serialization::Serialize(response, retCode);
195 Serialization::Serialize(response, static_cast<int>(dataType));
197 return response.Pop();
200 RawBuffer CKMLogic::getData(
205 const std::string &password)
210 int retCode = KEY_MANAGER_API_SUCCESS;
213 if (0 == m_userDataMap.count(cred.uid)) {
214 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
216 auto &handler = m_userDataMap[cred.uid];
217 retCode = handler.database.getDBRow(alias, cred.smackLabel, row);
220 MessageBuffer response;
221 Serialization::Serialize(response, static_cast<int>(LogicCommand::GET));
222 Serialization::Serialize(response, commandId);
223 Serialization::Serialize(response, retCode);
224 Serialization::Serialize(response, static_cast<int>(row.dataType));
225 Serialization::Serialize(response, row.data);
226 return response.Pop();
229 RawBuffer CKMLogic::getDataList(
234 int retCode = KEY_MANAGER_API_SUCCESS;
236 if (0 == m_userDataMap.count(cred.uid)) {
237 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
239 auto &handler = m_userDataMap[cred.uid];
244 MessageBuffer response;
245 Serialization::Serialize(response, static_cast<int>(LogicCommand::GET_LIST));
246 Serialization::Serialize(response, commandId);
247 Serialization::Serialize(response, retCode);
248 Serialization::Serialize(response, static_cast<int>(dataType));
249 Serialization::Serialize(response, AliasVector());
250 return response.Pop();
253 RawBuffer CKMLogic::createKeyPairRSA(
257 const Alias &privateKeyAlias,
258 const Alias &publicKeyAlias,
259 PolicySerializable policyPrivateKey,
260 PolicySerializable policyPublicKey)
264 (void)privateKeyAlias;
265 (void)publicKeyAlias,
266 (void)policyPrivateKey;
267 (void)policyPublicKey;
268 MessageBuffer response;
269 Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
270 Serialization::Serialize(response, commandId);
271 Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
273 return response.Pop();
276 RawBuffer CKMLogic::createKeyPairECDSA(
280 const Alias &privateKeyAlias,
281 const Alias &publicKeyAlias,
282 PolicySerializable policyPrivateKey,
283 PolicySerializable policyPublicKey)
287 (void)privateKeyAlias;
288 (void)publicKeyAlias,
289 (void)policyPrivateKey;
290 (void)policyPublicKey;
292 MessageBuffer response;
293 Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
294 Serialization::Serialize(response, commandId);
295 Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
297 return response.Pop();