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.
23 #include <protocols.h>
25 #include <dpl/serialization.h>
26 #include <dpl/log/log.h>
28 #include <ckm-service.h>
29 #include <ckm-logic.h>
32 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
33 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
34 } // namespace anonymous
38 CKMService::CKMService()
39 : m_logic(new CKMLogic)
42 CKMService::~CKMService() {
46 void CKMService::Start() {
50 void CKMService::Stop() {
54 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
56 return ServiceDescriptionVector {
57 {SERVICE_SOCKET_CKM_CONTROL, "key-manager::api-control", SOCKET_ID_CONTROL},
58 {SERVICE_SOCKET_CKM_STORAGE, "key-manager::api-storage", SOCKET_ID_STORAGE}
62 void CKMService::SetCommManager(CommMgr *manager)
64 ThreadService::SetCommManager(manager);
68 bool CKMService::ProcessOne(
69 const ConnectionID &conn,
72 LogDebug ("process One");
76 if (!info.buffer.Ready())
79 if (info.interfaceID == SOCKET_ID_CONTROL)
80 response = ProcessControl(info.buffer);
82 response = ProcessStorage(info.credentials, info.buffer);
84 m_serviceManager->Write(conn, response);
87 } Catch (MessageBuffer::Exception::Base) {
88 LogError("Broken protocol. Closing socket.");
89 } Catch (Exception::BrokenProtocol) {
90 LogError("Broken protocol. Closing socket.");
91 } catch (const DataType::Exception::Base &e) {
92 LogError("Closing socket. DBDataType::Exception: " << e.DumpToString());
93 } catch (const std::string &e) {
94 LogError("String exception(" << e << "). Closing socket");
95 } catch (const std::exception &e) {
96 LogError("Std exception:: " << e.what());
98 LogError("Unknown exception. Closing socket.");
101 m_serviceManager->Close(conn);
105 RawBuffer CKMService::ProcessControl(MessageBuffer &buffer) {
109 Password newPass, oldPass;
112 buffer.Deserialize(command);
114 LogDebug("Process control. Command: " << command);
116 cc = static_cast<ControlCommand>(command);
119 case ControlCommand::UNLOCK_USER_KEY:
120 buffer.Deserialize(user, newPass);
121 return m_logic->unlockUserKey(user, newPass);
122 case ControlCommand::LOCK_USER_KEY:
123 buffer.Deserialize(user);
124 return m_logic->lockUserKey(user);
125 case ControlCommand::REMOVE_USER_DATA:
126 buffer.Deserialize(user);
127 return m_logic->removeUserData(user);
128 case ControlCommand::CHANGE_USER_PASSWORD:
129 buffer.Deserialize(user, oldPass, newPass);
130 return m_logic->changeUserPassword(user, oldPass, newPass);
131 case ControlCommand::RESET_USER_PASSWORD:
132 buffer.Deserialize(user, newPass);
133 return m_logic->resetUserPassword(user, newPass);
134 case ControlCommand::REMOVE_APP_DATA:
135 buffer.Deserialize(smackLabel);
136 return m_logic->removeApplicationData(smackLabel);
137 case ControlCommand::UPDATE_CC_MODE:
138 return m_logic->updateCCMode();
139 case ControlCommand::SET_PERMISSION:
144 PermissionMask permissionMask = 0;
146 buffer.Deserialize(user, name, label, accessorLabel, permissionMask);
148 Credentials cred(user, label);
149 return m_logic->setPermission(
159 Throw(Exception::BrokenProtocol);
163 RawBuffer CKMService::ProcessStorage(Credentials &cred, MessageBuffer &buffer)
169 Label label, accessorLabel;
172 buffer.Deserialize(command);
173 buffer.Deserialize(msgID);
175 // This is a workaround solution for locktype=None in Tizen 2.2.1
176 // When locktype is None, lockscreen app doesn't interfere with unlocking process.
177 // Therefor lockscreen app cannot notify unlock events to key-manager when locktype is None.
178 // So, to unlock user data when lock type is None, key-manager always try to unlock user data with null password.
179 // Even if the result is fail, it will be ignored.
180 Password nullPassword("");
181 m_logic->unlockUserKey(cred.clientUid, nullPassword);
183 LogDebug("Process storage. Command: " << command);
185 switch(static_cast<LogicCommand>(command)) {
186 case LogicCommand::SAVE:
189 PolicySerializable policy;
190 buffer.Deserialize(tmpDataType, name, label, rawData, policy);
191 return m_logic->saveData(
197 DataType(tmpDataType),
200 case LogicCommand::SAVE_PKCS12:
203 PKCS12Serializable pkcs;
204 PolicySerializable keyPolicy, certPolicy;
205 buffer.Deserialize(name, label, pkcs, keyPolicy, certPolicy);
206 return m_logic->savePKCS12(
215 case LogicCommand::REMOVE:
217 buffer.Deserialize(name, label);
218 return m_logic->removeData(
224 case LogicCommand::GET:
227 buffer.Deserialize(tmpDataType, name, label, password);
228 return m_logic->getData(
231 DataType(tmpDataType),
236 case LogicCommand::GET_PKCS12:
240 buffer.Deserialize(name,
244 return m_logic->getPKCS12(
252 case LogicCommand::GET_LIST:
254 buffer.Deserialize(tmpDataType);
255 return m_logic->getDataList(
258 DataType(tmpDataType));
260 case LogicCommand::CREATE_KEY_AES:
265 PolicySerializable policyKey;
266 buffer.Deserialize(size,
270 return m_logic->createKeyAES(
278 case LogicCommand::CREATE_KEY_PAIR:
280 CryptoAlgorithmSerializable keyGenAlgorithm;
282 Label privateKeyLabel;
284 Label publicKeyLabel;
285 PolicySerializable policyPrivateKey;
286 PolicySerializable policyPublicKey;
287 buffer.Deserialize(keyGenAlgorithm,
294 return m_logic->createKeyPair(
305 case LogicCommand::GET_CHAIN_CERT:
307 RawBuffer certificate;
308 RawBufferVector untrustedVector;
309 RawBufferVector trustedVector;
311 buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
312 return m_logic->getCertificateChain(
320 case LogicCommand::GET_CHAIN_ALIAS:
322 RawBuffer certificate;
323 LabelNameVector untrustedVector;
324 LabelNameVector trustedVector;
326 buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
327 return m_logic->getCertificateChain(
335 case LogicCommand::CREATE_SIGNATURE:
337 Password password; // password for private_key
339 int padding = 0, hash = 0;
340 buffer.Deserialize(name, label, password, message, hash, padding);
341 return m_logic->createSignature(
346 password, // password for private_key
348 static_cast<HashAlgorithm>(hash),
349 static_cast<RSAPaddingAlgorithm>(padding));
351 case LogicCommand::VERIFY_SIGNATURE:
353 Password password; // password for public_key (optional)
356 //HashAlgorithm hash;
357 //RSAPaddingAlgorithm padding;
358 int padding = 0, hash = 0;
359 buffer.Deserialize(name,
366 return m_logic->verifySignature(
371 password, // password for public_key (optional)
374 static_cast<const HashAlgorithm>(hash),
375 static_cast<const RSAPaddingAlgorithm>(padding));
377 case LogicCommand::SET_PERMISSION:
379 PermissionMask permissionMask = 0;
380 buffer.Deserialize(name, label, accessorLabel, permissionMask);
381 return m_logic->setPermission(
391 Throw(Exception::BrokenProtocol);
395 void CKMService::ProcessMessage(MsgKeyRequest msg)
397 Crypto::GKeyShPtr key;
398 int ret = m_logic->getKeyForService(msg.cred,
403 MsgKeyResponse kResp(msg.id, key, ret);
405 if (!m_commMgr->SendMessage(kResp))
406 LogError("No listener found"); // can't do much more
408 LogError("Uncaught exception in SendMessage. Check listeners.");