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 <openssl/evp.h>
23 #include <dpl/serialization.h>
24 #include <dpl/log/log.h>
26 #include <client-manager-impl.h>
27 #include <client-common.h>
28 #include <message-buffer.h>
29 #include <protocols.h>
30 #include <generic-key.h>
31 #include <certificate-impl.h>
35 void clientInitialize(void) {
36 OpenSSL_add_all_ciphers();
37 OpenSSL_add_all_algorithms();
38 OpenSSL_add_all_digests();
41 } // namespace anonymous
45 bool ManagerImpl::s_isInit = false;
47 ManagerImpl::ManagerImpl()
50 // TODO secure with mutex
59 int ManagerImpl::saveBinaryData(
62 const RawBuffer &rawData,
67 return try_catch([&] {
68 if (alias.empty() || rawData.empty())
69 return CKM_API_ERROR_INPUT_PARAM;
71 MessageBuffer send, recv;
72 Serialization::Serialize(send, static_cast<int>(LogicCommand::SAVE));
73 Serialization::Serialize(send, m_counter);
74 Serialization::Serialize(send, static_cast<int>(dataType));
75 Serialization::Serialize(send, alias);
76 Serialization::Serialize(send, rawData);
77 Serialization::Serialize(send, PolicySerializable(policy));
79 int retCode = sendToServer(
80 SERVICE_SOCKET_CKM_STORAGE,
84 if (CKM_API_SUCCESS != retCode) {
91 Deserialization::Deserialize(recv, command);
92 Deserialization::Deserialize(recv, counter);
93 Deserialization::Deserialize(recv, retCode);
94 Deserialization::Deserialize(recv, opType);
96 if (counter != m_counter) {
97 return CKM_API_ERROR_UNKNOWN;
104 int ManagerImpl::saveKey(const Alias &alias, const KeyShPtr &key, const Policy &policy) {
105 if (key.get() == NULL)
106 return CKM_API_ERROR_INPUT_PARAM;
107 return saveBinaryData(alias, toDBDataType(key->getType()), key->getDER(), policy);
110 int ManagerImpl::saveCertificate(
112 const CertificateShPtr &cert,
113 const Policy &policy)
115 if (cert.get() == NULL)
116 return CKM_API_ERROR_INPUT_PARAM;
117 return saveBinaryData(alias, DBDataType::CERTIFICATE, cert->getDER(), policy);
120 int ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
121 if (!policy.extractable)
122 return CKM_API_ERROR_INPUT_PARAM;
123 return saveBinaryData(alias, DBDataType::BINARY_DATA, rawData, policy);
126 int ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
128 return try_catch([&] {
130 return CKM_API_ERROR_INPUT_PARAM;
132 MessageBuffer send, recv;
133 Serialization::Serialize(send, static_cast<int>(LogicCommand::REMOVE));
134 Serialization::Serialize(send, m_counter);
135 Serialization::Serialize(send, static_cast<int>(dataType));
136 Serialization::Serialize(send, alias);
138 int retCode = sendToServer(
139 SERVICE_SOCKET_CKM_STORAGE,
143 if (CKM_API_SUCCESS != retCode) {
150 Deserialization::Deserialize(recv, command);
151 Deserialization::Deserialize(recv, counter);
152 Deserialization::Deserialize(recv, retCode);
153 Deserialization::Deserialize(recv, opType);
155 if (counter != m_counter) {
156 return CKM_API_ERROR_UNKNOWN;
163 int ManagerImpl::removeKey(const Alias &alias) {
164 return removeBinaryData(alias, DBDataType::KEY_RSA_PUBLIC);
167 int ManagerImpl::removeCertificate(const Alias &alias) {
168 return removeBinaryData(alias, DBDataType::CERTIFICATE);
171 int ManagerImpl::removeData(const Alias &alias) {
172 return removeBinaryData(alias, DBDataType::BINARY_DATA);
175 int ManagerImpl::getBinaryData(
177 DBDataType sendDataType,
178 const std::string &password,
179 DBDataType &recvDataType,
182 return try_catch([&] {
184 return CKM_API_ERROR_INPUT_PARAM;
186 MessageBuffer send, recv;
187 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET));
188 Serialization::Serialize(send, m_counter);
189 Serialization::Serialize(send, static_cast<int>(sendDataType));
190 Serialization::Serialize(send, alias);
191 Serialization::Serialize(send, password);
193 int retCode = sendToServer(
194 SERVICE_SOCKET_CKM_STORAGE,
198 if (CKM_API_SUCCESS != retCode) {
205 Deserialization::Deserialize(recv, command);
206 Deserialization::Deserialize(recv, counter);
207 Deserialization::Deserialize(recv, retCode);
208 Deserialization::Deserialize(recv, tmpDataType);
209 Deserialization::Deserialize(recv, rawData);
210 recvDataType = static_cast<DBDataType>(tmpDataType);
212 if (counter != m_counter) {
213 return CKM_API_ERROR_UNKNOWN;
220 int ManagerImpl::getKey(const Alias &alias, const std::string &password, KeyShPtr &key) {
221 DBDataType recvDataType;
224 int retCode = getBinaryData(
226 DBDataType::KEY_RSA_PUBLIC,
231 if (retCode != CKM_API_SUCCESS)
234 KeyShPtr keyParsed(new GenericKey(rawData));
236 if (keyParsed->empty()) {
237 LogDebug("Key empty - failed to parse!");
238 return CKM_API_ERROR_BAD_RESPONSE;
243 return CKM_API_SUCCESS;
246 int ManagerImpl::getCertificate(const Alias &alias, const std::string &password, CertificateShPtr &cert)
248 DBDataType recvDataType;
251 int retCode = getBinaryData(
253 DBDataType::CERTIFICATE,
258 if (retCode != CKM_API_SUCCESS)
261 if (recvDataType != DBDataType::CERTIFICATE)
262 return CKM_API_ERROR_BAD_RESPONSE;
264 CertificateShPtr certParsed(new CertificateImpl(rawData, DataFormat::FORM_DER));
266 if (certParsed->empty())
267 return CKM_API_ERROR_BAD_RESPONSE;
271 return CKM_API_SUCCESS;
274 int ManagerImpl::getData(const Alias &alias, const std::string &password, RawBuffer &rawData)
276 DBDataType recvDataType;
278 int retCode = getBinaryData(
280 DBDataType::BINARY_DATA,
285 if (retCode != CKM_API_SUCCESS)
288 if (recvDataType != DBDataType::BINARY_DATA)
289 return CKM_API_ERROR_BAD_RESPONSE;
291 return CKM_API_SUCCESS;
294 int ManagerImpl::getBinaryDataAliasVector(DBDataType dataType, AliasVector &aliasVector)
296 return try_catch([&] {
298 MessageBuffer send, recv;
299 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET_LIST));
300 Serialization::Serialize(send, m_counter);
301 Serialization::Serialize(send, static_cast<int>(dataType));
303 int retCode = sendToServer(
304 SERVICE_SOCKET_CKM_STORAGE,
308 if (CKM_API_SUCCESS != retCode) {
316 Deserialization::Deserialize(recv, command);
317 Deserialization::Deserialize(recv, counter);
318 Deserialization::Deserialize(recv, retCode);
319 Deserialization::Deserialize(recv, tmpDataType);
320 Deserialization::Deserialize(recv, aliasVector);
321 if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != m_counter)) {
322 return CKM_API_ERROR_UNKNOWN;
329 int ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
330 return getBinaryDataAliasVector(DBDataType::KEY_RSA_PUBLIC, aliasVector);
333 int ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
334 return getBinaryDataAliasVector(DBDataType::CERTIFICATE, aliasVector);
337 int ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
338 return getBinaryDataAliasVector(DBDataType::BINARY_DATA, aliasVector);
341 int ManagerImpl::createKeyPairRSA(
342 const int size, // size in bits [1024, 2048, 4096]
343 const Alias &privateKeyAlias,
344 const Alias &publicKeyAlias,
345 const Policy &policyPrivateKey,
346 const Policy &policyPublicKey)
349 int my_counter = m_counter;
350 return try_catch([&] {
352 MessageBuffer send, recv;
353 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
354 Serialization::Serialize(send, my_counter);
355 Serialization::Serialize(send, static_cast<int>(size));
356 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
357 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
358 Serialization::Serialize(send, privateKeyAlias);
359 Serialization::Serialize(send, publicKeyAlias);
361 int retCode = sendToServer(
362 SERVICE_SOCKET_CKM_STORAGE,
366 if (CKM_API_SUCCESS != retCode) {
373 Deserialization::Deserialize(recv, command);
374 Deserialization::Deserialize(recv, counter);
375 Deserialization::Deserialize(recv, retCode);
376 if (counter != my_counter) {
377 return CKM_API_ERROR_UNKNOWN;
384 int ManagerImpl::createKeyPairECDSA(
386 const Alias &privateKeyAlias,
387 const Alias &publicKeyAlias,
388 const Policy &policyPrivateKey,
389 const Policy &policyPublicKey)
392 int my_counter = m_counter;
393 return try_catch([&] {
395 MessageBuffer send, recv;
396 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_ECDSA));
397 Serialization::Serialize(send, my_counter);
398 Serialization::Serialize(send, static_cast<unsigned int>(type));
399 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
400 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
401 Serialization::Serialize(send, privateKeyAlias);
402 Serialization::Serialize(send, publicKeyAlias);
404 int retCode = sendToServer(
405 SERVICE_SOCKET_CKM_STORAGE,
409 if (CKM_API_SUCCESS != retCode) {
416 Deserialization::Deserialize(recv, command);
417 Deserialization::Deserialize(recv, counter);
418 Deserialization::Deserialize(recv, retCode);
420 if (counter != my_counter) {
421 return CKM_API_ERROR_UNKNOWN;
430 LogicCommand command,
432 const CertificateShPtr &certificate,
434 CertificateShPtrVector &certificateChainVector)
436 return try_catch([&] {
438 MessageBuffer send, recv;
439 Serialization::Serialize(send, static_cast<int>(command));
440 Serialization::Serialize(send, counter);
441 Serialization::Serialize(send, certificate->getDER());
442 Serialization::Serialize(send, sendData);
443 int retCode = sendToServer(
444 SERVICE_SOCKET_CKM_STORAGE,
448 if (CKM_API_SUCCESS != retCode) {
454 RawBufferVector rawBufferVector;
456 Deserialization::Deserialize(recv, retCommand);
457 Deserialization::Deserialize(recv, retCounter);
458 Deserialization::Deserialize(recv, retCode);
459 Deserialization::Deserialize(recv, rawBufferVector);
461 if ((counter != retCounter) || (static_cast<int>(command) != retCommand)) {
462 return CKM_API_ERROR_UNKNOWN;
465 if (retCode != CKM_API_SUCCESS) {
469 for (auto &e: rawBufferVector) {
470 CertificateShPtr cert(new CertificateImpl(e, DataFormat::FORM_DER));
472 return CKM_API_ERROR_BAD_RESPONSE;
473 certificateChainVector.push_back(cert);
481 int ManagerImpl::getCertificateChain(
482 const CertificateShPtr &certificate,
483 const CertificateShPtrVector &untrustedCertificates,
484 CertificateShPtrVector &certificateChainVector)
486 RawBufferVector rawBufferVector;
488 for (auto &e: untrustedCertificates) {
489 rawBufferVector.push_back(e->getDER());
493 LogicCommand::GET_CHAIN_CERT,
497 certificateChainVector);
500 int ManagerImpl::getCertificateChain(
501 const CertificateShPtr &certificate,
502 const AliasVector &untrustedCertificates,
503 CertificateShPtrVector &certificateChainVector)
506 LogicCommand::GET_CHAIN_ALIAS,
509 untrustedCertificates,
510 certificateChainVector);
513 int ManagerImpl::createSignature(
514 const Alias &privateKeyAlias,
515 const std::string &password, // password for private_key
516 const RawBuffer &message,
517 const HashAlgorithm hash,
518 const RSAPaddingAlgorithm padding,
519 RawBuffer &signature)
522 int my_counter = m_counter;
523 return try_catch([&] {
525 MessageBuffer send, recv;
526 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_SIGNATURE));
527 Serialization::Serialize(send, my_counter);
528 Serialization::Serialize(send, privateKeyAlias);
529 Serialization::Serialize(send, password);
530 Serialization::Serialize(send, message);
531 Serialization::Serialize(send, static_cast<int>(hash));
532 Serialization::Serialize(send, static_cast<int>(padding));
534 int retCode = sendToServer(
535 SERVICE_SOCKET_CKM_STORAGE,
539 if (CKM_API_SUCCESS != retCode) {
546 Deserialization::Deserialize(recv, command);
547 Deserialization::Deserialize(recv, counter);
548 Deserialization::Deserialize(recv, retCode);
549 Deserialization::Deserialize(recv, signature);
551 if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
552 || (counter != my_counter))
554 return CKM_API_ERROR_UNKNOWN;
561 int ManagerImpl::verifySignature(
562 const Alias &publicKeyOrCertAlias,
563 const std::string &password, // password for public_key (optional)
564 const RawBuffer &message,
565 const RawBuffer &signature,
566 const HashAlgorithm hash,
567 const RSAPaddingAlgorithm padding)
570 int my_counter = m_counter;
571 return try_catch([&] {
573 MessageBuffer send, recv;
574 Serialization::Serialize(send, static_cast<int>(LogicCommand::VERIFY_SIGNATURE));
575 Serialization::Serialize(send, my_counter);
576 Serialization::Serialize(send, publicKeyOrCertAlias);
577 Serialization::Serialize(send, password);
578 Serialization::Serialize(send, message);
579 Serialization::Serialize(send, signature);
580 Serialization::Serialize(send, static_cast<int>(hash));
581 Serialization::Serialize(send, static_cast<int>(padding));
583 int retCode = sendToServer(
584 SERVICE_SOCKET_CKM_STORAGE,
588 if (CKM_API_SUCCESS != retCode) {
595 Deserialization::Deserialize(recv, command);
596 Deserialization::Deserialize(recv, counter);
597 Deserialization::Deserialize(recv, retCode);
599 if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
600 || (counter != my_counter))
602 return CKM_API_ERROR_UNKNOWN;
609 int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspStatus)
611 return try_catch([&] {
612 int my_counter = ++m_counter;
613 MessageBuffer send, recv;
615 RawBufferVector rawCertChain;
616 for (auto &e: certChain) {
617 rawCertChain.push_back(e->getDER());
620 Serialization::Serialize(send, my_counter);
621 Serialization::Serialize(send, rawCertChain);
623 int retCode = sendToServer(
628 if (CKM_API_SUCCESS != retCode) {
634 Deserialization::Deserialize(recv, counter);
635 Deserialization::Deserialize(recv, retCode);
636 Deserialization::Deserialize(recv, ocspStatus);
638 if (my_counter != counter) {
639 return CKM_API_ERROR_UNKNOWN;
646 ManagerShPtr Manager::create() {
647 return ManagerShPtr(new ManagerImpl());