f750ebe905f924fa045f07ed52cad1d00511b369
[platform/core/test/security-tests.git] / src / ckm / privileged / main.cpp
1 /*
2  *  Copyright (c) 2016 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <unistd.h>
17 #include <sys/types.h>
18
19 #include <fstream>
20 #include <iostream>
21
22 #include <netdb.h>
23
24 #include <dpl/test/test_runner.h>
25 #include <dpl/test/test_runner_child.h>
26
27 #include <tests_common.h>
28 #include <test-certs.h>
29 #include <access_provider2.h>
30 #include <ckm-common.h>
31 #include <ckm-privileged-common.h>
32
33 #include <ckm/ckm-manager.h>
34 #include <ckm/ckm-control.h>
35 #include <ckm/ckm-password.h>
36 #include <ckm/ckm-type.h>
37 #include <ckm/ckm-pkcs12.h>
38
39 #include <openssl/x509.h>
40 #include <openssl/x509v3.h>
41
42 namespace {
43 const int USER_APP = 5000;
44 const int GROUP_APP = 5000;
45
46 const int USER_APP_2 = 5020;
47 const int USER_APP_3 = 5030;
48
49 const char * const APP_PASS  = "user-pass";
50 const int USER_TEST = 5001;
51
52 const CKM::CertificateShPtrVector EMPTY_CERT_VECTOR;
53 const CKM::AliasVector EMPTY_ALIAS_VECTOR;
54 } // namespace anonymous
55
56 /*
57  *  How to numerate tests:
58  *  TABCD_NAME
59  *  T - test case (always T)
60  *  AB - number of test group (always two digits)
61  *  C - test number in group (all tests with same TABC must be run in the same time).
62  *  D - subtest.
63  */
64
65 RUNNER_TEST_GROUP_INIT(T151_CKM_STORAGE_PERNAMENT_TESTS);
66
67 RUNNER_TEST(T1510_init_unlock_key)
68 {
69     reset_user_data(USER_TEST, APP_PASS);
70 }
71
72 RUNNER_TEST(T1511_insert_data)
73 {
74     auto certee = TestData::getTestCertificate(TestData::OCSP_AVAILABLE_LEAF);
75     auto certim = TestData::getTestCertificate(TestData::OCSP_AVAILABLE_IM);
76     CKM::Alias certeeAlias("CertEE");
77     CKM::Alias certimAlias("CertIM");
78     {
79         ScopedDBUnlock unlock(USER_TEST, APP_PASS);
80         ScopedAccessProvider ap(TEST_LABEL);
81         ap.allowAPI("key-manager::api-storage", "rw");
82         ap.applyAndSwithToUser(USER_TEST, GROUP_APP);
83
84         auto manager = CKM::Manager::create();
85         RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certeeAlias, certee, CKM::Policy()));
86         RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certimAlias, certim, CKM::Policy()));
87     }
88
89     // restart CKM
90     stop_service(MANAGER);
91     start_service(MANAGER);
92
93     // actual test
94     {
95         ScopedDBUnlock unlock(USER_TEST, APP_PASS);
96         ScopedAccessProvider ap(TEST_LABEL);
97         ap.allowAPI("key-manager::api-storage", "rw");
98         ap.applyAndSwithToUser(USER_TEST, GROUP_APP);
99
100         auto manager = CKM::Manager::create();
101         int status1 = manager->saveCertificate(certeeAlias, certee, CKM::Policy());
102         int status2 = manager->saveCertificate(certimAlias, certim, CKM::Policy());
103         RUNNER_ASSERT_MSG(
104             CKM_API_ERROR_DB_ALIAS_EXISTS == status1,
105             "Certificate should be in database already. Error=" << CKM::APICodeToString(status1));
106         RUNNER_ASSERT_MSG(
107             CKM_API_ERROR_DB_ALIAS_EXISTS == status2,
108             "Certificate should be in database already. Error=" << CKM::APICodeToString(status2));
109     }
110 }
111
112 RUNNER_TEST(T1519_deinit)
113 {
114     remove_user_data(USER_TEST);
115 }
116
117 RUNNER_TEST_GROUP_INIT(T170_CKM_STORAGE_PERNAMENT_TESTS);
118
119 RUNNER_TEST(T1701_init_unlock_key)
120 {
121     unlock_user_data(USER_TEST+1, "t170-special-password");
122
123     ScopedAccessProvider ap(TEST_LABEL);
124     ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
125 }
126
127 RUNNER_CHILD_TEST(T1702_insert_data)
128 {
129     int temp;
130     ScopedAccessProvider ap(TEST_LABEL);
131     ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
132
133     auto certee = TestData::getTestCertificate(TestData::THIRD_PARTY_LEAF);
134
135     auto manager = CKM::Manager::create();
136     size_t current_aliases_num = count_aliases(ALIAS_CERT);
137     int status1 = manager->saveCertificate(CKM::Alias("CertEEE"), certee, CKM::Policy());
138
139     RUNNER_ASSERT_MSG(
140         CKM_API_SUCCESS == status1,
141         "Could not put certificate in datbase. Error=" << CKM::APICodeToString(status1));
142
143     CKM::AliasVector av;
144     RUNNER_ASSERT_MSG(
145         CKM_API_SUCCESS == (temp = manager->getCertificateAliasVector(av)),
146         "Error=" << CKM::APICodeToString(temp));
147     RUNNER_ASSERT_MSG(
148         (current_aliases_num+1) == static_cast<size_t>(temp = av.size()),
149         "Vector size: " << temp << ". Expected: " << (current_aliases_num+1));
150 }
151
152 RUNNER_TEST(T1703_removeApplicationData)
153 {
154     int tmp;
155     auto control = CKM::Control::create();
156     RUNNER_ASSERT_MSG(
157         CKM_API_SUCCESS == (tmp = control->removeApplicationData(TEST_LABEL)),
158         "Error=" << CKM::APICodeToString(tmp));
159 }
160
161 RUNNER_CHILD_TEST(T1704_data_test)
162 {
163     int temp;
164     ScopedAccessProvider ap(TEST_LABEL);
165     ap.applyAndSwithToUser(USER_TEST+1, GROUP_APP);
166
167     CKM::AliasVector av;
168     auto manager = CKM::Manager::create();
169
170     RUNNER_ASSERT_MSG(
171         CKM_API_SUCCESS == (temp = manager->getCertificateAliasVector(av)),
172         "Error=" << CKM::APICodeToString(temp));
173     RUNNER_ASSERT_MSG(
174         0 == (temp = av.size()),
175         "Vector size: " << temp << ". Expected: 0");
176 }
177
178 RUNNER_TEST(T1705_deinit)
179 {
180     remove_user_data(USER_TEST+1);
181 }
182
183 RUNNER_TEST(T17101_init)
184 {
185     int tmp;
186
187     auto control = CKM::Control::create();
188     RUNNER_ASSERT_MSG(
189         CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+2)),
190         "Error=" << CKM::APICodeToString(tmp));
191     RUNNER_ASSERT_MSG(
192         CKM_API_SUCCESS == (tmp = control->removeUserData(USER_TEST+2)),
193         "Error=" << CKM::APICodeToString(tmp));
194     RUNNER_ASSERT_MSG(
195         CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+2, "t1706-special-password")),
196         "Error=" << CKM::APICodeToString(tmp));
197
198     RUNNER_ASSERT_MSG(
199         CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+3)),
200         "Error=" << CKM::APICodeToString(tmp));
201     RUNNER_ASSERT_MSG(
202         CKM_API_SUCCESS == (tmp = control->removeUserData(USER_TEST+3)),
203         "Error=" << CKM::APICodeToString(tmp));
204     RUNNER_ASSERT_MSG(
205          CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+3, "t1706-special-password")),
206          "Error=" << CKM::APICodeToString(tmp));
207 }
208
209 RUNNER_CHILD_TEST(T17102_prep_data_01)
210 {
211     int temp;
212     ScopedAccessProvider ap(TEST_LABEL);
213     ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
214
215     CKM::AliasVector av;
216     auto manager = CKM::Manager::create();
217
218     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
219
220     CKM::RawBuffer buffer(data.begin(), data.end());
221     CKM::Policy exportable(CKM::Password(), true);
222
223     RUNNER_ASSERT_MSG(
224         CKM_API_SUCCESS == (temp = manager->saveData("data1", buffer, exportable)),
225         "Error=" << CKM::APICodeToString(temp));
226 }
227
228 RUNNER_CHILD_TEST(T17103_prep_data_02)
229 {
230     int temp;
231     ScopedAccessProvider ap(TEST_LABEL_2);
232     ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
233
234     CKM::AliasVector av;
235     auto manager = CKM::Manager::create();
236
237     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
238
239     CKM::RawBuffer buffer(data.begin(), data.end());
240     CKM::Policy exportable(CKM::Password(), true);
241
242     RUNNER_ASSERT_MSG(
243         CKM_API_SUCCESS == (temp = manager->saveData("data2", buffer, exportable)),
244         "Error=" << CKM::APICodeToString(temp));
245 }
246
247 RUNNER_CHILD_TEST(T17104_prep_data_03)
248 {
249     int temp;
250     ScopedAccessProvider ap(TEST_LABEL);
251     ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
252
253     CKM::AliasVector av;
254     auto manager = CKM::Manager::create();
255
256     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
257
258     CKM::RawBuffer buffer(data.begin(), data.end());
259     CKM::Policy exportable(CKM::Password(), true);
260
261     RUNNER_ASSERT_MSG(
262         CKM_API_SUCCESS == (temp = manager->saveData("data3", buffer, exportable)),
263         "Error=" << CKM::APICodeToString(temp));
264 }
265
266 RUNNER_CHILD_TEST(T17105_prep_data_04)
267 {
268     int temp;
269     ScopedAccessProvider ap(TEST_LABEL_2);
270     ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
271
272     CKM::AliasVector av;
273     auto manager = CKM::Manager::create();
274
275     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
276
277     CKM::RawBuffer buffer(data.begin(), data.end());
278     CKM::Policy exportable(CKM::Password(), true);
279
280     RUNNER_ASSERT_MSG(
281         CKM_API_SUCCESS == (temp = manager->saveData("data4", buffer, exportable)),
282         "Error=" << CKM::APICodeToString(temp));
283 }
284
285 RUNNER_TEST(T17106_remove_application)
286 {
287     int tmp;
288
289     auto control = CKM::Control::create();
290     RUNNER_ASSERT_MSG(
291         CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+3)),
292         "Error=" << CKM::APICodeToString(tmp));
293     RUNNER_ASSERT_MSG(
294         CKM_API_SUCCESS == (tmp = control->removeApplicationData(TEST_LABEL)),
295         "Error=" << CKM::APICodeToString(tmp));
296 }
297
298 RUNNER_CHILD_TEST(T17107_check_data_01)
299 {
300     int temp;
301     ScopedAccessProvider ap(TEST_LABEL);
302     ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
303
304     CKM::AliasVector av;
305     auto manager = CKM::Manager::create();
306
307     RUNNER_ASSERT_MSG(
308         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
309         "Error=" << CKM::APICodeToString(temp));
310     RUNNER_ASSERT_MSG(
311         0 == (temp = av.size()),
312         "Vector size: " << temp << ". Expected: 0");
313 }
314
315 RUNNER_CHILD_TEST(T17108_check_data_02)
316 {
317     int temp;
318     ScopedAccessProvider ap(TEST_LABEL_2);
319     ap.applyAndSwithToUser(USER_TEST+2, GROUP_APP);
320
321     CKM::AliasVector av;
322     auto manager = CKM::Manager::create();
323
324     RUNNER_ASSERT_MSG(
325         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
326         "Error=" << CKM::APICodeToString(temp));
327     RUNNER_ASSERT_MSG(
328         1 == (temp = av.size()),
329         "Vector size: " << temp << ". Expected: 1");
330 }
331
332 RUNNER_TEST(T17109_unlock_user2)
333 {
334     int tmp;
335
336     auto control = CKM::Control::create();
337     RUNNER_ASSERT_MSG(
338          CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+3, "t1706-special-password")),
339          "Error=" << CKM::APICodeToString(tmp));
340 }
341
342 RUNNER_CHILD_TEST(T17110_check_data_03)
343 {
344     int temp;
345     ScopedAccessProvider ap(TEST_LABEL);
346     ap.allowAPI("key-manager::api-storage", "rw");
347     ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
348
349     CKM::AliasVector av;
350     auto manager = CKM::Manager::create();
351
352     RUNNER_ASSERT_MSG(
353         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
354         "Error=" << CKM::APICodeToString(temp));
355     RUNNER_ASSERT_MSG(
356         0 == (temp = av.size()),
357         "Vector size: " << temp << ". Expected: 0");
358 }
359
360 RUNNER_CHILD_TEST(T17111_check_data_04)
361 {
362     int temp;
363     ScopedAccessProvider ap(TEST_LABEL_2);
364     ap.allowAPI("key-manager::api-storage", "rw");
365     ap.applyAndSwithToUser(USER_TEST+3, GROUP_APP);
366
367     CKM::AliasVector av;
368     auto manager = CKM::Manager::create();
369
370     RUNNER_ASSERT_MSG(
371         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
372         "Error=" << CKM::APICodeToString(temp));
373     RUNNER_ASSERT_MSG(
374         1 == (temp = av.size()),
375         "Vector size: " << temp << ". Expected: 1");
376 }
377
378 RUNNER_TEST(T17112_deinit)
379 {
380     remove_user_data(USER_TEST+2);
381     remove_user_data(USER_TEST+3);
382 }
383
384 int main(int argc, char *argv[])
385 {
386     if (geteuid() != 0)
387     {
388         std::cerr << argv[0] << " should be executed as root. Aborting" << std::endl;
389         return -1;
390     }
391     return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
392 }