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