AES: add generation, save, get support.
[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_AES:
239         {
240             int size = 0;
241             Name keyName;
242             Label keyLabel;
243             PolicySerializable policyKey;
244             buffer.Deserialize(size,
245                                policyKey,
246                                keyName,
247                                keyLabel);
248             return m_logic->createKeyAES(
249                 cred,
250                 msgID,
251                 size,
252                 keyName,
253                 keyLabel,
254                 policyKey);
255         }
256         case LogicCommand::CREATE_KEY_PAIR:
257         {
258             CryptoAlgorithmSerializable keyGenAlgorithm;
259             Name privateKeyName;
260             Label privateKeyLabel;
261             Name publicKeyName;
262             Label publicKeyLabel;
263             PolicySerializable policyPrivateKey;
264             PolicySerializable policyPublicKey;
265             buffer.Deserialize(keyGenAlgorithm,
266                                policyPrivateKey,
267                                policyPublicKey,
268                                privateKeyName,
269                                privateKeyLabel,
270                                publicKeyName,
271                                publicKeyLabel);
272             return m_logic->createKeyPair(
273                 cred,
274                 msgID,
275                 keyGenAlgorithm,
276                 privateKeyName,
277                 privateKeyLabel,
278                 publicKeyName,
279                 publicKeyLabel,
280                 policyPrivateKey,
281                 policyPublicKey);
282         }
283         case LogicCommand::GET_CHAIN_CERT:
284         {
285             RawBuffer certificate;
286             RawBufferVector untrustedVector;
287             RawBufferVector 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::GET_CHAIN_ALIAS:
299         {
300             RawBuffer certificate;
301             LabelNameVector untrustedVector;
302             LabelNameVector trustedVector;
303             bool systemCerts;
304             buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
305             return m_logic->getCertificateChain(
306                 cred,
307                 msgID,
308                 certificate,
309                 untrustedVector,
310                 trustedVector,
311                 systemCerts);
312         }
313         case LogicCommand::CREATE_SIGNATURE:
314         {
315             Password password;        // password for private_key
316             RawBuffer message;
317             int padding = 0, hash = 0;
318             buffer.Deserialize(name, label, password, message, hash, padding);
319             return m_logic->createSignature(
320                   cred,
321                   msgID,
322                   name,
323                   label,
324                   password,           // password for private_key
325                   message,
326                   static_cast<HashAlgorithm>(hash),
327                   static_cast<RSAPaddingAlgorithm>(padding));
328         }
329         case LogicCommand::VERIFY_SIGNATURE:
330         {
331             Password password;           // password for public_key (optional)
332             RawBuffer message;
333             RawBuffer signature;
334             //HashAlgorithm hash;
335             //RSAPaddingAlgorithm padding;
336             int padding = 0, hash = 0;
337             buffer.Deserialize(name,
338                                label,
339                                password,
340                                message,
341                                signature,
342                                hash,
343                                padding);
344             return m_logic->verifySignature(
345                 cred,
346                 msgID,
347                 name,
348                 label,
349                 password,           // password for public_key (optional)
350                 message,
351                 signature,
352                 static_cast<const HashAlgorithm>(hash),
353                 static_cast<const RSAPaddingAlgorithm>(padding));
354         }
355         case LogicCommand::SET_PERMISSION:
356         {
357             PermissionMask permissionMask = 0;
358             buffer.Deserialize(name, label, accessorLabel, permissionMask);
359             return m_logic->setPermission(
360                 cred,
361                 command,
362                 msgID,
363                 name,
364                 label,
365                 accessorLabel,
366                 permissionMask);
367         }
368         default:
369             Throw(Exception::BrokenProtocol);
370     }
371 }
372
373 } // namespace CKM
374