Add api CKM::Control::removeApplicationData
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 6 Aug 2014 13:27:45 +0000 (15:27 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:59:27 +0000 (14:59 +0200)
This api may be used during application uninstallation to remove
all application data from ckm datbase.

Change-Id: I5526744d9fface01621e755c7a09072ffb5c0548

src/include/ckm/ckm-control.h
src/manager/client/client-control.cpp
src/manager/common/protocols.h
src/manager/service/ckm-logic.cpp
src/manager/service/ckm-logic.h
src/manager/service/ckm-service.cpp
src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index 828d8f1..17ffaa3 100644 (file)
@@ -56,6 +56,12 @@ public:
     // The service will use DKEK in plain text and encrypt it in encrypted form (using new password).
     virtual int resetUserPassword(uid_t user, const Password &newPassword) = 0;
 
+    // Required for tizen 2.3.
+    // It will remove all application data owned by application identified
+    // by smackLabel. This function will remove application data from unlocked
+    // database only. This function may be used during application uninstallation.
+    virtual int removeApplicationData(const std::string &smackLabel) = 0;
+
     virtual ~Control(){}
 
     static ControlShPtr create();
index 97a8915..6f3c5d3 100644 (file)
@@ -165,6 +165,31 @@ public:
         });
     }
 
+    virtual int removeApplicationData(const std::string &smackLabel) {
+        return try_catch([&] {
+            if (smackLabel.empty()) {
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
+
+            MessageBuffer send,recv;
+            Serialization::Serialize(send, static_cast<int>(ControlCommand::REMOVE_APP_DATA));
+            Serialization::Serialize(send, smackLabel);
+
+            int retCode = sendToServer(
+                SERVICE_SOCKET_CKM_CONTROL,
+                send.Pop(),
+                recv);
+
+            if (CKM_API_SUCCESS != retCode) {
+                return retCode;
+            }
+
+            Deserialization::Deserialize(recv, retCode);
+
+            return retCode;
+        });
+    }
+
     virtual ~ControlImpl(){}
 };
 
index 4eac680..5b267a4 100644 (file)
@@ -38,7 +38,8 @@ enum class ControlCommand : int {
     LOCK_USER_KEY,
     REMOVE_USER_DATA,
     CHANGE_USER_PASSWORD,
-    RESET_USER_PASSWORD
+    RESET_USER_PASSWORD,
+    REMOVE_APP_DATA
 };
 
 enum class LogicCommand : int {
index 69434f7..b70357a 100755 (executable)
@@ -164,6 +164,32 @@ RawBuffer CKMLogic::resetUserPassword(
     return response.Pop();
 }
 
+RawBuffer CKMLogic::removeApplicationData(const std::string &smackLabel) {
+    int retCode = CKM_API_SUCCESS;
+
+    try {
+
+        if (smackLabel.empty()) {
+            retCode = CKM_API_ERROR_INPUT_PARAM;
+        } else {
+            for(auto &handler: m_userDataMap) {
+                handler.second.database.deleteKey(smackLabel);
+            }
+        }
+
+    } catch (const DBCrypto::Exception::InternalError &e) {
+        LogError("DBCrypto couldn't remove data: " << e.GetMessage());
+        retCode = CKM_API_ERROR_DB_ERROR;
+    } catch (const DBCrypto::Exception::TransactionError &e) {
+        LogError("DBCrypto transaction failed with message " << e.GetMessage());
+        retCode = CKM_API_ERROR_DB_ERROR;
+    }
+
+    MessageBuffer response;
+    Serialization::Serialize(response, retCode);
+    return response.Pop();
+}
+
 int CKMLogic::saveDataHelper(
     Credentials &cred,
     DBDataType dataType,
index 56a8348..9ed10c1 100644 (file)
@@ -65,6 +65,8 @@ public:
         uid_t user,
         const Password &newPassword);
 
+    RawBuffer removeApplicationData(const std::string &smackLabel);
+
     RawBuffer saveData(
         Credentials &cred,
         int commandId,
index 020df35..906929c 100644 (file)
@@ -110,9 +110,9 @@ RawBuffer CKMService::processControl(MessageBuffer &buffer) {
     uid_t user;
     ControlCommand cc;
     Password newPass, oldPass;
+    std::string smackLabel;
 
     Deserialization::Deserialize(buffer, command);
-    Deserialization::Deserialize(buffer, user);
 
     LogDebug("Process control. Command: " << command);
 
@@ -120,19 +120,27 @@ RawBuffer CKMService::processControl(MessageBuffer &buffer) {
 
     switch(cc) {
     case ControlCommand::UNLOCK_USER_KEY:
+        Deserialization::Deserialize(buffer, user);
         Deserialization::Deserialize(buffer, newPass);
         return m_logic->unlockUserKey(user, newPass);
     case ControlCommand::LOCK_USER_KEY:
+        Deserialization::Deserialize(buffer, user);
         return m_logic->lockUserKey(user);
     case ControlCommand::REMOVE_USER_DATA:
+        Deserialization::Deserialize(buffer, user);
         return m_logic->removeUserData(user);
     case ControlCommand::CHANGE_USER_PASSWORD:
+        Deserialization::Deserialize(buffer, user);
         Deserialization::Deserialize(buffer, oldPass);
         Deserialization::Deserialize(buffer, newPass);
         return m_logic->changeUserPassword(user, oldPass, newPass);
     case ControlCommand::RESET_USER_PASSWORD:
+        Deserialization::Deserialize(buffer, user);
         Deserialization::Deserialize(buffer, newPass);
         return m_logic->resetUserPassword(user, newPass);
+    case ControlCommand::REMOVE_APP_DATA:
+        Deserialization::Deserialize(buffer, smackLabel);
+        return m_logic->removeApplicationData(smackLabel);
     default:
         Throw(Exception::BrokenProtocol);
     }
index 0fc0cb1..2507252 100644 (file)
@@ -110,6 +110,10 @@ namespace {
             //                                 1           2
             "DELETE FROM CKM_TABLE WHERE alias=? AND label=?;";
 
+    const char *delete_data_with_key_cmd =
+            //                                 1
+            "DELETE FROM CKM_TABLE WHERE label=?;";
+
 // KEY_TABLE (label TEXT, key BLOB)
 
     const char *db_create_key_cmd =
@@ -508,10 +512,17 @@ using namespace DB;
     void DBCrypto::deleteKey(const std::string& label) {
         Try {
             Transaction transaction(this);
+
             SqlConnection::DataCommandUniquePtr deleteCommand =
                     m_connection->PrepareDataCommand(delete_key_cmd);
             deleteCommand->BindString(1, label.c_str());
             deleteCommand->Step();
+
+            SqlConnection::DataCommandUniquePtr deleteData =
+                m_connection->PrepareDataCommand(delete_data_with_key_cmd);
+            deleteData->BindString(1, label.c_str());
+            deleteData->Step();
+
             transaction.commit();
             return;
         } Catch (SqlConnection::Exception::SyntaxError) {
index 7abb79a..3153974 100644 (file)
@@ -80,7 +80,6 @@ namespace CKM {
             int deleteDBRow(
                     const Alias& alias,
                     const std::string &label);
-
             void saveKey(const std::string& label, const RawBuffer &key);
             RawBufferOptional getKey(
                     const std::string& label);