summaryrefslogtreecommitdiff
path: root/src/manager/service/ocsp-logic.cpp
blob: fe2a8d2b6966d92afe88cbf0d78b06baf9fd3a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *  Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        ocsp-logic.cpp
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @author      Kyungwook Tak (k.tak@samsung.com)
 * @version     1.0
 * @brief       OCSP logic implementation.
 */

#include <vector>
#include <string>

#include <system_info.h>

#include <ckm/ckm-error.h>

#include <dpl/log/log.h>
#include <dpl/serialization.h>

#include <message-buffer.h>

#include <ocsp-logic.h>
#include <ocsp.h>

namespace {

const std::vector<std::string> FEATURES = {
	"tizen.org/feature/network.internet",
	"tizen.org/feature/network.telephony",
	"tizen.org/feature/network.tethering.bluetooth",
	"tizen.org/feature/network.ethernet"
};

} // namespace anonymous

namespace CKM {

OCSPLogic::OCSPLogic() : m_isNetAvailable(false)
{
	setNetAvailable();
}

void OCSPLogic::setNetAvailable()
{
	bool value;
	int ret;

	for (const auto &feature : FEATURES) {
		value = false;

		ret = system_info_get_platform_bool(feature.c_str(), &value);

		if (ret != SYSTEM_INFO_ERROR_NONE) {
			LogError("Error in system_info_get_platform_bool. ret : " << ret);
			continue;
		}

		if (value) {
			m_isNetAvailable = true;
			return;
		}
	}

	m_isNetAvailable = false;
}

RawBuffer OCSPLogic::ocspCheck(int commandId, const RawBufferVector &rawChain,
							   bool allowed)
{
	CertificateImplVector certChain;
	OCSPModule ocsp;
	int retCode = CKM_API_SUCCESS;
	int ocspStatus = CKM_API_OCSP_STATUS_INTERNAL_ERROR;

	if (!m_isNetAvailable) {
		/* try again for in case of system-info error */
		setNetAvailable();
	}

	if (!m_isNetAvailable) {
		retCode = CKM_API_ERROR_NOT_SUPPORTED;
	} else if (!allowed) {
		retCode = CKM_API_ERROR_ACCESS_DENIED;
	} else if (rawChain.size() < 2) {
		LogError("Certificate chain should contain at least 2 certificates");
		retCode = CKM_API_ERROR_INPUT_PARAM;
	} else {
		for (auto &e : rawChain) {
			certChain.push_back(CertificateImpl(e, DataFormat::FORM_DER));

			if (certChain.rbegin()->empty()) {
				LogDebug("Error in parsing certificates!");
				retCode = CKM_API_ERROR_INPUT_PARAM;
				break;
			}
		}
	}

	if (retCode == CKM_API_SUCCESS)
		ocspStatus = ocsp.verify(certChain);

	return MessageBuffer::Serialize(commandId, retCode, ocspStatus).Pop();
}

} // namespace CKM