1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License
16 * @file client-manager-impl.cpp
17 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19 * @brief Manager implementation.
21 #include <dpl/serialization.h>
22 #include <dpl/log/log.h>
24 #include <client-manager-impl.h>
25 #include <client-common.h>
26 #include <message-buffer.h>
27 #include <protocols.h>
31 int Manager::ManagerImpl::saveBinaryData(
34 const RawBuffer &rawData,
39 return try_catch([&] {
40 if (alias.empty() || rawData.empty())
41 return CKM_API_ERROR_INPUT_PARAM;
43 MessageBuffer send, recv;
44 Serialization::Serialize(send, static_cast<int>(LogicCommand::SAVE));
45 Serialization::Serialize(send, m_counter);
46 Serialization::Serialize(send, static_cast<int>(dataType));
47 Serialization::Serialize(send, alias);
48 Serialization::Serialize(send, rawData);
49 Serialization::Serialize(send, PolicySerializable(policy));
51 int retCode = sendToServer(
52 SERVICE_SOCKET_CKM_STORAGE,
56 if (CKM_API_SUCCESS != retCode) {
63 Deserialization::Deserialize(recv, command);
64 Deserialization::Deserialize(recv, counter);
65 Deserialization::Deserialize(recv, retCode);
66 Deserialization::Deserialize(recv, opType);
68 if (counter != m_counter) {
69 return CKM_API_ERROR_UNKNOWN;
76 int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Policy &policy) {
78 return CKM_API_ERROR_INPUT_PARAM;
79 return saveBinaryData(alias, toDBDataType(key.getType()), key.getDER(), policy);
82 int Manager::ManagerImpl::saveCertificate(
84 const Certificate &cert,
87 return saveBinaryData(alias, DBDataType::CERTIFICATE, cert.getDER(), policy);
90 int Manager::ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
91 if (!policy.extractable)
92 return CKM_API_ERROR_INPUT_PARAM;
93 return saveBinaryData(alias, DBDataType::BINARY_DATA, rawData, policy);
96 int Manager::ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
98 return try_catch([&] {
100 return CKM_API_ERROR_INPUT_PARAM;
102 MessageBuffer send, recv;
103 Serialization::Serialize(send, static_cast<int>(LogicCommand::REMOVE));
104 Serialization::Serialize(send, m_counter);
105 Serialization::Serialize(send, static_cast<int>(dataType));
106 Serialization::Serialize(send, alias);
108 int retCode = sendToServer(
109 SERVICE_SOCKET_CKM_STORAGE,
113 if (CKM_API_SUCCESS != retCode) {
120 Deserialization::Deserialize(recv, command);
121 Deserialization::Deserialize(recv, counter);
122 Deserialization::Deserialize(recv, retCode);
123 Deserialization::Deserialize(recv, opType);
125 if (counter != m_counter) {
126 return CKM_API_ERROR_UNKNOWN;
133 int Manager::ManagerImpl::removeKey(const Alias &alias) {
134 return removeBinaryData(alias, DBDataType::KEY_RSA_PUBLIC);
137 int Manager::ManagerImpl::removeCertificate(const Alias &alias) {
138 return removeBinaryData(alias, DBDataType::CERTIFICATE);
141 int Manager::ManagerImpl::removeData(const Alias &alias) {
142 return removeBinaryData(alias, DBDataType::BINARY_DATA);
145 int Manager::ManagerImpl::getBinaryData(
147 DBDataType sendDataType,
148 const std::string &password,
149 DBDataType &recvDataType,
152 return try_catch([&] {
154 return CKM_API_ERROR_INPUT_PARAM;
156 MessageBuffer send, recv;
157 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET));
158 Serialization::Serialize(send, m_counter);
159 Serialization::Serialize(send, static_cast<int>(sendDataType));
160 Serialization::Serialize(send, alias);
161 Serialization::Serialize(send, password);
163 int retCode = sendToServer(
164 SERVICE_SOCKET_CKM_STORAGE,
168 if (CKM_API_SUCCESS != retCode) {
175 Deserialization::Deserialize(recv, command);
176 Deserialization::Deserialize(recv, counter);
177 Deserialization::Deserialize(recv, retCode);
178 Deserialization::Deserialize(recv, tmpDataType);
179 Deserialization::Deserialize(recv, rawData);
180 recvDataType = static_cast<DBDataType>(tmpDataType);
182 if (counter != m_counter) {
183 return CKM_API_ERROR_UNKNOWN;
190 int Manager::ManagerImpl::getKey(const Alias &alias, const std::string &password, Key &key) {
191 DBDataType recvDataType;
194 int retCode = getBinaryData(
196 DBDataType::KEY_RSA_PUBLIC,
201 if (retCode != CKM_API_SUCCESS)
204 Key keyParsed(rawData);
206 if (keyParsed.empty()) {
207 LogDebug("Key empty - failed to parse!");
208 return CKM_API_ERROR_BAD_RESPONSE;
213 return CKM_API_SUCCESS;
216 int Manager::ManagerImpl::getCertificate(const Alias &alias, const std::string &password, Certificate &cert)
218 DBDataType recvDataType;
221 int retCode = getBinaryData(
223 DBDataType::CERTIFICATE,
228 if (retCode != CKM_API_SUCCESS)
231 if (recvDataType != DBDataType::CERTIFICATE)
232 return CKM_API_ERROR_BAD_RESPONSE;
234 Certificate certParsed(rawData, DataFormat::FORM_DER);
236 if (certParsed.empty())
237 return CKM_API_ERROR_BAD_RESPONSE;
241 return CKM_API_SUCCESS;
244 int Manager::ManagerImpl::getData(const Alias &alias, const std::string &password, RawBuffer &rawData)
246 DBDataType recvDataType;
248 int retCode = getBinaryData(
250 DBDataType::BINARY_DATA,
255 if (retCode != CKM_API_SUCCESS)
258 if (recvDataType != DBDataType::BINARY_DATA)
259 return CKM_API_ERROR_BAD_RESPONSE;
261 return CKM_API_SUCCESS;
264 int Manager::ManagerImpl::getBinaryDataAliasVector(DBDataType dataType, AliasVector &aliasVector)
266 return try_catch([&] {
268 MessageBuffer send, recv;
269 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET_LIST));
270 Serialization::Serialize(send, m_counter);
271 Serialization::Serialize(send, static_cast<int>(dataType));
273 int retCode = sendToServer(
274 SERVICE_SOCKET_CKM_STORAGE,
278 if (CKM_API_SUCCESS != retCode) {
286 Deserialization::Deserialize(recv, command);
287 Deserialization::Deserialize(recv, counter);
288 Deserialization::Deserialize(recv, retCode);
289 Deserialization::Deserialize(recv, tmpDataType);
290 Deserialization::Deserialize(recv, aliasVector);
291 if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != m_counter)) {
292 return CKM_API_ERROR_UNKNOWN;
299 int Manager::ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
300 return getBinaryDataAliasVector(DBDataType::KEY_RSA_PUBLIC, aliasVector);
303 int Manager::ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
304 return getBinaryDataAliasVector(DBDataType::CERTIFICATE, aliasVector);
307 int Manager::ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
308 return getBinaryDataAliasVector(DBDataType::BINARY_DATA, aliasVector);
311 int Manager::ManagerImpl::createKeyPairRSA(
312 const int size, // size in bits [1024, 2048, 4096]
313 const Alias &privateKeyAlias,
314 const Alias &publicKeyAlias,
315 const Policy &policyPrivateKey,
316 const Policy &policyPublicKey)
319 int my_counter = m_counter;
320 return try_catch([&] {
322 MessageBuffer send, recv;
323 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
324 Serialization::Serialize(send, my_counter);
325 Serialization::Serialize(send, static_cast<int>(size));
326 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
327 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
328 Serialization::Serialize(send, privateKeyAlias);
329 Serialization::Serialize(send, publicKeyAlias);
331 int retCode = sendToServer(
332 SERVICE_SOCKET_CKM_STORAGE,
336 if (CKM_API_SUCCESS != retCode) {
343 Deserialization::Deserialize(recv, command);
344 Deserialization::Deserialize(recv, counter);
345 Deserialization::Deserialize(recv, retCode);
346 if (counter != my_counter) {
347 return CKM_API_ERROR_UNKNOWN;
354 int Manager::ManagerImpl::createKeyPairECDSA(
356 const Alias &privateKeyAlias,
357 const Alias &publicKeyAlias,
358 const Policy &policyPrivateKey,
359 const Policy &policyPublicKey)
362 int my_counter = m_counter;
363 return try_catch([&] {
365 MessageBuffer send, recv;
366 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_ECDSA));
367 Serialization::Serialize(send, my_counter);
368 Serialization::Serialize(send, static_cast<unsigned int>(type));
369 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
370 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
371 Serialization::Serialize(send, privateKeyAlias);
372 Serialization::Serialize(send, publicKeyAlias);
374 int retCode = sendToServer(
375 SERVICE_SOCKET_CKM_STORAGE,
379 if (CKM_API_SUCCESS != retCode) {
386 Deserialization::Deserialize(recv, command);
387 Deserialization::Deserialize(recv, counter);
388 Deserialization::Deserialize(recv, retCode);
390 if (counter != my_counter) {
391 return CKM_API_ERROR_UNKNOWN;
400 LogicCommand command,
402 const Certificate &certificate,
404 CertificateVector &certificateChainVector)
406 return try_catch([&] {
408 MessageBuffer send, recv;
409 Serialization::Serialize(send, static_cast<int>(command));
410 Serialization::Serialize(send, counter);
411 Serialization::Serialize(send, certificate.getDER());
412 Serialization::Serialize(send, sendData);
413 int retCode = sendToServer(
414 SERVICE_SOCKET_CKM_STORAGE,
418 if (CKM_API_SUCCESS != retCode) {
424 RawBufferVector rawBufferVector;
426 Deserialization::Deserialize(recv, retCommand);
427 Deserialization::Deserialize(recv, retCounter);
428 Deserialization::Deserialize(recv, retCode);
429 Deserialization::Deserialize(recv, rawBufferVector);
431 if ((counter != retCounter) || (static_cast<int>(command) != retCommand)) {
432 return CKM_API_ERROR_UNKNOWN;
435 if (retCode != CKM_API_SUCCESS) {
439 for (auto &e: rawBufferVector)
440 certificateChainVector.push_back(Certificate(e, DataFormat::FORM_DER));
447 int Manager::ManagerImpl::getCertificateChain(
448 const Certificate &certificate,
449 const CertificateVector &untrustedCertificates,
450 CertificateVector &certificateChainVector)
452 RawBufferVector rawBufferVector;
454 for (auto &e: untrustedCertificates) {
455 rawBufferVector.push_back(e.getDER());
459 LogicCommand::GET_CHAIN_CERT,
463 certificateChainVector);
466 int Manager::ManagerImpl::getCertificateChain(
467 const Certificate &certificate,
468 const AliasVector &untrustedCertificates,
469 CertificateVector &certificateChainVector)
472 (void) untrustedCertificates;
473 (void) certificateChainVector;
474 return CKM_API_ERROR_UNKNOWN;
477 int Manager::ManagerImpl::createSignature(
478 const Alias &privateKeyAlias,
479 const std::string &password, // password for private_key
480 const RawBuffer &message,
481 const HashAlgorithm hash,
482 const RSAPaddingAlgorithm padding,
483 RawBuffer &signature)
486 int my_counter = m_counter;
487 return try_catch([&] {
489 MessageBuffer send, recv;
490 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_SIGNATURE));
491 Serialization::Serialize(send, my_counter);
492 Serialization::Serialize(send, privateKeyAlias);
493 Serialization::Serialize(send, password);
494 Serialization::Serialize(send, message);
495 Serialization::Serialize(send, static_cast<int>(hash));
496 Serialization::Serialize(send, static_cast<int>(padding));
498 int retCode = sendToServer(
499 SERVICE_SOCKET_CKM_STORAGE,
503 if (CKM_API_SUCCESS != retCode) {
510 Deserialization::Deserialize(recv, command);
511 Deserialization::Deserialize(recv, counter);
512 Deserialization::Deserialize(recv, signature);
514 if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE)) || (counter != my_counter)) {
515 return CKM_API_ERROR_UNKNOWN;
522 int Manager::ManagerImpl::verifySignature(
523 const Alias &publicKeyOrCertAlias,
524 const std::string &password, // password for public_key (optional)
525 const RawBuffer &message,
526 const RawBuffer &signature,
527 const HashAlgorithm hash,
528 const RSAPaddingAlgorithm padding)
531 int my_counter = m_counter;
532 return try_catch([&] {
534 MessageBuffer send, recv;
535 Serialization::Serialize(send, static_cast<int>(LogicCommand::VERIFY_SIGNATURE));
536 Serialization::Serialize(send, my_counter);
537 Serialization::Serialize(send, publicKeyOrCertAlias);
538 Serialization::Serialize(send, password);
539 Serialization::Serialize(send, message);
540 Serialization::Serialize(send, signature);
541 Serialization::Serialize(send, static_cast<int>(hash));
542 Serialization::Serialize(send, static_cast<int>(padding));
544 int retCode = sendToServer(
545 SERVICE_SOCKET_CKM_STORAGE,
549 if (CKM_API_SUCCESS != retCode) {
556 Deserialization::Deserialize(recv, command);
557 Deserialization::Deserialize(recv, counter);
559 if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE)) || (counter != my_counter)) {
560 return CKM_API_ERROR_UNKNOWN;