Add service for control operations.
[platform/core/security/key-manager.git] / src / manager / client / client-control.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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 <client-common.h>
24 #include <message-buffer.h>
25 #include <protocols.h>
26
27 #include <ckm/key-manager.h>
28
29 namespace CKM {
30
31 class Control::ControlImpl {
32 public:
33     ControlImpl(){}
34     ControlImpl(const ControlImpl &) = delete;
35     ControlImpl(ControlImpl &&) = delete;
36     ControlImpl& operator=(const ControlImpl &) = delete;
37     ControlImpl& operator=(ControlImpl &&) = delete;
38
39     static int unlockUserKey(const std::string &user, const RawData &password) {
40         return try_catch([&] {
41             if (user.empty())
42                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
43
44             MessageBuffer send, recv;
45             Serialization::Serialize(send, static_cast<int>(ControlCommand::UNLOCK_USER_KEY));
46             Serialization::Serialize(send, user);
47             Serialization::Serialize(send, password);
48
49             int retCode = sendToServer(
50                 SERVICE_SOCKET_CKM_CONTROL,
51                 send.Pop(),
52                 recv);
53
54             if (KEY_MANAGER_API_SUCCESS != retCode) {
55                 return retCode;
56             }
57
58             Deserialization::Deserialize(recv, retCode);
59
60             return retCode;
61         });
62     }
63
64     static int lockUserKey(const std::string &user) {
65         return try_catch([&] {
66             if (user.empty())
67                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
68
69             MessageBuffer send, recv;
70             Serialization::Serialize(send, static_cast<int>(ControlCommand::LOCK_USER_KEY));
71             Serialization::Serialize(send, user);
72
73             int retCode = sendToServer(
74                 SERVICE_SOCKET_CKM_CONTROL,
75                 send.Pop(),
76                 recv);
77
78             if (KEY_MANAGER_API_SUCCESS != retCode) {
79                 return retCode;
80             }
81
82             Deserialization::Deserialize(recv, retCode);
83
84             return retCode;
85         });
86     }
87
88     static int removeUserData(const std::string &user) {
89         return try_catch([&] {
90             if (user.empty())
91                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
92
93             MessageBuffer send, recv;
94             Serialization::Serialize(send, static_cast<int>(ControlCommand::REMOVE_USER_DATA));
95             Serialization::Serialize(send, user);
96
97             int retCode = sendToServer(
98                 SERVICE_SOCKET_CKM_CONTROL,
99                 send.Pop(),
100                 recv);
101
102             if (KEY_MANAGER_API_SUCCESS != retCode) {
103                 return retCode;
104             }
105
106             Deserialization::Deserialize(recv, retCode);
107
108             return retCode;
109         });
110     }
111
112     static int changeUserPassword(const std::string &user, const RawData &oldPassword, const RawData &newPassword) {
113         return try_catch([&] {
114             if (user.empty())
115                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
116
117             MessageBuffer send, recv;
118             Serialization::Serialize(send, static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD));
119             Serialization::Serialize(send, user);
120             Serialization::Serialize(send, oldPassword);
121             Serialization::Serialize(send, newPassword);
122
123             int retCode = sendToServer(
124                 SERVICE_SOCKET_CKM_CONTROL,
125                 send.Pop(),
126                 recv);
127
128             if (KEY_MANAGER_API_SUCCESS != retCode) {
129                 return retCode;
130             }
131
132             Deserialization::Deserialize(recv, retCode);
133
134             return retCode;
135         });
136     }
137
138     static int resetUserPassword(const std::string &user, const RawData &newPassword) {
139         return try_catch([&] {
140             if (user.empty())
141                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
142
143             MessageBuffer send, recv;
144             Serialization::Serialize(send, static_cast<int>(ControlCommand::RESET_USER_PASSWORD));
145             Serialization::Serialize(send, user);
146             Serialization::Serialize(send, newPassword);
147
148             int retCode = sendToServer(
149                 SERVICE_SOCKET_CKM_CONTROL,
150                 send.Pop(),
151                 recv);
152
153             if (KEY_MANAGER_API_SUCCESS != retCode) {
154                 return retCode;
155             }
156
157             Deserialization::Deserialize(recv, retCode);
158
159             return retCode;
160         });
161     }
162
163     virtual ~ControlImpl(){}
164 };
165
166 Control::Control()
167   : m_impl(new ControlImpl)
168 {}
169
170 Control::~Control(){}
171
172 int Control::unlockUserKey(const std::string &user, const RawData &password) const {
173     return m_impl->unlockUserKey(user, password);
174 }
175
176 int Control::lockUserKey(const std::string &user) const {
177     return m_impl->lockUserKey(user);
178 }
179
180 int Control::removeUserData(const std::string &user) const {
181     return m_impl->removeUserData(user);
182 }
183
184 int Control::changeUserPassword(const std::string &user, const RawData &oldPassword, const RawData &newPassword) const {
185     return m_impl->changeUserPassword(user, oldPassword, newPassword);
186 }
187
188 int Control::resetUserPassword(const std::string &user, const RawData &newPassword) const {
189     return m_impl->resetUserPassword(user, newPassword);
190 }
191
192 } // namespace CKM
193