E2EE: Require e2ee-tests to be ran by default user
[platform/core/test/security-tests.git] / src / ckm / ckm-common.cpp
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.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <cstdlib>
25 #include <string>
26 #include <fstream>
27 #include <sys/smack.h>
28 #include <ckm/ckm-type.h>
29 #include <ckmc/ckmc-type.h>
30 #include <ckm-common.h>
31 #include <tests_common.h>
32 #include <ckm/ckm-control.h>
33 #include <ckm/ckm-manager.h>
34 #include <ckmc/ckmc-control.h>
35 #include <ckmc/ckmc-manager.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <unordered_map>
39 #include <tzplatform_config.h>
40
41 const std::string SMACK_USER_APP_PREFIX = "User::Pkg::";
42 const char *SYSTEM_LABEL = ckmc_owner_id_system;
43 const char *TEST_LABEL = "test_label";
44 const char *TEST_LABEL_2 = "test_label_2";
45 const char *TEST_LABEL_3 = "test_label_3";
46 const char *TEST_LABEL_4 = "test_label_4";
47 const char *TEST_LABEL_5 = "test_label_5";
48
49 void generate_random(size_t random_bytes, char *output)
50 {
51     RUNNER_ASSERT(random_bytes>0 && output);
52
53     std::ifstream is("/dev/urandom", std::ifstream::binary);
54     RUNNER_ASSERT_MSG(is, "Failed to read /dev/urandom");
55     is.read(output, random_bytes);
56     if(static_cast<std::streamsize>(random_bytes) != is.gcount()) {
57         RUNNER_ASSERT_MSG(false,
58                           "Not enough bytes read from /dev/urandom: " << random_bytes << "!=" <<
59                           is.gcount());
60     }
61 }
62
63 std::string getLabel() {
64     int ret;
65     char* myLabel = NULL;
66     RUNNER_ASSERT_MSG(0 <= (ret = smack_new_label_from_self(&myLabel)),
67                          "Failed to get smack label for self. Error: " << ret);
68     RUNNER_ASSERT_MSG(myLabel, "NULL smack label");
69     std::string result = myLabel;
70     free(myLabel);
71     return result;
72 }
73
74 std::string getOwnerIdFromSelf() {
75     const std::string& prefix = SMACK_USER_APP_PREFIX;
76     std::string smack = getLabel();
77     if (0 == smack.compare(0, prefix.size(), prefix))
78         return smack.substr(prefix.size(), std::string::npos);
79     return "/" + smack;
80 }
81
82 std::string aliasWithLabel(const char *label, const char *alias)
83 {
84     if(label)
85     {
86         std::stringstream ss;
87         ss << label << std::string(ckmc_label_name_separator) << alias;
88         return ss.str();
89     }
90     return std::string(alias);
91 }
92
93 std::string aliasWithLabelFromSelf(const char *alias)
94 {
95     std::ostringstream oss;
96     oss << getOwnerIdFromSelf() << ckmc_label_name_separator << alias;
97
98     return oss.str();
99 }
100
101 #define ERRORDESCRIBE(name) case name: return #name
102 const char * CKMCErrorToString(int error) {
103     switch(error) {
104         ERRORDESCRIBE(CKMC_ERROR_NONE);
105         ERRORDESCRIBE(CKMC_ERROR_INVALID_PARAMETER);
106         ERRORDESCRIBE(CKMC_ERROR_OUT_OF_MEMORY);
107         ERRORDESCRIBE(CKMC_ERROR_PERMISSION_DENIED);
108         ERRORDESCRIBE(CKMC_ERROR_SOCKET);
109         ERRORDESCRIBE(CKMC_ERROR_BAD_REQUEST);
110         ERRORDESCRIBE(CKMC_ERROR_BAD_RESPONSE);
111         ERRORDESCRIBE(CKMC_ERROR_SEND_FAILED);
112         ERRORDESCRIBE(CKMC_ERROR_RECV_FAILED);
113         ERRORDESCRIBE(CKMC_ERROR_AUTHENTICATION_FAILED);
114         ERRORDESCRIBE(CKMC_ERROR_BUFFER_TOO_SMALL);
115         ERRORDESCRIBE(CKMC_ERROR_SERVER_ERROR);
116         ERRORDESCRIBE(CKMC_ERROR_DB_LOCKED);
117         ERRORDESCRIBE(CKMC_ERROR_DB_ERROR);
118         ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_EXISTS);
119         ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_UNKNOWN);
120         ERRORDESCRIBE(CKMC_ERROR_VERIFICATION_FAILED);
121         ERRORDESCRIBE(CKMC_ERROR_INVALID_FORMAT);
122         ERRORDESCRIBE(CKMC_ERROR_FILE_ACCESS_DENIED);
123         ERRORDESCRIBE(CKMC_ERROR_NOT_EXPORTABLE);
124         ERRORDESCRIBE(CKMC_ERROR_FILE_SYSTEM);
125         ERRORDESCRIBE(CKMC_ERROR_NOT_SUPPORTED);
126         ERRORDESCRIBE(CKMC_ERROR_UNKNOWN);
127         default: return "Error not defined";
128     }
129 }
130 #undef ERRORDESCRIBE
131
132 std::string CKMCReadableError(int error) {
133     std::string output("Error: ");
134     output += std::to_string(error);
135     output += " Description: ";
136     output += CKMCErrorToString(error);
137     return output;
138 }
139
140 void save_data(const char* alias, const char *data, size_t len, const char* password,
141                int expected_err, bool exportable)
142 {
143     RUNNER_ASSERT(alias);
144     RUNNER_ASSERT(data);
145
146     ckmc_raw_buffer_s buffer;
147     buffer.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data));
148     buffer.size = len;
149     ckmc_policy_s policy;
150     policy.password = const_cast<char*>(password);
151     policy.extractable = exportable;
152     int ret = ckmc_save_data(alias, buffer, policy);
153     RUNNER_ASSERT_MSG(expected_err == ret, "Saving data failed. "
154                       << CKMCErrorToString(ret) << " while expected: "
155                       << CKMCErrorToString(expected_err));
156 }
157
158 void save_data(const char* alias, const char *data, int expected_err, bool exportable)
159 {
160     save_data(alias, data, strlen(data), nullptr, expected_err, exportable);
161 }
162
163 void save_data(const char* alias, const char *data, size_t len, int expected_err, bool exportable)
164 {
165     save_data(alias, data, len, nullptr, expected_err, exportable);
166 }
167
168 ScopedSaveData::ScopedSaveData(const char* alias, const char *data, int expected_err) : m_alias(alias)
169 {
170     save_data(alias, data, expected_err);
171 }
172
173 ScopedSaveData::~ScopedSaveData()
174 {
175     /*
176      * Let it throw. If we can't remove data then remaining tests results will be
177      * unreliable anyway.
178      */
179     check_remove_allowed(m_alias.c_str());
180 }
181
182 ScopedDBUnlock::ScopedDBUnlock(uid_t user_id, const char* passwd) : m_uid(user_id)
183 {
184     int temp;
185     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(user_id, passwd)), CKMCErrorToString(temp));
186 }
187 ScopedDBUnlock::~ScopedDBUnlock()
188 {
189     int temp;
190     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(m_uid)), CKMCErrorToString(temp));
191 }
192
193 void check_remove_allowed(const char* alias)
194 {
195     int ret = ckmc_remove_alias(alias);
196     // remove, but ignore non existing
197     RUNNER_ASSERT_MSG((CKMC_ERROR_NONE == ret) || (CKMC_ERROR_DB_ALIAS_UNKNOWN == ret),
198                          "Removing data failed: " << CKMCErrorToString(ret));
199 }
200
201 void check_remove_denied(const char* alias)
202 {
203     int ret = ckmc_remove_alias(alias);
204     RUNNER_ASSERT_MSG(
205             CKMC_ERROR_PERMISSION_DENIED == ret,
206             "App with different label shouldn't have rights to remove this data. "
207             << CKMCReadableError(ret));
208 }
209
210 void check_remove_not_visible(const char* alias)
211 {
212     int ret = ckmc_remove_alias(alias);
213     RUNNER_ASSERT_MSG(
214             CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
215             "App with different label shouldn't have rights to see this data. "
216             << CKMCReadableError(ret));
217 }
218
219 void check_read(const char* alias,
220                 const char *label,
221                 const char *test_data,
222                 size_t len,
223                 int expected_code)
224 {
225     ckmc_raw_buffer_s* buffer = NULL;
226     int ret = ckmc_get_data(aliasWithLabel(label, alias).c_str(), NULL, &buffer);
227     RUNNER_ASSERT_MSG(expected_code == ret, "Getting data failed. "
228                       "Expected " << CKMCErrorToString(expected_code) << ", "
229                       "while result " << CKMCErrorToString(ret));
230
231     if(expected_code == CKMC_ERROR_NONE)
232     {
233         // compare data with expected
234         RUNNER_ASSERT_MSG(
235                 buffer->size == len,
236                 "Extracted data length do not match expected data length (encrypted?):" <<
237                 buffer->size << "!=" << len);
238
239         RUNNER_ASSERT_MSG(
240                 memcmp(const_cast<const char*>(reinterpret_cast<char*>(buffer->data)),
241                        test_data, buffer->size) == 0,
242                 "Extracted data do not match expected data (encrypted?).");
243
244         ckmc_buffer_free(buffer);
245     }
246 }
247
248 void check_read(const char* alias, const char *label, const char *test_data, int expected_code)
249 {
250     check_read(alias, label, test_data, strlen(test_data), expected_code);
251 }
252
253 void check_read_allowed(const char* alias, const char *data)
254 {
255     // try to read previously saved data - label taken implicitly
256     check_read(alias, NULL, data);
257 }
258
259 void check_read_not_visible(const char* alias)
260 {
261     // try to read previously saved data - label taken implicitly
262     {
263         ckmc_raw_buffer_s* buffer = NULL;
264         int ret = ckmc_get_data(alias, NULL, &buffer);
265         RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
266                             "App with different label shouldn't have rights to see this data. " << CKMCErrorToString(ret));
267         ckmc_buffer_free(buffer);
268     }
269 }
270
271 void check_key(const char *alias, int expected_error, ckmc_key_type_e expected_type)
272 {
273     ckmc_key_s *test_key = NULL;
274     int temp = ckmc_get_key(alias, 0, &test_key);
275     RUNNER_ASSERT_MSG(
276             expected_error == temp,
277             "received: " << CKMCReadableError(temp) << " while expected: " << CKMCReadableError(expected_error));
278     if(expected_type != CKMC_KEY_NONE)
279     {
280         RUNNER_ASSERT_MSG(
281                 test_key->key_type == expected_type,
282                 "received type: " << test_key->key_type << " while expected type: " << expected_type);
283     }
284     ckmc_key_free(test_key);
285 }
286 void check_key_allowed(const char *alias, ckmc_key_type_e expected_type)
287 {
288     check_key(alias, CKMC_ERROR_NONE, expected_type);
289 }
290 void check_key_not_visible(const char *alias)
291 {
292     check_key(alias, CKMC_ERROR_DB_ALIAS_UNKNOWN);
293 }
294 void check_cert_allowed(const char *alias)
295 {
296     ckmc_cert_s *test_cert = NULL;
297     int temp = ckmc_get_cert(alias, 0, &test_cert);
298     ckmc_cert_free(test_cert);
299     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == temp, CKMCReadableError(temp));
300
301 }
302 void check_cert_not_visible(const char *alias)
303 {
304     ckmc_cert_s *test_cert = NULL;
305     int temp = ckmc_get_cert(alias, 0, &test_cert);
306     ckmc_cert_free(test_cert);
307     RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == temp,
308                       "App with different label shouldn't have rights to see this cert. " << CKMCErrorToString(temp));
309 }
310
311 void allow_access(const char* alias, const char* accessor, int permissionMask)
312 {
313     // data removal should revoke this access
314     int ret = ckmc_set_permission(alias, accessor, permissionMask);
315     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Trying to allow access returned: "
316                         << CKMCErrorToString(ret));
317 }
318
319 void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode)
320 {
321     // data removal should revoke this access
322     int ret = ckmc_set_permission(alias, accessor, permissionMask);
323     RUNNER_ASSERT_MSG(expectedCode == ret, "Trying to allow access returned "
324                         << CKMCErrorToString(ret) << ", while expected: "
325                         << CKMCErrorToString(expectedCode));
326 }
327
328 void deny_access(const char* alias, const char* accessor)
329 {
330     int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
331     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == ret, "Denying access failed. Error: "
332                         << CKMCErrorToString(ret));
333 }
334
335 void deny_access_negative(const char* alias, const char* accessor, int expectedCode)
336 {
337     int ret = ckmc_set_permission(alias, accessor, CKMC_PERMISSION_NONE);
338     RUNNER_ASSERT_MSG(expectedCode == ret, "Denying access failed. "
339                         << CKMCErrorToString(ret) << ", while expected: "
340                         << CKMCErrorToString(expectedCode));
341 }
342
343 void unlock_user_data(uid_t user_id, const char *passwd)
344 {
345     int ret;
346     auto control = CKM::Control::create();
347     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->unlockUserKey(user_id, passwd)),
348                       "Error=" << CKM::APICodeToString(ret));
349 }
350
351 void remove_user_data(uid_t user_id)
352 {
353     auto control = CKM::Control::create();
354     control->lockUserKey(user_id);
355     control->removeUserData(user_id);
356 }
357
358 void reset_user_data(uid_t user_id, const char *passwd)
359 {
360     remove_user_data(user_id);
361     unlock_user_data(user_id, passwd);
362 }
363
364 ckmc_raw_buffer_s prepare_message_buffer(const char * input)
365 {
366     ckmc_raw_buffer_s retval;
367     retval.data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(input));
368     retval.size = strlen(input);
369     return retval;
370 }
371
372 void check_alias_list(const CKM::AliasVector& expected)
373 {
374     ckmc_alias_list_s *aliasList = NULL;
375     int ret = ckmc_get_data_alias_list(&aliasList);
376     RUNNER_ASSERT_MSG(ret == 0, "Failed to get the list of data aliases. " << ret << " / " << CKMCErrorToString(ret));
377
378     CKM::AliasVector actual;
379     ckmc_alias_list_s *plist = aliasList;
380     while(plist)
381     {
382         actual.push_back(plist->alias);
383         plist = plist->next;
384     }
385     ckmc_alias_list_all_free(aliasList);
386
387     RUNNER_ASSERT_MSG(expected == actual, "Actual list of aliases differ from expected list.");
388 }
389
390 void check_alias_info_list_helper(const CKM::AliasPwdVector& expected, const CKM::AliasPwdVector& actual,
391         const std::string &userSmackLabel)
392 {
393     std::string errorLogMsg;
394     std::unordered_map<std::string, bool> aliasPwdMap;
395
396     RUNNER_ASSERT_MSG(expected.size() == actual.size(), "Aliases item count differs, expected: " <<
397         expected.size() << " actual: " << actual.size());
398
399     for (const auto &it : actual)
400     {
401         aliasPwdMap[std::get<0>(it)] = std::get<1>(it);
402     }
403
404
405     for (const auto &it : expected)
406     {
407         auto aliasPwd = aliasPwdMap.find(userSmackLabel + std::get<0>(it));
408         if (aliasPwd != aliasPwdMap.end()) {
409             if (aliasPwd->second != std::get<1>(it)) {
410                 errorLogMsg += "Alias: " + std::get<0>(it) + " has wrong encryption status: "
411                                 + std::to_string(std::get<1>(it)) + "\n";
412             }
413         }
414         else {
415             errorLogMsg += "Expected alias: " + std::get<0>(it) + " not found.\n";
416         }
417     }
418
419     if (!errorLogMsg.empty()) {
420         for (const auto &it : actual)
421         {
422             errorLogMsg += "Actual alias: " + std::get<0>(it) + " status: "
423                 + std::to_string(std::get<1>(it)) + "\n";
424         }
425         RUNNER_FAIL_MSG("Actual list of aliases differ from expected list.\n" + errorLogMsg);
426     }
427 }
428
429 void check_alias_info_list(const CKM::AliasPwdVector& expected)
430 {
431     ckmc_alias_info_list_s *aliasInfoList = NULL;
432     int ret = ckmc_get_data_alias_info_list(&aliasInfoList);
433     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get the list of data aliases. " << ret << " / "
434                       << CKMCErrorToString(ret));
435
436     CKM::AliasPwdVector actual;
437     ckmc_alias_info_list_s *plist = aliasInfoList;
438     char* alias;
439     bool isPasswordProtected;
440     unsigned int it = 0;
441     while (plist)
442     {
443         ret = ckmc_alias_info_get_alias(plist->info, &alias);
444         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get alias. " << ret << " / "
445                           << CKMCErrorToString(ret));
446         ret = ckmc_alias_info_is_password_protected(plist->info, &isPasswordProtected);
447         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get password protection status" << ret << " / "
448                           << CKMCErrorToString(ret));
449         RUNNER_ASSERT_MSG(alias != nullptr, "Got null alias. Iterator: " << it);
450         actual.push_back(std::make_pair(alias, isPasswordProtected));
451         plist = plist->next;
452         it++;
453     }
454     ckmc_alias_info_list_all_free(aliasInfoList);
455     check_alias_info_list_helper(expected, actual);
456 }
457
458 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count)
459 {
460     ckmc_alias_list_s *aliasList = NULL;
461     int ec;
462     switch(type)
463     {
464         case ALIAS_KEY:
465             ec = ckmc_get_key_alias_list(&aliasList);
466             break;
467
468         case ALIAS_CERT:
469             ec = ckmc_get_cert_alias_list(&aliasList);
470             break;
471
472         case ALIAS_DATA:
473             ec = ckmc_get_data_alias_list(&aliasList);
474             break;
475         default:
476             RUNNER_ASSERT_MSG(false, "Unsupported value ALIAS_KEY == " << (int)type);
477     }
478
479     if(ec == CKMC_ERROR_DB_ALIAS_UNKNOWN)
480         return 0;
481
482     RUNNER_ASSERT_MSG(ec == CKMC_ERROR_NONE,
483                       "Error: alias list failed, ec: " << CKMCErrorToString(ec));
484
485     ckmc_alias_list_s *plist = aliasList;
486     size_t return_count = 0;
487     while(plist)
488     {
489         plist = plist->next;
490         return_count ++;
491     }
492     ckmc_alias_list_all_free(aliasList);
493
494     RUNNER_ASSERT_MSG(
495       return_count >= minimum_initial_element_count,
496       "Error: alias list failed, current element count: " << return_count <<
497       " while expected minimal count of " << minimum_initial_element_count <<
498       " elements");
499
500     return return_count;
501 }
502
503 std::string sharedDatabase(const CKM::Alias & alias)
504 {
505     return aliasWithLabel(ckmc_owner_id_system, alias.c_str());
506 }
507
508 ckmc_raw_buffer_s* createRandomBufferCAPI(size_t random_bytes)
509 {
510     ckmc_raw_buffer_s* buffer = NULL;
511     char* data = static_cast<char*>(malloc(random_bytes*sizeof(char)));
512     RUNNER_ASSERT(data);
513     generate_random(random_bytes, data);
514     int ret = ckmc_buffer_new(reinterpret_cast<unsigned char*>(data), random_bytes, &buffer);
515     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Buffer creation failed: " << CKMCErrorToString(ret));
516     return buffer;
517 }
518
519 CKM::RawBuffer createRandomBuffer(size_t random_bytes)
520 {
521     char buffer[random_bytes];
522     generate_random(random_bytes, buffer);
523     return CKM::RawBuffer(buffer, buffer + random_bytes);
524 }
525
526 ckmc_key_s *generate_AES_key(size_t lengthBits, const char *passwd)
527 {
528     ckmc_key_s *retval = reinterpret_cast<ckmc_key_s *>(malloc(sizeof(ckmc_key_s)));
529     RUNNER_ASSERT(retval != NULL);
530
531     RUNNER_ASSERT(lengthBits%8 == 0);
532     char *char_key_AES = reinterpret_cast<char*>(malloc(lengthBits/8));
533     RUNNER_ASSERT(char_key_AES != NULL);
534     generate_random(lengthBits/8, char_key_AES);
535
536     retval->raw_key  = reinterpret_cast<unsigned char *>(char_key_AES);
537     retval->key_size = lengthBits/8;
538     retval->key_type = CKMC_KEY_AES;
539     retval->password = passwd?strdup(passwd):NULL;
540
541     return retval;
542 }
543
544 void validate_AES_key(ckmc_key_s *analyzed)
545 {
546     RUNNER_ASSERT_MSG(analyzed, "provided key is NULL");
547     RUNNER_ASSERT_MSG(analyzed->raw_key != NULL, "provided key is empty");
548     RUNNER_ASSERT_MSG(analyzed->key_size==(128/8) ||
549                       analyzed->key_size==(192/8) ||
550                       analyzed->key_size==(256/8), "provided key length is invalid");
551     RUNNER_ASSERT_MSG(analyzed->key_type = CKMC_KEY_AES, "expected AES key, while got: " << analyzed->key_type);
552 }
553
554 void compare_AES_keys(ckmc_key_s *first, ckmc_key_s *second)
555 {
556     validate_AES_key(first);
557     validate_AES_key(second);
558     RUNNER_ASSERT_MSG(
559         (first->key_size==second->key_size) &&
560         (memcmp(first->raw_key, second->raw_key, first->key_size)==0),
561         "data has been modified in key manager");
562     // bypassing password intentionally
563 }
564
565 ParamListPtr createParamListPtr()
566 {
567     ckmc_param_list_h list = NULL;
568     assert_positive(ckmc_param_list_new, &list);
569     return ParamListPtr(list, ckmc_param_list_free);
570 }
571
572 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
573 {
574     int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
575     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
576                       "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
577 }
578
579 void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer)
580 {
581     int ret = ckmc_param_list_set_integer(params.get(), name, integer);
582     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
583                       "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
584 }
585
586 void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal)
587 {
588     if(equal) {
589         RUNNER_ASSERT_MSG(b1->size == b2->size,
590                           "Buffer size differs: " << b1->size << "!=" << b2->size);
591         RUNNER_ASSERT_MSG(0 == memcmp(b1->data, b2->data, b1->size), "Buffer contents differ");
592     } else {
593         RUNNER_ASSERT_MSG(b1->size != b2->size || 0 != memcmp(b1->data, b2->data, b1->size),
594                           "Buffers should be different");
595     }
596 }
597
598 RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer)
599 {
600     return RawBufferPtr(buffer, ckmc_buffer_free);
601 }
602
603 CKM::Policy generate_ckm_policy(int iterator_nr) {
604     if (iterator_nr % 2) { // policy with password and with / without extractable flag
605         return CKM::Policy(CKM::Password("test_pwd"), iterator_nr % 4);
606     }
607     return CKM::Policy();
608 }
609
610 void require_default_user(char *argv[])
611 {
612     uid_t expected_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
613     if (expected_uid != geteuid()) {
614         std::string userStr("owner");
615         const char* user = tzplatform_getenv(TZ_SYS_DEFAULT_USER);
616         if (user)
617             userStr = user;
618
619         std::cerr << argv[0] << " should be executed as " << userStr << ". Aborting" << std::endl;
620         exit(-1);
621     }
622 }