Add system database - managed by service (uid<5000) users, accessible by priviledged...
[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
23 #include <protocols.h>
24
25 #include <dpl/serialization.h>
26 #include <dpl/log/log.h>
27
28 #include <ckm-service.h>
29 #include <ckm-logic.h>
30
31 namespace {
32 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
33 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
34 } // namespace anonymous
35
36 namespace CKM {
37
38 CKMService::CKMService()
39   : m_logic(new CKMLogic)
40 {}
41
42 CKMService::~CKMService() {
43     delete m_logic;
44 }
45
46 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
47 {
48     return ServiceDescriptionVector {
49         {SERVICE_SOCKET_CKM_CONTROL, "key-manager::api-control", SOCKET_ID_CONTROL},
50         {SERVICE_SOCKET_CKM_STORAGE, "key-manager::api-storage", SOCKET_ID_STORAGE}
51     };
52 }
53
54 bool CKMService::ProcessOne(
55     const ConnectionID &conn,
56     ConnectionInfo &info)
57 {
58     LogDebug ("process One");
59     RawBuffer response;
60
61     Try {
62         if (!info.buffer.Ready())
63             return false;
64
65         if (info.interfaceID == SOCKET_ID_CONTROL)
66             response = ProcessControl(info.buffer);
67         else
68             response = ProcessStorage(info.credentials, info.buffer);
69
70         m_serviceManager->Write(conn, response);
71
72         return true;
73     } Catch (MessageBuffer::Exception::Base) {
74         LogError("Broken protocol. Closing socket.");
75     } Catch (Exception::BrokenProtocol) {
76         LogError("Broken protocol. Closing socket.");
77     } catch (const DataType::Exception::Base &e) {
78         LogError("Closing socket. DBDataType::Exception: " << e.DumpToString());
79     } catch (const std::string &e) {
80         LogError("String exception(" << e << "). Closing socket");
81     } catch (const std::exception &e) {
82         LogError("Std exception:: " << e.what());
83     } catch (...) {
84         LogError("Unknown exception. Closing socket.");
85     }
86
87     m_serviceManager->Close(conn);
88     return false;
89 }
90
91 RawBuffer CKMService::ProcessControl(MessageBuffer &buffer) {
92     int command = 0;
93     uid_t user = 0;
94     ControlCommand cc;
95     Password newPass, oldPass;
96     Label smackLabel;
97
98     buffer.Deserialize(command);
99
100     LogDebug("Process control. Command: " << command);
101
102     cc = static_cast<ControlCommand>(command);
103
104     switch(cc) {
105     case ControlCommand::UNLOCK_USER_KEY:
106         buffer.Deserialize(user, newPass);
107         return m_logic->unlockUserKey(user, newPass);
108     case ControlCommand::LOCK_USER_KEY:
109         buffer.Deserialize(user);
110         return m_logic->lockUserKey(user);
111     case ControlCommand::REMOVE_USER_DATA:
112         buffer.Deserialize(user);
113         return m_logic->removeUserData(user);
114     case ControlCommand::CHANGE_USER_PASSWORD:
115         buffer.Deserialize(user, oldPass, newPass);
116         return m_logic->changeUserPassword(user, oldPass, newPass);
117     case ControlCommand::RESET_USER_PASSWORD:
118         buffer.Deserialize(user, newPass);
119         return m_logic->resetUserPassword(user, newPass);
120     case ControlCommand::REMOVE_APP_DATA:
121         buffer.Deserialize(smackLabel);
122         return m_logic->removeApplicationData(smackLabel);
123     case ControlCommand::UPDATE_CC_MODE:
124         return m_logic->updateCCMode();
125     case ControlCommand::SET_PERMISSION:
126     {
127         Name name;
128         Label label;
129         Label accessorLabel;
130         PermissionMask permissionMask = 0;
131
132         buffer.Deserialize(user, name, label, accessorLabel, permissionMask);
133
134         Credentials cred(user, label);
135         return m_logic->setPermission(
136             cred,
137             command,
138             0, // dummy
139             name,
140             label,
141             accessorLabel,
142             permissionMask);
143     }
144     default:
145         Throw(Exception::BrokenProtocol);
146     }
147 }
148
149 RawBuffer CKMService::ProcessStorage(Credentials &cred, MessageBuffer &buffer)
150 {
151     int command = 0;
152     int msgID = 0;
153     int tmpDataType = 0;
154     Name name;
155     Label label, accessorLabel;
156     std::string user;
157
158     buffer.Deserialize(command);
159     buffer.Deserialize(msgID);
160
161     LogDebug("Process storage. Command: " << command);
162
163     switch(static_cast<LogicCommand>(command)) {
164         case LogicCommand::SAVE:
165         {
166             RawBuffer rawData;
167             PolicySerializable policy;
168             buffer.Deserialize(tmpDataType, name, label, rawData, policy);
169             return m_logic->saveData(
170                 cred,
171                 msgID,
172                 name,
173                 label,
174                 rawData,
175                 DataType(tmpDataType),
176                 policy);
177         }
178         case LogicCommand::SAVE_PKCS12:
179         {
180             RawBuffer rawData;
181             PKCS12Serializable pkcs;
182             PolicySerializable keyPolicy, certPolicy;
183             buffer.Deserialize(name, label, pkcs, keyPolicy, certPolicy);
184             return m_logic->savePKCS12(
185                 cred,
186                 msgID,
187                 name,
188                 label,
189                 pkcs,
190                 keyPolicy,
191                 certPolicy);
192         }
193         case LogicCommand::REMOVE:
194         {
195             buffer.Deserialize(name, label);
196             return m_logic->removeData(
197                 cred,
198                 msgID,
199                 name,
200                 label);
201         }
202         case LogicCommand::GET:
203         {
204             Password password;
205             buffer.Deserialize(tmpDataType, name, label, password);
206             return m_logic->getData(
207                 cred,
208                 msgID,
209                 DataType(tmpDataType),
210                 name,
211                 label,
212                 password);
213         }
214         case LogicCommand::GET_PKCS12:
215         {
216             Password passKey;
217             Password passCert;
218             buffer.Deserialize(name,
219                                label,
220                                passKey,
221                                passCert);
222             return m_logic->getPKCS12(
223                 cred,
224                 msgID,
225                 name,
226                 label,
227                 passKey,
228                 passCert);
229         }
230         case LogicCommand::GET_LIST:
231         {
232             buffer.Deserialize(tmpDataType);
233             return m_logic->getDataList(
234                 cred,
235                 msgID,
236                 DataType(tmpDataType));
237         }
238         case LogicCommand::CREATE_KEY_PAIR_RSA:
239         case LogicCommand::CREATE_KEY_PAIR_DSA:
240         case LogicCommand::CREATE_KEY_PAIR_ECDSA:
241         {
242             int additional_param = 0;
243             Name privateKeyName;
244             Label privateKeyLabel;
245             Name publicKeyName;
246             Label publicKeyLabel;
247             PolicySerializable policyPrivateKey;
248             PolicySerializable policyPublicKey;
249             buffer.Deserialize(additional_param,
250                                policyPrivateKey,
251                                policyPublicKey,
252                                privateKeyName,
253                                privateKeyLabel,
254                                publicKeyName,
255                                publicKeyLabel);
256             return m_logic->createKeyPair(
257                 cred,
258                 static_cast<LogicCommand>(command),
259                 msgID,
260                 additional_param,
261                 privateKeyName,
262                 privateKeyLabel,
263                 publicKeyName,
264                 publicKeyLabel,
265                 policyPrivateKey,
266                 policyPublicKey);
267         }
268         case LogicCommand::GET_CHAIN_CERT:
269         {
270             RawBuffer certificate;
271             RawBufferVector untrustedVector;
272             RawBufferVector trustedVector;
273             bool systemCerts;
274             buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
275             return m_logic->getCertificateChain(
276                 cred,
277                 msgID,
278                 certificate,
279                 untrustedVector,
280                 trustedVector,
281                 systemCerts);
282         }
283         case LogicCommand::GET_CHAIN_ALIAS:
284         {
285             RawBuffer certificate;
286             LabelNameVector untrustedVector;
287             LabelNameVector trustedVector;
288             bool systemCerts;
289             buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
290             return m_logic->getCertificateChain(
291                 cred,
292                 msgID,
293                 certificate,
294                 untrustedVector,
295                 trustedVector,
296                 systemCerts);
297         }
298         case LogicCommand::CREATE_SIGNATURE:
299         {
300             Password password;        // password for private_key
301             RawBuffer message;
302             int padding = 0, hash = 0;
303             buffer.Deserialize(name, label, password, message, hash, padding);
304             return m_logic->createSignature(
305                   cred,
306                   msgID,
307                   name,
308                   label,
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             Password password;           // password for public_key (optional)
317             RawBuffer message;
318             RawBuffer signature;
319             //HashAlgorithm hash;
320             //RSAPaddingAlgorithm padding;
321             int padding = 0, hash = 0;
322             buffer.Deserialize(name,
323                                label,
324                                password,
325                                message,
326                                signature,
327                                hash,
328                                padding);
329             return m_logic->verifySignature(
330                 cred,
331                 msgID,
332                 name,
333                 label,
334                 password,           // password for public_key (optional)
335                 message,
336                 signature,
337                 static_cast<const HashAlgorithm>(hash),
338                 static_cast<const RSAPaddingAlgorithm>(padding));
339         }
340         case LogicCommand::SET_PERMISSION:
341         {
342             PermissionMask permissionMask = 0;
343             buffer.Deserialize(name, label, accessorLabel, permissionMask);
344             return m_logic->setPermission(
345                 cred,
346                 command,
347                 msgID,
348                 name,
349                 label,
350                 accessorLabel,
351                 permissionMask);
352         }
353         default:
354             Throw(Exception::BrokenProtocol);
355     }
356 }
357
358 } // namespace CKM
359