1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * @file client-manager-impl.cpp
17 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19 * @brief Manager implementation.
21 #include <dpl/serialization.h>
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>
31 int Manager::ManagerImpl::saveBinaryData(
34 const RawBuffer &rawData,
39 return try_catch([&] {
40 if (alias.empty() || rawData.empty())
41 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
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));
51 int retCode = sendToServer(
52 SERVICE_SOCKET_CKM_STORAGE,
56 if (KEY_MANAGER_API_SUCCESS != retCode) {
63 Deserialization::Deserialize(recv, command);
64 Deserialization::Deserialize(recv, counter);
65 Deserialization::Deserialize(recv, retCode);
66 Deserialization::Deserialize(recv, opType);
68 if (counter != m_counter) {
69 return KEY_MANAGER_API_ERROR_UNKNOWN;
76 int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Policy &policy) {
77 return saveBinaryData(alias, toDBDataType(key.getType()), key.getKey(), policy);
80 int Manager::ManagerImpl::saveCertificate(
82 const Certificate &cert,
85 return saveBinaryData(alias, DBDataType::CERTIFICATE, cert.getDER(), policy);
88 int Manager::ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
89 return saveBinaryData(alias, DBDataType::BINARY_DATA, rawData, policy);
92 int Manager::ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
94 return try_catch([&] {
96 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
98 MessageBuffer send, recv;
99 Serialization::Serialize(send, static_cast<int>(LogicCommand::REMOVE));
100 Serialization::Serialize(send, m_counter);
101 Serialization::Serialize(send, static_cast<int>(dataType));
102 Serialization::Serialize(send, alias);
104 int retCode = sendToServer(
105 SERVICE_SOCKET_CKM_STORAGE,
109 if (KEY_MANAGER_API_SUCCESS != retCode) {
116 Deserialization::Deserialize(recv, command);
117 Deserialization::Deserialize(recv, counter);
118 Deserialization::Deserialize(recv, retCode);
119 Deserialization::Deserialize(recv, opType);
121 if (counter != m_counter) {
122 return KEY_MANAGER_API_ERROR_UNKNOWN;
129 int Manager::ManagerImpl::removeKey(const Alias &alias) {
130 return removeBinaryData(alias, DBDataType::KEY_RSA_PUBLIC);
133 int Manager::ManagerImpl::removeCertificate(const Alias &alias) {
134 return removeBinaryData(alias, DBDataType::CERTIFICATE);
137 int Manager::ManagerImpl::removeData(const Alias &alias) {
138 return removeBinaryData(alias, DBDataType::BINARY_DATA);
141 int Manager::ManagerImpl::getBinaryData(
143 DBDataType sendDataType,
144 const RawBuffer &password,
145 DBDataType &recvDataType,
148 return try_catch([&] {
150 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
152 MessageBuffer send, recv;
153 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET));
154 Serialization::Serialize(send, m_counter);
155 Serialization::Serialize(send, static_cast<int>(sendDataType));
156 Serialization::Serialize(send, alias);
157 Serialization::Serialize(send, password);
159 int retCode = sendToServer(
160 SERVICE_SOCKET_CKM_STORAGE,
164 if (KEY_MANAGER_API_SUCCESS != retCode) {
170 Deserialization::Deserialize(recv, command);
171 Deserialization::Deserialize(recv, counter);
172 Deserialization::Deserialize(recv, retCode);
174 if (retCode == KEY_MANAGER_API_SUCCESS) {
176 Deserialization::Deserialize(recv, tmpDataType);
177 Deserialization::Deserialize(recv, rawData);
178 recvDataType = static_cast<DBDataType>(tmpDataType);
181 if (counter != m_counter) {
182 return KEY_MANAGER_API_ERROR_UNKNOWN;
189 int Manager::ManagerImpl::getKey(const Alias &alias, const RawBuffer &password, Key &key) {
190 DBDataType recvDataType;
193 int retCode = getBinaryData(
195 DBDataType::KEY_RSA_PUBLIC,
200 if (retCode != KEY_MANAGER_API_SUCCESS)
203 Key keyParsed(rawData, toKeyType(recvDataType));
205 if (keyParsed.empty())
206 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
210 return KEY_MANAGER_API_SUCCESS;
213 int Manager::ManagerImpl::getCertificate(const Alias &alias, const RawBuffer &password, Certificate &cert)
215 DBDataType recvDataType;
218 int retCode = getBinaryData(
220 DBDataType::CERTIFICATE,
225 if (retCode != KEY_MANAGER_API_SUCCESS)
228 if (recvDataType != DBDataType::CERTIFICATE)
229 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
231 Certificate certParsed(rawData, Certificate::Format::FORM_DER);
233 if (certParsed.empty())
234 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
238 return KEY_MANAGER_API_SUCCESS;
241 int Manager::ManagerImpl::getData(const Alias &alias, const RawBuffer &password, RawBuffer &rawData)
243 DBDataType recvDataType;
245 int retCode = getBinaryData(
247 DBDataType::CERTIFICATE,
252 if (retCode != KEY_MANAGER_API_SUCCESS)
255 if (recvDataType != DBDataType::BINARY_DATA)
256 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
258 return KEY_MANAGER_API_SUCCESS;