Implement key retrieval in encryption service
[platform/core/security/key-manager.git] / src / manager / service / encryption-service.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       encryption-service.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <stdexcept>
23 #include <utility>
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>
29
30 namespace {
31 const CKM::InterfaceID SOCKET_ID_ENCRYPTION = 0;
32 } // namespace anonymous
33
34 namespace CKM {
35
36 EncryptionService::EncryptionService() : m_logic(*this)
37 {
38 }
39
40 EncryptionService::~EncryptionService()
41 {
42 }
43
44 void EncryptionService::RespondToClient(const CryptoRequest& request,
45                                         int retCode,
46                                         const RawBuffer& data)
47 {
48     try {
49         RawBuffer response = MessageBuffer::Serialize(
50                 static_cast<int>(request.command), request.msgId, retCode, data).Pop();
51         m_serviceManager->Write(request.conn, response);
52     } catch (...) {
53         LogError("Failed to send response to the client");
54     }
55 }
56
57 void EncryptionService::RequestKey(const CryptoRequest& request)
58 {
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
62 }
63
64 GenericSocketService::ServiceDescriptionVector EncryptionService::GetServiceDescription()
65 {
66     return ServiceDescriptionVector {
67         {SERVICE_SOCKET_ENCRYPTION, "key-manager::api-encryption", SOCKET_ID_ENCRYPTION}
68     };
69 }
70
71 void EncryptionService::Start() {
72     Create();
73 }
74
75 void EncryptionService::Stop() {
76     Join();
77 }
78
79 void EncryptionService::SetCommManager(CommMgr *manager)
80 {
81     ThreadService::SetCommManager(manager);
82     Register(*manager);
83 }
84
85 bool EncryptionService::ProcessOne(
86     const ConnectionID &conn,
87     ConnectionInfo &info)
88 {
89     LogDebug ("process One");
90     try {
91         if (!info.buffer.Ready())
92             return false;
93
94         ProcessEncryption(conn, info.credentials, info.buffer);
95         return true;
96     } catch (MessageBuffer::Exception::Base) {
97         LogError("Broken protocol. Closing socket.");
98     } catch (const std::exception &e) {
99         LogError("Std exception:: " << e.what());
100     } catch (...) {
101         LogError("Unknown exception. Closing socket.");
102     }
103
104     m_serviceManager->Close(conn);
105     return false;
106 }
107
108 void EncryptionService::ProcessMessage(MsgKeyResponse msg)
109 {
110     m_logic.KeyRetrieved(std::move(msg));
111 }
112
113 void EncryptionService::ProcessEncryption(const ConnectionID &conn,
114                                           const Credentials &cred,
115                                           MessageBuffer &buffer)
116 {
117     int tmpCmd = 0;
118     CryptoRequest req;
119
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);
124
125     req.conn = conn;
126     req.cred = cred;
127     m_logic.Crypt(req);
128 }
129
130 } /* namespace CKM */