febbf7f2825669aabde40b98482a6af2d51c4874
[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:
239         {
240             CryptoAlgorithmSerializable keyGenAlgorithm;
241             Name privateKeyName;
242             Label privateKeyLabel;
243             Name publicKeyName;
244             Label publicKeyLabel;
245             PolicySerializable policyPrivateKey;
246             PolicySerializable policyPublicKey;
247             buffer.Deserialize(keyGenAlgorithm,
248                                policyPrivateKey,
249                                policyPublicKey,
250                                privateKeyName,
251                                privateKeyLabel,
252                                publicKeyName,
253                                publicKeyLabel);
254             return m_logic->createKeyPair(
255                 cred,
256                 msgID,
257                 keyGenAlgorithm,
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 untrustedVector;
269             RawBufferVector trustedVector;
270             bool systemCerts;
271             buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
272             return m_logic->getCertificateChain(
273                 cred,
274                 msgID,
275                 certificate,
276                 untrustedVector,
277                 trustedVector,
278                 systemCerts);
279         }
280         case LogicCommand::GET_CHAIN_ALIAS:
281         {
282             RawBuffer certificate;
283             LabelNameVector untrustedVector;
284             LabelNameVector trustedVector;
285             bool systemCerts;
286             buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
287             return m_logic->getCertificateChain(
288                 cred,
289                 msgID,
290                 certificate,
291                 untrustedVector,
292                 trustedVector,
293                 systemCerts);
294         }
295         case LogicCommand::CREATE_SIGNATURE:
296         {
297             Password password;        // password for private_key
298             RawBuffer message;
299             int padding = 0, hash = 0;
300             buffer.Deserialize(name, label, password, message, hash, padding);
301             return m_logic->createSignature(
302                   cred,
303                   msgID,
304                   name,
305                   label,
306                   password,           // password for private_key
307                   message,
308                   static_cast<HashAlgorithm>(hash),
309                   static_cast<RSAPaddingAlgorithm>(padding));
310         }
311         case LogicCommand::VERIFY_SIGNATURE:
312         {
313             Password password;           // password for public_key (optional)
314             RawBuffer message;
315             RawBuffer signature;
316             //HashAlgorithm hash;
317             //RSAPaddingAlgorithm padding;
318             int padding = 0, hash = 0;
319             buffer.Deserialize(name,
320                                label,
321                                password,
322                                message,
323                                signature,
324                                hash,
325                                padding);
326             return m_logic->verifySignature(
327                 cred,
328                 msgID,
329                 name,
330                 label,
331                 password,           // password for public_key (optional)
332                 message,
333                 signature,
334                 static_cast<const HashAlgorithm>(hash),
335                 static_cast<const RSAPaddingAlgorithm>(padding));
336         }
337         case LogicCommand::SET_PERMISSION:
338         {
339             PermissionMask permissionMask = 0;
340             buffer.Deserialize(name, label, accessorLabel, permissionMask);
341             return m_logic->setPermission(
342                 cred,
343                 command,
344                 msgID,
345                 name,
346                 label,
347                 accessorLabel,
348                 permissionMask);
349         }
350         default:
351             Throw(Exception::BrokenProtocol);
352     }
353 }
354
355 } // namespace CKM
356