CKM: add tests for system-db.
[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 fail
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     const char *private_key_alias = "/ sys-db-priv";
331     const char *public_key_alias = "/ sys-db-pub";
332     ckmc_policy_s policy_private_key;
333     ckmc_policy_s policy_public_key;
334     policy_private_key.password = NULL;
335     policy_private_key.extractable = 1;
336     policy_public_key.password = NULL;
337     policy_public_key.extractable = 1;
338     int temp;
339     RUNNER_ASSERT_MSG(
340             CKMC_ERROR_NONE ==
341                     (temp = ckmc_create_key_pair_rsa(1024,
342                                                      private_key_alias,
343                                                      public_key_alias,
344                                                      policy_private_key,
345                                                      policy_public_key)),
346              CKMCReadableError(temp));
347
348     // [test]
349     {
350         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
351         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
352         ckmc_raw_buffer_s *signature;
353         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
354
355         RUNNER_ASSERT_MSG(
356                 CKMC_ERROR_NONE == (temp = ckmc_create_signature(
357                         private_key_alias,
358                         NULL,
359                         msg_buff,
360                         hash_algo,
361                         pad_algo,
362                         &signature)),
363                 CKMCReadableError(temp));
364
365         RUNNER_ASSERT_MSG(
366                 CKMC_ERROR_NONE == (temp = ckmc_verify_signature(
367                         public_key_alias,
368                         NULL,
369                         msg_buff,
370                         *signature,
371                         hash_algo,
372                         pad_algo)),
373                 CKMCReadableError(temp));
374     }
375
376     // [test2]
377     {
378         ScopedAccessProvider ap(TEST_LABEL);
379         ap.allowAPI("key-manager::api-storage", "rw");
380         ap.applyAndSwithToUser(USER_APP, GROUP_APP);
381         ScopedDBUnlock unlock(USER_APP, APP_PASS);
382
383         ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
384         ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
385         ckmc_raw_buffer_s *signature;
386         ckmc_raw_buffer_s msg_buff = prepare_message_buffer("message test");
387
388         RUNNER_ASSERT_MSG(
389                 CKMC_ERROR_DB_ALIAS_UNKNOWN == (temp = ckmc_create_signature(
390                         private_key_alias,
391                         NULL,
392                         msg_buff,
393                         hash_algo,
394                         pad_algo,
395                         &signature)),
396                 CKMCReadableError(temp));
397     }
398 }
399
400 RUNNER_TEST(T5039_SYSTEM_APP_SET_REMOVE_ACCESS)
401 {
402     // [prepare]
403     // start as system service
404     // add resource to the system DB
405     // [test]
406     // add remove permission to a user app - expect fail
407
408     // [prepare]
409     GarbageCollector gc;
410     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
411
412     // [test]
413     allow_access_negative(TEST_SYSTEM_ALIAS, TEST_LABEL, CKMC_PERMISSION_REMOVE, CKMC_ERROR_INVALID_PARAMETER);
414 }
415
416 RUNNER_TEST(T5040_SYSTEM_SVC_ACCESS_DB)
417 {
418     // [prepare]
419     // start as system service
420     // add resource to the system DB
421     // [test]
422     // try to access the item - expect success
423
424     // [prepare]
425     GarbageCollector gc;
426     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
427
428     // [test]
429     check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
430 }
431
432 RUNNER_TEST(T5041_SYSTEM_SVC_1234_ACCESS_DB)
433 {
434     // [prepare]
435     // start as system service
436     // add resource to the system DB
437     // [test]
438     // switch to another system service
439     // try to access the item - expect success
440
441     // [prepare]
442     GarbageCollector gc;
443     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
444
445     // [test]
446     {
447         ScopedAccessProvider ap(TEST_LABEL_2);
448         ap.allowAPI("key-manager::api-storage", "rw");
449         ap.applyAndSwithToUser(USER_SERVICE_2, GROUP_SERVICE_2);
450
451         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
452     }
453 }
454
455 RUNNER_TEST(T5042_SYSTEM_SVC_1234_ADD_ITEM_TO_DB)
456 {
457     // [prepare]
458     // start as system service 1234
459     // add resource to the system DB
460     // [test]
461     // switch to another system service
462     // try to access the item - expect success
463
464     // [prepare]
465     {
466         ScopedAccessProvider ap(TEST_LABEL_2);
467         ap.allowAPI("key-manager::api-storage", "rw");
468         ap.applyAndSwithToUser(USER_SERVICE_2, GROUP_SERVICE_2);
469
470         // [test]
471         ScopedSaveData ssd(TEST_SYSTEM_ALIAS, TEST_DATA);
472         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
473     }
474 }
475
476 RUNNER_TEST(T5043_SYSTEM_SVC_4999_ACCESS_DB)
477 {
478     // [prepare]
479     // start as system service
480     // add resource to the system DB
481     // [test]
482     // switch to system service having uid maximum for system svcs
483     // try to access the item - expect success
484
485     // [prepare]
486     GarbageCollector gc;
487     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
488
489     // [test]
490     {
491         ScopedAccessProvider ap(TEST_LABEL_2);
492         ap.allowAPI("key-manager::api-storage", "rw");
493         ap.applyAndSwithToUser(USER_SERVICE_MAX, GROUP_SERVICE_MAX);
494
495         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
496     }
497 }
498
499 RUNNER_TEST(T5044_SYSTEM_SVC_5000_ACCESS_DB)
500 {
501     // [prepare]
502     // start as system service
503     // add resource to the system DB
504     // [test]
505     // switch to another, faulty system service with user-land uid==5000
506     // try to access the item - expect fail (no system service)
507
508     // [prepare]
509     GarbageCollector gc;
510     gc.save(TEST_SYSTEM_ALIAS, TEST_DATA);
511
512     // [test]
513     {
514         ScopedAccessProvider ap(TEST_LABEL_2);
515         ap.allowAPI("key-manager::api-storage", "rw");
516         ap.applyAndSwithToUser(USER_SERVICE_FAIL, GROUP_SERVICE_FAIL);
517
518         check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
519     }
520 }