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>
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()
89 : m_counter(0), m_storageConnection(SERVICE_SOCKET_CKM_STORAGE), m_ocspConnection(SERVICE_SOCKET_OCSP)
95 int ManagerImpl::saveBinaryData(
98 const RawBuffer &rawData,
101 int my_counter = ++m_counter;
103 return try_catch([&] {
104 if (alias.empty() || rawData.empty())
105 return CKM_API_ERROR_INPUT_PARAM;
108 AliasSupport helper(alias);
109 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE),
111 static_cast<int>(dataType),
115 PolicySerializable(policy));
117 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
118 if (CKM_API_SUCCESS != retCode)
124 recv.Deserialize(command, counter, retCode, opType);
126 if (counter != my_counter)
127 return CKM_API_ERROR_UNKNOWN;
133 int ManagerImpl::saveKey(const Alias &alias, const KeyShPtr &key, const Policy &policy) {
134 if (key.get() == NULL)
135 return CKM_API_ERROR_INPUT_PARAM;
137 return saveBinaryData(alias, DataType(key->getType()), key->getDER(), policy);
138 } Catch (DataType::Exception::Base) {
139 LogError("Error in key conversion. Could not convert KeyType::NONE to DBDataType!");
141 return CKM_API_ERROR_INPUT_PARAM;
144 int ManagerImpl::saveCertificate(
146 const CertificateShPtr &cert,
147 const Policy &policy)
149 if (cert.get() == NULL)
150 return CKM_API_ERROR_INPUT_PARAM;
151 return saveBinaryData(alias, DataType::CERTIFICATE, cert->getDER(), policy);
154 int ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
155 if (!policy.extractable)
156 return CKM_API_ERROR_INPUT_PARAM;
157 return saveBinaryData(alias, DataType::BINARY_DATA, rawData, policy);
161 int ManagerImpl::savePKCS12(
163 const PKCS12ShPtr &pkcs,
164 const Policy &keyPolicy,
165 const Policy &certPolicy)
167 if (alias.empty() || pkcs.get()==NULL)
168 return CKM_API_ERROR_INPUT_PARAM;
170 int my_counter = ++m_counter;
172 return try_catch([&] {
174 AliasSupport helper(alias);
175 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE_PKCS12),
179 PKCS12Serializable(*pkcs.get()),
180 PolicySerializable(keyPolicy),
181 PolicySerializable(certPolicy));
183 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
184 if (CKM_API_SUCCESS != retCode)
189 recv.Deserialize(command, counter, retCode);
191 if (counter != my_counter)
192 return CKM_API_ERROR_UNKNOWN;
198 int ManagerImpl::getPKCS12(const Alias &alias, PKCS12ShPtr &pkcs)
201 return CKM_API_ERROR_INPUT_PARAM;
203 int my_counter = ++m_counter;
205 return try_catch([&] {
207 AliasSupport helper(alias);
208 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_PKCS12),
213 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
214 if (CKM_API_SUCCESS != retCode)
219 PKCS12Serializable gotPkcs;
220 recv.Deserialize(command, counter, retCode, gotPkcs);
222 if (counter != my_counter)
223 return CKM_API_ERROR_UNKNOWN;
225 pkcs = std::make_shared<PKCS12Impl>(std::move(gotPkcs));
232 int ManagerImpl::removeAlias(const Alias &alias)
235 return CKM_API_ERROR_INPUT_PARAM;
237 int my_counter = ++m_counter;
239 return try_catch([&] {
241 AliasSupport helper(alias);
242 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::REMOVE),
247 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
248 if (CKM_API_SUCCESS != retCode)
253 recv.Deserialize(command, counter, retCode);
255 if (counter != my_counter)
256 return CKM_API_ERROR_UNKNOWN;
262 int ManagerImpl::getBinaryData(
264 DataType sendDataType,
265 const Password &password,
266 DataType &recvDataType,
270 return CKM_API_ERROR_INPUT_PARAM;
272 int my_counter = ++m_counter;
274 return try_catch([&] {
276 AliasSupport helper(alias);
277 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET),
279 static_cast<int>(sendDataType),
284 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
285 if (CKM_API_SUCCESS != retCode)
291 recv.Deserialize(command, counter, retCode, tmpDataType, rawData);
292 recvDataType = DataType(tmpDataType);
294 if (counter != my_counter)
295 return CKM_API_ERROR_UNKNOWN;
301 int ManagerImpl::getKey(const Alias &alias, const Password &password, KeyShPtr &key) {
302 DataType recvDataType;
305 int retCode = getBinaryData(
307 DataType::KEY_RSA_PUBLIC,
312 if (retCode != CKM_API_SUCCESS)
315 KeyShPtr keyParsed(new KeyImpl(rawData));
317 if (keyParsed->empty()) {
318 LogDebug("Key empty - failed to parse!");
319 return CKM_API_ERROR_BAD_RESPONSE;
324 return CKM_API_SUCCESS;
327 int ManagerImpl::getCertificate(const Alias &alias, const Password &password, CertificateShPtr &cert)
329 DataType recvDataType;
332 int retCode = getBinaryData(
334 DataType::CERTIFICATE,
339 if (retCode != CKM_API_SUCCESS)
342 if (recvDataType != DataType::CERTIFICATE)
343 return CKM_API_ERROR_BAD_RESPONSE;
345 CertificateShPtr certParsed(new CertificateImpl(rawData, DataFormat::FORM_DER));
347 if (certParsed->empty())
348 return CKM_API_ERROR_BAD_RESPONSE;
352 return CKM_API_SUCCESS;
355 int ManagerImpl::getData(const Alias &alias, const Password &password, RawBuffer &rawData)
357 DataType recvDataType = DataType::BINARY_DATA;
359 int retCode = getBinaryData(
361 DataType::BINARY_DATA,
366 if (retCode != CKM_API_SUCCESS)
369 if (recvDataType != DataType::BINARY_DATA)
370 return CKM_API_ERROR_BAD_RESPONSE;
372 return CKM_API_SUCCESS;
375 int ManagerImpl::getBinaryDataAliasVector(DataType dataType, AliasVector &aliasVector)
377 int my_counter = ++m_counter;
379 return try_catch([&] {
381 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
383 static_cast<int>(dataType));
385 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
386 if (CKM_API_SUCCESS != retCode)
392 LabelNameVector labelNameVector;
393 recv.Deserialize(command, counter, retCode, tmpDataType, labelNameVector);
394 if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != my_counter)) {
395 return CKM_API_ERROR_UNKNOWN;
398 for(const auto &it : labelNameVector)
399 aliasVector.push_back( AliasSupport::merge(it.first, it.second) );
405 int ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
406 // in fact datatype has no meaning here - if not certificate or binary data
407 // then manager decides to list all between DB_KEY_FIRST and DB_KEY_LAST
408 return getBinaryDataAliasVector(DataType::DB_KEY_LAST, aliasVector);
411 int ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
412 return getBinaryDataAliasVector(DataType::CERTIFICATE, aliasVector);
415 int ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
416 return getBinaryDataAliasVector(DataType::BINARY_DATA, aliasVector);
419 int ManagerImpl::createKeyPairRSA(
421 const Alias &privateKeyAlias,
422 const Alias &publicKeyAlias,
423 const Policy &policyPrivateKey,
424 const Policy &policyPublicKey)
426 return this->createKeyPair(CKM::KeyType::KEY_RSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
429 int ManagerImpl::createKeyPairDSA(
431 const Alias &privateKeyAlias,
432 const Alias &publicKeyAlias,
433 const Policy &policyPrivateKey,
434 const Policy &policyPublicKey)
436 return this->createKeyPair(CKM::KeyType::KEY_DSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
439 int ManagerImpl::createKeyPairECDSA(
441 const Alias &privateKeyAlias,
442 const Alias &publicKeyAlias,
443 const Policy &policyPrivateKey,
444 const Policy &policyPublicKey)
446 return this->createKeyPair(CKM::KeyType::KEY_ECDSA_PUBLIC, static_cast<int>(type), privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
449 int ManagerImpl::createKeyPair(
450 const KeyType key_type,
451 const int additional_param,
452 const Alias &privateKeyAlias,
453 const Alias &publicKeyAlias,
454 const Policy &policyPrivateKey,
455 const Policy &policyPublicKey)
458 LogicCommand cmd_type;
461 case KeyType::KEY_RSA_PUBLIC:
462 case KeyType::KEY_RSA_PRIVATE:
463 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
466 case KeyType::KEY_DSA_PUBLIC:
467 case KeyType::KEY_DSA_PRIVATE:
468 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
471 case KeyType::KEY_ECDSA_PUBLIC:
472 case KeyType::KEY_ECDSA_PRIVATE:
473 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
477 return CKM_API_ERROR_INPUT_PARAM;
480 // proceed with sending request
481 int my_counter = ++m_counter;
483 return try_catch([&] {
486 AliasSupport privateHelper(privateKeyAlias);
487 AliasSupport publicHelper(publicKeyAlias);
488 auto send = MessageBuffer::Serialize(static_cast<int>(cmd_type),
490 static_cast<int>(additional_param),
491 PolicySerializable(policyPrivateKey),
492 PolicySerializable(policyPublicKey),
493 privateHelper.getName(),
494 privateHelper.getLabel(),
495 publicHelper.getName(),
496 publicHelper.getLabel());
498 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
499 if (CKM_API_SUCCESS != retCode)
504 recv.Deserialize(command, counter, retCode);
505 if (counter != my_counter) {
506 return CKM_API_ERROR_UNKNOWN;
513 int ManagerImpl::getCertificateChain(
514 const CertificateShPtr &certificate,
515 const CertificateShPtrVector &untrustedCertificates,
516 const CertificateShPtrVector &trustedCertificates,
517 bool useTrustedSystemCertificates,
518 CertificateShPtrVector &certificateChainVector)
520 RawBufferVector untrustedVector;
521 RawBufferVector trustedVector;
523 for (auto &e: untrustedCertificates) {
524 untrustedVector.push_back(e->getDER());
526 for (auto &e: trustedCertificates) {
527 trustedVector.push_back(e->getDER());
532 LogicCommand::GET_CHAIN_CERT,
537 useTrustedSystemCertificates,
538 certificateChainVector);
541 int ManagerImpl::getCertificateChain(
542 const CertificateShPtr &certificate,
543 const AliasVector &untrustedCertificates,
544 const AliasVector &trustedCertificates,
545 bool useTrustedSystemCertificates,
546 CertificateShPtrVector &certificateChainVector)
548 LabelNameVector untrustedVector;
549 LabelNameVector trustedVector;
551 for (auto &e: untrustedCertificates) {
552 AliasSupport helper(e);
553 untrustedVector.push_back(std::make_pair(helper.getLabel(), helper.getName()));
555 for (auto &e: trustedCertificates) {
556 AliasSupport helper(e);
557 trustedVector.push_back(std::make_pair(helper.getLabel(), helper.getName()));
562 LogicCommand::GET_CHAIN_ALIAS,
567 useTrustedSystemCertificates,
568 certificateChainVector);
571 int ManagerImpl::createSignature(
572 const Alias &privateKeyAlias,
573 const Password &password, // password for private_key
574 const RawBuffer &message,
575 const HashAlgorithm hash,
576 const RSAPaddingAlgorithm padding,
577 RawBuffer &signature)
579 int my_counter = ++m_counter;
581 return try_catch([&] {
584 AliasSupport helper(privateKeyAlias);
585 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
591 static_cast<int>(hash),
592 static_cast<int>(padding));
594 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
595 if (CKM_API_SUCCESS != retCode)
600 recv.Deserialize(command, counter, retCode, signature);
602 if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
603 || (counter != my_counter))
605 return CKM_API_ERROR_UNKNOWN;
612 int ManagerImpl::verifySignature(
613 const Alias &publicKeyOrCertAlias,
614 const Password &password, // password for public_key (optional)
615 const RawBuffer &message,
616 const RawBuffer &signature,
617 const HashAlgorithm hash,
618 const RSAPaddingAlgorithm padding)
620 int my_counter = ++m_counter;
622 return try_catch([&] {
624 AliasSupport helper(publicKeyOrCertAlias);
625 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
632 static_cast<int>(hash),
633 static_cast<int>(padding));
635 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
636 if (CKM_API_SUCCESS != retCode)
641 recv.Deserialize(command, counter, retCode);
643 if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
644 || (counter != my_counter))
646 return CKM_API_ERROR_UNKNOWN;
653 int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspStatus)
655 return try_catch([&] {
656 int my_counter = ++m_counter;
659 RawBufferVector rawCertChain;
660 for (auto &e: certChain) {
661 rawCertChain.push_back(e->getDER());
664 auto send = MessageBuffer::Serialize(my_counter, rawCertChain);
666 int retCode = m_ocspConnection.processRequest(send.Pop(), recv);
667 if (CKM_API_SUCCESS != retCode)
671 recv.Deserialize(counter, retCode, ocspStatus);
673 if (my_counter != counter) {
674 return CKM_API_ERROR_UNKNOWN;
681 int ManagerImpl::setPermission(const Alias &alias,
682 const Label &accessor,
683 PermissionMask permissionMask)
685 int my_counter = ++m_counter;
687 return try_catch([&] {
689 AliasSupport helper(alias);
690 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SET_PERMISSION),
697 int retCode = m_storageConnection.processRequest(send.Pop(), recv);
698 if (CKM_API_SUCCESS != retCode)
703 recv.Deserialize(command, counter, retCode);
705 if (my_counter != counter) {
706 return CKM_API_ERROR_UNKNOWN;
713 ManagerShPtr Manager::create() {
715 return std::make_shared<ManagerImpl>();
716 } catch (const std::bad_alloc &) {
717 LogDebug("Bad alloc was caught during ManagerImpl creation.");
719 LogError("Critical error: Unknown exception was caught during ManagerImpl creation!");
721 return ManagerShPtr();