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