3df917ed25164b2453cb503817f2a63838177589
[platform/core/test/security-tests.git] / src / ckm / password-integration.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 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       password-integration.cpp
17  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version    1.0
19  */
20 #include <vector>
21 #include <dpl/test/test_runner.h>
22 #include <dpl/test/test_runner_child.h>
23
24 #include <tests_common.h>
25
26 #include <ckm/ckm-control.h>
27 #include <ckm/ckm-manager.h>
28 #include <ckm/ckm-password.h>
29 #include <ckm/ckm-type.h>
30
31 #include <security-server.h>
32
33 #include <access_provider2.h>
34 #include <clean-env.h>
35
36 CKM::Alias CKM_ALIAS1 = "ALIAS1";
37 CKM::Alias CKM_ALIAS2 = "ALIAS2";
38
39 CKM::RawBuffer BIN_DATA1 = {'A','B','R','A','C','A','D','A','B','R','A'};
40
41 const char * PASSWORD1 = "LongPassword1";
42 const char * PASSWORD2 = "LongerPassword2";
43
44 static const int USER_APP = 5000;
45
46 const unsigned int PASSWORD_RETRY_TIMEOUT_US = 500000;
47
48 void dropPrivileges() {
49     static const std::string LABEL1 = "TestLabel1";
50     static const int GROUP_APP = 5000;
51
52     AccessProvider ap(LABEL1);
53     ap.allowAPI("key-manager::api-storage", "rw");
54     ap.applyAndSwithToUser(USER_APP, GROUP_APP);
55 }
56
57 RUNNER_TEST_GROUP_INIT(T401_SECURITY_SERVER_PASSWORD_INTEGRATION);
58
59 RUNNER_TEST(T4010_INIT)
60 {
61     reset_security_server();
62     unsigned int attempt, max_attempt, expire_sec;
63
64     int ret = security_server_chk_pwd(NULL, &attempt, &max_attempt, &expire_sec);
65     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "");
66 }
67
68 RUNNER_CHILD_TEST(T4011_ADD_DATA)
69 {
70     dropPrivileges();
71
72     auto mgr = CKM::Manager::create();
73
74     int ret = mgr->saveData(CKM_ALIAS1, BIN_DATA1, CKM::Policy());
75     RUNNER_ASSERT_MSG(ret == CKM_API_SUCCESS, "");
76 }
77
78 RUNNER_TEST(T4012_CLOSE_CKM_DB)
79 {
80     auto ctl = CKM::Control::create();
81
82     int ret = ctl->lockUserKey(USER_APP);
83     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
84 }
85
86 RUNNER_CHILD_TEST(T4013_GET_DATA)
87 {
88     dropPrivileges();
89
90     auto mgr = CKM::Manager::create();
91
92     CKM::RawBuffer buffer;
93
94     // CKM will automaticly unlock with empty password
95     int ret = mgr->getData(CKM_ALIAS1, CKM::Password(), buffer);
96     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
97 }
98
99 RUNNER_TEST(T4014_UNLOCK_DATABASE_WITH_SECURITY_SERVER)
100 {
101     unsigned int attempt, max_attempt, expire_sec;
102
103     usleep(PASSWORD_RETRY_TIMEOUT_US);
104
105     int ret = security_server_chk_pwd(NULL, &attempt, &max_attempt, &expire_sec);
106     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "");
107 }
108
109 RUNNER_CHILD_TEST(T4015_GET_DATA)
110 {
111     dropPrivileges();
112     auto mgr = CKM::Manager::create();
113
114     CKM::RawBuffer buffer;
115     int ret = mgr->getData(CKM_ALIAS1, CKM::Password(), buffer);
116     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
117
118     RUNNER_ASSERT_MSG(buffer == BIN_DATA1, "Data mismatch");
119 }
120
121 RUNNER_TEST_GROUP_INIT(T402_SECURITY_SERVER_PASSWORD_INTEGRATION);
122
123 RUNNER_TEST(T4020_INIT)
124 {
125     reset_security_server();
126
127     int ret = security_server_set_pwd(NULL, PASSWORD1, 10, 10);
128     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "");
129 }
130
131 RUNNER_CHILD_TEST(T4021_ADD_DATA)
132 {
133     dropPrivileges();
134
135     auto mgr = CKM::Manager::create();
136
137     int ret = mgr->saveData(CKM_ALIAS1, BIN_DATA1, CKM::Policy());
138     RUNNER_ASSERT_MSG(ret == CKM_API_SUCCESS, "");
139 }
140
141 RUNNER_TEST(T4022_CLOSE_CKM_DB)
142 {
143     unsigned int attempt, max, expire;
144
145     auto ctl = CKM::Control::create();
146
147     int ret = ctl->lockUserKey(USER_APP);
148     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
149
150     usleep(PASSWORD_RETRY_TIMEOUT_US);
151
152     // login with current password to get rid of invalid "NULL" DKEK
153     ret = security_server_chk_pwd(PASSWORD1, &attempt, &max, &expire);
154     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "Error=" << ret);
155
156     ret = ctl->lockUserKey(USER_APP);
157     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
158 }
159
160 RUNNER_CHILD_TEST(T4023_GET_DATA_NEGATIVE)
161 {
162     dropPrivileges();
163
164     auto mgr = CKM::Manager::create();
165
166     CKM::RawBuffer buffer;
167     int ret = mgr->getData(CKM_ALIAS1, CKM::Password(), buffer);
168     RUNNER_ASSERT_MSG(CKM_API_ERROR_DB_LOCKED == ret, "Error=" << CKM::ErrorToString(ret));
169 }
170
171 RUNNER_TEST(T4024_UNLOCK_DATABASE_WITH_SECURITY_SERVER)
172 {
173     unsigned int attempt, max, expire;
174
175     usleep(PASSWORD_RETRY_TIMEOUT_US);
176     int ret = security_server_chk_pwd(PASSWORD1, &attempt, &max, &expire);
177     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "Error =" << ret);
178 }
179
180 RUNNER_CHILD_TEST(T4025_GET_DATA)
181 {
182     dropPrivileges();
183
184     auto mgr = CKM::Manager::create();
185
186     CKM::RawBuffer buffer;
187     int ret = mgr->getData(CKM_ALIAS1, CKM::Password(), buffer);
188     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
189
190     RUNNER_ASSERT_MSG(buffer == BIN_DATA1, "Data missmatch");
191 }
192
193 RUNNER_TEST_GROUP_INIT(T403_SECURITY_SERVER_PASSWORD_INTEGRATION);
194
195 RUNNER_TEST(T4030_INIT)
196 {
197     reset_security_server();
198
199     int ret = security_server_set_pwd(NULL, PASSWORD1, 10, 10);
200     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "");
201 }
202
203 RUNNER_CHILD_TEST(T4031_ADD_DATA)
204 {
205     dropPrivileges();
206
207     auto mgr = CKM::Manager::create();
208
209     int ret = mgr->saveData(CKM_ALIAS1, BIN_DATA1, CKM::Policy());
210     RUNNER_ASSERT_MSG(ret == CKM_API_SUCCESS, "");
211 }
212
213 RUNNER_TEST(T4032_CLOSE_CKM_DB)
214 {
215     unsigned int attempt, max, expire;
216
217     auto ctl = CKM::Control::create();
218
219     int ret = ctl->lockUserKey(USER_APP);
220     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
221
222     usleep(PASSWORD_RETRY_TIMEOUT_US);
223
224     // login with current password to get rid of invalid "NULL" DKEK
225     ret = security_server_chk_pwd(PASSWORD1, &attempt, &max, &expire);
226     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "Error=" << ret);
227
228     ret = ctl->lockUserKey(USER_APP);
229     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
230 }
231
232 RUNNER_CHILD_TEST(T4033_GET_DATA_NEGATIVE)
233 {
234     dropPrivileges();
235
236     auto mgr = CKM::Manager::create();
237
238     CKM::RawBuffer buffer;
239     int ret = mgr->getData(CKM_ALIAS1, CKM::Password(), buffer);
240     RUNNER_ASSERT_MSG(CKM_API_ERROR_DB_LOCKED == ret, "Error=" << CKM::ErrorToString(ret));
241 }
242
243 RUNNER_TEST(T4034_UNLOCK_DATABASE_WITH_SECURITY_SERVER)
244 {
245     usleep(PASSWORD_RETRY_TIMEOUT_US);
246
247     int ret = security_server_set_pwd(PASSWORD1, PASSWORD2, 10, 10);
248     RUNNER_ASSERT_MSG(ret == SECURITY_SERVER_API_SUCCESS, "Error=" << ret);
249 }
250
251 RUNNER_CHILD_TEST(T4035_GET_DATA)
252 {
253     dropPrivileges();
254
255     auto mgr = CKM::Manager::create();
256
257     CKM::RawBuffer buffer;
258     int ret = mgr->getData(CKM_ALIAS1, CKM::Password(), buffer);
259     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == ret, "Error=" << CKM::ErrorToString(ret));
260
261     RUNNER_ASSERT_MSG(buffer == BIN_DATA1, "Data mismatch");
262 }
263
264