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