2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
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>
28 #include <dpl/serialization.h>
29 #include <dpl/log/log.h>
31 #include <ckm-service.h>
32 #include <ckm-logic.h>
36 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
37 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
38 } // namespace anonymous
42 CKMService::CKMService()
43 : m_logic(new CKMLogic)
46 CKMService::~CKMService() {
50 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
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}
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;
65 void CKMService::write(const WriteEvent &event) {
66 LogDebug("Write event (" << event.size << " bytes)");
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));
76 bool CKMService::processOne(
77 const ConnectionID &conn,
80 LogDebug ("process One");
84 if (!info.buffer.Ready())
87 if (info.interfaceID == SOCKET_ID_CONTROL)
88 response = processControl(info.buffer);
90 response = processStorage(info.credentials, info.buffer);
92 m_serviceManager->Write(conn, response);
95 } Catch (MessageBuffer::Exception::Base) {
96 LogError("Broken protocol. Closing socket.");
97 } catch (const std::string &e) {
98 LogError("String exception(" << e << "). Closing socket");
100 LogError("Unknown exception. Closing socket.");
103 m_serviceManager->Close(conn);
107 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
111 std::string newPass, oldPass;
113 Deserialization::Deserialize(buffer, command);
114 Deserialization::Deserialize(buffer, user);
116 cc = static_cast<ControlCommand>(command);
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);
135 throw 1; // broken protocol
139 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
147 Deserialization::Deserialize(buffer, command);
148 Deserialization::Deserialize(buffer, commandId);
150 sc = static_cast<LogicCommand>(command);
153 case LogicCommand::SAVE:
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(
164 static_cast<DBDataType>(tmpDataType),
169 case LogicCommand::REMOVE:
171 Deserialization::Deserialize(buffer, tmpDataType);
172 Deserialization::Deserialize(buffer, alias);
173 return m_logic->removeData(
176 static_cast<DBDataType>(tmpDataType),
179 case LogicCommand::GET:
181 std::string password;
182 Deserialization::Deserialize(buffer, tmpDataType);
183 Deserialization::Deserialize(buffer, alias);
184 Deserialization::Deserialize(buffer, password);
185 return m_logic->getData(
188 static_cast<DBDataType>(tmpDataType),
192 case LogicCommand::GET_LIST:
194 Deserialization::Deserialize(buffer, tmpDataType);
195 return m_logic->getDataList(
198 static_cast<DBDataType>(tmpDataType));
200 case LogicCommand::CREATE_KEY_PAIR_RSA:
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(
219 case LogicCommand::CREATE_KEY_PAIR_ECDSA:
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(
240 throw 1; // broken protocol
245 void CKMService::close(const CloseEvent &event) {
246 LogDebug("Close event");
247 m_connectionInfoMap.erase(event.connectionID.counter);