Add generic serialization/deserialization methods
[platform/core/security/key-manager.git] / src / manager / service / ckm-service.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-service.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       CKM service implementation.
21  */
22 #include <service-thread.h>
23 #include <generic-socket-manager.h>
24 #include <connection-info.h>
25 #include <message-buffer.h>
26 #include <protocols.h>
27
28 #include <dpl/serialization.h>
29 #include <dpl/log/log.h>
30
31 #include <ckm-service.h>
32 #include <ckm-logic.h>
33
34 namespace {
35 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
36 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
37 } // namespace anonymous
38
39 namespace CKM {
40
41 CKMService::CKMService()
42   : m_logic(new CKMLogic)
43 {}
44
45 CKMService::~CKMService() {
46     delete m_logic;
47 }
48
49 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
50 {
51     return ServiceDescriptionVector {
52         {SERVICE_SOCKET_CKM_CONTROL, "key-manager::api-control", SOCKET_ID_CONTROL},
53         {SERVICE_SOCKET_CKM_STORAGE, "key-manager::api-storage", SOCKET_ID_STORAGE}
54     };
55 }
56
57 void CKMService::accept(const AcceptEvent &event) {
58     LogDebug("Accept event");
59     auto &info = m_connectionInfoMap[event.connectionID.counter];
60     info.interfaceID = event.interfaceID;
61     info.credentials = event.credentials;
62 }
63
64 void CKMService::write(const WriteEvent &event) {
65     LogDebug("Write event (" << event.size << " bytes)");
66 }
67
68 void CKMService::process(const ReadEvent &event) {
69     LogDebug("Read event");
70     auto &info = m_connectionInfoMap[event.connectionID.counter];
71     info.buffer.Push(event.rawBuffer);
72     while(processOne(event.connectionID, info));
73 }
74
75 bool CKMService::processOne(
76     const ConnectionID &conn,
77     ConnectionInfo &info)
78 {
79     LogDebug ("process One");
80     RawBuffer response;
81
82     Try {
83         if (!info.buffer.Ready())
84             return false;
85
86         if (info.interfaceID == SOCKET_ID_CONTROL)
87             response = processControl(info.buffer);
88         else
89             response = processStorage(info.credentials, info.buffer);
90
91         m_serviceManager->Write(conn, response);
92
93         return true;
94     } Catch (MessageBuffer::Exception::Base) {
95         LogError("Broken protocol. Closing socket.");
96     } Catch (Exception::BrokenProtocol) {
97         LogError("Broken protocol. Closing socket.");
98     } catch (const std::string &e) {
99         LogError("String exception(" << e << "). Closing socket");
100     } catch (...) {
101         LogError("Unknown exception. Closing socket.");
102     }
103
104     m_serviceManager->Close(conn);
105     return false;
106 }
107
108 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
109     int command;
110     int cc_mode_status;
111     uid_t user;
112     ControlCommand cc;
113     Password newPass, oldPass;
114     std::string smackLabel;
115
116     buffer.Deserialize(command);
117
118     LogDebug("Process control. Command: " << command);
119
120     cc = static_cast<ControlCommand>(command);
121
122     switch(cc) {
123     case ControlCommand::UNLOCK_USER_KEY:
124         buffer.Deserialize(user, newPass);
125         return m_logic->unlockUserKey(user, newPass);
126     case ControlCommand::LOCK_USER_KEY:
127         buffer.Deserialize(user);
128         return m_logic->lockUserKey(user);
129     case ControlCommand::REMOVE_USER_DATA:
130         buffer.Deserialize(user);
131         return m_logic->removeUserData(user);
132     case ControlCommand::CHANGE_USER_PASSWORD:
133         buffer.Deserialize(user, oldPass, newPass);
134         return m_logic->changeUserPassword(user, oldPass, newPass);
135     case ControlCommand::RESET_USER_PASSWORD:
136         buffer.Deserialize(user, newPass);
137         return m_logic->resetUserPassword(user, newPass);
138     case ControlCommand::REMOVE_APP_DATA:
139         buffer.Deserialize(smackLabel);
140         return m_logic->removeApplicationData(smackLabel);
141     case ControlCommand::SET_CC_MODE:
142         buffer.Deserialize(cc_mode_status);
143         return m_logic->setCCModeStatus(static_cast<CCModeState>(cc_mode_status));
144     case ControlCommand::ALLOW_ACCESS:
145     {
146         std::string owner;
147         std::string item_alias;
148         std::string accessor_label;
149         int req_rights;
150
151         buffer.Deserialize(user, owner, item_alias, accessor_label, req_rights);
152         Credentials cred =
153             {
154                 user,
155                 owner
156             };
157         return m_logic->allowAccess(
158             cred,
159             command,
160             0, // dummy
161             item_alias,
162             accessor_label,
163             static_cast<AccessRight>(req_rights));
164     }
165     case ControlCommand::DENY_ACCESS:
166     {
167         std::string owner;
168         std::string item_alias;
169         std::string accessor_label;
170
171         buffer.Deserialize(user, owner, item_alias, accessor_label);
172         Credentials cred =
173             {
174                 user,
175                 owner
176             };
177         return m_logic->denyAccess(
178             cred,
179             command,
180             0, // dummy
181             item_alias,
182             accessor_label);
183     }
184     default:
185         Throw(Exception::BrokenProtocol);
186     }
187 }
188
189 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer)
190 {
191     int command;
192     int msgID;
193     int tmpDataType;
194     Alias alias;
195     std::string user;
196
197     buffer.Deserialize(command);
198     buffer.Deserialize(msgID);
199
200     // This is a workaround solution for locktype=None in Tizen 2.2.1
201     // When locktype is None, lockscreen app doesn't interfere with unlocking process.
202     // Therefor lockscreen app cannot notify unlock events to key-manager when locktype is None.
203     // So, to unlock user data when lock type is None, key-manager always try to unlock user data with null password.
204     // Even if the result is fail, it will be ignored.
205     Password nullPassword("");
206     m_logic->unlockUserKey(cred.uid, nullPassword);
207
208     LogDebug("Process storage. Command: " << command);
209
210     switch(static_cast<LogicCommand>(command)) {
211         case LogicCommand::SAVE:
212         {
213             RawBuffer rawData;
214             PolicySerializable policy;
215             buffer.Deserialize(tmpDataType, alias, rawData, policy);
216             return m_logic->saveData(
217                 cred,
218                 msgID,
219                 static_cast<DBDataType>(tmpDataType),
220                 alias,
221                 rawData,
222                 policy);
223         }
224         case LogicCommand::REMOVE:
225         {
226             buffer.Deserialize(tmpDataType, alias);
227             return m_logic->removeData(
228                 cred,
229                 msgID,
230                 static_cast<DBDataType>(tmpDataType),
231                 alias);
232         }
233         case LogicCommand::GET:
234         {
235             Password password;
236             buffer.Deserialize(tmpDataType, alias, password);
237             return m_logic->getData(
238                 cred,
239                 msgID,
240                 static_cast<DBDataType>(tmpDataType),
241                 alias,
242                 password);
243         }
244         case LogicCommand::GET_LIST:
245         {
246             buffer.Deserialize(tmpDataType);
247             return m_logic->getDataList(
248                 cred,
249                 msgID,
250                 static_cast<DBDataType>(tmpDataType));
251         }
252         case LogicCommand::CREATE_KEY_PAIR_RSA:
253         case LogicCommand::CREATE_KEY_PAIR_DSA:
254         case LogicCommand::CREATE_KEY_PAIR_ECDSA:
255         {
256             int additional_param;
257             Alias privateKeyAlias;
258             Alias publicKeyAlias;
259             PolicySerializable policyPrivateKey;
260             PolicySerializable policyPublicKey;
261             buffer.Deserialize(additional_param,
262                                policyPrivateKey,
263                                policyPublicKey,
264                                privateKeyAlias,
265                                publicKeyAlias);
266             return m_logic->createKeyPair(
267                 cred,
268                 static_cast<LogicCommand>(command),
269                 msgID,
270                 additional_param,
271                 privateKeyAlias,
272                 publicKeyAlias,
273                 policyPrivateKey,
274                 policyPublicKey);
275         }
276         case LogicCommand::GET_CHAIN_CERT:
277         {
278             RawBuffer certificate;
279             RawBufferVector rawBufferVector;
280             buffer.Deserialize(certificate, rawBufferVector);
281             return m_logic->getCertificateChain(
282                 cred,
283                 msgID,
284                 certificate,
285                 rawBufferVector);
286         }
287         case LogicCommand::GET_CHAIN_ALIAS:
288         {
289             RawBuffer certificate;
290             AliasVector aliasVector;
291             buffer.Deserialize(certificate, aliasVector);
292             return m_logic->getCertificateChain(
293                 cred,
294                 msgID,
295                 certificate,
296                 aliasVector);
297         }
298         case LogicCommand::CREATE_SIGNATURE:
299         {
300             Alias privateKeyAlias;
301             Password password;        // password for private_key
302             RawBuffer message;
303             int padding, hash;
304             buffer.Deserialize(privateKeyAlias, password, message, hash, padding);
305             return m_logic->createSignature(
306                   cred,
307                   msgID,
308                   privateKeyAlias,
309                   password,           // password for private_key
310                   message,
311                   static_cast<HashAlgorithm>(hash),
312                   static_cast<RSAPaddingAlgorithm>(padding));
313         }
314         case LogicCommand::VERIFY_SIGNATURE:
315         {
316             Alias publicKeyOrCertAlias;
317             Password password;           // password for public_key (optional)
318             RawBuffer message;
319             RawBuffer signature;
320             //HashAlgorithm hash;
321             //RSAPaddingAlgorithm padding;
322             int padding, hash;
323             buffer.Deserialize(publicKeyOrCertAlias,
324                                password,
325                                message,
326                                signature,
327                                hash,
328                                padding);
329             return m_logic->verifySignature(
330                 cred,
331                 msgID,
332                 publicKeyOrCertAlias,
333                 password,           // password for public_key (optional)
334                 message,
335                 signature,
336                 static_cast<const HashAlgorithm>(hash),
337                 static_cast<const RSAPaddingAlgorithm>(padding));
338         }
339         case LogicCommand::ALLOW_ACCESS:
340         {
341             Alias item_alias;
342             std::string accessor_label;
343             int req_rights;
344             buffer.Deserialize(item_alias, accessor_label, req_rights);
345             return m_logic->allowAccess(
346                 cred,
347                 command,
348                 msgID,
349                 item_alias,
350                 accessor_label,
351                 static_cast<AccessRight>(req_rights));
352         }
353         case LogicCommand::DENY_ACCESS:
354         {
355             Alias item_alias;
356             std::string accessor_label;
357             buffer.Deserialize(item_alias, accessor_label);
358             return m_logic->denyAccess(
359                 cred,
360                 command,
361                 msgID,
362                 item_alias,
363                 accessor_label);
364         }
365         default:
366             Throw(Exception::BrokenProtocol);
367     }
368 }
369
370
371 void CKMService::close(const CloseEvent &event) {
372     LogDebug("Close event");
373     m_connectionInfoMap.erase(event.connectionID.counter);
374 }
375
376 } // namespace CKM
377