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 LabelNameVector labelNameVector;
307 recv.Deserialize(command, counter, retCode, tmpDataType, labelNameVector);
308 if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != m_counter)) {
309 return CKM_API_ERROR_UNKNOWN;
312 for(const auto &it : labelNameVector)
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 AliasSupport helper(privateKeyAlias);
523 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
529 static_cast<int>(hash),
530 static_cast<int>(padding));
531 int retCode = sendToServer(
532 SERVICE_SOCKET_CKM_STORAGE,
536 if (CKM_API_SUCCESS != retCode) {
543 recv.Deserialize(command, counter, retCode, signature);
545 if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
546 || (counter != my_counter))
548 return CKM_API_ERROR_UNKNOWN;
555 int ManagerImpl::verifySignature(
556 const Alias &publicKeyOrCertAlias,
557 const Password &password, // password for public_key (optional)
558 const RawBuffer &message,
559 const RawBuffer &signature,
560 const HashAlgorithm hash,
561 const RSAPaddingAlgorithm padding)
564 int my_counter = m_counter;
565 return try_catch([&] {
568 AliasSupport helper(publicKeyOrCertAlias);
569 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
576 static_cast<int>(hash),
577 static_cast<int>(padding));
578 int retCode = sendToServer(
579 SERVICE_SOCKET_CKM_STORAGE,
583 if (CKM_API_SUCCESS != retCode) {
590 recv.Deserialize(command, counter, retCode);
592 if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
593 || (counter != my_counter))
595 return CKM_API_ERROR_UNKNOWN;
602 int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspStatus)
604 return try_catch([&] {
605 int my_counter = ++m_counter;
608 RawBufferVector rawCertChain;
609 for (auto &e: certChain) {
610 rawCertChain.push_back(e->getDER());
613 auto send = MessageBuffer::Serialize(my_counter, rawCertChain);
615 int retCode = sendToServer(
620 if (CKM_API_SUCCESS != retCode) {
626 recv.Deserialize(counter, retCode, ocspStatus);
628 if (my_counter != counter) {
629 return CKM_API_ERROR_UNKNOWN;
636 int ManagerImpl::allowAccess(const Alias &alias,
637 const Label &accessor,
641 int my_counter = m_counter;
642 return try_catch([&] {
644 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::ALLOW_ACCESS),
648 static_cast<int>(granted));
649 int retCode = sendToServer(
650 SERVICE_SOCKET_CKM_STORAGE,
654 if (CKM_API_SUCCESS != retCode) {
660 recv.Deserialize(command, counter, retCode);
662 if (my_counter != counter) {
663 return CKM_API_ERROR_UNKNOWN;
670 int ManagerImpl::denyAccess(const Alias &alias, const Label &accessor)
673 int my_counter = m_counter;
674 return try_catch([&] {
676 auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::DENY_ACCESS),
680 int retCode = sendToServer(
681 SERVICE_SOCKET_CKM_STORAGE,
685 if (CKM_API_SUCCESS != retCode) {
691 recv.Deserialize(command, counter, retCode);
693 if (my_counter != counter) {
694 return CKM_API_ERROR_UNKNOWN;
701 ManagerShPtr Manager::create() {
703 return std::make_shared<ManagerImpl>();
704 } catch (const std::bad_alloc &) {
705 LogDebug("Bad alloc was caught during ManagerImpl creation.");
707 LogError("Critical error: Unknown exception was caught during ManagerImpl creation!");
709 return ManagerShPtr();