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)
888 {
889         GarbageCollector gc;
890         const size_t BIG_SIZE = 5000000; // can't go much further because of stack size limit
891         ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
892
893         char big_data[BIG_SIZE];
894         std::ifstream is("/dev/urandom", std::ifstream::binary);
895         if(is)
896                 is.read(big_data, BIG_SIZE);
897
898         RUNNER_ASSERT_MSG(is,
899                         "Only " << is.gcount() << "/" << BIG_SIZE << " bytes read from /dev/urandom");
900
901         gc.save(TEST_ALIAS1.c_str(), big_data, BIG_SIZE, CKMC_ERROR_NONE);
902
903         check_read(TEST_OBJECT1, TEST_LABEL, big_data, BIG_SIZE, CKMC_ERROR_NONE);
904 }
905
906 RUNNER_TEST(T3050_deinit_C_API)
907 {
908         int temp;
909
910         remove_user_data(0);
911         RUNNER_ASSERT_MSG(
912                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
913                         CKMCReadableError(temp));
914         RUNNER_ASSERT_MSG(
915                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
916                         CKMCReadableError(temp));
917 }
918
919 RUNNER_TEST_GROUP_INIT(T305_CKMC_QUICK_CREATE_PAIR_CAPI);
920
921 RUNNER_TEST(T3051_CAPI_init)
922 {
923         int temp;
924
925         RUNNER_ASSERT_MSG(
926                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
927                         CKMCReadableError(temp));
928
929         RUNNER_ASSERT_MSG(
930                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
931                         CKMCReadableError(temp));
932 }
933
934 RUNNER_CHILD_TEST(T3052_CAPI_create_RSA_key)
935 {
936         int temp;
937
938         ScopedAccessProvider ap("mylabel");
939         ap.allowAPI("key-manager::api-storage", "rw");
940         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
941
942         size_t size = 1024;
943         const char *private_key_alias = "RSA-test-1-priv";
944         const char *public_key_alias = "RSA-test-1-pub";
945         ckmc_policy_s policy_private_key;
946         ckmc_policy_s policy_public_key;
947
948         policy_private_key.password = NULL;
949         policy_private_key.extractable = 1;
950
951         policy_public_key.password = NULL;
952         policy_public_key.extractable = 1;
953
954
955         size_t current_aliases_num = count_aliases(ALIAS_KEY);
956
957         RUNNER_ASSERT_MSG(
958                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
959                         CKMCReadableError(temp));
960
961         size_t actual_cnt = count_aliases(ALIAS_KEY);
962         RUNNER_ASSERT_MSG(
963                         (current_aliases_num+2) == actual_cnt,
964                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << actual_cnt);
965
966         ckmc_key_s *privateKey;
967         RUNNER_ASSERT_MSG(
968                         CKMC_ERROR_NONE == (temp = ckmc_get_key(private_key_alias, policy_private_key.password,&privateKey)),
969                         CKMCReadableError(temp));
970         RUNNER_ASSERT_MSG(
971                         privateKey->key_type == CKMC_KEY_RSA_PRIVATE,
972                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_RSA_PRIVATE) << ", actual=" << static_cast<int>(privateKey->key_type));
973         RUNNER_ASSERT_MSG(
974                         privateKey != NULL && privateKey->key_size > 0 && privateKey->raw_key != NULL,
975                         "Private key is broken.");
976
977         ckmc_key_s *publicKey;
978         RUNNER_ASSERT_MSG(
979                         CKMC_ERROR_NONE == (temp = ckmc_get_key(public_key_alias, policy_public_key.password, &publicKey)),
980                         CKMCReadableError(temp));
981         RUNNER_ASSERT_MSG(
982                         publicKey->key_type == CKMC_KEY_RSA_PUBLIC,
983                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_RSA_PUBLIC) << ", actual=" << static_cast<int>(publicKey->key_type));
984         RUNNER_ASSERT_MSG(
985                         publicKey != NULL && publicKey->key_size > 0 && publicKey->raw_key != NULL,
986                         "Public key is broken.");
987
988         // on next attempt to generate keys with the same alias, expect fail (alias exists)
989         RUNNER_ASSERT_MSG(
990                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_rsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
991                         CKMCReadableError(temp));
992 }
993
994 RUNNER_CHILD_TEST(T3053_CAPI_create_DSA_key)
995 {
996         int temp;
997
998         AccessProvider ap("mylabel");
999         ap.allowAPI("key-manager::api-storage", "rw");
1000         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
1001
1002         size_t size = 1024;
1003         const char *private_key_alias = "DSA-test-2-priv";
1004         const char *public_key_alias = "DSA-test-2-pub";
1005         ckmc_policy_s policy_private_key;
1006         ckmc_policy_s policy_public_key;
1007
1008         policy_private_key.password = NULL;
1009         policy_private_key.extractable = 1;
1010
1011         policy_public_key.password = NULL;
1012         policy_public_key.extractable = 1;
1013
1014         size_t current_aliases_num = count_aliases(ALIAS_KEY);
1015
1016         RUNNER_ASSERT_MSG(
1017                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
1018                         CKMCReadableError(temp));
1019
1020         size_t actual_cnt = count_aliases(ALIAS_KEY);
1021         RUNNER_ASSERT_MSG(
1022                         (current_aliases_num+2) == actual_cnt,
1023                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << actual_cnt);
1024
1025         ckmc_key_s *privateKey = 0;
1026         RUNNER_ASSERT_MSG(
1027                         CKMC_ERROR_NONE == (temp = ckmc_get_key(private_key_alias, policy_private_key.password,&privateKey)),
1028                         CKMCReadableError(temp));
1029         RUNNER_ASSERT_MSG(
1030                         privateKey != NULL && privateKey->key_size > 0 && privateKey->raw_key != NULL,
1031                         "Private key is broken.");
1032         RUNNER_ASSERT_MSG(
1033                         privateKey->key_type == CKMC_KEY_DSA_PRIVATE,
1034                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_DSA_PRIVATE) << ", actual=" << static_cast<int>(privateKey->key_type));
1035         ckmc_key_free(privateKey);
1036
1037         ckmc_key_s *pubKey = 0;
1038         RUNNER_ASSERT_MSG(
1039                         CKMC_ERROR_NONE == (temp = ckmc_get_key(public_key_alias, policy_public_key.password, &pubKey)),
1040                         CKMCReadableError(temp));
1041         RUNNER_ASSERT_MSG(
1042                         pubKey != NULL && pubKey->key_size > 0 && pubKey->raw_key != NULL,
1043                         "Public key is broken.");
1044         RUNNER_ASSERT_MSG(
1045                         pubKey->key_type == CKMC_KEY_DSA_PUBLIC,
1046                         "Key Type Error: expected =" << static_cast<int>(CKMC_KEY_DSA_PUBLIC) << ", actual=" << static_cast<int>(pubKey->key_type));
1047         ckmc_key_free(pubKey);
1048
1049         // on next attempt to generate keys with the same alias, expect fail (alias exists)
1050         RUNNER_ASSERT_MSG(
1051                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_create_key_pair_dsa(size, private_key_alias, public_key_alias, policy_private_key, policy_public_key)),
1052                         CKMCReadableError(temp));
1053 }
1054
1055
1056 RUNNER_CHILD_TEST(T3054_CAPI_create_AES_key)
1057 {
1058         int temp;
1059         size_t size = 128;
1060         CKM::Alias key_alias = sharedDatabase("AES-gen-test-1");
1061         ckmc_policy_s policy_key;
1062
1063         policy_key.password = NULL;
1064         policy_key.extractable = 1;
1065
1066         int current_aliases_num = count_aliases(ALIAS_KEY);
1067
1068         RUNNER_ASSERT_MSG(
1069                         CKMC_ERROR_NONE == (temp = ckmc_create_key_aes(size, key_alias.c_str(), policy_key)),
1070                         CKMCReadableError(temp));
1071
1072         RUNNER_ASSERT_MSG(
1073                         (current_aliases_num+1) == (temp = count_aliases(ALIAS_KEY)),
1074                         "Error: expecting " << (current_aliases_num+2) << " aliases, while found " << temp);
1075
1076         ckmc_key_s *get_AES_key = 0;
1077         RUNNER_ASSERT_MSG(
1078                         CKMC_ERROR_NONE == (temp = ckmc_get_key(key_alias.c_str(), policy_key.password, &get_AES_key)),
1079                         CKMCReadableError(temp));
1080         validate_AES_key(get_AES_key);
1081         ckmc_key_free(get_AES_key);
1082 }
1083
1084
1085 RUNNER_TEST(T3055_CAPI_deinit)
1086 {
1087         int temp;
1088
1089         RUNNER_ASSERT_MSG(
1090                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
1091                         CKMCReadableError(temp));
1092         RUNNER_ASSERT_MSG(
1093                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
1094                         CKMCReadableError(temp));
1095 }
1096
1097
1098 RUNNER_TEST_GROUP_INIT(T306_CKMC_CAPI_CreateKeyPair);
1099
1100 RUNNER_TEST(T3061_CAPI_init)
1101 {
1102         remove_user_data(0);
1103         reset_user_data(USER_APP, USER_PASS);
1104 }
1105
1106 RUNNER_TEST(T3062_CAPI_CreateKeyPairRSA)
1107 {
1108         int temp;
1109
1110         size_t size = 1024;
1111         CKM::Alias private_key_alias = sharedDatabase("rsa-test-1");
1112         CKM::Alias public_key_alias = sharedDatabase("rsa-test-2");
1113         ckmc_policy_s policy_private_key;
1114         ckmc_policy_s policy_public_key;
1115
1116         policy_private_key.password = const_cast<char *>("privatepassword");
1117         policy_private_key.extractable = 0;
1118
1119         policy_public_key.password = NULL;
1120         policy_public_key.extractable = 1;
1121
1122         RUNNER_ASSERT_MSG(
1123                         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)),
1124                         CKMCReadableError(temp));
1125
1126         // on next attempt to generate keys with the same alias, expect fail (alias exists)
1127         RUNNER_ASSERT_MSG(
1128                         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)),
1129                         CKMCReadableError(temp));
1130 }
1131
1132 RUNNER_TEST(T3063_CAPI_CreateKeyPairDSA)
1133 {
1134         int temp;
1135
1136         size_t size = 1024;
1137         CKM::Alias private_key_alias = sharedDatabase("dsa-test-1");
1138         CKM::Alias public_key_alias = sharedDatabase("dsa-test-2");
1139         ckmc_policy_s policy_private_key;
1140         ckmc_policy_s policy_public_key;
1141
1142         policy_private_key.password = const_cast<char *>("privatepassword");
1143         policy_private_key.extractable = 0;
1144
1145         policy_public_key.password = NULL;
1146         policy_public_key.extractable = 1;
1147
1148         RUNNER_ASSERT_MSG(
1149                         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)),
1150                         CKMCReadableError(temp));
1151 }
1152
1153 RUNNER_TEST(T3064_CAPI_CreateKeyPairECDSA)
1154 {
1155         int temp;
1156
1157         ckmc_ec_type_e ectype = CKMC_EC_PRIME192V1;
1158         CKM::Alias private_key_alias = sharedDatabase("ecdsa-test-1");
1159         CKM::Alias public_key_alias = sharedDatabase("ecdsa-test-2");
1160         ckmc_policy_s policy_private_key;
1161         ckmc_policy_s policy_public_key;
1162
1163         policy_private_key.password = const_cast<char *>("privatepassword");
1164         policy_private_key.extractable = 0;
1165
1166         policy_public_key.password = NULL;
1167         policy_public_key.extractable = 1;
1168
1169         RUNNER_ASSERT_MSG(
1170                         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)),
1171                         CKMCReadableError(temp));
1172 }
1173
1174 RUNNER_TEST(T3065_CAPI_deinit)
1175 {
1176         remove_user_data(0);
1177 }
1178
1179 // TODO
1180 //RUNNER_TEST_GROUP_INIT(T120_NEGATIVE_TESTS);
1181
1182
1183
1184 RUNNER_TEST_GROUP_INIT(T307_CKMC_CAPI_OCSP_TESTS);
1185
1186 RUNNER_TEST(T3071_CAPI_init)
1187 {
1188         remove_user_data(0);
1189 }
1190
1191 RUNNER_TEST(T3074_CAPI_ckmc_ocsp_check)
1192 {
1193         std::string ee = TestData::getTestCertificateBase64(TestData::MBANK);
1194         std::string im = TestData::getTestCertificateBase64(TestData::SYMANTEC);
1195
1196         ckmc_cert_s c_cert;
1197         c_cert.raw_cert = reinterpret_cast<unsigned char *>(const_cast<char *>(ee.c_str()));
1198         c_cert.cert_size = ee.size();
1199         c_cert.data_format = CKMC_FORM_PEM;
1200
1201         ckmc_cert_s c_cert1;
1202         c_cert1.raw_cert = reinterpret_cast<unsigned char *>(const_cast<char *>(im.c_str()));
1203         c_cert1.cert_size = im.size();
1204         c_cert1.data_format = CKMC_FORM_PEM;
1205
1206         ckmc_cert_list_s untrustedcerts;
1207         untrustedcerts.cert = &c_cert1;
1208         untrustedcerts.next = NULL;
1209
1210         ckmc_cert_list_s *cert_chain_list;
1211
1212         int     tmp = ckmc_get_cert_chain(&c_cert, &untrustedcerts, &cert_chain_list);
1213         RUNNER_ASSERT_MSG(
1214                         CKMC_ERROR_NONE == tmp, CKMCReadableError(tmp));
1215
1216         RUNNER_ASSERT_MSG(cert_chain_list != NULL, "Wrong size of certificate chain.");
1217
1218         ckmc_ocsp_status_e ocsp_status;
1219         RUNNER_ASSERT_MSG(CKMC_ERROR_NONE == (tmp = ckmc_ocsp_check(cert_chain_list, &ocsp_status)), CKMCReadableError(tmp));
1220         RUNNER_ASSERT_MSG(ocsp_status == CKMC_OCSP_STATUS_GOOD, "Wrong status: " << static_cast<int>(ocsp_status));
1221 }
1222
1223 RUNNER_TEST(T3075_CAPI_deinit)
1224 {
1225         remove_user_data(0);
1226 }
1227
1228
1229 RUNNER_TEST_GROUP_INIT(T308_CAPI_CREATE_AND_VERIFY_SIGNATURE);
1230
1231 RUNNER_TEST(T3081_CAPI__init)
1232 {
1233         int temp;
1234
1235         RUNNER_ASSERT_MSG(
1236                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
1237                         CKMCReadableError(temp));
1238
1239         RUNNER_ASSERT_MSG(
1240                         CKMC_ERROR_NONE == (temp = ckmc_unlock_user_key(USER_APP, USER_PASS)),
1241                         CKMCReadableError(temp));
1242
1243         remove_user_data(0);
1244 }
1245
1246 RUNNER_TEST(T3082_CAPI__rsa_key_create_verify)
1247 {
1248         int temp;
1249
1250         std::string prv = "-----BEGIN RSA PRIVATE KEY-----\n"
1251                 "Proc-Type: 4,ENCRYPTED\n"
1252                 "DEK-Info: DES-EDE3-CBC,6C6507B11671DABC\n"
1253                 "\n"
1254                 "YiKNviNqc/V/i241CKtAVsNckesE0kcaka3VrY7ApXR+Va93YoEwVQ8gB9cE/eHH\n"
1255                 "S0j3ZS1PAVFM/qo4ZnPdMzaSLvTQw0GAL90wWgF3XQ+feMnWyBObEoQdGXE828TB\n"
1256                 "SLz4UOIQ55Dx6JSWTfEhwAlPs2cEWD14xvuxPzAEzBIYmWmBBsCN94YgFeRTzjH0\n"
1257                 "TImoYVMN60GgOfZWw6rXq9RaV5dY0Y6F1piypCLGD35VaXAutdHIDvwUGECPm7SN\n"
1258                 "w05jRro53E1vb4mYlZEY/bs4q7XEOI5+ZKT76Xn0oEJNX1KRL1h2q8fgUkm5j40M\n"
1259                 "uQj71aLR9KyIoQARwGLeRy09tLVjH3fj66CCMqaPcxcIRIyWi5yYBB0s53ipm6A9\n"
1260                 "CYuyc7MS2C0pOdWKsDvYsHR/36KUiIdPuhF4AbaTqqO0eWeuP7Na7dGK56Fl+ooi\n"
1261                 "cUpJr7cIqMl2vL25B0jW7d4TB3zwCEkVVD1fBPeNoZWo30z4bILcBqjjPkQfHZ2e\n"
1262                 "xNraG3qI4FHjoPT8JEE8p+PgwaMoINlICyIMKiCdvwz9yEnsHPy7FkmatpS+jFoS\n"
1263                 "mg8R9vMwgK/HGEm0dmb/7/a0XsG2jCDm6cOmJdZJFQ8JW7hFs3eOHpNlQYDChG2D\n"
1264                 "A1ExslqBtbpicywTZhzFdYU/hxeCr4UqcY27Zmhr4JlBPMyvadWKeOqCamWepjbT\n"
1265                 "T/MhWJbmWgZbI5s5sbpu7cOYubQcUIEsTaQXGx/KEzGo1HLn9tzSeQfP/nqjAD/L\n"
1266                 "T5t1Mb8o4LuV/fGIT33Q3i2FospJMqp2JINNzG18I6Fjo08PTvJ3row40Rb76+lJ\n"
1267                 "wN1IBthgBgsgsOdB6XNc56sV+uq2TACsNNWw+JnFRCkCQgfF/KUrvN+WireWq88B\n"
1268                 "9UPG+Hbans5A6K+y1a+bzfdYnKws7x8wNRyPxb7Vb2t9ZTl5PBorPLVGsjgf9N5X\n"
1269                 "tCdBlfJsUdXot+EOxrIczV5zx0JIB1Y9hrDG07RYkzPuJKxkW7skqeLo8oWGVpaQ\n"
1270                 "LGWvuebky1R75hcSuL3e4QHfjBHPdQ31fScB884tqkbhBAWr2nT9bYEmyT170bno\n"
1271                 "8QkyOSb99xZBX55sLDHs9p61sTJr2C9Lz/KaWQs+3hTkpwSjSRyjEMH2n491qiQX\n"
1272                 "G+kvLEnvtR8sl9zinorj/RfsxyPntAxudfY3qaYUu2QkLvVdfTVUVbxS/Fg8f7B3\n"
1273                 "hEjCtpKgFjPxQuHE3didNOr5xM7mkmLN/QA7yHVgdpE64T5mFgC3JcVRpcR7zBPH\n"
1274                 "3OeXHgjrhDfN8UIX/cq6gNgD8w7O0rhHa3mEXI1xP14ykPcJ7wlRuLm9P3fwx5A2\n"
1275                 "jQrVKJKw1Nzummmspn4VOpJY3LkH4Sxo4e7Soo1l1cxJpzmERwgMF+vGz1L70+DG\n"
1276                 "M0hVrz1PxlOsBBFgcdS4TB91DIs/RcFDqrJ4gOPNKCgBP+rgTXXLFcxUwJfE3lKg\n"
1277                 "Kmpwdne6FuQYX3eyRVAmPgOHbJuRQCh/V4fYo51UxCcEKeKy6UgOPEJlXksWGbH5\n"
1278                 "VFmlytYW6dFKJvjltSmK6L2r+TlyEQoXwTqe4bkfhB2LniDEq28hKQ==\n"
1279                 "-----END RSA PRIVATE KEY-----\n";
1280
1281         std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1282                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1283                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1284                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1285                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1286                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1287                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1288                 "zQIDAQAB\n"
1289                 "-----END PUBLIC KEY-----\n";
1290
1291         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1292
1293         CKM::Alias pub_alias = sharedDatabase("pub1");
1294         CKM::Alias pri_alias = sharedDatabase("prv1");
1295         const char *key_passwd = "1234";
1296         char *pri_passwd = NULL;
1297         char *pub_passwd = NULL;
1298         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1299         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1300         ckmc_raw_buffer_s *signature;
1301
1302         ckmc_key_s pubkey;
1303         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1304         pubkey.key_size = pub.size();
1305         pubkey.key_type = CKMC_KEY_NONE;
1306         pubkey.password = NULL;
1307
1308         ckmc_policy_s pubpolicy;
1309         pubpolicy.password = pub_passwd;
1310         pubpolicy.extractable = 0;
1311
1312         ckmc_policy_s pripolicy;
1313         pripolicy.password = pri_passwd;
1314         pripolicy.extractable = 1;
1315
1316         ckmc_key_s prikey;
1317         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1318         prikey.key_size = prv.size();
1319         prikey.key_type = CKMC_KEY_NONE;
1320         prikey.password = const_cast<char *>(key_passwd);
1321
1322
1323         RUNNER_ASSERT_MSG(
1324                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1325                         CKMCReadableError(temp));
1326
1327         RUNNER_ASSERT_MSG(
1328                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias.c_str(), pubkey, pubpolicy)),
1329                         CKMCReadableError(temp));
1330
1331         RUNNER_ASSERT_MSG(
1332                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1333                                         pri_alias.c_str(),
1334                                         pri_passwd,
1335                                         msg_buff,
1336                                         hash_algo,
1337                                         pad_algo,
1338                                         &signature)),
1339                         CKMCReadableError(temp));
1340
1341         RUNNER_ASSERT_MSG(
1342                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1343                                         pub_alias.c_str(),
1344                                         pub_passwd,
1345                                         msg_buff,
1346                                         *signature,
1347                                         hash_algo,
1348                                         pad_algo)),
1349                         CKMCReadableError(temp));
1350 }
1351
1352 RUNNER_TEST(T3083_CAPI__rsa_key_create_verify_negative)
1353 {
1354         int temp;
1355
1356         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message asdfaslkdfjlksadjf test");
1357         CKM::Alias pub_alias = sharedDatabase("pub1");
1358         CKM::Alias pri_alias = sharedDatabase("prv1");
1359         char *pri_passwd = NULL;
1360         char *pub_passwd = NULL;
1361         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1362         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1363         ckmc_raw_buffer_s *signature;
1364
1365         RUNNER_ASSERT_MSG(
1366                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1367                                         pri_alias.c_str(),
1368                                         pri_passwd,
1369                                         msg_buff,
1370                                         hash_algo,
1371                                         pad_algo,
1372                                         &signature)),
1373                         CKMCReadableError(temp));
1374
1375         RUNNER_ASSERT_MSG(
1376                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1377                                         pub_alias.c_str(),
1378                                         pub_passwd,
1379                                         msg_buff,
1380                                         *signature,
1381                                         hash_algo,
1382                                         pad_algo)),
1383                         CKMCReadableError(temp));
1384
1385         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1386         memcpy((void*)signature->data, "BROKEN", 6);
1387
1388         RUNNER_ASSERT_MSG(
1389                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1390                                         pub_alias.c_str(),
1391                                         pub_passwd,
1392                                         msg_buff,
1393                                         *signature,
1394                                         hash_algo,
1395                                         pad_algo)),
1396                         CKMCReadableError(temp));
1397 }
1398
1399 RUNNER_TEST(T3084_CAPI__ec_key_create_verify)
1400 {
1401         int temp;
1402
1403         std::string prv = "-----BEGIN EC PRIVATE KEY-----\n"
1404                 "MHQCAQEEIJNud6U4h8EM1rASn4W5vQOJELTaVPQTUiESaBULvQUVoAcGBSuBBAAK\n"
1405                 "oUQDQgAEL1R+hgjiFrdjbUKRNOxUG8ze9nveD9zT05YHeT7vK0w08AUL1HCH5nFV\n"
1406                 "ljePBYSxe6CybFiseayaxRxjA+iF1g==\n"
1407                 "-----END EC PRIVATE KEY-----\n";
1408
1409         std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1410                 "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEL1R+hgjiFrdjbUKRNOxUG8ze9nveD9zT\n"
1411                 "05YHeT7vK0w08AUL1HCH5nFVljePBYSxe6CybFiseayaxRxjA+iF1g==\n"
1412                 "-----END PUBLIC KEY-----\n";
1413
1414         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1415         CKM::Alias pri_alias = sharedDatabase("ecprv2");
1416         CKM::Alias pub_alias = sharedDatabase("ecpub2");
1417         char *key_passwd = NULL;
1418         char *pri_passwd = NULL;
1419         char *pub_passwd = NULL;
1420         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1421         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1422         ckmc_raw_buffer_s *signature;
1423
1424         ckmc_key_s pubkey;
1425         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1426         pubkey.key_size = pub.size();
1427         pubkey.key_type = CKMC_KEY_NONE;
1428         pubkey.password = NULL;
1429
1430         ckmc_policy_s pubpolicy;
1431         pubpolicy.password = pub_passwd;
1432         pubpolicy.extractable = 1;
1433
1434         ckmc_key_s prikey;
1435         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1436         prikey.key_size = prv.size();
1437         prikey.key_type = CKMC_KEY_NONE;
1438         prikey.password = key_passwd;
1439
1440         ckmc_policy_s pripolicy;
1441         pripolicy.password = pri_passwd;
1442         pripolicy.extractable = 0;
1443
1444         RUNNER_ASSERT_MSG(
1445                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias.c_str(), pubkey, pubpolicy)),
1446                         CKMCReadableError(temp));
1447         RUNNER_ASSERT_MSG(
1448                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1449                         CKMCReadableError(temp));
1450
1451         RUNNER_ASSERT_MSG(
1452                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1453                                         pri_alias.c_str(),
1454                                         pri_passwd,
1455                                         msg_buff,
1456                                         hash_algo,
1457                                         pad_algo,
1458                                         &signature)),
1459                         CKMCReadableError(temp));
1460
1461         RUNNER_ASSERT_MSG(
1462                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1463                                         pub_alias.c_str(),
1464                                         pub_passwd,
1465                                         msg_buff,
1466                                         *signature,
1467                                         hash_algo,
1468                                         pad_algo)),
1469                         CKMCReadableError(temp));
1470
1471         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1472         memcpy((void*)signature->data, "BROKEN", 6);
1473
1474         RUNNER_ASSERT_MSG(
1475                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1476                                         pub_alias.c_str(),
1477                                         pub_passwd,
1478                                         msg_buff,
1479                                         *signature,
1480                                         hash_algo,
1481                                         pad_algo)),
1482                         CKMCReadableError(temp));
1483 }
1484
1485 RUNNER_TEST(T3085_CAPI__rsa_cert_create_verify_signature)
1486 {
1487         int temp;
1488
1489         std::string prv =
1490                 "-----BEGIN RSA PRIVATE KEY-----\n"
1491                 "MIICXQIBAAKBgQDCKb9BkTdOjCTXKPi/H5FSGuyrgzORBtR3nCTg7SRnL47zNGEj\n"
1492                 "l2wkgsY9ZO3UJHm0gy5KMjWeCuUVkSD3G46J9obg1bYJivCQBJKxfieA8sWOtNq1\n"
1493                 "M8emHGK8o3sjaRklrngmk2xSCs5vFJVlCluzAYUmrPDm64C3+n4yW4pBCQIDAQAB\n"
1494                 "AoGAd1IWgiHO3kuLvFome7XXpaB8P27SutZ6rcLcewnhLDRy4g0XgTrmL43abBJh\n"
1495                 "gdSkooVXZity/dvuKpHUs2dQ8W8zYiFFsHfu9qqLmLP6SuBPyUCvlUDH5BGfjjxI\n"
1496                 "5qGWIowj/qGHKpbQ7uB+Oe2BHwbHao0zFZIkfKqY0mX9U00CQQDwF/4zQcGS1RX/\n"
1497                 "229gowTsvSGVmm8cy1jGst6xkueEuOEZ/AVPO1fjavz+nTziUk4E5lZHAj18L6Hl\n"
1498                 "iO29LRujAkEAzwbEWVhfTJewCZIFf3sY3ifXhGZhVKDHVzPBNyoft8Z+09DMHTJb\n"
1499                 "EYg85MIbR73aUyIWsEci/CPk6LPRNv47YwJAHtQF2NEFqPPhakPjzjXAaSFz0YDN\n"
1500                 "6ZWWpZTMEWL6hUkz5iE9EUpeY54WNB8+dRT6XZix1VZNTMfU8uMdG6BSHwJBAKYM\n"
1501                 "gm47AGz5eVujwD8op6CACk+KomRzdI+P1lh9s+T+E3mnDiAY5IxiXp0Ix0K6lyN4\n"
1502                 "wwPuerQLwi2XFKZsMYsCQQDOiSQFP9PfXh9kFzN6e89LxOdnqC/r9i5GDB3ea8eL\n"
1503                 "SCRprpzqOXZvOP1HBAEjsJ6k4f8Dqj1fm+y8ZcgAZUPr\n"
1504                 "-----END RSA PRIVATE KEY-----\n";
1505
1506         std::string pub =
1507                 "-----BEGIN CERTIFICATE-----\n"
1508                 "MIICijCCAfOgAwIBAgIJAMvaNHQ1ozT8MA0GCSqGSIb3DQEBBQUAMF4xCzAJBgNV\n"
1509                 "BAYTAlBMMQ0wCwYDVQQIDARMb2R6MQ0wCwYDVQQHDARMb2R6MRAwDgYDVQQKDAdT\n"
1510                 "YW1zdW5nMREwDwYDVQQLDAhTZWN1cml0eTEMMAoGA1UEAwwDQ0tNMB4XDTE0MDcw\n"
1511                 "MjEyNDE0N1oXDTE3MDcwMTEyNDE0N1owXjELMAkGA1UEBhMCUEwxDTALBgNVBAgM\n"
1512                 "BExvZHoxDTALBgNVBAcMBExvZHoxEDAOBgNVBAoMB1NhbXN1bmcxETAPBgNVBAsM\n"
1513                 "CFNlY3VyaXR5MQwwCgYDVQQDDANDS00wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ\n"
1514                 "AoGBAMIpv0GRN06MJNco+L8fkVIa7KuDM5EG1HecJODtJGcvjvM0YSOXbCSCxj1k\n"
1515                 "7dQkebSDLkoyNZ4K5RWRIPcbjon2huDVtgmK8JAEkrF+J4DyxY602rUzx6YcYryj\n"
1516                 "eyNpGSWueCaTbFIKzm8UlWUKW7MBhSas8ObrgLf6fjJbikEJAgMBAAGjUDBOMB0G\n"
1517                 "A1UdDgQWBBQuW9DuITahZJ6saVZZI0aBlis5vzAfBgNVHSMEGDAWgBQuW9DuITah\n"
1518                 "ZJ6saVZZI0aBlis5vzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAB2X\n"
1519                 "GNtJopBJR3dCpzDONknr/c6qcsPVa3nH4c7qzy6F+4bgqa5IObnoF8zUrvD2sMAO\n"
1520                 "km3C/N+Qzt8Rb7ORM6U4tlPp1kZ5t6PKjghhNaiYwVm9A/Zm+wyAmRIkQiYDr4MX\n"
1521                 "e+bRAkPmJeEWpaav1lvvBnFzGSGJrnSSeWUegGyn\n"
1522                 "-----END CERTIFICATE-----\n";
1523
1524         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1525
1526         CKM::Alias pri_alias = sharedDatabase("prv3");
1527         CKM::Alias pub_alias = sharedDatabase("pub3");
1528         char *key_passwd = NULL;
1529         char *pri_passwd = NULL;
1530         char *pub_passwd = NULL;
1531         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1532         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1533         ckmc_raw_buffer_s *signature;
1534
1535         ckmc_cert_s cert;
1536         cert.raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1537         cert.cert_size = pub.size();
1538         cert.data_format = CKMC_FORM_PEM;
1539
1540         ckmc_policy_s certpolicy;
1541         certpolicy.password = pub_passwd;
1542         certpolicy.extractable = 1;
1543
1544         ckmc_key_s prikey;
1545         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1546         prikey.key_size = prv.size();
1547         prikey.key_type = CKMC_KEY_NONE;
1548         prikey.password = key_passwd;
1549
1550         ckmc_policy_s pripolicy;
1551         pripolicy.password = pri_passwd;
1552         pripolicy.extractable = 0;
1553
1554         RUNNER_ASSERT_MSG(
1555                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(pub_alias.c_str(), cert, certpolicy)),
1556                         CKMCReadableError(temp));
1557         RUNNER_ASSERT_MSG(
1558                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1559                         CKMCReadableError(temp));
1560
1561
1562         RUNNER_ASSERT_MSG(
1563                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1564                                         pri_alias.c_str(),
1565                                         pri_passwd,
1566                                         msg_buff,
1567                                         hash_algo,
1568                                         pad_algo,
1569                                         &signature)),
1570                         CKMCReadableError(temp));
1571
1572         RUNNER_ASSERT_MSG(
1573                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1574                                         pub_alias.c_str(),
1575                                         pub_passwd,
1576                                         msg_buff,
1577                                         *signature,
1578                                         hash_algo,
1579                                         pad_algo)),
1580                         CKMCReadableError(temp));
1581
1582         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1583         memcpy((void*)signature->data, "BROKEN", 6);
1584
1585         RUNNER_ASSERT_MSG(
1586                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1587                                         pub_alias.c_str(),
1588                                         pub_passwd,
1589                                         msg_buff,
1590                                         *signature,
1591                                         hash_algo,
1592                                         pad_algo)),
1593                         CKMCReadableError(temp));
1594 }
1595
1596 RUNNER_TEST(T3086_CAPI__dsa_ext_key_create_verify_with_negative)
1597 {
1598         int temp;
1599
1600         const std::string pub = "-----BEGIN PUBLIC KEY-----\n"
1601                 "MIIBtzCCASwGByqGSM44BAEwggEfAoGBALeveaD/EheW+ws1YuW77f344+brkEzm\n"
1602                 "BVfFYHr7t+jwu6nQe341SoESJG+PCgrrhy76KNDCfveiwEoWufVHnI4bYBU/ClzP\n"
1603                 "A3amf6c5yud45ZR/b6OiAuew6ohY0mQGnzqeio8BaCsZaJ6EziCSlkdIDJisSfPg\n"
1604                 "nlWHqf4AwHVdAhUA7I1JQ7sBFJ+N19w3Omu+aO8EG08CgYEAldagy/Ccxhh43cZu\n"
1605                 "AZQxgJLCcp1jg6NdPMdkZ2TcSijvaVxBu+gjEGOqN5Os2V6UF7S/k/rjHYmcX9ux\n"
1606                 "gpjkC31yTNrKyERIAFIYZtG2K7LVBUZq5Fgm7I83QBVchJ2PA7mBaugJFEhNjbhK\n"
1607                 "NRip5UH38le1YDZ/IiA+svFOpeoDgYQAAoGAPT91aEgwFdulzmHlvr3k+GBCE9z+\n"
1608                 "hq0c3FGUCtGbVOqg2KPqMBgwSb4MC0msQys4DTVZhLJI+C5eIPEHgfBMqY1ZNJdO\n"
1609                 "OSCQciDXnRfSqKbT6tjDTgR5jmh5bG1Q8QFeBTHCDsQHoQYWgx0nyu12lASN80rC\n"
1610                 "YMYCBhubtrVaLmc=\n"
1611                 "-----END PUBLIC KEY-----";
1612
1613         const std::string priv = "-----BEGIN DSA PRIVATE KEY-----\n"
1614                 "MIIBvAIBAAKBgQC3r3mg/xIXlvsLNWLlu+39+OPm65BM5gVXxWB6+7fo8Lup0Ht+\n"
1615                 "NUqBEiRvjwoK64cu+ijQwn73osBKFrn1R5yOG2AVPwpczwN2pn+nOcrneOWUf2+j\n"
1616                 "ogLnsOqIWNJkBp86noqPAWgrGWiehM4gkpZHSAyYrEnz4J5Vh6n+AMB1XQIVAOyN\n"
1617                 "SUO7ARSfjdfcNzprvmjvBBtPAoGBAJXWoMvwnMYYeN3GbgGUMYCSwnKdY4OjXTzH\n"
1618                 "ZGdk3Eoo72lcQbvoIxBjqjeTrNlelBe0v5P64x2JnF/bsYKY5At9ckzayshESABS\n"
1619                 "GGbRtiuy1QVGauRYJuyPN0AVXISdjwO5gWroCRRITY24SjUYqeVB9/JXtWA2fyIg\n"
1620                 "PrLxTqXqAoGAPT91aEgwFdulzmHlvr3k+GBCE9z+hq0c3FGUCtGbVOqg2KPqMBgw\n"
1621                 "Sb4MC0msQys4DTVZhLJI+C5eIPEHgfBMqY1ZNJdOOSCQciDXnRfSqKbT6tjDTgR5\n"
1622                 "jmh5bG1Q8QFeBTHCDsQHoQYWgx0nyu12lASN80rCYMYCBhubtrVaLmcCFQC0IB4m\n"
1623                 "u1roOuaPY+Hl19BlTE2qdw==\n"
1624                 "-----END DSA PRIVATE KEY-----";
1625
1626         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1627
1628         CKM::Alias pub_alias = sharedDatabase("dsa-pub1");
1629         CKM::Alias pri_alias = sharedDatabase("dsa-prv1");
1630         char *pri_passwd = NULL;
1631         char *pub_passwd = NULL;
1632         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1633         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1634         ckmc_raw_buffer_s *signature = NULL;
1635
1636         ckmc_key_s pubkey;
1637         pubkey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1638         pubkey.key_size = pub.size();
1639         pubkey.key_type = CKMC_KEY_NONE;
1640         pubkey.password = NULL;
1641
1642         ckmc_policy_s pubpolicy;
1643         pubpolicy.password = pub_passwd;
1644         pubpolicy.extractable = 0;
1645
1646         ckmc_policy_s pripolicy;
1647         pripolicy.password = pri_passwd;
1648         pripolicy.extractable = 1;
1649
1650         ckmc_key_s prikey;
1651         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(priv.c_str()));
1652         prikey.key_size = priv.size();
1653         prikey.key_type = CKMC_KEY_NONE;
1654         prikey.password = NULL;
1655
1656
1657         RUNNER_ASSERT_MSG(
1658                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1659                         CKMCReadableError(temp));
1660
1661         RUNNER_ASSERT_MSG(
1662                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pub_alias.c_str(), pubkey, pubpolicy)),
1663                         CKMCReadableError(temp));
1664
1665         RUNNER_ASSERT_MSG(
1666                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1667                                         pri_alias.c_str(),
1668                                         pri_passwd,
1669                                         msg_buff,
1670                                         hash_algo,
1671                                         pad_algo,
1672                                         &signature)),
1673                         CKMCReadableError(temp));
1674
1675         // positive test
1676         RUNNER_ASSERT_MSG(
1677                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1678                                         pub_alias.c_str(),
1679                                         pub_passwd,
1680                                         msg_buff,
1681                                         *signature,
1682                                         hash_algo,
1683                                         pad_algo)),
1684                         CKMCReadableError(temp));
1685
1686         // negative test
1687         ckmc_raw_buffer_s invalid_msg_buff = prepare_message_buffer("invalid message test");
1688         RUNNER_ASSERT_MSG(
1689                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1690                                         pub_alias.c_str(),
1691                                         pub_passwd,
1692                                         invalid_msg_buff,
1693                                         *signature,
1694                                         hash_algo,
1695                                         pad_algo)),
1696                         CKMCReadableError(temp));
1697
1698         ckmc_buffer_free(signature);
1699 }
1700
1701 RUNNER_TEST(T3087_CAPI__dsa_int_key_create_verify_with_negative)
1702 {
1703         int temp;
1704
1705         size_t size = 1024;
1706         ckmc_policy_s policy_private_key;
1707         ckmc_policy_s policy_public_key;
1708
1709         policy_private_key.password = NULL;
1710         policy_private_key.extractable = 1;
1711
1712         policy_public_key.password = NULL;
1713         policy_public_key.extractable = 1;
1714
1715         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1716
1717         CKM::Alias pub_alias = sharedDatabase("dsa-pub2");
1718         CKM::Alias pri_alias = sharedDatabase("dsa-prv2");
1719
1720         RUNNER_ASSERT_MSG(
1721                         CKMC_ERROR_NONE == (temp = ckmc_create_key_pair_dsa(size, pri_alias.c_str(), pub_alias.c_str(), policy_private_key, policy_public_key)),
1722                         "Error=" << temp);
1723
1724         char *pri_passwd = NULL;
1725         char *pub_passwd = NULL;
1726         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1727         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1728         ckmc_raw_buffer_s *signature;
1729
1730         ckmc_key_s *pubkey = NULL;
1731         ckmc_key_s *prikey = NULL;
1732         RUNNER_ASSERT_MSG(
1733                         CKMC_ERROR_NONE == (temp = ckmc_get_key(pri_alias.c_str(), 0, &prikey)),
1734                         CKMCReadableError(temp));
1735
1736         RUNNER_ASSERT_MSG(
1737                         CKMC_ERROR_NONE == (temp = ckmc_get_key(pub_alias.c_str(), 0, &pubkey)),
1738                         CKMCReadableError(temp));
1739
1740         RUNNER_ASSERT_MSG(
1741                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1742                                         pri_alias.c_str(),
1743                                         pri_passwd,
1744                                         msg_buff,
1745                                         hash_algo,
1746                                         pad_algo,
1747                                         &signature)),
1748                         CKMCReadableError(temp));
1749
1750         // positive test
1751         RUNNER_ASSERT_MSG(
1752                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1753                                         pub_alias.c_str(),
1754                                         pub_passwd,
1755                                         msg_buff,
1756                                         *signature,
1757                                         hash_algo,
1758                                         pad_algo)),
1759                         CKMCReadableError(temp));
1760
1761         // negative test
1762         ckmc_raw_buffer_s invalid_msg_buff = prepare_message_buffer("invalid message test");
1763         RUNNER_ASSERT_MSG(
1764                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1765                                         pub_alias.c_str(),
1766                                         pub_passwd,
1767                                         invalid_msg_buff,
1768                                         *signature,
1769                                         hash_algo,
1770                                         pad_algo)),
1771                         CKMCReadableError(temp));
1772
1773         ckmc_key_free(prikey);
1774         ckmc_key_free(pubkey);
1775         ckmc_buffer_free(signature);
1776 }
1777
1778 RUNNER_TEST(T3088_CAPI__ecdsa_cert_create_verify_signature)
1779 {
1780         int temp;
1781
1782         std::string prv =
1783                 "-----BEGIN EC PRIVATE KEY-----\n"
1784                 "MIH8AgEBBBRPb/2utS5aCtyuwmzIHpU6LH3mc6CBsjCBrwIBATAgBgcqhkjOPQEB\n"
1785                 "AhUA/////////////////////3////8wQwQU/////////////////////3////wE\n"
1786                 "FByXvvxUvXqLZaz4n4HU1K3FZfpFAxUAEFPN5CwU1pbmdodWFRdTO/P4M0UEKQRK\n"
1787                 "lrVojvVzKEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAAAAAA\n"
1788                 "AAAAAfTI+Seu08p1IlcCAQGhLAMqAATehLqu61gKC3Tgr4wQMVoguAhhG3Uwwz8u\n"
1789                 "ELyhe7yPCAuOoLZlTLgf\n"
1790                 "-----END EC PRIVATE KEY-----\n";
1791
1792         std::string pub =
1793                 "-----BEGIN CERTIFICATE-----\n"
1794                 "MIICfDCCAjqgAwIBAgIJANIytpeTKlXBMAkGByqGSM49BAEwXjELMAkGA1UEBhMC\n"
1795                 "UEwxDTALBgNVBAgMBExvZHoxDTALBgNVBAcMBExvZHoxEDAOBgNVBAoMB1NhbXN1\n"
1796                 "bmcxETAPBgNVBAsMCFNlY3VyaXR5MQwwCgYDVQQDDANDS00wHhcNMTQwNzAyMTI0\n"
1797                 "MTQ3WhcNMTcwNzAxMTI0MTQ3WjBeMQswCQYDVQQGEwJQTDENMAsGA1UECAwETG9k\n"
1798                 "ejENMAsGA1UEBwwETG9kejEQMA4GA1UECgwHU2Ftc3VuZzERMA8GA1UECwwIU2Vj\n"
1799                 "dXJpdHkxDDAKBgNVBAMMA0NLTTCB6jCBuwYHKoZIzj0CATCBrwIBATAgBgcqhkjO\n"
1800                 "PQEBAhUA/////////////////////3////8wQwQU/////////////////////3//\n"
1801                 "//wEFByXvvxUvXqLZaz4n4HU1K3FZfpFAxUAEFPN5CwU1pbmdodWFRdTO/P4M0UE\n"
1802                 "KQRKlrVojvVzKEZkaYlow4u5E8v8giOmKFUxaJR9WdzJEgQjUTd6xfsyAhUBAAAA\n"
1803                 "AAAAAAAAAfTI+Seu08p1IlcCAQEDKgAE3oS6rutYCgt04K+MEDFaILgIYRt1MMM/\n"
1804                 "LhC8oXu8jwgLjqC2ZUy4H6NQME4wHQYDVR0OBBYEFELElWx3kbLo55Cfn1vywsEZ\n"
1805                 "ccsmMB8GA1UdIwQYMBaAFELElWx3kbLo55Cfn1vywsEZccsmMAwGA1UdEwQFMAMB\n"
1806                 "Af8wCQYHKoZIzj0EAQMxADAuAhUAumC4mGoyK97SxTvVBQ+ELfCbxEECFQCbMJ72\n"
1807                 "Q1oBry6NEc+lLFmWMDesAA==\n"
1808                 "-----END CERTIFICATE-----\n";
1809
1810         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
1811
1812         CKM::Alias pri_alias = sharedDatabase("prv4");
1813         CKM::Alias pub_alias = sharedDatabase("pub4");
1814         char *key_passwd = NULL;
1815         char *pri_passwd = NULL;
1816         char *pub_passwd = NULL;
1817         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
1818         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
1819         ckmc_raw_buffer_s *signature;
1820
1821         ckmc_cert_s cert;
1822         cert.raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(pub.c_str()));
1823         cert.cert_size = pub.size();
1824         cert.data_format = CKMC_FORM_PEM;
1825
1826         ckmc_policy_s certpolicy;
1827         certpolicy.password = pub_passwd;
1828         certpolicy.extractable = 1;
1829
1830         ckmc_key_s prikey;
1831         prikey.raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(prv.c_str()));
1832         prikey.key_size = prv.size();
1833         prikey.key_type = CKMC_KEY_NONE;
1834         prikey.password = key_passwd;
1835
1836         ckmc_policy_s pripolicy;
1837         pripolicy.password = pri_passwd;
1838         pripolicy.extractable = 0;
1839
1840
1841         RUNNER_ASSERT_MSG(
1842                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(pub_alias.c_str(), cert, certpolicy)),
1843                         CKMCReadableError(temp));
1844         RUNNER_ASSERT_MSG(
1845                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pri_alias.c_str(), prikey, pripolicy)),
1846                         CKMCReadableError(temp));
1847
1848         RUNNER_ASSERT_MSG(
1849                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
1850                                         pri_alias.c_str(),
1851                                         pri_passwd,
1852                                         msg_buff,
1853                                         hash_algo,
1854                                         pad_algo,
1855                                         &signature)),
1856                         CKMCReadableError(temp));
1857
1858         RUNNER_ASSERT_MSG(
1859                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
1860                                         pub_alias.c_str(),
1861                                         pub_passwd,
1862                                         msg_buff,
1863                                         *signature,
1864                                         hash_algo,
1865                                         pad_algo)),
1866                         CKMCReadableError(temp));
1867
1868         RUNNER_ASSERT_MSG(signature->size > 6, "Signature is too small");
1869         memcpy((void*)signature->data, "BROKEN", 6);
1870
1871         RUNNER_ASSERT_MSG(
1872                         CKMC_ERROR_VERIFICATION_FAILED == (temp = ckmc_verify_signature(
1873                                         pub_alias.c_str(),
1874                                         pub_passwd,
1875                                         msg_buff,
1876                                         *signature,
1877                                         hash_algo,
1878                                         pad_algo)),
1879                         CKMCReadableError(temp));
1880 }
1881
1882 RUNNER_TEST(T3089_CAPI__deinit)
1883 {
1884         remove_user_data(0);
1885 }
1886
1887
1888 //#######################################################################################
1889
1890 void _assertKey(ckmc_key_s *key, unsigned char *raw_key, unsigned int key_size, ckmc_key_type_e key_type, char *password)
1891 {
1892         RUNNER_ASSERT_MSG(key->key_size == key_size, "Key Size Error" );
1893         RUNNER_ASSERT_MSG(key->key_type == key_type, "Key Type Error" );
1894
1895         if(key->password != NULL && password != NULL) {
1896                 RUNNER_ASSERT_MSG(strcmp(key->password, password) == 0, "Password Error" );
1897         }else if(key->password == NULL && password == NULL) {
1898                 RUNNER_ASSERT_MSG(true, "Password Error" );
1899         }else {
1900                 RUNNER_ASSERT_MSG(false, "Password Error" );
1901         }
1902
1903         if(key->raw_key != NULL && raw_key != NULL) {
1904                 for(unsigned int i=0; i<key_size; i++) {
1905                         RUNNER_ASSERT_MSG((key->raw_key)[i] == raw_key[i],  "Raw Key Error" );
1906                 }
1907         }else if(key->raw_key == NULL && raw_key == NULL) {
1908                 RUNNER_ASSERT_MSG(true,  "Raw Key Error" );
1909         }else {
1910                 RUNNER_ASSERT_MSG(false,  "Raw Key Error" );
1911         }
1912 }
1913
1914 RUNNER_TEST_GROUP_INIT(T309_CKMC_CAPI_TYPES);
1915
1916 RUNNER_TEST(T3091_CAPI_TYPE_init)
1917 {
1918         remove_user_data(0);
1919         reset_user_data(USER_APP, USER_PASS);
1920 }
1921
1922 RUNNER_TEST(T3092_CAPI_TYPE_KEY)
1923 {
1924         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
1925                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1926                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1927                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1928                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1929                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1930                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1931                 "zQIDAQAB\n"
1932                 "-----END PUBLIC KEY-----";
1933
1934         unsigned char *raw_key = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(keyPem.c_str()));
1935         unsigned int key_size = keyPem.size();
1936         ckmc_key_type_e key_type = CKMC_KEY_NONE;
1937         char *password = const_cast< char *>("");
1938
1939         ckmc_key_s *key;
1940         ckmc_key_new(raw_key, key_size, key_type, password, &key);
1941
1942         _assertKey(key, raw_key, key_size, key_type, password);
1943         ckmc_key_free(key);
1944
1945         char *passwordNull = NULL;
1946         ckmc_key_s *key2;
1947         ckmc_key_new(raw_key, key_size, key_type, passwordNull, &key2);
1948         ckmc_key_free(key2);
1949 }
1950
1951 RUNNER_TEST(T3093_CAPI_TYPE_BUFFER)
1952 {
1953         std::string keyPem = "-----BEGIN PUBLIC KEY-----\n"
1954                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4\n"
1955                 "T4tUddtZNi0NVjQn9RFH1NMa220GsRhRO56F77FlSVFKfSfVZKIiWg6C+DVCkcLf\n"
1956                 "zXJ/Z0pvwOQYBAqVMFjV6efQGN0JzJ1Unu7pPRiZl7RKGEI+cyzzrcDyrLLrQ2W7\n"
1957                 "0ZySkNEOv6Frx9JgC5NExuYY4lk2fQQa38JXiZkfyzif2em0px7mXbyf5LjccsKq\n"
1958                 "v1e+XLtMsL0ZefRcqsP++NzQAI8fKX7WBT+qK0HJDLiHrKOTWYzx6CwJ66LD/vvf\n"
1959                 "j55xtsKDLVDbsotvf8/m6VLMab+vqKk11TP4tq6yo0mwyTADvgl1zowQEO9I1W6o\n"
1960                 "zQIDAQAB\n"
1961                 "-----END PUBLIC KEY-----";
1962
1963         unsigned char *data = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(keyPem.c_str()));
1964         unsigned int size = keyPem.size();
1965
1966         ckmc_raw_buffer_s *buff;
1967         ckmc_buffer_new(data, size, &buff);
1968
1969         RUNNER_ASSERT_MSG(buff->size == size, "Size Error" );
1970
1971         if(buff->data != NULL && data != NULL) {
1972                 for(unsigned int i=0; i<size; i++) {
1973                         RUNNER_ASSERT_MSG((buff->data)[i] == data[i],  "Raw data Error" );
1974                 }
1975         }else if(buff->data == NULL && data == NULL) {
1976                 RUNNER_ASSERT_MSG(true,  "Raw data Error" );
1977         }else {
1978                 RUNNER_ASSERT_MSG(false,  "Raw data Error" );
1979         }
1980
1981         ckmc_buffer_free(buff);
1982 }
1983
1984 RUNNER_TEST(T3094_CAPI_TYPE_CERT)
1985 {
1986         std::string certPem = TestData::getTestCertificateBase64(TestData::GIAG2);
1987
1988         unsigned char *raw_cert = const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(certPem.c_str()));
1989         unsigned int size = certPem.size();
1990         ckmc_data_format_e form = CKMC_FORM_PEM;
1991
1992         ckmc_cert_s *ckmCert;
1993         ckmc_cert_new(raw_cert, size, form, &ckmCert);
1994
1995         RUNNER_ASSERT_MSG(ckmCert->cert_size == size, "Size Error" );
1996
1997         if(ckmCert->raw_cert != NULL && raw_cert != NULL) {
1998                 for(unsigned int i=0; i<size; i++) {
1999                         RUNNER_ASSERT_MSG((ckmCert->raw_cert)[i] == raw_cert[i],  "Raw data Error" );
2000                 }
2001         }else if(ckmCert->raw_cert == NULL && raw_cert == NULL) {
2002                 RUNNER_ASSERT_MSG(true,  "raw_cert Error" );
2003         }else {
2004                 RUNNER_ASSERT_MSG(false,  "raw_cert Error" );
2005         }
2006
2007         RUNNER_ASSERT_MSG(ckmCert->data_format == form, "ckmc_cert_form Error" );
2008
2009         ckmc_cert_free(ckmCert);
2010 }
2011
2012
2013 RUNNER_TEST(T3095_CAPI_TYPE_load_cert_file)
2014 {
2015         int ret;
2016
2017         std::string certStr = TestData::getTestCertificateBase64(TestData::MBANK);
2018
2019         const char *file_name = "/tmp/ckmc_test_cert.pem";
2020         remove(file_name);
2021
2022         FILE* cert_file;
2023         cert_file = fopen(file_name, "w");
2024         fprintf(cert_file, "%s",certStr.c_str());
2025         fclose(cert_file);
2026
2027         ckmc_cert_s *pcert;
2028
2029         RUNNER_ASSERT_MSG(
2030                         CKMC_ERROR_NONE == (ret = ckmc_load_cert_from_file(file_name, &pcert)),
2031                         CKMCReadableError(ret));
2032
2033         RUNNER_ASSERT_MSG(
2034                         pcert != NULL && pcert->cert_size > 0,"Fail to load cert from file.");
2035
2036         CKM::Alias lcert_alias = sharedDatabase("lcert_alias");
2037         ckmc_policy_s policy;
2038         policy.password = NULL;
2039         policy.extractable = 1;
2040
2041         RUNNER_ASSERT_MSG(
2042                         CKMC_ERROR_NONE == (ret = ckmc_save_cert(lcert_alias.c_str(), *pcert, policy)),
2043                         CKMCReadableError(ret));
2044
2045         remove(file_name);
2046 }
2047
2048 RUNNER_TEST(T3096_CAPI_TYPE_load_p12_file) {
2049         const char *p12file = "/usr/share/ckm-test/capi-t3096.p12";
2050         const char *password = "password";
2051
2052         int temp;
2053
2054         ckmc_key_s *private_key = NULL;
2055         ckmc_cert_s *cert = NULL;
2056         ckmc_cert_list_s *ca_cert_list = NULL;
2057
2058         RUNNER_ASSERT_MSG(
2059                         CKMC_ERROR_NONE == (temp = ckmc_load_from_pkcs12_file(p12file, password,
2060                                         &private_key, &cert, &ca_cert_list)),
2061                         "Error=" << temp);
2062         RUNNER_ASSERT_MSG(private_key != NULL, "Null private_key");
2063         RUNNER_ASSERT_MSG(cert != NULL, "Null cert");
2064         RUNNER_ASSERT_MSG(ca_cert_list != NULL, "Null ca_cert_list");
2065
2066         ckmc_policy_s policy;
2067         policy.password = NULL;
2068         policy.extractable = 1;
2069
2070
2071         CKM::Alias pkey_alias = sharedDatabase("pkey_alias");
2072         RUNNER_ASSERT_MSG(
2073                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pkey_alias.c_str(), *private_key, policy)),
2074                         CKMCReadableError(temp));
2075
2076         CKM::Alias cert_alias = sharedDatabase("cert_alias");
2077         RUNNER_ASSERT_MSG(
2078                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(cert_alias.c_str(), *cert, policy)),
2079                         CKMCReadableError(temp));
2080         std::string caCertAlias = sharedDatabase("ca_cert_alias_");
2081         const char *idx = "0";
2082         int cnt = 0;
2083         ckmc_cert_list_s *tmpList = ca_cert_list;
2084         while(tmpList != NULL) {
2085                 caCertAlias.append(idx);
2086                 RUNNER_ASSERT_MSG(
2087                                 CKMC_ERROR_NONE == (temp = ckmc_save_cert(caCertAlias.c_str(), *(tmpList->cert), policy)),
2088                                 CKMCReadableError(temp));
2089                 tmpList = tmpList->next;
2090                 cnt ++;
2091         }
2092
2093         RUNNER_ASSERT_MSG(cnt ==  2, "Invalid CA Cert Count");
2094
2095         ckmc_key_free(private_key);
2096         ckmc_cert_free(cert);
2097         ckmc_cert_list_all_free(ca_cert_list);
2098 }
2099
2100 RUNNER_TEST(T3097_CAPI_TYPE_load_p12_file2) {
2101         const char *p12file = "/usr/share/ckm-test/capi-t3096.p12";
2102         const char *password = "password";
2103
2104         int temp;
2105
2106         ckmc_pkcs12_s *ppkcs12 = NULL;
2107
2108         RUNNER_ASSERT_MSG(
2109                         CKMC_ERROR_NONE == (temp = ckmc_pkcs12_load(p12file, password, &ppkcs12)),
2110                         CKMCReadableError(temp));
2111         RUNNER_ASSERT_MSG(ppkcs12->priv_key != NULL, "Null private_key");
2112         RUNNER_ASSERT_MSG(ppkcs12->cert != NULL, "Null cert");
2113         RUNNER_ASSERT_MSG(ppkcs12->ca_chain != NULL, "Null ca_cert_list");
2114
2115         ckmc_policy_s policy;
2116         policy.password = NULL;
2117         policy.extractable = 1;
2118
2119
2120         CKM::Alias pkey_alias = sharedDatabase("pkey_alias2");
2121         RUNNER_ASSERT_MSG(
2122                         CKMC_ERROR_NONE == (temp = ckmc_save_key(pkey_alias.c_str(), *(ppkcs12->priv_key), policy)),
2123                         CKMCReadableError(temp));
2124
2125         CKM::Alias cert_alias = sharedDatabase("cert_alias2");
2126         RUNNER_ASSERT_MSG(
2127                         CKMC_ERROR_NONE == (temp = ckmc_save_cert(cert_alias.c_str(), *(ppkcs12->cert), policy)),
2128                         CKMCReadableError(temp));
2129         std::string caCertAlias = sharedDatabase("ca_cert_alias_2_");
2130         const char *idx = "0";
2131         int cnt = 0;
2132         ckmc_cert_list_s *tmpList = ppkcs12->ca_chain;
2133         while(tmpList != NULL) {
2134                 caCertAlias.append(idx);
2135                 RUNNER_ASSERT_MSG(
2136                                 CKMC_ERROR_NONE == (temp = ckmc_save_cert(caCertAlias.c_str(), *(tmpList->cert), policy)),
2137                                 CKMCReadableError(temp));
2138                 tmpList = tmpList->next;
2139                 cnt ++;
2140         }
2141
2142         RUNNER_ASSERT_MSG(cnt ==  2, "Invalid CA Cert Count");
2143
2144         ckmc_pkcs12_free(ppkcs12);
2145 }
2146
2147 RUNNER_TEST(T3098_CAPI_TYPE_deinit)
2148 {
2149         int temp;
2150         remove_user_data(0);
2151         RUNNER_ASSERT_MSG(
2152                         CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
2153                         CKMCReadableError(temp));
2154         RUNNER_ASSERT_MSG(
2155                         CKMC_ERROR_NONE == (temp = ckmc_remove_user_data(USER_APP)),
2156                         CKMCReadableError(temp));
2157 }
2158
2159 RUNNER_TEST_GROUP_INIT(T310_CKMC_CAPI_PKCS12);
2160
2161 namespace
2162 {
2163 CKM::Alias alias_PKCS_exportable = sharedDatabase("CAPI-test-PKCS-export");
2164 CKM::Alias alias_PKCS_not_exportable = sharedDatabase("CAPI-test-PKCS-no-export");
2165 }
2166
2167 RUNNER_TEST(T3101_CAPI_PKCS12_init)
2168 {
2169         remove_user_data(0);
2170 }
2171
2172 RUNNER_TEST(T3102_CAPI_PKCS12_negative_wrong_password)
2173 {
2174         const char *wrong_passwd = "wrong";
2175         ckmc_pkcs12_s *ppkcs12 = NULL;
2176         int temp;
2177         RUNNER_ASSERT_MSG(
2178                         CKMC_ERROR_INVALID_FORMAT == (temp = ckmc_pkcs12_load("/usr/share/ckm-test/test1801.pkcs12", wrong_passwd, &ppkcs12)),
2179                         CKMCReadableError(temp));
2180 }
2181
2182 RUNNER_TEST(T3103_CAPI_PKCS12_add_bundle_with_chain_certs)
2183 {
2184         ckmc_pkcs12_s *ppkcs12 = NULL;
2185         int temp;
2186         RUNNER_ASSERT_MSG(
2187                         CKMC_ERROR_NONE == (temp = ckmc_pkcs12_load("/usr/share/ckm-test/pkcs.p12", NULL, &ppkcs12)),
2188                         CKMCReadableError(temp));
2189
2190         RUNNER_ASSERT_MSG(NULL != ppkcs12->cert, "no certificate in PKCS12");
2191         RUNNER_ASSERT_MSG(NULL != ppkcs12->priv_key, "no private key in PKCS12");
2192         RUNNER_ASSERT_MSG(NULL != ppkcs12->ca_chain, "no chain certificates in PKCS12");
2193
2194         // save to the CKM
2195         ckmc_policy_s exportable;
2196         exportable.password = NULL;
2197         exportable.extractable = 1;
2198         ckmc_policy_s notExportable;
2199         notExportable.password = NULL;
2200         notExportable.extractable = 0;
2201
2202         RUNNER_ASSERT_MSG(
2203                         CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_exportable.c_str(), ppkcs12, exportable, exportable)),
2204                         CKMCReadableError(temp));
2205         RUNNER_ASSERT_MSG(
2206                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_exportable.c_str(), ppkcs12, exportable, exportable)),
2207                         CKMCReadableError(temp));
2208         RUNNER_ASSERT_MSG(
2209                         CKMC_ERROR_NONE == (temp = ckmc_save_pkcs12(alias_PKCS_not_exportable.c_str(), ppkcs12, notExportable, notExportable)),
2210                         CKMCReadableError(temp));
2211         RUNNER_ASSERT_MSG(
2212                         CKMC_ERROR_DB_ALIAS_EXISTS == (temp = ckmc_save_pkcs12(alias_PKCS_not_exportable.c_str(), ppkcs12, notExportable, notExportable)),
2213                         CKMCReadableError(temp));
2214
2215         // try to lookup key
2216         ckmc_key_s *key_lookup = NULL;
2217         RUNNER_ASSERT_MSG(
2218                         CKMC_ERROR_NONE == (temp = ckmc_get_key(alias_PKCS_exportable.c_str(), NULL, &key_lookup)),
2219                         CKMCReadableError(temp));
2220         ckmc_key_free(key_lookup);
2221         key_lookup = NULL;
2222         RUNNER_ASSERT_MSG(
2223                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_key(alias_PKCS_not_exportable.c_str(), "", &key_lookup)),
2224                         CKMCReadableError(temp));
2225         ckmc_key_free(key_lookup);
2226
2227         // try to lookup certificate
2228         ckmc_cert_s *cert_lookup = NULL;
2229         RUNNER_ASSERT_MSG(
2230                         CKMC_ERROR_NONE == (temp = ckmc_get_cert(alias_PKCS_exportable.c_str(), NULL, &cert_lookup)),
2231                         CKMCReadableError(temp));
2232         ckmc_cert_free(cert_lookup);
2233         cert_lookup = NULL;
2234         RUNNER_ASSERT_MSG(
2235                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_cert(alias_PKCS_not_exportable.c_str(), NULL, &cert_lookup)),
2236                         CKMCReadableError(temp));
2237         ckmc_cert_free(cert_lookup);
2238 }
2239
2240 RUNNER_TEST(T3104_CAPI_PKCS12_get_PKCS)
2241 {
2242         int temp;
2243         ckmc_pkcs12_s *pkcs = NULL;
2244
2245         // fail - no entry
2246         RUNNER_ASSERT_MSG(
2247                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_pkcs12(sharedDatabase("i-do-not-exist").c_str(), NULL, NULL, &pkcs)),
2248                         CKMCReadableError(temp));
2249         ckmc_pkcs12_free(pkcs);
2250         pkcs = NULL;
2251
2252         // fail - not exportable
2253         RUNNER_ASSERT_MSG(
2254                         CKMC_ERROR_NOT_EXPORTABLE == (temp = ckmc_get_pkcs12(alias_PKCS_not_exportable.c_str(), NULL, NULL, &pkcs)),
2255                         CKMCReadableError(temp));
2256         ckmc_pkcs12_free(pkcs);
2257         pkcs = NULL;
2258
2259         // success - exportable
2260         RUNNER_ASSERT_MSG(
2261                         CKMC_ERROR_NONE == (temp = ckmc_get_pkcs12(alias_PKCS_exportable.c_str(), NULL, NULL, &pkcs)),
2262                         CKMCReadableError(temp));
2263
2264         RUNNER_ASSERT_MSG(NULL != pkcs->cert, "no certificate in PKCS12");
2265         RUNNER_ASSERT_MSG(NULL != pkcs->priv_key, "no private key in PKCS12");
2266         RUNNER_ASSERT_MSG(NULL != pkcs->ca_chain, "no chain certificates in PKCS12");
2267         size_t cntr = 0;
2268         ckmc_cert_list_s *iter = pkcs->ca_chain;
2269         do {
2270                 cntr ++;
2271                 iter = iter->next;
2272         } while(iter);
2273         RUNNER_ASSERT_MSG(2 == cntr, "invalid number of chain certificates in PKCS12");
2274
2275         ckmc_pkcs12_free(pkcs);
2276 }
2277
2278 RUNNER_TEST(T3105_CAPI_PKCS12_create_and_verify_signature)
2279 {
2280         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
2281
2282         int temp;
2283         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
2284         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
2285         ckmc_raw_buffer_s *signature = NULL;
2286
2287         RUNNER_ASSERT_MSG(
2288                         CKMC_ERROR_NONE == (temp = ckmc_create_signature(
2289                                         alias_PKCS_exportable.c_str(),
2290                                         NULL,
2291                                         msg_buff,
2292                                         hash_algo,
2293                                         pad_algo,
2294                                         &signature)),
2295                         CKMCReadableError(temp));
2296
2297         RUNNER_ASSERT_MSG(
2298                         CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
2299                                         alias_PKCS_exportable.c_str(),
2300                                         NULL,
2301                                         msg_buff,
2302                                         *signature,
2303                                         hash_algo,
2304                                         pad_algo)),
2305                         CKMCReadableError(temp));
2306 }
2307
2308 RUNNER_TEST(T3106_CAPI_PKCS12_remove_bundle_with_chain_certs)
2309 {
2310         int tmp;
2311
2312         // remove the whole PKCS12 bundles
2313         RUNNER_ASSERT_MSG(
2314                         CKMC_ERROR_NONE == (tmp = ckmc_remove_alias(alias_PKCS_exportable.c_str())),
2315                         CKMCReadableError(tmp));
2316         RUNNER_ASSERT_MSG(
2317                         CKMC_ERROR_NONE == (tmp = ckmc_remove_alias(alias_PKCS_not_exportable.c_str())),
2318                         CKMCReadableError(tmp));
2319
2320         // expect lookup fails due to unknown alias
2321         // try to lookup key
2322         ckmc_key_s *key_lookup = NULL;
2323         RUNNER_ASSERT_MSG(
2324                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_exportable.c_str(), NULL, &key_lookup)),
2325                         CKMCReadableError(tmp));
2326         ckmc_key_free(key_lookup);
2327         key_lookup = NULL;
2328         RUNNER_ASSERT_MSG(
2329                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_key(alias_PKCS_not_exportable.c_str(), NULL, &key_lookup)),
2330                         CKMCReadableError(tmp));
2331         ckmc_key_free(key_lookup);
2332
2333         // try to lookup certificate
2334         ckmc_cert_s *cert_lookup = NULL;
2335         RUNNER_ASSERT_MSG(
2336                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_exportable.c_str(), NULL, &cert_lookup)),
2337                         CKMCReadableError(tmp));
2338         ckmc_cert_free(cert_lookup);
2339         cert_lookup = NULL;
2340         RUNNER_ASSERT_MSG(
2341                         CKMC_ERROR_DB_ALIAS_UNKNOWN == (tmp = ckmc_get_cert(alias_PKCS_not_exportable.c_str(), NULL, &cert_lookup)),
2342                         CKMCReadableError(tmp));
2343         ckmc_cert_free(cert_lookup);
2344 }
2345
2346 RUNNER_TEST(T3109_CAPI_PKCS12_deinit)
2347 {
2348         remove_user_data(0);
2349 }
2350
2351
2352 RUNNER_TEST_GROUP_INIT(T320_CAPI_EMPTY_DATABASE);
2353
2354 RUNNER_TEST(T3201_CAPI_unlock_database)
2355 {
2356         reset_user_data(USER_APP, USER_PASS);
2357 }
2358
2359 RUNNER_CHILD_TEST(T3202_CAPI_get_data_from_empty_database)
2360 {
2361         ScopedDBUnlock unlock(USER_APP, USER_PASS);
2362         ScopedAccessProvider ap(TEST_LABEL);
2363         ap.allowAPI("key-manager::api-storage", "rw");
2364         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
2365
2366         int temp;
2367         CKM::Alias alias = "mykey";
2368         char *password = NULL;
2369         ckmc_key_s *test_key = NULL;
2370
2371         RUNNER_ASSERT_MSG(
2372                 CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_key(alias.c_str(), password, &test_key)),
2373                 "Error=" << temp);
2374
2375         RUNNER_ASSERT_MSG(NULL == test_key, "Key value should not be changed");
2376 }
2377
2378 RUNNER_CHILD_TEST(T3203_CAPI_lock_database)
2379 {
2380         ScopedDBUnlock unlock(USER_APP, USER_PASS);
2381         ScopedAccessProvider ap(TEST_LABEL);
2382         ap.allowAPI("key-manager::api-storage", "rw");
2383         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
2384
2385         int temp;
2386         RUNNER_ASSERT_MSG( CKMC_ERROR_NONE == (temp = ckmc_lock_user_key(USER_APP)),
2387                 CKMCReadableError(temp));
2388 }
2389
2390 RUNNER_CHILD_TEST(T3204_CAPI_get_data_from_locked_database)
2391 {
2392         ScopedAccessProvider ap(TEST_LABEL);
2393         ap.allowAPI("key-manager::api-storage", "rw");
2394         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
2395
2396         int temp;
2397         CKM::Alias alias = "mykey";
2398         char *password = NULL;
2399         ckmc_key_s *test_key = NULL;
2400
2401 #ifndef PASSWORD_PROTECTION_DISABLE
2402         RUNNER_ASSERT_MSG(
2403                 CKMC_ERROR_DB_LOCKED == (temp = ckmc_get_key(alias.c_str(), password, &test_key)),
2404                 CKMCReadableError(temp));
2405
2406         RUNNER_ASSERT_MSG(NULL == test_key, "Key value should not be changed");
2407 #else
2408         RUNNER_ASSERT_MSG(
2409                 CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_get_key(alias.c_str(), password, &test_key)),
2410                 CKMCReadableError(temp));
2411 #endif
2412 }
2413
2414 RUNNER_TEST(T3204_deinit)
2415 {
2416         remove_user_data(USER_APP);
2417 }
2418