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
17 * @file ckm-service.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief CKM 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>
35 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
36 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
37 } // namespace anonymous
41 CKMService::CKMService()
42 : m_logic(new CKMLogic)
45 CKMService::~CKMService() {
49 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
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}
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;
64 void CKMService::write(const WriteEvent &event) {
65 LogDebug("Write event (" << event.size << " bytes)");
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));
75 bool CKMService::processOne(
76 const ConnectionID &conn,
79 LogDebug ("process One");
83 if (!info.buffer.Ready())
86 if (info.interfaceID == SOCKET_ID_CONTROL)
87 response = processControl(info.buffer);
89 response = processStorage(info.credentials, info.buffer);
91 m_serviceManager->Write(conn, response);
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");
101 LogError("Unknown exception. Closing socket.");
104 m_serviceManager->Close(conn);
108 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
112 Password newPass, oldPass;
113 std::string smackLabel;
115 buffer.Deserialize(command);
117 LogDebug("Process control. Command: " << command);
119 cc = static_cast<ControlCommand>(command);
122 case ControlCommand::UNLOCK_USER_KEY:
123 buffer.Deserialize(user, newPass);
124 return m_logic->unlockUserKey(user, newPass);
125 case ControlCommand::LOCK_USER_KEY:
126 buffer.Deserialize(user);
127 return m_logic->lockUserKey(user);
128 case ControlCommand::REMOVE_USER_DATA:
129 buffer.Deserialize(user);
130 return m_logic->removeUserData(user);
131 case ControlCommand::CHANGE_USER_PASSWORD:
132 buffer.Deserialize(user, oldPass, newPass);
133 return m_logic->changeUserPassword(user, oldPass, newPass);
134 case ControlCommand::RESET_USER_PASSWORD:
135 buffer.Deserialize(user, newPass);
136 return m_logic->resetUserPassword(user, newPass);
137 case ControlCommand::REMOVE_APP_DATA:
138 buffer.Deserialize(smackLabel);
139 return m_logic->removeApplicationData(smackLabel);
140 case ControlCommand::UPDATE_CC_MODE:
141 return m_logic->updateCCMode();
142 case ControlCommand::ALLOW_ACCESS:
145 std::string item_alias;
146 std::string accessor_label;
149 buffer.Deserialize(user, owner, item_alias, accessor_label, req_rights);
155 return m_logic->allowAccess(
161 static_cast<AccessRight>(req_rights));
163 case ControlCommand::DENY_ACCESS:
166 std::string item_alias;
167 std::string accessor_label;
169 buffer.Deserialize(user, owner, item_alias, accessor_label);
175 return m_logic->denyAccess(
183 Throw(Exception::BrokenProtocol);
187 RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer)
195 buffer.Deserialize(command);
196 buffer.Deserialize(msgID);
198 // This is a workaround solution for locktype=None in Tizen 2.2.1
199 // When locktype is None, lockscreen app doesn't interfere with unlocking process.
200 // Therefor lockscreen app cannot notify unlock events to key-manager when locktype is None.
201 // So, to unlock user data when lock type is None, key-manager always try to unlock user data with null password.
202 // Even if the result is fail, it will be ignored.
203 Password nullPassword("");
204 m_logic->unlockUserKey(cred.uid, nullPassword);
206 LogDebug("Process storage. Command: " << command);
208 switch(static_cast<LogicCommand>(command)) {
209 case LogicCommand::SAVE:
212 PolicySerializable policy;
213 buffer.Deserialize(tmpDataType, alias, rawData, policy);
214 return m_logic->saveData(
217 static_cast<DBDataType>(tmpDataType),
222 case LogicCommand::REMOVE:
224 buffer.Deserialize(tmpDataType, alias);
225 return m_logic->removeData(
228 static_cast<DBDataType>(tmpDataType),
231 case LogicCommand::GET:
234 buffer.Deserialize(tmpDataType, alias, password);
235 return m_logic->getData(
238 static_cast<DBDataType>(tmpDataType),
242 case LogicCommand::GET_LIST:
244 buffer.Deserialize(tmpDataType);
245 return m_logic->getDataList(
248 static_cast<DBDataType>(tmpDataType));
250 case LogicCommand::CREATE_KEY_PAIR_RSA:
251 case LogicCommand::CREATE_KEY_PAIR_DSA:
252 case LogicCommand::CREATE_KEY_PAIR_ECDSA:
254 int additional_param;
255 Alias privateKeyAlias;
256 Alias publicKeyAlias;
257 PolicySerializable policyPrivateKey;
258 PolicySerializable policyPublicKey;
259 buffer.Deserialize(additional_param,
264 return m_logic->createKeyPair(
266 static_cast<LogicCommand>(command),
274 case LogicCommand::GET_CHAIN_CERT:
276 RawBuffer certificate;
277 RawBufferVector rawBufferVector;
278 buffer.Deserialize(certificate, rawBufferVector);
279 return m_logic->getCertificateChain(
285 case LogicCommand::GET_CHAIN_ALIAS:
287 RawBuffer certificate;
288 AliasVector aliasVector;
289 buffer.Deserialize(certificate, aliasVector);
290 return m_logic->getCertificateChain(
296 case LogicCommand::CREATE_SIGNATURE:
298 Alias privateKeyAlias;
299 Password password; // password for private_key
302 buffer.Deserialize(privateKeyAlias, password, message, hash, padding);
303 return m_logic->createSignature(
307 password, // password for private_key
309 static_cast<HashAlgorithm>(hash),
310 static_cast<RSAPaddingAlgorithm>(padding));
312 case LogicCommand::VERIFY_SIGNATURE:
314 Alias publicKeyOrCertAlias;
315 Password password; // password for public_key (optional)
318 //HashAlgorithm hash;
319 //RSAPaddingAlgorithm padding;
321 buffer.Deserialize(publicKeyOrCertAlias,
327 return m_logic->verifySignature(
330 publicKeyOrCertAlias,
331 password, // password for public_key (optional)
334 static_cast<const HashAlgorithm>(hash),
335 static_cast<const RSAPaddingAlgorithm>(padding));
337 case LogicCommand::ALLOW_ACCESS:
340 std::string accessor_label;
342 buffer.Deserialize(item_alias, accessor_label, req_rights);
343 return m_logic->allowAccess(
349 static_cast<AccessRight>(req_rights));
351 case LogicCommand::DENY_ACCESS:
354 std::string accessor_label;
355 buffer.Deserialize(item_alias, accessor_label);
356 return m_logic->denyAccess(
364 Throw(Exception::BrokenProtocol);
369 void CKMService::close(const CloseEvent &event) {
370 LogDebug("Close event");
371 m_connectionInfoMap.erase(event.connectionID.counter);