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.getPureDomainKEK());
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];
123 fs.saveDomainKEK(handler.keyProvider.getWrappedDomainKEK(newPassword));
126 MessageBuffer response;
127 Serialization::Serialize(response, retCode);
128 return response.Pop();
131 RawBuffer CKMLogic::saveData(
136 const RawBuffer &key,
137 const PolicySerializable &policy)
139 int retCode = KEY_MANAGER_API_SUCCESS;
141 if (0 == m_userDataMap.count(cred.uid)) {
142 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
144 RawBuffer iv(10,'c');
158 auto &handler = m_userDataMap[cred.uid];
159 retCode = handler.database.saveDBRow(row);
162 MessageBuffer response;
163 Serialization::Serialize(response, static_cast<int>(LogicCommand::SAVE));
164 Serialization::Serialize(response, commandId);
165 Serialization::Serialize(response, retCode);
166 Serialization::Serialize(response, static_cast<int>(dataType));
168 return response.Pop();
171 RawBuffer CKMLogic::removeData(
180 int retCode = KEY_MANAGER_API_SUCCESS;
182 if (0 == m_userDataMap.count(cred.uid)) {
183 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
186 auto &handler = m_userDataMap[cred.uid];
190 MessageBuffer response;
191 Serialization::Serialize(response, static_cast<int>(LogicCommand::REMOVE));
192 Serialization::Serialize(response, commandId);
193 Serialization::Serialize(response, retCode);
194 Serialization::Serialize(response, static_cast<int>(dataType));
196 return response.Pop();
199 RawBuffer CKMLogic::getData(
204 const std::string &password)
209 int retCode = KEY_MANAGER_API_SUCCESS;
212 if (0 == m_userDataMap.count(cred.uid)) {
213 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
215 auto &handler = m_userDataMap[cred.uid];
216 retCode = handler.database.getDBRow(alias, cred.smackLabel, row);
219 MessageBuffer response;
220 Serialization::Serialize(response, static_cast<int>(LogicCommand::GET));
221 Serialization::Serialize(response, commandId);
222 Serialization::Serialize(response, retCode);
223 Serialization::Serialize(response, static_cast<int>(row.dataType));
224 Serialization::Serialize(response, row.data);
225 return response.Pop();
228 RawBuffer CKMLogic::getDataList(
233 int retCode = KEY_MANAGER_API_SUCCESS;
235 if (0 == m_userDataMap.count(cred.uid)) {
236 retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
238 auto &handler = m_userDataMap[cred.uid];
243 MessageBuffer response;
244 Serialization::Serialize(response, static_cast<int>(LogicCommand::GET_LIST));
245 Serialization::Serialize(response, commandId);
246 Serialization::Serialize(response, retCode);
247 Serialization::Serialize(response, static_cast<int>(dataType));
248 Serialization::Serialize(response, AliasVector());
249 return response.Pop();
252 RawBuffer CKMLogic::createKeyPairRSA(
256 const Alias &privateKeyAlias,
257 const Alias &publicKeyAlias,
258 PolicySerializable policyPrivateKey,
259 PolicySerializable policyPublicKey)
263 (void)privateKeyAlias;
264 (void)publicKeyAlias,
265 (void)policyPrivateKey;
266 (void)policyPublicKey;
267 MessageBuffer response;
268 Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
269 Serialization::Serialize(response, commandId);
270 Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
272 return response.Pop();
275 RawBuffer CKMLogic::createKeyPairECDSA(
279 const Alias &privateKeyAlias,
280 const Alias &publicKeyAlias,
281 PolicySerializable policyPrivateKey,
282 PolicySerializable policyPublicKey)
286 (void)privateKeyAlias;
287 (void)publicKeyAlias,
288 (void)policyPrivateKey;
289 (void)policyPublicKey;
291 MessageBuffer response;
292 Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
293 Serialization::Serialize(response, commandId);
294 Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
296 return response.Pop();