CC mode logic updated
[platform/core/security/key-manager.git] / src / manager / service / ckm-logic.cpp
index 587d9bb..7744066 100644 (file)
@@ -19,6 +19,7 @@
  * @version     1.0
  * @brief       Sample service implementation.
  */
+#include <vconf/vconf.h>
 #include <dpl/serialization.h>
 #include <dpl/log/log.h>
 #include <ckm/ckm-error.h>
 #include <file-system.h>
 #include <CryptoService.h>
 #include <ckm-logic.h>
-#include <generic-key.h>
+#include <key-impl.h>
+
+#ifndef VCONFKEY_SECURITY_MDPP_STATE
+#define VCONFKEY_SECURITY_MDPP_STATE = "file/security_mdpp/security_mdpp_state";
+#endif
 
 namespace {
 const char * const CERT_SYSTEM_DIR = "/etc/ssl/certs";
+
+const char* const MDPP_MODE_ENFORCING = "Enforcing";
+const char* const MDPP_MODE_ENABLED = "Enabled";
+
 } // anonymous namespace
 
 namespace CKM {
 
-CKMLogic::CKMLogic()
+CKMLogic::CKMLogic() : m_ccMode(false)
 {
     int retCode = FileSystem::init();
     // TODO what can I do when init went wrong? exit(-1) ??
@@ -46,6 +55,8 @@ CKMLogic::CKMLogic()
     if (CKM_API_SUCCESS != m_certStore.setSystemCertificateDir(CERT_SYSTEM_DIR)) {
         LogError("Fatal error in CertificateStore::setSystemCertificateDir. Chain creation will not work");
     }
+
+    updateCCMode_internal();
 }
 
 CKMLogic::~CKMLogic(){}
@@ -77,6 +88,13 @@ RawBuffer CKMLogic::unlockUserKey(uid_t user, const Password &password) {
             RawBuffer key = handle.keyProvider.getPureDEK(wrappedDatabaseDEK);
             handle.database = DBCrypto(fs.getDBPath(), key);
             handle.crypto = CryptoLogic();
+
+            // remove data of removed apps during locked state
+            AppLabelVector removedApps = fs.clearRemovedsApps();
+            for(auto& appSmackLabel : removedApps) {
+                handle.database.deleteKey(appSmackLabel);
+            }
+
             // TODO wipe key
         }
     } catch (const KeyProvider::Exception::PassWordError &e) {
@@ -99,9 +117,44 @@ RawBuffer CKMLogic::unlockUserKey(uid_t user, const Password &password) {
         m_userDataMap.erase(user);
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, retCode);
-    return response.Pop();
+    return MessageBuffer::Serialize(retCode).Pop();
+}
+
+void CKMLogic::updateCCMode_internal() {
+    int fipsModeStatus = 0;
+    int rc = 0;
+    bool newMode;
+
+    char *mdppState = vconf_get_str(VCONFKEY_SECURITY_MDPP_STATE);
+    newMode = ( mdppState && (!strcmp(mdppState, MDPP_MODE_ENABLED) ||
+                              !strcmp(mdppState, MDPP_MODE_ENFORCING)) );
+    if (newMode == m_ccMode)
+        return;
+
+    m_ccMode = newMode;
+
+    fipsModeStatus = FIPS_mode();
+
+    if(m_ccMode) {
+        if(fipsModeStatus == 0) { // If FIPS mode off
+            rc = FIPS_mode_set(1); // Change FIPS_mode from off to on
+            if(rc == 0) {
+                LogError("Error in FIPS_mode_set function");
+            }
+        }
+    } else {
+        if(fipsModeStatus == 1) { // If FIPS mode on
+            rc = FIPS_mode_set(0); // Change FIPS_mode from on to off
+            if(rc == 0) {
+                LogError("Error in FIPS_mode_set function");
+            }
+        }
+    }
+}
+
+RawBuffer CKMLogic::updateCCMode() {
+    updateCCMode_internal();
+    return MessageBuffer::Serialize(CKM_API_SUCCESS).Pop();
 }
 
 RawBuffer CKMLogic::lockUserKey(uid_t user) {
@@ -109,9 +162,8 @@ RawBuffer CKMLogic::lockUserKey(uid_t user) {
     // TODO try catch for all errors that should be supported by error code
     m_userDataMap.erase(user);
 
-    MessageBuffer response;
-    Serialization::Serialize(response, retCode);
-    return response.Pop();
+    return MessageBuffer::Serialize(retCode).Pop();
+
 }
 
 RawBuffer CKMLogic::removeUserData(uid_t user) {
@@ -122,9 +174,7 @@ RawBuffer CKMLogic::removeUserData(uid_t user) {
     FileSystem fs(user);
     fs.removeUserData();
 
-    MessageBuffer response;
-    Serialization::Serialize(response, retCode);
-    return response.Pop();
+    return MessageBuffer::Serialize(retCode).Pop();
 }
 
 RawBuffer CKMLogic::changeUserPassword(
@@ -153,9 +203,7 @@ RawBuffer CKMLogic::changeUserPassword(
         retCode = CKM_API_ERROR_SERVER_ERROR;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, retCode);
-    return response.Pop();
+    return MessageBuffer::Serialize(retCode).Pop();
 }
 
 RawBuffer CKMLogic::resetUserPassword(
@@ -172,9 +220,7 @@ RawBuffer CKMLogic::resetUserPassword(
         fs.saveDKEK(handler.keyProvider.getWrappedDomainKEK(newPassword));
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, retCode);
-    return response.Pop();
+    return MessageBuffer::Serialize(retCode).Pop();
 }
 
 RawBuffer CKMLogic::removeApplicationData(const std::string &smackLabel) {
@@ -185,8 +231,15 @@ RawBuffer CKMLogic::removeApplicationData(const std::string &smackLabel) {
         if (smackLabel.empty()) {
             retCode = CKM_API_ERROR_INPUT_PARAM;
         } else {
-            for(auto &handler: m_userDataMap) {
-                handler.second.database.deleteKey(smackLabel);
+            UidVector uids = FileSystem::getUIDsFromDBFile();
+            for (auto userId : uids) {
+                if (0 == m_userDataMap.count(userId)) {
+                    FileSystem fs(userId);
+                    fs.addRemovedApp(smackLabel);
+                } else {
+                    auto &handle = m_userDataMap[userId];
+                    handle.database.deleteKey(smackLabel);
+                }
             }
         }
 
@@ -198,9 +251,7 @@ RawBuffer CKMLogic::removeApplicationData(const std::string &smackLabel) {
         retCode = CKM_API_ERROR_DB_ERROR;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, retCode);
-    return response.Pop();
+    return MessageBuffer::Serialize(retCode).Pop();
 }
 
 int CKMLogic::saveDataHelper(
@@ -213,7 +264,8 @@ int CKMLogic::saveDataHelper(
     if (0 == m_userDataMap.count(cred.uid))
         return CKM_API_ERROR_DB_LOCKED;
 
-    DBRow row = { alias, cred.smackLabel, policy.restricted,
+    // proceed to data save
+    DBRow row = { alias, cred.smackLabel,
          policy.extractable, dataType, DBCMAlgType::NONE,
          0, RawBuffer(), static_cast<int>(key.size()), key, RawBuffer() };
 
@@ -235,12 +287,52 @@ int CKMLogic::saveDataHelper(
         key = handler.keyProvider.getPureDEK(key);
         handler.crypto.pushKey(cred.smackLabel, key);
     }
-    handler.crypto.encryptRow(policy.password, row);
+
+    // Do not encrypt data with password during cc_mode on
+    if(m_ccMode) {
+        handler.crypto.encryptRow("", row);
+    } else {
+        handler.crypto.encryptRow(policy.password, row);
+    }
+
     handler.database.saveDBRow(row);
     transaction.commit();
     return CKM_API_SUCCESS;
 }
 
+void CKMLogic::verifyBinaryData(DBDataType dataType, const RawBuffer &input_data) const
+{
+    // verify the data integrity
+    switch(dataType)
+    {
+        case DBDataType::KEY_RSA_PUBLIC:
+        case DBDataType::KEY_RSA_PRIVATE:
+        case DBDataType::KEY_ECDSA_PUBLIC:
+        case DBDataType::KEY_ECDSA_PRIVATE:
+        case DBDataType::KEY_DSA_PUBLIC:
+        case DBDataType::KEY_DSA_PRIVATE:
+        case DBDataType::KEY_AES:
+        {
+            KeyShPtr output_key = CKM::Key::create(input_data);
+            if(output_key.get() == NULL)
+                ThrowMsg(CKMLogic::Exception::InputDataInvalid, "provided binary data is not valid key data");
+            break;
+        }
+
+        case DBDataType::CERTIFICATE:
+        {
+            CertificateShPtr cert = CKM::Certificate::create(input_data, DataFormat::FORM_DER);
+            if(cert.get() == NULL)
+                ThrowMsg(CKMLogic::Exception::InputDataInvalid, "provided binary data is not valid certificate data");
+            break;
+        }
+
+        // TODO: add here BINARY_DATA verification, i.e: max size etc.
+
+        default: break;
+    }
+}
+
 RawBuffer CKMLogic::saveData(
     Credentials &cred,
     int commandId,
@@ -251,8 +343,13 @@ RawBuffer CKMLogic::saveData(
 {
     int retCode = CKM_API_SUCCESS;
     try {
+        verifyBinaryData(dataType, key);
+
         retCode = saveDataHelper(cred, dataType, alias, key, policy);
         LogDebug("SaveDataHelper returned: " << retCode);
+    } catch (const CKMLogic::Exception::InputDataInvalid &e) {
+        LogError("Provided data invalid: " << e.GetMessage());
+        retCode = CKM_API_ERROR_INPUT_PARAM;
     } catch (const KeyProvider::Exception::Base &e) {
         LogError("KeyProvider failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
@@ -270,12 +367,10 @@ RawBuffer CKMLogic::saveData(
         retCode = CKM_API_ERROR_DB_ERROR;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::SAVE));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, static_cast<int>(dataType));
-
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE),
+                                             commandId,
+                                             retCode,
+                                             static_cast<int>(dataType));
     return response.Pop();
 }
 
@@ -295,6 +390,9 @@ RawBuffer CKMLogic::removeData(
                 LogError("No row for given alias and label");
                 retCode = CKM_API_ERROR_DB_ALIAS_UNKNOWN;
             }
+        } Catch (DBCrypto::Exception::PermissionDenied) {
+            LogError("Error: not enough permissions!");
+            retCode = CKM_API_ERROR_ACCESS_DENIED;
         } Catch (CKM::Exception) {
             LogError("Error in deleting row!");
             retCode = CKM_API_ERROR_DB_ERROR;
@@ -303,12 +401,10 @@ RawBuffer CKMLogic::removeData(
         retCode = CKM_API_ERROR_DB_LOCKED;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::REMOVE));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, static_cast<int>(dataType));
-
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::REMOVE),
+                                             commandId,
+                                             retCode,
+                                             static_cast<int>(dataType));
     return response.Pop();
 }
 
@@ -319,7 +415,6 @@ int CKMLogic::getDataHelper(
     const Password &password,
     DBRow &row)
 {
-
     if (0 == m_userDataMap.count(cred.uid))
         return CKM_API_ERROR_DB_LOCKED;
 
@@ -377,6 +472,9 @@ RawBuffer CKMLogic::getData(
     } catch (const CryptoLogic::Exception::Base &e) {
         LogError("CryptoLogic failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
+    } catch (const DBCrypto::Exception::PermissionDenied &e) {
+        LogError("DBCrypto failed with message: " << e.GetMessage());
+        retCode = CKM_API_ERROR_ACCESS_DENIED;
     } catch (const DBCrypto::Exception::Base &e) {
         LogError("DBCrypto failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_DB_ERROR;
@@ -392,12 +490,20 @@ RawBuffer CKMLogic::getData(
         retCode = CKM_API_ERROR_NOT_EXPORTABLE;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::GET));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, static_cast<int>(row.dataType));
-    Serialization::Serialize(response, row.data);
+    // Prevent extracting private keys during cc-mode on
+    if((m_ccMode) && (row.dataType == DBDataType::KEY_RSA_PRIVATE ||
+                      row.dataType == DBDataType::KEY_ECDSA_PRIVATE ||
+                      row.dataType == DBDataType::KEY_DSA_PRIVATE))
+    {
+        row.data.clear();
+        retCode = CKM_API_ERROR_BAD_REQUEST;
+    }
+
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET),
+                                             commandId,
+                                             retCode,
+                                             static_cast<int>(row.dataType),
+                                             row.data);
     return response.Pop();
 }
 
@@ -413,7 +519,7 @@ RawBuffer CKMLogic::getDataList(
         auto &handler = m_userDataMap[cred.uid];
         Try {
             if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
-                handler.database.getAliases(dataType, cred.smackLabel, aliasVector);
+                handler.database.getAliases(cred.smackLabel, dataType, aliasVector);
             } else {
                 handler.database.getKeyAliases(cred.smackLabel, aliasVector);
             }
@@ -425,18 +531,19 @@ RawBuffer CKMLogic::getDataList(
         retCode = CKM_API_ERROR_DB_LOCKED;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::GET_LIST));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, static_cast<int>(dataType));
-    Serialization::Serialize(response, aliasVector);
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
+                                             commandId,
+                                             retCode,
+                                             static_cast<int>(dataType),
+                                             aliasVector);
     return response.Pop();
 }
 
-int CKMLogic::createKeyPairRSAHelper(
+
+int CKMLogic::createKeyPairHelper(
     Credentials &cred,
-    int size,
+    const KeyType key_type,
+    const int additional_param,
     const Alias &aliasPrivate,
     const Alias &aliasPublic,
     const PolicySerializable &policyPrivate,
@@ -446,11 +553,30 @@ int CKMLogic::createKeyPairRSAHelper(
         return CKM_API_ERROR_DB_LOCKED;
 
     auto &handler = m_userDataMap[cred.uid];
-    GenericKey prv, pub;
+    KeyImpl prv, pub;
     int retCode;
+    switch(key_type)
+    {
+        case KeyType::KEY_RSA_PUBLIC:
+        case KeyType::KEY_RSA_PRIVATE:
+            retCode = CryptoService::createKeyPairRSA(additional_param, prv, pub);
+            break;
+
+        case KeyType::KEY_DSA_PUBLIC:
+        case KeyType::KEY_DSA_PRIVATE:
+            retCode = CryptoService::createKeyPairDSA(additional_param, prv, pub);
+            break;
+
+        case KeyType::KEY_ECDSA_PUBLIC:
+        case KeyType::KEY_ECDSA_PRIVATE:
+            retCode = CryptoService::createKeyPairECDSA(static_cast<ElipticCurve>(additional_param), prv, pub);
+            break;
+
+        default:
+            return CKM_API_ERROR_INPUT_PARAM;
+    }
 
-    if (CKM_CRYPTO_CREATEKEY_SUCCESS !=
-        (retCode = CryptoService::createKeyPairRSA(size, prv, pub)))
+    if (CKM_CRYPTO_CREATEKEY_SUCCESS != retCode)
     {
         LogDebug("CryptoService error with code: " << retCode);
         return CKM_API_ERROR_SERVER_ERROR; // TODO error code
@@ -480,10 +606,11 @@ int CKMLogic::createKeyPairRSAHelper(
     return retCode;
 }
 
-RawBuffer CKMLogic::createKeyPairRSA(
+RawBuffer CKMLogic::createKeyPair(
     Credentials &cred,
+    LogicCommand protocol_cmd,
     int commandId,
-    int size,
+    const int additional_param,
     const Alias &aliasPrivate,
     const Alias &aliasPublic,
     const PolicySerializable &policyPrivate,
@@ -491,10 +618,27 @@ RawBuffer CKMLogic::createKeyPairRSA(
 {
     int retCode = CKM_API_SUCCESS;
 
+    KeyType key_type = KeyType::KEY_NONE;
+    switch(protocol_cmd)
+    {
+        case LogicCommand::CREATE_KEY_PAIR_RSA:
+            key_type = KeyType::KEY_RSA_PUBLIC;
+            break;
+        case LogicCommand::CREATE_KEY_PAIR_DSA:
+            key_type = KeyType::KEY_DSA_PUBLIC;
+            break;
+        case LogicCommand::CREATE_KEY_PAIR_ECDSA:
+            key_type = KeyType::KEY_ECDSA_PUBLIC;
+            break;
+        default:
+            break;
+    }
+
     try {
-        retCode = createKeyPairRSAHelper(
+        retCode = createKeyPairHelper(
                         cred,
-                        size,
+                        key_type,
+                        additional_param,
                         aliasPrivate,
                         aliasPublic,
                         policyPrivate,
@@ -514,100 +658,7 @@ RawBuffer CKMLogic::createKeyPairRSA(
         retCode = CKM_API_ERROR_DB_ERROR;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-
-    return response.Pop();
-}
-
-int CKMLogic::createKeyPairECDSAHelper(
-    Credentials &cred,
-    int type,
-    const Alias &aliasPrivate,
-    const Alias &aliasPublic,
-    const PolicySerializable &policyPrivate,
-    const PolicySerializable &policyPublic)
-{
-    if (0 >= m_userDataMap.count(cred.uid))
-        return CKM_API_ERROR_DB_LOCKED;
-
-    auto &handler = m_userDataMap[cred.uid];
-    GenericKey prv, pub;
-    int retCode;
-
-    if (CKM_CRYPTO_CREATEKEY_SUCCESS !=
-        (retCode = CryptoService::createKeyPairECDSA(static_cast<ElipticCurve>(type), prv, pub)))
-    {
-        LogError("CryptoService failed with code: " << retCode);
-        return CKM_API_ERROR_SERVER_ERROR; // TODO error code
-    }
-
-    DBCrypto::Transaction transaction(&handler.database);
-
-    retCode = saveDataHelper(cred,
-                            toDBDataType(prv.getType()),
-                            aliasPrivate,
-                            prv.getDER(),
-                            policyPrivate);
-
-    if (CKM_API_SUCCESS != retCode)
-        return retCode;
-
-    retCode = saveDataHelper(cred,
-                            toDBDataType(pub.getType()),
-                            aliasPublic,
-                            pub.getDER(),
-                            policyPublic);
-
-    if (CKM_API_SUCCESS != retCode)
-        return retCode;
-
-    transaction.commit();
-
-    return retCode;
-}
-
-RawBuffer CKMLogic::createKeyPairECDSA(
-    Credentials &cred,
-    int commandId,
-    int type,
-    const Alias &aliasPrivate,
-    const Alias &aliasPublic,
-    const PolicySerializable &policyPrivate,
-    const PolicySerializable &policyPublic)
-{
-    int retCode = CKM_API_SUCCESS;
-
-    try {
-        retCode = createKeyPairECDSAHelper(
-                        cred,
-                        type,
-                        aliasPrivate,
-                        aliasPublic,
-                        policyPrivate,
-                        policyPublic);
-    } catch (const DBCrypto::Exception::AliasExists &e) {
-        LogDebug("DBCrypto error: alias exists: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ALIAS_EXISTS;
-    } catch (const DBCrypto::Exception::TransactionError &e) {
-        LogDebug("DBCrypto error: transaction error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    } catch (const CKM::CryptoLogic::Exception::Base &e) {
-        LogDebug("CryptoLogic error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_SERVER_ERROR;
-    } catch (const DBCrypto::Exception::InternalError &e) {
-        LogDebug("DBCrypto internal error: " << e.GetMessage());
-        retCode = CKM_API_ERROR_DB_ERROR;
-    }
-
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-
-    return response.Pop();
+    return MessageBuffer::Serialize(static_cast<int>(protocol_cmd), commandId, retCode).Pop();
 }
 
 RawBuffer CKMLogic::getCertificateChain(
@@ -635,11 +686,10 @@ RawBuffer CKMLogic::getCertificateChain(
             chainRawVector.push_back(e.getDER());
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::GET_CHAIN_CERT));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, chainRawVector);
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_CHAIN_CERT),
+                                             commandId,
+                                             retCode,
+                                             chainRawVector);
     return response.Pop();
 }
 
@@ -682,6 +732,9 @@ RawBuffer CKMLogic::getCertificateChain(
     } catch (const CryptoLogic::Exception::Base &e) {
         LogError("DBCyptorModule failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
+    } catch (const DBCrypto::Exception::PermissionDenied &e) {
+        LogError("DBCrypto failed with message: " << e.GetMessage());
+        retCode = CKM_API_ERROR_ACCESS_DENIED;
     } catch (const DBCrypto::Exception::Base &e) {
         LogError("DBCrypto failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_DB_ERROR;
@@ -690,11 +743,10 @@ RawBuffer CKMLogic::getCertificateChain(
     }
 
 senderror:
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::GET_CHAIN_ALIAS));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, chainRawVector);
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_CHAIN_ALIAS),
+                                             commandId,
+                                             retCode,
+                                             chainRawVector);
     return response.Pop();
 }
 
@@ -715,17 +767,17 @@ RawBuffer CKMLogic::createSignature(
 
     try {
         do {
-            retCode = getDataHelper(cred, DBDataType::KEY_RSA_PUBLIC, privateKeyAlias, password, row);
+            retCode = getDataHelper(cred, DBDataType::DB_KEY_FIRST, privateKeyAlias, password, row);
             if (CKM_API_SUCCESS != retCode) {
                 LogError("getDataHelper return error");
                 break;
             }
 
-            GenericKey keyParsed(row.data, Password());
+            KeyImpl keyParsed(row.data, Password());
             if (keyParsed.empty())
                 retCode = CKM_API_ERROR_SERVER_ERROR;
             else
-                cs.createSignature(keyParsed, message, hash, padding, signature);
+                retCode = cs.createSignature(keyParsed, message, hash, padding, signature);
         } while(0);
     } catch (const KeyProvider::Exception::Base &e) {
         LogError("KeyProvider failed with message: " << e.GetMessage());
@@ -733,6 +785,9 @@ RawBuffer CKMLogic::createSignature(
     } catch (const CryptoLogic::Exception::Base &e) {
         LogError("CryptoLogic failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
+    } catch (const DBCrypto::Exception::PermissionDenied &e) {
+        LogError("DBCrypto failed with message: " << e.GetMessage());
+        retCode = CKM_API_ERROR_ACCESS_DENIED;
     } catch (const DBCrypto::Exception::Base &e) {
         LogError("DBCrypto failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_DB_ERROR;
@@ -741,11 +796,10 @@ RawBuffer CKMLogic::createSignature(
         retCode = CKM_API_ERROR_SERVER_ERROR;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_SIGNATURE));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-    Serialization::Serialize(response, signature);
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
+                                             commandId,
+                                             retCode,
+                                             signature);
     return response.Pop();
 }
 
@@ -765,18 +819,18 @@ RawBuffer CKMLogic::verifySignature(
         do {
             CryptoService cs;
             DBRow row;
-            GenericKey key;
+            KeyImpl key;
 
             retCode = getDataHelper(cred, DBDataType::DB_KEY_FIRST, publicKeyOrCertAlias, password, row);
 
             if (retCode == CKM_API_SUCCESS) {
-                key = GenericKey(row.data);
+                key = KeyImpl(row.data);
             } else if (retCode == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
                 retCode = getDataHelper(cred, DBDataType::CERTIFICATE, publicKeyOrCertAlias, password, row);
                 if (retCode != CKM_API_SUCCESS)
                     break;
                 CertificateImpl cert(row.data, DataFormat::FORM_DER);
-                key = cert.getGenericKey();
+                key = cert.getKeyImpl();
             } else {
                 break;
             }
@@ -800,6 +854,9 @@ RawBuffer CKMLogic::verifySignature(
     } catch (const CryptoLogic::Exception::Base &e) {
         LogError("CryptoLogic failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_SERVER_ERROR;
+    } catch (const DBCrypto::Exception::PermissionDenied &e) {
+        LogError("DBCrypto failed with message: " << e.GetMessage());
+        retCode = CKM_API_ERROR_ACCESS_DENIED;
     } catch (const DBCrypto::Exception::Base &e) {
         LogError("DBCrypto failed with message: " << e.GetMessage());
         retCode = CKM_API_ERROR_DB_ERROR;
@@ -808,12 +865,72 @@ RawBuffer CKMLogic::verifySignature(
         retCode = CKM_API_ERROR_SERVER_ERROR;
     }
 
-    MessageBuffer response;
-    Serialization::Serialize(response, static_cast<int>(LogicCommand::VERIFY_SIGNATURE));
-    Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, retCode);
-
+    auto response = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
+                                             commandId,
+                                             retCode);
     return response.Pop();
 }
+
+RawBuffer CKMLogic::allowAccess(
+        Credentials &cred,
+        int command,
+        int msgID,
+        const Alias &item_alias,
+        const std::string &accessor_label,
+        const AccessRight req_rights)
+{
+    int retCode = CKM_API_ERROR_VERIFICATION_FAILED;
+
+    if (0 < m_userDataMap.count(cred.uid))
+    {
+        Try {
+            retCode = m_userDataMap[cred.uid].database.setAccessRights(cred.smackLabel, item_alias, accessor_label, req_rights);
+        } Catch (DBCrypto::Exception::InvalidArgs) {
+            LogError("Error: invalid args!");
+            retCode = CKM_API_ERROR_INPUT_PARAM;
+        } Catch (DBCrypto::Exception::PermissionDenied) {
+            LogError("Error: not enough permissions!");
+            retCode = CKM_API_ERROR_ACCESS_DENIED;
+        } Catch (CKM::Exception) {
+            LogError("Error in set row!");
+            retCode = CKM_API_ERROR_DB_ERROR;
+        }
+    } else {
+        retCode = CKM_API_ERROR_DB_LOCKED;
+    }
+
+    return MessageBuffer::Serialize(command, msgID, retCode).Pop();
+}
+
+RawBuffer CKMLogic::denyAccess(
+        Credentials &cred,
+        int command,
+        int msgID,
+        const Alias &item_alias,
+        const std::string &accessor_label)
+{
+    int retCode = CKM_API_ERROR_VERIFICATION_FAILED;
+
+    if (0 < m_userDataMap.count(cred.uid))
+    {
+        Try {
+            retCode = m_userDataMap[cred.uid].database.clearAccessRights(cred.smackLabel, item_alias, accessor_label);
+        } Catch (DBCrypto::Exception::PermissionDenied) {
+            LogError("Error: not enough permissions!");
+            retCode = CKM_API_ERROR_ACCESS_DENIED;
+        } Catch (DBCrypto::Exception::InvalidArgs) {
+            LogError("Error: permission not found!");
+            retCode = CKM_API_ERROR_INPUT_PARAM;
+        } Catch (CKM::Exception) {
+            LogError("Error in deleting row!");
+            retCode = CKM_API_ERROR_DB_ERROR;
+        }
+    } else {
+        retCode = CKM_API_ERROR_DB_LOCKED;
+    }
+
+    return MessageBuffer::Serialize(command, msgID, retCode).Pop();
+}
+
 } // namespace CKM