Fix: key returned by getDomainKEK had wrong size.
[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.getPureDomainKEK());
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         FileSystem fs(user);
123         fs.saveDomainKEK(handler.keyProvider.getWrappedDomainKEK(newPassword));
124     }
125
126     MessageBuffer response;
127     Serialization::Serialize(response, retCode);
128     return response.Pop();
129 }
130
131 RawBuffer CKMLogic::saveData(
132     Credentials &cred,
133     int commandId,
134     DBDataType dataType,
135     const Alias &alias,
136     const RawBuffer &key,
137     const PolicySerializable &policy)
138 {
139     int retCode = KEY_MANAGER_API_SUCCESS;
140
141     if (0 == m_userDataMap.count(cred.uid)) {
142         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
143     } else {
144         RawBuffer iv(10,'c');
145
146         DBRow row = {
147             alias,
148             cred.smackLabel,
149             policy.restricted,
150             policy.extractable,
151             dataType,
152             0,
153             0,
154             iv,
155             key.size(),
156             key };
157
158         auto &handler = m_userDataMap[cred.uid];
159         retCode = handler.database.saveDBRow(row);
160     }
161
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));
167
168     return response.Pop();
169 }
170
171 RawBuffer CKMLogic::removeData(
172     Credentials &cred,
173     int commandId,
174     DBDataType dataType,
175     const Alias &alias)
176 {
177     (void)cred;
178     (void)alias;
179
180     int retCode = KEY_MANAGER_API_SUCCESS;
181
182     if (0 == m_userDataMap.count(cred.uid)) {
183         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
184     } else {
185         // TODO
186         auto &handler = m_userDataMap[cred.uid];
187         (void)handler;
188     }
189
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));
195
196     return response.Pop();
197 }
198
199 RawBuffer CKMLogic::getData(
200     Credentials &cred,
201     int commandId,
202     DBDataType dataType,
203     const Alias &alias,
204     const std::string &password)
205 {
206     (void)dataType;
207     (void)password;
208
209     int retCode = KEY_MANAGER_API_SUCCESS;
210     DBRow row;
211
212     if (0 == m_userDataMap.count(cred.uid)) {
213         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
214     } else {
215         auto &handler = m_userDataMap[cred.uid];
216         retCode = handler.database.getDBRow(alias, cred.smackLabel, row);
217     }
218
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();
226 }
227
228 RawBuffer CKMLogic::getDataList(
229     Credentials &cred,
230     int commandId,
231     DBDataType dataType)
232 {
233     int retCode = KEY_MANAGER_API_SUCCESS;
234
235     if (0 == m_userDataMap.count(cred.uid)) {
236         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
237     } else {
238         auto &handler = m_userDataMap[cred.uid];
239         // TODO
240         (void)handler;
241     }
242
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();
250 }
251
252 RawBuffer CKMLogic::createKeyPairRSA(
253     Credentials &cred,
254     int commandId,
255     int size,
256     const Alias &privateKeyAlias,
257     const Alias &publicKeyAlias,
258     PolicySerializable policyPrivateKey,
259     PolicySerializable policyPublicKey)
260
261     (void)cred;
262     (void)size;
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));
271  
272     return response.Pop();
273 }
274
275 RawBuffer CKMLogic::createKeyPairECDSA(
276     Credentials &cred,
277     int commandId,
278     int type,
279     const Alias &privateKeyAlias,
280     const Alias &publicKeyAlias,
281     PolicySerializable policyPrivateKey,
282     PolicySerializable policyPublicKey)
283 {
284     (void)cred;
285     (void)type;
286     (void)privateKeyAlias;
287     (void)publicKeyAlias,
288     (void)policyPrivateKey;
289     (void)policyPublicKey;
290     
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));
295  
296     return response.Pop();
297 }
298
299 } // namespace CKM
300