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