fd5cd13572a08a29d985704eb8456c27d454d838
[platform/core/test/security-tests.git] / src / ckm / privileged / main.cpp
1 /*
2  *  Copyright (c) 2016 - 2020 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 <scoped-app-context.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         ScopedAppContext ctx(TEST_LABEL, USER_TEST, GROUP_APP);
81
82         auto manager = CKM::Manager::create();
83         RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certeeAlias, certee, CKM::Policy()));
84         RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certimAlias, certim, CKM::Policy()));
85     }
86
87     // restart CKM
88     stop_service(MANAGER);
89     start_service(MANAGER);
90
91     // actual test
92     {
93         ScopedDBUnlock unlock(USER_TEST, APP_PASS);
94         ScopedAppContext ctx(TEST_LABEL, USER_TEST, GROUP_APP);
95
96         auto manager = CKM::Manager::create();
97         int status1 = manager->saveCertificate(certeeAlias, certee, CKM::Policy());
98         int status2 = manager->saveCertificate(certimAlias, certim, CKM::Policy());
99         RUNNER_ASSERT_MSG(
100             CKM_API_ERROR_DB_ALIAS_EXISTS == status1,
101             "Certificate should be in database already. Error=" << CKM::APICodeToString(status1));
102         RUNNER_ASSERT_MSG(
103             CKM_API_ERROR_DB_ALIAS_EXISTS == status2,
104             "Certificate should be in database already. Error=" << CKM::APICodeToString(status2));
105     }
106 }
107
108 RUNNER_TEST(T1519_deinit)
109 {
110     remove_user_data(USER_TEST);
111 }
112
113 RUNNER_TEST_GROUP_INIT(T170_CKM_STORAGE_PERNAMENT_TESTS);
114
115 RUNNER_TEST(T1701_init_unlock_key)
116 {
117     unlock_user_data(USER_TEST+1, "t170-special-password");
118
119     ScopedAppContext ctx(TEST_LABEL, USER_TEST+1, GROUP_APP);
120 }
121
122 RUNNER_CHILD_TEST(T1702_insert_data)
123 {
124     int temp;
125     ScopedAppContext ctx(TEST_LABEL, USER_TEST+1, GROUP_APP);
126
127     auto certee = TestData::getTestCertificate(TestData::THIRD_PARTY_LEAF);
128
129     auto manager = CKM::Manager::create();
130     size_t current_aliases_num = count_aliases(ALIAS_CERT);
131     int status1 = manager->saveCertificate(CKM::Alias("CertEEE"), certee, CKM::Policy());
132
133     RUNNER_ASSERT_MSG(
134         CKM_API_SUCCESS == status1,
135         "Could not put certificate in datbase. Error=" << CKM::APICodeToString(status1));
136
137     CKM::AliasVector av;
138     RUNNER_ASSERT_MSG(
139         CKM_API_SUCCESS == (temp = manager->getCertificateAliasVector(av)),
140         "Error=" << CKM::APICodeToString(temp));
141     RUNNER_ASSERT_MSG(
142         (current_aliases_num+1) == static_cast<size_t>(temp = av.size()),
143         "Vector size: " << temp << ". Expected: " << (current_aliases_num+1));
144 }
145
146 RUNNER_TEST(T1703_removeApplicationData)
147 {
148     int tmp;
149     auto control = CKM::Control::create();
150     RUNNER_ASSERT_MSG(
151         CKM_API_SUCCESS == (tmp = control->removeApplicationData(TEST_LABEL)),
152         "Error=" << CKM::APICodeToString(tmp));
153 }
154
155 RUNNER_CHILD_TEST(T1704_data_test)
156 {
157     int temp;
158     ScopedAppContext ctx(TEST_LABEL, USER_TEST+1, GROUP_APP);
159
160     CKM::AliasVector av;
161     auto manager = CKM::Manager::create();
162
163     RUNNER_ASSERT_MSG(
164         CKM_API_SUCCESS == (temp = manager->getCertificateAliasVector(av)),
165         "Error=" << CKM::APICodeToString(temp));
166     RUNNER_ASSERT_MSG(
167         0 == (temp = av.size()),
168         "Vector size: " << temp << ". Expected: 0");
169 }
170
171 RUNNER_TEST(T1705_deinit)
172 {
173     remove_user_data(USER_TEST+1);
174 }
175
176 RUNNER_TEST(T17101_init)
177 {
178     int tmp;
179
180     auto control = CKM::Control::create();
181     RUNNER_ASSERT_MSG(
182         CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+2)),
183         "Error=" << CKM::APICodeToString(tmp));
184     RUNNER_ASSERT_MSG(
185         CKM_API_SUCCESS == (tmp = control->removeUserData(USER_TEST+2)),
186         "Error=" << CKM::APICodeToString(tmp));
187     RUNNER_ASSERT_MSG(
188         CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+2, "t1706-special-password")),
189         "Error=" << CKM::APICodeToString(tmp));
190
191     RUNNER_ASSERT_MSG(
192         CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+3)),
193         "Error=" << CKM::APICodeToString(tmp));
194     RUNNER_ASSERT_MSG(
195         CKM_API_SUCCESS == (tmp = control->removeUserData(USER_TEST+3)),
196         "Error=" << CKM::APICodeToString(tmp));
197     RUNNER_ASSERT_MSG(
198          CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+3, "t1706-special-password")),
199          "Error=" << CKM::APICodeToString(tmp));
200 }
201
202 RUNNER_CHILD_TEST(T17102_prep_data_01)
203 {
204     int temp;
205     ScopedAppContext ctx(TEST_LABEL, USER_TEST+2, GROUP_APP);
206
207     CKM::AliasVector av;
208     auto manager = CKM::Manager::create();
209
210     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
211
212     CKM::RawBuffer buffer(data.begin(), data.end());
213     CKM::Policy exportable(CKM::Password(), true);
214
215     RUNNER_ASSERT_MSG(
216         CKM_API_SUCCESS == (temp = manager->saveData("data1", buffer, exportable)),
217         "Error=" << CKM::APICodeToString(temp));
218 }
219
220 RUNNER_CHILD_TEST(T17103_prep_data_02)
221 {
222     int temp;
223     ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+2, GROUP_APP);
224
225     CKM::AliasVector av;
226     auto manager = CKM::Manager::create();
227
228     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
229
230     CKM::RawBuffer buffer(data.begin(), data.end());
231     CKM::Policy exportable(CKM::Password(), true);
232
233     RUNNER_ASSERT_MSG(
234         CKM_API_SUCCESS == (temp = manager->saveData("data2", buffer, exportable)),
235         "Error=" << CKM::APICodeToString(temp));
236 }
237
238 RUNNER_CHILD_TEST(T17104_prep_data_03)
239 {
240     int temp;
241     ScopedAppContext ctx(TEST_LABEL, USER_TEST+3, GROUP_APP);
242
243     CKM::AliasVector av;
244     auto manager = CKM::Manager::create();
245
246     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
247
248     CKM::RawBuffer buffer(data.begin(), data.end());
249     CKM::Policy exportable(CKM::Password(), true);
250
251     RUNNER_ASSERT_MSG(
252         CKM_API_SUCCESS == (temp = manager->saveData("data3", buffer, exportable)),
253         "Error=" << CKM::APICodeToString(temp));
254 }
255
256 RUNNER_CHILD_TEST(T17105_prep_data_04)
257 {
258     int temp;
259     ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+3, GROUP_APP);
260
261     CKM::AliasVector av;
262     auto manager = CKM::Manager::create();
263
264     std::string data = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b1bXDa+S8/MGWnMkru4";
265
266     CKM::RawBuffer buffer(data.begin(), data.end());
267     CKM::Policy exportable(CKM::Password(), true);
268
269     RUNNER_ASSERT_MSG(
270         CKM_API_SUCCESS == (temp = manager->saveData("data4", buffer, exportable)),
271         "Error=" << CKM::APICodeToString(temp));
272 }
273
274 RUNNER_TEST(T17106_remove_application)
275 {
276     int tmp;
277
278     auto control = CKM::Control::create();
279     RUNNER_ASSERT_MSG(
280         CKM_API_SUCCESS == (tmp = control->lockUserKey(USER_TEST+3)),
281         "Error=" << CKM::APICodeToString(tmp));
282     RUNNER_ASSERT_MSG(
283         CKM_API_SUCCESS == (tmp = control->removeApplicationData(TEST_LABEL)),
284         "Error=" << CKM::APICodeToString(tmp));
285 }
286
287 RUNNER_CHILD_TEST(T17107_check_data_01)
288 {
289     int temp;
290     ScopedAppContext ctx(TEST_LABEL, USER_TEST+2, GROUP_APP);
291
292     CKM::AliasVector av;
293     auto manager = CKM::Manager::create();
294
295     RUNNER_ASSERT_MSG(
296         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
297         "Error=" << CKM::APICodeToString(temp));
298     RUNNER_ASSERT_MSG(
299         0 == (temp = av.size()),
300         "Vector size: " << temp << ". Expected: 0");
301 }
302
303 RUNNER_CHILD_TEST(T17108_check_data_02)
304 {
305     int temp;
306     ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+2, GROUP_APP);
307
308     CKM::AliasVector av;
309     auto manager = CKM::Manager::create();
310
311     RUNNER_ASSERT_MSG(
312         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
313         "Error=" << CKM::APICodeToString(temp));
314     RUNNER_ASSERT_MSG(
315         1 == (temp = av.size()),
316         "Vector size: " << temp << ". Expected: 1");
317 }
318
319 RUNNER_TEST(T17109_unlock_user2)
320 {
321     int tmp;
322
323     auto control = CKM::Control::create();
324     RUNNER_ASSERT_MSG(
325          CKM_API_SUCCESS == (tmp = control->unlockUserKey(USER_TEST+3, "t1706-special-password")),
326          "Error=" << CKM::APICodeToString(tmp));
327 }
328
329 RUNNER_CHILD_TEST(T17110_check_data_03)
330 {
331     int temp;
332     ScopedAppContext ctx(TEST_LABEL, USER_TEST+3, GROUP_APP);
333
334     CKM::AliasVector av;
335     auto manager = CKM::Manager::create();
336
337     RUNNER_ASSERT_MSG(
338         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
339         "Error=" << CKM::APICodeToString(temp));
340     RUNNER_ASSERT_MSG(
341         0 == (temp = av.size()),
342         "Vector size: " << temp << ". Expected: 0");
343 }
344
345 RUNNER_CHILD_TEST(T17111_check_data_04)
346 {
347     int temp;
348     ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+3, GROUP_APP);
349
350     CKM::AliasVector av;
351     auto manager = CKM::Manager::create();
352
353     RUNNER_ASSERT_MSG(
354         CKM_API_SUCCESS == (temp = manager->getDataAliasVector(av)),
355         "Error=" << CKM::APICodeToString(temp));
356     RUNNER_ASSERT_MSG(
357         1 == (temp = av.size()),
358         "Vector size: " << temp << ". Expected: 1");
359 }
360
361 RUNNER_TEST(T17112_deinit)
362 {
363     remove_user_data(USER_TEST+2);
364     remove_user_data(USER_TEST+3);
365 }
366
367 int main(int argc, char *argv[])
368 {
369     if (geteuid() != 0)
370     {
371         std::cerr << argv[0] << " should be executed as root. Aborting" << std::endl;
372         return -1;
373     }
374     return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
375 }