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