summaryrefslogtreecommitdiff
path: root/src/manager/crypto/sw-backend/obj.cpp
blob: 8b5e21a7558aef6df80ef2d293e6a851e2fa7eaa (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 *  Copyright (c) 2000 - 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       obj.cpp
 * @author     Bartłomiej Grzelewski (b.grzelewski@samsung.com)
 * @version    1.0
 */
#include <memory>

#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/x509.h>

#include <dpl/log/log.h>

#include <generic-backend/exception.h>
#include <sw-backend/obj.h>
#include <sw-backend/internals.h>

#define EVP_SUCCESS 1    // DO NOTCHANGE THIS VALUE
#define EVP_FAIL    0    // DO NOTCHANGE THIS VALUE

namespace CKM {
namespace Crypto {
namespace SW {

namespace {

AlgoType key2algo(DataType type)
{
	switch (static_cast<int>(type)) {
	case DataType::Type::KEY_RSA_PRIVATE:
	case DataType::Type::KEY_RSA_PUBLIC:
		return AlgoType::RSA_SV;

	case DataType::Type::KEY_DSA_PRIVATE:
	case DataType::Type::KEY_DSA_PUBLIC:
		return AlgoType::DSA_SV;

	case DataType::Type::KEY_ECDSA_PRIVATE:
	case DataType::Type::KEY_ECDSA_PUBLIC:
		return AlgoType::ECDSA_SV;

	default:
		ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", type);
	}
}

} // namespace anonymous

typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr;

RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
{
	return Internals::symmetricEncrypt(getBinary(), alg, data);
}
RawBuffer SKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &cipher)
{
	return Internals::symmetricDecrypt(getBinary(), alg, cipher);
}

RawBuffer AKey::sign(
	const CryptoAlgorithm &alg,
	const RawBuffer &message)
{
	CryptoAlgorithm algWithType(alg);
	algWithType.setParam(ParamName::ALGO_TYPE, key2algo(m_type));
	return Internals::sign(getEvpShPtr().get(), algWithType, message);
}

int AKey::verify(const CryptoAlgorithm &alg, const RawBuffer &message,
				 const RawBuffer &sign)
{
	CryptoAlgorithm algWithType(alg);
	EVP_PKEY *evp = getEvpShPtr().get();
	AlgoType type;

	// setup algorithm type basing on evp key type if it doesn't exist
	if (!algWithType.getParam(ParamName::ALGO_TYPE, type)) {
		int subType = EVP_PKEY_type(evp->type);

		switch (subType) {
		case EVP_PKEY_RSA:
			type = AlgoType::RSA_SV;
			break;

		case EVP_PKEY_DSA:
			type = AlgoType::DSA_SV;
			break;

		case EVP_PKEY_EC:
			type = AlgoType::ECDSA_SV;
			break;

		default:
			ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", subType);
		}

		algWithType.setParam(ParamName::ALGO_TYPE, type);
	}

	return Internals::verify(evp, algWithType, message, sign);
}

RawBuffer AKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
{
	return Internals::asymmetricEncrypt(getEvpShPtr(), alg, data);
}

RawBuffer AKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
{
	return Internals::asymmetricDecrypt(getEvpShPtr(), alg, data);
}

EvpShPtr AKey::getEvpShPtr()
{
	if (m_evp)
		return m_evp;

	EVP_PKEY *pkey = NULL;
	BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);

	LogDebug("Start to parse key:");

	if (!pkey) {
		(void)BIO_reset(bio.get());
		BIO_write(bio.get(), m_raw.data(), m_raw.size());
		pkey = d2i_PrivateKey_bio(bio.get(), NULL);
		LogDebug("Trying d2i_PrivateKey_bio Status: " << (void *)pkey);
	}

	if (!pkey) {
		(void)BIO_reset(bio.get());
		BIO_write(bio.get(), m_raw.data(), m_raw.size());
		pkey = d2i_PUBKEY_bio(bio.get(), NULL);
		LogDebug("Trying d2i_PUBKEY_bio Status: " << (void *)pkey);
	}

	if (!pkey)
		ThrowErr(Exc::Crypto::InternalError, "Failed to parse key");

	m_evp.reset(pkey, EVP_PKEY_free);
	return m_evp;
}

EvpShPtr Cert::getEvpShPtr()
{
	if (m_evp)
		return m_evp;

	int size = static_cast<int>(m_raw.size());
	const unsigned char *ptr = reinterpret_cast<const unsigned char *>
							   (m_raw.data());

	X509 *x509 = d2i_X509(NULL, &ptr, size);

	if (!x509)
		ThrowErr(Exc::Crypto::InternalError, "Failed to parse certificate.");

	m_evp.reset(X509_get_pubkey(x509), EVP_PKEY_free);
	X509_free(x509);
	return m_evp;
}

} // namespace SW
} // namespace Crypto
} // namespace CKM