tizen 2.4 release
[framework/security/key-manager.git] / src / manager / service / ocsp-logic.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-logic.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       OCSP logic implementation.
21  */
22 #include <ckm/ckm-error.h>
23
24 #include <dpl/log/log.h>
25 #include <dpl/serialization.h>
26
27 #include <message-buffer.h>
28
29 #include <ocsp-logic.h>
30 #include <ocsp.h>
31
32 #include <system_info.h>
33
34 #define FEATURE_WIFI         "tizen.org/feature/network.internet"
35 #define FEATURE_TELEPHONY    "tizen.org/feature/network.telephony"
36 #define FEATURE_TETHERING_BT "tizen.org/feature/network.tethering.bluetooth"
37 #define FEATURE_ETHERNET     "tizen.org/feature/network.ethernet"
38
39 namespace CKM {
40
41 namespace {
42
43 bool isFeatureOn(const char *feature)
44 {
45     bool value = false;
46
47     if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(feature, &value)) {
48         // system info capi error.
49         return false;
50     }
51
52     return value;
53 }
54
55 } // namespace anonymous
56
57
58 OCSPLogic::OCSPLogic()
59   : m_isNetAvailable(false)
60 {
61     setNetAvailable();
62 }
63
64 void OCSPLogic::setNetAvailable()
65 {
66     if (isFeatureOn(FEATURE_WIFI)
67         || isFeatureOn(FEATURE_TELEPHONY)
68         || isFeatureOn(FEATURE_TETHERING_BT)
69         || isFeatureOn(FEATURE_ETHERNET)) {
70         m_isNetAvailable = true;
71     }
72     else {
73         m_isNetAvailable = false;
74     }
75 }
76
77 RawBuffer OCSPLogic::ocspCheck(int commandId, const RawBufferVector &rawChain) {
78     CertificateImplVector certChain;
79     OCSPModule ocsp;
80     int retCode = CKM_API_SUCCESS;
81     int ocspStatus = CKM_API_OCSP_STATUS_INTERNAL_ERROR;
82
83
84     if (!m_isNetAvailable) {
85         // try again for in case of system-info error
86         setNetAvailable();
87     }
88
89     if (!m_isNetAvailable) {
90         retCode = CKM_API_ERROR_NOT_SUPPORTED;
91     }
92     else {
93        for (auto &e: rawChain) {
94            certChain.push_back(CertificateImpl(e, DataFormat::FORM_DER));
95            if (certChain.rbegin()->empty()) {
96                LogDebug("Error in parsing certificates!");
97                retCode = CKM_API_ERROR_INPUT_PARAM;
98                break;
99            }
100         }
101
102         if (certChain.size() < 2)
103             retCode = CKM_API_ERROR_INPUT_PARAM;
104         else if (retCode == CKM_API_SUCCESS)
105             ocspStatus = ocsp.verify(certChain);
106     }
107
108     return MessageBuffer::Serialize(commandId, retCode, ocspStatus).Pop();
109 }
110
111 } // namespace CKM
112