Integration with CryptoService class.
[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
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, "ckm::api-control", SOCKET_ID_CONTROL},
53         {SERVICE_SOCKET_CKM_STORAGE, "ckm::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 (const std::string &e) {
97         LogError("String exception(" << e << "). Closing socket");
98     } catch (...) {
99         LogError("Unknown exception. Closing socket.");
100     }
101
102     m_serviceManager->Close(conn);
103     return false;
104 }
105
106 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
107     int command;
108     uid_t user;
109     ControlCommand cc;
110     std::string newPass, oldPass;
111
112     Deserialization::Deserialize(buffer, command);
113     Deserialization::Deserialize(buffer, user);
114
115     cc = static_cast<ControlCommand>(command);
116
117     switch(cc) {
118     case ControlCommand::UNLOCK_USER_KEY:
119         Deserialization::Deserialize(buffer, newPass);
120         return m_logic->unlockUserKey(user, newPass);
121     case ControlCommand::LOCK_USER_KEY:
122         return m_logic->lockUserKey(user);
123     case ControlCommand::REMOVE_USER_DATA:
124         return m_logic->removeUserData(user);
125     case ControlCommand::CHANGE_USER_PASSWORD:
126         Deserialization::Deserialize(buffer, oldPass);
127         Deserialization::Deserialize(buffer, newPass);
128         return m_logic->changeUserPassword(user, oldPass, newPass);
129     case ControlCommand::RESET_USER_PASSWORD:
130         Deserialization::Deserialize(buffer, newPass);
131         return m_logic->resetUserPassword(user, newPass);
132     default:
133         // TODO
134         throw 1; // broken protocol
135     }
136 }
137
138 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
139     int command;
140     int commandId;
141     int tmpDataType;
142     Alias alias;
143     std::string user;
144     LogicCommand sc;
145
146     Deserialization::Deserialize(buffer, command);
147     Deserialization::Deserialize(buffer, commandId);
148
149     sc = static_cast<LogicCommand>(command);
150
151     switch(sc) {
152         case LogicCommand::SAVE:
153         {
154             RawBuffer rawData;
155             PolicySerializable policy;
156             Deserialization::Deserialize(buffer, tmpDataType);
157             Deserialization::Deserialize(buffer, alias);
158             Deserialization::Deserialize(buffer, rawData);
159             Deserialization::Deserialize(buffer, policy);
160             return m_logic->saveData(
161                 cred,
162                 commandId,
163                 static_cast<DBDataType>(tmpDataType),
164                 alias,
165                 rawData,
166                 policy);
167         }
168         case LogicCommand::REMOVE:
169         {
170             Deserialization::Deserialize(buffer, tmpDataType);
171             Deserialization::Deserialize(buffer, alias);
172             return m_logic->removeData(
173                 cred,
174                 commandId,
175                 static_cast<DBDataType>(tmpDataType),
176                 alias);
177         }
178         case LogicCommand::GET:
179         {
180             std::string password;
181             Deserialization::Deserialize(buffer, tmpDataType);
182             Deserialization::Deserialize(buffer, alias);
183             Deserialization::Deserialize(buffer, password);
184             return m_logic->getData(
185                 cred,
186                 commandId,
187                 static_cast<DBDataType>(tmpDataType),
188                 alias,
189                 password);
190         }
191         case LogicCommand::GET_LIST:
192         {
193             Deserialization::Deserialize(buffer, tmpDataType);
194             return m_logic->getDataList(
195                 cred,
196                 commandId,
197                 static_cast<DBDataType>(tmpDataType));
198         }
199         case LogicCommand::CREATE_KEY_PAIR_RSA:
200         {
201             int size;
202             Alias privateKeyAlias;
203             Alias publicKeyAlias;
204             PolicySerializable policyPrivateKey;
205             PolicySerializable policyPublicKey;
206             Deserialization::Deserialize(buffer, size);
207             Deserialization::Deserialize(buffer, policyPrivateKey);
208             Deserialization::Deserialize(buffer, policyPublicKey);
209             Deserialization::Deserialize(buffer, privateKeyAlias);
210             Deserialization::Deserialize(buffer, publicKeyAlias);
211             return m_logic->createKeyPairRSA(
212                 cred,
213                 commandId,
214                 size,
215                 privateKeyAlias,
216                 publicKeyAlias,
217                 policyPrivateKey,
218                 policyPublicKey);
219         }
220         case LogicCommand::CREATE_KEY_PAIR_ECDSA:
221         {
222             unsigned int type;
223             Alias privateKeyAlias;
224             Alias publicKeyAlias;
225             PolicySerializable policyPrivateKey;
226             PolicySerializable policyPublicKey;
227             Deserialization::Deserialize(buffer, type);
228             Deserialization::Deserialize(buffer, policyPrivateKey);
229             Deserialization::Deserialize(buffer, policyPublicKey);
230             Deserialization::Deserialize(buffer, privateKeyAlias);
231             Deserialization::Deserialize(buffer, publicKeyAlias);
232             return m_logic->createKeyPairECDSA(
233                 cred,
234                 commandId,
235                 type,
236                 privateKeyAlias,
237                 publicKeyAlias,
238                 policyPrivateKey,
239                 policyPublicKey);
240         }
241         default:
242         // TODO
243             throw 1; // broken protocol
244     }
245 }
246
247
248 void CKMService::close(const CloseEvent &event) {
249     LogDebug("Close event");
250     m_connectionInfoMap.erase(event.connectionID.counter);
251 }
252
253 } // namespace CKM
254