2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
22 #include <dpl/serialization.h>
23 #include <dpl/log/log.h>
24 #include <ckm/ckm-error.h>
25 #include <ckm/ckm-type.h>
26 #include <key-provider.h>
27 #include <file-system.h>
28 #include <CryptoService.h>
29 #include <ckm-logic.h>
33 const char * const CERT_SYSTEM_DIR = "/etc/ssl/certs";
34 } // anonymous namespace
40 int retCode = FileSystem::init();
41 // TODO what can I do when init went wrong? exit(-1) ??
43 LogError("Fatal error in FileSystem::init()");
46 if (CKM_API_SUCCESS != m_certStore.setSystemCertificateDir(CERT_SYSTEM_DIR)) {
47 LogError("Fatal error in CertificateStore::setSystemCertificateDir. Chain creation will not work");
50 cc_mode_status = CCModeState::CC_MODE_OFF;
53 CKMLogic::~CKMLogic(){}
55 RawBuffer CKMLogic::unlockUserKey(uid_t user, const Password &password) {
56 // TODO try catch for all errors that should be supported by error code
57 int retCode = CKM_API_SUCCESS;
60 if (0 == m_userDataMap.count(user) || !(m_userDataMap[user].keyProvider.isInitialized())) {
61 auto &handle = m_userDataMap[user];
63 auto wrappedDomainKEK = fs.getDKEK();
65 if (wrappedDomainKEK.empty()) {
66 wrappedDomainKEK = KeyProvider::generateDomainKEK(std::to_string(user), password);
67 fs.saveDKEK(wrappedDomainKEK);
70 handle.keyProvider = KeyProvider(wrappedDomainKEK, password);
72 auto wrappedDatabaseDEK = fs.getDBDEK();
74 if (wrappedDatabaseDEK.empty()) {
75 wrappedDatabaseDEK = handle.keyProvider.generateDEK(std::to_string(user));
76 fs.saveDBDEK(wrappedDatabaseDEK);
79 RawBuffer key = handle.keyProvider.getPureDEK(wrappedDatabaseDEK);
80 handle.database = DBCrypto(fs.getDBPath(), key);
81 handle.crypto = CryptoLogic();
83 // remove data of removed apps during locked state
84 AppLabelVector removedApps = fs.clearRemovedsApps();
85 for(auto& appSmackLabel : removedApps) {
86 handle.database.deleteKey(appSmackLabel);
91 } catch (const KeyProvider::Exception::PassWordError &e) {
92 LogError("Incorrect Password " << e.GetMessage());
93 retCode = CKM_API_ERROR_AUTHENTICATION_FAILED;
94 } catch (const KeyProvider::Exception::Base &e) {
95 LogError("Error in KeyProvider " << e.GetMessage());
96 retCode = CKM_API_ERROR_SERVER_ERROR;
97 } catch (const CryptoLogic::Exception::Base &e) {
98 LogError("CryptoLogic error: " << e.GetMessage());
99 retCode = CKM_API_ERROR_SERVER_ERROR;
100 } catch (const CKM::Exception &e) {
101 LogError("CKM::Exception: " << e.GetMessage());
102 retCode = CKM_API_ERROR_SERVER_ERROR;
105 if(retCode != CKM_API_SUCCESS) {
106 // When not successful, UserData in m_userDataMap should be erased.
107 // Because other operations make decision based on the existence of UserData in m_userDataMap.
108 m_userDataMap.erase(user);
111 return MessageBuffer::Serialize(retCode).Pop();
114 RawBuffer CKMLogic::setCCModeStatus(CCModeState mode_status) {
116 int retCode = CKM_API_SUCCESS;
117 int fipsModeStatus = 0;
120 if((mode_status != CCModeState:: CC_MODE_OFF) && (mode_status != CCModeState:: CC_MODE_ON)) {
121 retCode = CKM_API_ERROR_INPUT_PARAM;
124 cc_mode_status = mode_status;
125 fipsModeStatus = FIPS_mode();
127 if(cc_mode_status == CCModeState:: CC_MODE_ON) {
128 if(fipsModeStatus == 0) { // If FIPS mode off
129 rc = FIPS_mode_set(1); // Change FIPS_mode from off to on
131 LogError("Error in FIPS_mode_set function");
135 if(fipsModeStatus == 1) { // If FIPS mode on
136 rc = FIPS_mode_set(0); // Change FIPS_mode from on to off
138 LogError("Error in FIPS_mode_set function");
143 return MessageBuffer::Serialize(retCode).Pop();
146 RawBuffer CKMLogic::lockUserKey(uid_t user) {
147 int retCode = CKM_API_SUCCESS;
148 // TODO try catch for all errors that should be supported by error code
149 m_userDataMap.erase(user);
151 return MessageBuffer::Serialize(retCode).Pop();
155 RawBuffer CKMLogic::removeUserData(uid_t user) {
156 int retCode = CKM_API_SUCCESS;
157 // TODO try catch for all errors that should be supported by error code
158 m_userDataMap.erase(user);
163 return MessageBuffer::Serialize(retCode).Pop();
166 RawBuffer CKMLogic::changeUserPassword(
168 const Password &oldPassword,
169 const Password &newPassword)
171 int retCode = CKM_API_SUCCESS;
174 auto wrappedDomainKEK = fs.getDKEK();
175 if (wrappedDomainKEK.empty()) {
176 retCode = CKM_API_ERROR_BAD_REQUEST;
178 wrappedDomainKEK = KeyProvider::reencrypt(wrappedDomainKEK, oldPassword, newPassword);
179 fs.saveDKEK(wrappedDomainKEK);
181 } catch (const KeyProvider::Exception::PassWordError &e) {
182 LogError("Incorrect Password " << e.GetMessage());
183 retCode = CKM_API_ERROR_AUTHENTICATION_FAILED;
184 } catch (const KeyProvider::Exception::Base &e) {
185 LogError("Error in KeyProvider " << e.GetMessage());
186 retCode = CKM_API_ERROR_SERVER_ERROR;
187 } catch (const CKM::Exception &e) {
188 LogError("CKM::Exception: " << e.GetMessage());
189 retCode = CKM_API_ERROR_SERVER_ERROR;
192 return MessageBuffer::Serialize(retCode).Pop();
195 RawBuffer CKMLogic::resetUserPassword(
197 const Password &newPassword)
199 int retCode = CKM_API_SUCCESS;
201 if (0 == m_userDataMap.count(user)) {
202 retCode = CKM_API_ERROR_BAD_REQUEST;
204 auto &handler = m_userDataMap[user];
206 fs.saveDKEK(handler.keyProvider.getWrappedDomainKEK(newPassword));
209 return MessageBuffer::Serialize(retCode).Pop();
212 RawBuffer CKMLogic::removeApplicationData(const std::string &smackLabel) {
213 int retCode = CKM_API_SUCCESS;
217 if (smackLabel.empty()) {
218 retCode = CKM_API_ERROR_INPUT_PARAM;
220 UidVector uids = FileSystem::getUIDsFromDBFile();
221 for (auto userId : uids) {
222 if (0 == m_userDataMap.count(userId)) {
223 FileSystem fs(userId);
224 fs.addRemovedApp(smackLabel);
226 auto &handle = m_userDataMap[userId];
227 handle.database.deleteKey(smackLabel);
232 } catch (const DBCrypto::Exception::InternalError &e) {
233 LogError("DBCrypto couldn't remove data: " << e.GetMessage());
234 retCode = CKM_API_ERROR_DB_ERROR;
235 } catch (const DBCrypto::Exception::TransactionError &e) {
236 LogError("DBCrypto transaction failed with message " << e.GetMessage());
237 retCode = CKM_API_ERROR_DB_ERROR;
240 return MessageBuffer::Serialize(retCode).Pop();
243 int CKMLogic::saveDataHelper(
247 const RawBuffer &key,
248 const PolicySerializable &policy)
250 if (0 == m_userDataMap.count(cred.uid))
251 return CKM_API_ERROR_DB_LOCKED;
253 DBRow row = { alias, cred.smackLabel,
254 policy.extractable, dataType, DBCMAlgType::NONE,
255 0, RawBuffer(), static_cast<int>(key.size()), key, RawBuffer() };
257 auto &handler = m_userDataMap[cred.uid];
258 DBCrypto::Transaction transaction(&handler.database);
259 if (!handler.crypto.haveKey(cred.smackLabel)) {
261 auto key_optional = handler.database.getKey(cred.smackLabel);
263 LogDebug("No Key in database found. Generating new one for label: "
265 key = handler.keyProvider.generateDEK(cred.smackLabel);
266 handler.database.saveKey(cred.smackLabel, key);
268 LogDebug("Key from DB");
272 key = handler.keyProvider.getPureDEK(key);
273 handler.crypto.pushKey(cred.smackLabel, key);
276 // Do not encrypt data with password during cc_mode on
277 if(cc_mode_status == CCModeState::CC_MODE_ON) {
278 handler.crypto.encryptRow("", row);
280 handler.crypto.encryptRow(policy.password, row);
283 handler.database.saveDBRow(row);
284 transaction.commit();
285 return CKM_API_SUCCESS;
288 RawBuffer CKMLogic::saveData(
293 const RawBuffer &key,
294 const PolicySerializable &policy)
296 int retCode = CKM_API_SUCCESS;
298 retCode = saveDataHelper(cred, dataType, alias, key, policy);
299 LogDebug("SaveDataHelper returned: " << retCode);
300 } catch (const KeyProvider::Exception::Base &e) {
301 LogError("KeyProvider failed with message: " << e.GetMessage());
302 retCode = CKM_API_ERROR_SERVER_ERROR;
303 } catch (const CryptoLogic::Exception::Base &e) {
304 LogError("CryptoLogic failed with message: " << e.GetMessage());
305 retCode = CKM_API_ERROR_SERVER_ERROR;
306 } catch (const DBCrypto::Exception::InternalError &e) {
307 LogError("DBCrypto failed with message: " << e.GetMessage());
308 retCode = CKM_API_ERROR_DB_ERROR;
309 } catch (const DBCrypto::Exception::AliasExists &e) {
310 LogError("DBCrypto couldn't save duplicate alias");
311 retCode = CKM_API_ERROR_DB_ALIAS_EXISTS;
312 } catch (const DBCrypto::Exception::TransactionError &e) {
313 LogError("DBCrypto transaction failed with message " << e.GetMessage());
314 retCode = CKM_API_ERROR_DB_ERROR;
317 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE),
320 static_cast<int>(dataType));
321 return response.Pop();
324 RawBuffer CKMLogic::removeData(
330 int retCode = CKM_API_SUCCESS;
332 if (0 < m_userDataMap.count(cred.uid)) {
334 auto erased = m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
335 // check if the data existed or not
337 LogError("No row for given alias and label");
338 retCode = CKM_API_ERROR_DB_ALIAS_UNKNOWN;
340 } Catch (DBCrypto::Exception::PermissionDenied) {
341 LogError("Error: not enough permissions!");
342 retCode = CKM_API_ERROR_ACCESS_DENIED;
343 } Catch (CKM::Exception) {
344 LogError("Error in deleting row!");
345 retCode = CKM_API_ERROR_DB_ERROR;
348 retCode = CKM_API_ERROR_DB_LOCKED;
351 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::REMOVE),
354 static_cast<int>(dataType));
355 return response.Pop();
358 int CKMLogic::getDataHelper(
362 const Password &password,
365 if (0 == m_userDataMap.count(cred.uid))
366 return CKM_API_ERROR_DB_LOCKED;
368 auto &handler = m_userDataMap[cred.uid];
370 DBCrypto::DBRowOptional row_optional;
371 if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
372 row_optional = handler.database.getDBRow(alias, cred.smackLabel, dataType);
373 } else if ((static_cast<int>(dataType) >= static_cast<int>(DBDataType::DB_KEY_FIRST))
374 && (static_cast<int>(dataType) <= static_cast<int>(DBDataType::DB_KEY_LAST)))
376 row_optional = handler.database.getKeyDBRow(alias, cred.smackLabel);
378 LogError("Unknown type of requested data" << (int)dataType);
379 return CKM_API_ERROR_BAD_REQUEST;
382 LogError("No row for given alias, label and type");
383 return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
388 if (!handler.crypto.haveKey(row.smackLabel)) {
390 auto key_optional = handler.database.getKey(row.smackLabel);
392 LogError("No key for given label in database");
393 return CKM_API_ERROR_DB_ERROR;
396 key = handler.keyProvider.getPureDEK(key);
397 handler.crypto.pushKey(cred.smackLabel, key);
399 handler.crypto.decryptRow(password, row);
401 return CKM_API_SUCCESS;
404 RawBuffer CKMLogic::getData(
409 const Password &password)
411 int retCode = CKM_API_SUCCESS;
415 retCode = getDataHelper(cred, dataType, alias, password, row);
416 } catch (const KeyProvider::Exception::Base &e) {
417 LogError("KeyProvider failed with error: " << e.GetMessage());
418 retCode = CKM_API_ERROR_SERVER_ERROR;
419 } catch (const CryptoLogic::Exception::Base &e) {
420 LogError("CryptoLogic failed with message: " << e.GetMessage());
421 retCode = CKM_API_ERROR_SERVER_ERROR;
422 } catch (const DBCrypto::Exception::PermissionDenied &e) {
423 LogError("DBCrypto failed with message: " << e.GetMessage());
424 retCode = CKM_API_ERROR_ACCESS_DENIED;
425 } catch (const DBCrypto::Exception::Base &e) {
426 LogError("DBCrypto failed with message: " << e.GetMessage());
427 retCode = CKM_API_ERROR_DB_ERROR;
430 if (CKM_API_SUCCESS != retCode) {
432 row.dataType = dataType;
435 if ((CKM_API_SUCCESS == retCode) && (row.exportable == 0)) {
437 retCode = CKM_API_ERROR_NOT_EXPORTABLE;
440 // Prevent extracting private keys during cc-mode on
441 if((cc_mode_status == CCModeState::CC_MODE_ON) && (row.dataType == DBDataType::KEY_RSA_PRIVATE || row.dataType == DBDataType::KEY_ECDSA_PRIVATE || row.dataType == DBDataType::KEY_DSA_PRIVATE)) {
443 retCode = CKM_API_ERROR_BAD_REQUEST;
446 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET),
449 static_cast<int>(row.dataType),
451 return response.Pop();
454 RawBuffer CKMLogic::getDataList(
459 int retCode = CKM_API_SUCCESS;
460 AliasVector aliasVector;
462 if (0 < m_userDataMap.count(cred.uid)) {
463 auto &handler = m_userDataMap[cred.uid];
465 if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
466 handler.database.getAliases(cred.smackLabel, dataType, aliasVector);
468 handler.database.getKeyAliases(cred.smackLabel, aliasVector);
470 } Catch (CKM::Exception) {
471 LogError("Failed to get aliases");
472 retCode = CKM_API_ERROR_DB_ERROR;
475 retCode = CKM_API_ERROR_DB_LOCKED;
478 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
481 static_cast<int>(dataType),
483 return response.Pop();
487 int CKMLogic::createKeyPairHelper(
489 const KeyType key_type,
490 const int additional_param,
491 const Alias &aliasPrivate,
492 const Alias &aliasPublic,
493 const PolicySerializable &policyPrivate,
494 const PolicySerializable &policyPublic)
496 if (0 >= m_userDataMap.count(cred.uid))
497 return CKM_API_ERROR_DB_LOCKED;
499 auto &handler = m_userDataMap[cred.uid];
504 case KeyType::KEY_RSA_PUBLIC:
505 case KeyType::KEY_RSA_PRIVATE:
506 retCode = CryptoService::createKeyPairRSA(additional_param, prv, pub);
509 case KeyType::KEY_DSA_PUBLIC:
510 case KeyType::KEY_DSA_PRIVATE:
511 retCode = CryptoService::createKeyPairDSA(additional_param, prv, pub);
514 case KeyType::KEY_ECDSA_PUBLIC:
515 case KeyType::KEY_ECDSA_PRIVATE:
516 retCode = CryptoService::createKeyPairECDSA(static_cast<ElipticCurve>(additional_param), prv, pub);
520 return CKM_API_ERROR_INPUT_PARAM;
523 if (CKM_CRYPTO_CREATEKEY_SUCCESS != retCode)
525 LogDebug("CryptoService error with code: " << retCode);
526 return CKM_API_ERROR_SERVER_ERROR; // TODO error code
529 DBCrypto::Transaction transaction(&handler.database);
530 retCode = saveDataHelper(cred,
531 toDBDataType(prv.getType()),
536 if (CKM_API_SUCCESS != retCode)
539 retCode = saveDataHelper(cred,
540 toDBDataType(pub.getType()),
545 if (CKM_API_SUCCESS != retCode)
548 transaction.commit();
553 RawBuffer CKMLogic::createKeyPair(
555 LogicCommand protocol_cmd,
557 const int additional_param,
558 const Alias &aliasPrivate,
559 const Alias &aliasPublic,
560 const PolicySerializable &policyPrivate,
561 const PolicySerializable &policyPublic)
563 int retCode = CKM_API_SUCCESS;
565 KeyType key_type = KeyType::KEY_NONE;
568 case LogicCommand::CREATE_KEY_PAIR_RSA:
569 key_type = KeyType::KEY_RSA_PUBLIC;
571 case LogicCommand::CREATE_KEY_PAIR_DSA:
572 key_type = KeyType::KEY_DSA_PUBLIC;
574 case LogicCommand::CREATE_KEY_PAIR_ECDSA:
575 key_type = KeyType::KEY_ECDSA_PUBLIC;
582 retCode = createKeyPairHelper(
591 } catch (DBCrypto::Exception::AliasExists &e) {
592 LogDebug("DBCrypto error: alias exists: " << e.GetMessage());
593 retCode = CKM_API_ERROR_DB_ALIAS_EXISTS;
594 } catch (DBCrypto::Exception::TransactionError &e) {
595 LogDebug("DBCrypto error: transaction error: " << e.GetMessage());
596 retCode = CKM_API_ERROR_DB_ERROR;
597 } catch (CKM::CryptoLogic::Exception::Base &e) {
598 LogDebug("CryptoLogic error: " << e.GetMessage());
599 retCode = CKM_API_ERROR_SERVER_ERROR;
600 } catch (DBCrypto::Exception::InternalError &e) {
601 LogDebug("DBCrypto internal error: " << e.GetMessage());
602 retCode = CKM_API_ERROR_DB_ERROR;
605 return MessageBuffer::Serialize(static_cast<int>(protocol_cmd), commandId, retCode).Pop();
608 RawBuffer CKMLogic::getCertificateChain(
611 const RawBuffer &certificate,
612 const RawBufferVector &untrustedRawCertVector)
616 CertificateImpl cert(certificate, DataFormat::FORM_DER);
617 CertificateImplVector untrustedCertVector;
618 CertificateImplVector chainVector;
619 RawBufferVector chainRawVector;
621 for (auto &e: untrustedRawCertVector)
622 untrustedCertVector.push_back(CertificateImpl(e, DataFormat::FORM_DER));
624 LogDebug("Cert is empty: " << cert.empty());
626 int retCode = m_certStore.verifyCertificate(cert, untrustedCertVector, chainVector);
628 if (retCode == CKM_API_SUCCESS) {
629 for (auto &e : chainVector)
630 chainRawVector.push_back(e.getDER());
633 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_CHAIN_CERT),
637 return response.Pop();
640 RawBuffer CKMLogic::getCertificateChain(
643 const RawBuffer &certificate,
644 const AliasVector &aliasVector)
646 int retCode = CKM_API_SUCCESS;
647 RawBufferVector chainRawVector;
649 CertificateImpl cert(certificate, DataFormat::FORM_DER);
650 CertificateImplVector untrustedCertVector;
651 CertificateImplVector chainVector;
655 retCode = CKM_API_ERROR_SERVER_ERROR;
659 for (auto &i: aliasVector) {
660 retCode = getDataHelper(cred, DBDataType::CERTIFICATE, i, Password(), row);
662 if (retCode != CKM_API_SUCCESS)
665 untrustedCertVector.push_back(CertificateImpl(row.data, DataFormat::FORM_DER));
668 retCode = m_certStore.verifyCertificate(cert, untrustedCertVector, chainVector);
670 if (retCode != CKM_API_SUCCESS)
673 for (auto &i: chainVector)
674 chainRawVector.push_back(i.getDER());
676 } catch (const CryptoLogic::Exception::Base &e) {
677 LogError("DBCyptorModule failed with message: " << e.GetMessage());
678 retCode = CKM_API_ERROR_SERVER_ERROR;
679 } catch (const DBCrypto::Exception::PermissionDenied &e) {
680 LogError("DBCrypto failed with message: " << e.GetMessage());
681 retCode = CKM_API_ERROR_ACCESS_DENIED;
682 } catch (const DBCrypto::Exception::Base &e) {
683 LogError("DBCrypto failed with message: " << e.GetMessage());
684 retCode = CKM_API_ERROR_DB_ERROR;
686 LogError("Unknown error.");
690 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_CHAIN_ALIAS),
694 return response.Pop();
697 RawBuffer CKMLogic::createSignature(
700 const Alias &privateKeyAlias,
701 const Password &password, // password for private_key
702 const RawBuffer &message,
703 const HashAlgorithm hash,
704 const RSAPaddingAlgorithm padding)
710 int retCode = CKM_API_SUCCESS;
714 retCode = getDataHelper(cred, DBDataType::DB_KEY_FIRST, privateKeyAlias, password, row);
715 if (CKM_API_SUCCESS != retCode) {
716 LogError("getDataHelper return error");
720 KeyImpl keyParsed(row.data, Password());
721 if (keyParsed.empty())
722 retCode = CKM_API_ERROR_SERVER_ERROR;
724 retCode = cs.createSignature(keyParsed, message, hash, padding, signature);
726 } catch (const KeyProvider::Exception::Base &e) {
727 LogError("KeyProvider failed with message: " << e.GetMessage());
728 retCode = CKM_API_ERROR_SERVER_ERROR;
729 } catch (const CryptoLogic::Exception::Base &e) {
730 LogError("CryptoLogic failed with message: " << e.GetMessage());
731 retCode = CKM_API_ERROR_SERVER_ERROR;
732 } catch (const DBCrypto::Exception::PermissionDenied &e) {
733 LogError("DBCrypto failed with message: " << e.GetMessage());
734 retCode = CKM_API_ERROR_ACCESS_DENIED;
735 } catch (const DBCrypto::Exception::Base &e) {
736 LogError("DBCrypto failed with message: " << e.GetMessage());
737 retCode = CKM_API_ERROR_DB_ERROR;
738 } catch (const CKM::Exception &e) {
739 LogError("Unknown CKM::Exception: " << e.GetMessage());
740 retCode = CKM_API_ERROR_SERVER_ERROR;
743 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
747 return response.Pop();
750 RawBuffer CKMLogic::verifySignature(
753 const Alias &publicKeyOrCertAlias,
754 const Password &password, // password for public_key (optional)
755 const RawBuffer &message,
756 const RawBuffer &signature,
757 const HashAlgorithm hash,
758 const RSAPaddingAlgorithm padding)
760 int retCode = CKM_API_ERROR_VERIFICATION_FAILED;
768 retCode = getDataHelper(cred, DBDataType::DB_KEY_FIRST, publicKeyOrCertAlias, password, row);
770 if (retCode == CKM_API_SUCCESS) {
771 key = KeyImpl(row.data);
772 } else if (retCode == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
773 retCode = getDataHelper(cred, DBDataType::CERTIFICATE, publicKeyOrCertAlias, password, row);
774 if (retCode != CKM_API_SUCCESS)
776 CertificateImpl cert(row.data, DataFormat::FORM_DER);
777 key = cert.getKeyImpl();
783 retCode = CKM_API_ERROR_SERVER_ERROR;
787 retCode = cs.verifySignature(key, message, signature, hash, padding);
789 } catch (const CryptoService::Exception::Crypto_internal &e) {
790 LogError("KeyProvider failed with message: " << e.GetMessage());
791 retCode = CKM_API_ERROR_SERVER_ERROR;
792 } catch (const CryptoService::Exception::opensslError &e) {
793 LogError("KeyProvider failed with message: " << e.GetMessage());
794 retCode = CKM_API_ERROR_SERVER_ERROR;
795 } catch (const KeyProvider::Exception::Base &e) {
796 LogError("KeyProvider failed with error: " << e.GetMessage());
797 retCode = CKM_API_ERROR_SERVER_ERROR;
798 } catch (const CryptoLogic::Exception::Base &e) {
799 LogError("CryptoLogic failed with message: " << e.GetMessage());
800 retCode = CKM_API_ERROR_SERVER_ERROR;
801 } catch (const DBCrypto::Exception::PermissionDenied &e) {
802 LogError("DBCrypto failed with message: " << e.GetMessage());
803 retCode = CKM_API_ERROR_ACCESS_DENIED;
804 } catch (const DBCrypto::Exception::Base &e) {
805 LogError("DBCrypto failed with message: " << e.GetMessage());
806 retCode = CKM_API_ERROR_DB_ERROR;
807 } catch (const CKM::Exception &e) {
808 LogError("Unknown CKM::Exception: " << e.GetMessage());
809 retCode = CKM_API_ERROR_SERVER_ERROR;
812 auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
815 return response.Pop();
818 RawBuffer CKMLogic::allowAccess(
822 const Alias &item_alias,
823 const std::string &accessor_label,
824 const AccessRight req_rights)
826 int retCode = CKM_API_ERROR_VERIFICATION_FAILED;
828 if (0 < m_userDataMap.count(cred.uid))
831 retCode = m_userDataMap[cred.uid].database.setAccessRights(cred.smackLabel, item_alias, accessor_label, req_rights);
832 } Catch (DBCrypto::Exception::InvalidArgs) {
833 LogError("Error: invalid args!");
834 retCode = CKM_API_ERROR_INPUT_PARAM;
835 } Catch (DBCrypto::Exception::PermissionDenied) {
836 LogError("Error: not enough permissions!");
837 retCode = CKM_API_ERROR_ACCESS_DENIED;
838 } Catch (CKM::Exception) {
839 LogError("Error in set row!");
840 retCode = CKM_API_ERROR_DB_ERROR;
843 retCode = CKM_API_ERROR_DB_LOCKED;
846 return MessageBuffer::Serialize(command, msgID, retCode).Pop();
849 RawBuffer CKMLogic::denyAccess(
853 const Alias &item_alias,
854 const std::string &accessor_label)
856 int retCode = CKM_API_ERROR_VERIFICATION_FAILED;
858 if (0 < m_userDataMap.count(cred.uid))
861 retCode = m_userDataMap[cred.uid].database.clearAccessRights(cred.smackLabel, item_alias, accessor_label);
862 } Catch (DBCrypto::Exception::PermissionDenied) {
863 LogError("Error: not enough permissions!");
864 retCode = CKM_API_ERROR_ACCESS_DENIED;
865 } Catch (DBCrypto::Exception::InvalidArgs) {
866 LogError("Error: permission not found!");
867 retCode = CKM_API_ERROR_INPUT_PARAM;
868 } Catch (CKM::Exception) {
869 LogError("Error in deleting row!");
870 retCode = CKM_API_ERROR_DB_ERROR;
873 retCode = CKM_API_ERROR_DB_LOCKED;
876 return MessageBuffer::Serialize(command, msgID, retCode).Pop();