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>
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;
72 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE),
74 static_cast<int>(dataType),
77 PolicySerializable(policy));
79 int retCode = sendToServer(
80 SERVICE_SOCKET_CKM_STORAGE,
84 if (CKM_API_SUCCESS != retCode) {
91 recv.Deserialize(command, counter, retCode, opType);
93 if (counter != m_counter) {
94 return CKM_API_ERROR_UNKNOWN;
101 int ManagerImpl::saveKey(const Alias &alias, const KeyShPtr &key, const Policy &policy) {
102 if (key.get() == NULL)
103 return CKM_API_ERROR_INPUT_PARAM;
104 return saveBinaryData(alias, toDBDataType(key->getType()), key->getDER(), policy);
107 int ManagerImpl::saveCertificate(
109 const CertificateShPtr &cert,
110 const Policy &policy)
112 if (cert.get() == NULL)
113 return CKM_API_ERROR_INPUT_PARAM;
114 return saveBinaryData(alias, DBDataType::CERTIFICATE, cert->getDER(), policy);
117 int ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
118 if (!policy.extractable)
119 return CKM_API_ERROR_INPUT_PARAM;
120 return saveBinaryData(alias, DBDataType::BINARY_DATA, rawData, policy);
123 int ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
125 return try_catch([&] {
127 return CKM_API_ERROR_INPUT_PARAM;
130 AliasSupport helper(alias);
131 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::REMOVE),
133 static_cast<int>(dataType),
136 int retCode = sendToServer(
137 SERVICE_SOCKET_CKM_STORAGE,
141 if (CKM_API_SUCCESS != retCode) {
148 recv.Deserialize(command, counter, retCode, opType);
150 if (counter != m_counter) {
151 return CKM_API_ERROR_UNKNOWN;
158 int ManagerImpl::removeKey(const Alias &alias) {
159 return removeBinaryData(alias, DBDataType::KEY_RSA_PUBLIC);
162 int ManagerImpl::removeCertificate(const Alias &alias) {
163 return removeBinaryData(alias, DBDataType::CERTIFICATE);
166 int ManagerImpl::removeData(const Alias &alias) {
167 return removeBinaryData(alias, DBDataType::BINARY_DATA);
170 int ManagerImpl::getBinaryData(
172 DBDataType sendDataType,
173 const Password &password,
174 DBDataType &recvDataType,
177 return try_catch([&] {
179 return CKM_API_ERROR_INPUT_PARAM;
182 AliasSupport helper(alias);
183 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET),
185 static_cast<int>(sendDataType),
189 int retCode = sendToServer(
190 SERVICE_SOCKET_CKM_STORAGE,
194 if (CKM_API_SUCCESS != retCode) {
201 recv.Deserialize(command, counter, retCode, tmpDataType,rawData);
202 recvDataType = static_cast<DBDataType>(tmpDataType);
204 if (counter != m_counter) {
205 return CKM_API_ERROR_UNKNOWN;
212 int ManagerImpl::getKey(const Alias &alias, const Password &password, KeyShPtr &key) {
213 DBDataType recvDataType;
216 int retCode = getBinaryData(
218 DBDataType::KEY_RSA_PUBLIC,
223 if (retCode != CKM_API_SUCCESS)
226 KeyShPtr keyParsed(new KeyImpl(rawData));
228 if (keyParsed->empty()) {
229 LogDebug("Key empty - failed to parse!");
230 return CKM_API_ERROR_BAD_RESPONSE;
235 return CKM_API_SUCCESS;
238 int ManagerImpl::getCertificate(const Alias &alias, const Password &password, CertificateShPtr &cert)
240 DBDataType recvDataType;
243 int retCode = getBinaryData(
245 DBDataType::CERTIFICATE,
250 if (retCode != CKM_API_SUCCESS)
253 if (recvDataType != DBDataType::CERTIFICATE)
254 return CKM_API_ERROR_BAD_RESPONSE;
256 CertificateShPtr certParsed(new CertificateImpl(rawData, DataFormat::FORM_DER));
258 if (certParsed->empty())
259 return CKM_API_ERROR_BAD_RESPONSE;
263 return CKM_API_SUCCESS;
266 int ManagerImpl::getData(const Alias &alias, const Password &password, RawBuffer &rawData)
268 DBDataType recvDataType;
270 int retCode = getBinaryData(
272 DBDataType::BINARY_DATA,
277 if (retCode != CKM_API_SUCCESS)
280 if (recvDataType != DBDataType::BINARY_DATA)
281 return CKM_API_ERROR_BAD_RESPONSE;
283 return CKM_API_SUCCESS;
286 int ManagerImpl::getBinaryDataAliasVector(DBDataType dataType, AliasVector &aliasVector)
288 return try_catch([&] {
291 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
293 static_cast<int>(dataType));
294 int retCode = sendToServer(
295 SERVICE_SOCKET_CKM_STORAGE,
299 if (CKM_API_SUCCESS != retCode) {
306 LabelAliasVector labelAliasVector;
307 recv.Deserialize(command, counter, retCode, tmpDataType, labelAliasVector);
308 if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != m_counter)) {
309 return CKM_API_ERROR_UNKNOWN;
312 for(const auto it : labelAliasVector)
313 aliasVector.push_back( AliasSupport::merge(it.first, it.second) );
319 int ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
320 // in fact datatype has no meaning here - if not certificate or binary data
321 // then manager decides to list all between DB_KEY_FIRST and DB_KEY_LAST
322 return getBinaryDataAliasVector(DBDataType::DB_KEY_LAST, aliasVector);
325 int ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
326 return getBinaryDataAliasVector(DBDataType::CERTIFICATE, aliasVector);
329 int ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
330 return getBinaryDataAliasVector(DBDataType::BINARY_DATA, aliasVector);
333 int ManagerImpl::createKeyPairRSA(
335 const Alias &privateKeyAlias,
336 const Alias &publicKeyAlias,
337 const Policy &policyPrivateKey,
338 const Policy &policyPublicKey)
340 return this->createKeyPair(CKM::KeyType::KEY_RSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
343 int ManagerImpl::createKeyPairDSA(
345 const Alias &privateKeyAlias,
346 const Alias &publicKeyAlias,
347 const Policy &policyPrivateKey,
348 const Policy &policyPublicKey)
350 return this->createKeyPair(CKM::KeyType::KEY_DSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
353 int ManagerImpl::createKeyPairECDSA(
355 const Alias &privateKeyAlias,
356 const Alias &publicKeyAlias,
357 const Policy &policyPrivateKey,
358 const Policy &policyPublicKey)
360 return this->createKeyPair(CKM::KeyType::KEY_ECDSA_PUBLIC, static_cast<int>(type), privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
363 int ManagerImpl::createKeyPair(
364 const KeyType key_type,
365 const int additional_param,
366 const Alias &privateKeyAlias,
367 const Alias &publicKeyAlias,
368 const Policy &policyPrivateKey,
369 const Policy &policyPublicKey)
372 LogicCommand cmd_type;
375 case KeyType::KEY_RSA_PUBLIC:
376 case KeyType::KEY_RSA_PRIVATE:
377 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
380 case KeyType::KEY_DSA_PUBLIC:
381 case KeyType::KEY_DSA_PRIVATE:
382 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
385 case KeyType::KEY_ECDSA_PUBLIC:
386 case KeyType::KEY_ECDSA_PRIVATE:
387 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
391 return CKM_API_ERROR_INPUT_PARAM;
394 // proceed with sending request
396 int my_counter = m_counter;
397 return try_catch([&] {
400 auto send = MessageBuffer::Serialize(static_cast<int>(cmd_type),
402 static_cast<int>(additional_param),
403 PolicySerializable(policyPrivateKey),
404 PolicySerializable(policyPublicKey),
407 int retCode = sendToServer(
408 SERVICE_SOCKET_CKM_STORAGE,
412 if (CKM_API_SUCCESS != retCode) {
418 recv.Deserialize(command, counter, retCode);
419 if (counter != my_counter) {
420 return CKM_API_ERROR_UNKNOWN;
430 LogicCommand command,
432 const CertificateShPtr &certificate,
434 CertificateShPtrVector &certificateChainVector)
436 return try_catch([&] {
439 auto send = MessageBuffer::Serialize(static_cast<int>(command),
441 certificate->getDER(),
443 int retCode = sendToServer(
444 SERVICE_SOCKET_CKM_STORAGE,
448 if (CKM_API_SUCCESS != retCode) {
454 RawBufferVector rawBufferVector;
455 recv.Deserialize(retCommand, retCounter, retCode, rawBufferVector);
457 if ((counter != retCounter) || (static_cast<int>(command) != retCommand)) {
458 return CKM_API_ERROR_UNKNOWN;
461 if (retCode != CKM_API_SUCCESS) {
465 for (auto &e: rawBufferVector) {
466 CertificateShPtr cert(new CertificateImpl(e, DataFormat::FORM_DER));
468 return CKM_API_ERROR_BAD_RESPONSE;
469 certificateChainVector.push_back(cert);
477 int ManagerImpl::getCertificateChain(
478 const CertificateShPtr &certificate,
479 const CertificateShPtrVector &untrustedCertificates,
480 CertificateShPtrVector &certificateChainVector)
482 RawBufferVector rawBufferVector;
484 for (auto &e: untrustedCertificates) {
485 rawBufferVector.push_back(e->getDER());
489 LogicCommand::GET_CHAIN_CERT,
493 certificateChainVector);
496 int ManagerImpl::getCertificateChain(
497 const CertificateShPtr &certificate,
498 const AliasVector &untrustedCertificates,
499 CertificateShPtrVector &certificateChainVector)
502 LogicCommand::GET_CHAIN_ALIAS,
505 untrustedCertificates,
506 certificateChainVector);
509 int ManagerImpl::createSignature(
510 const Alias &privateKeyAlias,
511 const Password &password, // password for private_key
512 const RawBuffer &message,
513 const HashAlgorithm hash,
514 const RSAPaddingAlgorithm padding,
515 RawBuffer &signature)
518 int my_counter = m_counter;
519 return try_catch([&] {
522 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
527 static_cast<int>(hash),
528 static_cast<int>(padding));
529 int retCode = sendToServer(
530 SERVICE_SOCKET_CKM_STORAGE,
534 if (CKM_API_SUCCESS != retCode) {
541 recv.Deserialize(command, counter, retCode, signature);
543 if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
544 || (counter != my_counter))
546 return CKM_API_ERROR_UNKNOWN;
553 int ManagerImpl::verifySignature(
554 const Alias &publicKeyOrCertAlias,
555 const Password &password, // password for public_key (optional)
556 const RawBuffer &message,
557 const RawBuffer &signature,
558 const HashAlgorithm hash,
559 const RSAPaddingAlgorithm padding)
562 int my_counter = m_counter;
563 return try_catch([&] {
566 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
568 publicKeyOrCertAlias,
572 static_cast<int>(hash),
573 static_cast<int>(padding));
574 int retCode = sendToServer(
575 SERVICE_SOCKET_CKM_STORAGE,
579 if (CKM_API_SUCCESS != retCode) {
586 recv.Deserialize(command, counter, retCode);
588 if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
589 || (counter != my_counter))
591 return CKM_API_ERROR_UNKNOWN;
598 int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspStatus)
600 return try_catch([&] {
601 int my_counter = ++m_counter;
604 RawBufferVector rawCertChain;
605 for (auto &e: certChain) {
606 rawCertChain.push_back(e->getDER());
609 auto send = MessageBuffer::Serialize(my_counter, rawCertChain);
611 int retCode = sendToServer(
616 if (CKM_API_SUCCESS != retCode) {
622 recv.Deserialize(counter, retCode, ocspStatus);
624 if (my_counter != counter) {
625 return CKM_API_ERROR_UNKNOWN;
632 int ManagerImpl::allowAccess(const std::string &alias,
633 const std::string &accessor,
637 int my_counter = m_counter;
638 return try_catch([&] {
640 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::ALLOW_ACCESS),
644 static_cast<int>(granted));
645 int retCode = sendToServer(
646 SERVICE_SOCKET_CKM_STORAGE,
650 if (CKM_API_SUCCESS != retCode) {
656 recv.Deserialize(command, counter, retCode);
658 if (my_counter != counter) {
659 return CKM_API_ERROR_UNKNOWN;
666 int ManagerImpl::denyAccess(const std::string &alias, const std::string &accessor)
669 int my_counter = m_counter;
670 return try_catch([&] {
672 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::DENY_ACCESS),
676 int retCode = sendToServer(
677 SERVICE_SOCKET_CKM_STORAGE,
681 if (CKM_API_SUCCESS != retCode) {
687 recv.Deserialize(command, counter, retCode);
689 if (my_counter != counter) {
690 return CKM_API_ERROR_UNKNOWN;
697 ManagerShPtr Manager::create() {
699 return std::make_shared<ManagerImpl>();
700 } catch (const std::bad_alloc &) {
701 LogDebug("Bad alloc was caught during ManagerImpl creation.");
703 LogError("Critical error: Unknown exception was caught during ManagerImpl creation!");
705 return ManagerShPtr();