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>
22 #include <dpl/log/log.h>
24 #include <client-manager-impl.h>
25 #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.getDER(), 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);
202 if (keyParsed.empty()) {
203 LogDebug("Key empty - failed to parse!");
204 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
209 return KEY_MANAGER_API_SUCCESS;
212 int Manager::ManagerImpl::getCertificate(const Alias &alias, const std::string &password, Certificate &cert)
214 DBDataType recvDataType;
217 int retCode = getBinaryData(
219 DBDataType::CERTIFICATE,
224 if (retCode != KEY_MANAGER_API_SUCCESS)
227 if (recvDataType != DBDataType::CERTIFICATE)
228 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
230 Certificate certParsed(rawData, DataFormat::FORM_DER);
232 if (certParsed.empty())
233 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
237 return KEY_MANAGER_API_SUCCESS;
240 int Manager::ManagerImpl::getData(const Alias &alias, const std::string &password, RawBuffer &rawData)
242 DBDataType recvDataType;
244 int retCode = getBinaryData(
246 DBDataType::BINARY_DATA,
251 if (retCode != KEY_MANAGER_API_SUCCESS)
254 if (recvDataType != DBDataType::BINARY_DATA)
255 return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
257 return KEY_MANAGER_API_SUCCESS;
260 int Manager::ManagerImpl::getBinaryDataAliasVector(DBDataType dataType, AliasVector &aliasVector)
262 return try_catch([&] {
264 MessageBuffer send, recv;
265 Serialization::Serialize(send, static_cast<int>(LogicCommand::GET_LIST));
266 Serialization::Serialize(send, m_counter);
267 Serialization::Serialize(send, static_cast<int>(dataType));
269 int retCode = sendToServer(
270 SERVICE_SOCKET_CKM_STORAGE,
274 if (KEY_MANAGER_API_SUCCESS != retCode) {
282 Deserialization::Deserialize(recv, command);
283 Deserialization::Deserialize(recv, counter);
284 Deserialization::Deserialize(recv, retCode);
285 Deserialization::Deserialize(recv, tmpDataType);
286 Deserialization::Deserialize(recv, aliasVector);
288 if (counter != m_counter) {
289 return KEY_MANAGER_API_ERROR_UNKNOWN;
296 int Manager::ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
297 return getBinaryDataAliasVector(DBDataType::KEY_RSA_PUBLIC, aliasVector);
300 int Manager::ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
301 return getBinaryDataAliasVector(DBDataType::CERTIFICATE, aliasVector);
304 int Manager::ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
305 return getBinaryDataAliasVector(DBDataType::BINARY_DATA, aliasVector);
308 int Manager::ManagerImpl::createKeyPairRSA(
309 const int size, // size in bits [1024, 2048, 4096]
310 const Alias &privateKeyAlias,
311 const Alias &publicKeyAlias,
312 const Policy &policyPrivateKey,
313 const Policy &policyPublicKey)
316 int my_counter = m_counter;
317 return try_catch([&] {
319 MessageBuffer send, recv;
320 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
321 Serialization::Serialize(send, my_counter);
322 Serialization::Serialize(send, static_cast<int>(size));
323 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
324 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
325 Serialization::Serialize(send, privateKeyAlias);
326 Serialization::Serialize(send, publicKeyAlias);
330 int retCode = sendToServer(
331 SERVICE_SOCKET_CKM_STORAGE,
335 if (KEY_MANAGER_API_SUCCESS != retCode) {
343 Deserialization::Deserialize(recv, command);
344 Deserialization::Deserialize(recv, counter);
345 Deserialization::Deserialize(recv, retCode);
347 if (counter != my_counter) {
348 return KEY_MANAGER_API_ERROR_UNKNOWN;
355 int Manager::ManagerImpl::createKeyPairECDSA(
357 const Alias &privateKeyAlias,
358 const Alias &publicKeyAlias,
359 const Policy &policyPrivateKey,
360 const Policy &policyPublicKey)
363 int my_counter = m_counter;
364 return try_catch([&] {
366 MessageBuffer send, recv;
367 Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_ECDSA));
368 Serialization::Serialize(send, my_counter);
369 Serialization::Serialize(send, static_cast<unsigned int>(type));
370 Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
371 Serialization::Serialize(send, PolicySerializable(policyPublicKey));
372 Serialization::Serialize(send, privateKeyAlias);
373 Serialization::Serialize(send, publicKeyAlias);
376 int retCode = sendToServer(
377 SERVICE_SOCKET_CKM_STORAGE,
381 if (KEY_MANAGER_API_SUCCESS != retCode) {
388 Deserialization::Deserialize(recv, command);
389 Deserialization::Deserialize(recv, counter);
390 Deserialization::Deserialize(recv, retCode);
392 if (counter != my_counter) {
393 return KEY_MANAGER_API_ERROR_UNKNOWN;