CKM: no more "empty password for lockscreen" logics.
[platform/core/test/security-tests.git] / tests / ckm / ckm-common.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 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 /*
17  * @file       ckm-common.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21 #include <string>
22
23 #include <sys/smack.h>
24 #include <ckmc/ckmc-type.h>
25 #include <ckm-common.h>
26 #include <tests_common.h>
27 #include <access_provider2.h>
28 #include <ckm/ckm-control.h>
29 #include <ckm/ckm-manager.h>
30 #include <ckmc/ckmc-control.h>
31 #include <ckmc/ckmc-manager.h>
32 #include <dbus_access.h>
33
34 const char* SERVICE[] = {
35         "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2dlistener_2eservice",
36         "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2eservice" };
37
38 void start_service(ServiceIdx idx)
39 {
40     DBusAccess dbus(SERVICE[idx]);
41     dbus.start();
42 }
43
44 void stop_service(ServiceIdx idx)
45 {
46     DBusAccess dbus(SERVICE[idx]);
47     dbus.stop();
48 }
49
50
51 void switch_to_storage_user(const char* label)
52 {
53     AccessProvider ap(label);
54     ap.allowAPI("key-manager::api-storage", "rw");
55     ap.applyAndSwithToUser(APP_UID, APP_GID);
56 }
57
58 void switch_to_storage_ocsp_user(const char* label)
59 {
60     AccessProvider ap(label);
61     ap.allowAPI("key-manager::api-storage", "rw");
62     ap.allowAPI("key-manager::api-ocsp", "rw");
63     ap.applyAndSwithToUser(APP_UID, APP_GID);
64 }
65
66 DBCleanup::~DBCleanup()
67 {
68     // Let it throw. If db can't be cleared further tests are unreliable
69     CKM::ManagerShPtr mgr = CKM::Manager::create();
70     for(const auto& it:m_aliases)
71         mgr->removeAlias(it);
72     m_aliases.clear();
73 }
74
75 // returns process label
76 CharPtr get_label()
77 {
78     int ret;
79     char* my_label = NULL;
80     RUNNER_ASSERT_MSG(0 <= (ret = smack_new_label_from_self(&my_label)),
81                          "Failed to get smack label for self. Error: " << ret);
82
83     return CharPtr(my_label, free);
84 }
85
86 std::string aliasWithLabel(const char *label, const char *alias)
87 {
88     if(label)
89     {
90         std::stringstream ss;
91         ss << label << std::string(ckmc_label_name_separator) << alias;
92         return ss.str();
93     }
94     return std::string(alias);
95 }
96
97 // changes process label
98 void change_label(const char* label)
99 {
100     int ret = smack_set_label_for_self(label);
101     RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self. Error: " << ret);
102 }
103
104 ScopedLabel::ScopedLabel(const char* label) : m_original_label(get_label())
105 {
106     change_label(label);
107 }
108
109 ScopedLabel::~ScopedLabel()
110 {
111     /*
112      * Let it throw. If we can't restore label then remaining tests results will be
113      * unreliable anyway.
114      */
115     change_label(m_original_label.get());
116 }
117
118 const char * CKMCErrorToString(int error) {
119 #define ERRORDESCRIBE(name) case name: return #name
120     switch(error) {
121         ERRORDESCRIBE(CKMC_ERROR_NONE);
122         ERRORDESCRIBE(CKMC_ERROR_INVALID_PARAMETER);
123         ERRORDESCRIBE(CKMC_ERROR_OUT_OF_MEMORY);
124         ERRORDESCRIBE(CKMC_ERROR_PERMISSION_DENIED);
125         ERRORDESCRIBE(CKMC_ERROR_SOCKET);
126         ERRORDESCRIBE(CKMC_ERROR_BAD_REQUEST);
127         ERRORDESCRIBE(CKMC_ERROR_BAD_RESPONSE);
128         ERRORDESCRIBE(CKMC_ERROR_SEND_FAILED);
129         ERRORDESCRIBE(CKMC_ERROR_RECV_FAILED);
130         ERRORDESCRIBE(CKMC_ERROR_AUTHENTICATION_FAILED);
131         ERRORDESCRIBE(CKMC_ERROR_BUFFER_TOO_SMALL);
132         ERRORDESCRIBE(CKMC_ERROR_SERVER_ERROR);
133         ERRORDESCRIBE(CKMC_ERROR_DB_LOCKED);
134         ERRORDESCRIBE(CKMC_ERROR_DB_ERROR);
135         ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_EXISTS);
136         ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_UNKNOWN);
137         ERRORDESCRIBE(CKMC_ERROR_VERIFICATION_FAILED);
138         ERRORDESCRIBE(CKMC_ERROR_INVALID_FORMAT);
139         ERRORDESCRIBE(CKMC_ERROR_FILE_ACCESS_DENIED);
140         ERRORDESCRIBE(CKMC_ERROR_NOT_EXPORTABLE);
141         ERRORDESCRIBE(CKMC_ERROR_FILE_SYSTEM);
142         ERRORDESCRIBE(CKMC_ERROR_UNKNOWN);
143         default: return "Error not defined";
144     }
145 #undef ERRORDESCRIBE
146 }
147
148 std::string CKMCReadableError(int error) {
149     std::string output("Error: ");
150     output += std::to_string(error);
151     output += " Description: ";
152     output += CKMCErrorToString(error);
153     return output;
154 }
155
156 void unlock_user_data(uid_t user_id, const char *passwd)
157 {
158     int ret;
159     auto control = CKM::Control::create();
160     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->unlockUserKey(user_id, passwd)),
161                       "Error=" << CKM::ErrorToString(ret));
162 }
163
164 void remove_user_data(uid_t user_id)
165 {
166     int ret;
167     auto control = CKM::Control::create();
168     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->lockUserKey(user_id)),
169                       "Error=" << CKM::ErrorToString(ret));
170     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->removeUserData(user_id)),
171                       "Remove user data failed with error: " << CKM::ErrorToString(ret));
172 }
173
174 void reset_user_data(uid_t user_id, const char *passwd)
175 {
176     remove_user_data(user_id);
177     unlock_user_data(user_id, passwd);
178 }