Add support for certificates.
[platform/core/security/key-manager.git] / src / manager / client / client-manager-impl.cpp
1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License
14  *
15  *
16  * @file        client-manager-impl.cpp
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version     1.0
19  * @brief       Manager implementation.
20  */
21 #include <dpl/serialization.h>
22
23 #include <client-manager-impl.h>
24 #include <client-common.h>
25 #include <client-key-impl.h>
26 #include <message-buffer.h>
27 #include <protocols.h>
28
29 namespace CKM {
30
31 int Manager::ManagerImpl::saveBinaryData(
32     const Alias &alias,
33     DBDataType dataType,
34     const RawData &rawData,
35     const Policy &policy)
36 {
37     m_counter++;
38
39     return try_catch([&] {
40         if (alias.empty() || rawData.empty())
41             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
42
43         MessageBuffer send, recv;
44         Serialization::Serialize(send, static_cast<int>(LogicCommand::SAVE));
45         Serialization::Serialize(send, m_counter);
46         Serialization::Serialize(send, static_cast<int>(dataType));
47         Serialization::Serialize(send, alias);
48         Serialization::Serialize(send, rawData);
49         Serialization::Serialize(send, PolicySerializable(policy));
50
51         int retCode = sendToServer(
52             SERVICE_SOCKET_CKM_STORAGE,
53             send.Pop(),
54             recv);
55
56         if (KEY_MANAGER_API_SUCCESS != retCode) {
57             return retCode;
58         }
59
60         int command;
61         int counter;
62         int opType;
63         Deserialization::Deserialize(recv, command);
64         Deserialization::Deserialize(recv, counter);
65         Deserialization::Deserialize(recv, opType);
66         Deserialization::Deserialize(recv, retCode);
67
68         if (counter != m_counter) {
69             return KEY_MANAGER_API_ERROR_UNKNOWN;
70         }
71
72         return retCode;
73     });
74 }
75
76 int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Policy &policy) {
77     return saveBinaryData(alias, toDBDataType(key.getType()), key.getKey(), policy);
78 }
79
80 int Manager::ManagerImpl::saveCertificate(
81     const Alias &alias,
82     const Certificate &cert,
83     const Policy &policy)
84 {
85     return saveBinaryData(alias, DBDataType::CERTIFICATE, cert.getDER(), policy);
86 }
87
88 int Manager::ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
89 {
90     return try_catch([&] {
91         if (alias.empty())
92             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
93
94         MessageBuffer send, recv;
95         Serialization::Serialize(send, static_cast<int>(LogicCommand::REMOVE));
96         Serialization::Serialize(send, m_counter);
97         Serialization::Serialize(send, static_cast<int>(dataType));
98         Serialization::Serialize(send, alias);
99
100         int retCode = sendToServer(
101             SERVICE_SOCKET_CKM_STORAGE,
102             send.Pop(),
103             recv);
104
105         if (KEY_MANAGER_API_SUCCESS != retCode) {
106             return retCode;
107         }
108
109         int command;
110         int counter;
111         int opType;
112         Deserialization::Deserialize(recv, command);
113         Deserialization::Deserialize(recv, counter);
114         Deserialization::Deserialize(recv, opType);
115         Deserialization::Deserialize(recv, retCode);
116
117         if (counter != m_counter) {
118             return KEY_MANAGER_API_ERROR_UNKNOWN;
119         }
120
121         return retCode;
122     });
123 }
124
125 int Manager::ManagerImpl::removeKey(const Alias &alias) {
126     return removeBinaryData(alias, DBDataType::KEY_RSA_PUBLIC);
127 }
128
129 int Manager::ManagerImpl::removeCertificate(const Alias &alias) {
130     return removeBinaryData(alias, DBDataType::CERTIFICATE);
131 }
132
133 int Manager::ManagerImpl::getBinaryData(
134     const Alias &alias,
135     DBDataType sendDataType,
136     const RawData &password,
137     DBDataType &recvDataType,
138     RawData &rawData)
139 {
140     return try_catch([&] {
141         if (alias.empty())
142             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
143
144         MessageBuffer send, recv;
145         Serialization::Serialize(send, static_cast<int>(LogicCommand::GET));
146         Serialization::Serialize(send, m_counter);
147         Serialization::Serialize(send, static_cast<int>(sendDataType));
148         Serialization::Serialize(send, alias);
149         Serialization::Serialize(send, password);
150
151         int retCode = sendToServer(
152             SERVICE_SOCKET_CKM_STORAGE,
153             send.Pop(),
154             recv);
155
156         if (KEY_MANAGER_API_SUCCESS != retCode) {
157             return retCode;
158         }
159
160         int command;
161         int counter;
162         int opType;
163         Deserialization::Deserialize(recv, command);
164         Deserialization::Deserialize(recv, counter);
165         Deserialization::Deserialize(recv, opType);
166         Deserialization::Deserialize(recv, retCode);
167
168         if (retCode == KEY_MANAGER_API_SUCCESS) {
169             int tmpDataType;
170             Deserialization::Deserialize(recv, tmpDataType);
171             Deserialization::Deserialize(recv, rawData);
172             recvDataType = static_cast<DBDataType>(tmpDataType);
173         }
174
175         if (counter != m_counter) {
176             return KEY_MANAGER_API_ERROR_UNKNOWN;
177         }
178
179         return retCode;
180     });
181 }
182
183 int Manager::ManagerImpl::getKey(const Alias &alias, const RawData &password, Key &key) {
184     DBDataType recvDataType;
185     RawData rawData;
186
187     int retCode = getBinaryData(
188         alias,
189         DBDataType::KEY_RSA_PUBLIC,
190         password,
191         recvDataType,
192         rawData);
193
194     if (retCode != KEY_MANAGER_API_SUCCESS)
195         return retCode;
196
197     Key keyParsed(rawData, toKeyType(recvDataType));
198
199     if (keyParsed.empty())
200         return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
201
202     key = keyParsed;
203
204     return KEY_MANAGER_API_SUCCESS;
205 }
206
207 int Manager::ManagerImpl::getCertificate(const Alias &alias, const RawData &password, Certificate &cert)
208 {
209     DBDataType recvDataType;
210     RawData rawData;
211
212     int retCode = getBinaryData(
213         alias,
214         DBDataType::CERTIFICATE,
215         password,
216         recvDataType,
217         rawData);
218
219     if (retCode != KEY_MANAGER_API_SUCCESS)
220         return retCode;
221
222     Certificate certParsed(rawData, Certificate::Format::FORM_DER);
223
224     if (certParsed.empty())
225         return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
226
227     cert = certParsed;
228
229     return KEY_MANAGER_API_SUCCESS;
230 }
231
232 } // namespace CKM
233