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 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
48 return ServiceDescriptionVector {
49 {SERVICE_SOCKET_CKM_CONTROL, "key-manager::api-control", SOCKET_ID_CONTROL},
50 {SERVICE_SOCKET_CKM_STORAGE, "key-manager::api-storage", SOCKET_ID_STORAGE}
54 bool CKMService::ProcessOne(
55 const ConnectionID &conn,
58 LogDebug ("process One");
62 if (!info.buffer.Ready())
65 if (info.interfaceID == SOCKET_ID_CONTROL)
66 response = ProcessControl(info.buffer);
68 response = ProcessStorage(info.credentials, info.buffer);
70 m_serviceManager->Write(conn, response);
73 } Catch (MessageBuffer::Exception::Base) {
74 LogError("Broken protocol. Closing socket.");
75 } Catch (Exception::BrokenProtocol) {
76 LogError("Broken protocol. Closing socket.");
77 } catch (const DataType::Exception::Base &e) {
78 LogError("Closing socket. DBDataType::Exception: " << e.DumpToString());
79 } catch (const std::string &e) {
80 LogError("String exception(" << e << "). Closing socket");
81 } catch (const std::exception &e) {
82 LogError("Std exception:: " << e.what());
84 LogError("Unknown exception. Closing socket.");
87 m_serviceManager->Close(conn);
91 RawBuffer CKMService::ProcessControl(MessageBuffer &buffer) {
95 Password newPass, oldPass;
98 buffer.Deserialize(command);
100 LogDebug("Process control. Command: " << command);
102 cc = static_cast<ControlCommand>(command);
105 case ControlCommand::UNLOCK_USER_KEY:
106 buffer.Deserialize(user, newPass);
107 return m_logic->unlockUserKey(user, newPass);
108 case ControlCommand::LOCK_USER_KEY:
109 buffer.Deserialize(user);
110 return m_logic->lockUserKey(user);
111 case ControlCommand::REMOVE_USER_DATA:
112 buffer.Deserialize(user);
113 return m_logic->removeUserData(user);
114 case ControlCommand::CHANGE_USER_PASSWORD:
115 buffer.Deserialize(user, oldPass, newPass);
116 return m_logic->changeUserPassword(user, oldPass, newPass);
117 case ControlCommand::RESET_USER_PASSWORD:
118 buffer.Deserialize(user, newPass);
119 return m_logic->resetUserPassword(user, newPass);
120 case ControlCommand::REMOVE_APP_DATA:
121 buffer.Deserialize(smackLabel);
122 return m_logic->removeApplicationData(smackLabel);
123 case ControlCommand::UPDATE_CC_MODE:
124 return m_logic->updateCCMode();
125 case ControlCommand::SET_PERMISSION:
130 PermissionMask permissionMask = 0;
132 buffer.Deserialize(user, name, label, accessorLabel, permissionMask);
134 Credentials cred(user, label);
135 return m_logic->setPermission(
145 Throw(Exception::BrokenProtocol);
149 RawBuffer CKMService::ProcessStorage(Credentials &cred, MessageBuffer &buffer)
155 Label label, accessorLabel;
158 buffer.Deserialize(command);
159 buffer.Deserialize(msgID);
161 LogDebug("Process storage. Command: " << command);
163 switch(static_cast<LogicCommand>(command)) {
164 case LogicCommand::SAVE:
167 PolicySerializable policy;
168 buffer.Deserialize(tmpDataType, name, label, rawData, policy);
169 return m_logic->saveData(
175 DataType(tmpDataType),
178 case LogicCommand::SAVE_PKCS12:
181 PKCS12Serializable pkcs;
182 PolicySerializable keyPolicy, certPolicy;
183 buffer.Deserialize(name, label, pkcs, keyPolicy, certPolicy);
184 return m_logic->savePKCS12(
193 case LogicCommand::REMOVE:
195 buffer.Deserialize(name, label);
196 return m_logic->removeData(
202 case LogicCommand::GET:
205 buffer.Deserialize(tmpDataType, name, label, password);
206 return m_logic->getData(
209 DataType(tmpDataType),
214 case LogicCommand::GET_PKCS12:
218 buffer.Deserialize(name,
222 return m_logic->getPKCS12(
230 case LogicCommand::GET_LIST:
232 buffer.Deserialize(tmpDataType);
233 return m_logic->getDataList(
236 DataType(tmpDataType));
238 case LogicCommand::CREATE_KEY_PAIR:
240 CryptoAlgorithmSerializable keyGenAlgorithm;
242 Label privateKeyLabel;
244 Label publicKeyLabel;
245 PolicySerializable policyPrivateKey;
246 PolicySerializable policyPublicKey;
247 buffer.Deserialize(keyGenAlgorithm,
254 return m_logic->createKeyPair(
265 case LogicCommand::GET_CHAIN_CERT:
267 RawBuffer certificate;
268 RawBufferVector untrustedVector;
269 RawBufferVector trustedVector;
271 buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
272 return m_logic->getCertificateChain(
280 case LogicCommand::GET_CHAIN_ALIAS:
282 RawBuffer certificate;
283 LabelNameVector untrustedVector;
284 LabelNameVector trustedVector;
286 buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
287 return m_logic->getCertificateChain(
295 case LogicCommand::CREATE_SIGNATURE:
297 Password password; // password for private_key
299 int padding = 0, hash = 0;
300 buffer.Deserialize(name, label, password, message, hash, padding);
301 return m_logic->createSignature(
306 password, // password for private_key
308 static_cast<HashAlgorithm>(hash),
309 static_cast<RSAPaddingAlgorithm>(padding));
311 case LogicCommand::VERIFY_SIGNATURE:
313 Password password; // password for public_key (optional)
316 //HashAlgorithm hash;
317 //RSAPaddingAlgorithm padding;
318 int padding = 0, hash = 0;
319 buffer.Deserialize(name,
326 return m_logic->verifySignature(
331 password, // password for public_key (optional)
334 static_cast<const HashAlgorithm>(hash),
335 static_cast<const RSAPaddingAlgorithm>(padding));
337 case LogicCommand::SET_PERMISSION:
339 PermissionMask permissionMask = 0;
340 buffer.Deserialize(name, label, accessorLabel, permissionMask);
341 return m_logic->setPermission(
351 Throw(Exception::BrokenProtocol);