Implementation of Control::unlockUserKey
[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.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample 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 #include <key-impl.h>
34
35 namespace {
36 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
37 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
38 } // namespace anonymous
39
40 namespace CKM {
41
42 CKMService::CKMService()
43   : m_logic(new CKMLogic)
44 {}
45
46 CKMService::~CKMService() {
47     delete m_logic;
48 }
49
50 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
51 {
52     return ServiceDescriptionVector {
53         {SERVICE_SOCKET_CKM_CONTROL, "ckm::api-control", SOCKET_ID_CONTROL},
54         {SERVICE_SOCKET_CKM_STORAGE, "ckm::api-storage", SOCKET_ID_STORAGE}
55     };
56 }
57
58 void CKMService::accept(const AcceptEvent &event) {
59     LogDebug("Accept event");
60     auto &info = m_connectionInfoMap[event.connectionID.counter];
61     info.interfaceID = event.interfaceID;
62     info.credentials = event.credentials;
63 }
64
65 void CKMService::write(const WriteEvent &event) {
66     LogDebug("Write event (" << event.size << " bytes)");
67 }
68
69 void CKMService::process(const ReadEvent &event) {
70     LogDebug("Read event");
71     auto &info = m_connectionInfoMap[event.connectionID.counter];
72     info.buffer.Push(event.rawBuffer);
73     while(processOne(event.connectionID, info));
74 }
75
76 bool CKMService::processOne(
77     const ConnectionID &conn,
78     ConnectionInfo &info)
79 {
80     LogDebug ("process One");
81     RawBuffer response;
82
83     Try {
84         if (!info.buffer.Ready())
85             return false;
86
87         if (info.interfaceID == SOCKET_ID_CONTROL)
88             response = processControl(info.buffer);
89         else
90             response = processStorage(info.credentials, info.buffer);
91
92         m_serviceManager->Write(conn, response);
93
94         return true;
95     } Catch (MessageBuffer::Exception::Base) {
96         LogError("Broken protocol. Closing socket.");
97     } catch (const std::string &e) {
98         LogError("String exception(" << e << "). Closing socket");
99     } catch (...) {
100         LogError("Unknown exception. Closing socket.");
101     }
102
103     m_serviceManager->Close(conn);
104     return false;
105 }
106
107 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
108     int command;
109     uid_t user;
110     ControlCommand cc;
111     std::string newPass, oldPass;
112
113     Deserialization::Deserialize(buffer, command);
114     Deserialization::Deserialize(buffer, user);
115
116     cc = static_cast<ControlCommand>(command);
117
118     switch(cc) {
119     case ControlCommand::UNLOCK_USER_KEY:
120         Deserialization::Deserialize(buffer, newPass);
121         return m_logic->unlockUserKey(user, newPass);
122     case ControlCommand::LOCK_USER_KEY:
123         return m_logic->lockUserKey(user);
124     case ControlCommand::REMOVE_USER_DATA:
125         return m_logic->removeUserData(user);
126     case ControlCommand::CHANGE_USER_PASSWORD:
127         Deserialization::Deserialize(buffer, oldPass);
128         Deserialization::Deserialize(buffer, newPass);
129         return m_logic->changeUserPassword(user, oldPass, newPass);
130     case ControlCommand::RESET_USER_PASSWORD:
131         Deserialization::Deserialize(buffer, newPass);
132         return m_logic->resetUserPassword(user, newPass);
133     default:
134         // TODO
135         throw 1; // broken protocol
136     }
137 }
138
139 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
140     int command;
141     int commandId;
142     int tmpDataType;
143     Alias alias;
144     std::string user;
145     LogicCommand sc;
146
147     Deserialization::Deserialize(buffer, command);
148     Deserialization::Deserialize(buffer, commandId);
149
150     sc = static_cast<LogicCommand>(command);
151
152     switch(sc) {
153         case LogicCommand::SAVE:
154         {
155             RawBuffer rawData;
156             PolicySerializable policy;
157             Deserialization::Deserialize(buffer, tmpDataType);
158             Deserialization::Deserialize(buffer, alias);
159             Deserialization::Deserialize(buffer, rawData);
160             Deserialization::Deserialize(buffer, policy);
161             return m_logic->saveData(
162                 cred,
163                 commandId,
164                 static_cast<DBDataType>(tmpDataType),
165                 alias,
166                 rawData,
167                 policy);
168         }
169         case LogicCommand::REMOVE:
170         {
171             Deserialization::Deserialize(buffer, tmpDataType);
172             Deserialization::Deserialize(buffer, alias);
173             return m_logic->removeData(
174                 cred,
175                 commandId,
176                 static_cast<DBDataType>(tmpDataType),
177                 alias);
178         }
179         case LogicCommand::GET:
180         {
181             std::string password;
182             Deserialization::Deserialize(buffer, tmpDataType);
183             Deserialization::Deserialize(buffer, alias);
184             Deserialization::Deserialize(buffer, password);
185             return m_logic->getData(
186                 cred,
187                 commandId,
188                 static_cast<DBDataType>(tmpDataType),
189                 alias,
190                 password);
191         }
192         case LogicCommand::GET_LIST:
193         {
194             Deserialization::Deserialize(buffer, tmpDataType);
195             return m_logic->getDataList(
196                 cred,
197                 commandId,
198                 static_cast<DBDataType>(tmpDataType));
199         }
200         case LogicCommand::CREATE_KEY_PAIR_RSA:
201         {
202             int size;
203             Alias privateKeyAlias;
204             Alias publicKeyAlias;
205             PolicySerializable policyPrivateKey;
206             PolicySerializable policyPublicKey;
207             Deserialization::Deserialize(buffer, size);
208             Deserialization::Deserialize(buffer, policyPrivateKey);
209             Deserialization::Deserialize(buffer, policyPublicKey);
210             return m_logic->createKeyPairRSA(
211                 cred,
212                 commandId,
213                 size,
214                 privateKeyAlias,
215                 publicKeyAlias,
216                 policyPrivateKey,
217                 policyPublicKey);
218         }
219         case LogicCommand::CREATE_KEY_PAIR_ECDSA:
220         {
221             unsigned int type;
222             Alias privateKeyAlias;
223             Alias publicKeyAlias;
224             PolicySerializable policyPrivateKey;
225             PolicySerializable policyPublicKey;
226             Deserialization::Deserialize(buffer, type);
227             Deserialization::Deserialize(buffer, policyPrivateKey);
228             Deserialization::Deserialize(buffer, policyPublicKey);
229             return m_logic->createKeyPairECDSA(
230                 cred,
231                 commandId,
232                 type,
233                 privateKeyAlias,
234                 publicKeyAlias,
235                 policyPrivateKey,
236                 policyPublicKey);
237         }
238         default:
239         // TODO
240             throw 1; // broken protocol
241     }
242 }
243
244
245 void CKMService::close(const CloseEvent &event) {
246     LogDebug("Close event");
247     m_connectionInfoMap.erase(event.connectionID.counter);
248 }
249
250 } // namespace CKM
251