${PROJECT_SOURCE_DIR}/src/ckm/ckm-common.cpp
${PROJECT_SOURCE_DIR}/src/ckm/cc-mode.cpp
# ${PROJECT_SOURCE_DIR}/src/ckm/password-integration.cpp
+ ${PROJECT_SOURCE_DIR}/src/ckm/system-db.cpp
${PROJECT_SOURCE_DIR}/src/ckm/clean-env.cpp
${PROJECT_SOURCE_DIR}/src/ckm/test-certs.cpp
)
#include <tests_common.h>
AccessProvider::AccessProvider(const std::string &mySubject)
- : m_mySubject(mySubject)
-{}
+ : m_mySubject(mySubject), m_inSwitchContext(false)
+{
+ RUNNER_ASSERT_MSG(m_mySubject.size() > 0, "No smack label provided to AccessProvider!");
+}
+
+AccessProvider::AccessProvider(const std::string &mySubject, int uid, int gid)
+ : m_mySubject(mySubject), m_inSwitchContext(false)
+{
+ RUNNER_ASSERT_MSG(m_mySubject.size() > 0, "No smack label provided to AccessProvider!");
+ applyAndSwithToUser(uid, gid);
+}
void AccessProvider::allowAPI(const std::string &api, const std::string &rule) {
m_smackAccess.add(m_mySubject, api, rule);
m_smackAccess.apply();
}
-void AccessProvider::applyAndSwithToUser(int uid, int gid) {
+void AccessProvider::applyAndSwithToUser(int uid, int gid)
+{
+ RUNNER_ASSERT_MSG(m_inSwitchContext == false, "already switched context");
+
+ // get calling label
+ char* my_label = NULL;
+ RUNNER_ASSERT(smack_new_label_from_self(&my_label) > 0);
+ if(my_label)
+ {
+ m_origLabel = std::string(my_label);
+ free(my_label);
+ }
+ RUNNER_ASSERT(m_origLabel.size() > 0);
+
RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_mySubject.c_str()),
"Error in smack_revoke_subject(" << m_mySubject << ")");
apply();
RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(m_mySubject.c_str()),
"Error in smack_set_label_for_self.");
- RUNNER_ASSERT_MSG(0 == setgid(gid),
+
+ m_origUid = getuid();
+ m_origGid = getgid();
+ RUNNER_ASSERT_MSG(0 == setegid(gid),
"Error in setgid.");
- RUNNER_ASSERT_MSG(0 == setuid(uid),
+ RUNNER_ASSERT_MSG(0 == seteuid(uid),
"Error in setuid.");
+ m_inSwitchContext = true;
}
+ScopedAccessProvider::~ScopedAccessProvider()
+{
+ if(m_inSwitchContext == true)
+ {
+ RUNNER_ASSERT_MSG(0 == setegid(m_origGid), "Error in setgid.");
+ RUNNER_ASSERT_MSG(0 == seteuid(m_origUid), "Error in setuid.");
+ RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_mySubject.c_str()),
+ "Error in smack_revoke_subject(" << m_mySubject << ")");
+ RUNNER_ASSERT_MSG(0 == smack_set_label_for_self(m_origLabel.c_str()),
+ "Error in smack_set_label_for_self.");
+ m_inSwitchContext = false;
+ }
+}
* limitations under the License.
*/
/*
- * @file access_provider.h
+ * @file access_provider2.h
* @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
* @version 1.0
* @brief Common functions and macros used in security-tests package.
class AccessProvider {
public:
- AccessProvider(const std::string &mySubject);
+ explicit AccessProvider(const std::string &mySubject);
+ AccessProvider(const std::string &mySubject, int uid, int gid);
+ virtual ~AccessProvider() {}
AccessProvider(const AccessProvider &second) = delete;
AccessProvider& operator=(const AccessProvider &second) = delete;
void apply();
void applyAndSwithToUser(int uid, int gid);
- virtual ~AccessProvider(){}
private:
- std::string m_mySubject;
SmackAccess m_smackAccess;
+protected:
+ std::string m_mySubject;
+ uid_t m_origUid;
+ gid_t m_origGid;
+ std::string m_origLabel;
+ bool m_inSwitchContext;
};
-#endif // _ACCESS_FOR_DUMMIES_H_
+class ScopedAccessProvider : public AccessProvider {
+public:
+ explicit ScopedAccessProvider(const std::string &mySubject)
+ : AccessProvider(mySubject) {}
+ ScopedAccessProvider(const std::string &mySubject, int uid, int gid)
+ : AccessProvider(mySubject, uid, gid) {}
+ virtual ~ScopedAccessProvider();
+};
+#endif // _ACCESS_FOR_DUMMIES_H_
const char* TEST_DATA = "dsflsdkghkslhglrtghierhgilrehgidsafasdffsgfdgdgfdgfdgfdgfdggf";
-void save_data(const char* alias, const char *data)
-{
- ckmc_raw_buffer_s buffer;
- buffer.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data));
- buffer.size = strlen(data);
- ckmc_policy_s policy;
- policy.password = NULL;
- policy.extractable = true;
-
- int ret = ckmc_save_data(alias, buffer, policy);
- RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Saving data failed. Error: " << ret);
-}
-
-void save_data(const char* alias)
-{
- save_data(alias, TEST_DATA);
-}
-
-void check_remove_allowed(const char* alias)
-{
- int ret = ckmc_remove_alias(alias);
- // remove, but ignore non existing
- RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret || CKMC_ERROR_DB_ALIAS_UNKNOWN,
- "Removing data failed: " << CKMCErrorToString(ret));
-}
-
-void check_remove_denied(const char* alias)
-{
- int ret = ckmc_remove_alias(alias);
- RUNNER_ASSERT_MSG(
- CKMC_ERROR_PERMISSION_DENIED == ret,
- "App with different label shouldn't have rights to remove this data. Error: " << ret);
-}
-
-void check_remove_not_visible(const char* alias)
-{
- int ret = ckmc_remove_alias(alias);
- RUNNER_ASSERT_MSG(
- CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
- "App with different label shouldn't have rights to see this data. Error: " << ret);
-}
-
-void check_read(const char* alias, const char *label, const char *test_data, int expected_code = CKMC_ERROR_NONE)
-{
- ckmc_raw_buffer_s* buffer = NULL;
- int ret = ckmc_get_data(aliasWithLabel(label, alias).c_str(), NULL, &buffer);
- RUNNER_ASSERT_MSG(expected_code == ret, "Getting data failed. Expected code: " << expected_code << ", while result: " << CKMCErrorToString(ret));
-
- if(expected_code == CKMC_ERROR_NONE)
- {
- // compare data with expected
- RUNNER_ASSERT_MSG(
- buffer->size == strlen(test_data),
- "Extracted data length do not match expected data length (encrypted?).");
-
- RUNNER_ASSERT_MSG(
- memcmp(const_cast<const char*>(reinterpret_cast<char*>(buffer->data)), test_data, buffer->size) == 0,
- "Extracted data do not match expected data (encrypted?).");
-
- ckmc_buffer_free(buffer);
- }
-}
-
-void check_read_allowed(const char* alias, const char *data)
-{
- // try to read previously saved data - label taken implicitly
- check_read(alias, 0, data);
-}
-void check_read_allowed(const char* alias)
-{
- check_read_allowed(alias, TEST_DATA);
-}
-
-void check_read_not_visible(const char* alias)
-{
- // try to read previously saved data - label taken implicitly
- {
- ckmc_raw_buffer_s* buffer = NULL;
- int ret = ckmc_get_data(alias, NULL, &buffer);
- RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
- "App with different label shouldn't have rights to see this data." << CKMCErrorToString(ret));
- ckmc_buffer_free(buffer);
- }
-}
void allow_access_deprecated(const char* alias, const char* accessor, ckmc_access_right_e accessRights)
{
RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << CKMCErrorToString(ret));
}
-void allow_access(const char* alias, const char* accessor, int permissionMask)
-{
- // data removal should revoke this access
- int ret = ckmc_set_permission(alias, accessor, permissionMask);
- RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: " << CKMCErrorToString(ret));
-}
-
-void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode)
-{
- // data removal should revoke this access
- int ret = ckmc_set_permission(alias, accessor, permissionMask);
- RUNNER_ASSERT_MSG(expectedCode == ret, "Trying to allow access returned " << CKMCErrorToString(ret) << ", while expected: " << CKMCErrorToString(expectedCode));
-}
-
-void deny_access(const char* alias, const char* accessor)
-{
- int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
- RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Denying access failed. Error: " << CKMCErrorToString(ret));
-}
-
-void deny_access_negative(const char* alias, const char* accessor, int expectedCode)
-{
- int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
- RUNNER_ASSERT_MSG(expectedCode == ret, "Denying access failed. " << CKMCErrorToString(ret) << ", while expected: " << CKMCErrorToString(expectedCode));
-}
-
void allow_access_deprecated_by_adm(const char* alias, const char* accessor, ckmc_access_right_e accessRights)
{
// data removal should revoke this access
RUNNER_ASSERT_MSG(count == expected, "Expected " << expected << " aliases, got " << count);
}
-// saves data upon construction and deletes it upon destruction
-class ScopedSaveData
-{
-public:
- ScopedSaveData(const char* alias) : m_alias(alias)
- {
- save_data(alias);
- }
- ScopedSaveData(const char* alias, const char *data) : m_alias(alias)
- {
- save_data(alias, data);
- }
-
- ~ScopedSaveData()
- {
- /*
- * Let it throw. If we can't remove data then remaining tests results will be
- * unreliable anyway.
- */
- check_remove_allowed(m_alias);
- }
-private:
- const char* m_alias;
-};
-
} // namespace anonymous
RUNNER_TEST_GROUP_INIT (T300_CKMC_ACCESS_CONTROL_C_API);
{
switch_to_storage_user(TEST_LABEL);
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// deny non existing access to existing alias
int ret = ckmc_set_permission(TEST_ALIAS, "label", CKMC_PERMISSION_NONE);
{
switch_to_storage_user(TEST_LABEL);
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
CharPtr label = get_label();
int ret = ckmc_set_permission(TEST_ALIAS, label.get(), CKMC_PERMISSION_READ);
RUNNER_CHILD_TEST(T3007_manager_check_alias_valid)
{
switch_to_storage_user(TEST_LABEL);
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
std::string test_alias_playground = std::string("AAA BBB CCC");
check_read(test_alias_playground.c_str(), 0, TEST_DATA, CKMC_ERROR_INVALID_PARAMETER);
RUNNER_CHILD_TEST(T3008_manager_check_label_valid)
{
switch_to_storage_user(TEST_LABEL);
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// basic test
std::string test_label_playground = std::string("AAA BBB CCC");
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
{
ScopedLabel sl(TEST_LABEL2);
RUNNER_TEST(T3021_manager_access_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3022_manager_access_allowed_with_remove)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3023_manager_access_allowed_remove_denied)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
{
std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
- check_read_allowed(TEST_ALIAS_adr.c_str());
+ check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3025_manager_remove_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
RUNNER_TEST(T3026_manager_double_allow)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// access should be overwritten
allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
- check_read_allowed(TEST_ALIAS_adr.c_str());
+ check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3027_manager_allow_deny)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
ScopedLabel sl(TEST_LABEL2);
check_remove_denied(TEST_ALIAS_adr.c_str());
- check_read_allowed(TEST_ALIAS_adr.c_str());
+ check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
}
deny_access(TEST_ALIAS, TEST_LABEL2);
{
CharPtr top_label = get_label();
const char *additional_data = "label-2-data";
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
{
}
// test if accessing valid alias (of label1 domain)
- check_read_allowed(TEST_ALIAS);
+ check_read_allowed(TEST_ALIAS, TEST_DATA);
// access should not be possible - already left the LABEL2 scope, object should be removed
check_read_not_visible(aliasWithLabel(TEST_LABEL2, TEST_ALIAS).c_str());
RUNNER_TEST(T3029_manager_access_modification_by_foreign_label)
{
ScopedLabel sl(TEST_LABEL);
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, TEST_LABEL3, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
ScopedLabel sl(TEST_LABEL2);
// checks if only aliases readable by given app are returned
RUNNER_TEST(T3030_manager_get_all_aliases)
{
- ScopedSaveData ssd1(TEST_ALIAS);
- ScopedSaveData ssd2(TEST_ALIAS2);
+ ScopedSaveData ssd1(TEST_ALIAS, TEST_DATA);
+ ScopedSaveData ssd2(TEST_ALIAS2, TEST_DATA);
int count = count_aliases();
// check that app can access other aliases when it has permission
check_alias_count(count - 1);
- ScopedSaveData ssd3(TEST_ALIAS3);
+ ScopedSaveData ssd3(TEST_ALIAS3, TEST_DATA);
// check that app can access its own aliases
check_alias_count(count - 1 + 1);
RUNNER_TEST(T3031_manager_deprecated_access_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3032_manager_deprecated_access_allowed_with_remove)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ_REMOVE);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3033_manager_deprecated_access_allowed_remove_denied)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ);
{
std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
- check_read_allowed(TEST_ALIAS_adr.c_str());
+ check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3034_manager_deprecated_remove_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ_REMOVE);
{
// tries to deny non existing access
RUNNER_TEST(T3105_control_deny_access_non_existing_access)
{
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
CharPtr label = get_label();
// tries to allow application to access its own data
RUNNER_TEST(T3106_control_allow_access_to_myself)
{
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
CharPtr label = get_label();
int ret = ckmc_set_permission(TEST_ALIAS, label.get(), CKMC_PERMISSION_READ);
RUNNER_TEST(T3121_control_access_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3122_control_access_allowed_with_remove)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3122_control_access_allowed_remove_denied)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ);
{
RUNNER_TEST(T3125_control_remove_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
RUNNER_TEST(T3126_control_double_allow)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// access should be overwritten
allow_access_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
- check_read_allowed(TEST_ALIAS_adr.c_str());
+ check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3127_control_allow_deny)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
std::string TEST_ALIAS_adr = aliasWithLabel(top_label.get(), TEST_ALIAS);
ScopedLabel sl(TEST_LABEL2);
check_remove_denied(TEST_ALIAS_adr.c_str());
- check_read_allowed(TEST_ALIAS_adr.c_str());
+ check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
}
CharPtr label = get_label();
deny_access_by_adm(TEST_ALIAS, TEST_LABEL2);
// checks if only aliases readable by given app are returned
RUNNER_TEST(T3130_control_get_all_aliases)
{
- ScopedSaveData ssd1(TEST_ALIAS);
- ScopedSaveData ssd2(TEST_ALIAS2);
+ ScopedSaveData ssd1(TEST_ALIAS, TEST_DATA);
+ ScopedSaveData ssd2(TEST_ALIAS2, TEST_DATA);
int count = count_aliases();
// check that app can access other aliases when it has permission
check_alias_count(count - 1);
- ScopedSaveData ssd3(TEST_ALIAS3);
+ ScopedSaveData ssd3(TEST_ALIAS3, TEST_DATA);
// check that app can access its own aliases
check_alias_count(count - 1 + 1);
// tries to add access to data in a database of invalid user
RUNNER_TEST(T3140_control_allow_invalid_user)
{
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
int ret = ckmc_set_permission_by_adm(
APP_UID, aliasWithLabel(get_label().get(), TEST_ALIAS).c_str(), TEST_LABEL2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
// tries to revoke access to data in a database of invalid user
RUNNER_TEST(T3141_control_deny_invalid_user)
{
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
int ret = ckmc_set_permission_by_adm(APP_UID, aliasWithLabel(get_label().get(), TEST_ALIAS).c_str(), TEST_LABEL2, CKMC_PERMISSION_NONE);
RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
RUNNER_TEST(T3142_control_deprecated_access_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3143_control_deprecated_access_allowed_with_remove)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ_REMOVE);
{
ScopedLabel sl(TEST_LABEL2);
- check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str());
+ check_read_allowed(aliasWithLabel(top_label.get(), TEST_ALIAS).c_str(), TEST_DATA);
}
}
RUNNER_TEST(T3144_control_deprecated_access_allowed_remove_denied)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ);
{
RUNNER_TEST(T3145_control_deprecated_remove_allowed)
{
CharPtr top_label = get_label();
- ScopedSaveData ssd(TEST_ALIAS);
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
allow_access_deprecated_by_adm(TEST_ALIAS, TEST_LABEL2, CKMC_AR_READ_REMOVE);
{
return ec;
}
-ckmc_raw_buffer_s prepare_message_buffer(const char * input)
-{
- ckmc_raw_buffer_s retval;
- retval.data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(input));
- retval.size = strlen(input);
- return retval;
-}
-
} // namespace anonymous
RUNNER_CHILD_TEST(T3026_user_app_save_key_C_API)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T3027_app_user_save_keys_exportable_flag)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T3043_app_user_save_bin_data_C_API)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
{
int temp;
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
#include <ckmc/ckmc-control.h>
#include <ckmc/ckmc-manager.h>
#include <service_manager.h>
+#include <unistd.h>
const char* SERVICE[] = {
"central-key-manager-listener.service",
return output;
}
+void save_data(const char* alias, const char *data, int expected_err)
+{
+ RUNNER_ASSERT(alias);
+ RUNNER_ASSERT(data);
+
+ ckmc_raw_buffer_s buffer;
+ buffer.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data));
+ buffer.size = strlen(data);
+ ckmc_policy_s policy;
+ policy.password = NULL;
+ policy.extractable = true;
+
+ int ret = ckmc_save_data(alias, buffer, policy);
+ RUNNER_ASSERT_MSG(expected_err == ret, "Saving data failed. "
+ << CKMCErrorToString(ret) << " while expected: "
+ << CKMCErrorToString(expected_err));
+}
+
+ScopedSaveData::ScopedSaveData(const char* alias, const char *data, int expected_err) : m_alias(alias)
+{
+ save_data(alias, data, expected_err);
+}
+
+ScopedSaveData::~ScopedSaveData()
+{
+ /*
+ * Let it throw. If we can't remove data then remaining tests results will be
+ * unreliable anyway.
+ */
+ check_remove_allowed(m_alias.c_str());
+}
+
+void GarbageCollector::save(const char* alias, const char *data, int expected_err)
+{
+ save_data(alias, data, expected_err);
+
+ if(CKMC_ERROR_NONE == expected_err)
+ {
+ save_item item;
+ item.item_alias = std::string(alias);
+ item.owner_label = std::string(get_label().get());
+ item.owner_uid = geteuid();
+ item.owner_gid = getegid();
+ m_garbage.push_back(item);
+ }
+}
+
+GarbageCollector::~GarbageCollector()
+{
+ for(auto & item : m_garbage)
+ {
+ ScopedAccessProvider ap(item.owner_label, item.owner_uid, item.owner_gid);
+ check_remove_allowed(item.item_alias.c_str());
+ }
+}
+
+ScopedDBUnlock::ScopedDBUnlock(uid_t user_id, const char* passwd) : m_uid(user_id)
+{
+ int temp;
+ RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(user_id)), CKMCErrorToString(temp));
+ RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(user_id, passwd)), CKMCErrorToString(temp));
+}
+ScopedDBUnlock::~ScopedDBUnlock()
+{
+ int temp;
+ RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(m_uid)), CKMCErrorToString(temp));
+ RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(m_uid)), CKMCErrorToString(temp));
+}
+
+void check_remove_allowed(const char* alias)
+{
+ int ret = ckmc_remove_alias(alias);
+ // remove, but ignore non existing
+ RUNNER_ASSERT_MSG((CKMC_ERROR_NONE == ret) || (CKMC_ERROR_DB_ALIAS_UNKNOWN == ret),
+ "Removing data failed: " << CKMCErrorToString(ret));
+}
+
+void check_remove_denied(const char* alias)
+{
+ int ret = ckmc_remove_alias(alias);
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_PERMISSION_DENIED == ret,
+ "App with different label shouldn't have rights to remove this data. "
+ << CKMCReadableError(ret));
+}
+
+void check_remove_not_visible(const char* alias)
+{
+ int ret = ckmc_remove_alias(alias);
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
+ "App with different label shouldn't have rights to see this data. "
+ << CKMCReadableError(ret));
+}
+
+void check_read(const char* alias, const char *label, const char *test_data, int expected_code)
+{
+ ckmc_raw_buffer_s* buffer = NULL;
+ int ret = ckmc_get_data(aliasWithLabel(label, alias).c_str(), NULL, &buffer);
+ RUNNER_ASSERT_MSG(expected_code == ret, "Getting data failed. "
+ "Expected " << CKMCErrorToString(expected_code) << ", "
+ "while result " << CKMCErrorToString(ret));
+
+ if(expected_code == CKMC_ERROR_NONE)
+ {
+ // compare data with expected
+ RUNNER_ASSERT_MSG(
+ buffer->size == strlen(test_data),
+ "Extracted data length do not match expected data length (encrypted?).");
+
+ RUNNER_ASSERT_MSG(
+ memcmp(const_cast<const char*>(reinterpret_cast<char*>(buffer->data)),
+ test_data, buffer->size) == 0,
+ "Extracted data do not match expected data (encrypted?).");
+
+ ckmc_buffer_free(buffer);
+ }
+}
+
+void check_read_allowed(const char* alias, const char *data)
+{
+ // try to read previously saved data - label taken implicitly
+ check_read(alias, NULL, data);
+}
+
+void check_read_not_visible(const char* alias)
+{
+ // try to read previously saved data - label taken implicitly
+ {
+ ckmc_raw_buffer_s* buffer = NULL;
+ int ret = ckmc_get_data(alias, NULL, &buffer);
+ RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
+ "App with different label shouldn't have rights to see this data." << CKMCErrorToString(ret));
+ ckmc_buffer_free(buffer);
+ }
+}
+
+void allow_access(const char* alias, const char* accessor, int permissionMask)
+{
+ // data removal should revoke this access
+ int ret = ckmc_set_permission(alias, accessor, permissionMask);
+ RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: "
+ << CKMCErrorToString(ret));
+}
+
+void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode)
+{
+ // data removal should revoke this access
+ int ret = ckmc_set_permission(alias, accessor, permissionMask);
+ RUNNER_ASSERT_MSG(expectedCode == ret, "Trying to allow access returned "
+ << CKMCErrorToString(ret) << ", while expected: "
+ << CKMCErrorToString(expectedCode));
+}
+
+void deny_access(const char* alias, const char* accessor)
+{
+ int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
+ RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Denying access failed. Error: "
+ << CKMCErrorToString(ret));
+}
+
+void deny_access_negative(const char* alias, const char* accessor, int expectedCode)
+{
+ int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
+ RUNNER_ASSERT_MSG(expectedCode == ret, "Denying access failed. "
+ << CKMCErrorToString(ret) << ", while expected: "
+ << CKMCErrorToString(expectedCode));
+}
+
void unlock_user_data(uid_t user_id, const char *passwd)
{
int ret;
remove_user_data(user_id);
unlock_user_data(user_id, passwd);
}
+
+ckmc_raw_buffer_s prepare_message_buffer(const char * input)
+{
+ ckmc_raw_buffer_s retval;
+ retval.data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(input));
+ retval.size = strlen(input);
+ return retval;
+}
+
+void check_alias_list(const CKM::AliasVector& expected)
+{
+ ckmc_alias_list_s *aliasList = NULL;
+ int ret = ckmc_get_data_alias_list(&aliasList);
+ RUNNER_ASSERT_MSG(ret == 0, "Failed to get the list of data aliases. " << ret << " / " << CKMCErrorToString(ret));
+
+ CKM::AliasVector actual;
+ ckmc_alias_list_s *plist = aliasList;
+ while(plist)
+ {
+ actual.push_back(plist->alias);
+ plist = plist->next;
+ }
+ ckmc_alias_list_all_free(aliasList);
+
+ RUNNER_ASSERT_MSG(expected == actual, "Actual list of aliases differ from expected list.");
+}
#pragma once
+#include <string>
#include <unordered_set>
#include <memory>
#include <ckm/ckm-type.h>
+#include <ckmc/ckmc-type.h>
#include <ckmc/ckmc-error.h>
#include <tests_common.h>
+#include <sys/types.h>
void switch_to_storage_user(const char* label);
void switch_to_storage_ocsp_user(const char* label);
CharPtr m_original_label;
};
+void save_data(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
+class ScopedSaveData
+{
+public:
+ ScopedSaveData(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
+ virtual ~ScopedSaveData();
+
+private:
+ std::string m_alias;
+};
+
+class GarbageCollector
+{
+public:
+ void save(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
+ virtual ~GarbageCollector();
+
+private:
+ struct save_item {
+ std::string item_alias;
+ std::string owner_label;
+ uid_t owner_uid;
+ gid_t owner_gid;
+ };
+ std::vector<save_item> m_garbage;
+};
+
+class ScopedDBUnlock
+{
+public:
+ ScopedDBUnlock(uid_t user_id, const char* passwd);
+ virtual ~ScopedDBUnlock();
+
+private:
+ uid_t m_uid;
+};
+
+void check_remove_allowed(const char* alias);
+void check_remove_denied(const char* alias);
+void check_remove_not_visible(const char* alias);
+void check_read(const char* alias, const char *label, const char *test_data, int expected_code = CKMC_ERROR_NONE);
+void check_read_allowed(const char* alias, const char *data);
+void check_read_not_visible(const char* alias);
+void allow_access(const char* alias, const char* accessor, int permissionMask);
+void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode);
+void deny_access(const char* alias, const char* accessor);
+void deny_access_negative(const char* alias, const char* accessor, int expectedCode);
+
void unlock_user_data(uid_t user_id, const char *passwd);
void remove_user_data(uid_t user_id);
void reset_user_data(uid_t user_id, const char *passwd);
+
+ckmc_raw_buffer_s prepare_message_buffer(const char * input);
+void check_alias_list(const CKM::AliasVector& expected);
RUNNER_CHILD_TEST(T1013_user_app_save_key)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T1022_app_user_save_keys_get_alias)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T1023_app_user_save_keys_exportable_flag)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T1032_app_user_save_bin_data)
{
- AccessProvider ap("mylabel");
+ ScopedAccessProvider ap("mylabel");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
auto manager = CKM::Manager::create();
CKM::AliasVector av;
- AccessProvider ap("mylabel-rsa");
+ ScopedAccessProvider ap("mylabel-rsa");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
auto manager = CKM::Manager::create();
CKM::AliasVector av;
- AccessProvider ap("mylabel-rsa");
+ ScopedAccessProvider ap("mylabel-rsa");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
auto manager = CKM::Manager::create();
CKM::AliasVector av;
- AccessProvider ap("mylabel-dsa");
+ ScopedAccessProvider ap("mylabel-dsa");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T1510_init_unlock_key)
{
int tmp;
- AccessProvider ap("my-label");
+ ScopedAccessProvider ap("my-label");
ap.allowAPI("key-manager::api-control", "rw");
ap.applyAndSwithToUser(USER_TEST, GROUP_APP);
RUNNER_CHILD_TEST(T1511_insert_data)
{
- AccessProvider ap("my-label");
+ ScopedAccessProvider ap("my-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST, GROUP_APP);
RUNNER_CHILD_TEST(T1519_deinit)
{
int tmp;
- AccessProvider ap("my-label");
+ ScopedAccessProvider ap("my-label");
ap.allowAPI("key-manager::api-control", "rw");
ap.applyAndSwithToUser(USER_APP, GROUP_APP);
RUNNER_CHILD_TEST(T1702_insert_data)
{
int temp;
- AccessProvider ap("t170-special-label");
+ ScopedAccessProvider ap("t170-special-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
RUNNER_CHILD_TEST(T1704_data_test)
{
int temp;
- AccessProvider ap("t170-special-label");
+ ScopedAccessProvider ap("t170-special-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
RUNNER_CHILD_TEST(T17102_prep_data_01)
{
int temp;
- AccessProvider ap("t1706-special-label");
+ ScopedAccessProvider ap("t1706-special-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
RUNNER_CHILD_TEST(T17103_prep_data_02)
{
int temp;
- AccessProvider ap("t1706-special-label2");
+ ScopedAccessProvider ap("t1706-special-label2");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
RUNNER_CHILD_TEST(T17104_prep_data_03)
{
int temp;
- AccessProvider ap("t1706-special-label");
+ ScopedAccessProvider ap("t1706-special-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
RUNNER_CHILD_TEST(T17105_prep_data_04)
{
int temp;
- AccessProvider ap("t1706-special-label2");
+ ScopedAccessProvider ap("t1706-special-label2");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
RUNNER_CHILD_TEST(T17107_check_data_01)
{
int temp;
- AccessProvider ap("t1706-special-label");
+ ScopedAccessProvider ap("t1706-special-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
RUNNER_CHILD_TEST(T17108_check_data_02)
{
int temp;
- AccessProvider ap("t1706-special-label2");
+ ScopedAccessProvider ap("t1706-special-label2");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
RUNNER_CHILD_TEST(T17110_check_data_03)
{
int temp;
- AccessProvider ap("t1706-special-label");
+ ScopedAccessProvider ap("t1706-special-label");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
RUNNER_CHILD_TEST(T17111_check_data_04)
{
int temp;
- AccessProvider ap("t1706-special-label2");
+ ScopedAccessProvider ap("t1706-special-label2");
ap.allowAPI("key-manager::api-storage", "rw");
ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
--- /dev/null
+/*
+ * Copyright (c) 2000 - 2015 Samsung Electronics Co.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ *
+ * @file system-db.cpp
+ * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
+ * @version 1.0
+ */
+#include <dpl/test/test_runner.h>
+#include <dpl/test/test_runner_child.h>
+#include <dpl/log/log.h>
+#include <tests_common.h>
+#include <ckm-common.h>
+#include <ckm/ckm-control.h>
+#include <ckmc/ckmc-manager.h>
+#include <ckmc/ckmc-type.h>
+#include <access_provider2.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+namespace
+{
+const uid_t USER_SERVICE = 0;
+const uid_t USER_SERVICE_2 = 1234;
+const uid_t GROUP_SERVICE_2 = 1234;
+const uid_t USER_SERVICE_MAX = 4999;
+const uid_t GROUP_SERVICE_MAX = 4999;
+const uid_t USER_SERVICE_FAIL = 5000;
+const uid_t GROUP_SERVICE_FAIL = 5000;
+const uid_t USER_APP = 5050;
+const uid_t GROUP_APP = 5050;
+const char* APP_PASS = "user-pass";
+
+const char* TEST_ALIAS = "test-alias";
+const char* SYSTEM_LABEL = "/";
+const char* TEST_SYSTEM_ALIAS = "/ test-alias";
+const char* TEST_SYSTEM_ALIAS_2 = "/ test-alias-2";
+const char* TEST_LABEL = "test-label";
+const char* TEST_LABEL_2 = "test-label-2";
+
+const char* TEST_DATA =
+ "Lorem Ipsum. At vero eos et accusamus et iusto odio dignissimos ducimus "
+ "qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores "
+ "et quas molestias excepturi sint occaecati cupiditate non provident, "
+ "similique sunt in culpa qui officia deserunt mollitia animi, id est "
+ "laborum et dolorum fuga. ";
+}
+
+
+RUNNER_TEST_GROUP_INIT(T50_SYSTEM_DB);
+
+RUNNER_TEST(T5010_CLIENT_APP_LOCKED_PRIVATE_DB)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // add permission to the resource to a user app
+ // [test]
+ // switch to user app, leave DB locked
+ // try to access system DB item - expect fail
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+ allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
+ }
+}
+
+RUNNER_TEST(T5020_CLIENT_APP_ADD_TO_PRIVATE_DB)
+{
+ // [test]
+ // switch to user app, unlock DB
+ // when accessing private DB - owner==me
+ // try to write to private DB - expect success
+ // try to get item from private DB - expect success
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
+ check_read(TEST_ALIAS, TEST_LABEL, TEST_DATA);
+ }
+}
+
+RUNNER_TEST(T5031_CLIENT_APP_ACCESS_WITH_PERMISSION)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // add permission to the resource to a user app
+ // [test]
+ // switch to user app, unlock DB
+ // try to access the system item - expect success
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+ allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
+ }
+}
+
+RUNNER_TEST(T5032_CLIENT_APP_ACCESS_NO_PERMISSION)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // [test]
+ // switch to user app, unlock DB
+ // try to access the system item - expect fail
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
+ }
+}
+
+RUNNER_TEST(T5033_CLIENT_APP_PERMISSION_REMOVAL)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // add permission to the resource to a user app
+ // [test]
+ // switch to user app, unlock DB
+ // try to access the system item - expect success
+ // [prepare2]
+ // as system service, remove the item (expecting to remove permission)
+ // add item again, do not add permission
+ // [test2]
+ // switch to user app, unlock DB
+ // try to access the system item - expect fail
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+ allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
+ }
+
+ // [prepare2]
+ check_remove_allowed(TEST_SYSTEM_ALIAS);
+
+ // [test2]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
+ }
+}
+
+RUNNER_TEST(T5034_CLIENT_APP_SET_READ_ACCESS)
+{
+ // [test]
+ // switch to user app, unlock DB
+ // try to write to private DB - expect success
+ // try to write to system DB - expect fail
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ ScopedSaveData ssdsystem_user(TEST_ALIAS, TEST_DATA);
+ ScopedSaveData ssdsystem_system(TEST_SYSTEM_ALIAS, TEST_DATA, CKMC_ERROR_PERMISSION_DENIED);
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
+ }
+}
+
+RUNNER_TEST(T5035_CLIENT_APP_TRY_REMOVING_SYSTEM_ITEM)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // add permission to the resource to a user app
+ // [test]
+ // switch to user app, unlock DB
+ // try to remove item from system DB - expect fail
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+ allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ check_remove_denied(TEST_SYSTEM_ALIAS);
+ }
+}
+
+RUNNER_TEST(T5036_CLIENT_LIST_ACCESSIBLE_ITEMS)
+{
+ // [prepare]
+ // start as system service
+ // add data A to the system DB
+ // add data B to the system DB
+ // add permission to data A to a user app
+ // [test]
+ // system service list items - expect both items to appear
+ // [test2]
+ // switch to user app, unlock DB
+ // add data as user
+ // user lists items - expect system item A and private item
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+ gc.save(TEST_SYSTEM_ALIAS_2, TEST_DATA);
+ allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
+
+ // [test]
+ check_alias_list({TEST_SYSTEM_ALIAS, TEST_SYSTEM_ALIAS_2});
+
+ // [test2]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+ ScopedSaveData user_data(TEST_ALIAS, TEST_DATA);
+
+ check_alias_list({TEST_SYSTEM_ALIAS,
+ aliasWithLabel(TEST_LABEL, TEST_ALIAS)});
+ }
+}
+
+RUNNER_TEST(T5037_CLIENT_APP_TRY_GENERATE_KEY_IN_SYSTEM_DB)
+{
+ // [test]
+ // switch to user app, unlock DB
+ // try to generate a key in system DB - expect fail
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ const char *private_key_alias = "/ sys-db-priv";
+ const char *public_key_alias = "/ sys-db-pub";
+ ckmc_policy_s policy_private_key;
+ ckmc_policy_s policy_public_key;
+ policy_private_key.password = NULL;
+ policy_private_key.extractable = 1;
+ policy_public_key.password = NULL;
+ policy_public_key.extractable = 1;
+ int temp;
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_PERMISSION_DENIED ==
+ (temp = ckmc_create_key_pair_rsa(1024,
+ private_key_alias,
+ public_key_alias,
+ policy_private_key,
+ policy_public_key)),
+ CKMCReadableError(temp));
+ }
+}
+
+RUNNER_TEST(T5038_CLIENT_SERVER_CREATE_VERIFY_SYSTEM_DB)
+{
+ // [prepare]
+ // start as system service
+ // generate RSA key in system DB
+ // [test]
+ // try to create and verify signature in system DB - expect success
+ // [test2]
+ // switch to user app, unlock DB
+ // try to create signature in system DB - expect fail
+
+ // [prepare]
+ const char *private_key_alias = "/ sys-db-priv";
+ const char *public_key_alias = "/ sys-db-pub";
+ ckmc_policy_s policy_private_key;
+ ckmc_policy_s policy_public_key;
+ policy_private_key.password = NULL;
+ policy_private_key.extractable = 1;
+ policy_public_key.password = NULL;
+ policy_public_key.extractable = 1;
+ int temp;
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_NONE ==
+ (temp = ckmc_create_key_pair_rsa(1024,
+ private_key_alias,
+ public_key_alias,
+ policy_private_key,
+ policy_public_key)),
+ CKMCReadableError(temp));
+
+ // [test]
+ {
+ ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
+ ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
+ ckmc_raw_buffer_s *signature;
+ ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
+
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_NONE == (temp = ckmc_create_signature(
+ private_key_alias,
+ NULL,
+ msg_buff,
+ hash_algo,
+ pad_algo,
+ &signature)),
+ CKMCReadableError(temp));
+
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
+ public_key_alias,
+ NULL,
+ msg_buff,
+ *signature,
+ hash_algo,
+ pad_algo)),
+ CKMCReadableError(temp));
+ }
+
+ // [test2]
+ {
+ ScopedAccessProvider ap(TEST_LABEL);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_APP, GROUP_APP);
+ ScopedDBUnlock unlock(USER_APP, APP_PASS);
+
+ ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
+ ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
+ ckmc_raw_buffer_s *signature;
+ ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
+
+ RUNNER_ASSERT_MSG(
+ CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_create_signature(
+ private_key_alias,
+ NULL,
+ msg_buff,
+ hash_algo,
+ pad_algo,
+ &signature)),
+ CKMCReadableError(temp));
+ }
+}
+
+RUNNER_TEST(T5039_SYSTEM_APP_SET_REMOVE_ACCESS)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // [test]
+ // add remove permission to a user app - expect fail
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+
+ // [test]
+ allow_access_negative(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_REMOVE, CKMC_ERROR_INVALID_PARAMETER);
+}
+
+RUNNER_TEST(T5040_SYSTEM_SVC_ACCESS_DB)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // [test]
+ // try to access the item - expect success
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+
+ // [test]
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
+}
+
+RUNNER_TEST(T5041_SYSTEM_SVC_1234_ACCESS_DB)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // [test]
+ // switch to another system service
+ // try to access the item - expect success
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL_2);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_SERVICE_2, GROUP_SERVICE_2);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
+ }
+}
+
+RUNNER_TEST(T5042_SYSTEM_SVC_1234_ADD_ITEM_TO_DB)
+{
+ // [prepare]
+ // start as system service 1234
+ // add resource to the system DB
+ // [test]
+ // switch to another system service
+ // try to access the item - expect success
+
+ // [prepare]
+ {
+ ScopedAccessProvider ap(TEST_LABEL_2);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_SERVICE_2, GROUP_SERVICE_2);
+
+ // [test]
+ ScopedSaveData ssd(TEST_SYSTEM_ALIAS, TEST_DATA);
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
+ }
+}
+
+RUNNER_TEST(T5043_SYSTEM_SVC_4999_ACCESS_DB)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // [test]
+ // switch to system service having uid maximum for system svcs
+ // try to access the item - expect success
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL_2);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_SERVICE_MAX, GROUP_SERVICE_MAX);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
+ }
+}
+
+RUNNER_TEST(T5044_SYSTEM_SVC_5000_ACCESS_DB)
+{
+ // [prepare]
+ // start as system service
+ // add resource to the system DB
+ // [test]
+ // switch to another, faulty system service with user-land uid==5000
+ // try to access the item - expect fail (no system service)
+
+ // [prepare]
+ GarbageCollector gc;
+ gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
+
+ // [test]
+ {
+ ScopedAccessProvider ap(TEST_LABEL_2);
+ ap.allowAPI("key-manager::api-storage", "rw");
+ ap.applyAndSwithToUser(USER_SERVICE_FAIL, GROUP_SERVICE_FAIL);
+
+ check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
+ }
+}