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 Credentials& /*cred*/,
58 const Alias& /*alias*/,
59 const Label& /*label*/)
61 // This will be replaced in next commit
62 throw std::runtime_error("Not supported");
65 GenericSocketService::ServiceDescriptionVector EncryptionService::GetServiceDescription()
67 return ServiceDescriptionVector {
68 {SERVICE_SOCKET_ENCRYPTION, "key-manager::api-encryption", SOCKET_ID_ENCRYPTION}
72 void EncryptionService::Start() {
76 void EncryptionService::Stop() {
80 bool EncryptionService::ProcessOne(
81 const ConnectionID &conn,
84 LogDebug ("process One");
86 if (!info.buffer.Ready())
89 ProcessEncryption(conn, info.credentials, info.buffer);
91 } catch (MessageBuffer::Exception::Base) {
92 LogError("Broken protocol. Closing socket.");
93 } catch (const std::exception &e) {
94 LogError("Std exception:: " << e.what());
96 LogError("Unknown exception. Closing socket.");
99 m_serviceManager->Close(conn);
103 void EncryptionService::ProcessEncryption(const ConnectionID &conn,
104 const Credentials &cred,
105 MessageBuffer &buffer)
110 buffer.Deserialize(tmpCmd, req.msgId, req.cas, req.name, req.label, req.password, req.input);
111 req.command = static_cast<EncryptionCommand>(tmpCmd);
112 if (req.command != EncryptionCommand::ENCRYPT && req.command != EncryptionCommand::DECRYPT)
113 throw std::runtime_error("Unsupported command: " + tmpCmd);
120 } /* namespace CKM */