summaryrefslogtreecommitdiff
path: root/src/manager/common/protocols.cpp
blob: 8cf65725aab710f2f7827cd140bbaf87fa36bec9 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *  Copyright (c) 2016 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        protocols.cpp
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @author      Zofia Abramowska (z.abramowska@samsung.com)
 * @version     1.0
 * @brief       List of all protocols supported by Central Key Manager.
 */
#include "protocols.h"

#include <utility>

#include <dpl/serialization.h>
#include <dpl/log/log.h>
#include <ckm/ckm-type.h>

namespace CKM {

char const *const SERVICE_SOCKET_ECHO = "/tmp/.central-key-manager-echo.sock";
char const *const SERVICE_SOCKET_CKM_CONTROL =
	"/tmp/.central-key-manager-api-control.sock";
char const *const SERVICE_SOCKET_CKM_STORAGE =
	"/tmp/.central-key-manager-api-storage.sock";
char const *const SERVICE_SOCKET_OCSP =
	"/tmp/.central-key-manager-api-ocsp.sock";
char const *const SERVICE_SOCKET_ENCRYPTION =
	"/tmp/.central-key-manager-api-encryption.sock";
char const *const LABEL_NAME_SEPARATOR = " ";
char const *const OWNER_ID_SYSTEM = "/System";
char const *const OWNER_ID_ADMIN_USER = "/User";

PKCS12Serializable::PKCS12Serializable()
{
}

PKCS12Serializable::PKCS12Serializable(const PKCS12 &pkcs)
	: PKCS12Impl(pkcs)
{
}

PKCS12Serializable::PKCS12Serializable(PKCS12Serializable &&other)
	: PKCS12Impl(std::move(other))
{
}

PKCS12Serializable &PKCS12Serializable::operator=(PKCS12Serializable &&other)
{
	if (this == &other)
		return *this;

	m_pkey = std::move(other.m_pkey);
	m_cert = std::move(other.m_cert);
	m_ca = std::move(other.m_ca);

	return *this;
}

PKCS12Serializable::PKCS12Serializable(IStream &stream)
{
	bool keyPresent = false;
	Deserialization::Deserialize(stream, keyPresent);

	if (keyPresent) {
		int keyType;
		RawBuffer keyData;
		Deserialization::Deserialize(stream, keyType);
		Deserialization::Deserialize(stream, keyData);
		m_pkey = CKM::Key::create(keyData);

		if (m_pkey)
			LogDebug("private key from pkcs deserialized success. key size: " <<
					 keyData.size() << " and DER size: " << m_pkey->getDER().size());
		else
			LogError("private key from pkcs deserialized fail");
	}

	bool certPresent = false;
	Deserialization::Deserialize(stream, certPresent);

	if (certPresent) {
		RawBuffer certData;
		Deserialization::Deserialize(stream, certData);
		m_cert = CKM::Certificate::create(certData, DataFormat::FORM_DER);

		if (m_cert)
			LogDebug("certificate from pkcs deserialized success. cert size: " <<
					 certData.size() << " and DER size: " << m_cert->getDER().size());
		else
			LogError("certificate from pkcs deserialized fail");
	}

	size_t numCA = 0;
	Deserialization::Deserialize(stream, numCA);

	for (size_t i = 0; i < numCA; i++) {
		RawBuffer CAcertData;
		Deserialization::Deserialize(stream, CAcertData);
		auto ca = CKM::Certificate::create(CAcertData, DataFormat::FORM_DER);
		if (ca) {
			LogDebug("ca certificate from pkcs deserialized success. cert size: " <<
					 CAcertData.size() << " and DER size: " << ca->getDER().size());

			m_ca.emplace_back(std::move(ca));
		} else {
			LogError("ca certificate from pkcs deserialized fail");
		}
	}
}

PKCS12Serializable::PKCS12Serializable(KeyShPtr &&privKey,
									   CertificateShPtr &&cert, CertificateShPtrVector &&chainCerts)
{
	m_pkey = std::move(privKey);
	m_cert = std::move(cert);
	m_ca = std::move(chainCerts);
}

void PKCS12Serializable::Serialize(IStream &stream) const
{
	auto keyPtr = getKey();
	bool isKeyPresent = !!keyPtr;

	// logics if PKCS is correct or not is on the service side.
	// sending number of keys and certificates to allow proper parsing on the service side.
	// (what if no key or cert present? attempt to deserialize a not present key/cert would
	// throw an error and close the connection).
	Serialization::Serialize(stream, isKeyPresent);

	if (isKeyPresent) {
		Serialization::Serialize(stream, DataType(keyPtr->getType()));
		Serialization::Serialize(stream, keyPtr->getDER());
		LogDebug("private key from pkcs serialized success. key DER size: " <<
				 keyPtr->getDER().size());
	}

	auto certPtr = getCertificate();
	bool isCertPresent = !!certPtr;
	Serialization::Serialize(stream, isCertPresent);

	if (isCertPresent) {
		Serialization::Serialize(stream, certPtr->getDER());
		LogDebug("certificate from pkcs serialized success. cert DER size: " <<
				 certPtr->getDER().size());
	}

	auto caCertPtrVec = getCaCertificateShPtrVector();
	Serialization::Serialize(stream, caCertPtrVec.size());

	for (auto &caCertPtr : getCaCertificateShPtrVector()) {
		Serialization::Serialize(stream, caCertPtr->getDER());
		LogDebug("ca certificate from pkcs serialized success. cert DER size: " <<
				 caCertPtr->getDER().size());
	}
};


CryptoAlgorithmSerializable::CryptoAlgorithmSerializable()
{
}

CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(
	const CryptoAlgorithm &algo) :
	CryptoAlgorithm(algo)
{
}

CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(IStream &stream)
{
	size_t plen = 0;
	Deserializer<size_t>::Deserialize(stream, plen);

	while (plen) {
		ParamName name;
		uint64_t integer;
		RawBuffer buffer;
		int tmpName;
		Deserializer<int>::Deserialize(stream, tmpName);
		name = static_cast<ParamName>(tmpName);

		switch (name) {
		case ParamName::ED_IV:
		case ParamName::ED_AAD:
		case ParamName::ED_LABEL:
			Deserializer<RawBuffer>::Deserialize(stream, buffer);
			setParam(name, buffer);
			break;

		case ParamName::ALGO_TYPE:
		case ParamName::ED_CTR_LEN:
		case ParamName::ED_TAG_LEN:
		case ParamName::GEN_KEY_LEN:
		case ParamName::GEN_EC:
		case ParamName::SV_HASH_ALGO:
		case ParamName::SV_RSA_PADDING:
			Deserializer<uint64_t>::Deserialize(stream, integer);
			setParam(name, integer);
			break;

		default:
			ThrowMsg(UnsupportedParam, "Unsupported param name");
		}

		plen--;
	}
}

void CryptoAlgorithmSerializable::Serialize(IStream &stream) const
{
	Serializer<size_t>::Serialize(stream, m_params.size());

	for (const auto &it : m_params) {
		Serializer<int>::Serialize(stream, static_cast<int>(it.first));
		uint64_t integer;
		RawBuffer buffer;

		if (it.second->getInt(integer))
			Serializer<uint64_t>::Serialize(stream, integer);
		else if (it.second->getBuffer(buffer))
			Serializer<RawBuffer>::Serialize(stream, buffer);
		else
			ThrowMsg(UnsupportedParam, "Unsupported param type");
	}
}

} // namespace CKM