0e0cfca6582a24ff272d69669dcf3f1966d903a8
[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::THIRD_PARTY_LEAF);
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::THIRD_PARTY_LEAF);
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::THIRD_PARTY_LEAF);
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_alias_new_C_API)
598 {
599         char *full_alias = NULL;
600         std::string owner_id = "iocma412ovyc";
601         std::string alias = "fvyuweq27c";
602
603         std::string full_alias_str = owner_id + ckmc_owner_id_separator + alias;
604
605         int temp = ckmc_alias_new(owner_id.c_str(), alias.c_str(), &full_alias);
606         std::unique_ptr<char, void(*)(void *)> p(full_alias, ::free);
607         RUNNER_ASSERT_MSG(temp == CKMC_ERROR_NONE, CKMCReadableError(temp));
608         RUNNER_ASSERT_MSG(full_alias_str == full_alias,
609                         "Invalid full alias. expected(" << full_alias_str <<
610                         ") actual(" << full_alias << ")");
611 }
612
613 RUNNER_TEST(T3037_deinit_C_API)
614 {
615         int temp;
616
617         RUNNER_ASSERT_MSG(
618                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
619                         CKMCReadableError(temp));
620         RUNNER_ASSERT_MSG(
621                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
622                         CKMCReadableError(temp));
623 }
624
625 RUNNER_TEST_GROUP_INIT (T3040_CKMC_QUICK_REMOVE_BIN_DATA_TEST_C_API);
626
627 RUNNER_TEST(T3041_init_C_API)
628 {
629         reset_user_data(USER_APP, "simple-password");
630 }
631
632 RUNNER_TEST(T3042_save_get_bin_data_C_API)
633 {
634         int temp;
635
636         ckmc_raw_buffer_s testData1, testData2, testData3;
637         char* password = NULL;
638
639         std::string binData1 = "My bin data1";
640         std::string binData2 = "My bin data2";
641         std::string binData3 = "My bin data3";
642         char* char_binData1 = new char[binData1.length() + 1];
643         char* char_binData2 = new char[binData2.length() + 1];
644         char* char_binData3 = new char[binData3.length() + 1];
645         std::strcpy(char_binData1, binData1.c_str());
646         std::strcpy(char_binData2, binData2.c_str());
647         std::strcpy(char_binData3, binData3.c_str());
648         testData1.data = (unsigned char *) char_binData1;
649         testData2.data = (unsigned char *) char_binData2;
650         testData3.data = (unsigned char *) char_binData3;
651         testData1.size = binData1.length()+1;
652         testData2.size = binData2.length()+1;
653         testData3.size = binData3.length()+1;
654
655         ckmc_policy_s test_policy1, test_policy2, test_policy3;
656
657         test_policy1.password = password;
658         test_policy1.extractable = 1;
659         test_policy2.password = password;
660         test_policy2.extractable = 1;
661         test_policy3.password = password;
662         test_policy3.extractable = 0;
663
664         size_t current_aliases_num = count_aliases(ALIAS_DATA);
665
666         RUNNER_ASSERT_MSG(
667                         CKMC_ERROR_NONE == (temp = ckmc_save_data("data1", testData1, test_policy1)), // should change it as null value
668                         CKMCReadableError(temp));
669
670         RUNNER_ASSERT_MSG(
671                         CKMC_ERROR_NONE == (temp = ckmc_save_data("data2", testData2, test_policy1)), // should change it as null value
672                         CKMCReadableError(temp));
673
674         RUNNER_ASSERT_MSG(
675                         CKMC_ERROR_NONE == (temp = ckmc_save_data("data3", testData3, test_policy2)),
676                         CKMCReadableError(temp));
677
678         RUNNER_ASSERT_MSG(
679                         CKMC_ERROR_INVALID_PARAMETER == (temp = ckmc_save_data("data4", testData3, test_policy3)),
680                         CKMCReadableError(temp));
681
682         size_t actual_cnt = count_aliases(ALIAS_DATA);
683         RUNNER_ASSERT_MSG(
684                         (current_aliases_num+3) == actual_cnt,
685                         "Error: expecting " << (current_aliases_num+3) << " aliases, while found " << actual_cnt);
686
687         ckmc_raw_buffer_s *testData4;
688         RUNNER_ASSERT_MSG(
689                         CKMC_ERROR_NONE == (temp = ckmc_get_data("data2", password, &testData4)),
690                         CKMCReadableError(temp));
691
692         int compareResult;
693         compareResult = (strcmp((const char *)testData2.data, (const char *)testData4->data));
694         RUNNER_ASSERT_MSG( compareResult == 0,
695                         "Data corrupted");
696 }
697
698 RUNNER_TEST(T3043_app_user_save_bin_data_C_API)
699 {
700         int temp;
701         ckmc_raw_buffer_s testData1;
702         char* password = NULL;
703         std::string binData1 = "My bin data";
704         char* char_binData1 = new char[binData1.length() + 1];
705         std::strcpy(char_binData1, binData1.c_str());
706         testData1.data = (unsigned char *) char_binData1;
707         testData1.size = binData1.length()+1;
708
709         ckmc_policy_s test_policy1, test_policy2;
710
711         test_policy1.password = password;
712         test_policy1.extractable = 1;
713
714         test_policy2.password = password;
715         test_policy2.extractable = 1;
716
717         std::string binData = "My bin data";
718
719         size_t current_aliases_num = count_aliases(ALIAS_DATA);
720
721         RUNNER_ASSERT_MSG(
722                         CKMC_ERROR_NONE == (temp = ckmc_save_data("appdata1", testData1, test_policy1)),
723                         CKMCReadableError(temp));
724         RUNNER_ASSERT_MSG(
725                         CKMC_ERROR_NONE == (temp = ckmc_save_data("appdata2", testData1, test_policy1)),
726                         CKMCReadableError(temp));
727         RUNNER_ASSERT_MSG(
728                         CKMC_ERROR_NONE == (temp = ckmc_save_data("appdata3", testData1, test_policy2)),
729                         CKMCReadableError(temp));
730
731         size_t actual_cnt = count_aliases(ALIAS_DATA);
732         RUNNER_ASSERT_MSG(
733                                 (current_aliases_num+3) == actual_cnt,
734                                 "Error: expecting " << (current_aliases_num+3) << " aliases, while found " << actual_cnt);
735 }
736
737 RUNNER_TEST(T3044_remove_bin_data_C_API)
738 {
739         int temp;
740
741         size_t current_aliases_num = count_aliases(ALIAS_DATA, 2);
742
743         RUNNER_ASSERT_MSG(
744                         CKMC_ERROR_NONE == (temp = ckmc_remove_data("data1")),
745                         CKMCReadableError(temp));
746         RUNNER_ASSERT_MSG(
747                         CKMC_ERROR_NONE == (temp = ckmc_remove_data("data3")),
748                         CKMCReadableError(temp));
749
750         size_t actual_cnt = count_aliases(ALIAS_DATA);
751         RUNNER_ASSERT_MSG(
752                                 (current_aliases_num-2) == actual_cnt,
753                                 "Error: expecting " << (current_aliases_num-2) << " aliases, while found " << actual_cnt);
754
755         char* password = NULL;
756
757         ckmc_raw_buffer_s *testData1, testData2;
758
759         std::string testStr = "My bin data2";
760         char* char_testData2 = new char[testStr.length() + 1];
761         std::strcpy(char_testData2, testStr.c_str());
762         testData2.data = (unsigned char *) char_testData2;
763         testData2.size = testStr.length()+1;
764
765         CKM::RawBuffer buffer;
766         RUNNER_ASSERT_MSG(
767                         CKMC_ERROR_NONE == (temp = ckmc_get_data("data2", password, &testData1)),
768                         CKMCReadableError(temp));
769
770         int compareResult;
771         compareResult = (strcmp((const char *)testData2.data, (const char *)testData1->data));
772         RUNNER_ASSERT_MSG( compareResult == 0,
773                         "Data corrupted");
774
775         RUNNER_ASSERT_MSG(
776                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_data("data3", password, &testData1)),
777                         CKMCReadableError(temp));
778 }
779
780 RUNNER_TEST(T3045_save_big_data_C_API, RemoveDataEnv<USER_APP>)
781 {
782         // We don't know which backend will be used
783         const size_t BIG_SIZE = 100000;
784
785         std::vector<char> big_data(BIG_SIZE);
786         std::ifstream is("/dev/urandom", std::ifstream::binary);
787         if(is)
788                 is.read(big_data.data(), BIG_SIZE);
789
790         RUNNER_ASSERT_MSG(is,
791                         "Only " << is.gcount() << "/" << BIG_SIZE << " bytes read from /dev/urandom");
792
793         save_data(TEST_OBJECT1, big_data.data(), BIG_SIZE, CKMC_ERROR_NONE);
794         auto self_label = getOwnerIdFromSelf();
795         check_read(TEST_OBJECT1, self_label.c_str(), big_data.data(), BIG_SIZE, CKMC_ERROR_NONE);
796 }
797
798 RUNNER_TEST(T3046_save_get_password_protected_data)
799 {
800         const char *alias = "T3046_data_alias";
801         const char *password = "test-password";
802         std::vector<unsigned char> data = { 0x28, 0x34, 0x5a, 0xf3 };
803
804         ckmc_raw_buffer_s raw_buffer;
805         raw_buffer.data = data.data();
806         raw_buffer.size = data.size();
807
808         ckmc_policy_s policy;
809         policy.password = const_cast<char *>(password);
810         policy.extractable = 1;
811
812         int temp;
813
814         RUNNER_ASSERT_MSG(
815                 CKMC_ERROR_NONE == (temp = ckmc_save_data(alias, raw_buffer, policy)),
816                 CKMCReadableError(temp));
817
818         ckmc_raw_buffer_s *raw_buffer_stored = nullptr;
819
820         // test negative case first
821         temp = ckmc_get_data(alias, "invalid_password", &raw_buffer_stored);
822
823         // make scoped buffer for in case of success
824         std::unique_ptr<ckmc_raw_buffer_s, void(*)(ckmc_raw_buffer_s *)> scoped_buffer(
825                 raw_buffer_stored, ckmc_buffer_free);
826
827         RUNNER_ASSERT_MSG(temp == CKMC_ERROR_AUTHENTICATION_FAILED,
828                 "expected(" << CKMCErrorToString(CKMC_ERROR_AUTHENTICATION_FAILED)
829                         << ") but ret(" << CKMCErrorToString(temp) << ")");
830
831         temp = ckmc_get_data(alias, password, &raw_buffer_stored);
832         // reset buffer with raw buffer of positive case
833         scoped_buffer.reset(raw_buffer_stored);
834         RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == temp, CKMCReadableError(temp));
835
836         RUNNER_ASSERT_MSG(
837                 raw_buffer_stored->size == raw_buffer.size,
838                 "stored raw buffer size(" << raw_buffer_stored->size
839                         << ") is differ to original raw buffer size(" << raw_buffer.size << ")");
840         for (size_t i = 0; i < raw_buffer_stored->size; ++i)
841                 RUNNER_ASSERT_MSG(
842                         raw_buffer_stored->data[i] == raw_buffer.data[i],
843                         "stored raw buffer data[" << i << "](" << raw_buffer_stored->data[i]
844                                 << ") is differ to original raw buffer data["
845                                 << i << "](" << raw_buffer.data[i] << ")");
846
847         RUNNER_ASSERT_MSG(
848                 CKMC_ERROR_NONE == (temp = ckmc_remove_alias(alias)),
849                 CKMCReadableError(temp));
850 }
851
852 RUNNER_TEST(T3050_deinit_C_API)
853 {
854         int temp;
855
856         RUNNER_ASSERT_MSG(
857                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
858                         CKMCReadableError(temp));
859         RUNNER_ASSERT_MSG(
860                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
861                         CKMCReadableError(temp));
862 }
863
864 RUNNER_TEST_GROUP_INIT(T305_CKMC_QUICK_CREATE_PAIR_CAPI);
865
866 RUNNER_TEST(T3051_CAPI_init)
867 {
868         int temp;
869
870         RUNNER_ASSERT_MSG(
871                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
872                         CKMCReadableError(temp));
873
874         RUNNER_ASSERT_MSG(
875                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
876                         CKMCReadableError(temp));
877 }
878
879 RUNNER_TEST(T3052_CAPI_create_RSA_key)
880 {
881         int temp;
882         size_t size = 1024;
883         const char *private_key_alias = "RSA-test-1-priv";
884         const char *public_key_alias = "RSA-test-1-pub";
885         ckmc_policy_s policy_private_key;
886         ckmc_policy_s policy_public_key;
887
888         policy_private_key.password = NULL;
889         policy_private_key.extractable = 1;
890
891         policy_public_key.password = NULL;
892         policy_public_key.extractable = 1;
893
894
895         size_t current_aliases_num = count_aliases(ALIAS_KEY);
896
897         RUNNER_ASSERT_MSG(
898                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
899                         CKMCReadableError(temp));
900
901         size_t actual_cnt = count_aliases(ALIAS_KEY);
902         RUNNER_ASSERT_MSG(
903                         (current_aliases_num+2) == actual_cnt,
904                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << actual_cnt);
905
906         ckmc_key_s *privateKey;
907         RUNNER_ASSERT_MSG(
908                         CKMC_ERROR_NONE == (temp = ckmc_get_key(private_key_alias, policy_private_key.password,&privateKey)),
909                         CKMCReadableError(temp));
910         RUNNER_ASSERT_MSG(
911                         privateKey->key_type == CKMC_KEY_RSA_PRIVATE,
912                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_RSA_PRIVATE) << ", actual=" << static_cast<int>(privateKey->key_type));
913         RUNNER_ASSERT_MSG(
914                         privateKey != NULL && privateKey->key_size > 0 && privateKey->raw_key != NULL,
915                         "Private key is broken.");
916
917         ckmc_key_s *publicKey;
918         RUNNER_ASSERT_MSG(
919                         CKMC_ERROR_NONE == (temp = ckmc_get_key(public_key_alias, policy_public_key.password, &publicKey)),
920                         CKMCReadableError(temp));
921         RUNNER_ASSERT_MSG(
922                         publicKey->key_type == CKMC_KEY_RSA_PUBLIC,
923                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_RSA_PUBLIC) << ", actual=" << static_cast<int>(publicKey->key_type));
924         RUNNER_ASSERT_MSG(
925                         publicKey != NULL && publicKey->key_size > 0 && publicKey->raw_key != NULL,
926                         "Public key is broken.");
927
928         // on next attempt to generate keys with the same alias, expect fail (alias exists)
929         RUNNER_ASSERT_MSG(
930                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
931                         CKMCReadableError(temp));
932 }
933
934 RUNNER_TEST(T3053_CAPI_create_DSA_key)
935 {
936         int temp;
937         size_t size = 1024;
938         const char *private_key_alias = "DSA-test-2-priv";
939         const char *public_key_alias = "DSA-test-2-pub";
940         ckmc_policy_s policy_private_key;
941         ckmc_policy_s policy_public_key;
942
943         policy_private_key.password = NULL;
944         policy_private_key.extractable = 1;
945
946         policy_public_key.password = NULL;
947         policy_public_key.extractable = 1;
948
949         size_t current_aliases_num = count_aliases(ALIAS_KEY);
950
951         RUNNER_ASSERT_MSG(
952                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
953                         CKMCReadableError(temp));
954
955         size_t actual_cnt = count_aliases(ALIAS_KEY);
956         RUNNER_ASSERT_MSG(
957                         (current_aliases_num+2) == actual_cnt,
958                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << actual_cnt);
959
960         ckmc_key_s *privateKey = 0;
961         RUNNER_ASSERT_MSG(
962                         CKMC_ERROR_NONE == (temp = ckmc_get_key(private_key_alias, policy_private_key.password,&privateKey)),
963                         CKMCReadableError(temp));
964         RUNNER_ASSERT_MSG(
965                         privateKey != NULL && privateKey->key_size > 0 && privateKey->raw_key != NULL,
966                         "Private key is broken.");
967         RUNNER_ASSERT_MSG(
968                         privateKey->key_type == CKMC_KEY_DSA_PRIVATE,
969                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_DSA_PRIVATE) << ", actual=" << static_cast<int>(privateKey->key_type));
970         ckmc_key_free(privateKey);
971
972         ckmc_key_s *pubKey = 0;
973         RUNNER_ASSERT_MSG(
974                         CKMC_ERROR_NONE == (temp = ckmc_get_key(public_key_alias, policy_public_key.password, &pubKey)),
975                         CKMCReadableError(temp));
976         RUNNER_ASSERT_MSG(
977                         pubKey != NULL && pubKey->key_size > 0 && pubKey->raw_key != NULL,
978                         "Public key is broken.");
979         RUNNER_ASSERT_MSG(
980                         pubKey->key_type == CKMC_KEY_DSA_PUBLIC,
981                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_DSA_PUBLIC) << ", actual=" << static_cast<int>(pubKey->key_type));
982         ckmc_key_free(pubKey);
983
984         // on next attempt to generate keys with the same alias, expect fail (alias exists)
985         RUNNER_ASSERT_MSG(
986                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
987                         CKMCReadableError(temp));
988 }
989
990
991 RUNNER_TEST(T3054_CAPI_create_AES_key)
992 {
993         int temp;
994         size_t size = 128;
995         CKM::Alias key_alias = "AES-gen-test-1";
996         ckmc_policy_s policy_key;
997
998         policy_key.password = NULL;
999         policy_key.extractable = 1;
1000
1001         int current_aliases_num = count_aliases(ALIAS_KEY);
1002
1003         RUNNER_ASSERT_MSG(
1004                         CKMC_ERROR_NONE == (temp = ckmc_create_key_aes(size, key_alias.c_str(), policy_key)),
1005                         CKMCReadableError(temp));
1006
1007         RUNNER_ASSERT_MSG(
1008                         (current_aliases_num+1) == (temp = count_aliases(ALIAS_KEY)),
1009                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << temp);
1010
1011         ckmc_key_s *get_AES_key = 0;
1012         RUNNER_ASSERT_MSG(
1013                         CKMC_ERROR_NONE == (temp = ckmc_get_key(key_alias.c_str(), policy_key.password, &get_AES_key)),
1014                         CKMCReadableError(temp));
1015         validate_AES_key(get_AES_key);
1016         ckmc_key_free(get_AES_key);
1017 }
1018
1019
1020 RUNNER_TEST(T3055_CAPI_deinit)
1021 {
1022         int temp;
1023
1024         RUNNER_ASSERT_MSG(
1025                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
1026                         CKMCReadableError(temp));
1027         RUNNER_ASSERT_MSG(
1028                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
1029                         CKMCReadableError(temp));
1030 }
1031
1032
1033 RUNNER_TEST_GROUP_INIT(T306_CKMC_CAPI_CreateKeyPair);
1034
1035 RUNNER_TEST(T3061_CAPI_init)
1036 {
1037         reset_user_data(USER_APP, USER_PASS);
1038 }
1039
1040 RUNNER_TEST(T3062_CAPI_CreateKeyPairRSA)
1041 {
1042         int temp;
1043
1044         size_t size = 1024;
1045         CKM::Alias private_key_alias = "rsa-test-1";
1046         CKM::Alias public_key_alias = "rsa-test-2";
1047         ckmc_policy_s policy_private_key;
1048         ckmc_policy_s policy_public_key;
1049
1050         policy_private_key.password = const_cast<char *>("privatepassword");
1051         policy_private_key.extractable = 0;
1052
1053         policy_public_key.password = NULL;
1054         policy_public_key.extractable = 1;
1055
1056         RUNNER_ASSERT_MSG(
1057                         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)),
1058                         CKMCReadableError(temp));
1059
1060         // on next attempt to generate keys with the same alias, expect fail (alias exists)
1061         RUNNER_ASSERT_MSG(
1062                         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)),
1063                         CKMCReadableError(temp));
1064 }
1065
1066 RUNNER_TEST(T3063_CAPI_CreateKeyPairDSA)
1067 {
1068         int temp;
1069
1070         size_t size = 1024;
1071         CKM::Alias private_key_alias = "dsa-test-1";
1072         CKM::Alias public_key_alias = "dsa-test-2";
1073         ckmc_policy_s policy_private_key;
1074         ckmc_policy_s policy_public_key;
1075
1076         policy_private_key.password = const_cast<char *>("privatepassword");
1077         policy_private_key.extractable = 0;
1078
1079         policy_public_key.password = NULL;
1080         policy_public_key.extractable = 1;
1081
1082         RUNNER_ASSERT_MSG(
1083                         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)),
1084                         CKMCReadableError(temp));
1085 }
1086
1087 RUNNER_TEST(T3064_CAPI_CreateKeyPairECDSA)
1088 {
1089         int temp;
1090
1091         ckmc_ec_type_e ectype = CKMC_EC_PRIME192V1;
1092         CKM::Alias private_key_alias = "ecdsa-test-1";
1093         CKM::Alias public_key_alias = "ecdsa-test-2";
1094         ckmc_policy_s policy_private_key;
1095         ckmc_policy_s policy_public_key;
1096
1097         policy_private_key.password = const_cast<char *>("privatepassword");
1098         policy_private_key.extractable = 0;
1099
1100         policy_public_key.password = NULL;
1101         policy_public_key.extractable = 1;
1102
1103         RUNNER_ASSERT_MSG(
1104                         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)),
1105                         CKMCReadableError(temp));
1106 }
1107
1108 RUNNER_TEST(T3065_CAPI_deinit)
1109 {
1110         remove_user_data(USER_APP);
1111 }
1112
1113 // TODO
1114 //RUNNER_TEST_GROUP_INIT(T120_NEGATIVE_TESTS);
1115
1116
1117 RUNNER_TEST_GROUP_INIT(T307_CKMC_CAPI_OCSP_TESTS);
1118
1119 RUNNER_TEST(T3071_CAPI_init)
1120 {
1121         remove_user_data(USER_APP);
1122 }
1123
1124 RUNNER_TEST(T3074_CAPI_ckmc_ocsp_check)
1125 {
1126         std::string ee = TestData::getTestCertificateBase64(TestData::OCSP_AVAILABLE_LEAF);
1127         std::string im = TestData::getTestCertificateBase64(TestData::OCSP_AVAILABLE_IM);
1128
1129         ckmc_cert_s c_cert;
1130         c_cert.raw_cert = reinterpret_cast<unsigned char *>(const_cast<char *>(ee.c_str()));
1131         c_cert.cert_size = ee.size();
1132         c_cert.data_format = CKMC_FORM_PEM;
1133
1134         ckmc_cert_s c_cert1;
1135         c_cert1.raw_cert = reinterpret_cast<unsigned char *>(const_cast<char *>(im.c_str()));
1136         c_cert1.cert_size = im.size();
1137         c_cert1.data_format = CKMC_FORM_PEM;
1138
1139         ckmc_cert_list_s untrustedcerts;
1140         untrustedcerts.cert = &c_cert1;
1141         untrustedcerts.next = NULL;
1142
1143         ckmc_cert_list_s *cert_chain_list;
1144
1145         int     tmp = ckmc_get_cert_chain(&c_cert, &untrustedcerts, &cert_chain_list);
1146         RUNNER_ASSERT_MSG(
1147                         CKMC_ERROR_NONE == tmp, CKMCReadableError(tmp));
1148
1149         RUNNER_ASSERT_MSG(cert_chain_list != NULL, "Wrong size of certificate chain.");
1150
1151         ckmc_ocsp_status_e ocsp_status;
1152         RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (tmp = ckmc_ocsp_check(cert_chain_list, &ocsp_status)), CKMCReadableError(tmp));
1153         RUNNER_ASSERT_MSG(ocsp_status == CKMC_OCSP_STATUS_GOOD, "Wrong status: " << static_cast<int>(ocsp_status));
1154 }
1155
1156 RUNNER_TEST(T3075_CAPI_deinit)
1157 {
1158         remove_user_data(USER_APP);
1159 }
1160
1161
1162 RUNNER_TEST_GROUP_INIT(T308_CAPI_CREATE_AND_VERIFY_SIGNATURE);
1163
1164 RUNNER_TEST(T3081_CAPI__init)
1165 {
1166         int temp;
1167
1168         RUNNER_ASSERT_MSG(
1169                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
1170                         CKMCReadableError(temp));
1171
1172         RUNNER_ASSERT_MSG(
1173                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
1174                         CKMCReadableError(temp));
1175 }
1176
1177 RUNNER_TEST(T3082_CAPI__rsa_key_create_verify)
1178 {
1179         int temp;
1180
1181         std::string prv = "-----BEGIN RSA PRIVATE KEY-----\n"
1182                 "Proc-Type: 4,ENCRYPTED\n"
1183                 "DEK-Info: DES-EDE3-CBC,6C6507B11671DABC\n"
1184                 "\n"
1185                 "YiKNviNqc/V/i241CKtAVsNckesE0kcaka3VrY7ApXR+Va93YoEwVQ8gB9cE/eHH\n"
1186                 "S0j3ZS1PAVFM/qo4ZnPdMzaSLvTQw0GAL90wWgF3XQ+feMnWyBObEoQdGXE828TB\n"
1187                 "SLz4UOIQ55Dx6JSWTfEhwAlPs2cEWD14xvuxPzAEzBIYmWmBBsCN94YgFeRTzjH0\n"
1188                 "TImoYVMN60GgOfZWw6rXq9RaV5dY0Y6F1piypCLGD35VaXAutdHIDvwUGECPm7SN\n"
1189                 "w05jRro53E1vb4mYlZEY/bs4q7XEOI5+ZKT76Xn0oEJNX1KRL1h2q8fgUkm5j40M\n"
1190                 "uQj71aLR9KyIoQARwGLeRy09tLVjH3fj66CCMqaPcxcIRIyWi5yYBB0s53ipm6A9\n"
1191                 "CYuyc7MS2C0pOdWKsDvYsHR/36KUiIdPuhF4AbaTqqO0eWeuP7Na7dGK56Fl+ooi\n"
1192                 "cUpJr7cIqMl2vL25B0jW7d4TB3zwCEkVVD1fBPeNoZWo30z4bILcBqjjPkQfHZ2e\n"
1193                 "xNraG3qI4FHjoPT8JEE8p+PgwaMoINlICyIMKiCdvwz9yEnsHPy7FkmatpS+jFoS\n"
1194                 "mg8R9vMwgK/HGEm0dmb/7/a0XsG2jCDm6cOmJdZJFQ8JW7hFs3eOHpNlQYDChG2D\n"
1195                 "A1ExslqBtbpicywTZhzFdYU/hxeCr4UqcY27Zmhr4JlBPMyvadWKeOqCamWepjbT\n"
1196                 "T/MhWJbmWgZbI5s5sbpu7cOYubQcUIEsTaQXGx/KEzGo1HLn9tzSeQfP/nqjAD/L\n"
1197                 "T5t1Mb8o4LuV/fGIT33Q3i2FospJMqp2JINNzG18I6Fjo08PTvJ3row40Rb76+lJ\n"
1198                 "wN1IBthgBgsgsOdB6XNc56sV+uq2TACsNNWw+JnFRCkCQgfF/KUrvN+WireWq88B\n"
1199                 "9UPG+Hbans5A6K+y1a+bzfdYnKws7x8wNRyPxb7Vb2t9ZTl5PBorPLVGsjgf9N5X\n"
1200                 "tCdBlfJsUdXot+EOxrIczV5zx0JIB1Y9hrDG07RYkzPuJKxkW7skqeLo8oWGVpaQ\n"
1201                 "LGWvuebky1R75hcSuL3e4QHfjBHPdQ31fScB884tqkbhBAWr2nT9bYEmyT170bno\n"
1202                 "8QkyOSb99xZBX55sLDHs9p61sTJr2C9Lz/KaWQs+3hTkpwSjSRyjEMH2n491qiQX\n"
1203                 "G+kvLEnvtR8sl9zinorj/RfsxyPntAxudfY3qaYUu2QkLvVdfTVUVbxS/Fg8f7B3\n"
1204                 "hEjCtpKgFjPxQuHE3didNOr5xM7mkmLN/QA7yHVgdpE64T5mFgC3JcVRpcR7zBPH\n"
1205                 "3OeXHgjrhDfN8UIX/cq6gNgD8w7O0rhHa3mEXI1xP14ykPcJ7wlRuLm9P3fwx5A2\n"
1206                 "jQrVKJKw1Nzummmspn4VOpJY3LkH4Sxo4e7Soo1l1cxJpzmERwgMF+vGz1L70+DG\n"
1207                 "M0hVrz1PxlOsBBFgcdS4TB91DIs/RcFDqrJ4gOPNKCgBP+rgTXXLFcxUwJfE3lKg\n"
1208                 "Kmpwdne6FuQYX3eyRVAmPgOHbJuRQCh/V4fYo51UxCcEKeKy6UgOPEJlXksWGbH5\n"
1209                 "VFmlytYW6dFKJvjltSmK6L2r+TlyEQoXwTqe4bkfhB2LniDEq28hKQ==\n"
1210                 "-----END RSA PRIVATE KEY-----\n";
1211
1212         std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1213                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1214                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1215                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1216                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1217                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1218                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1219                 "zQIDAQAB\n"
1220                 "-----END PUBLIC KEY-----\n";
1221
1222         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1223
1224         CKM::Alias pub_alias = "pub1";
1225         CKM::Alias pri_alias = "prv1";
1226         const char *key_passwd = "1234";
1227         char *pri_passwd = NULL;
1228         char *pub_passwd = NULL;
1229         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1230         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1231         ckmc_raw_buffer_s *signature;
1232
1233         ckmc_key_s pubkey;
1234         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1235         pubkey.key_size = pub.size();
1236         pubkey.key_type = CKMC_KEY_NONE;
1237         pubkey.password = NULL;
1238
1239         ckmc_policy_s pubpolicy;
1240         pubpolicy.password = pub_passwd;
1241         pubpolicy.extractable = 0;
1242
1243         ckmc_policy_s pripolicy;
1244         pripolicy.password = pri_passwd;
1245         pripolicy.extractable = 1;
1246
1247         ckmc_key_s prikey;
1248         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1249         prikey.key_size = prv.size();
1250         prikey.key_type = CKMC_KEY_NONE;
1251         prikey.password = const_cast<char *>(key_passwd);
1252
1253
1254         RUNNER_ASSERT_MSG(
1255                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1256                         CKMCReadableError(temp));
1257
1258         RUNNER_ASSERT_MSG(
1259                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias.c_str(), pubkey, pubpolicy)),
1260                         CKMCReadableError(temp));
1261
1262         RUNNER_ASSERT_MSG(
1263                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1264                                         pri_alias.c_str(),
1265                                         pri_passwd,
1266                                         msg_buff,
1267                                         hash_algo,
1268                                         pad_algo,
1269                                         &signature)),
1270                         CKMCReadableError(temp));
1271
1272         RUNNER_ASSERT_MSG(
1273                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1274                                         pub_alias.c_str(),
1275                                         pub_passwd,
1276                                         msg_buff,
1277                                         *signature,
1278                                         hash_algo,
1279                                         pad_algo)),
1280                         CKMCReadableError(temp));
1281 }
1282
1283 RUNNER_TEST(T3083a_CAPI__rsa_key_create_verify_negative)
1284 {
1285         int temp;
1286
1287         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message asdfaslkdfjlksadjf test");
1288         CKM::Alias pub_alias = "pub1";
1289         CKM::Alias pri_alias = "prv1";
1290         char *pri_passwd = NULL;
1291         char *pub_passwd = NULL;
1292         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1293         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1294         ckmc_raw_buffer_s *signature;
1295
1296         RUNNER_ASSERT_MSG(
1297                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1298                                         pri_alias.c_str(),
1299                                         pri_passwd,
1300                                         msg_buff,
1301                                         hash_algo,
1302                                         pad_algo,
1303                                         &signature)),
1304                         CKMCReadableError(temp));
1305
1306         RUNNER_ASSERT_MSG(
1307                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1308                                         pub_alias.c_str(),
1309                                         pub_passwd,
1310                                         msg_buff,
1311                                         *signature,
1312                                         hash_algo,
1313                                         pad_algo)),
1314                         CKMCReadableError(temp));
1315
1316         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1317         memcpy((void*)signature->data, "BROKEN", 6);
1318
1319         RUNNER_ASSERT_MSG(
1320                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1321                                         pub_alias.c_str(),
1322                                         pub_passwd,
1323                                         msg_buff,
1324                                         *signature,
1325                                         hash_algo,
1326                                         pad_algo)),
1327                         CKMCReadableError(temp));
1328 }
1329
1330 RUNNER_TEST(T3083b_CAPI__rsa_key_create_verify_hash_and_padding)
1331 {
1332         int temp;
1333
1334         unsigned char hashed_msg[256] = {}; // shouldn't need padding (2048-bit key)
1335         ckmc_raw_buffer_s msg_buff;
1336         msg_buff.data = hashed_msg;
1337         msg_buff.size = sizeof(hashed_msg);
1338         ckmc_raw_buffer_s short_data = prepare_message_buffer("length not equal to key size");
1339         CKM::Alias pub_alias = "pub1";
1340         CKM::Alias pri_alias = "prv1";
1341         char *pri_passwd = NULL;
1342         char *pub_passwd = NULL;
1343         ckmc_raw_buffer_s *signature;
1344
1345         // sign: no padding + hash
1346         RUNNER_ASSERT_MSG(
1347                         CKMC_ERROR_INVALID_PARAMETER == (temp = ckmc_create_signature(
1348                                         pri_alias.c_str(),
1349                                         pri_passwd,
1350                                         msg_buff,
1351                                         CKMC_HASH_SHA256,
1352                                         CKMC_NONE_PADDING,
1353                                         &signature)),
1354                         CKMCReadableError(temp));
1355
1356         // sign: no padding + no hash + short data
1357         RUNNER_ASSERT_MSG(
1358                         CKMC_ERROR_INVALID_PARAMETER == (temp = ckmc_create_signature(
1359                                         pri_alias.c_str(),
1360                                         pri_passwd,
1361                                         short_data,
1362                                         CKMC_HASH_NONE,
1363                                         CKMC_NONE_PADDING,
1364                                         &signature)),
1365                         CKMCReadableError(temp));
1366
1367         // sign: no padding + no hash + correct length
1368         RUNNER_ASSERT_MSG(
1369                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1370                                         pri_alias.c_str(),
1371                                         pri_passwd,
1372                                         msg_buff,
1373                                         CKMC_HASH_NONE,
1374                                         CKMC_NONE_PADDING,
1375                                         &signature)),
1376                         CKMCReadableError(temp));
1377
1378         // verify: no padding + no hash
1379         RUNNER_ASSERT_MSG(
1380                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1381                                         pub_alias.c_str(),
1382                                         pub_passwd,
1383                                         msg_buff,
1384                                         *signature,
1385                                         CKMC_HASH_NONE,
1386                                         CKMC_NONE_PADDING)),
1387                         CKMCReadableError(temp));
1388
1389         // verify: padding + no hash
1390         RUNNER_ASSERT_MSG(
1391                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1392                                         pub_alias.c_str(),
1393                                         pub_passwd,
1394                                         msg_buff,
1395                                         *signature,
1396                                         CKMC_HASH_NONE,
1397                                         CKMC_PKCS1_PADDING)),
1398                         CKMCReadableError(temp));
1399
1400         // verify: no padding + hash
1401         RUNNER_ASSERT_MSG(
1402                         CKMC_ERROR_INVALID_PARAMETER == (temp = ckmc_verify_signature(
1403                                         pub_alias.c_str(),
1404                                         pub_passwd,
1405                                         msg_buff,
1406                                         *signature,
1407                                         CKMC_HASH_SHA256,
1408                                         CKMC_NONE_PADDING)),
1409                         CKMCReadableError(temp));
1410
1411         ckmc_buffer_free(signature);
1412
1413         // sign: padding + no hash + short data
1414         RUNNER_ASSERT_MSG(
1415                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1416                                         pri_alias.c_str(),
1417                                         pri_passwd,
1418                                         short_data,
1419                                         CKMC_HASH_NONE,
1420                                         CKMC_PKCS1_PADDING,
1421                                         &signature)),
1422                         CKMCReadableError(temp));
1423
1424         // verify: padding + no hash
1425         RUNNER_ASSERT_MSG(
1426                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1427                                         pub_alias.c_str(),
1428                                         pub_passwd,
1429                                         short_data,
1430                                         *signature,
1431                                         CKMC_HASH_NONE,
1432                                         CKMC_PKCS1_PADDING)),
1433                         CKMCReadableError(temp));
1434
1435         // verify: no padding + no hash
1436         RUNNER_ASSERT_MSG(
1437                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1438                                         pub_alias.c_str(),
1439                                         pub_passwd,
1440                                         short_data,
1441                                         *signature,
1442                                         CKMC_HASH_NONE,
1443                                         CKMC_NONE_PADDING)),
1444                         CKMCReadableError(temp));
1445
1446         // verify: no padding + hash
1447         RUNNER_ASSERT_MSG(
1448                         CKMC_ERROR_INVALID_PARAMETER == (temp = ckmc_verify_signature(
1449                                         pub_alias.c_str(),
1450                                         pub_passwd,
1451                                         short_data,
1452                                         *signature,
1453                                         CKMC_HASH_SHA256,
1454                                         CKMC_NONE_PADDING)),
1455                         CKMCReadableError(temp));
1456
1457         ckmc_buffer_free(signature);
1458 }
1459
1460
1461 RUNNER_TEST(T3084_CAPI__ec_key_create_verify)
1462 {
1463         int temp;
1464
1465         std::string prv = "-----BEGIN EC PRIVATE KEY-----\n"
1466                 "MHQCAQEEIJNud6U4h8EM1rASn4W5vQOJELTaVPQTUiESaBULvQUVoAcGBSuBBAAK\n"
1467                 "oUQDQgAEL1R+hgjiFrdjbUKRNOxUG8ze9nveD9zT05YHeT7vK0w08AUL1HCH5nFV\n"
1468                 "ljePBYSxe6CybFiseayaxRxjA+iF1g==\n"
1469                 "-----END EC PRIVATE KEY-----\n";
1470
1471         std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1472                 "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEL1R+hgjiFrdjbUKRNOxUG8ze9nveD9zT\n"
1473                 "05YHeT7vK0w08AUL1HCH5nFVljePBYSxe6CybFiseayaxRxjA+iF1g==\n"
1474                 "-----END PUBLIC KEY-----\n";
1475
1476         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1477         CKM::Alias pri_alias = "ecprv2";
1478         CKM::Alias pub_alias = "ecpub2";
1479         char *key_passwd = NULL;
1480         char *pri_passwd = NULL;
1481         char *pub_passwd = NULL;
1482         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1483         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1484         ckmc_raw_buffer_s *signature;
1485
1486         ckmc_key_s pubkey;
1487         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1488         pubkey.key_size = pub.size();
1489         pubkey.key_type = CKMC_KEY_NONE;
1490         pubkey.password = NULL;
1491
1492         ckmc_policy_s pubpolicy;
1493         pubpolicy.password = pub_passwd;
1494         pubpolicy.extractable = 1;
1495
1496         ckmc_key_s prikey;
1497         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1498         prikey.key_size = prv.size();
1499         prikey.key_type = CKMC_KEY_NONE;
1500         prikey.password = key_passwd;
1501
1502         ckmc_policy_s pripolicy;
1503         pripolicy.password = pri_passwd;
1504         pripolicy.extractable = 0;
1505
1506         RUNNER_ASSERT_MSG(
1507                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias.c_str(), pubkey, pubpolicy)),
1508                         CKMCReadableError(temp));
1509         RUNNER_ASSERT_MSG(
1510                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1511                         CKMCReadableError(temp));
1512
1513         RUNNER_ASSERT_MSG(
1514                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1515                                         pri_alias.c_str(),
1516                                         pri_passwd,
1517                                         msg_buff,
1518                                         hash_algo,
1519                                         pad_algo,
1520                                         &signature)),
1521                         CKMCReadableError(temp));
1522
1523         RUNNER_ASSERT_MSG(
1524                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1525                                         pub_alias.c_str(),
1526                                         pub_passwd,
1527                                         msg_buff,
1528                                         *signature,
1529                                         hash_algo,
1530                                         pad_algo)),
1531                         CKMCReadableError(temp));
1532
1533         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1534         memcpy((void*)signature->data, "BROKEN", 6);
1535
1536         RUNNER_ASSERT_MSG(
1537                         CKMC_ERROR_VERIFICATION_FAILED == (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
1547 RUNNER_TEST(T3085_CAPI__rsa_cert_create_verify_signature)
1548 {
1549         int temp;
1550
1551         std::string prv =
1552                 "-----BEGIN RSA PRIVATE KEY-----\n"
1553                 "MIICXQIBAAKBgQDCKb9BkTdOjCTXKPi/H5FSGuyrgzORBtR3nCTg7SRnL47zNGEj\n"
1554                 "l2wkgsY9ZO3UJHm0gy5KMjWeCuUVkSD3G46J9obg1bYJivCQBJKxfieA8sWOtNq1\n"
1555                 "M8emHGK8o3sjaRklrngmk2xSCs5vFJVlCluzAYUmrPDm64C3+n4yW4pBCQIDAQAB\n"
1556                 "AoGAd1IWgiHO3kuLvFome7XXpaB8P27SutZ6rcLcewnhLDRy4g0XgTrmL43abBJh\n"
1557                 "gdSkooVXZity/dvuKpHUs2dQ8W8zYiFFsHfu9qqLmLP6SuBPyUCvlUDH5BGfjjxI\n"
1558                 "5qGWIowj/qGHKpbQ7uB+Oe2BHwbHao0zFZIkfKqY0mX9U00CQQDwF/4zQcGS1RX/\n"
1559                 "229gowTsvSGVmm8cy1jGst6xkueEuOEZ/AVPO1fjavz+nTziUk4E5lZHAj18L6Hl\n"
1560                 "iO29LRujAkEAzwbEWVhfTJewCZIFf3sY3ifXhGZhVKDHVzPBNyoft8Z+09DMHTJb\n"
1561                 "EYg85MIbR73aUyIWsEci/CPk6LPRNv47YwJAHtQF2NEFqPPhakPjzjXAaSFz0YDN\n"
1562                 "6ZWWpZTMEWL6hUkz5iE9EUpeY54WNB8+dRT6XZix1VZNTMfU8uMdG6BSHwJBAKYM\n"
1563                 "gm47AGz5eVujwD8op6CACk+KomRzdI+P1lh9s+T+E3mnDiAY5IxiXp0Ix0K6lyN4\n"
1564                 "wwPuerQLwi2XFKZsMYsCQQDOiSQFP9PfXh9kFzN6e89LxOdnqC/r9i5GDB3ea8eL\n"
1565                 "SCRprpzqOXZvOP1HBAEjsJ6k4f8Dqj1fm+y8ZcgAZUPr\n"
1566                 "-----END RSA PRIVATE KEY-----\n";
1567
1568         std::string pub =
1569                 "-----BEGIN CERTIFICATE-----\n"
1570                 "MIICijCCAfOgAwIBAgIJAMvaNHQ1ozT8MA0GCSqGSIb3DQEBBQUAMF4xCzAJBgNV\n"
1571                 "BAYTAlBMMQ0wCwYDVQQIDARMb2R6MQ0wCwYDVQQHDARMb2R6MRAwDgYDVQQKDAdT\n"
1572                 "YW1zdW5nMREwDwYDVQQLDAhTZWN1cml0eTEMMAoGA1UEAwwDQ0tNMB4XDTE0MDcw\n"
1573                 "MjEyNDE0N1oXDTE3MDcwMTEyNDE0N1owXjELMAkGA1UEBhMCUEwxDTALBgNVBAgM\n"
1574                 "BExvZHoxDTALBgNVBAcMBExvZHoxEDAOBgNVBAoMB1NhbXN1bmcxETAPBgNVBAsM\n"
1575                 "CFNlY3VyaXR5MQwwCgYDVQQDDANDS00wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ\n"
1576                 "AoGBAMIpv0GRN06MJNco+L8fkVIa7KuDM5EG1HecJODtJGcvjvM0YSOXbCSCxj1k\n"
1577                 "7dQkebSDLkoyNZ4K5RWRIPcbjon2huDVtgmK8JAEkrF+J4DyxY602rUzx6YcYryj\n"
1578                 "eyNpGSWueCaTbFIKzm8UlWUKW7MBhSas8ObrgLf6fjJbikEJAgMBAAGjUDBOMB0G\n"
1579                 "A1UdDgQWBBQuW9DuITahZJ6saVZZI0aBlis5vzAfBgNVHSMEGDAWgBQuW9DuITah\n"
1580                 "ZJ6saVZZI0aBlis5vzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAB2X\n"
1581                 "GNtJopBJR3dCpzDONknr/c6qcsPVa3nH4c7qzy6F+4bgqa5IObnoF8zUrvD2sMAO\n"
1582                 "km3C/N+Qzt8Rb7ORM6U4tlPp1kZ5t6PKjghhNaiYwVm9A/Zm+wyAmRIkQiYDr4MX\n"
1583                 "e+bRAkPmJeEWpaav1lvvBnFzGSGJrnSSeWUegGyn\n"
1584                 "-----END CERTIFICATE-----\n";
1585
1586         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1587
1588         CKM::Alias pri_alias = "prv3";
1589         CKM::Alias pub_alias = "pub3";
1590         char *key_passwd = NULL;
1591         char *pri_passwd = NULL;
1592         char *pub_passwd = NULL;
1593         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1594         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1595         ckmc_raw_buffer_s *signature;
1596
1597         ckmc_cert_s cert;
1598         cert.raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1599         cert.cert_size = pub.size();
1600         cert.data_format = CKMC_FORM_PEM;
1601
1602         ckmc_policy_s certpolicy;
1603         certpolicy.password = pub_passwd;
1604         certpolicy.extractable = 1;
1605
1606         ckmc_key_s prikey;
1607         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1608         prikey.key_size = prv.size();
1609         prikey.key_type = CKMC_KEY_NONE;
1610         prikey.password = key_passwd;
1611
1612         ckmc_policy_s pripolicy;
1613         pripolicy.password = pri_passwd;
1614         pripolicy.extractable = 0;
1615
1616         RUNNER_ASSERT_MSG(
1617                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(pub_alias.c_str(), cert, certpolicy)),
1618                         CKMCReadableError(temp));
1619         RUNNER_ASSERT_MSG(
1620                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1621                         CKMCReadableError(temp));
1622
1623
1624         RUNNER_ASSERT_MSG(
1625                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1626                                         pri_alias.c_str(),
1627                                         pri_passwd,
1628                                         msg_buff,
1629                                         hash_algo,
1630                                         pad_algo,
1631                                         &signature)),
1632                         CKMCReadableError(temp));
1633
1634         RUNNER_ASSERT_MSG(
1635                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1636                                         pub_alias.c_str(),
1637                                         pub_passwd,
1638                                         msg_buff,
1639                                         *signature,
1640                                         hash_algo,
1641                                         pad_algo)),
1642                         CKMCReadableError(temp));
1643
1644         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1645         memcpy((void*)signature->data, "BROKEN", 6);
1646
1647         RUNNER_ASSERT_MSG(
1648                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1649                                         pub_alias.c_str(),
1650                                         pub_passwd,
1651                                         msg_buff,
1652                                         *signature,
1653                                         hash_algo,
1654                                         pad_algo)),
1655                         CKMCReadableError(temp));
1656 }
1657
1658 RUNNER_TEST(T3086_CAPI__dsa_ext_key_create_verify_with_negative)
1659 {
1660         int temp;
1661
1662         const std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1663                 "MIIBtzCCASwGByqGSM44BAEwggEfAoGBALeveaD/EheW+ws1YuW77f344+brkEzm\n"
1664                 "BVfFYHr7t+jwu6nQe341SoESJG+PCgrrhy76KNDCfveiwEoWufVHnI4bYBU/ClzP\n"
1665                 "A3amf6c5yud45ZR/b6OiAuew6ohY0mQGnzqeio8BaCsZaJ6EziCSlkdIDJisSfPg\n"
1666                 "nlWHqf4AwHVdAhUA7I1JQ7sBFJ+N19w3Omu+aO8EG08CgYEAldagy/Ccxhh43cZu\n"
1667                 "AZQxgJLCcp1jg6NdPMdkZ2TcSijvaVxBu+gjEGOqN5Os2V6UF7S/k/rjHYmcX9ux\n"
1668                 "gpjkC31yTNrKyERIAFIYZtG2K7LVBUZq5Fgm7I83QBVchJ2PA7mBaugJFEhNjbhK\n"
1669                 "NRip5UH38le1YDZ/IiA+svFOpeoDgYQAAoGAPT91aEgwFdulzmHlvr3k+GBCE9z+\n"
1670                 "hq0c3FGUCtGbVOqg2KPqMBgwSb4MC0msQys4DTVZhLJI+C5eIPEHgfBMqY1ZNJdO\n"
1671                 "OSCQciDXnRfSqKbT6tjDTgR5jmh5bG1Q8QFeBTHCDsQHoQYWgx0nyu12lASN80rC\n"
1672                 "YMYCBhubtrVaLmc=\n"
1673                 "-----END PUBLIC KEY-----";
1674
1675         const std::string priv = "-----BEGIN DSA PRIVATE KEY-----\n"
1676                 "MIIBvAIBAAKBgQC3r3mg/xIXlvsLNWLlu+39+OPm65BM5gVXxWB6+7fo8Lup0Ht+\n"
1677                 "NUqBEiRvjwoK64cu+ijQwn73osBKFrn1R5yOG2AVPwpczwN2pn+nOcrneOWUf2+j\n"
1678                 "ogLnsOqIWNJkBp86noqPAWgrGWiehM4gkpZHSAyYrEnz4J5Vh6n+AMB1XQIVAOyN\n"
1679                 "SUO7ARSfjdfcNzprvmjvBBtPAoGBAJXWoMvwnMYYeN3GbgGUMYCSwnKdY4OjXTzH\n"
1680                 "ZGdk3Eoo72lcQbvoIxBjqjeTrNlelBe0v5P64x2JnF/bsYKY5At9ckzayshESABS\n"
1681                 "GGbRtiuy1QVGauRYJuyPN0AVXISdjwO5gWroCRRITY24SjUYqeVB9/JXtWA2fyIg\n"
1682                 "PrLxTqXqAoGAPT91aEgwFdulzmHlvr3k+GBCE9z+hq0c3FGUCtGbVOqg2KPqMBgw\n"
1683                 "Sb4MC0msQys4DTVZhLJI+C5eIPEHgfBMqY1ZNJdOOSCQciDXnRfSqKbT6tjDTgR5\n"
1684                 "jmh5bG1Q8QFeBTHCDsQHoQYWgx0nyu12lASN80rCYMYCBhubtrVaLmcCFQC0IB4m\n"
1685                 "u1roOuaPY+Hl19BlTE2qdw==\n"
1686                 "-----END DSA PRIVATE KEY-----";
1687
1688         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1689
1690         CKM::Alias pub_alias = "dsa-pub1";
1691         CKM::Alias pri_alias = "dsa-prv1";
1692         char *pri_passwd = NULL;
1693         char *pub_passwd = NULL;
1694         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1695         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1696         ckmc_raw_buffer_s *signature = NULL;
1697
1698         ckmc_key_s pubkey;
1699         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1700         pubkey.key_size = pub.size();
1701         pubkey.key_type = CKMC_KEY_NONE;
1702         pubkey.password = NULL;
1703
1704         ckmc_policy_s pubpolicy;
1705         pubpolicy.password = pub_passwd;
1706         pubpolicy.extractable = 0;
1707
1708         ckmc_policy_s pripolicy;
1709         pripolicy.password = pri_passwd;
1710         pripolicy.extractable = 1;
1711
1712         ckmc_key_s prikey;
1713         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(priv.c_str()));
1714         prikey.key_size = priv.size();
1715         prikey.key_type = CKMC_KEY_NONE;
1716         prikey.password = NULL;
1717
1718
1719         RUNNER_ASSERT_MSG(
1720                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1721                         CKMCReadableError(temp));
1722
1723         RUNNER_ASSERT_MSG(
1724                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias.c_str(), pubkey, pubpolicy)),
1725                         CKMCReadableError(temp));
1726
1727         RUNNER_ASSERT_MSG(
1728                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1729                                         pri_alias.c_str(),
1730                                         pri_passwd,
1731                                         msg_buff,
1732                                         hash_algo,
1733                                         pad_algo,
1734                                         &signature)),
1735                         CKMCReadableError(temp));
1736
1737         // positive test
1738         RUNNER_ASSERT_MSG(
1739                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1740                                         pub_alias.c_str(),
1741                                         pub_passwd,
1742                                         msg_buff,
1743                                         *signature,
1744                                         hash_algo,
1745                                         pad_algo)),
1746                         CKMCReadableError(temp));
1747
1748         // negative test
1749         ckmc_raw_buffer_s invalid_msg_buff = prepare_message_buffer("invalid message test");
1750         RUNNER_ASSERT_MSG(
1751                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1752                                         pub_alias.c_str(),
1753                                         pub_passwd,
1754                                         invalid_msg_buff,
1755                                         *signature,
1756                                         hash_algo,
1757                                         pad_algo)),
1758                         CKMCReadableError(temp));
1759
1760         ckmc_buffer_free(signature);
1761 }
1762
1763 RUNNER_TEST(T3087_CAPI__dsa_int_key_create_verify_with_negative)
1764 {
1765         int temp;
1766
1767         size_t size = 1024;
1768         ckmc_policy_s policy_private_key;
1769         ckmc_policy_s policy_public_key;
1770
1771         policy_private_key.password = NULL;
1772         policy_private_key.extractable = 1;
1773
1774         policy_public_key.password = NULL;
1775         policy_public_key.extractable = 1;
1776
1777         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1778
1779         CKM::Alias pub_alias = "dsa-pub2";
1780         CKM::Alias pri_alias = "dsa-prv2";
1781
1782         RUNNER_ASSERT_MSG(
1783                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, pri_alias.c_str(), pub_alias.c_str(), policy_private_key, policy_public_key)),
1784                         "Error=" << temp);
1785
1786         char *pri_passwd = NULL;
1787         char *pub_passwd = NULL;
1788         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1789         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1790         ckmc_raw_buffer_s *signature;
1791
1792         ckmc_key_s *pubkey = NULL;
1793         ckmc_key_s *prikey = NULL;
1794         RUNNER_ASSERT_MSG(
1795                         CKMC_ERROR_NONE == (temp = ckmc_get_key(pri_alias.c_str(), 0, &prikey)),
1796                         CKMCReadableError(temp));
1797
1798         RUNNER_ASSERT_MSG(
1799                         CKMC_ERROR_NONE == (temp = ckmc_get_key(pub_alias.c_str(), 0, &pubkey)),
1800                         CKMCReadableError(temp));
1801
1802         RUNNER_ASSERT_MSG(
1803                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1804                                         pri_alias.c_str(),
1805                                         pri_passwd,
1806                                         msg_buff,
1807                                         hash_algo,
1808                                         pad_algo,
1809                                         &signature)),
1810                         CKMCReadableError(temp));
1811
1812         // positive test
1813         RUNNER_ASSERT_MSG(
1814                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1815                                         pub_alias.c_str(),
1816                                         pub_passwd,
1817                                         msg_buff,
1818                                         *signature,
1819                                         hash_algo,
1820                                         pad_algo)),
1821                         CKMCReadableError(temp));
1822
1823         // negative test
1824         ckmc_raw_buffer_s invalid_msg_buff = prepare_message_buffer("invalid message test");
1825         RUNNER_ASSERT_MSG(
1826                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1827                                         pub_alias.c_str(),
1828                                         pub_passwd,
1829                                         invalid_msg_buff,
1830                                         *signature,
1831                                         hash_algo,
1832                                         pad_algo)),
1833                         CKMCReadableError(temp));
1834
1835         ckmc_key_free(prikey);
1836         ckmc_key_free(pubkey);
1837         ckmc_buffer_free(signature);
1838 }
1839
1840 RUNNER_TEST(T3088_CAPI__ecdsa_cert_create_verify_signature)
1841 {
1842         int temp;
1843
1844         std::string prv =
1845                 "-----BEGIN EC PRIVATE KEY-----\n"
1846                 "MIH8AgEBBBRPb/2utS5aCtyuwmzIHpU6LH3mc6CBsjCBrwIBATAgBgcqhkjOPQEB\n"
1847                 "AhUA/////////////////////3////8wQwQU/////////////////////3////wE\n"
1848                 "FByXvvxUvXqLZaz4n4HU1K3FZfpFAxUAEFPN5CwU1pbmdodWFRdTO/P4M0UEKQRK\n"
1849                 "lrVojvVzKEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAAAAAA\n"
1850                 "AAAAAfTI+Seu08p1IlcCAQGhLAMqAATehLqu61gKC3Tgr4wQMVoguAhhG3Uwwz8u\n"
1851                 "ELyhe7yPCAuOoLZlTLgf\n"
1852                 "-----END EC PRIVATE KEY-----\n";
1853
1854         std::string pub =
1855                 "-----BEGIN CERTIFICATE-----\n"
1856                 "MIICfDCCAjqgAwIBAgIJANIytpeTKlXBMAkGByqGSM49BAEwXjELMAkGA1UEBhMC\n"
1857                 "UEwxDTALBgNVBAgMBExvZHoxDTALBgNVBAcMBExvZHoxEDAOBgNVBAoMB1NhbXN1\n"
1858                 "bmcxETAPBgNVBAsMCFNlY3VyaXR5MQwwCgYDVQQDDANDS00wHhcNMTQwNzAyMTI0\n"
1859                 "MTQ3WhcNMTcwNzAxMTI0MTQ3WjBeMQswCQYDVQQGEwJQTDENMAsGA1UECAwETG9k\n"
1860                 "ejENMAsGA1UEBwwETG9kejEQMA4GA1UECgwHU2Ftc3VuZzERMA8GA1UECwwIU2Vj\n"
1861                 "dXJpdHkxDDAKBgNVBAMMA0NLTTCB6jCBuwYHKoZIzj0CATCBrwIBATAgBgcqhkjO\n"
1862                 "PQEBAhUA/////////////////////3////8wQwQU/////////////////////3//\n"
1863                 "//wEFByXvvxUvXqLZaz4n4HU1K3FZfpFAxUAEFPN5CwU1pbmdodWFRdTO/P4M0UE\n"
1864                 "KQRKlrVojvVzKEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAA\n"
1865                 "AAAAAAAAAfTI+Seu08p1IlcCAQEDKgAE3oS6rutYCgt04K+MEDFaILgIYRt1MMM/\n"
1866                 "LhC8oXu8jwgLjqC2ZUy4H6NQME4wHQYDVR0OBBYEFELElWx3kbLo55Cfn1vywsEZ\n"
1867                 "ccsmMB8GA1UdIwQYMBaAFELElWx3kbLo55Cfn1vywsEZccsmMAwGA1UdEwQFMAMB\n"
1868                 "Af8wCQYHKoZIzj0EAQMxADAuAhUAumC4mGoyK97SxTvVBQ+ELfCbxEECFQCbMJ72\n"
1869                 "Q1oBry6NEc+lLFmWMDesAA==\n"
1870                 "-----END CERTIFICATE-----\n";
1871
1872         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1873
1874         CKM::Alias pri_alias = "prv4";
1875         CKM::Alias pub_alias = "pub4";
1876         char *key_passwd = NULL;
1877         char *pri_passwd = NULL;
1878         char *pub_passwd = NULL;
1879         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1880         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1881         ckmc_raw_buffer_s *signature;
1882
1883         ckmc_cert_s cert;
1884         cert.raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1885         cert.cert_size = pub.size();
1886         cert.data_format = CKMC_FORM_PEM;
1887
1888         ckmc_policy_s certpolicy;
1889         certpolicy.password = pub_passwd;
1890         certpolicy.extractable = 1;
1891
1892         ckmc_key_s prikey;
1893         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1894         prikey.key_size = prv.size();
1895         prikey.key_type = CKMC_KEY_NONE;
1896         prikey.password = key_passwd;
1897
1898         ckmc_policy_s pripolicy;
1899         pripolicy.password = pri_passwd;
1900         pripolicy.extractable = 0;
1901
1902
1903         RUNNER_ASSERT_MSG(
1904                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(pub_alias.c_str(), cert, certpolicy)),
1905                         CKMCReadableError(temp));
1906         RUNNER_ASSERT_MSG(
1907                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1908                         CKMCReadableError(temp));
1909
1910         RUNNER_ASSERT_MSG(
1911                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1912                                         pri_alias.c_str(),
1913                                         pri_passwd,
1914                                         msg_buff,
1915                                         hash_algo,
1916                                         pad_algo,
1917                                         &signature)),
1918                         CKMCReadableError(temp));
1919
1920         RUNNER_ASSERT_MSG(
1921                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1922                                         pub_alias.c_str(),
1923                                         pub_passwd,
1924                                         msg_buff,
1925                                         *signature,
1926                                         hash_algo,
1927                                         pad_algo)),
1928                         CKMCReadableError(temp));
1929
1930         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1931         memcpy((void*)signature->data, "BROKEN", 6);
1932
1933         RUNNER_ASSERT_MSG(
1934                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1935                                         pub_alias.c_str(),
1936                                         pub_passwd,
1937                                         msg_buff,
1938                                         *signature,
1939                                         hash_algo,
1940                                         pad_algo)),
1941                         CKMCReadableError(temp));
1942 }
1943
1944 RUNNER_TEST(T3089_CAPI_deinit)
1945 {
1946         remove_user_data(USER_APP);
1947 }
1948
1949
1950 //#######################################################################################
1951
1952 void _assertKey(ckmc_key_s *key, unsigned char *raw_key, unsigned int key_size, ckmc_key_type_e key_type, char *password)
1953 {
1954         RUNNER_ASSERT_MSG(key->key_size == key_size, "Key Size Error" );
1955         RUNNER_ASSERT_MSG(key->key_type == key_type, "Key Type Error" );
1956
1957         if(key->password != NULL && password != NULL) {
1958                 RUNNER_ASSERT_MSG(strcmp(key->password, password) == 0, "Password Error" );
1959         }else if(key->password == NULL && password == NULL) {
1960                 RUNNER_ASSERT_MSG(true, "Password Error" );
1961         }else {
1962                 RUNNER_ASSERT_MSG(false, "Password Error" );
1963         }
1964
1965         if(key->raw_key != NULL && raw_key != NULL) {
1966                 for(unsigned int i=0; i<key_size; i++) {
1967                         RUNNER_ASSERT_MSG((key->raw_key)[i] == raw_key[i],  "Raw Key Error" );
1968                 }
1969         }else if(key->raw_key == NULL && raw_key == NULL) {
1970                 RUNNER_ASSERT_MSG(true,  "Raw Key Error" );
1971         }else {
1972                 RUNNER_ASSERT_MSG(false,  "Raw Key Error" );
1973         }
1974 }
1975
1976 RUNNER_TEST_GROUP_INIT(T309_CKMC_CAPI_TYPES);
1977
1978 RUNNER_TEST(T3091_CAPI_TYPE_init)
1979 {
1980         reset_user_data(USER_APP, USER_PASS);
1981 }
1982
1983 RUNNER_TEST(T3092_CAPI_TYPE_KEY)
1984 {
1985         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
1986                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1987                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1988                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1989                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1990                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1991                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1992                 "zQIDAQAB\n"
1993                 "-----END PUBLIC KEY-----";
1994
1995         unsigned char *raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(keyPem.c_str()));
1996         unsigned int key_size = keyPem.size();
1997         ckmc_key_type_e key_type = CKMC_KEY_NONE;
1998         char *password = const_cast< char *>("");
1999
2000         ckmc_key_s *key;
2001         ckmc_key_new(raw_key, key_size, key_type, password, &key);
2002
2003         _assertKey(key, raw_key, key_size, key_type, password);
2004         ckmc_key_free(key);
2005
2006         char *passwordNull = NULL;
2007         ckmc_key_s *key2;
2008         ckmc_key_new(raw_key, key_size, key_type, passwordNull, &key2);
2009         ckmc_key_free(key2);
2010 }
2011
2012 RUNNER_TEST(T3093_CAPI_TYPE_BUFFER)
2013 {
2014         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
2015                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
2016                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
2017                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
2018                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
2019                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
2020                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
2021                 "zQIDAQAB\n"
2022                 "-----END PUBLIC KEY-----";
2023
2024         unsigned char *data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(keyPem.c_str()));
2025         unsigned int size = keyPem.size();
2026
2027         ckmc_raw_buffer_s *buff;
2028         ckmc_buffer_new(data, size, &buff);
2029
2030         RUNNER_ASSERT_MSG(buff->size == size, "Size Error" );
2031
2032         if(buff->data != NULL && data != NULL) {
2033                 for(unsigned int i=0; i<size; i++) {
2034                         RUNNER_ASSERT_MSG((buff->data)[i] == data[i],  "Raw data Error" );
2035                 }
2036         }else if(buff->data == NULL && data == NULL) {
2037                 RUNNER_ASSERT_MSG(true,  "Raw data Error" );
2038         }else {
2039                 RUNNER_ASSERT_MSG(false,  "Raw data Error" );
2040         }
2041
2042         ckmc_buffer_free(buff);
2043 }
2044
2045 RUNNER_TEST(T3094_CAPI_TYPE_CERT)
2046 {
2047         std::string certPem = TestData::getTestCertificateBase64(TestData::THIRD_PARTY_LEAF);
2048
2049         unsigned char *raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(certPem.c_str()));
2050         unsigned int size = certPem.size();
2051         ckmc_data_format_e form = CKMC_FORM_PEM;
2052
2053         ckmc_cert_s *ckmCert;
2054         ckmc_cert_new(raw_cert, size, form, &ckmCert);
2055
2056         RUNNER_ASSERT_MSG(ckmCert->cert_size == size, "Size Error" );
2057
2058         if(ckmCert->raw_cert != NULL && raw_cert != NULL) {
2059                 for(unsigned int i=0; i<size; i++) {
2060                         RUNNER_ASSERT_MSG((ckmCert->raw_cert)[i] == raw_cert[i],  "Raw data Error" );
2061                 }
2062         }else if(ckmCert->raw_cert == NULL && raw_cert == NULL) {
2063                 RUNNER_ASSERT_MSG(true,  "raw_cert Error" );
2064         }else {
2065                 RUNNER_ASSERT_MSG(false,  "raw_cert Error" );
2066         }
2067
2068         RUNNER_ASSERT_MSG(ckmCert->data_format == form, "ckmc_cert_form Error" );
2069
2070         ckmc_cert_free(ckmCert);
2071 }
2072
2073
2074 RUNNER_TEST(T3095_CAPI_TYPE_load_cert_file)
2075 {
2076         int ret;
2077
2078         std::string certStr = TestData::getTestCertificateBase64(TestData::THIRD_PARTY_LEAF);
2079
2080         const char *file_name = "/tmp/ckmc_test_cert.pem";
2081         remove(file_name);
2082
2083         FILE* cert_file;
2084         cert_file = fopen(file_name, "w");
2085         fprintf(cert_file, "%s",certStr.c_str());
2086         fclose(cert_file);
2087
2088         ckmc_cert_s *pcert;
2089
2090         RUNNER_ASSERT_MSG(
2091                         CKMC_ERROR_NONE == (ret = ckmc_load_cert_from_file(file_name, &pcert)),
2092                         CKMCReadableError(ret));
2093
2094         RUNNER_ASSERT_MSG(
2095                         pcert != NULL && pcert->cert_size > 0,"Fail to load cert from file.");
2096
2097         CKM::Alias lcert_alias = "lcert_alias";
2098         ckmc_policy_s policy;
2099         policy.password = NULL;
2100         policy.extractable = 1;
2101
2102         RUNNER_ASSERT_MSG(
2103                         CKMC_ERROR_NONE == (ret = ckmc_save_cert(lcert_alias.c_str(), *pcert, policy)),
2104                         CKMCReadableError(ret));
2105
2106         remove(file_name);
2107 }
2108
2109 RUNNER_TEST(T3096_CAPI_TYPE_load_p12_file) {
2110         const char *p12file = CKM_TEST_DIR "/capi-t3096.p12";
2111         const char *password = "password";
2112
2113         int temp;
2114
2115         ckmc_key_s *private_key = NULL;
2116         ckmc_cert_s *cert = NULL;
2117         ckmc_cert_list_s *ca_cert_list = NULL;
2118
2119         RUNNER_ASSERT_MSG(
2120                         CKMC_ERROR_NONE == (temp = ckmc_load_from_pkcs12_file(p12file, password,
2121                                         &private_key, &cert, &ca_cert_list)),
2122                         "Error=" << temp);
2123         RUNNER_ASSERT_MSG(private_key != NULL, "Null private_key");
2124         RUNNER_ASSERT_MSG(cert != NULL, "Null cert");
2125         RUNNER_ASSERT_MSG(ca_cert_list != NULL, "Null ca_cert_list");
2126
2127         ckmc_policy_s policy;
2128         policy.password = NULL;
2129         policy.extractable = 1;
2130
2131
2132         CKM::Alias pkey_alias = "pkey_alias";
2133         RUNNER_ASSERT_MSG(
2134                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pkey_alias.c_str(), *private_key, policy)),
2135                         CKMCReadableError(temp));
2136
2137         CKM::Alias cert_alias = "cert_alias";
2138         RUNNER_ASSERT_MSG(
2139                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(cert_alias.c_str(), *cert, policy)),
2140                         CKMCReadableError(temp));
2141         std::string caCertAlias = "ca_cert_alias_";
2142         const char *idx = "0";
2143         int cnt = 0;
2144         ckmc_cert_list_s *tmpList = ca_cert_list;
2145         while(tmpList != NULL) {
2146                 caCertAlias.append(idx);
2147                 RUNNER_ASSERT_MSG(
2148                                 CKMC_ERROR_NONE == (temp = ckmc_save_cert(caCertAlias.c_str(), *(tmpList->cert), policy)),
2149                                 CKMCReadableError(temp));
2150                 tmpList = tmpList->next;
2151                 cnt ++;
2152         }
2153
2154         RUNNER_ASSERT_MSG(cnt ==  2, "Invalid CA Cert Count");
2155
2156         ckmc_key_free(private_key);
2157         ckmc_cert_free(cert);
2158         ckmc_cert_list_all_free(ca_cert_list);
2159 }
2160
2161 RUNNER_TEST(T3097_CAPI_TYPE_load_p12_file2) {
2162         const char *p12file = CKM_TEST_DIR "/capi-t3096.p12";
2163         const char *password = "password";
2164
2165         int temp;
2166
2167         ckmc_pkcs12_s *ppkcs12 = NULL;
2168
2169         RUNNER_ASSERT_MSG(
2170                         CKMC_ERROR_NONE == (temp = ckmc_pkcs12_load(p12file, password, &ppkcs12)),
2171                         CKMCReadableError(temp));
2172         RUNNER_ASSERT_MSG(ppkcs12->priv_key != NULL, "Null private_key");
2173         RUNNER_ASSERT_MSG(ppkcs12->cert != NULL, "Null cert");
2174         RUNNER_ASSERT_MSG(ppkcs12->ca_chain != NULL, "Null ca_cert_list");
2175
2176         ckmc_policy_s policy;
2177         policy.password = NULL;
2178         policy.extractable = 1;
2179
2180
2181         CKM::Alias pkey_alias = "pkey_alias2";
2182         RUNNER_ASSERT_MSG(
2183                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pkey_alias.c_str(), *(ppkcs12->priv_key), policy)),
2184                         CKMCReadableError(temp));
2185
2186         CKM::Alias cert_alias = "cert_alias2";
2187         RUNNER_ASSERT_MSG(
2188                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(cert_alias.c_str(), *(ppkcs12->cert), policy)),
2189                         CKMCReadableError(temp));
2190         std::string caCertAlias = "ca_cert_alias_2_";
2191         const char *idx = "0";
2192         int cnt = 0;
2193         ckmc_cert_list_s *tmpList = ppkcs12->ca_chain;
2194         while(tmpList != NULL) {
2195                 caCertAlias.append(idx);
2196                 RUNNER_ASSERT_MSG(
2197                                 CKMC_ERROR_NONE == (temp = ckmc_save_cert(caCertAlias.c_str(), *(tmpList->cert), policy)),
2198                                 CKMCReadableError(temp));
2199                 tmpList = tmpList->next;
2200                 cnt ++;
2201         }
2202
2203         RUNNER_ASSERT_MSG(cnt ==  2, "Invalid CA Cert Count");
2204
2205         ckmc_pkcs12_free(ppkcs12);
2206 }
2207
2208 RUNNER_TEST(T3098_CAPI_TYPE_deinit)
2209 {
2210         int temp;
2211         RUNNER_ASSERT_MSG(
2212                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
2213                         CKMCReadableError(temp));
2214         RUNNER_ASSERT_MSG(
2215                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
2216                         CKMCReadableError(temp));
2217 }
2218
2219 RUNNER_TEST_GROUP_INIT(T310_CKMC_CAPI_PKCS12);
2220
2221 namespace
2222 {
2223 CKM::Alias alias_PKCS_exportable = "CAPI-test-PKCS-export";
2224 CKM::Alias alias_PKCS_not_exportable = "CAPI-test-PKCS-no-export";
2225 CKM::Alias alias_PKCS_key_not_exportable = "CAPI-test-PKCS-no-key-export";
2226 }
2227
2228 RUNNER_TEST(T3101_CAPI_PKCS12_init)
2229 {
2230         remove_user_data(USER_APP);
2231 }
2232
2233 RUNNER_TEST(T3102_CAPI_PKCS12_negative_wrong_password)
2234 {
2235         const char *wrong_passwd = "wrong";
2236         ckmc_pkcs12_s *ppkcs12 = NULL;
2237         int temp;
2238         RUNNER_ASSERT_MSG(
2239                         CKMC_ERROR_INVALID_FORMAT == (temp = ckmc_pkcs12_load(CKM_TEST_DIR "/test1801.pkcs12", wrong_passwd, &ppkcs12)),
2240                         CKMCReadableError(temp));
2241 }
2242
2243 RUNNER_TEST(T3103_CAPI_PKCS12_add_bundle_with_chain_certs)
2244 {
2245         ckmc_pkcs12_s *ppkcs12 = NULL;
2246         int temp;
2247         RUNNER_ASSERT_MSG(
2248                         CKMC_ERROR_NONE == (temp = ckmc_pkcs12_load(CKM_TEST_DIR "/pkcs.p12", NULL, &ppkcs12)),
2249                         CKMCReadableError(temp));
2250
2251         RUNNER_ASSERT_MSG(NULL != ppkcs12->cert, "no certificate in PKCS12");
2252         RUNNER_ASSERT_MSG(NULL != ppkcs12->priv_key, "no private key in PKCS12");
2253         RUNNER_ASSERT_MSG(NULL != ppkcs12->ca_chain, "no chain certificates in PKCS12");
2254
2255         // save to the CKM
2256         ckmc_policy_s exportable;
2257         exportable.password = NULL;
2258         exportable.extractable = 1;
2259         ckmc_policy_s notExportable;
2260         notExportable.password = NULL;
2261         notExportable.extractable = 0;
2262
2263         RUNNER_ASSERT_MSG(
2264                         CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_exportable.c_str(), ppkcs12, exportable, exportable)),
2265                         CKMCReadableError(temp));
2266         RUNNER_ASSERT_MSG(
2267                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_exportable.c_str(), ppkcs12, exportable, exportable)),
2268                         CKMCReadableError(temp));
2269         RUNNER_ASSERT_MSG(
2270                         CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_not_exportable.c_str(), ppkcs12, notExportable, notExportable)),
2271                         CKMCReadableError(temp));
2272         RUNNER_ASSERT_MSG(
2273                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_not_exportable.c_str(), ppkcs12, notExportable, notExportable)),
2274                         CKMCReadableError(temp));
2275         RUNNER_ASSERT_MSG(
2276                         CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_key_not_exportable.c_str(), ppkcs12, notExportable, exportable)),
2277                         CKMCReadableError(temp));
2278         RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_key_not_exportable.c_str(), ppkcs12, notExportable, exportable)),
2279                         CKMCReadableError(temp));
2280
2281         // try to lookup key
2282         ckmc_key_s *key_lookup = NULL;
2283         RUNNER_ASSERT_MSG(
2284                         CKMC_ERROR_NONE == (temp = ckmc_get_key(alias_PKCS_exportable.c_str(), NULL, &key_lookup)),
2285                         CKMCReadableError(temp));
2286         ckmc_key_free(key_lookup);
2287         key_lookup = NULL;
2288         RUNNER_ASSERT_MSG(
2289                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_key(alias_PKCS_not_exportable.c_str(), "", &key_lookup)),
2290                         CKMCReadableError(temp));
2291         ckmc_key_free(key_lookup);
2292         key_lookup = NULL;
2293         RUNNER_ASSERT_MSG(CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_key(alias_PKCS_key_not_exportable.c_str(), "", &key_lookup)),
2294                         CKMCReadableError(temp));
2295         ckmc_key_free(key_lookup);
2296
2297         // try to lookup certificate
2298         ckmc_cert_s *cert_lookup = NULL;
2299         RUNNER_ASSERT_MSG(
2300                         CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias_PKCS_exportable.c_str(), NULL, &cert_lookup)),
2301                         CKMCReadableError(temp));
2302         ckmc_cert_free(cert_lookup);
2303         cert_lookup = NULL;
2304         RUNNER_ASSERT_MSG(
2305                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_cert(alias_PKCS_not_exportable.c_str(), NULL, &cert_lookup)),
2306                         CKMCReadableError(temp));
2307         ckmc_cert_free(cert_lookup);
2308         cert_lookup = NULL;
2309         RUNNER_ASSERT_MSG(
2310                         CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias_PKCS_key_not_exportable.c_str(), NULL, &cert_lookup)),
2311                         CKMCReadableError(temp));
2312         ckmc_cert_free(cert_lookup);
2313 }
2314
2315 RUNNER_TEST(T3104_CAPI_PKCS12_get_PKCS)
2316 {
2317         int temp;
2318         ckmc_pkcs12_s *pkcs = NULL;
2319
2320         // fail - no entry
2321         RUNNER_ASSERT_MSG(
2322                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_pkcs12("i-do-not-exist", NULL, NULL, &pkcs)),
2323                         CKMCReadableError(temp));
2324         ckmc_pkcs12_free(pkcs);
2325         pkcs = NULL;
2326
2327         // fail - not exportable
2328         RUNNER_ASSERT_MSG(
2329                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_pkcs12(alias_PKCS_not_exportable.c_str(), NULL, NULL, &pkcs)),
2330                         CKMCReadableError(temp));
2331         ckmc_pkcs12_free(pkcs);
2332         pkcs = NULL;
2333
2334         // success - partially exportable
2335         RUNNER_ASSERT_MSG(
2336                         CKMC_ERROR_NONE == (temp = ckmc_get_pkcs12(alias_PKCS_key_not_exportable.c_str(), NULL, NULL, &pkcs)),
2337                         CKMCReadableError(temp));
2338
2339         RUNNER_ASSERT_MSG(NULL != pkcs->cert, "no certificate in PKCS12");
2340         RUNNER_ASSERT_MSG(NULL == pkcs->priv_key, "there should be no private key in PKCS12");
2341         RUNNER_ASSERT_MSG(NULL != pkcs->ca_chain, "no chain certificates in PKCS12");
2342         size_t cntr = 0;
2343         ckmc_cert_list_s *iter = pkcs->ca_chain;
2344         do {
2345                 cntr++;
2346                 iter = iter->next;
2347         } while (iter);
2348         RUNNER_ASSERT_MSG(2 == cntr, "invalid number of chain certificates in PKCS12");
2349
2350         ckmc_pkcs12_free(pkcs);
2351
2352
2353         // success - exportable
2354         RUNNER_ASSERT_MSG(
2355                         CKMC_ERROR_NONE == (temp = ckmc_get_pkcs12(alias_PKCS_exportable.c_str(), NULL, NULL, &pkcs)),
2356                         CKMCReadableError(temp));
2357
2358         RUNNER_ASSERT_MSG(NULL != pkcs->cert, "no certificate in PKCS12");
2359         RUNNER_ASSERT_MSG(NULL != pkcs->priv_key, "no private key in PKCS12");
2360         RUNNER_ASSERT_MSG(NULL != pkcs->ca_chain, "no chain certificates in PKCS12");
2361         cntr = 0;
2362         iter = pkcs->ca_chain;
2363         do {
2364                 cntr ++;
2365                 iter = iter->next;
2366         } while(iter);
2367         RUNNER_ASSERT_MSG(2 == cntr, "invalid number of chain certificates in PKCS12");
2368
2369         ckmc_pkcs12_free(pkcs);
2370 }
2371
2372 RUNNER_TEST(T3105_CAPI_PKCS12_create_and_verify_signature)
2373 {
2374         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
2375
2376         int temp;
2377         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
2378         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
2379         ckmc_raw_buffer_s *signature = NULL;
2380
2381         RUNNER_ASSERT_MSG(
2382                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
2383                                         alias_PKCS_exportable.c_str(),
2384                                         NULL,
2385                                         msg_buff,
2386                                         hash_algo,
2387                                         pad_algo,
2388                                         &signature)),
2389                         CKMCReadableError(temp));
2390
2391         RUNNER_ASSERT_MSG(
2392                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
2393                                         alias_PKCS_exportable.c_str(),
2394                                         NULL,
2395                                         msg_buff,
2396                                         *signature,
2397                                         hash_algo,
2398                                         pad_algo)),
2399                         CKMCReadableError(temp));
2400 }
2401
2402 RUNNER_TEST(T3106_CAPI_PKCS12_remove_bundle_with_chain_certs)
2403 {
2404         int tmp;
2405
2406         // remove the whole PKCS12 bundles
2407         RUNNER_ASSERT_MSG(
2408                         CKMC_ERROR_NONE == (tmp = ckmc_remove_alias(alias_PKCS_exportable.c_str())),
2409                         CKMCReadableError(tmp));
2410         RUNNER_ASSERT_MSG(
2411                         CKMC_ERROR_NONE == (tmp = ckmc_remove_alias(alias_PKCS_not_exportable.c_str())),
2412                         CKMCReadableError(tmp));
2413         RUNNER_ASSERT_MSG(
2414                         CKMC_ERROR_NONE == (tmp = ckmc_remove_alias(alias_PKCS_key_not_exportable.c_str())),
2415                         CKMCReadableError(tmp));
2416
2417         // expect lookup fails due to unknown alias
2418         // try to lookup key
2419         ckmc_key_s *key_lookup = NULL;
2420         RUNNER_ASSERT_MSG(
2421                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_exportable.c_str(), NULL, &key_lookup)),
2422                         CKMCReadableError(tmp));
2423         ckmc_key_free(key_lookup);
2424         key_lookup = NULL;
2425         RUNNER_ASSERT_MSG(
2426                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_not_exportable.c_str(), NULL, &key_lookup)),
2427                         CKMCReadableError(tmp));
2428         ckmc_key_free(key_lookup);
2429         key_lookup = NULL;
2430         RUNNER_ASSERT_MSG(
2431                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_key_not_exportable.c_str(), NULL, &key_lookup)),
2432                         CKMCReadableError(tmp));
2433         ckmc_key_free(key_lookup);
2434
2435         // try to lookup certificate
2436         ckmc_cert_s *cert_lookup = NULL;
2437         RUNNER_ASSERT_MSG(
2438                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_exportable.c_str(), NULL, &cert_lookup)),
2439                         CKMCReadableError(tmp));
2440         ckmc_cert_free(cert_lookup);
2441         cert_lookup = NULL;
2442         RUNNER_ASSERT_MSG(
2443                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_not_exportable.c_str(), NULL, &cert_lookup)),
2444                         CKMCReadableError(tmp));
2445         ckmc_cert_free(cert_lookup);
2446         cert_lookup = NULL;
2447         RUNNER_ASSERT_MSG(
2448                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_key_not_exportable.c_str(), NULL, &cert_lookup)),
2449                         CKMCReadableError(tmp));
2450         ckmc_cert_free(cert_lookup);
2451 }
2452
2453 RUNNER_TEST(T3109_CAPI_PKCS12_deinit)
2454 {
2455         remove_user_data(USER_APP);
2456 }
2457
2458
2459 RUNNER_TEST_GROUP_INIT(T320_CAPI_EMPTY_DATABASE);
2460
2461 RUNNER_TEST(T3201_CAPI_unlock_database)
2462 {
2463         reset_user_data(USER_APP, USER_PASS);
2464 }
2465
2466 RUNNER_TEST(T3202_CAPI_get_data_from_empty_database)
2467 {
2468         ScopedDBUnlock unlock(USER_APP, USER_PASS);
2469
2470         int temp;
2471         CKM::Alias alias = "mykey";
2472         char *password = NULL;
2473         ckmc_key_s *test_key = NULL;
2474
2475         RUNNER_ASSERT_MSG(
2476                 CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_key(alias.c_str(), password, &test_key)),
2477                 "Error=" << temp);
2478
2479         RUNNER_ASSERT_MSG(NULL == test_key, "Key value should not be changed");
2480 }
2481
2482 RUNNER_TEST(T3203_CAPI_lock_database)
2483 {
2484         ScopedDBUnlock unlock(USER_APP, USER_PASS);
2485
2486         int temp;
2487         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
2488                 CKMCReadableError(temp));
2489 }
2490
2491 RUNNER_TEST(T3204_CAPI_get_data_from_locked_database)
2492 {
2493         int temp;
2494         CKM::Alias alias = "mykey";
2495         char *password = NULL;
2496         ckmc_key_s *test_key = NULL;
2497
2498         RUNNER_ASSERT_MSG(
2499                 CKMC_ERROR_DB_LOCKED == (temp = ckmc_get_key(alias.c_str(), password, &test_key)),
2500                 CKMCReadableError(temp));
2501
2502         RUNNER_ASSERT_MSG(NULL == test_key, "Key value should not be changed");
2503 }
2504
2505 RUNNER_TEST(T3204_deinit)
2506 {
2507         remove_user_data(USER_APP);
2508 }
2509