2 * Copyright (c) 2015 - 2020 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() :
41 EncryptionService::~EncryptionService()
45 void EncryptionService::RespondToClient(const CryptoRequest &request,
47 const RawBuffer &data)
50 RawBuffer response = SerializeMessage(request.msgId, retCode, data);
51 m_serviceManager->Write(request.conn, response);
53 LogError("Failed to send response to the client");
57 void EncryptionService::RequestKey(unsigned id, const CryptoRequest &request)
59 MsgKeyRequest kReq(id, request.cred, request.name,
60 request.explicitOwner, request.password);
62 if (!m_commMgr->SendMessage(kReq))
63 throw std::runtime_error("No listener found");// TODO
66 GenericSocketService::ServiceDescriptionVector
67 EncryptionService::GetServiceDescription()
69 return ServiceDescriptionVector {
70 {SERVICE_SOCKET_ENCRYPTION, "http://tizen.org/privilege/keymanager", SOCKET_ID_ENCRYPTION}
74 void EncryptionService::Start()
79 void EncryptionService::Stop()
84 void EncryptionService::SetCommManager(CommMgr *manager)
86 ThreadService::SetCommManager(manager);
90 // Encryption Service does not support any kind of security-check
91 // and 3rd parameter is not required
92 bool EncryptionService::ProcessOne(
93 const ConnectionID &conn,
97 LogDebug("process One");
100 if (!info.buffer.Ready())
103 ProcessEncryption(conn, info.credentials, info.buffer);
105 } catch (MessageBuffer::Exception::Base &) {
106 LogError("Broken protocol. Closing socket.");
107 } catch (const std::exception &e) {
108 LogError("Std exception:: " << e.what());
110 LogError("Unknown exception. Closing socket.");
113 m_serviceManager->Close(conn);
117 void EncryptionService::ProcessMessage(MsgKeyResponse msg)
119 m_logic.KeyRetrieved(std::move(msg));
122 void EncryptionService::ProcessEncryption(const ConnectionID &conn,
123 const Credentials &cred,
124 MessageBuffer &buffer)
129 buffer.Deserialize(tmpCmd, req.msgId, req.cas, req.name, req.explicitOwner,
130 req.password, req.input);
131 req.command = static_cast<EncryptionCommand>(tmpCmd);
133 if (req.command != EncryptionCommand::ENCRYPT &&
134 req.command != EncryptionCommand::DECRYPT)
135 throw std::runtime_error("Unsupported command: " + tmpCmd);
142 void EncryptionService::CustomHandle(const ReadEvent &event)
144 auto &info = m_connectionInfoMap[event.connectionID.counter];
145 info.buffer.Push(RawBuffer(event.rawBuffer));
147 while (ProcessOne(event.connectionID, info, true));
150 void EncryptionService::CustomHandle(const SecurityEvent &/*event*/)
152 LogError("This should not happend! SecurityEvent was called on EncryptionService!");
155 void EncryptionService::CustomHandle(const CloseEvent &event)
157 // call the default handler
158 ThreadService::Handle(event);
160 m_logic.DropRequests(event.connectionID);
163 } /* namespace CKM */