CKM: Check backend info
[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
149 struct Info {
150     Info(const CKM::Alias &alias,
151          bool passwordProtected,
152          CKM::BackendId backend = CKM::BackendId::SW) :
153         alias(alias),
154         passwordProtected(passwordProtected),
155         backend(backend) {}
156
157     CKM::Alias alias;
158     bool passwordProtected;
159     CKM::BackendId backend;
160 };
161 typedef std::vector<Info> InfoVector;
162 typedef std::unordered_map<std::string, Info> InfoMap;
163
164 CKM::BackendId backend();
165
166 void check_alias_info_list_helper(const InfoVector& expected,
167                                   const InfoMap& actual,
168                                   const std::string &userSmackLabel = {});
169 void check_alias_info_list(const InfoVector& expected);
170
171 typedef enum {
172     ALIAS_KEY,
173     ALIAS_CERT,
174     ALIAS_DATA
175 } alias_type_;
176 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count = 0);
177 std::string sharedDatabase(const CKM::Alias & alias);
178 CKM::RawBuffer createRandomBuffer(size_t random_bytes);
179 ckmc_raw_buffer_s* createRandomBufferCAPI(size_t random_bytes);
180
181 ckmc_key_s *generate_AES_key(size_t lengthBits, const char *passwd);
182 void validate_AES_key(ckmc_key_s *analyzed);
183 void compare_AES_keys(ckmc_key_s *first, ckmc_key_s *second); // true if equal
184 CKM::Policy generate_ckm_policy(int iterator_nr); // generates policy based on given number
185
186 // Test env class for database cleanup. Pass database uids to cleanup before and after test
187 template <uid_t ...Args>
188 class RemoveDataEnv;
189
190 template <>
191 class RemoveDataEnv<>
192 {
193 public:
194     void init(const std::string&)
195     {}
196     void finish()
197     {}
198 };
199
200 template <uid_t UID, uid_t ...Args>
201 class RemoveDataEnv<UID, Args...> : public RemoveDataEnv<Args...>
202 {
203 public:
204     void init(const std::string & str) {
205         remove_user_data(UID);
206         RemoveDataEnv<Args...>::init(str);
207     }
208     void finish() {
209         RemoveDataEnv<Args...>::finish();
210         remove_user_data(UID);
211     }
212 };
213
214 typedef std::shared_ptr<ckmc_raw_buffer_s> RawBufferPtr;
215 typedef std::shared_ptr<struct __ckmc_param_list> ParamListPtr;
216 typedef std::shared_ptr<struct __ckmc_cipher_ctx> CipherCtxPtr;
217
218 ParamListPtr createParamListPtr();
219 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer);
220 void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer);
221
222 void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal=true);
223
224 RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer);
225
226 CipherCtxPtr create_cipher_ctx(ckmc_cipher_ctx_h ctx);
227
228
229 template <typename F, typename... Args>
230 void test_no_observer(F&& func, Args... args)
231 {
232     CKM::ManagerAsync::ObserverPtr obs;
233     CKM::ManagerAsync mgr;
234
235     try {
236         (mgr.*func)(obs, args...);
237         RUNNER_ASSERT_MSG(false, "function() should have thrown an exception");
238     } catch (const std::invalid_argument& e) {
239         RUNNER_ASSERT(true);
240     } catch (...) {
241         RUNNER_ASSERT_MSG(false, "Unexpected exception");
242     }
243 }
244
245 class AliasRemover
246 {
247 public:
248     AliasRemover(const char* alias) : alias(alias) {}
249     ~AliasRemover() {
250         ckmc_remove_alias(alias);
251     }
252
253     AliasRemover(AliasRemover&& other) {
254         alias = other.alias;
255         other.alias = nullptr;
256     }
257
258     AliasRemover& operator=(AliasRemover&& other) {
259         if (&other == this)
260             return *this;
261
262         alias = other.alias;
263         other.alias = nullptr;
264     }
265
266 private:
267     const char* alias;
268 };
269
270 void require_default_user(char *argv[]);