7a584ba36770b985698877b763bb3cc2e01f1a7a
[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 PasswordInfoVector& expected,
391                                   const std::unordered_map<std::string, bool>& actual,
392                                   const std::string &userSmackLabel)
393 {
394     std::string errorLogMsg;
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 : expected)
400     {
401         auto aliasPwd = actual.find(userSmackLabel + it.alias);
402         if (aliasPwd != actual.end()) {
403             if (aliasPwd->second != it.passwordProtected) {
404                 errorLogMsg += "Alias: " + it.alias + " has wrong encryption status: "
405                                 + std::to_string(it.passwordProtected) + "\n";
406             }
407         }
408         else {
409             errorLogMsg += "Expected alias: " + it.alias + " not found.\n";
410         }
411     }
412
413     if (!errorLogMsg.empty()) {
414         for (const auto& [alias, passwordProtected] : actual)
415         {
416             errorLogMsg += "Actual alias: " + alias + " status: "
417                 + std::to_string(passwordProtected) + "\n";
418         }
419         RUNNER_FAIL_MSG("Actual list of aliases differ from expected list.\n" + errorLogMsg);
420     }
421 }
422
423 void check_alias_info_list(const PasswordInfoVector& expected)
424 {
425     ckmc_alias_info_list_s *aliasInfoList = NULL;
426     int ret = ckmc_get_data_alias_info_list(&aliasInfoList);
427     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get the list of data aliases. " << ret << " / "
428                       << CKMCErrorToString(ret));
429
430     std::unordered_map<std::string, bool> actual;
431     ckmc_alias_info_list_s *plist = aliasInfoList;
432     char* alias;
433     bool isPasswordProtected;
434     unsigned int it = 0;
435     while (plist)
436     {
437         ret = ckmc_alias_info_get_alias(plist->info, &alias);
438         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get alias. " << ret << " / "
439                           << CKMCErrorToString(ret));
440         ret = ckmc_alias_info_is_password_protected(plist->info, &isPasswordProtected);
441         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get password protection status" << ret << " / "
442                           << CKMCErrorToString(ret));
443         RUNNER_ASSERT_MSG(alias != nullptr, "Got null alias. Iterator: " << it);
444
445         actual[alias] = isPasswordProtected;
446         plist = plist->next;
447         it++;
448     }
449     ckmc_alias_info_list_all_free(aliasInfoList);
450     check_alias_info_list_helper(expected, actual);
451 }
452
453 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count)
454 {
455     ckmc_alias_list_s *aliasList = NULL;
456     int ec;
457     switch(type)
458     {
459         case ALIAS_KEY:
460             ec = ckmc_get_key_alias_list(&aliasList);
461             break;
462
463         case ALIAS_CERT:
464             ec = ckmc_get_cert_alias_list(&aliasList);
465             break;
466
467         case ALIAS_DATA:
468             ec = ckmc_get_data_alias_list(&aliasList);
469             break;
470         default:
471             RUNNER_ASSERT_MSG(false, "Unsupported value ALIAS_KEY == " << (int)type);
472     }
473
474     if(ec == CKMC_ERROR_DB_ALIAS_UNKNOWN)
475         return 0;
476
477     RUNNER_ASSERT_MSG(ec == CKMC_ERROR_NONE,
478                       "Error: alias list failed, ec: " << CKMCErrorToString(ec));
479
480     ckmc_alias_list_s *plist = aliasList;
481     size_t return_count = 0;
482     while(plist)
483     {
484         plist = plist->next;
485         return_count ++;
486     }
487     ckmc_alias_list_all_free(aliasList);
488
489     RUNNER_ASSERT_MSG(
490       return_count >= minimum_initial_element_count,
491       "Error: alias list failed, current element count: " << return_count <<
492       " while expected minimal count of " << minimum_initial_element_count <<
493       " elements");
494
495     return return_count;
496 }
497
498 std::string sharedDatabase(const CKM::Alias & alias)
499 {
500     return aliasWithLabel(ckmc_owner_id_system, alias.c_str());
501 }
502
503 ckmc_raw_buffer_s* createRandomBufferCAPI(size_t random_bytes)
504 {
505     ckmc_raw_buffer_s* buffer = NULL;
506     char* data = static_cast<char*>(malloc(random_bytes*sizeof(char)));
507     RUNNER_ASSERT(data);
508     generate_random(random_bytes, data);
509     int ret = ckmc_buffer_new(reinterpret_cast<unsigned char*>(data), random_bytes, &buffer);
510     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Buffer creation failed: " << CKMCErrorToString(ret));
511     return buffer;
512 }
513
514 CKM::RawBuffer createRandomBuffer(size_t random_bytes)
515 {
516     char buffer[random_bytes];
517     generate_random(random_bytes, buffer);
518     return CKM::RawBuffer(buffer, buffer + random_bytes);
519 }
520
521 ckmc_key_s *generate_AES_key(size_t lengthBits, const char *passwd)
522 {
523     ckmc_key_s *retval = reinterpret_cast<ckmc_key_s *>(malloc(sizeof(ckmc_key_s)));
524     RUNNER_ASSERT(retval != NULL);
525
526     RUNNER_ASSERT(lengthBits%8 == 0);
527     char *char_key_AES = reinterpret_cast<char*>(malloc(lengthBits/8));
528     RUNNER_ASSERT(char_key_AES != NULL);
529     generate_random(lengthBits/8, char_key_AES);
530
531     retval->raw_key  = reinterpret_cast<unsigned char *>(char_key_AES);
532     retval->key_size = lengthBits/8;
533     retval->key_type = CKMC_KEY_AES;
534     retval->password = passwd?strdup(passwd):NULL;
535
536     return retval;
537 }
538
539 void validate_AES_key(ckmc_key_s *analyzed)
540 {
541     RUNNER_ASSERT_MSG(analyzed, "provided key is NULL");
542     RUNNER_ASSERT_MSG(analyzed->raw_key != NULL, "provided key is empty");
543     RUNNER_ASSERT_MSG(analyzed->key_size==(128/8) ||
544                       analyzed->key_size==(192/8) ||
545                       analyzed->key_size==(256/8), "provided key length is invalid");
546     RUNNER_ASSERT_MSG(analyzed->key_type = CKMC_KEY_AES, "expected AES key, while got: " << analyzed->key_type);
547 }
548
549 void compare_AES_keys(ckmc_key_s *first, ckmc_key_s *second)
550 {
551     validate_AES_key(first);
552     validate_AES_key(second);
553     RUNNER_ASSERT_MSG(
554         (first->key_size==second->key_size) &&
555         (memcmp(first->raw_key, second->raw_key, first->key_size)==0),
556         "data has been modified in key manager");
557     // bypassing password intentionally
558 }
559
560 ParamListPtr createParamListPtr()
561 {
562     ckmc_param_list_h list = NULL;
563     assert_positive(ckmc_param_list_new, &list);
564     return ParamListPtr(list, ckmc_param_list_free);
565 }
566
567 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
568 {
569     int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
570     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
571                       "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
572 }
573
574 void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer)
575 {
576     int ret = ckmc_param_list_set_integer(params.get(), name, integer);
577     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
578                       "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
579 }
580
581 void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal)
582 {
583     if(equal) {
584         RUNNER_ASSERT_MSG(b1->size == b2->size,
585                           "Buffer size differs: " << b1->size << "!=" << b2->size);
586         RUNNER_ASSERT_MSG(0 == memcmp(b1->data, b2->data, b1->size), "Buffer contents differ");
587     } else {
588         RUNNER_ASSERT_MSG(b1->size != b2->size || 0 != memcmp(b1->data, b2->data, b1->size),
589                           "Buffers should be different");
590     }
591 }
592
593 RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer)
594 {
595     return RawBufferPtr(buffer, ckmc_buffer_free);
596 }
597
598 CipherCtxPtr create_cipher_ctx(ckmc_cipher_ctx_h ctx)
599 {
600     return CipherCtxPtr(ctx, ckmc_cipher_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 }