2 * Copyright (c) 2000 - 2015 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 encryption-service.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
24 #include <encryption-service.h>
25 #include <protocols.h>
26 #include <dpl/log/log.h>
27 #include <dpl/serialization.h>
28 #include <crypto-request.h>
31 const CKM::InterfaceID SOCKET_ID_ENCRYPTION = 0;
32 } // namespace anonymous
36 EncryptionService::EncryptionService() : m_logic(*this)
40 EncryptionService::~EncryptionService()
44 void EncryptionService::RespondToClient(const CryptoRequest& request,
46 const RawBuffer& data)
49 RawBuffer response = MessageBuffer::Serialize(
50 static_cast<int>(request.command), request.msgId, retCode, data).Pop();
51 m_serviceManager->Write(request.conn, response);
53 LogError("Failed to send response to the client");
57 void EncryptionService::RequestKey(const CryptoRequest& request)
59 MsgKeyRequest kReq(request.msgId, request.cred, request.name, request.label, request.password);
60 if (!m_commMgr->SendMessage(kReq))
61 throw std::runtime_error("No listener found"); // TODO
64 GenericSocketService::ServiceDescriptionVector EncryptionService::GetServiceDescription()
66 return ServiceDescriptionVector {
67 {SERVICE_SOCKET_ENCRYPTION, "key-manager::api-encryption", SOCKET_ID_ENCRYPTION}
71 void EncryptionService::Start() {
75 void EncryptionService::Stop() {
79 void EncryptionService::SetCommManager(CommMgr *manager)
81 ThreadService::SetCommManager(manager);
85 bool EncryptionService::ProcessOne(
86 const ConnectionID &conn,
89 LogDebug ("process One");
91 if (!info.buffer.Ready())
94 ProcessEncryption(conn, info.credentials, info.buffer);
96 } catch (MessageBuffer::Exception::Base) {
97 LogError("Broken protocol. Closing socket.");
98 } catch (const std::exception &e) {
99 LogError("Std exception:: " << e.what());
101 LogError("Unknown exception. Closing socket.");
104 m_serviceManager->Close(conn);
108 void EncryptionService::ProcessMessage(MsgKeyResponse msg)
110 m_logic.KeyRetrieved(std::move(msg));
113 void EncryptionService::ProcessEncryption(const ConnectionID &conn,
114 const Credentials &cred,
115 MessageBuffer &buffer)
120 buffer.Deserialize(tmpCmd, req.msgId, req.cas, req.name, req.label, req.password, req.input);
121 req.command = static_cast<EncryptionCommand>(tmpCmd);
122 if (req.command != EncryptionCommand::ENCRYPT && req.command != EncryptionCommand::DECRYPT)
123 throw std::runtime_error("Unsupported command: " + tmpCmd);
130 } /* namespace CKM */