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>
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 std::string &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) {
171 Deserialization::Deserialize(recv, command);
172 Deserialization::Deserialize(recv, counter);
173 Deserialization::Deserialize(recv, retCode);
174 Deserialization::Deserialize(recv, tmpDataType);
175 Deserialization::Deserialize(recv, rawData);
176 recvDataType = static_cast<DBDataType>(tmpDataType);
178 if (counter != m_counter) {
179 return KEY_MANAGER_API_ERROR_UNKNOWN;
186 int Manager::ManagerImpl::getKey(const Alias &alias, const std::string &password, Key &key) {
187 DBDataType recvDataType;
190 int retCode = getBinaryData(
192 DBDataType::KEY_RSA_PUBLIC,
197 if (retCode != KEY_MANAGER_API_SUCCESS)
200 Key keyParsed(rawData, toKeyType(recvDataType));
202 if (keyParsed.empty())
203 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
207 return KEY_MANAGER_API_SUCCESS;
210 int Manager::ManagerImpl::getCertificate(const Alias &alias, const std::string &password, Certificate &cert)
212 DBDataType recvDataType;
215 int retCode = getBinaryData(
217 DBDataType::CERTIFICATE,
222 if (retCode != KEY_MANAGER_API_SUCCESS)
225 if (recvDataType != DBDataType::CERTIFICATE)
226 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
228 Certificate certParsed(rawData, DataFormat::FORM_DER);
230 if (certParsed.empty())
231 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
235 return KEY_MANAGER_API_SUCCESS;
238 int Manager::ManagerImpl::getData(const Alias &alias, const std::string &password, RawBuffer &rawData)
240 DBDataType recvDataType;
242 int retCode = getBinaryData(
244 DBDataType::CERTIFICATE,
249 if (retCode != KEY_MANAGER_API_SUCCESS)
252 if (recvDataType != DBDataType::BINARY_DATA)
253 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
255 return KEY_MANAGER_API_SUCCESS;
258 int Manager::ManagerImpl::requestBinaryDataAliasVector(DBDataType dataType, AliasVector &aliasVector)
260 return try_catch([&] {
262 MessageBuffer send, recv;
263 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET_LIST));
264 Serialization::Serialize(send, m_counter);
265 Serialization::Serialize(send, static_cast<int>(dataType));
267 int retCode = sendToServer(
268 SERVICE_SOCKET_CKM_STORAGE,
272 if (KEY_MANAGER_API_SUCCESS != retCode) {
280 Deserialization::Deserialize(recv, command);
281 Deserialization::Deserialize(recv, counter);
282 Deserialization::Deserialize(recv, retCode);
283 Deserialization::Deserialize(recv, tmpDataType);
284 Deserialization::Deserialize(recv, aliasVector);
286 if (counter != m_counter) {
287 return KEY_MANAGER_API_ERROR_UNKNOWN;
294 int Manager::ManagerImpl::requestKeyAliasVector(AliasVector &aliasVector) {
295 return requestBinaryDataAliasVector(DBDataType::KEY_RSA_PUBLIC, aliasVector);
298 int Manager::ManagerImpl::requestCertificateAliasVector(AliasVector &aliasVector) {
299 return requestBinaryDataAliasVector(DBDataType::CERTIFICATE, aliasVector);
302 int Manager::ManagerImpl::requestDataAliasVector(AliasVector &aliasVector) {
303 return requestBinaryDataAliasVector(DBDataType::BINARY_DATA, aliasVector);
306 int Manager::ManagerImpl::createKeyPairRSA(
307 const int size, // size in bits [1024, 2048, 4096]
308 const Alias &privateKeyAlias,
309 const Alias &publicKeyAlias,
310 const Policy &policyPrivateKey,
311 const Policy &policyPublicKey)
314 int my_counter = m_counter;
315 return try_catch([&] {
317 MessageBuffer send, recv;
318 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
319 Serialization::Serialize(send, my_counter);
320 Serialization::Serialize(send, static_cast<int>(size));
321 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
322 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
323 Serialization::Serialize(send, privateKeyAlias);
324 Serialization::Serialize(send, publicKeyAlias);
328 int retCode = sendToServer(
329 SERVICE_SOCKET_CKM_STORAGE,
333 if (KEY_MANAGER_API_SUCCESS != retCode) {
341 Deserialization::Deserialize(recv, command);
342 Deserialization::Deserialize(recv, counter);
343 Deserialization::Deserialize(recv, retCode);
345 if (counter != my_counter) {
346 return KEY_MANAGER_API_ERROR_UNKNOWN;
353 int Manager::ManagerImpl::createKeyPairECDSA(
355 const Alias &privateKeyAlias,
356 const Alias &publicKeyAlias,
357 const Policy &policyPrivateKey,
358 const Policy &policyPublicKey)
361 int my_counter = m_counter;
362 return try_catch([&] {
364 MessageBuffer send, recv;
365 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_ECDSA));
366 Serialization::Serialize(send, my_counter);
367 Serialization::Serialize(send, static_cast<unsigned int>(type));
368 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
369 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
370 Serialization::Serialize(send, privateKeyAlias);
371 Serialization::Serialize(send, publicKeyAlias);
374 int retCode = sendToServer(
375 SERVICE_SOCKET_CKM_STORAGE,
379 if (KEY_MANAGER_API_SUCCESS != retCode) {
386 Deserialization::Deserialize(recv, command);
387 Deserialization::Deserialize(recv, counter);
388 Deserialization::Deserialize(recv, retCode);
390 if (counter != my_counter) {
391 return KEY_MANAGER_API_ERROR_UNKNOWN;