CKM: no more "empty password for lockscreen" logics.
[platform/core/test/security-tests.git] / tests / ckm / capi-testcases.cpp
1 #include <dpl/test/test_runner.h>
2 #include <dpl/test/test_runner_child.h>
3
4 #include <tests_common.h>
5 #include <test-certs.h>
6 #include <ckm-common.h>
7 #include <access_provider2.h>
8
9 #include <ckm/ckm-manager.h>
10 #include <ckm/ckm-control.h>
11 #include <ckm/ckm-type.h>
12
13 #include <ckmc/ckmc-manager.h>
14 #include <ckmc/ckmc-control.h>
15 #include <ckmc/ckmc-type.h>
16 #include <ckmc/ckmc-error.h>
17
18 #include <ckm-common.h>
19
20 #include <string>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25
26 static const int USER_APP = 5000;
27 static const int GROUP_APP = 5000;
28
29 namespace {
30
31 typedef enum {
32         ALIAS_KEY,
33         ALIAS_CERT,
34         ALIAS_DATA
35 } alias_type_;
36
37 const char* USER_PASS = "user-pass";
38
39 int count_aliases(alias_type_ type, int minimum_initial_element_count = 0)
40 {
41         ckmc_alias_list_s *aliasList = NULL;
42         int ec;
43         switch(type)
44         {
45                 case ALIAS_KEY:
46                         ec = ckmc_get_key_alias_list(&aliasList);
47                         break;
48
49                 case ALIAS_CERT:
50                         ec = ckmc_get_cert_alias_list(&aliasList);
51                         break;
52
53                 case ALIAS_DATA:
54                         ec = ckmc_get_data_alias_list(&aliasList);
55                         break;
56         }
57         if(ec == CKMC_ERROR_DB_ALIAS_UNKNOWN)
58                 return 0;
59         else if(ec==0)
60         {
61                 ckmc_alias_list_s *plist = aliasList;
62                 int return_count = 0;
63                 while(plist)
64                 {
65                         plist = plist->next;
66                         return_count ++;
67                 }
68                 ckmc_alias_list_all_free(aliasList);
69
70                 RUNNER_ASSERT_MSG(
71                                 return_count >= minimum_initial_element_count,
72                                 "Error: alias list failed, current element count: " << return_count <<
73                                 " while expected minimal count of " << minimum_initial_element_count <<
74                                 " elements");
75
76                 return return_count;
77         }
78         else
79         {
80                 // error - fail
81                 RUNNER_ASSERT_MSG(
82                                 ec >= 0,
83                                 "Error: alias list failed, ec: " << CKMCErrorToString(ec));
84         }
85
86         return ec;
87 }
88
89 ckmc_raw_buffer_s prepare_message_buffer(const char * input)
90 {
91         ckmc_raw_buffer_s retval;
92         retval.data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(input));
93         retval.size = strlen(input);
94         return retval;
95 }
96
97 } // namespace anonymous
98
99
100 RUNNER_TEST_GROUP_INIT (T301_CKMC_CONTROL_C_API);
101
102 RUNNER_TEST(T3011_Control_C_API)
103 {
104         int temp;
105
106         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
107                         CKMCReadableError(temp));
108
109         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
110                         CKMCReadableError(temp));
111 }
112
113 RUNNER_TEST(T3012_Control_C_API)
114 {
115         int temp;
116
117         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "simple-password")),
118                         CKMCReadableError(temp));
119         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
120                         CKMCReadableError(temp));
121 }
122
123 RUNNER_TEST(T3013_Control_C_API)
124 {
125         int temp;
126
127         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "simple-password")),
128                         CKMCReadableError(temp));
129 }
130
131 RUNNER_TEST(T3014_Control_C_API)
132 {
133         int temp;
134         const uid_t UNIQUE_USER = 15;
135
136     // clean up environment
137     RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(UNIQUE_USER)),
138             CKMCReadableError(temp));
139     // unlock with default password
140     RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(UNIQUE_USER, NULL)),
141             CKMCReadableError(temp));
142         // reset password (NULL, "simple-password")
143         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_reset_user_password(UNIQUE_USER, "simple-password")),
144                         CKMCReadableError(temp));
145         // get rid of NULL DKEK
146         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(UNIQUE_USER, "simple-password")),
147                         CKMCReadableError(temp));
148         // lock db
149         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(UNIQUE_USER)),
150                         CKMCReadableError(temp));
151         // try to reset password when db locked
152         RUNNER_ASSERT_MSG( CKMC_ERROR_BAD_REQUEST == (temp = ckmc_reset_user_password(UNIQUE_USER, "simple-password")),
153                         CKMCReadableError(temp));
154     // clean up environment
155     RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(UNIQUE_USER)),
156             CKMCReadableError(temp));
157 }
158
159 RUNNER_TEST(T3015_Control_C_API)
160 {
161         int temp;
162
163         RUNNER_ASSERT_MSG(
164                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(20, "test-pass")),
165                         CKMCReadableError(temp));
166         RUNNER_ASSERT_MSG(
167                         CKMC_ERROR_NONE == (temp = ckmc_change_user_password(20, "test-pass", "new-pass")),
168                         CKMCReadableError(temp));
169         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(20)),
170                         CKMCReadableError(temp));
171         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(20)),
172                         CKMCReadableError(temp));
173 }
174
175 RUNNER_TEST(T3016_Control_C_API)
176 {
177         int temp;
178
179         RUNNER_ASSERT_MSG(
180                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
181                         CKMCReadableError(temp));
182         RUNNER_ASSERT_MSG(
183                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
184                         CKMCReadableError(temp));
185         RUNNER_ASSERT_MSG(
186                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(20)),
187                         CKMCReadableError(temp));
188         RUNNER_ASSERT_MSG(
189                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(20)),
190                         CKMCReadableError(temp));
191 }
192
193 RUNNER_TEST_GROUP_INIT (T302_CKMC_QUICK_SET_GET_TESTS_C_API);
194
195 RUNNER_TEST(T3021_init_C_API)
196 {
197         int temp;
198
199         RUNNER_ASSERT_MSG(
200                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "test-pass")),
201                         CKMCReadableError(temp));
202         RUNNER_ASSERT_MSG(
203                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
204                         CKMCReadableError(temp));
205 }
206
207 RUNNER_TEST(T3022_key_C_API)
208 {
209         int temp;
210
211         ckmc_key_s test_key, *test_key2;
212         ckmc_policy_s test_policy;
213
214         char* password = NULL;
215         const char *alias = "mykey";
216
217         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
218                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
219                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
220                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
221                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
222                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
223                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
224                 "zQIDAQAB\n"
225                 "-----END PUBLIC KEY-----";
226
227         char* char_keypem = new char[keyPem.length() + 1];
228
229         std::strcpy(char_keypem, keyPem.c_str());
230         test_key.raw_key =  (unsigned char *)char_keypem;
231         test_key.key_size = keyPem.length();
232         test_key.key_type = CKMC_KEY_RSA_PUBLIC;
233         test_key.password = password;
234
235         test_policy.password = password;
236         test_policy.extractable = 1;
237
238         test_key2 = &test_key;
239
240         RUNNER_ASSERT_MSG(
241                         CKMC_ERROR_NONE == (temp = ckmc_save_key(alias, test_key, test_policy)),
242                         CKMCReadableError(temp));
243
244         RUNNER_ASSERT_MSG(
245                         CKMC_ERROR_NONE == (temp = ckmc_get_key(alias, password, &test_key2)),
246                         CKMCReadableError(temp));
247 }
248
249 RUNNER_TEST(T3023_certificate_C_API)
250 {
251         int temp;
252
253         std::string certPem = TestData::getTestCertificateBase64(TestData::GIAG2);
254
255         char* password = NULL;
256         ckmc_cert_s *cert2;
257         ckmc_cert_s cert;
258
259         const char *alias = "test-cert-1-RSA";
260
261         ckmc_policy_s test_policy;
262         test_policy.password = password;
263         test_policy.extractable = 1;
264
265         char* char_certPem = new char[certPem.length() + 1];
266         std::strcpy(char_certPem, certPem.c_str());
267         cert.raw_cert =  (unsigned char *)char_certPem;
268         cert.cert_size = certPem.length();
269         cert.data_format = CKMC_FORM_PEM;
270
271         RUNNER_ASSERT_MSG(
272                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(alias, cert, test_policy)),
273                         CKMCReadableError(temp));
274
275         RUNNER_ASSERT_MSG(
276                         CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias, password, &cert2)),
277                         CKMCReadableError(temp));
278
279         ckmc_cert_free(cert2);
280 }
281
282 RUNNER_TEST(T3024_certificate_remove_C_API)
283 {
284         int temp;
285
286         char* password = NULL;
287         ckmc_cert_s *cert2;
288         const char *alias = "test-cert-1-RSA";
289
290         RUNNER_ASSERT_MSG(
291                         CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias, password, &cert2)),
292                         CKMCReadableError(temp));
293         ckmc_cert_free(cert2);
294
295         RUNNER_ASSERT_MSG(
296                         CKMC_ERROR_NONE == (temp = ckmc_remove_cert(alias)),
297                         CKMCReadableError(temp));
298
299         RUNNER_ASSERT_MSG(
300                         CKMC_ERROR_NONE != (temp = ckmc_get_cert(alias, password, &cert2)),
301                         CKMCReadableError(temp));
302 }
303
304 RUNNER_TEST(T3025_certificate_list_C_API)
305 {
306         int temp;
307
308         std::string certPem = TestData::getTestCertificateBase64(TestData::GIAG2);
309
310         char* password = NULL;
311         ckmc_cert_s cert;
312
313         ckmc_policy_s test_policy;
314         test_policy.password = password;
315         test_policy.extractable = 1;
316
317         char* char_certPem = new char[certPem.length() + 1];
318         std::strcpy(char_certPem, certPem.c_str());
319         cert.raw_cert =  (unsigned char *)char_certPem;
320         cert.cert_size = certPem.length();
321         cert.data_format = CKMC_FORM_PEM;
322
323         int current_aliases_num = count_aliases(ALIAS_CERT);
324
325         RUNNER_ASSERT_MSG(
326                         CKMC_ERROR_NONE == (temp = ckmc_save_cert("cert_test1", cert, test_policy)),
327                         CKMCReadableError(temp));
328
329         RUNNER_ASSERT_MSG(
330                         CKMC_ERROR_NONE == (temp = ckmc_save_cert("cert_test2", cert, test_policy)),
331                         CKMCReadableError(temp));
332
333         RUNNER_ASSERT_MSG(
334                         CKMC_ERROR_NONE == (temp = ckmc_save_cert("cert_test3", cert, test_policy)),
335                         CKMCReadableError(temp));
336
337         RUNNER_ASSERT_MSG(
338                         (current_aliases_num+3) == (temp = count_aliases(ALIAS_CERT)),
339                         "Error: expecting " << (current_aliases_num+3) << " aliases, while found " << temp);
340 }
341
342
343 RUNNER_CHILD_TEST(T3026_user_app_save_key_C_API)
344 {
345         AccessProvider ap("mylabel");
346         ap.allowAPI("key-manager::api-storage", "rw");
347         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
348
349         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
350                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
351                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
352                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
353                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
354                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
355                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
356                 "zQIDAQAB\n"
357                 "-----END PUBLIC KEY-----";
358
359         int temp;
360
361         ckmc_key_s test_key, *test_key2;
362         ckmc_policy_s test_policy;
363
364         char* password = NULL;
365         const char *passwordPolicy = "x";
366         const char *alias = "mykey";
367         char* char_keypem = new char[keyPem.length() + 1];
368
369         std::strcpy(char_keypem, keyPem.c_str());
370         test_key.raw_key =  (unsigned char *)char_keypem;
371         test_key.key_size = keyPem.length();
372         test_key.key_type = CKMC_KEY_RSA_PUBLIC;
373         test_key.password = password;
374
375         test_policy.password = const_cast<char *>(passwordPolicy);
376         test_policy.extractable = 1;
377
378         test_key2 = &test_key;
379
380
381         RUNNER_ASSERT_MSG(
382                         CKMC_ERROR_NONE == (temp = ckmc_save_key(alias, test_key, test_policy)),
383                         CKMCReadableError(temp));
384         RUNNER_ASSERT_MSG(
385                         CKMC_ERROR_NONE == (temp = ckmc_get_key(alias, passwordPolicy, &test_key2)),
386                         CKMCReadableError(temp));
387
388         //       RUNNER_ASSERT_MSG(
389         //                       key.getDER() == key2.getDER(), "Key value has been changed by service");
390 }
391
392 RUNNER_CHILD_TEST(T3027_app_user_save_keys_exportable_flag)
393 {
394         AccessProvider ap("mylabel");
395         ap.allowAPI("key-manager::api-storage", "rw");
396         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
397
398         int temp;
399         auto manager = CKM::Manager::create();
400
401         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
402                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
403                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
404                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
405                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
406                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
407                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
408                 "zQIDAQAB\n"
409                 "-----END PUBLIC KEY-----";
410
411         ckmc_policy_s test_policy;
412         ckmc_key_s test_key, *test_key2;
413         char* char_keypem = new char[keyPem.length() + 1];
414         char* password = NULL;
415
416         std::strcpy(char_keypem, keyPem.c_str());
417         test_key.raw_key = (unsigned char *)char_keypem;
418         test_key.key_size = keyPem.length();
419         test_key.key_type = CKMC_KEY_RSA_PUBLIC;
420         test_key.password = password;
421
422         test_policy.password = password;
423         test_policy.extractable = 0;
424
425         RUNNER_ASSERT_MSG(
426                         CKMC_ERROR_NONE == (temp = ckmc_save_key("appkey1", test_key, test_policy)),
427                         CKMCReadableError(temp));
428
429         RUNNER_ASSERT_MSG(
430                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_key("appkey1", password, &test_key2)),
431                         CKMCReadableError(temp));
432 }
433
434
435 RUNNER_TEST(T3028_certificate_with_DSA_key_C_API)
436 {
437         int temp;
438
439         std::string certPem = TestData::getTestCertificateBase64(TestData::GIAG2);
440
441         char* password = NULL;
442         ckmc_cert_s *cert2 = NULL;
443         ckmc_cert_s cert;
444
445         ckmc_policy_s test_policy;
446         test_policy.password = password;
447         test_policy.extractable = 1;
448
449         char* char_certPem = new char[certPem.length() + 1];
450         std::strcpy(char_certPem, certPem.c_str());
451         cert.raw_cert =  (unsigned char *)char_certPem;
452         cert.cert_size = certPem.length();
453         cert.data_format = CKMC_FORM_PEM;
454
455         const char *alias = "test-cert-1-DSA";
456         RUNNER_ASSERT_MSG(
457                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(alias, cert, test_policy)),
458                         CKMCReadableError(temp));
459
460         RUNNER_ASSERT_MSG(
461                         CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias, password, &cert2)),
462                         CKMCReadableError(temp));
463
464         ckmc_cert_free(cert2);
465 }
466
467 RUNNER_TEST(T3029_deinit_C_API)
468 {
469         int temp;
470
471         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
472                         CKMCReadableError(temp));
473         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
474                         CKMCReadableError(temp));
475         RUNNER_ASSERT_MSG(
476                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
477                         CKMCReadableError(temp));
478         RUNNER_ASSERT_MSG(
479                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
480                         CKMCReadableError(temp));
481 }
482
483
484 RUNNER_TEST_GROUP_INIT (T3030_CKMC_QUICK_GET_ALIAS_TESTS_C_API);
485
486 RUNNER_TEST(T3031_init_C_API)
487 {
488         int temp;
489
490         RUNNER_ASSERT_MSG(
491                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "simple-password")),
492                         CKMCReadableError(temp));
493         RUNNER_ASSERT_MSG(
494                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, "simple-password")),
495                         CKMCReadableError(temp));
496 }
497
498 RUNNER_TEST(T3032_save_keys_get_alias_C_API)
499 {
500         int temp;
501
502         char* password = NULL;
503         ckmc_policy_s test_policy1, test_policy2, test_policy3;
504         ckmc_key_s test_key;
505
506         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
507                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
508                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
509                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
510                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
511                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
512                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
513                 "zQIDAQAB\n"
514                 "-----END PUBLIC KEY-----";
515
516         char* char_keypem = new char[keyPem.length() + 1];
517
518         std::strcpy(char_keypem, keyPem.c_str());
519         test_key.raw_key = (unsigned char *)char_keypem;
520         test_key.key_size = keyPem.length();
521         test_key.key_type = CKMC_KEY_RSA_PUBLIC;
522         test_key.password = password;
523
524         test_policy1.password = password;
525         test_policy1.extractable = 1;
526
527         test_policy2.password = password;
528         test_policy2.extractable = 0;
529
530         test_policy3.password = password;
531         test_policy3.extractable = 0;
532
533         int current_aliases_num = count_aliases(ALIAS_KEY);
534
535         RUNNER_ASSERT_MSG(
536                         CKMC_ERROR_NONE == (temp = ckmc_save_key("rootkey1", test_key, test_policy1)),
537                         CKMCReadableError(temp));
538
539         RUNNER_ASSERT_MSG(
540                         CKMC_ERROR_NONE == (temp = ckmc_save_key("rootkey2", test_key, test_policy2)),
541                         CKMCReadableError(temp));
542
543         RUNNER_ASSERT_MSG(
544                         CKMC_ERROR_NONE == (temp = ckmc_save_key("rootkey3", test_key, test_policy3)),
545                         CKMCReadableError(temp));
546
547         RUNNER_ASSERT_MSG(
548                         (current_aliases_num+3) == (temp = count_aliases(ALIAS_KEY)),
549                         "Error: expecting " << (current_aliases_num+3) << " aliases, while found " << temp);
550 }
551
552
553 RUNNER_TEST(T3033_remove_key_C_API)
554 {
555         int temp;
556
557         char* password = NULL;
558
559         ckmc_key_s *test_key2;
560         RUNNER_ASSERT_MSG(
561                         CKMC_ERROR_NONE == (temp = ckmc_get_key("rootkey1", password, &test_key2)),
562                         CKMCReadableError(temp));
563
564         RUNNER_ASSERT_MSG(
565                         CKMC_ERROR_NONE == (temp = ckmc_remove_key("rootkey1")),
566                         CKMCReadableError(temp));
567
568         RUNNER_ASSERT_MSG(
569                         CKMC_ERROR_NONE != (temp = ckmc_get_key("rootkey1", password, &test_key2)),
570                         CKMCReadableError(temp));
571 }
572
573 RUNNER_TEST(T3034_deinit_C_API)
574 {
575         int temp;
576
577         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
578                         CKMCReadableError(temp));
579         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
580                         CKMCReadableError(temp));
581         RUNNER_ASSERT_MSG(
582                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
583                         CKMCReadableError(temp));
584         RUNNER_ASSERT_MSG(
585                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
586                         CKMCReadableError(temp));
587 }
588
589 RUNNER_TEST_GROUP_INIT (T3040_CKMC_QUICK_REMOVE_BIN_DATA_TEST_C_API);
590
591 RUNNER_TEST(T3041_init_C_API)
592 {
593         int temp;
594         RUNNER_ASSERT_MSG(
595                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "simple-password")),
596                         CKMCReadableError(temp));
597         RUNNER_ASSERT_MSG(
598                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, "simple-password")),
599                         CKMCReadableError(temp));
600 }
601
602 RUNNER_TEST(T3042_save_get_bin_data_C_API)
603 {
604         int temp;
605
606         ckmc_raw_buffer_s testData1, testData2, testData3;
607         char* password = NULL;
608
609         std::string binData1 = "My bin data1";
610         std::string binData2 = "My bin data2";
611         std::string binData3 = "My bin data3";
612         char* char_binData1 = new char[binData1.length() + 1];
613         char* char_binData2 = new char[binData2.length() + 1];
614         char* char_binData3 = new char[binData3.length() + 1];
615         std::strcpy(char_binData1, binData1.c_str());
616         std::strcpy(char_binData2, binData2.c_str());
617         std::strcpy(char_binData3, binData3.c_str());
618         testData1.data = (unsigned char *) char_binData1;
619         testData2.data = (unsigned char *) char_binData2;
620         testData3.data = (unsigned char *) char_binData3;
621         testData1.size = binData1.length()+1;
622         testData2.size = binData2.length()+1;
623         testData3.size = binData3.length()+1;
624
625         ckmc_policy_s test_policy1, test_policy2, test_policy3;
626
627         test_policy1.password = password;
628         test_policy1.extractable = 1;
629         test_policy2.password = password;
630         test_policy2.extractable = 1;
631         test_policy3.password = password;
632         test_policy3.extractable = 0;
633
634         int current_aliases_num = count_aliases(ALIAS_DATA);
635
636         RUNNER_ASSERT_MSG(
637                         CKMC_ERROR_NONE == (temp = ckmc_save_data("data1", testData1, test_policy1)), // should change it as null value
638                         CKMCReadableError(temp));
639
640         RUNNER_ASSERT_MSG(
641                         CKMC_ERROR_NONE == (temp = ckmc_save_data("data2", testData2, test_policy1)), // should change it as null value
642                         CKMCReadableError(temp));
643
644         RUNNER_ASSERT_MSG(
645                         CKMC_ERROR_NONE == (temp = ckmc_save_data("data3", testData3, test_policy2)),
646                         CKMCReadableError(temp));
647
648         RUNNER_ASSERT_MSG(
649                         CKMC_ERROR_INVALID_PARAMETER == (temp = ckmc_save_data("data4", testData3, test_policy3)),
650                         CKMCReadableError(temp));
651
652         RUNNER_ASSERT_MSG(
653                         (current_aliases_num+3) == (temp = count_aliases(ALIAS_DATA)),
654                         "Error: expecting " << (current_aliases_num+3) << " aliases, while found " << temp);
655
656         ckmc_raw_buffer_s *testData4;
657         RUNNER_ASSERT_MSG(
658                         CKMC_ERROR_NONE == (temp = ckmc_get_data("data2", password, &testData4)),
659                         CKMCReadableError(temp));
660
661         int compareResult;
662         compareResult = (strcmp((const char *)testData2.data, (const char *)testData4->data));
663         RUNNER_ASSERT_MSG( compareResult == 0,
664                         "Data corrupted");
665 }
666
667 RUNNER_CHILD_TEST(T3043_app_user_save_bin_data_C_API)
668 {
669         AccessProvider ap("mylabel");
670         ap.allowAPI("key-manager::api-storage", "rw");
671         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
672
673         int temp;
674         ckmc_raw_buffer_s testData1;
675         char* password = NULL;
676         std::string binData1 = "My bin data";
677         char* char_binData1 = new char[binData1.length() + 1];
678         std::strcpy(char_binData1, binData1.c_str());
679         testData1.data = (unsigned char *) char_binData1;
680         testData1.size = binData1.length()+1;
681
682         ckmc_policy_s test_policy1, test_policy2;
683
684         test_policy1.password = password;
685         test_policy1.extractable = 1;
686
687         test_policy2.password = password;
688         test_policy2.extractable = 1;
689
690         std::string binData = "My bin data";
691
692         int current_aliases_num = count_aliases(ALIAS_DATA);
693
694         RUNNER_ASSERT_MSG(
695                         CKMC_ERROR_NONE == (temp = ckmc_save_data("appdata1", testData1, test_policy1)),
696                         CKMCReadableError(temp));
697         RUNNER_ASSERT_MSG(
698                         CKMC_ERROR_NONE == (temp = ckmc_save_data("appdata2", testData1, test_policy1)),
699                         CKMCReadableError(temp));
700         RUNNER_ASSERT_MSG(
701                         CKMC_ERROR_NONE == (temp = ckmc_save_data("appdata3", testData1, test_policy2)),
702                         CKMCReadableError(temp));
703
704         RUNNER_ASSERT_MSG(
705                                 (current_aliases_num+3) == (temp = count_aliases(ALIAS_DATA)),
706                                 "Error: expecting " << (current_aliases_num+3) << " aliases, while found " << temp);
707 }
708
709 RUNNER_TEST(T3044_remove_bin_data_C_API)
710 {
711         int temp;
712
713         int current_aliases_num = count_aliases(ALIAS_DATA, 2);
714
715         RUNNER_ASSERT_MSG(
716                         CKMC_ERROR_NONE == (temp = ckmc_remove_data("data1")),
717                         CKMCReadableError(temp));
718         RUNNER_ASSERT_MSG(
719                         CKMC_ERROR_NONE == (temp = ckmc_remove_data("data3")),
720                         CKMCReadableError(temp));
721
722         RUNNER_ASSERT_MSG(
723                                 (current_aliases_num-2) == (temp = count_aliases(ALIAS_DATA)),
724                                 "Error: expecting " << (current_aliases_num-2) << " aliases, while found " << temp);
725
726         char* password = NULL;
727
728         ckmc_raw_buffer_s *testData1, testData2;
729
730         std::string testStr = "My bin data2";
731         char* char_testData2 = new char[testStr.length() + 1];
732         std::strcpy(char_testData2, testStr.c_str());
733         testData2.data = (unsigned char *) char_testData2;
734         testData2.size = testStr.length()+1;
735
736         CKM::RawBuffer buffer;
737         RUNNER_ASSERT_MSG(
738                         CKMC_ERROR_NONE == (temp = ckmc_get_data("data2", password, &testData1)),
739                         CKMCReadableError(temp));
740
741         int compareResult;
742         compareResult = (strcmp((const char *)testData2.data, (const char *)testData1->data));
743         RUNNER_ASSERT_MSG( compareResult == 0,
744                         "Data corrupted");
745
746         RUNNER_ASSERT_MSG(
747                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_data("data3", password, &testData1)),
748                         CKMCReadableError(temp));
749 }
750
751 RUNNER_TEST(T3045_deinit_C_API)
752 {
753         int temp;
754
755         RUNNER_ASSERT_MSG(
756                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
757                         CKMCReadableError(temp));
758         RUNNER_ASSERT_MSG(
759                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
760                         CKMCReadableError(temp));
761         RUNNER_ASSERT_MSG(
762                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
763                         CKMCReadableError(temp));
764         RUNNER_ASSERT_MSG(
765                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
766                         CKMCReadableError(temp));
767 }
768
769 RUNNER_TEST_GROUP_INIT(T305_CKMC_QUICK_CREATE_PAIR_CAPI);
770
771 RUNNER_TEST(T3051_CAPI_init)
772 {
773         int temp;
774
775         RUNNER_ASSERT_MSG(
776                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
777                         CKMCReadableError(temp));
778
779         RUNNER_ASSERT_MSG(
780                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
781                         CKMCReadableError(temp));
782 }
783
784 RUNNER_CHILD_TEST(T3052_CAPI_create_rsa_key)
785 {
786         int temp;
787
788         AccessProvider ap("mylabel");
789         ap.allowAPI("key-manager::api-storage", "rw");
790         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
791
792         size_t size = 1024;
793         const char *private_key_alias = "RSA-test-1-priv";
794         const char *public_key_alias = "RSA-test-1-pub";
795         ckmc_policy_s policy_private_key;
796         ckmc_policy_s policy_public_key;
797
798         policy_private_key.password = NULL;
799         policy_private_key.extractable = 1;
800
801         policy_public_key.password = NULL;
802         policy_public_key.extractable = 1;
803
804
805         int current_aliases_num = count_aliases(ALIAS_KEY);
806
807         RUNNER_ASSERT_MSG(
808                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
809                         CKMCReadableError(temp));
810
811         RUNNER_ASSERT_MSG(
812                         (current_aliases_num+2) == (temp = count_aliases(ALIAS_KEY)),
813                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << temp);
814
815         ckmc_key_s *privateKey;
816         RUNNER_ASSERT_MSG(
817                         CKMC_ERROR_NONE == (temp = ckmc_get_key(private_key_alias, policy_private_key.password,&privateKey)),
818                         CKMCReadableError(temp));
819         RUNNER_ASSERT_MSG(
820                         privateKey->key_type == CKMC_KEY_RSA_PRIVATE,
821                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_RSA_PRIVATE) << ", actual=" << static_cast<int>(privateKey->key_type));
822         RUNNER_ASSERT_MSG(
823                         privateKey != NULL && privateKey->key_size > 0 && privateKey->raw_key != NULL,
824                         "Private key is broken.");
825
826         ckmc_key_s *publicKey;
827         RUNNER_ASSERT_MSG(
828                         CKMC_ERROR_NONE == (temp = ckmc_get_key(public_key_alias, policy_public_key.password, &publicKey)),
829                         CKMCReadableError(temp));
830         RUNNER_ASSERT_MSG(
831                         publicKey->key_type == CKMC_KEY_RSA_PUBLIC,
832                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_RSA_PUBLIC) << ", actual=" << static_cast<int>(publicKey->key_type));
833         RUNNER_ASSERT_MSG(
834                         publicKey != NULL && publicKey->key_size > 0 && publicKey->raw_key != NULL,
835                         "Public key is broken.");
836
837         // on next attempt to generate keys with the same alias, expect fail (alias exists)
838         RUNNER_ASSERT_MSG(
839                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
840                         CKMCReadableError(temp));
841 }
842
843 RUNNER_CHILD_TEST(T3053_CAPI_create_dsa_key)
844 {
845         int temp;
846
847         AccessProvider ap("mylabel");
848         ap.allowAPI("key-manager::api-storage", "rw");
849         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
850
851         size_t size = 1024;
852         const char *private_key_alias = "DSA-test-2-priv";
853         const char *public_key_alias = "DSA-test-2-pub";
854         ckmc_policy_s policy_private_key;
855         ckmc_policy_s policy_public_key;
856
857         policy_private_key.password = NULL;
858         policy_private_key.extractable = 1;
859
860         policy_public_key.password = NULL;
861         policy_public_key.extractable = 1;
862
863         int current_aliases_num = count_aliases(ALIAS_KEY);
864
865         RUNNER_ASSERT_MSG(
866                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
867                         CKMCReadableError(temp));
868
869         RUNNER_ASSERT_MSG(
870                         (current_aliases_num+2) == (temp = count_aliases(ALIAS_KEY)),
871                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << temp);
872
873         ckmc_key_s *privateKey = 0;
874         RUNNER_ASSERT_MSG(
875                         CKMC_ERROR_NONE == (temp = ckmc_get_key(private_key_alias, policy_private_key.password,&privateKey)),
876                         CKMCReadableError(temp));
877         RUNNER_ASSERT_MSG(
878                         privateKey != NULL && privateKey->key_size > 0 && privateKey->raw_key != NULL,
879                         "Private key is broken.");
880         RUNNER_ASSERT_MSG(
881                         privateKey->key_type == CKMC_KEY_DSA_PRIVATE,
882                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_DSA_PRIVATE) << ", actual=" << static_cast<int>(privateKey->key_type));
883         ckmc_key_free(privateKey);
884
885         ckmc_key_s *pubKey = 0;
886         RUNNER_ASSERT_MSG(
887                         CKMC_ERROR_NONE == (temp = ckmc_get_key(public_key_alias, policy_public_key.password, &pubKey)),
888                         CKMCReadableError(temp));
889         RUNNER_ASSERT_MSG(
890                         pubKey != NULL && pubKey->key_size > 0 && pubKey->raw_key != NULL,
891                         "Public key is broken.");
892         RUNNER_ASSERT_MSG(
893                         pubKey->key_type == CKMC_KEY_DSA_PUBLIC,
894                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_DSA_PUBLIC) << ", actual=" << static_cast<int>(pubKey->key_type));
895         ckmc_key_free(pubKey);
896
897         // on next attempt to generate keys with the same alias, expect fail (alias exists)
898         RUNNER_ASSERT_MSG(
899                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
900                         CKMCReadableError(temp));
901 }
902
903
904 RUNNER_TEST(T3054_CAPI_deinit)
905 {
906         int temp;
907
908         RUNNER_ASSERT_MSG(
909                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
910                         CKMCReadableError(temp));
911         RUNNER_ASSERT_MSG(
912                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
913                         CKMCReadableError(temp));
914 }
915
916
917 RUNNER_TEST_GROUP_INIT(T306_CKMC_CAPI_CreateKeyPair);
918
919 RUNNER_TEST(T3061_CAPI_init)
920 {
921         int temp;
922         RUNNER_ASSERT_MSG(
923                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
924                         CKMCReadableError(temp));
925         RUNNER_ASSERT_MSG(
926                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
927                         CKMCReadableError(temp));
928         RUNNER_ASSERT_MSG(
929                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "test-pass")),
930                         CKMCReadableError(temp));
931         RUNNER_ASSERT_MSG(
932                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
933                         CKMCReadableError(temp));
934 }
935
936 RUNNER_TEST(T3062_CAPI_CreateKeyPairRSA)
937 {
938         int temp;
939
940         size_t size = 1024;
941         const char *private_key_alias = "rsa-test-1";
942         const char *public_key_alias = "rsa-test-2";
943         ckmc_policy_s policy_private_key;
944         ckmc_policy_s policy_public_key;
945
946         policy_private_key.password = const_cast<char *>("privatepassword");
947         policy_private_key.extractable = 0;
948
949         policy_public_key.password = NULL;
950         policy_public_key.extractable = 1;
951
952         RUNNER_ASSERT_MSG(
953                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
954                         CKMCReadableError(temp));
955
956         // on next attempt to generate keys with the same alias, expect fail (alias exists)
957         RUNNER_ASSERT_MSG(
958                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
959                         CKMCReadableError(temp));
960 }
961
962 RUNNER_TEST(T3063_CAPI_CreateKeyPairDSA)
963 {
964         int temp;
965
966         size_t size = 1024;
967         const char *private_key_alias = "dsa-test-1";
968         const char *public_key_alias = "dsa-test-2";
969         ckmc_policy_s policy_private_key;
970         ckmc_policy_s policy_public_key;
971
972         policy_private_key.password = const_cast<char *>("privatepassword");
973         policy_private_key.extractable = 0;
974
975         policy_public_key.password = NULL;
976         policy_public_key.extractable = 1;
977
978         RUNNER_ASSERT_MSG(
979                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
980                         CKMCReadableError(temp));
981 }
982
983 RUNNER_TEST(T3064_CAPI_CreateKeyPairECDSA)
984 {
985         int temp;
986
987         ckmc_ec_type_e ectype = CKMC_EC_PRIME192V1;
988         const char *private_key_alias = "ecdsa-test-1";
989         const char *public_key_alias = "ecdsa-test-2";
990         ckmc_policy_s policy_private_key;
991         ckmc_policy_s policy_public_key;
992
993         policy_private_key.password = const_cast<char *>("privatepassword");
994         policy_private_key.extractable = 0;
995
996         policy_public_key.password = NULL;
997         policy_public_key.extractable = 1;
998
999         RUNNER_ASSERT_MSG(
1000                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_ecdsa(ectype, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
1001                         CKMCReadableError(temp));
1002 }
1003
1004 RUNNER_TEST(T3065_CAPI_deinit)
1005 {
1006         int temp;
1007
1008         RUNNER_ASSERT_MSG(
1009                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
1010                         CKMCReadableError(temp));
1011         RUNNER_ASSERT_MSG(
1012                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
1013                         CKMCReadableError(temp));
1014 }
1015
1016 // TODO
1017 //RUNNER_TEST_GROUP_INIT(T120_NEGATIVE_TESTS);
1018
1019
1020
1021 RUNNER_TEST_GROUP_INIT(T307_CKMC_CAPI_OCSP_TESTS);
1022
1023 RUNNER_TEST(T3071_CAPI_init)
1024 {
1025         int temp;
1026         RUNNER_ASSERT_MSG(
1027                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "test-pass")),
1028                         CKMCReadableError(temp));
1029 }
1030
1031 RUNNER_TEST(T3074_CAPI_ckmc_ocsp_check)
1032 {
1033     std::string ee = TestData::getTestCertificateBase64(TestData::MBANK);
1034     std::string im = TestData::getTestCertificateBase64(TestData::SYMANTEC);
1035
1036         ckmc_cert_s c_cert;
1037         c_cert.raw_cert = reinterpret_cast<unsigned char *>(const_cast<char *>(ee.c_str()));
1038         c_cert.cert_size = ee.size();
1039         c_cert.data_format = CKMC_FORM_PEM;
1040
1041         ckmc_cert_s c_cert1;
1042         c_cert1.raw_cert = reinterpret_cast<unsigned char *>(const_cast<char *>(im.c_str()));
1043         c_cert1.cert_size = im.size();
1044         c_cert1.data_format = CKMC_FORM_PEM;
1045
1046         ckmc_cert_list_s untrustedcerts;
1047         untrustedcerts.cert = &c_cert1;
1048         untrustedcerts.next = NULL;
1049
1050         ckmc_cert_list_s *cert_chain_list;
1051
1052     int tmp = ckmc_get_cert_chain(&c_cert, &untrustedcerts, &cert_chain_list);
1053         RUNNER_ASSERT_MSG(
1054                         CKMC_ERROR_NONE == tmp, CKMCReadableError(tmp));
1055
1056         RUNNER_ASSERT_MSG(cert_chain_list != NULL, "Wrong size of certificate chain.");
1057
1058     ckmc_ocsp_status_e ocsp_status;
1059     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (tmp = ckmc_ocsp_check(cert_chain_list, &ocsp_status)), CKMCReadableError(tmp));
1060     RUNNER_ASSERT_MSG(ocsp_status == CKMC_OCSP_STATUS_GOOD, "Wrong status: " << static_cast<int>(ocsp_status));
1061 }
1062
1063 RUNNER_TEST(T3075_CAPI_deinit)
1064 {
1065         int temp;
1066
1067         RUNNER_ASSERT_MSG(
1068                 CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
1069                 CKMCReadableError(temp));
1070         RUNNER_ASSERT_MSG(
1071                 CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
1072                 CKMCReadableError(temp));
1073 }
1074
1075
1076 RUNNER_TEST_GROUP_INIT(T308_CAPI_CREATE_AND_VERIFY_SIGNATURE);
1077
1078 RUNNER_TEST(T3081_CAPI__init)
1079 {
1080         int temp;
1081
1082         RUNNER_ASSERT_MSG(
1083                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
1084                         CKMCReadableError(temp));
1085
1086         RUNNER_ASSERT_MSG(
1087                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
1088                         CKMCReadableError(temp));
1089
1090         RUNNER_ASSERT_MSG(
1091                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
1092                         CKMCReadableError(temp));
1093
1094         RUNNER_ASSERT_MSG(
1095                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "test-pass")),
1096                         CKMCReadableError(temp));
1097 }
1098
1099 RUNNER_TEST(T3082_CAPI__rsa_key_create_verify)
1100 {
1101         int temp;
1102
1103         std::string prv = "-----BEGIN RSA PRIVATE KEY-----\n"
1104                 "Proc-Type: 4,ENCRYPTED\n"
1105                 "DEK-Info: DES-EDE3-CBC,6C6507B11671DABC\n"
1106                 "\n"
1107                 "YiKNviNqc/V/i241CKtAVsNckesE0kcaka3VrY7ApXR+Va93YoEwVQ8gB9cE/eHH\n"
1108                 "S0j3ZS1PAVFM/qo4ZnPdMzaSLvTQw0GAL90wWgF3XQ+feMnWyBObEoQdGXE828TB\n"
1109                 "SLz4UOIQ55Dx6JSWTfEhwAlPs2cEWD14xvuxPzAEzBIYmWmBBsCN94YgFeRTzjH0\n"
1110                 "TImoYVMN60GgOfZWw6rXq9RaV5dY0Y6F1piypCLGD35VaXAutdHIDvwUGECPm7SN\n"
1111                 "w05jRro53E1vb4mYlZEY/bs4q7XEOI5+ZKT76Xn0oEJNX1KRL1h2q8fgUkm5j40M\n"
1112                 "uQj71aLR9KyIoQARwGLeRy09tLVjH3fj66CCMqaPcxcIRIyWi5yYBB0s53ipm6A9\n"
1113                 "CYuyc7MS2C0pOdWKsDvYsHR/36KUiIdPuhF4AbaTqqO0eWeuP7Na7dGK56Fl+ooi\n"
1114                 "cUpJr7cIqMl2vL25B0jW7d4TB3zwCEkVVD1fBPeNoZWo30z4bILcBqjjPkQfHZ2e\n"
1115                 "xNraG3qI4FHjoPT8JEE8p+PgwaMoINlICyIMKiCdvwz9yEnsHPy7FkmatpS+jFoS\n"
1116                 "mg8R9vMwgK/HGEm0dmb/7/a0XsG2jCDm6cOmJdZJFQ8JW7hFs3eOHpNlQYDChG2D\n"
1117                 "A1ExslqBtbpicywTZhzFdYU/hxeCr4UqcY27Zmhr4JlBPMyvadWKeOqCamWepjbT\n"
1118                 "T/MhWJbmWgZbI5s5sbpu7cOYubQcUIEsTaQXGx/KEzGo1HLn9tzSeQfP/nqjAD/L\n"
1119                 "T5t1Mb8o4LuV/fGIT33Q3i2FospJMqp2JINNzG18I6Fjo08PTvJ3row40Rb76+lJ\n"
1120                 "wN1IBthgBgsgsOdB6XNc56sV+uq2TACsNNWw+JnFRCkCQgfF/KUrvN+WireWq88B\n"
1121                 "9UPG+Hbans5A6K+y1a+bzfdYnKws7x8wNRyPxb7Vb2t9ZTl5PBorPLVGsjgf9N5X\n"
1122                 "tCdBlfJsUdXot+EOxrIczV5zx0JIB1Y9hrDG07RYkzPuJKxkW7skqeLo8oWGVpaQ\n"
1123                 "LGWvuebky1R75hcSuL3e4QHfjBHPdQ31fScB884tqkbhBAWr2nT9bYEmyT170bno\n"
1124                 "8QkyOSb99xZBX55sLDHs9p61sTJr2C9Lz/KaWQs+3hTkpwSjSRyjEMH2n491qiQX\n"
1125                 "G+kvLEnvtR8sl9zinorj/RfsxyPntAxudfY3qaYUu2QkLvVdfTVUVbxS/Fg8f7B3\n"
1126                 "hEjCtpKgFjPxQuHE3didNOr5xM7mkmLN/QA7yHVgdpE64T5mFgC3JcVRpcR7zBPH\n"
1127                 "3OeXHgjrhDfN8UIX/cq6gNgD8w7O0rhHa3mEXI1xP14ykPcJ7wlRuLm9P3fwx5A2\n"
1128                 "jQrVKJKw1Nzummmspn4VOpJY3LkH4Sxo4e7Soo1l1cxJpzmERwgMF+vGz1L70+DG\n"
1129                 "M0hVrz1PxlOsBBFgcdS4TB91DIs/RcFDqrJ4gOPNKCgBP+rgTXXLFcxUwJfE3lKg\n"
1130                 "Kmpwdne6FuQYX3eyRVAmPgOHbJuRQCh/V4fYo51UxCcEKeKy6UgOPEJlXksWGbH5\n"
1131                 "VFmlytYW6dFKJvjltSmK6L2r+TlyEQoXwTqe4bkfhB2LniDEq28hKQ==\n"
1132                 "-----END RSA PRIVATE KEY-----\n";
1133
1134         std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1135                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1136                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1137                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1138                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1139                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1140                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1141                 "zQIDAQAB\n"
1142                 "-----END PUBLIC KEY-----\n";
1143
1144         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1145
1146         const char *pub_alias = "pub1";
1147         const char *pri_alias = "prv1";
1148         const char *key_passwd = "1234";
1149         char *pri_passwd = NULL;
1150         char *pub_passwd = NULL;
1151         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1152         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1153         ckmc_raw_buffer_s *signature;
1154
1155         ckmc_key_s pubkey;
1156         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1157         pubkey.key_size = pub.size();
1158         pubkey.key_type = CKMC_KEY_NONE;
1159         pubkey.password = NULL;
1160
1161         ckmc_policy_s pubpolicy;
1162         pubpolicy.password = pub_passwd;
1163         pubpolicy.extractable = 0;
1164
1165         ckmc_policy_s pripolicy;
1166         pripolicy.password = pri_passwd;
1167         pripolicy.extractable = 1;
1168
1169         ckmc_key_s prikey;
1170         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1171         prikey.key_size = prv.size();
1172         prikey.key_type = CKMC_KEY_NONE;
1173         prikey.password = const_cast<char *>(key_passwd);
1174
1175
1176         RUNNER_ASSERT_MSG(
1177                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias, prikey, pripolicy)),
1178                         CKMCReadableError(temp));
1179
1180         RUNNER_ASSERT_MSG(
1181                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias, pubkey, pubpolicy)),
1182                         CKMCReadableError(temp));
1183
1184         RUNNER_ASSERT_MSG(
1185                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1186                                         pri_alias,
1187                                         pri_passwd,
1188                                         msg_buff,
1189                                         hash_algo,
1190                                         pad_algo,
1191                                         &signature)),
1192                         CKMCReadableError(temp));
1193
1194         RUNNER_ASSERT_MSG(
1195                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1196                                         pub_alias,
1197                                         pub_passwd,
1198                                         msg_buff,
1199                                         *signature,
1200                                         hash_algo,
1201                                         pad_algo)),
1202                         CKMCReadableError(temp));
1203 }
1204
1205 RUNNER_TEST(T3083_CAPI__rsa_key_create_verify_negative)
1206 {
1207         int temp;
1208
1209         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message asdfaslkdfjlksadjf test");
1210         const char *pub_alias = "pub1";
1211         const char *pri_alias = "prv1";
1212         char *pri_passwd = NULL;
1213         char *pub_passwd = NULL;
1214         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1215         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1216         ckmc_raw_buffer_s *signature;
1217
1218         RUNNER_ASSERT_MSG(
1219                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1220                                         pri_alias,
1221                                         pri_passwd,
1222                                         msg_buff,
1223                                         hash_algo,
1224                                         pad_algo,
1225                                         &signature)),
1226                         CKMCReadableError(temp));
1227
1228         RUNNER_ASSERT_MSG(
1229                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1230                                         pub_alias,
1231                                         pub_passwd,
1232                                         msg_buff,
1233                                         *signature,
1234                                         hash_algo,
1235                                         pad_algo)),
1236                         CKMCReadableError(temp));
1237
1238         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1239         memcpy((void*)signature->data, "BROKEN", 6);
1240
1241         RUNNER_ASSERT_MSG(
1242                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1243                                         pub_alias,
1244                                         pub_passwd,
1245                                         msg_buff,
1246                                         *signature,
1247                                         hash_algo,
1248                                         pad_algo)),
1249                         CKMCReadableError(temp));
1250 }
1251
1252 RUNNER_TEST(T3084_CAPI__ec_key_create_verify)
1253 {
1254         int temp;
1255
1256         std::string prv = "-----BEGIN EC PRIVATE KEY-----\n"
1257                 "MHQCAQEEIJNud6U4h8EM1rASn4W5vQOJELTaVPQTUiESaBULvQUVoAcGBSuBBAAK\n"
1258                 "oUQDQgAEL1R+hgjiFrdjbUKRNOxUG8ze9nveD9zT05YHeT7vK0w08AUL1HCH5nFV\n"
1259                 "ljePBYSxe6CybFiseayaxRxjA+iF1g==\n"
1260                 "-----END EC PRIVATE KEY-----\n";
1261
1262         std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1263                 "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEL1R+hgjiFrdjbUKRNOxUG8ze9nveD9zT\n"
1264                 "05YHeT7vK0w08AUL1HCH5nFVljePBYSxe6CybFiseayaxRxjA+iF1g==\n"
1265                 "-----END PUBLIC KEY-----\n";
1266
1267         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1268         const char *pri_alias = "ecprv2";
1269         const char *pub_alias = "ecpub2";
1270         char *key_passwd = NULL;
1271         char *pri_passwd = NULL;
1272         char *pub_passwd = NULL;
1273         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1274         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1275         ckmc_raw_buffer_s *signature;
1276
1277         ckmc_key_s pubkey;
1278         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1279         pubkey.key_size = pub.size();
1280         pubkey.key_type = CKMC_KEY_NONE;
1281         pubkey.password = NULL;
1282
1283         ckmc_policy_s pubpolicy;
1284         pubpolicy.password = pub_passwd;
1285         pubpolicy.extractable = 1;
1286
1287         ckmc_key_s prikey;
1288         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1289         prikey.key_size = prv.size();
1290         prikey.key_type = CKMC_KEY_NONE;
1291         prikey.password = key_passwd;
1292
1293         ckmc_policy_s pripolicy;
1294         pripolicy.password = pri_passwd;
1295         pripolicy.extractable = 0;
1296
1297         RUNNER_ASSERT_MSG(
1298                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias, pubkey, pubpolicy)),
1299                         CKMCReadableError(temp));
1300         RUNNER_ASSERT_MSG(
1301                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias, prikey, pripolicy)),
1302                         CKMCReadableError(temp));
1303
1304         RUNNER_ASSERT_MSG(
1305                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1306                                         pri_alias,
1307                                         pri_passwd,
1308                                         msg_buff,
1309                                         hash_algo,
1310                                         pad_algo,
1311                                         &signature)),
1312                         CKMCReadableError(temp));
1313
1314         RUNNER_ASSERT_MSG(
1315                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1316                                         pub_alias,
1317                                         pub_passwd,
1318                                         msg_buff,
1319                                         *signature,
1320                                         hash_algo,
1321                                         pad_algo)),
1322                         CKMCReadableError(temp));
1323
1324         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1325         memcpy((void*)signature->data, "BROKEN", 6);
1326
1327         RUNNER_ASSERT_MSG(
1328                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1329                                         pub_alias,
1330                                         pub_passwd,
1331                                         msg_buff,
1332                                         *signature,
1333                                         hash_algo,
1334                                         pad_algo)),
1335                         CKMCReadableError(temp));
1336 }
1337
1338 RUNNER_TEST(T3085_CAPI__rsa_cert_create_verify_signature)
1339 {
1340         int temp;
1341
1342         std::string prv =
1343                 "-----BEGIN RSA PRIVATE KEY-----\n"
1344                 "MIICXQIBAAKBgQDCKb9BkTdOjCTXKPi/H5FSGuyrgzORBtR3nCTg7SRnL47zNGEj\n"
1345                 "l2wkgsY9ZO3UJHm0gy5KMjWeCuUVkSD3G46J9obg1bYJivCQBJKxfieA8sWOtNq1\n"
1346                 "M8emHGK8o3sjaRklrngmk2xSCs5vFJVlCluzAYUmrPDm64C3+n4yW4pBCQIDAQAB\n"
1347                 "AoGAd1IWgiHO3kuLvFome7XXpaB8P27SutZ6rcLcewnhLDRy4g0XgTrmL43abBJh\n"
1348                 "gdSkooVXZity/dvuKpHUs2dQ8W8zYiFFsHfu9qqLmLP6SuBPyUCvlUDH5BGfjjxI\n"
1349                 "5qGWIowj/qGHKpbQ7uB+Oe2BHwbHao0zFZIkfKqY0mX9U00CQQDwF/4zQcGS1RX/\n"
1350                 "229gowTsvSGVmm8cy1jGst6xkueEuOEZ/AVPO1fjavz+nTziUk4E5lZHAj18L6Hl\n"
1351                 "iO29LRujAkEAzwbEWVhfTJewCZIFf3sY3ifXhGZhVKDHVzPBNyoft8Z+09DMHTJb\n"
1352                 "EYg85MIbR73aUyIWsEci/CPk6LPRNv47YwJAHtQF2NEFqPPhakPjzjXAaSFz0YDN\n"
1353                 "6ZWWpZTMEWL6hUkz5iE9EUpeY54WNB8+dRT6XZix1VZNTMfU8uMdG6BSHwJBAKYM\n"
1354                 "gm47AGz5eVujwD8op6CACk+KomRzdI+P1lh9s+T+E3mnDiAY5IxiXp0Ix0K6lyN4\n"
1355                 "wwPuerQLwi2XFKZsMYsCQQDOiSQFP9PfXh9kFzN6e89LxOdnqC/r9i5GDB3ea8eL\n"
1356                 "SCRprpzqOXZvOP1HBAEjsJ6k4f8Dqj1fm+y8ZcgAZUPr\n"
1357                 "-----END RSA PRIVATE KEY-----\n";
1358
1359         std::string pub =
1360                 "-----BEGIN CERTIFICATE-----\n"
1361                 "MIICijCCAfOgAwIBAgIJAMvaNHQ1ozT8MA0GCSqGSIb3DQEBBQUAMF4xCzAJBgNV\n"
1362                 "BAYTAlBMMQ0wCwYDVQQIDARMb2R6MQ0wCwYDVQQHDARMb2R6MRAwDgYDVQQKDAdT\n"
1363                 "YW1zdW5nMREwDwYDVQQLDAhTZWN1cml0eTEMMAoGA1UEAwwDQ0tNMB4XDTE0MDcw\n"
1364                 "MjEyNDE0N1oXDTE3MDcwMTEyNDE0N1owXjELMAkGA1UEBhMCUEwxDTALBgNVBAgM\n"
1365                 "BExvZHoxDTALBgNVBAcMBExvZHoxEDAOBgNVBAoMB1NhbXN1bmcxETAPBgNVBAsM\n"
1366                 "CFNlY3VyaXR5MQwwCgYDVQQDDANDS00wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ\n"
1367                 "AoGBAMIpv0GRN06MJNco+L8fkVIa7KuDM5EG1HecJODtJGcvjvM0YSOXbCSCxj1k\n"
1368                 "7dQkebSDLkoyNZ4K5RWRIPcbjon2huDVtgmK8JAEkrF+J4DyxY602rUzx6YcYryj\n"
1369                 "eyNpGSWueCaTbFIKzm8UlWUKW7MBhSas8ObrgLf6fjJbikEJAgMBAAGjUDBOMB0G\n"
1370                 "A1UdDgQWBBQuW9DuITahZJ6saVZZI0aBlis5vzAfBgNVHSMEGDAWgBQuW9DuITah\n"
1371                 "ZJ6saVZZI0aBlis5vzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAB2X\n"
1372                 "GNtJopBJR3dCpzDONknr/c6qcsPVa3nH4c7qzy6F+4bgqa5IObnoF8zUrvD2sMAO\n"
1373                 "km3C/N+Qzt8Rb7ORM6U4tlPp1kZ5t6PKjghhNaiYwVm9A/Zm+wyAmRIkQiYDr4MX\n"
1374                 "e+bRAkPmJeEWpaav1lvvBnFzGSGJrnSSeWUegGyn\n"
1375                 "-----END CERTIFICATE-----\n";
1376
1377         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1378
1379         const char *pri_alias = "prv3";
1380         const char *pub_alias = "pub3";
1381         char *key_passwd = NULL;
1382         char *pri_passwd = NULL;
1383         char *pub_passwd = NULL;
1384         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1385         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1386         ckmc_raw_buffer_s *signature;
1387
1388         ckmc_cert_s cert;
1389         cert.raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1390         cert.cert_size = pub.size();
1391         cert.data_format = CKMC_FORM_PEM;
1392
1393         ckmc_policy_s certpolicy;
1394         certpolicy.password = pub_passwd;
1395         certpolicy.extractable = 1;
1396
1397         ckmc_key_s prikey;
1398         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1399         prikey.key_size = prv.size();
1400         prikey.key_type = CKMC_KEY_NONE;
1401         prikey.password = key_passwd;
1402
1403         ckmc_policy_s pripolicy;
1404         pripolicy.password = pri_passwd;
1405         pripolicy.extractable = 0;
1406
1407         RUNNER_ASSERT_MSG(
1408                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(pub_alias, cert, certpolicy)),
1409                         CKMCReadableError(temp));
1410         RUNNER_ASSERT_MSG(
1411                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias, prikey, pripolicy)),
1412                         CKMCReadableError(temp));
1413
1414
1415         RUNNER_ASSERT_MSG(
1416                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1417                                         pri_alias,
1418                                         pri_passwd,
1419                                         msg_buff,
1420                                         hash_algo,
1421                                         pad_algo,
1422                                         &signature)),
1423                         CKMCReadableError(temp));
1424
1425         RUNNER_ASSERT_MSG(
1426                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1427                                         pub_alias,
1428                                         pub_passwd,
1429                                         msg_buff,
1430                                         *signature,
1431                                         hash_algo,
1432                                         pad_algo)),
1433                         CKMCReadableError(temp));
1434
1435         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1436         memcpy((void*)signature->data, "BROKEN", 6);
1437
1438         RUNNER_ASSERT_MSG(
1439                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1440                                         pub_alias,
1441                                         pub_passwd,
1442                                         msg_buff,
1443                                         *signature,
1444                                         hash_algo,
1445                                         pad_algo)),
1446                         CKMCReadableError(temp));
1447 }
1448
1449 RUNNER_TEST(T3086_CAPI__dsa_ext_key_create_verify_with_negative)
1450 {
1451         int temp;
1452
1453         const std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1454                 "MIIBtzCCASwGByqGSM44BAEwggEfAoGBALeveaD/EheW+ws1YuW77f344+brkEzm\n"
1455                 "BVfFYHr7t+jwu6nQe341SoESJG+PCgrrhy76KNDCfveiwEoWufVHnI4bYBU/ClzP\n"
1456                 "A3amf6c5yud45ZR/b6OiAuew6ohY0mQGnzqeio8BaCsZaJ6EziCSlkdIDJisSfPg\n"
1457                 "nlWHqf4AwHVdAhUA7I1JQ7sBFJ+N19w3Omu+aO8EG08CgYEAldagy/Ccxhh43cZu\n"
1458                 "AZQxgJLCcp1jg6NdPMdkZ2TcSijvaVxBu+gjEGOqN5Os2V6UF7S/k/rjHYmcX9ux\n"
1459                 "gpjkC31yTNrKyERIAFIYZtG2K7LVBUZq5Fgm7I83QBVchJ2PA7mBaugJFEhNjbhK\n"
1460                 "NRip5UH38le1YDZ/IiA+svFOpeoDgYQAAoGAPT91aEgwFdulzmHlvr3k+GBCE9z+\n"
1461                 "hq0c3FGUCtGbVOqg2KPqMBgwSb4MC0msQys4DTVZhLJI+C5eIPEHgfBMqY1ZNJdO\n"
1462                 "OSCQciDXnRfSqKbT6tjDTgR5jmh5bG1Q8QFeBTHCDsQHoQYWgx0nyu12lASN80rC\n"
1463                 "YMYCBhubtrVaLmc=\n"
1464                 "-----END PUBLIC KEY-----";
1465
1466         const std::string priv = "-----BEGIN DSA PRIVATE KEY-----\n"
1467                 "MIIBvAIBAAKBgQC3r3mg/xIXlvsLNWLlu+39+OPm65BM5gVXxWB6+7fo8Lup0Ht+\n"
1468                 "NUqBEiRvjwoK64cu+ijQwn73osBKFrn1R5yOG2AVPwpczwN2pn+nOcrneOWUf2+j\n"
1469                 "ogLnsOqIWNJkBp86noqPAWgrGWiehM4gkpZHSAyYrEnz4J5Vh6n+AMB1XQIVAOyN\n"
1470                 "SUO7ARSfjdfcNzprvmjvBBtPAoGBAJXWoMvwnMYYeN3GbgGUMYCSwnKdY4OjXTzH\n"
1471                 "ZGdk3Eoo72lcQbvoIxBjqjeTrNlelBe0v5P64x2JnF/bsYKY5At9ckzayshESABS\n"
1472                 "GGbRtiuy1QVGauRYJuyPN0AVXISdjwO5gWroCRRITY24SjUYqeVB9/JXtWA2fyIg\n"
1473                 "PrLxTqXqAoGAPT91aEgwFdulzmHlvr3k+GBCE9z+hq0c3FGUCtGbVOqg2KPqMBgw\n"
1474                 "Sb4MC0msQys4DTVZhLJI+C5eIPEHgfBMqY1ZNJdOOSCQciDXnRfSqKbT6tjDTgR5\n"
1475                 "jmh5bG1Q8QFeBTHCDsQHoQYWgx0nyu12lASN80rCYMYCBhubtrVaLmcCFQC0IB4m\n"
1476                 "u1roOuaPY+Hl19BlTE2qdw==\n"
1477                 "-----END DSA PRIVATE KEY-----";
1478
1479         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1480
1481         const char *pub_alias = "dsa-pub1";
1482         const char *pri_alias = "dsa-prv1";
1483         char *pri_passwd = NULL;
1484         char *pub_passwd = NULL;
1485         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1486         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1487         ckmc_raw_buffer_s *signature = NULL;
1488
1489         ckmc_key_s pubkey;
1490         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1491         pubkey.key_size = pub.size();
1492         pubkey.key_type = CKMC_KEY_NONE;
1493         pubkey.password = NULL;
1494
1495         ckmc_policy_s pubpolicy;
1496         pubpolicy.password = pub_passwd;
1497         pubpolicy.extractable = 0;
1498
1499         ckmc_policy_s pripolicy;
1500         pripolicy.password = pri_passwd;
1501         pripolicy.extractable = 1;
1502
1503         ckmc_key_s prikey;
1504         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(priv.c_str()));
1505         prikey.key_size = priv.size();
1506         prikey.key_type = CKMC_KEY_NONE;
1507         prikey.password = NULL;
1508
1509
1510         RUNNER_ASSERT_MSG(
1511                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias, prikey, pripolicy)),
1512                         CKMCReadableError(temp));
1513
1514         RUNNER_ASSERT_MSG(
1515                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias, pubkey, pubpolicy)),
1516                         CKMCReadableError(temp));
1517
1518         RUNNER_ASSERT_MSG(
1519                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1520                                         pri_alias,
1521                                         pri_passwd,
1522                                         msg_buff,
1523                                         hash_algo,
1524                                         pad_algo,
1525                                         &signature)),
1526                         CKMCReadableError(temp));
1527
1528         // positive test
1529         RUNNER_ASSERT_MSG(
1530                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1531                                         pub_alias,
1532                                         pub_passwd,
1533                                         msg_buff,
1534                                         *signature,
1535                                         hash_algo,
1536                                         pad_algo)),
1537                         CKMCReadableError(temp));
1538
1539         // negative test
1540         ckmc_raw_buffer_s invalid_msg_buff = prepare_message_buffer("invalid message test");
1541         RUNNER_ASSERT_MSG(
1542                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1543                                         pub_alias,
1544                                         pub_passwd,
1545                                         invalid_msg_buff,
1546                                         *signature,
1547                                         hash_algo,
1548                                         pad_algo)),
1549                         CKMCReadableError(temp));
1550
1551         ckmc_buffer_free(signature);
1552 }
1553
1554 RUNNER_TEST(T3087_CAPI__dsa_int_key_create_verify_with_negative)
1555 {
1556         int temp;
1557
1558         size_t size = 1024;
1559         ckmc_policy_s policy_private_key;
1560         ckmc_policy_s policy_public_key;
1561
1562         policy_private_key.password = NULL;
1563         policy_private_key.extractable = 1;
1564
1565         policy_public_key.password = NULL;
1566         policy_public_key.extractable = 1;
1567
1568         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1569
1570         const char *pub_alias = "dsa-pub2";
1571         const char *pri_alias = "dsa-prv2";
1572
1573         RUNNER_ASSERT_MSG(
1574                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, pri_alias, pub_alias, policy_private_key, policy_public_key)),
1575                         "Error=" << temp);
1576
1577         char *pri_passwd = NULL;
1578         char *pub_passwd = NULL;
1579         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1580         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1581         ckmc_raw_buffer_s *signature;
1582
1583         ckmc_key_s *pubkey = NULL;
1584         ckmc_key_s *prikey = NULL;
1585         RUNNER_ASSERT_MSG(
1586                         CKMC_ERROR_NONE == (temp = ckmc_get_key(pri_alias, 0, &prikey)),
1587                         CKMCReadableError(temp));
1588
1589         RUNNER_ASSERT_MSG(
1590                         CKMC_ERROR_NONE == (temp = ckmc_get_key(pub_alias, 0, &pubkey)),
1591                         CKMCReadableError(temp));
1592
1593         RUNNER_ASSERT_MSG(
1594                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1595                                         pri_alias,
1596                                         pri_passwd,
1597                                         msg_buff,
1598                                         hash_algo,
1599                                         pad_algo,
1600                                         &signature)),
1601                         CKMCReadableError(temp));
1602
1603         // positive test
1604         RUNNER_ASSERT_MSG(
1605                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1606                                         pub_alias,
1607                                         pub_passwd,
1608                                         msg_buff,
1609                                         *signature,
1610                                         hash_algo,
1611                                         pad_algo)),
1612                         CKMCReadableError(temp));
1613
1614         // negative test
1615         ckmc_raw_buffer_s invalid_msg_buff = prepare_message_buffer("invalid message test");
1616         RUNNER_ASSERT_MSG(
1617                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1618                                         pub_alias,
1619                                         pub_passwd,
1620                                         invalid_msg_buff,
1621                                         *signature,
1622                                         hash_algo,
1623                                         pad_algo)),
1624                         CKMCReadableError(temp));
1625
1626         ckmc_key_free(prikey);
1627         ckmc_key_free(pubkey);
1628         ckmc_buffer_free(signature);
1629 }
1630
1631 RUNNER_TEST(T3088_CAPI__ecdsa_cert_create_verify_signature)
1632 {
1633         int temp;
1634
1635         std::string prv =
1636                 "-----BEGIN EC PRIVATE KEY-----\n"
1637                 "MIH8AgEBBBRPb/2utS5aCtyuwmzIHpU6LH3mc6CBsjCBrwIBATAgBgcqhkjOPQEB\n"
1638                 "AhUA/////////////////////3////8wQwQU/////////////////////3////wE\n"
1639                 "FByXvvxUvXqLZaz4n4HU1K3FZfpFAxUAEFPN5CwU1pbmdodWFRdTO/P4M0UEKQRK\n"
1640                 "lrVojvVzKEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAAAAAA\n"
1641                 "AAAAAfTI+Seu08p1IlcCAQGhLAMqAATehLqu61gKC3Tgr4wQMVoguAhhG3Uwwz8u\n"
1642                 "ELyhe7yPCAuOoLZlTLgf\n"
1643                 "-----END EC PRIVATE KEY-----\n";
1644
1645         std::string pub =
1646                 "-----BEGIN CERTIFICATE-----\n"
1647                 "MIICfDCCAjqgAwIBAgIJANIytpeTKlXBMAkGByqGSM49BAEwXjELMAkGA1UEBhMC\n"
1648                 "UEwxDTALBgNVBAgMBExvZHoxDTALBgNVBAcMBExvZHoxEDAOBgNVBAoMB1NhbXN1\n"
1649                 "bmcxETAPBgNVBAsMCFNlY3VyaXR5MQwwCgYDVQQDDANDS00wHhcNMTQwNzAyMTI0\n"
1650                 "MTQ3WhcNMTcwNzAxMTI0MTQ3WjBeMQswCQYDVQQGEwJQTDENMAsGA1UECAwETG9k\n"
1651                 "ejENMAsGA1UEBwwETG9kejEQMA4GA1UECgwHU2Ftc3VuZzERMA8GA1UECwwIU2Vj\n"
1652                 "dXJpdHkxDDAKBgNVBAMMA0NLTTCB6jCBuwYHKoZIzj0CATCBrwIBATAgBgcqhkjO\n"
1653                 "PQEBAhUA/////////////////////3////8wQwQU/////////////////////3//\n"
1654                 "//wEFByXvvxUvXqLZaz4n4HU1K3FZfpFAxUAEFPN5CwU1pbmdodWFRdTO/P4M0UE\n"
1655                 "KQRKlrVojvVzKEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAA\n"
1656                 "AAAAAAAAAfTI+Seu08p1IlcCAQEDKgAE3oS6rutYCgt04K+MEDFaILgIYRt1MMM/\n"
1657                 "LhC8oXu8jwgLjqC2ZUy4H6NQME4wHQYDVR0OBBYEFELElWx3kbLo55Cfn1vywsEZ\n"
1658                 "ccsmMB8GA1UdIwQYMBaAFELElWx3kbLo55Cfn1vywsEZccsmMAwGA1UdEwQFMAMB\n"
1659                 "Af8wCQYHKoZIzj0EAQMxADAuAhUAumC4mGoyK97SxTvVBQ+ELfCbxEECFQCbMJ72\n"
1660                 "Q1oBry6NEc+lLFmWMDesAA==\n"
1661                 "-----END CERTIFICATE-----\n";
1662
1663         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1664
1665         const char *pri_alias = "prv4";
1666         const char *pub_alias = "pub4";
1667         char *key_passwd = NULL;
1668         char *pri_passwd = NULL;
1669         char *pub_passwd = NULL;
1670         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1671         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1672         ckmc_raw_buffer_s *signature;
1673
1674         ckmc_cert_s cert;
1675         cert.raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1676         cert.cert_size = pub.size();
1677         cert.data_format = CKMC_FORM_PEM;
1678
1679         ckmc_policy_s certpolicy;
1680         certpolicy.password = pub_passwd;
1681         certpolicy.extractable = 1;
1682
1683         ckmc_key_s prikey;
1684         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1685         prikey.key_size = prv.size();
1686         prikey.key_type = CKMC_KEY_NONE;
1687         prikey.password = key_passwd;
1688
1689         ckmc_policy_s pripolicy;
1690         pripolicy.password = pri_passwd;
1691         pripolicy.extractable = 0;
1692
1693
1694         RUNNER_ASSERT_MSG(
1695                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(pub_alias, cert, certpolicy)),
1696                         CKMCReadableError(temp));
1697         RUNNER_ASSERT_MSG(
1698                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias, prikey, pripolicy)),
1699                         CKMCReadableError(temp));
1700
1701         RUNNER_ASSERT_MSG(
1702                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1703                                         pri_alias,
1704                                         pri_passwd,
1705                                         msg_buff,
1706                                         hash_algo,
1707                                         pad_algo,
1708                                         &signature)),
1709                         CKMCReadableError(temp));
1710
1711         RUNNER_ASSERT_MSG(
1712                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1713                                         pub_alias,
1714                                         pub_passwd,
1715                                         msg_buff,
1716                                         *signature,
1717                                         hash_algo,
1718                                         pad_algo)),
1719                         CKMCReadableError(temp));
1720
1721         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1722         memcpy((void*)signature->data, "BROKEN", 6);
1723
1724         RUNNER_ASSERT_MSG(
1725                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1726                                         pub_alias,
1727                                         pub_passwd,
1728                                         msg_buff,
1729                                         *signature,
1730                                         hash_algo,
1731                                         pad_algo)),
1732                         CKMCReadableError(temp));
1733 }
1734
1735 RUNNER_TEST(T3089_CAPI__deinit)
1736 {
1737         int temp;
1738
1739         RUNNER_ASSERT_MSG(
1740                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
1741                         CKMCReadableError(temp));
1742         RUNNER_ASSERT_MSG(
1743                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
1744                         CKMCReadableError(temp));
1745 }
1746
1747
1748 //#######################################################################################
1749
1750 void _assertKey(ckmc_key_s *key, unsigned char *raw_key, unsigned int key_size, ckmc_key_type_e key_type, char *password)
1751 {
1752         RUNNER_ASSERT_MSG(key->key_size == key_size, "Key Size Error" );
1753         RUNNER_ASSERT_MSG(key->key_type == key_type, "Key Type Error" );
1754
1755         if(key->password != NULL && password != NULL) {
1756                 RUNNER_ASSERT_MSG(strcmp(key->password, password) == 0, "Password Error" );
1757         }else if(key->password == NULL && password == NULL) {
1758                 RUNNER_ASSERT_MSG(true, "Password Error" );
1759         }else {
1760                 RUNNER_ASSERT_MSG(false, "Password Error" );
1761         }
1762
1763         if(key->raw_key != NULL && raw_key != NULL) {
1764                 for(unsigned int i=0; i<key_size; i++) {
1765                         RUNNER_ASSERT_MSG((key->raw_key)[i] == raw_key[i],  "Raw Key Error" );
1766                 }
1767         }else if(key->raw_key == NULL && raw_key == NULL) {
1768                 RUNNER_ASSERT_MSG(true,  "Raw Key Error" );
1769         }else {
1770                 RUNNER_ASSERT_MSG(false,  "Raw Key Error" );
1771         }
1772 }
1773
1774 RUNNER_TEST_GROUP_INIT(T309_CKMC_CAPI_TYPES);
1775
1776 RUNNER_TEST(T3091_CAPI_TYPE_init)
1777 {
1778         int temp;
1779         RUNNER_ASSERT_MSG(
1780                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
1781                         CKMCReadableError(temp));
1782         RUNNER_ASSERT_MSG(
1783                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
1784                         CKMCReadableError(temp));
1785         RUNNER_ASSERT_MSG(
1786                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "test-pass")),
1787                         CKMCReadableError(temp));
1788         RUNNER_ASSERT_MSG(
1789                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
1790                         CKMCReadableError(temp));
1791 }
1792
1793 RUNNER_TEST(T3092_CAPI_TYPE_KEY)
1794 {
1795         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
1796                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1797                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1798                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1799                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1800                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1801                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1802                 "zQIDAQAB\n"
1803                 "-----END PUBLIC KEY-----";
1804
1805         unsigned char *raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(keyPem.c_str()));
1806         unsigned int key_size = keyPem.size();
1807         ckmc_key_type_e key_type = CKMC_KEY_NONE;
1808         char *password = const_cast< char *>("");
1809
1810         ckmc_key_s *key;
1811         ckmc_key_new(raw_key, key_size, key_type, password, &key);
1812
1813         _assertKey(key, raw_key, key_size, key_type, password);
1814         ckmc_key_free(key);
1815
1816         char *passwordNull = NULL;
1817         ckmc_key_s *key2;
1818         ckmc_key_new(raw_key, key_size, key_type, passwordNull, &key2);
1819         ckmc_key_free(key2);
1820 }
1821
1822 RUNNER_TEST(T3093_CAPI_TYPE_BUFFER)
1823 {
1824         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
1825                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1826                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1827                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1828                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1829                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1830                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1831                 "zQIDAQAB\n"
1832                 "-----END PUBLIC KEY-----";
1833
1834         unsigned char *data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(keyPem.c_str()));
1835         unsigned int size = keyPem.size();
1836
1837         ckmc_raw_buffer_s *buff;
1838         ckmc_buffer_new(data, size, &buff);
1839
1840         RUNNER_ASSERT_MSG(buff->size == size, "Size Error" );
1841
1842         if(buff->data != NULL && data != NULL) {
1843                 for(unsigned int i=0; i<size; i++) {
1844                         RUNNER_ASSERT_MSG((buff->data)[i] == data[i],  "Raw data Error" );
1845                 }
1846         }else if(buff->data == NULL && data == NULL) {
1847                 RUNNER_ASSERT_MSG(true,  "Raw data Error" );
1848         }else {
1849                 RUNNER_ASSERT_MSG(false,  "Raw data Error" );
1850         }
1851
1852         ckmc_buffer_free(buff);
1853 }
1854
1855 RUNNER_TEST(T3094_CAPI_TYPE_CERT)
1856 {
1857     std::string certPem = TestData::getTestCertificateBase64(TestData::GIAG2);
1858
1859         unsigned char *raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(certPem.c_str()));
1860         unsigned int size = certPem.size();
1861         ckmc_data_format_e form = CKMC_FORM_PEM;
1862
1863         ckmc_cert_s *ckmCert;
1864         ckmc_cert_new(raw_cert, size, form, &ckmCert);
1865
1866         RUNNER_ASSERT_MSG(ckmCert->cert_size == size, "Size Error" );
1867
1868         if(ckmCert->raw_cert != NULL && raw_cert != NULL) {
1869                 for(unsigned int i=0; i<size; i++) {
1870                         RUNNER_ASSERT_MSG((ckmCert->raw_cert)[i] == raw_cert[i],  "Raw data Error" );
1871                 }
1872         }else if(ckmCert->raw_cert == NULL && raw_cert == NULL) {
1873                 RUNNER_ASSERT_MSG(true,  "raw_cert Error" );
1874         }else {
1875                 RUNNER_ASSERT_MSG(false,  "raw_cert Error" );
1876         }
1877
1878         RUNNER_ASSERT_MSG(ckmCert->data_format == form, "ckmc_cert_form Error" );
1879
1880         ckmc_cert_free(ckmCert);
1881 }
1882
1883
1884 RUNNER_TEST(T3095_CAPI_TYPE_load_cert_file)
1885 {
1886         int ret;
1887
1888     std::string certStr = TestData::getTestCertificateBase64(TestData::MBANK);
1889
1890         const char *file_name = "/tmp/ckmc_test_cert.pem";
1891         remove(file_name);
1892
1893         FILE* cert_file;
1894         cert_file = fopen(file_name, "w");
1895         fprintf(cert_file, "%s",certStr.c_str());
1896         fclose(cert_file);
1897
1898         ckmc_cert_s *pcert;
1899
1900         RUNNER_ASSERT_MSG(
1901                         CKMC_ERROR_NONE == (ret = ckmc_load_cert_from_file(file_name, &pcert)),
1902             CKMCReadableError(ret));
1903
1904         RUNNER_ASSERT_MSG(
1905                         pcert != NULL && pcert->cert_size > 0,"Fail to load cert from file.");
1906
1907         const char *lcert_alias = "lcert_alias";
1908         ckmc_policy_s policy;
1909         policy.password = NULL;
1910         policy.extractable = 1;
1911
1912         RUNNER_ASSERT_MSG(
1913                         CKMC_ERROR_NONE == (ret = ckmc_save_cert(lcert_alias, *pcert, policy)),
1914                         CKMCReadableError(ret));
1915
1916         remove(file_name);
1917 }
1918
1919 RUNNER_TEST(T3096_CAPI_TYPE_load_p12_file) {
1920         const char *p12file = "/usr/share/ckm-test/capi-t3096.p12";
1921         const char *password = "password";
1922
1923         int temp;
1924
1925         ckmc_key_s *private_key = NULL;
1926         ckmc_cert_s *cert = NULL;
1927         ckmc_cert_list_s *ca_cert_list = NULL;
1928
1929         RUNNER_ASSERT_MSG(
1930                         CKMC_ERROR_NONE == (temp = ckmc_load_from_pkcs12_file(p12file, password,
1931                                         &private_key, &cert, &ca_cert_list)),
1932                         "Error=" << temp);
1933         RUNNER_ASSERT_MSG(private_key != NULL, "Null private_key");
1934         RUNNER_ASSERT_MSG(cert != NULL, "Null cert");
1935         RUNNER_ASSERT_MSG(ca_cert_list != NULL, "Null ca_cert_list");
1936
1937         ckmc_policy_s policy;
1938         policy.password = NULL;
1939         policy.extractable = 1;
1940
1941
1942         const char *pkey_alias = "pkey_alias";
1943         RUNNER_ASSERT_MSG(
1944                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pkey_alias, *private_key, policy)),
1945                         CKMCReadableError(temp));
1946
1947         const char *cert_alias = "cert_alias";
1948         RUNNER_ASSERT_MSG(
1949                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(cert_alias, *cert, policy)),
1950                         CKMCReadableError(temp));
1951         std::string caCertAlias = "ca_cert_alias_";
1952         const char *idx = "0";
1953         int cnt = 0;
1954         ckmc_cert_list_s *tmpList = ca_cert_list;
1955         while(tmpList != NULL) {
1956                 caCertAlias.append(idx);
1957                 RUNNER_ASSERT_MSG(
1958                                 CKMC_ERROR_NONE == (temp = ckmc_save_cert(caCertAlias.c_str(), *(tmpList->cert), policy)),
1959                                 CKMCReadableError(temp));
1960                 tmpList = tmpList->next;
1961                 cnt ++;
1962         }
1963
1964         RUNNER_ASSERT_MSG(cnt ==  2, "Invalid CA Cert Count");
1965
1966         ckmc_key_free(private_key);
1967         ckmc_cert_free(cert);
1968         ckmc_cert_list_all_free(ca_cert_list);
1969 }
1970
1971 RUNNER_TEST(T3097_CAPI_TYPE_load_p12_file2) {
1972     const char *p12file = "/usr/share/ckm-test/capi-t3096.p12";
1973     const char *password = "password";
1974
1975     int temp;
1976
1977     ckmc_pkcs12_s *ppkcs12 = NULL;
1978
1979     RUNNER_ASSERT_MSG(
1980             CKMC_ERROR_NONE == (temp = ckmc_pkcs12_load(p12file, password, &ppkcs12)),
1981             CKMCReadableError(temp));
1982     RUNNER_ASSERT_MSG(ppkcs12->priv_key != NULL, "Null private_key");
1983     RUNNER_ASSERT_MSG(ppkcs12->cert != NULL, "Null cert");
1984     RUNNER_ASSERT_MSG(ppkcs12->ca_chain != NULL, "Null ca_cert_list");
1985
1986     ckmc_policy_s policy;
1987     policy.password = NULL;
1988     policy.extractable = 1;
1989
1990
1991     const char *pkey_alias = "pkey_alias2";
1992     RUNNER_ASSERT_MSG(
1993             CKMC_ERROR_NONE == (temp = ckmc_save_key(pkey_alias, *(ppkcs12->priv_key), policy)),
1994             CKMCReadableError(temp));
1995
1996     const char *cert_alias = "cert_alias2";
1997     RUNNER_ASSERT_MSG(
1998             CKMC_ERROR_NONE == (temp = ckmc_save_cert(cert_alias, *(ppkcs12->cert), policy)),
1999             CKMCReadableError(temp));
2000     std::string caCertAlias = "ca_cert_alias_2_";
2001     const char *idx = "0";
2002     int cnt = 0;
2003     ckmc_cert_list_s *tmpList = ppkcs12->ca_chain;
2004     while(tmpList != NULL) {
2005         caCertAlias.append(idx);
2006         RUNNER_ASSERT_MSG(
2007                 CKMC_ERROR_NONE == (temp = ckmc_save_cert(caCertAlias.c_str(), *(tmpList->cert), policy)),
2008                 CKMCReadableError(temp));
2009         tmpList = tmpList->next;
2010         cnt ++;
2011     }
2012
2013     RUNNER_ASSERT_MSG(cnt ==  2, "Invalid CA Cert Count");
2014
2015     ckmc_pkcs12_free(ppkcs12);
2016 }
2017
2018 RUNNER_TEST(T3098_CAPI_TYPE_deinit)
2019 {
2020         int temp;
2021         RUNNER_ASSERT_MSG(
2022                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
2023                         CKMCReadableError(temp));
2024         RUNNER_ASSERT_MSG(
2025                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
2026                         CKMCReadableError(temp));
2027         RUNNER_ASSERT_MSG(
2028                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
2029                         CKMCReadableError(temp));
2030         RUNNER_ASSERT_MSG(
2031                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
2032                         CKMCReadableError(temp));
2033 }
2034
2035 RUNNER_TEST_GROUP_INIT(T310_CKMC_CAPI_PKCS12);
2036
2037 namespace
2038 {
2039 const char* alias_PKCS_exportable = "CAPI-test-PKCS-export";
2040 const char* alias_PKCS_not_exportable = "CAPI-test-PKCS-no-export";
2041 }
2042
2043 RUNNER_TEST(T3101_CAPI_PKCS12_init)
2044 {
2045     int temp;
2046     RUNNER_ASSERT_MSG(
2047             CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
2048             CKMCReadableError(temp));
2049     RUNNER_ASSERT_MSG(
2050             CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "PKCS-pass")),
2051             CKMCReadableError(temp));
2052 }
2053
2054 RUNNER_TEST(T3102_CAPI_PKCS12_negative_wrong_password)
2055 {
2056     const char *wrong_passwd = "wrong";
2057     ckmc_pkcs12_s *ppkcs12 = NULL;
2058     int temp;
2059     RUNNER_ASSERT_MSG(
2060             CKMC_ERROR_INVALID_FORMAT == (temp = ckmc_pkcs12_load("/usr/share/ckm-test/test1801.pkcs12", wrong_passwd, &ppkcs12)),
2061             CKMCReadableError(temp));
2062 }
2063
2064 RUNNER_TEST(T3103_CAPI_PKCS12_add_bundle_with_chain_certs)
2065 {
2066     ckmc_pkcs12_s *ppkcs12 = NULL;
2067     int temp;
2068     RUNNER_ASSERT_MSG(
2069             CKMC_ERROR_NONE == (temp = ckmc_pkcs12_load("/usr/share/ckm-test/pkcs.p12", NULL, &ppkcs12)),
2070             CKMCReadableError(temp));
2071
2072     RUNNER_ASSERT_MSG(NULL != ppkcs12->cert, "no certificate in PKCS12");
2073     RUNNER_ASSERT_MSG(NULL != ppkcs12->priv_key, "no private key in PKCS12");
2074     RUNNER_ASSERT_MSG(NULL != ppkcs12->ca_chain, "no chain certificates in PKCS12");
2075
2076     // save to the CKM
2077     ckmc_policy_s exportable;
2078     exportable.password = NULL;
2079     exportable.extractable = 1;
2080     ckmc_policy_s notExportable;
2081     notExportable.password = NULL;
2082     notExportable.extractable = 0;
2083
2084     RUNNER_ASSERT_MSG(
2085             CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_exportable, ppkcs12, exportable, exportable)),
2086             CKMCReadableError(temp));
2087     RUNNER_ASSERT_MSG(
2088             CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_exportable, ppkcs12, exportable, exportable)),
2089             CKMCReadableError(temp));
2090     RUNNER_ASSERT_MSG(
2091             CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_not_exportable, ppkcs12, notExportable, notExportable)),
2092             CKMCReadableError(temp));
2093     RUNNER_ASSERT_MSG(
2094             CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_not_exportable, ppkcs12, notExportable, notExportable)),
2095             CKMCReadableError(temp));
2096
2097     // try to lookup key
2098     ckmc_key_s *key_lookup = NULL;
2099     RUNNER_ASSERT_MSG(
2100             CKMC_ERROR_NONE == (temp = ckmc_get_key(alias_PKCS_exportable, NULL, &key_lookup)),
2101             CKMCReadableError(temp));
2102     ckmc_key_free(key_lookup);
2103     key_lookup = NULL;
2104     RUNNER_ASSERT_MSG(
2105             CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_key(alias_PKCS_not_exportable, "", &key_lookup)),
2106             CKMCReadableError(temp));
2107     ckmc_key_free(key_lookup);
2108
2109     // try to lookup certificate
2110     ckmc_cert_s *cert_lookup = NULL;
2111     RUNNER_ASSERT_MSG(
2112             CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias_PKCS_exportable, NULL, &cert_lookup)),
2113             CKMCReadableError(temp));
2114     ckmc_cert_free(cert_lookup);
2115     cert_lookup = NULL;
2116     RUNNER_ASSERT_MSG(
2117             CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_cert(alias_PKCS_not_exportable, NULL, &cert_lookup)),
2118             CKMCReadableError(temp));
2119     ckmc_cert_free(cert_lookup);
2120 }
2121
2122 RUNNER_TEST(T3104_CAPI_PKCS12_get_PKCS)
2123 {
2124     int temp;
2125     ckmc_pkcs12_s *pkcs = NULL;
2126
2127     // fail - no entry
2128     RUNNER_ASSERT_MSG(
2129             CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_pkcs12("i-do-not-exist", NULL, NULL, &pkcs)),
2130             CKMCReadableError(temp));
2131     ckmc_pkcs12_free(pkcs);
2132     pkcs = NULL;
2133
2134     // fail - not exportable
2135     RUNNER_ASSERT_MSG(
2136             CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_pkcs12(alias_PKCS_not_exportable, NULL, NULL, &pkcs)),
2137             CKMCReadableError(temp));
2138     ckmc_pkcs12_free(pkcs);
2139     pkcs = NULL;
2140
2141     // success - exportable
2142     RUNNER_ASSERT_MSG(
2143             CKMC_ERROR_NONE == (temp = ckmc_get_pkcs12(alias_PKCS_exportable, NULL, NULL, &pkcs)),
2144             CKMCReadableError(temp));
2145
2146     RUNNER_ASSERT_MSG(NULL != pkcs->cert, "no certificate in PKCS12");
2147     RUNNER_ASSERT_MSG(NULL != pkcs->priv_key, "no private key in PKCS12");
2148     RUNNER_ASSERT_MSG(NULL != pkcs->ca_chain, "no chain certificates in PKCS12");
2149     size_t cntr = 0;
2150     ckmc_cert_list_s *iter = pkcs->ca_chain;
2151     do {
2152         cntr ++;
2153         iter = iter->next;
2154     } while(iter);
2155     RUNNER_ASSERT_MSG(2 == cntr, "invalid number of chain certificates in PKCS12");
2156
2157     ckmc_pkcs12_free(pkcs);
2158 }
2159
2160 RUNNER_TEST(T3105_CAPI_PKCS12_create_and_verify_signature)
2161 {
2162     ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
2163
2164     int temp;
2165     ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
2166     ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
2167     ckmc_raw_buffer_s *signature = NULL;
2168
2169     RUNNER_ASSERT_MSG(
2170             CKMC_ERROR_NONE == (temp = ckmc_create_signature(
2171                     alias_PKCS_exportable,
2172                     NULL,
2173                     msg_buff,
2174                     hash_algo,
2175                     pad_algo,
2176                     &signature)),
2177             CKMCReadableError(temp));
2178
2179     RUNNER_ASSERT_MSG(
2180             CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
2181                     alias_PKCS_exportable,
2182                     NULL,
2183                     msg_buff,
2184                     *signature,
2185                     hash_algo,
2186                     pad_algo)),
2187             CKMCReadableError(temp));
2188 }
2189
2190 RUNNER_TEST(T3106_CAPI_PKCS12_remove_bundle_with_chain_certs)
2191 {
2192     int tmp;
2193
2194     // remove the whole PKCS12 bundles
2195     RUNNER_ASSERT_MSG(
2196             CKMC_ERROR_NONE == (tmp = ckmc_remove_pkcs12(alias_PKCS_exportable)),
2197             CKMCReadableError(tmp));
2198     RUNNER_ASSERT_MSG(
2199             CKMC_ERROR_NONE == (tmp = ckmc_remove_pkcs12(alias_PKCS_not_exportable)),
2200             CKMCReadableError(tmp));
2201
2202     // expect lookup fails due to unknown alias
2203     // try to lookup key
2204     ckmc_key_s *key_lookup = NULL;
2205     RUNNER_ASSERT_MSG(
2206             CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_exportable, NULL, &key_lookup)),
2207             CKMCReadableError(tmp));
2208     ckmc_key_free(key_lookup);
2209     key_lookup = NULL;
2210     RUNNER_ASSERT_MSG(
2211             CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_not_exportable, NULL, &key_lookup)),
2212             CKMCReadableError(tmp));
2213     ckmc_key_free(key_lookup);
2214
2215     // try to lookup certificate
2216     ckmc_cert_s *cert_lookup = NULL;
2217     RUNNER_ASSERT_MSG(
2218             CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_exportable, NULL, &cert_lookup)),
2219             CKMCReadableError(tmp));
2220     ckmc_cert_free(cert_lookup);
2221     cert_lookup = NULL;
2222     RUNNER_ASSERT_MSG(
2223             CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_not_exportable, NULL, &cert_lookup)),
2224             CKMCReadableError(tmp));
2225     ckmc_cert_free(cert_lookup);
2226 }
2227
2228 RUNNER_TEST(T3109_CAPI_PKCS12_deinit)
2229 {
2230     int temp;
2231     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
2232             CKMCReadableError(temp));
2233     RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
2234             CKMCReadableError(temp));
2235 }
2236
2237
2238 RUNNER_TEST_GROUP_INIT(T320_CAPI_EMPTY_DATABASE);
2239
2240 RUNNER_TEST(T3201_CAPI_unlock_database)
2241 {
2242         int temp;
2243         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
2244                 CKMCReadableError(temp));
2245         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
2246                 CKMCReadableError(temp));
2247         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(0, "simple-password")),
2248                 CKMCReadableError(temp));
2249 }
2250
2251 RUNNER_CHILD_TEST(T3202_CAPI_get_data_from_empty_database)
2252 {
2253         int temp;
2254         char alias[10] = "mykey";
2255         char *password = NULL;
2256         ckmc_key_s *test_key = NULL;
2257
2258         RUNNER_ASSERT_MSG(
2259                 CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_key(alias, password, &test_key)),
2260                 "Error=" << temp);
2261
2262         RUNNER_ASSERT_MSG(NULL == test_key, "Key value should not be changed");
2263 }
2264
2265 RUNNER_CHILD_TEST(T3203_CAPI_lock_database)
2266 {
2267         int temp;
2268         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(0)),
2269                 CKMCReadableError(temp));
2270 }
2271
2272 RUNNER_CHILD_TEST(T3204_CAPI_get_data_from_locked_database)
2273 {
2274         int temp;
2275         char alias[10] = "mykey";
2276         char *password = NULL;
2277         ckmc_key_s *test_key = NULL;
2278
2279         RUNNER_ASSERT_MSG(
2280                 CKMC_ERROR_DB_LOCKED == (temp = ckmc_get_key(alias, password, &test_key)),
2281                 CKMCReadableError(temp));
2282
2283         RUNNER_ASSERT_MSG(NULL == test_key, "Key value should not be changed");
2284 }
2285
2286 RUNNER_TEST(T3204_deinit)
2287 {
2288         int temp;
2289         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(0)),
2290                 CKMCReadableError(temp));
2291 }
2292