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 LogDebug("Process storage. Command: " << command);
177 switch(static_cast<LogicCommand>(command)) {
178 case LogicCommand::SAVE:
181 PolicySerializable policy;
182 buffer.Deserialize(tmpDataType, name, label, rawData, policy);
183 return m_logic->saveData(
189 DataType(tmpDataType),
192 case LogicCommand::SAVE_PKCS12:
195 PKCS12Serializable pkcs;
196 PolicySerializable keyPolicy, certPolicy;
197 buffer.Deserialize(name, label, pkcs, keyPolicy, certPolicy);
198 return m_logic->savePKCS12(
207 case LogicCommand::REMOVE:
209 buffer.Deserialize(name, label);
210 return m_logic->removeData(
216 case LogicCommand::GET:
219 buffer.Deserialize(tmpDataType, name, label, password);
220 return m_logic->getData(
223 DataType(tmpDataType),
228 case LogicCommand::GET_PKCS12:
232 buffer.Deserialize(name,
236 return m_logic->getPKCS12(
244 case LogicCommand::GET_LIST:
246 buffer.Deserialize(tmpDataType);
247 return m_logic->getDataList(
250 DataType(tmpDataType));
252 case LogicCommand::CREATE_KEY_AES:
257 PolicySerializable policyKey;
258 buffer.Deserialize(size,
262 return m_logic->createKeyAES(
270 case LogicCommand::CREATE_KEY_PAIR:
272 CryptoAlgorithmSerializable keyGenAlgorithm;
274 Label privateKeyLabel;
276 Label publicKeyLabel;
277 PolicySerializable policyPrivateKey;
278 PolicySerializable policyPublicKey;
279 buffer.Deserialize(keyGenAlgorithm,
286 return m_logic->createKeyPair(
297 case LogicCommand::GET_CHAIN_CERT:
299 RawBuffer certificate;
300 RawBufferVector untrustedVector;
301 RawBufferVector trustedVector;
303 buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
304 return m_logic->getCertificateChain(
312 case LogicCommand::GET_CHAIN_ALIAS:
314 RawBuffer certificate;
315 LabelNameVector untrustedVector;
316 LabelNameVector trustedVector;
318 buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
319 return m_logic->getCertificateChain(
327 case LogicCommand::CREATE_SIGNATURE:
329 Password password; // password for private_key
331 int padding = 0, hash = 0;
332 buffer.Deserialize(name, label, password, message, hash, padding);
333 return m_logic->createSignature(
338 password, // password for private_key
340 static_cast<HashAlgorithm>(hash),
341 static_cast<RSAPaddingAlgorithm>(padding));
343 case LogicCommand::VERIFY_SIGNATURE:
345 Password password; // password for public_key (optional)
348 //HashAlgorithm hash;
349 //RSAPaddingAlgorithm padding;
350 int padding = 0, hash = 0;
351 buffer.Deserialize(name,
358 return m_logic->verifySignature(
363 password, // password for public_key (optional)
366 static_cast<const HashAlgorithm>(hash),
367 static_cast<const RSAPaddingAlgorithm>(padding));
369 case LogicCommand::SET_PERMISSION:
371 PermissionMask permissionMask = 0;
372 buffer.Deserialize(name, label, accessorLabel, permissionMask);
373 return m_logic->setPermission(
383 Throw(Exception::BrokenProtocol);
387 void CKMService::ProcessMessage(MsgKeyRequest msg)
389 Crypto::GKeyShPtr key;
390 int ret = m_logic->getKeyForService(msg.cred,
395 MsgKeyResponse kResp(msg.id, key, ret);
397 if (!m_commMgr->SendMessage(kResp))
398 LogError("No listener found"); // can't do much more
400 LogError("Uncaught exception in SendMessage. Check listeners.");