Simplify implementation of ServiceThread
[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
23 #include <protocols.h>
24
25 #include <dpl/serialization.h>
26 #include <dpl/log/log.h>
27
28 #include <ocsp-service.h>
29 #include <ocsp-logic.h>
30
31 namespace {
32 const CKM::InterfaceID SOCKET_ID_OCSP = 0;
33 } // namespace anonymous
34
35 namespace CKM {
36
37 OCSPService::OCSPService()
38   : m_logic(new OCSPLogic())
39 {}
40
41 OCSPService::~OCSPService() {
42     delete m_logic;
43 }
44
45 GenericSocketService::ServiceDescriptionVector OCSPService::GetServiceDescription()
46 {
47     return ServiceDescriptionVector {
48         {SERVICE_SOCKET_OCSP, "key-manager::api-ocsp", SOCKET_ID_OCSP}
49     };
50 }
51
52 bool OCSPService::ProcessOne(
53     const ConnectionID &conn,
54     ConnectionInfo &info)
55 {
56     LogDebug ("process One");
57
58     Try {
59         if (!info.buffer.Ready())
60             return false;
61
62         auto &buffer = info.buffer;
63
64         int commandId = 0;
65         RawBufferVector chainVector;
66         buffer.Deserialize(commandId, chainVector);
67
68         RawBuffer response = m_logic->ocspCheck(commandId, chainVector);
69         m_serviceManager->Write(conn, response);
70
71         return true;
72     } Catch (MessageBuffer::Exception::Base) {
73         LogError("Broken protocol. Closing socket.");
74     } catch (const std::string &e) {
75         LogError("String exception(" << e << "). Closing socket");
76     } catch (...) {
77         LogError("Unknown exception. Closing socket.");
78     }
79
80     m_serviceManager->Close(conn);
81     return false;
82 }
83
84 } // namespace CKM
85