tizen 2.3 release
[framework/web/wearable/wrt-security.git] / wrt_ocsp / src / wrt_ocsp_api.cpp
1 /*
2  * Copyright (c) 2012 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        wrt_ocsp_api.cpp
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       This file contains implementation of WRT OCSP api
21  */
22
23 #include <dpl/log/log.h>
24 #include <dpl/dbus/dbus_client.h>
25 #include "ocsp_server_api.h"
26 #include "SecurityCommunicationClient.h"
27
28 #include "wrt_ocsp_api.h"
29
30 static WrtSecurity::Communication::Client *communicationClient = NULL;
31
32 wrt_ocsp_return_t wrt_ocsp_initialize(void){
33 #ifdef __SUPPORT_OCSP__
34     if (NULL != communicationClient) {
35         LogError("wrt_ocsp_api already initialized");
36         return WRT_OCSP_INTERNAL_ERROR;
37     }
38
39     Try {
40         communicationClient = new WrtSecurity::Communication::Client(WrtSecurity::OcspServerApi::INTERFACE_NAME());
41     } Catch (WrtSecurity::Communication::Client::Exception::SecurityCommunicationClientException) {
42         LogError("Can't connect to daemon");
43         return WRT_OCSP_INTERNAL_ERROR;
44     }
45 #endif
46     LogInfo("Initialized");
47     return WRT_OCSP_OK;
48 }
49
50 wrt_ocsp_return_t wrt_ocsp_shutdown(void){
51 #ifdef __SUPPORT_OCSP__    
52     if (NULL == communicationClient) {
53         LogError("wrt_ocsp_api not initialized");
54         return WRT_OCSP_INTERNAL_ERROR;
55     }
56     delete communicationClient;
57     communicationClient = NULL;
58 #endif
59     LogInfo("Shutdown");
60     return WRT_OCSP_OK;
61 }
62
63 wrt_ocsp_return_t wrt_ocsp_verify_widget(wrt_ocsp_widget_handle_t handle,
64                                          wrt_ocsp_widget_verification_status_t* status){
65
66     LogInfo("Verifying");
67     if (NULL == status) {
68         LogError("Invalid arguments");
69         return WRT_OCSP_INVALID_ARGUMENTS;
70     }
71     int intResponse;
72
73 #ifdef __SUPPORT_OCSP__ 
74     Try {
75         communicationClient->call(WrtSecurity::OcspServerApi::CHECK_ACCESS_METHOD(),
76                                   handle,
77                                   &intResponse);
78     } Catch (WrtSecurity::Communication::Client::Exception::SecurityCommunicationClientException) {
79         LogError("Problem with connection to daemon");
80         return WRT_OCSP_INTERNAL_ERROR;
81     }
82 #endif
83     intResponse = 0;
84     (*status) = static_cast<wrt_ocsp_widget_verification_status_t>(intResponse);
85     LogInfo("Widget verified with response " << intResponse);
86     return WRT_OCSP_OK;
87 }