DBDataType refactoring.
[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 DBDataType::Exception::Base &e) {
99         LogError("Closing socket. DBDataType::Exception: " << e.DumpToString());
100     } catch (const std::string &e) {
101         LogError("String exception(" << e << "). Closing socket");
102     } catch (const std::exception &e) {
103         LogError("Std exception:: " << e.what());
104     } catch (...) {
105         LogError("Unknown exception. Closing socket.");
106     }
107
108     m_serviceManager->Close(conn);
109     return false;
110 }
111
112 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
113     int command = 0;
114     uid_t user = 0;
115     ControlCommand cc;
116     Password newPass, oldPass;
117     Label smackLabel;
118
119     buffer.Deserialize(command);
120
121     LogDebug("Process control. Command: " << command);
122
123     cc = static_cast<ControlCommand>(command);
124
125     switch(cc) {
126     case ControlCommand::UNLOCK_USER_KEY:
127         buffer.Deserialize(user, newPass);
128         return m_logic->unlockUserKey(user, newPass);
129     case ControlCommand::LOCK_USER_KEY:
130         buffer.Deserialize(user);
131         return m_logic->lockUserKey(user);
132     case ControlCommand::REMOVE_USER_DATA:
133         buffer.Deserialize(user);
134         return m_logic->removeUserData(user);
135     case ControlCommand::CHANGE_USER_PASSWORD:
136         buffer.Deserialize(user, oldPass, newPass);
137         return m_logic->changeUserPassword(user, oldPass, newPass);
138     case ControlCommand::RESET_USER_PASSWORD:
139         buffer.Deserialize(user, newPass);
140         return m_logic->resetUserPassword(user, newPass);
141     case ControlCommand::REMOVE_APP_DATA:
142         buffer.Deserialize(smackLabel);
143         return m_logic->removeApplicationData(smackLabel);
144     case ControlCommand::UPDATE_CC_MODE:
145         return m_logic->updateCCMode();
146     case ControlCommand::SET_PERMISSION:
147     {
148         Name name;
149         Label label;
150         Label accessorLabel;
151         int accessorPermissions = 0;
152
153         buffer.Deserialize(user, name, label, accessorLabel, accessorPermissions);
154         Credentials cred = { user, label };
155         return m_logic->setPermission(
156             cred,
157             command,
158             0, // dummy
159             name,
160             label,
161             accessorLabel,
162             static_cast<Permission>(accessorPermissions));
163     }
164     default:
165         Throw(Exception::BrokenProtocol);
166     }
167 }
168
169 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer)
170 {
171     int command = 0;
172     int msgID = 0;
173     int tmpDataType = 0;
174     Name name;
175     Label label, accessorLabel;
176     std::string user;
177
178     buffer.Deserialize(command);
179     buffer.Deserialize(msgID);
180
181     // This is a workaround solution for locktype=None in Tizen 2.2.1
182     // When locktype is None, lockscreen app doesn't interfere with unlocking process.
183     // Therefor lockscreen app cannot notify unlock events to key-manager when locktype is None.
184     // So, to unlock user data when lock type is None, key-manager always try to unlock user data with null password.
185     // Even if the result is fail, it will be ignored.
186     Password nullPassword("");
187     m_logic->unlockUserKey(cred.uid, nullPassword);
188
189     LogDebug("Process storage. Command: " << command);
190
191     switch(static_cast<LogicCommand>(command)) {
192         case LogicCommand::SAVE:
193         {
194             RawBuffer rawData;
195             PolicySerializable policy;
196             buffer.Deserialize(tmpDataType, name, label, rawData, policy);
197             return m_logic->saveData(
198                 cred,
199                 msgID,
200                 DBDataType(tmpDataType),
201                 name,
202                 label,
203                 rawData,
204                 policy);
205         }
206         case LogicCommand::REMOVE:
207         {
208             buffer.Deserialize(name, label);
209             return m_logic->removeData(
210                 cred,
211                 msgID,
212                 name,
213                 label);
214         }
215         case LogicCommand::GET:
216         {
217             Password password;
218             buffer.Deserialize(tmpDataType, name, label, password);
219             return m_logic->getData(
220                 cred,
221                 msgID,
222                 DBDataType(tmpDataType),
223                 name,
224                 label,
225                 password);
226         }
227         case LogicCommand::GET_LIST:
228         {
229             buffer.Deserialize(tmpDataType);
230             return m_logic->getDataList(
231                 cred,
232                 msgID,
233                 DBDataType(tmpDataType));
234         }
235         case LogicCommand::CREATE_KEY_PAIR_RSA:
236         case LogicCommand::CREATE_KEY_PAIR_DSA:
237         case LogicCommand::CREATE_KEY_PAIR_ECDSA:
238         {
239             int additional_param = 0;
240             Name privateKeyName;
241             Label privateKeyLabel;
242             Name publicKeyName;
243             Label publicKeyLabel;
244             PolicySerializable policyPrivateKey;
245             PolicySerializable policyPublicKey;
246             buffer.Deserialize(additional_param,
247                                policyPrivateKey,
248                                policyPublicKey,
249                                privateKeyName,
250                                privateKeyLabel,
251                                publicKeyName,
252                                publicKeyLabel);
253             return m_logic->createKeyPair(
254                 cred,
255                 static_cast<LogicCommand>(command),
256                 msgID,
257                 additional_param,
258                 privateKeyName,
259                 privateKeyLabel,
260                 publicKeyName,
261                 publicKeyLabel,
262                 policyPrivateKey,
263                 policyPublicKey);
264         }
265         case LogicCommand::GET_CHAIN_CERT:
266         {
267             RawBuffer certificate;
268             RawBufferVector rawBufferVector;
269             buffer.Deserialize(certificate, rawBufferVector);
270             return m_logic->getCertificateChain(
271                 cred,
272                 msgID,
273                 certificate,
274                 rawBufferVector);
275         }
276         case LogicCommand::GET_CHAIN_ALIAS:
277         {
278             RawBuffer certificate;
279             LabelNameVector untrusted_certs;
280             buffer.Deserialize(certificate, untrusted_certs);
281             return m_logic->getCertificateChain(
282                 cred,
283                 msgID,
284                 certificate,
285                 untrusted_certs);
286         }
287         case LogicCommand::CREATE_SIGNATURE:
288         {
289             Password password;        // password for private_key
290             RawBuffer message;
291             int padding = 0, hash = 0;
292             buffer.Deserialize(name, label, password, message, hash, padding);
293             return m_logic->createSignature(
294                   cred,
295                   msgID,
296                   name,
297                   label,
298                   password,           // password for private_key
299                   message,
300                   static_cast<HashAlgorithm>(hash),
301                   static_cast<RSAPaddingAlgorithm>(padding));
302         }
303         case LogicCommand::VERIFY_SIGNATURE:
304         {
305             Password password;           // password for public_key (optional)
306             RawBuffer message;
307             RawBuffer signature;
308             //HashAlgorithm hash;
309             //RSAPaddingAlgorithm padding;
310             int padding = 0, hash = 0;
311             buffer.Deserialize(name,
312                                label,
313                                password,
314                                message,
315                                signature,
316                                hash,
317                                padding);
318             return m_logic->verifySignature(
319                 cred,
320                 msgID,
321                 name,
322                 label,
323                 password,           // password for public_key (optional)
324                 message,
325                 signature,
326                 static_cast<const HashAlgorithm>(hash),
327                 static_cast<const RSAPaddingAlgorithm>(padding));
328         }
329         case LogicCommand::SET_PERMISSION:
330         {
331             int accessorPermissions = 0;
332             buffer.Deserialize(name, label, accessorLabel, accessorPermissions);
333             return m_logic->setPermission(
334                 cred,
335                 command,
336                 msgID,
337                 name,
338                 label,
339                 accessorLabel,
340                 static_cast<Permission>(accessorPermissions));
341         }
342         default:
343             Throw(Exception::BrokenProtocol);
344     }
345 }
346
347
348 void CKMService::close(const CloseEvent &event) {
349     LogDebug("Close event");
350     m_connectionInfoMap.erase(event.connectionID.counter);
351 }
352
353 } // namespace CKM
354