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