Integration with CryptoService class.
[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(uid_t user, const std::string &password) {
40         return try_catch([&] {
41             MessageBuffer send, recv;
42             Serialization::Serialize(send, static_cast<int>(ControlCommand::UNLOCK_USER_KEY));
43             Serialization::Serialize(send, user);
44             Serialization::Serialize(send, password);
45
46             int retCode = sendToServer(
47                 SERVICE_SOCKET_CKM_CONTROL,
48                 send.Pop(),
49                 recv);
50
51             if (KEY_MANAGER_API_SUCCESS != retCode) {
52                 return retCode;
53             }
54
55             Deserialization::Deserialize(recv, retCode);
56
57             return retCode;
58         });
59     }
60
61     static int lockUserKey(uid_t user) {
62         return try_catch([&] {
63             MessageBuffer send, recv;
64             Serialization::Serialize(send, static_cast<int>(ControlCommand::LOCK_USER_KEY));
65             Serialization::Serialize(send, user);
66
67             int retCode = sendToServer(
68                 SERVICE_SOCKET_CKM_CONTROL,
69                 send.Pop(),
70                 recv);
71
72             if (KEY_MANAGER_API_SUCCESS != retCode) {
73                 return retCode;
74             }
75
76             Deserialization::Deserialize(recv, retCode);
77
78             return retCode;
79         });
80     }
81
82     static int removeUserData(uid_t user) {
83         return try_catch([&] {
84             MessageBuffer send, recv;
85             Serialization::Serialize(send, static_cast<int>(ControlCommand::REMOVE_USER_DATA));
86             Serialization::Serialize(send, user);
87
88             int retCode = sendToServer(
89                 SERVICE_SOCKET_CKM_CONTROL,
90                 send.Pop(),
91                 recv);
92
93             if (KEY_MANAGER_API_SUCCESS != retCode) {
94                 return retCode;
95             }
96
97             Deserialization::Deserialize(recv, retCode);
98
99             return retCode;
100         });
101     }
102
103     static int changeUserPassword(uid_t user, const std::string &oldPassword, const std::string &newPassword) {
104         return try_catch([&] {
105             MessageBuffer send, recv;
106             Serialization::Serialize(send, static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD));
107             Serialization::Serialize(send, user);
108             Serialization::Serialize(send, oldPassword);
109             Serialization::Serialize(send, newPassword);
110
111             int retCode = sendToServer(
112                 SERVICE_SOCKET_CKM_CONTROL,
113                 send.Pop(),
114                 recv);
115
116             if (KEY_MANAGER_API_SUCCESS != retCode) {
117                 return retCode;
118             }
119
120             Deserialization::Deserialize(recv, retCode);
121
122             return retCode;
123         });
124     }
125
126     static int resetUserPassword(uid_t user, const std::string &newPassword) {
127         return try_catch([&] {
128             MessageBuffer send, recv;
129             Serialization::Serialize(send, static_cast<int>(ControlCommand::RESET_USER_PASSWORD));
130             Serialization::Serialize(send, user);
131             Serialization::Serialize(send, newPassword);
132
133             int retCode = sendToServer(
134                 SERVICE_SOCKET_CKM_CONTROL,
135                 send.Pop(),
136                 recv);
137
138             if (KEY_MANAGER_API_SUCCESS != retCode) {
139                 return retCode;
140             }
141
142             Deserialization::Deserialize(recv, retCode);
143
144             return retCode;
145         });
146     }
147
148     virtual ~ControlImpl(){}
149 };
150
151 Control::Control()
152   : m_impl(new ControlImpl)
153 {}
154
155 Control::~Control(){}
156
157 int Control::unlockUserKey(uid_t user, const std::string &password) const {
158     return m_impl->unlockUserKey(user, password);
159 }
160
161 int Control::lockUserKey(uid_t user) const {
162     return m_impl->lockUserKey(user);
163 }
164
165 int Control::removeUserData(uid_t user) const {
166     return m_impl->removeUserData(user);
167 }
168
169 int Control::changeUserPassword(uid_t user, const std::string &oldPassword, const std::string &newPassword) const {
170     return m_impl->changeUserPassword(user, oldPassword, newPassword);
171 }
172
173 int Control::resetUserPassword(uid_t user, const std::string &newPassword) const {
174     return m_impl->resetUserPassword(user, newPassword);
175 }
176
177 } // namespace CKM
178