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