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 <crypto-init.h>
27 #include <client-manager-impl.h>
28 #include <client-common.h>
29 #include <message-buffer.h>
30 #include <protocols.h>
32 #include <certificate-impl.h>
39 ServiceConnection & serviceConnection,
42 const CertificateShPtr &certificate,
43 const T &untrustedVector,
44 const T &trustedVector,
45 bool useTrustedSystemCertificates,
46 CertificateShPtrVector &certificateChainVector)
48 return try_catch([&] {
51 auto send = MessageBuffer::Serialize(static_cast<int>(command),
53 certificate->getDER(),
56 useTrustedSystemCertificates);
58 int retCode = serviceConnection.processRequest(send.Pop(), recv);
59 if (CKM_API_SUCCESS != retCode)
64 RawBufferVector rawBufferVector;
65 recv.Deserialize(retCommand, retCounter, retCode, rawBufferVector);
67 if ((counter != retCounter) || (static_cast<int>(command) != retCommand)) {
68 return CKM_API_ERROR_UNKNOWN;
71 if (retCode != CKM_API_SUCCESS) {
75 for (auto &e: rawBufferVector) {
76 CertificateShPtr cert(new CertificateImpl(e, DataFormat::FORM_DER));
78 return CKM_API_ERROR_BAD_RESPONSE;
79 certificateChainVector.push_back(cert);
86 } // namespace anonymous
88 ManagerImpl::ManagerImpl()
90 m_storageConnection(SERVICE_SOCKET_CKM_STORAGE),
91 m_ocspConnection(SERVICE_SOCKET_OCSP),
92 m_encryptionConnection(SERVICE_SOCKET_ENCRYPTION)
98 int ManagerImpl::saveBinaryData(
101 const RawBuffer &rawData,
102 const Policy &policy)
104 int my_counter = ++m_counter;
106 return try_catch([&] {
107 if (alias.empty() || rawData.empty())
108 return CKM_API_ERROR_INPUT_PARAM;
111 AliasSupport helper(alias);
112 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE),
114 static_cast<int>(dataType),
118 PolicySerializable(policy));
120 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
121 if (CKM_API_SUCCESS != retCode)
127 recv.Deserialize(command, counter, retCode, opType);
129 if (counter != my_counter)
130 return CKM_API_ERROR_UNKNOWN;
136 int ManagerImpl::saveKey(const Alias &alias, const KeyShPtr &key, const Policy &policy) {
137 if (key.get() == NULL)
138 return CKM_API_ERROR_INPUT_PARAM;
140 return saveBinaryData(alias, DataType(key->getType()), key->getDER(), policy);
141 } Catch (DataType::Exception::Base) {
142 LogError("Error in key conversion. Could not convert KeyType::NONE to DBDataType!");
144 return CKM_API_ERROR_INPUT_PARAM;
147 int ManagerImpl::saveCertificate(
149 const CertificateShPtr &cert,
150 const Policy &policy)
152 if (cert.get() == NULL)
153 return CKM_API_ERROR_INPUT_PARAM;
154 return saveBinaryData(alias, DataType::CERTIFICATE, cert->getDER(), policy);
157 int ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
158 if (!policy.extractable)
159 return CKM_API_ERROR_INPUT_PARAM;
160 return saveBinaryData(alias, DataType::BINARY_DATA, rawData, policy);
164 int ManagerImpl::savePKCS12(
166 const PKCS12ShPtr &pkcs,
167 const Policy &keyPolicy,
168 const Policy &certPolicy)
170 if (alias.empty() || pkcs.get()==NULL)
171 return CKM_API_ERROR_INPUT_PARAM;
173 int my_counter = ++m_counter;
175 return try_catch([&] {
177 AliasSupport helper(alias);
178 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE_PKCS12),
182 PKCS12Serializable(*pkcs.get()),
183 PolicySerializable(keyPolicy),
184 PolicySerializable(certPolicy));
186 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
187 if (CKM_API_SUCCESS != retCode)
192 recv.Deserialize(command, counter, retCode);
194 if (counter != my_counter)
195 return CKM_API_ERROR_UNKNOWN;
201 int ManagerImpl::getPKCS12(const Alias &alias, PKCS12ShPtr &pkcs)
203 return getPKCS12(alias, Password(), Password(), pkcs);
206 int ManagerImpl::getPKCS12(const Alias &alias, const Password &keyPass, const Password &certPass, PKCS12ShPtr &pkcs)
209 return CKM_API_ERROR_INPUT_PARAM;
211 int my_counter = ++m_counter;
213 return try_catch([&] {
215 AliasSupport helper(alias);
216 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_PKCS12),
223 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
224 if (CKM_API_SUCCESS != retCode)
229 PKCS12Serializable gotPkcs;
230 recv.Deserialize(command, counter, retCode, gotPkcs);
232 if (counter != my_counter)
233 return CKM_API_ERROR_UNKNOWN;
235 pkcs = std::make_shared<PKCS12Impl>(std::move(gotPkcs));
242 int ManagerImpl::removeAlias(const Alias &alias)
245 return CKM_API_ERROR_INPUT_PARAM;
247 int my_counter = ++m_counter;
249 return try_catch([&] {
251 AliasSupport helper(alias);
252 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::REMOVE),
257 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
258 if (CKM_API_SUCCESS != retCode)
263 recv.Deserialize(command, counter, retCode);
265 if (counter != my_counter)
266 return CKM_API_ERROR_UNKNOWN;
272 int ManagerImpl::getBinaryData(
274 DataType sendDataType,
275 const Password &password,
276 DataType &recvDataType,
280 return CKM_API_ERROR_INPUT_PARAM;
282 int my_counter = ++m_counter;
284 return try_catch([&] {
286 AliasSupport helper(alias);
287 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET),
289 static_cast<int>(sendDataType),
294 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
295 if (CKM_API_SUCCESS != retCode)
301 recv.Deserialize(command, counter, retCode, tmpDataType, rawData);
302 recvDataType = DataType(tmpDataType);
304 if (counter != my_counter)
305 return CKM_API_ERROR_UNKNOWN;
311 int ManagerImpl::getKey(const Alias &alias, const Password &password, KeyShPtr &key) {
312 DataType recvDataType;
315 int retCode = getBinaryData(
317 DataType::KEY_RSA_PUBLIC,
322 if (retCode != CKM_API_SUCCESS)
325 KeyShPtr keyParsed(new KeyImpl(rawData));
327 if (keyParsed->empty()) {
328 LogDebug("Key empty - failed to parse!");
329 return CKM_API_ERROR_BAD_RESPONSE;
334 return CKM_API_SUCCESS;
337 int ManagerImpl::getCertificate(const Alias &alias, const Password &password, CertificateShPtr &cert)
339 DataType recvDataType;
342 int retCode = getBinaryData(
344 DataType::CERTIFICATE,
349 if (retCode != CKM_API_SUCCESS)
352 if (recvDataType != DataType::CERTIFICATE)
353 return CKM_API_ERROR_BAD_RESPONSE;
355 CertificateShPtr certParsed(new CertificateImpl(rawData, DataFormat::FORM_DER));
357 if (certParsed->empty())
358 return CKM_API_ERROR_BAD_RESPONSE;
362 return CKM_API_SUCCESS;
365 int ManagerImpl::getData(const Alias &alias, const Password &password, RawBuffer &rawData)
367 DataType recvDataType = DataType::BINARY_DATA;
369 int retCode = getBinaryData(
371 DataType::BINARY_DATA,
376 if (retCode != CKM_API_SUCCESS)
379 if (recvDataType != DataType::BINARY_DATA)
380 return CKM_API_ERROR_BAD_RESPONSE;
382 return CKM_API_SUCCESS;
385 int ManagerImpl::getBinaryDataAliasVector(DataType dataType, AliasVector &aliasVector)
387 int my_counter = ++m_counter;
389 return try_catch([&] {
391 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
393 static_cast<int>(dataType));
395 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
396 if (CKM_API_SUCCESS != retCode)
402 LabelNameVector labelNameVector;
403 recv.Deserialize(command, counter, retCode, tmpDataType, labelNameVector);
404 if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != my_counter)) {
405 return CKM_API_ERROR_UNKNOWN;
408 for(const auto &it : labelNameVector)
409 aliasVector.push_back( AliasSupport::merge(it.first, it.second) );
415 int ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
416 // in fact datatype has no meaning here - if not certificate or binary data
417 // then manager decides to list all between DB_KEY_FIRST and DB_KEY_LAST
418 return getBinaryDataAliasVector(DataType::DB_KEY_LAST, aliasVector);
421 int ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
422 return getBinaryDataAliasVector(DataType::CERTIFICATE, aliasVector);
425 int ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
426 return getBinaryDataAliasVector(DataType::BINARY_DATA, aliasVector);
429 int ManagerImpl::createKeyPairRSA(
431 const Alias &privateKeyAlias,
432 const Alias &publicKeyAlias,
433 const Policy &policyPrivateKey,
434 const Policy &policyPublicKey)
436 return this->createKeyPair(CKM::KeyType::KEY_RSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
439 int ManagerImpl::createKeyPairDSA(
441 const Alias &privateKeyAlias,
442 const Alias &publicKeyAlias,
443 const Policy &policyPrivateKey,
444 const Policy &policyPublicKey)
446 return this->createKeyPair(CKM::KeyType::KEY_DSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
449 int ManagerImpl::createKeyPairECDSA(
451 const Alias &privateKeyAlias,
452 const Alias &publicKeyAlias,
453 const Policy &policyPrivateKey,
454 const Policy &policyPublicKey)
456 return this->createKeyPair(CKM::KeyType::KEY_ECDSA_PUBLIC, static_cast<int>(type), privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
459 int ManagerImpl::createKeyPair(
460 const KeyType key_type,
461 const int additional_param,
462 const Alias &privateKeyAlias,
463 const Alias &publicKeyAlias,
464 const Policy &policyPrivateKey,
465 const Policy &policyPublicKey)
468 CryptoAlgorithm keyGenAlgorithm;
471 case KeyType::KEY_RSA_PUBLIC:
472 case KeyType::KEY_RSA_PRIVATE:
473 keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
474 keyGenAlgorithm.addParam(ParamName::GEN_KEY_LEN, additional_param);
477 case KeyType::KEY_DSA_PUBLIC:
478 case KeyType::KEY_DSA_PRIVATE:
479 keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
480 keyGenAlgorithm.addParam(ParamName::GEN_KEY_LEN, additional_param);
483 case KeyType::KEY_ECDSA_PUBLIC:
484 case KeyType::KEY_ECDSA_PRIVATE:
485 keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
486 keyGenAlgorithm.addParam(ParamName::GEN_EC, additional_param);
490 return CKM_API_ERROR_INPUT_PARAM;
493 // proceed with sending request
494 int my_counter = ++m_counter;
496 return try_catch([&] {
499 AliasSupport privateHelper(privateKeyAlias);
500 AliasSupport publicHelper(publicKeyAlias);
501 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_KEY_PAIR),
503 CryptoAlgorithmSerializable(keyGenAlgorithm),
504 PolicySerializable(policyPrivateKey),
505 PolicySerializable(policyPublicKey),
506 privateHelper.getName(),
507 privateHelper.getLabel(),
508 publicHelper.getName(),
509 publicHelper.getLabel());
511 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
512 if (CKM_API_SUCCESS != retCode)
517 recv.Deserialize(command, counter, retCode);
518 if (counter != my_counter) {
519 return CKM_API_ERROR_UNKNOWN;
526 int ManagerImpl::getCertificateChain(
527 const CertificateShPtr &certificate,
528 const CertificateShPtrVector &untrustedCertificates,
529 const CertificateShPtrVector &trustedCertificates,
530 bool useTrustedSystemCertificates,
531 CertificateShPtrVector &certificateChainVector)
533 RawBufferVector untrustedVector;
534 RawBufferVector trustedVector;
536 for (auto &e: untrustedCertificates) {
537 untrustedVector.push_back(e->getDER());
539 for (auto &e: trustedCertificates) {
540 trustedVector.push_back(e->getDER());
545 LogicCommand::GET_CHAIN_CERT,
550 useTrustedSystemCertificates,
551 certificateChainVector);
554 int ManagerImpl::getCertificateChain(
555 const CertificateShPtr &certificate,
556 const AliasVector &untrustedCertificates,
557 const AliasVector &trustedCertificates,
558 bool useTrustedSystemCertificates,
559 CertificateShPtrVector &certificateChainVector)
561 LabelNameVector untrustedVector;
562 LabelNameVector trustedVector;
564 for (auto &e: untrustedCertificates) {
565 AliasSupport helper(e);
566 untrustedVector.push_back(std::make_pair(helper.getLabel(), helper.getName()));
568 for (auto &e: trustedCertificates) {
569 AliasSupport helper(e);
570 trustedVector.push_back(std::make_pair(helper.getLabel(), helper.getName()));
575 LogicCommand::GET_CHAIN_ALIAS,
580 useTrustedSystemCertificates,
581 certificateChainVector);
584 int ManagerImpl::createSignature(
585 const Alias &privateKeyAlias,
586 const Password &password, // password for private_key
587 const RawBuffer &message,
588 const HashAlgorithm hash,
589 const RSAPaddingAlgorithm padding,
590 RawBuffer &signature)
592 int my_counter = ++m_counter;
594 return try_catch([&] {
597 AliasSupport helper(privateKeyAlias);
598 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
604 static_cast<int>(hash),
605 static_cast<int>(padding));
607 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
608 if (CKM_API_SUCCESS != retCode)
613 recv.Deserialize(command, counter, retCode, signature);
615 if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
616 || (counter != my_counter))
618 return CKM_API_ERROR_UNKNOWN;
625 int ManagerImpl::verifySignature(
626 const Alias &publicKeyOrCertAlias,
627 const Password &password, // password for public_key (optional)
628 const RawBuffer &message,
629 const RawBuffer &signature,
630 const HashAlgorithm hash,
631 const RSAPaddingAlgorithm padding)
633 int my_counter = ++m_counter;
635 return try_catch([&] {
637 AliasSupport helper(publicKeyOrCertAlias);
638 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
645 static_cast<int>(hash),
646 static_cast<int>(padding));
648 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
649 if (CKM_API_SUCCESS != retCode)
654 recv.Deserialize(command, counter, retCode);
656 if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
657 || (counter != my_counter))
659 return CKM_API_ERROR_UNKNOWN;
666 int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspStatus)
668 return try_catch([&] {
669 int my_counter = ++m_counter;
672 RawBufferVector rawCertChain;
673 for (auto &e: certChain) {
674 rawCertChain.push_back(e->getDER());
677 auto send = MessageBuffer::Serialize(my_counter, rawCertChain);
679 int retCode = m_ocspConnection.processRequest(send.Pop(), recv);
680 if (CKM_API_SUCCESS != retCode)
684 recv.Deserialize(counter, retCode, ocspStatus);
686 if (my_counter != counter) {
687 return CKM_API_ERROR_UNKNOWN;
694 int ManagerImpl::setPermission(const Alias &alias,
695 const Label &accessor,
696 PermissionMask permissionMask)
698 int my_counter = ++m_counter;
700 return try_catch([&] {
702 AliasSupport helper(alias);
703 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SET_PERMISSION),
710 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
711 if (CKM_API_SUCCESS != retCode)
716 recv.Deserialize(command, counter, retCode);
718 if (my_counter != counter) {
719 return CKM_API_ERROR_UNKNOWN;
726 int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
727 const Alias &keyAlias,
728 const Password &password,
729 const RawBuffer& plain,
730 RawBuffer& encrypted)
732 int my_counter = ++m_counter;
734 return try_catch([&] {
736 AliasSupport helper(keyAlias);
737 CryptoAlgorithmSerializable cas(algo);
738 auto send = MessageBuffer::Serialize(static_cast<int>(EncryptionCommand::ENCRYPT),
746 int retCode = m_encryptionConnection.processRequest(send.Pop(), recv);
747 if (CKM_API_SUCCESS != retCode)
752 recv.Deserialize(command, counter, encrypted);
754 if (my_counter != counter) {
755 return CKM_API_ERROR_UNKNOWN;
762 int ManagerImpl::decrypt(const CryptoAlgorithm &algo,
763 const Alias &keyAlias,
764 const Password &password,
765 const RawBuffer& encrypted,
766 RawBuffer& decrypted)
768 int my_counter = ++m_counter;
770 return try_catch([&] {
772 AliasSupport helper(keyAlias);
773 CryptoAlgorithmSerializable cas(algo);
774 auto send = MessageBuffer::Serialize(static_cast<int>(EncryptionCommand::DECRYPT),
782 int retCode = m_encryptionConnection.processRequest(send.Pop(), recv);
783 if (CKM_API_SUCCESS != retCode)
788 recv.Deserialize(command, counter, decrypted);
790 if (my_counter != counter) {
791 return CKM_API_ERROR_UNKNOWN;
798 ManagerShPtr Manager::create() {
800 return std::make_shared<ManagerImpl>();
801 } catch (const std::bad_alloc &) {
802 LogDebug("Bad alloc was caught during ManagerImpl creation.");
804 LogError("Critical error: Unknown exception was caught during ManagerImpl creation!");
806 return ManagerShPtr();