system-db proper clearing after test run.
[platform/core/test/security-tests.git] / src / ckm / system-db.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  *
16  * @file       system-db.cpp
17  * @author     Maciej Karpiuk (m.karpiuk2@samsung.com)
18  * @version    1.0
19  */
20 #include <dpl/test/test_runner.h>
21 #include <dpl/test/test_runner_child.h>
22 #include <dpl/log/log.h>
23 #include <tests_common.h>
24 #include <ckm-common.h>
25 #include <ckm/ckm-control.h>
26 #include <ckmc/ckmc-manager.h>
27 #include <ckmc/ckmc-type.h>
28 #include <access_provider2.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31
32 namespace
33 {
34 const uid_t USER_SERVICE        = 0;
35 const uid_t USER_SERVICE_2      = 1234;
36 const uid_t GROUP_SERVICE_2     = 1234;
37 const uid_t USER_SERVICE_MAX    = 4999;
38 const uid_t GROUP_SERVICE_MAX   = 4999;
39 const uid_t USER_SERVICE_FAIL   = 5000;
40 const uid_t GROUP_SERVICE_FAIL  = 5000;
41 const uid_t USER_APP            = 5050;
42 const uid_t GROUP_APP           = 5050;
43 const char* APP_PASS            = "user-pass";
44
45 const char* TEST_ALIAS          = "test-alias";
46 const char* SYSTEM_LABEL        = "/";
47 const char* TEST_SYSTEM_ALIAS   = "/ test-alias";
48 const char* TEST_SYSTEM_ALIAS_2 = "/ test-alias-2";
49 const char* TEST_LABEL          = "test-label";
50 const char* TEST_LABEL_2        = "test-label-2";
51
52 const char* TEST_DATA =
53         "Lorem Ipsum. At vero eos et accusamus et iusto odio dignissimos ducimus "
54         "qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores "
55         "et quas molestias excepturi sint occaecati cupiditate non provident, "
56         "similique sunt in culpa qui officia deserunt mollitia animi, id est "
57         "laborum et dolorum fuga. ";
58 }
59
60
61 RUNNER_TEST_GROUP_INIT(T50_SYSTEM_DB);
62
63 RUNNER_TEST(T5010_CLIENT_APP_LOCKED_PRIVATE_DB)
64 {
65     // [prepare]
66     // start as system service
67     // add resource to the system DB
68     // add permission to the resource to a user app
69     // [test]
70     // switch to user app, leave DB locked
71     // try to access system DB item - expect success
72
73     // [prepare]
74     GarbageCollector gc;
75     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
76     allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
77
78     // [test]
79     {
80         ScopedAccessProvider ap(TEST_LABEL);
81         ap.allowAPI("key-manager::api-storage", "rw");
82         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
83
84         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
85     }
86 }
87
88 RUNNER_TEST(T5020_CLIENT_APP_ADD_TO_PRIVATE_DB)
89 {
90     // [test]
91     // switch to user app, unlock DB
92     // when accessing private DB - owner==me
93     // try to write to private DB - expect success
94     // try to get item from private DB - expect success
95
96     // [test]
97     {
98         ScopedAccessProvider ap(TEST_LABEL);
99         ap.allowAPI("key-manager::api-storage", "rw");
100         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
101         ScopedDBUnlock unlock(USER_APP, APP_PASS);
102
103         ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
104         check_read(TEST_ALIAS, TEST_LABEL, TEST_DATA);
105     }
106 }
107
108 RUNNER_TEST(T5031_CLIENT_APP_ACCESS_WITH_PERMISSION)
109 {
110     // [prepare]
111     // start as system service
112     // add resource to the system DB
113     // add permission to the resource to a user app
114     // [test]
115     // switch to user app, unlock DB
116     // try to access the system item - expect success
117
118     // [prepare]
119     GarbageCollector gc;
120     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
121     allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
122
123     // [test]
124     {
125         ScopedAccessProvider ap(TEST_LABEL);
126         ap.allowAPI("key-manager::api-storage", "rw");
127         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
128         ScopedDBUnlock unlock(USER_APP, APP_PASS);
129
130         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
131     }
132 }
133
134 RUNNER_TEST(T5032_CLIENT_APP_ACCESS_NO_PERMISSION)
135 {
136     // [prepare]
137     // start as system service
138     // add resource to the system DB
139     // [test]
140     // switch to user app, unlock DB
141     // try to access the system item - expect fail
142
143     // [prepare]
144     GarbageCollector gc;
145     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
146
147     // [test]
148     {
149         ScopedAccessProvider ap(TEST_LABEL);
150         ap.allowAPI("key-manager::api-storage", "rw");
151         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
152         ScopedDBUnlock unlock(USER_APP, APP_PASS);
153
154         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
155     }
156 }
157
158 RUNNER_TEST(T5033_CLIENT_APP_PERMISSION_REMOVAL)
159 {
160     // [prepare]
161     // start as system service
162     // add resource to the system DB
163     // add permission to the resource to a user app
164     // [test]
165     // switch to user app, unlock DB
166     // try to access the system item - expect success
167     // [prepare2]
168     // as system service, remove the item (expecting to remove permission)
169     // add item again, do not add permission
170     // [test2]
171     // switch to user app, unlock DB
172     // try to access the system item - expect fail
173
174     // [prepare]
175     GarbageCollector gc;
176     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
177     allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
178
179     // [test]
180     {
181         ScopedAccessProvider ap(TEST_LABEL);
182         ap.allowAPI("key-manager::api-storage", "rw");
183         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
184         ScopedDBUnlock unlock(USER_APP, APP_PASS);
185
186         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
187     }
188
189     // [prepare2]
190     check_remove_allowed(TEST_SYSTEM_ALIAS);
191
192     // [test2]
193     {
194         ScopedAccessProvider ap(TEST_LABEL);
195         ap.allowAPI("key-manager::api-storage", "rw");
196         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
197         ScopedDBUnlock unlock(USER_APP, APP_PASS);
198
199         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
200     }
201 }
202
203 RUNNER_TEST(T5034_CLIENT_APP_SET_READ_ACCESS)
204 {
205     // [test]
206     // switch to user app, unlock DB
207     // try to write to private DB - expect success
208     // try to write to system DB  - expect fail
209
210     // [test]
211     {
212         ScopedAccessProvider ap(TEST_LABEL);
213         ap.allowAPI("key-manager::api-storage", "rw");
214         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
215         ScopedDBUnlock unlock(USER_APP, APP_PASS);
216
217         ScopedSaveData ssdsystem_user(TEST_ALIAS, TEST_DATA);
218         ScopedSaveData ssdsystem_system(TEST_SYSTEM_ALIAS, TEST_DATA, CKMC_ERROR_PERMISSION_DENIED);
219         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
220     }
221 }
222
223 RUNNER_TEST(T5035_CLIENT_APP_TRY_REMOVING_SYSTEM_ITEM)
224 {
225     // [prepare]
226     // start as system service
227     // add resource to the system DB
228     // add permission to the resource to a user app
229     // [test]
230     // switch to user app, unlock DB
231     // try to remove item from system DB  - expect fail
232
233     // [prepare]
234     GarbageCollector gc;
235     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
236     allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
237
238     // [test]
239     {
240         ScopedAccessProvider ap(TEST_LABEL);
241         ap.allowAPI("key-manager::api-storage", "rw");
242         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
243         ScopedDBUnlock unlock(USER_APP, APP_PASS);
244
245         check_remove_denied(TEST_SYSTEM_ALIAS);
246     }
247 }
248
249 RUNNER_TEST(T5036_CLIENT_LIST_ACCESSIBLE_ITEMS)
250 {
251     // [prepare]
252     // start as system service
253     // add data A to the system DB
254     // add data B to the system DB
255     // add permission to data A to a user app
256     // [test]
257     // system service list items - expect both items to appear
258     // [test2]
259     // switch to user app, unlock DB
260     // add data as user
261     // user lists items - expect system item A and private item
262
263     // [prepare]
264     GarbageCollector gc;
265     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
266     gc.save(TEST_SYSTEM_ALIAS_2, TEST_DATA);
267     allow_access(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_READ);
268
269     // [test]
270     check_alias_list({TEST_SYSTEM_ALIAS, TEST_SYSTEM_ALIAS_2});
271
272     // [test2]
273     {
274         ScopedAccessProvider ap(TEST_LABEL);
275         ap.allowAPI("key-manager::api-storage", "rw");
276         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
277         ScopedDBUnlock unlock(USER_APP, APP_PASS);
278         ScopedSaveData user_data(TEST_ALIAS, TEST_DATA);
279
280         check_alias_list({TEST_SYSTEM_ALIAS,
281                           aliasWithLabel(TEST_LABEL, TEST_ALIAS)});
282     }
283 }
284
285 RUNNER_TEST(T5037_CLIENT_APP_TRY_GENERATE_KEY_IN_SYSTEM_DB)
286 {
287     // [test]
288     // switch to user app, unlock DB
289     // try to generate a key in system DB  - expect fail
290
291     // [test]
292     {
293         ScopedAccessProvider ap(TEST_LABEL);
294         ap.allowAPI("key-manager::api-storage", "rw");
295         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
296         ScopedDBUnlock unlock(USER_APP, APP_PASS);
297
298         const char *private_key_alias = "/ sys-db-priv";
299         const char *public_key_alias = "/ sys-db-pub";
300         ckmc_policy_s policy_private_key;
301         ckmc_policy_s policy_public_key;
302         policy_private_key.password = NULL;
303         policy_private_key.extractable = 1;
304         policy_public_key.password = NULL;
305         policy_public_key.extractable = 1;
306         int temp;
307         RUNNER_ASSERT_MSG(
308                  CKMC_ERROR_PERMISSION_DENIED ==
309                         (temp = ckmc_create_key_pair_rsa(1024,
310                                                          private_key_alias,
311                                                          public_key_alias,
312                                                          policy_private_key,
313                                                          policy_public_key)),
314                  CKMCReadableError(temp));
315     }
316 }
317
318 RUNNER_TEST(T5038_CLIENT_SERVER_CREATE_VERIFY_SYSTEM_DB)
319 {
320     // [prepare]
321     // start as system service
322     // generate RSA key in system DB
323     // [test]
324     // try to create and verify signature in system DB  - expect success
325     // [test2]
326     // switch to user app, unlock DB
327     // try to create signature in system DB  - expect fail
328
329     // [prepare]
330     GarbageCollector gc;
331     const char *private_key_alias = "/ sys-db-priv";
332     const char *public_key_alias = "/ sys-db-pub";
333     gc.add(private_key_alias);
334     gc.add(public_key_alias);
335     ckmc_policy_s policy_private_key;
336     ckmc_policy_s policy_public_key;
337     policy_private_key.password = NULL;
338     policy_private_key.extractable = 1;
339     policy_public_key.password = NULL;
340     policy_public_key.extractable = 1;
341     int temp;
342     RUNNER_ASSERT_MSG(
343             CKMC_ERROR_NONE ==
344                     (temp = ckmc_create_key_pair_rsa(1024,
345                                                      private_key_alias,
346                                                      public_key_alias,
347                                                      policy_private_key,
348                                                      policy_public_key)),
349              CKMCReadableError(temp));
350
351     // [test]
352     {
353         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
354         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
355         ckmc_raw_buffer_s *signature;
356         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
357
358         RUNNER_ASSERT_MSG(
359                 CKMC_ERROR_NONE == (temp = ckmc_create_signature(
360                         private_key_alias,
361                         NULL,
362                         msg_buff,
363                         hash_algo,
364                         pad_algo,
365                         &signature)),
366                 CKMCReadableError(temp));
367
368         RUNNER_ASSERT_MSG(
369                 CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
370                         public_key_alias,
371                         NULL,
372                         msg_buff,
373                         *signature,
374                         hash_algo,
375                         pad_algo)),
376                 CKMCReadableError(temp));
377     }
378
379     // [test2]
380     {
381         ScopedAccessProvider ap(TEST_LABEL);
382         ap.allowAPI("key-manager::api-storage", "rw");
383         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
384         ScopedDBUnlock unlock(USER_APP, APP_PASS);
385
386         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
387         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
388         ckmc_raw_buffer_s *signature;
389         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
390
391         RUNNER_ASSERT_MSG(
392                 CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_create_signature(
393                         private_key_alias,
394                         NULL,
395                         msg_buff,
396                         hash_algo,
397                         pad_algo,
398                         &signature)),
399                 CKMCReadableError(temp));
400     }
401 }
402
403 RUNNER_TEST(T5039_SYSTEM_APP_SET_REMOVE_ACCESS)
404 {
405     // [prepare]
406     // start as system service
407     // add resource to the system DB
408     // [test]
409     // add remove permission to a user app - expect fail
410
411     // [prepare]
412     GarbageCollector gc;
413     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
414
415     // [test]
416     allow_access_negative(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_REMOVE, CKMC_ERROR_INVALID_PARAMETER);
417 }
418
419 RUNNER_TEST(T5040_SYSTEM_SVC_ACCESS_DB)
420 {
421     // [prepare]
422     // start as system service
423     // add resource to the system DB
424     // [test]
425     // try to access the item - expect success
426
427     // [prepare]
428     GarbageCollector gc;
429     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
430
431     // [test]
432     check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
433 }
434
435 RUNNER_TEST(T5041_SYSTEM_SVC_1234_ACCESS_DB)
436 {
437     // [prepare]
438     // start as system service
439     // add resource to the system DB
440     // [test]
441     // switch to another system service
442     // try to access the item - expect success
443
444     // [prepare]
445     GarbageCollector gc;
446     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
447
448     // [test]
449     {
450         ScopedAccessProvider ap(TEST_LABEL_2);
451         ap.allowAPI("key-manager::api-storage", "rw");
452         ap.applyAndSwithToUser(USER_SERVICE_2, GROUP_SERVICE_2);
453
454         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
455     }
456 }
457
458 RUNNER_TEST(T5042_SYSTEM_SVC_1234_ADD_ITEM_TO_DB)
459 {
460     // [prepare]
461     // start as system service 1234
462     // add resource to the system DB
463     // [test]
464     // switch to another system service
465     // try to access the item - expect success
466
467     // [prepare]
468     {
469         ScopedAccessProvider ap(TEST_LABEL_2);
470         ap.allowAPI("key-manager::api-storage", "rw");
471         ap.applyAndSwithToUser(USER_SERVICE_2, GROUP_SERVICE_2);
472
473         // [test]
474         ScopedSaveData ssd(TEST_SYSTEM_ALIAS, TEST_DATA);
475         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
476     }
477 }
478
479 RUNNER_TEST(T5043_SYSTEM_SVC_4999_ACCESS_DB)
480 {
481     // [prepare]
482     // start as system service
483     // add resource to the system DB
484     // [test]
485     // switch to system service having uid maximum for system svcs
486     // try to access the item - expect success
487
488     // [prepare]
489     GarbageCollector gc;
490     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
491
492     // [test]
493     {
494         ScopedAccessProvider ap(TEST_LABEL_2);
495         ap.allowAPI("key-manager::api-storage", "rw");
496         ap.applyAndSwithToUser(USER_SERVICE_MAX, GROUP_SERVICE_MAX);
497
498         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
499     }
500 }
501
502 RUNNER_TEST(T5044_SYSTEM_SVC_5000_ACCESS_DB)
503 {
504     // [prepare]
505     // start as system service
506     // add resource to the system DB
507     // [test]
508     // switch to another, faulty system service with user-land uid==5000
509     // try to access the item - expect fail (no system service)
510
511     // [prepare]
512     GarbageCollector gc;
513     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
514
515     // [test]
516     {
517         ScopedAccessProvider ap(TEST_LABEL_2);
518         ap.allowAPI("key-manager::api-storage", "rw");
519         ap.applyAndSwithToUser(USER_SERVICE_FAIL, GROUP_SERVICE_FAIL);
520
521         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
522     }
523 }