Add 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 Credentials& /*cred*/,
58                                    const Alias& /*alias*/,
59                                    const Label& /*label*/)
60 {
61     // This will be replaced in next commit
62     throw std::runtime_error("Not supported");
63 }
64
65 GenericSocketService::ServiceDescriptionVector EncryptionService::GetServiceDescription()
66 {
67     return ServiceDescriptionVector {
68         {SERVICE_SOCKET_ENCRYPTION, "key-manager::api-encryption", SOCKET_ID_ENCRYPTION}
69     };
70 }
71
72 void EncryptionService::Start() {
73     Create();
74 }
75
76 void EncryptionService::Stop() {
77     Join();
78 }
79
80 bool EncryptionService::ProcessOne(
81     const ConnectionID &conn,
82     ConnectionInfo &info)
83 {
84     LogDebug ("process One");
85     try {
86         if (!info.buffer.Ready())
87             return false;
88
89         ProcessEncryption(conn, info.credentials, info.buffer);
90         return true;
91     } catch (MessageBuffer::Exception::Base) {
92         LogError("Broken protocol. Closing socket.");
93     } catch (const std::exception &e) {
94         LogError("Std exception:: " << e.what());
95     } catch (...) {
96         LogError("Unknown exception. Closing socket.");
97     }
98
99     m_serviceManager->Close(conn);
100     return false;
101 }
102
103 void EncryptionService::ProcessEncryption(const ConnectionID &conn,
104                                           const Credentials &cred,
105                                           MessageBuffer &buffer)
106 {
107     int tmpCmd = 0;
108     CryptoRequest req;
109
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);
114
115     req.conn = conn;
116     req.cred = cred;
117     m_logic.Crypt(req);
118 }
119
120 } /* namespace CKM */