Integration ckm-logic with database module.
[platform/core/security/key-manager.git] / src / manager / service / ckm-logic.cpp
1 /*
2  *  Copyright (c) 2000 - 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  *
17  * @file        ckm-logic.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
22 #include <dpl/serialization.h>
23 #include <dpl/log/log.h>
24
25 #include <ckm/ckm-error.h>
26 #include <ckm/ckm-type.h>
27 #include <key-provider.h>
28 #include <file-system.h>
29
30 #include <ckm-logic.h>
31 namespace CKM {
32
33 CKMLogic::CKMLogic(){
34     int retCode = FileSystem::init();
35     // TODO what can I do when init went wrong? exit(-1) ??
36     if (retCode) {
37         LogError("Fatal error in FileSystem::init()");
38     }
39 }
40
41 CKMLogic::~CKMLogic(){}
42
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;
46
47     UserData &handle = m_userDataMap[user];
48
49     if (!(handle.keyProvider.isInitialized())) {
50         auto &handle = m_userDataMap[user];
51
52         FileSystem fs(user);
53         auto wrappedDomainKEK = fs.getDomainKEK();
54
55         if (wrappedDomainKEK.empty()) {
56             wrappedDomainKEK = KeyProvider::generateDomainKEK(std::to_string(user), password);
57             fs.saveDomainKEK(wrappedDomainKEK);
58         }
59
60         handle.keyProvider = KeyProvider(wrappedDomainKEK, password);
61         handle.database = DBCrypto(fs.getDBPath(), handle.keyProvider.getDomainKEK());
62     }
63
64     MessageBuffer response;
65     Serialization::Serialize(response, retCode);
66     return response.Pop();
67 }
68
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);
73
74     MessageBuffer response;
75     Serialization::Serialize(response, retCode);
76     return response.Pop();
77 }
78
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);
83
84     FileSystem fs(user);
85     fs.removeUserData();
86
87     MessageBuffer response;
88     Serialization::Serialize(response, retCode);
89     return response.Pop();
90 }
91
92 RawBuffer CKMLogic::changeUserPassword(
93     uid_t user,
94     const std::string &oldPassword,
95     const std::string &newPassword)
96 {
97     int retCode = KEY_MANAGER_API_SUCCESS;
98     // TODO try-catch
99     FileSystem fs(user);
100     auto wrappedDomainKEK = fs.getDomainKEK();
101     if (wrappedDomainKEK.empty()) {
102         retCode = KEY_MANAGER_API_ERROR_BAD_REQUEST;
103     } else {
104         wrappedDomainKEK = KeyProvider::reencrypt(wrappedDomainKEK, oldPassword, newPassword);
105         fs.saveDomainKEK(wrappedDomainKEK);
106     }
107     MessageBuffer response;
108     Serialization::Serialize(response, retCode);
109     return response.Pop();
110 }
111
112 RawBuffer CKMLogic::resetUserPassword(
113     uid_t user,
114     const std::string &newPassword)
115 {
116     int retCode = KEY_MANAGER_API_SUCCESS;
117     // TODO try-catch
118     if (0 == m_userDataMap.count(user)) {
119         retCode = KEY_MANAGER_API_ERROR_BAD_REQUEST;
120     } else {
121         auto &handler = m_userDataMap[user];
122         auto wrappedDomainKEK = handler.keyProvider.getDomainKEK(newPassword);
123         FileSystem fs(user);
124         fs.saveDomainKEK(wrappedDomainKEK);
125     }
126
127     MessageBuffer response;
128     Serialization::Serialize(response, retCode);
129     return response.Pop();
130 }
131
132 RawBuffer CKMLogic::saveData(
133     Credentials &cred,
134     int commandId,
135     DBDataType dataType,
136     const Alias &alias,
137     const RawBuffer &key,
138     const PolicySerializable &policy)
139 {
140     int retCode = KEY_MANAGER_API_SUCCESS;
141
142     if (0 == m_userDataMap.count(cred.uid)) {
143         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
144     } else {
145         RawBuffer iv(10,'c');
146
147         DBRow row = {
148             alias,
149             cred.smackLabel,
150             policy.restricted,
151             policy.extractable,
152             dataType,
153             0,
154             0,
155             iv,
156             key.size(),
157             key };
158
159         auto &handler = m_userDataMap[cred.uid];
160         retCode = handler.database.saveDBRow(row);
161     }
162
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));
168
169     return response.Pop();
170 }
171
172 RawBuffer CKMLogic::removeData(
173     Credentials &cred,
174     int commandId,
175     DBDataType dataType,
176     const Alias &alias)
177 {
178     (void)cred;
179     (void)alias;
180
181     int retCode = KEY_MANAGER_API_SUCCESS;
182
183     if (0 == m_userDataMap.count(cred.uid)) {
184         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
185     } else {
186         // TODO
187         auto &handler = m_userDataMap[cred.uid];
188         (void)handler;
189     }
190
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));
196
197     return response.Pop();
198 }
199
200 RawBuffer CKMLogic::getData(
201     Credentials &cred,
202     int commandId,
203     DBDataType dataType,
204     const Alias &alias,
205     const std::string &password)
206 {
207     (void)dataType;
208     (void)password;
209
210     int retCode = KEY_MANAGER_API_SUCCESS;
211     DBRow row;
212
213     if (0 == m_userDataMap.count(cred.uid)) {
214         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
215     } else {
216         auto &handler = m_userDataMap[cred.uid];
217         retCode = handler.database.getDBRow(alias, cred.smackLabel, row);
218     }
219
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();
227 }
228
229 RawBuffer CKMLogic::getDataList(
230     Credentials &cred,
231     int commandId,
232     DBDataType dataType)
233 {
234     int retCode = KEY_MANAGER_API_SUCCESS;
235
236     if (0 == m_userDataMap.count(cred.uid)) {
237         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
238     } else {
239         auto &handler = m_userDataMap[cred.uid];
240         // TODO
241         (void)handler;
242     }
243
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();
251 }
252
253 RawBuffer CKMLogic::createKeyPairRSA(
254     Credentials &cred,
255     int commandId,
256     int size,
257     const Alias &privateKeyAlias,
258     const Alias &publicKeyAlias,
259     PolicySerializable policyPrivateKey,
260     PolicySerializable policyPublicKey)
261
262     (void)cred;
263     (void)size;
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));
272  
273     return response.Pop();
274 }
275
276 RawBuffer CKMLogic::createKeyPairECDSA(
277     Credentials &cred,
278     int commandId,
279     int type,
280     const Alias &privateKeyAlias,
281     const Alias &publicKeyAlias,
282     PolicySerializable policyPrivateKey,
283     PolicySerializable policyPublicKey)
284 {
285     (void)cred;
286     (void)type;
287     (void)privateKeyAlias;
288     (void)publicKeyAlias,
289     (void)policyPrivateKey;
290     (void)policyPublicKey;
291     
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));
296  
297     return response.Pop();
298 }
299
300 } // namespace CKM
301