Change contact information to Mr Dongsun Lee
[platform/core/security/key-manager.git] / src / manager / client / client-control.cpp
1 /*
2  *  Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  *  Contact: Dongsun Lee <ds73.lee@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        client-common.cpp
19  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20  * @version     1.0
21  * @brief       This file is implementation of client-common functions.
22  */
23 #include <dpl/log/log.h>
24
25 #include <client-common.h>
26 #include <message-buffer.h>
27 #include <protocols.h>
28
29 #include <ckm/ckm-control.h>
30
31 namespace CKM {
32
33 class ControlImpl : public Control {
34 public:
35         ControlImpl() : m_controlConnection(SERVICE_SOCKET_CKM_CONTROL) {}
36         ControlImpl(const ControlImpl &) = delete;
37         ControlImpl(ControlImpl &&) = delete;
38         ControlImpl &operator=(const ControlImpl &) = delete;
39         ControlImpl &operator=(ControlImpl &&) = delete;
40
41         virtual int unlockUserKey(uid_t user, const Password &password)
42         {
43                 EXCEPTION_GUARD_START_CPPAPI
44
45                 if ((int)user < 0)
46                         return CKM_API_ERROR_INPUT_PARAM;
47
48                 MessageBuffer recv;
49                 auto send = MessageBuffer::Serialize(static_cast<int>
50                                                                                          (ControlCommand::UNLOCK_USER_KEY),
51                                                                                          user,
52                                                                                          password);
53
54                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
55
56                 if (CKM_API_SUCCESS != retCode)
57                         return retCode;
58
59                 recv.Deserialize(retCode);
60
61                 return retCode;
62
63                 EXCEPTION_GUARD_END
64         }
65
66         virtual int lockUserKey(uid_t user)
67         {
68                 EXCEPTION_GUARD_START_CPPAPI
69
70                 if ((int)user < 0)
71                         return CKM_API_ERROR_INPUT_PARAM;
72
73                 MessageBuffer recv;
74                 auto send = MessageBuffer::Serialize(static_cast<int>
75                                                                                          (ControlCommand::LOCK_USER_KEY), user);
76
77                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
78
79                 if (CKM_API_SUCCESS != retCode)
80                         return retCode;
81
82                 recv.Deserialize(retCode);
83
84                 return retCode;
85
86                 EXCEPTION_GUARD_END
87         }
88
89         virtual int removeUserData(uid_t user)
90         {
91                 EXCEPTION_GUARD_START_CPPAPI
92
93                 if ((int)user < 0)
94                         return CKM_API_ERROR_INPUT_PARAM;
95
96                 MessageBuffer recv;
97                 auto send = MessageBuffer::Serialize(static_cast<int>
98                                                                                          (ControlCommand::REMOVE_USER_DATA), user);
99
100                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
101
102                 if (CKM_API_SUCCESS != retCode)
103                         return retCode;
104
105                 recv.Deserialize(retCode);
106
107                 return retCode;
108
109                 EXCEPTION_GUARD_END
110         }
111
112         virtual int changeUserPassword(uid_t user, const Password &oldPassword,
113                                                                    const Password &newPassword)
114         {
115                 EXCEPTION_GUARD_START_CPPAPI
116
117                 if ((int)user < 0)
118                         return CKM_API_ERROR_INPUT_PARAM;
119
120                 MessageBuffer recv;
121                 auto send = MessageBuffer::Serialize(
122                                                 static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD),
123                                                 user,
124                                                 oldPassword,
125                                                 newPassword);
126
127                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
128
129                 if (CKM_API_SUCCESS != retCode)
130                         return retCode;
131
132                 recv.Deserialize(retCode);
133
134                 return retCode;
135
136                 EXCEPTION_GUARD_END
137         }
138
139         virtual int resetUserPassword(uid_t user, const Password &newPassword)
140         {
141                 EXCEPTION_GUARD_START_CPPAPI
142
143                 if ((int)user < 0)
144                         return CKM_API_ERROR_INPUT_PARAM;
145
146                 MessageBuffer recv;
147                 auto send = MessageBuffer::Serialize(
148                                                 static_cast<int>(ControlCommand::RESET_USER_PASSWORD),
149                                                 user,
150                                                 newPassword);
151
152                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
153
154                 if (CKM_API_SUCCESS != retCode)
155                         return retCode;
156
157                 recv.Deserialize(retCode);
158
159                 return retCode;
160
161                 EXCEPTION_GUARD_END
162         }
163
164         virtual int removeApplicationData(const ClientId &owner)
165         {
166                 EXCEPTION_GUARD_START_CPPAPI
167
168                 if (owner.empty())
169                         return CKM_API_ERROR_INPUT_PARAM;
170
171                 MessageBuffer recv;
172                 auto send = MessageBuffer::Serialize(static_cast<int>
173                                                                                          (ControlCommand::REMOVE_APP_DATA), owner);
174
175                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
176
177                 if (CKM_API_SUCCESS != retCode)
178                         return retCode;
179
180                 recv.Deserialize(retCode);
181
182                 return retCode;
183
184                 EXCEPTION_GUARD_END
185         }
186
187         virtual int updateCCMode()
188         {
189                 EXCEPTION_GUARD_START_CPPAPI
190
191                 MessageBuffer recv;
192                 auto send = MessageBuffer::Serialize(static_cast<int>
193                                                                                          (ControlCommand::UPDATE_CC_MODE));
194
195                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
196
197                 if (CKM_API_SUCCESS != retCode)
198                         return retCode;
199
200                 recv.Deserialize(retCode);
201
202                 return retCode;
203
204                 EXCEPTION_GUARD_END
205         }
206
207         virtual int setPermission(uid_t user,
208                                                           const Alias &alias,
209                                                           const ClientId &accessor,
210                                                           PermissionMask permissionMask)
211         {
212                 EXCEPTION_GUARD_START_CPPAPI
213
214                 MessageBuffer recv;
215                 AliasSupport helper(alias);
216                 auto send = MessageBuffer::Serialize(static_cast<int>
217                                                                                          (ControlCommand::SET_PERMISSION),
218                                                                                          static_cast<int>(user),
219                                                                                          helper.getName(),
220                                                                                          helper.getOwner(),
221                                                                                          accessor,
222                                                                                          permissionMask);
223
224                 int retCode = m_controlConnection.processRequest(send.Pop(), recv);
225
226                 if (CKM_API_SUCCESS != retCode)
227                         return retCode;
228
229                 int command;
230                 int counter;
231                 recv.Deserialize(command, counter, retCode);
232
233                 return retCode;
234
235                 EXCEPTION_GUARD_END
236         }
237
238         virtual ~ControlImpl() {}
239
240 private:
241         CKM::ServiceConnection m_controlConnection;
242 };
243
244 ControlShPtr Control::create()
245 {
246         try {
247                 return std::make_shared<ControlImpl>();
248         } catch (const std::bad_alloc &) {
249                 LogDebug("Bad alloc was caught during ControlImpl creation.");
250         } catch (...) {
251                 LogError("Critical error: Unknown exception was caught druing ControlImpl creation!");
252         }
253
254         return ControlShPtr();
255 }
256
257 } // namespace CKM
258