CKM: Check backend info
[platform/core/test/security-tests.git] / src / ckm / ckm-common.cpp
index 714fa64..2ceb95b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
  * @version    1.0
  */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <cstdlib>
 #include <string>
 #include <fstream>
 #include <sys/smack.h>
@@ -32,6 +36,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <unordered_map>
+#include <tzplatform_config.h>
 
 const std::string SMACK_USER_APP_PREFIX = "User::Pkg::";
 const char *SYSTEM_LABEL = ckmc_owner_id_system;
@@ -382,55 +387,63 @@ void check_alias_list(const CKM::AliasVector& expected)
     RUNNER_ASSERT_MSG(expected == actual, "Actual list of aliases differ from expected list.");
 }
 
-void check_alias_info_list_helper(const CKM::AliasPwdVector& expected, const CKM::AliasPwdVector& actual,
-        const std::string userSmackLabel)
+void check_alias_info_list_helper(const InfoVector& expected,
+                                  const InfoMap& actual,
+                                  const std::string &userSmackLabel)
 {
     std::string errorLogMsg;
-    std::unordered_map<std::string, bool> aliasPwdMap;
 
     RUNNER_ASSERT_MSG(expected.size() == actual.size(), "Aliases item count differs, expected: " <<
         expected.size() << " actual: " << actual.size());
 
-    for (const auto &it : actual)
-    {
-        aliasPwdMap[std::get<0>(it)] = std::get<1>(it);
-    }
-
-
-    for (const auto &it : expected)
+    for (const auto &expectedIt : expected)
     {
-        auto aliasPwd = aliasPwdMap.find(userSmackLabel + std::get<0>(it));
-        if (aliasPwd != aliasPwdMap.end()) {
-            if (aliasPwd->second != std::get<1>(it)) {
-                errorLogMsg += "Alias: " + std::get<0>(it) + " has wrong encryption status: "
-                                + std::to_string(std::get<1>(it)) + "\n";
+        auto actualIt = actual.find(userSmackLabel + expectedIt.alias);
+        if (actualIt != actual.end()) {
+            if (actualIt->second.passwordProtected != expectedIt.passwordProtected) {
+                errorLogMsg += "Alias: " + actualIt->second.alias + " has wrong encryption status: "
+                    + std::to_string(actualIt->second.passwordProtected) + "\n";
+            }
+            if (actualIt->second.backend != expectedIt.backend) {
+                errorLogMsg += "Alias: " + actualIt->second.alias + " belongs to wrong backend: "
+                    + std::to_string(static_cast<int>(actualIt->second.backend)) + "\n";
             }
         }
         else {
-            errorLogMsg += "Expected alias: " + std::get<0>(it) + " not found.\n";
+            errorLogMsg += "Expected alias: " + actualIt->second.alias + " not found.\n";
         }
     }
 
     if (!errorLogMsg.empty()) {
-        for (const auto &it : actual)
+        for (const auto& [alias, info] : actual)
         {
-            errorLogMsg += "Actual alias: " + std::get<0>(it) + " status: "
-                + std::to_string(std::get<1>(it)) + "\n";
+            errorLogMsg += "Actual alias: " + alias +
+                " status: " + std::to_string(info.passwordProtected) +
+                " backend: " + std::to_string(static_cast<int>(info.backend)) + "\n";
         }
         RUNNER_FAIL_MSG("Actual list of aliases differ from expected list.\n" + errorLogMsg);
     }
 }
 
-void check_alias_info_list(const CKM::AliasPwdVector& expected, const std::string& userSmackLabel)
+CKM::BackendId backend()
+{
+#ifdef TZ_BACKEND
+    return CKM::BackendId::TZ;
+#else
+    return CKM::BackendId::SW;
+#endif
+}
+
+void check_alias_info_list(const InfoVector& expected)
 {
     ckmc_alias_info_list_s *aliasInfoList = NULL;
     int ret = ckmc_get_data_alias_info_list(&aliasInfoList);
     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get the list of data aliases. " << ret << " / "
                       << CKMCErrorToString(ret));
 
-    CKM::AliasPwdVector actual;
+    InfoMap actual;
     ckmc_alias_info_list_s *plist = aliasInfoList;
-    char* alias;
+    char* alias = nullptr;
     bool isPasswordProtected;
     unsigned int it = 0;
     while (plist)
@@ -438,16 +451,23 @@ void check_alias_info_list(const CKM::AliasPwdVector& expected, const std::strin
         ret = ckmc_alias_info_get_alias(plist->info, &alias);
         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get alias. " << ret << " / "
                           << CKMCErrorToString(ret));
+        RUNNER_ASSERT_MSG(alias != nullptr, "Got null alias. Iterator: " << it);
+
         ret = ckmc_alias_info_is_password_protected(plist->info, &isPasswordProtected);
         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get password protection status" << ret << " / "
                           << CKMCErrorToString(ret));
-        RUNNER_ASSERT_MSG(alias != nullptr, "Got null alias. Iterator: " << it);
-        actual.push_back(std::make_pair(alias, isPasswordProtected));
+
+        ckmc_backend_id_e backend;
+        ret = ckmc_alias_info_get_backend(plist->info, &backend);
+        RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get backend" << ret << " / "
+                          << CKMCErrorToString(ret));
+
+        actual.try_emplace(alias, alias, isPasswordProtected, static_cast<CKM::BackendId>(backend));
         plist = plist->next;
         it++;
     }
     ckmc_alias_info_list_all_free(aliasInfoList);
-    check_alias_info_list_helper(expected, actual, userSmackLabel);
+    check_alias_info_list_helper(expected, actual);
 }
 
 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count)
@@ -564,13 +584,28 @@ ParamListPtr createParamListPtr()
     return ParamListPtr(list, ckmc_param_list_free);
 }
 
-void assert_buffers_equal(const ckmc_raw_buffer_s b1, const ckmc_raw_buffer_s b2, bool equal)
+void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
+{
+    int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
+    RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
+                      "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
+}
+
+void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer)
+{
+    int ret = ckmc_param_list_set_integer(params.get(), name, integer);
+    RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
+                      "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
+}
+
+void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal)
 {
     if(equal) {
-        RUNNER_ASSERT_MSG(b1.size == b2.size, "Buffer size differs: " << b1.size << "!=" << b2.size);
-        RUNNER_ASSERT_MSG(0 == memcmp(b1.data, b2.data, b1.size), "Buffer contents differ");
+        RUNNER_ASSERT_MSG(b1->size == b2->size,
+                          "Buffer size differs: " << b1->size << "!=" << b2->size);
+        RUNNER_ASSERT_MSG(0 == memcmp(b1->data, b2->data, b1->size), "Buffer contents differ");
     } else {
-        RUNNER_ASSERT_MSG(b1.size != b2.size || 0 != memcmp(b1.data, b2.data, b1.size),
+        RUNNER_ASSERT_MSG(b1->size != b2->size || 0 != memcmp(b1->data, b2->data, b1->size),
                           "Buffers should be different");
     }
 }
@@ -580,9 +615,28 @@ RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer)
     return RawBufferPtr(buffer, ckmc_buffer_free);
 }
 
+CipherCtxPtr create_cipher_ctx(ckmc_cipher_ctx_h ctx)
+{
+    return CipherCtxPtr(ctx, ckmc_cipher_free);
+}
+
 CKM::Policy generate_ckm_policy(int iterator_nr) {
     if (iterator_nr % 2) { // policy with password and with / without extractable flag
         return CKM::Policy(CKM::Password("test_pwd"), iterator_nr % 4);
     }
     return CKM::Policy();
 }
+
+void require_default_user(char *argv[])
+{
+    uid_t expected_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
+    if (expected_uid != geteuid()) {
+        std::string userStr("owner");
+        const char* user = tzplatform_getenv(TZ_SYS_DEFAULT_USER);
+        if (user)
+            userStr = user;
+
+        std::cerr << argv[0] << " should be executed as " << userStr << ". Aborting" << std::endl;
+        exit(-1);
+    }
+}