c0578ac2ba76129d64f5fd7753ef02e1c569d2ec
[platform/core/test/security-tests.git] / src / ckm / ckm-common.h
1 /*
2  *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       ckm-common.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <string>
25 #include <memory>
26 #include <stdexcept>
27 #include <ckm/ckm-type.h>
28 #include <ckm/ckm-manager-async.h>
29 #include <ckmc/ckmc-type.h>
30 #include <ckmc/ckmc-error.h>
31 #include <ckmc/ckmc-manager.h>
32 #include <tests_common.h>
33 #include <sys/types.h>
34
35 extern const std::string SMACK_USER_APP_PREFIX;
36 extern const char *SYSTEM_LABEL;
37 extern const char *TEST_LABEL;
38 extern const char *TEST_LABEL_2;
39 extern const char *TEST_LABEL_3;
40 extern const char *TEST_LABEL_4;
41 extern const char *TEST_LABEL_5;
42
43 // support for error printing
44 const char * CKMCErrorToString(int error);
45 std::string CKMCReadableError(int error);
46
47 // RUNNER_ASSERT wrappers
48 template <typename F, typename... Args>
49 void assert_result(int expected, F&& func, Args... args)
50 {
51     int ret = func(args...);
52     RUNNER_ASSERT_MSG(ret == expected,
53                       "Expected: " << CKMCErrorToString(expected) << "(" << expected << ")"
54                       " got: " << CKMCErrorToString(ret) << "(" << ret << ")");
55 }
56
57 template <typename F, typename... Args>
58 void assert_positive(F&& func, Args... args)
59 {
60     assert_result(CKMC_ERROR_NONE, std::move(func), args...);
61 }
62
63 template <typename F, typename... Args>
64 void assert_invalid_param(F&& func, Args... args)
65 {
66     assert_result(CKMC_ERROR_INVALID_PARAMETER, std::move(func), args...);
67 }
68
69
70 // list operations
71 template <typename T>
72 size_t list_size(const T* list)
73 {
74     size_t size = 0;
75     while(list) {
76         list = list->next;
77         size++;
78     }
79     return size;
80 }
81
82 std::string getLabel();
83 // returns process owner id
84 std::string getOwnerIdFromSelf();
85
86 std::string aliasWithLabel(const char *label, const char *alias);
87
88 std::string aliasWithLabelFromSelf(const char *alias);
89
90 void save_data(const char* alias, const char *data, size_t len, const char* password,
91                int expected_err = CKMC_ERROR_NONE, bool exportable = true);
92 void save_data(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE,
93                bool exportable = true);
94 void save_data(const char* alias, const char *data, size_t len,
95                int expected_err = CKMC_ERROR_NONE, bool exportable = true);
96 class ScopedSaveData
97 {
98 public:
99     ScopedSaveData(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
100     virtual ~ScopedSaveData();
101
102 private:
103     std::string m_alias;
104 };
105
106 class ScopedDBUnlock
107 {
108 public:
109     ScopedDBUnlock(uid_t user_id, const char* passwd);
110     virtual ~ScopedDBUnlock();
111
112 private:
113     uid_t m_uid;
114 };
115
116 void check_remove_allowed(const char* alias);
117 void check_remove_denied(const char* alias);
118 void check_remove_not_visible(const char* alias);
119 void check_read(const char* alias,
120                 const char *label,
121                 const char *test_data,
122                 size_t len,
123                 int expected_code = CKMC_ERROR_NONE);
124 void check_read(const char* alias,
125                 const char *label,
126                 const char *test_data,
127                 int expected_code = CKMC_ERROR_NONE);
128 void check_read_allowed(const char* alias, const char *data);
129 void check_read_not_visible(const char* alias);
130 void check_key(const char *alias,
131                int expected_error = CKMC_ERROR_NONE,
132                ckmc_key_type_e expected_type = CKMC_KEY_NONE);
133 void check_key_allowed(const char *alias, ckmc_key_type_e expected_type = CKMC_KEY_NONE);
134 void check_key_not_visible(const char *alias);
135 void check_cert_allowed(const char *alias);
136 void check_cert_not_visible(const char *alias);
137 void allow_access(const char* alias, const char* accessor, int permissionMask);
138 void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode);
139 void deny_access(const char* alias, const char* accessor);
140 void deny_access_negative(const char* alias, const char* accessor, int expectedCode);
141
142 void unlock_user_data(uid_t user_id, const char *passwd);
143 void remove_user_data(uid_t user_id);
144 void reset_user_data(uid_t user_id, const char *passwd);
145
146 ckmc_raw_buffer_s prepare_message_buffer(const char * input);
147 void check_alias_list(const CKM::AliasVector& expected);
148 void check_alias_info_list_helper(const CKM::AliasInfoVector& expected,
149                                   const CKM::AliasInfoVector& actual,
150                                   const std::string &userSmackLabel = {});
151 std::pair<std::string, CKM::AliasInfo> make_alias_info(const std::string& alias, bool password);
152 void check_alias_info_list(const CKM::AliasInfoVector& expected);
153
154 typedef enum {
155     ALIAS_KEY,
156     ALIAS_CERT,
157     ALIAS_DATA
158 } alias_type_;
159 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count = 0);
160 std::string sharedDatabase(const CKM::Alias & alias);
161 CKM::RawBuffer createRandomBuffer(size_t random_bytes);
162 ckmc_raw_buffer_s* createRandomBufferCAPI(size_t random_bytes);
163
164 ckmc_key_s *generate_AES_key(size_t lengthBits, const char *passwd);
165 void validate_AES_key(ckmc_key_s *analyzed);
166 void compare_AES_keys(ckmc_key_s *first, ckmc_key_s *second); // true if equal
167 CKM::Policy generate_ckm_policy(int iterator_nr); // generates policy based on given number
168
169 // Test env class for database cleanup. Pass database uids to cleanup before and after test
170 template <uid_t ...Args>
171 class RemoveDataEnv;
172
173 template <>
174 class RemoveDataEnv<>
175 {
176 public:
177     void init(const std::string&)
178     {}
179     void finish()
180     {}
181 };
182
183 template <uid_t UID, uid_t ...Args>
184 class RemoveDataEnv<UID, Args...> : public RemoveDataEnv<Args...>
185 {
186 public:
187     void init(const std::string & str) {
188         remove_user_data(UID);
189         RemoveDataEnv<Args...>::init(str);
190     }
191     void finish() {
192         RemoveDataEnv<Args...>::finish();
193         remove_user_data(UID);
194     }
195 };
196
197 typedef std::shared_ptr<ckmc_raw_buffer_s> RawBufferPtr;
198 typedef std::shared_ptr<struct __ckmc_param_list> ParamListPtr;
199
200 ParamListPtr createParamListPtr();
201 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer);
202 void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer);
203
204 void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal=true);
205
206 RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer);
207
208
209 template <typename F, typename... Args>
210 void test_no_observer(F&& func, Args... args)
211 {
212     CKM::ManagerAsync::ObserverPtr obs;
213     CKM::ManagerAsync mgr;
214
215     try {
216         (mgr.*func)(obs, args...);
217         RUNNER_ASSERT_MSG(false, "function() should have thrown an exception");
218     } catch (const std::invalid_argument& e) {
219         RUNNER_ASSERT(true);
220     } catch (...) {
221         RUNNER_ASSERT_MSG(false, "Unexpected exception");
222     }
223 }
224
225 class AliasRemover
226 {
227 public:
228     AliasRemover(const char* alias) : alias(alias) {}
229     ~AliasRemover() {
230         ckmc_remove_alias(alias);
231     }
232
233     AliasRemover(AliasRemover&& other) {
234         alias = other.alias;
235         other.alias = nullptr;
236     }
237
238     AliasRemover& operator=(AliasRemover&& other) {
239         if (&other == this)
240             return *this;
241
242         alias = other.alias;
243         other.alias = nullptr;
244     }
245
246 private:
247     const char* alias;
248 };
249
250 void require_default_user(char *argv[]);