Use SafeBuffer in C++ api. Rename SafeBuffer to RawBuffer.
[platform/core/security/key-manager.git] / src / manager / service / ocsp-service.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co.
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        ocsp-service.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       OCSP service implementation.
21  */
22 #include <service-thread.h>
23 #include <generic-socket-manager.h>
24 #include <connection-info.h>
25 #include <message-buffer.h>
26 #include <protocols.h>
27
28 #include <dpl/serialization.h>
29 #include <dpl/log/log.h>
30
31 #include <ocsp-service.h>
32 #include <ocsp-logic.h>
33
34 namespace {
35 const CKM::InterfaceID SOCKET_ID_OCSP = 0;
36 } // namespace anonymous
37
38 namespace CKM {
39
40 OCSPService::OCSPService()
41   : m_logic(new OCSPLogic())
42 {}
43
44 OCSPService::~OCSPService() {
45     delete m_logic;
46 }
47
48 GenericSocketService::ServiceDescriptionVector OCSPService::GetServiceDescription()
49 {
50     return ServiceDescriptionVector {
51         {SERVICE_SOCKET_OCSP, "key-manager::api-ocsp", SOCKET_ID_OCSP}
52     };
53 }
54
55 void OCSPService::accept(const AcceptEvent &event) {
56     LogDebug("Accept event");
57     auto &info = m_connectionInfoMap[event.connectionID.counter];
58     info.interfaceID = event.interfaceID;
59     info.credentials = event.credentials;
60 }
61
62 void OCSPService::write(const WriteEvent &event) {
63     LogDebug("Write event (" << event.size << " bytes )");
64 }
65
66 void OCSPService::process(const ReadEvent &event) {
67     LogDebug("Read event");
68     auto &info = m_connectionInfoMap[event.connectionID.counter];
69     info.buffer.Push(event.rawBuffer);
70     while(processOne(event.connectionID, info));
71 }
72
73 bool OCSPService::processOne(
74     const ConnectionID &conn,
75     ConnectionInfo &info)
76 {
77     LogDebug ("process One");
78
79     Try {
80         if (!info.buffer.Ready())
81             return false;
82
83         auto &buffer = info.buffer;
84
85         int commandId;
86         RawBufferVector chainVector;
87         Deserialization::Deserialize(buffer, commandId);
88         Deserialization::Deserialize(buffer, chainVector);
89
90         RawBuffer response = m_logic->ocspCheck(commandId, chainVector);
91         m_serviceManager->Write(conn, response);
92
93         return true;
94     } Catch (MessageBuffer::Exception::Base) {
95         LogError("Broken protocol. Closing socket.");
96     } catch (const std::string &e) {
97         LogError("String exception(" << e << "). Closing socket");
98     } catch (...) {
99         LogError("Unknown exception. Closing socket.");
100     }
101
102     m_serviceManager->Close(conn);
103     return false;
104 }
105
106 void OCSPService::close(const CloseEvent &event) {
107     LogDebug("Close event");
108     m_connectionInfoMap.erase(event.connectionID.counter);
109 }
110
111 } // namespace CKM
112