2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file client-manager-async-impl.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
24 #include <ckm/ckm-error.h>
25 #include <message-buffer.h>
26 #include <client-common.h>
28 #include <client-manager-async-impl.h>
32 int ManagerAsync::Impl::m_counter = 0;
34 ManagerAsync::Impl::Impl()
38 ManagerAsync::Impl::~Impl()
42 void ManagerAsync::Impl::saveKey(const ObserverPtr& observer,
47 observerCheck(observer);
48 if (alias.empty() || !key) {
49 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
53 saveBinaryData(observer, alias, DataType(key->getType()), key->getDER(), policy);
54 } Catch(DataType::Exception::Base) {
55 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
59 void ManagerAsync::Impl::saveCertificate(const ObserverPtr& observer,
61 const CertificateShPtr& cert,
64 observerCheck(observer);
65 if (alias.empty() || !cert) {
66 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
69 saveBinaryData(observer, alias, DataType::CERTIFICATE, cert->getDER(), policy);
72 void ManagerAsync::Impl::saveData(const ObserverPtr& observer,
74 const RawBuffer& data,
77 observerCheck(observer);
78 if (alias.empty() || data.empty()) {
79 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
82 saveBinaryData(observer, alias, DataType::BINARY_DATA, data, policy);
85 void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr& observer,
88 const RawBuffer& rawData,
92 AliasSupport helper(alias);
93 sendToStorage(observer,
94 static_cast<int>(LogicCommand::SAVE),
96 static_cast<int>(dataType),
100 PolicySerializable(policy));
101 }, [&observer](int error){ observer->ReceivedError(error); } );
104 void ManagerAsync::Impl::savePKCS12(const ManagerAsync::ObserverPtr& observer,
106 const PKCS12ShPtr &pkcs,
107 const Policy &keyPolicy,
108 const Policy &certPolicy)
110 try_catch_async([&] {
111 AliasSupport helper(alias);
112 sendToStorage(observer,
113 static_cast<int>(LogicCommand::SAVE_PKCS12),
117 PKCS12Serializable(*pkcs.get()),
118 PolicySerializable(keyPolicy),
119 PolicySerializable(certPolicy));
121 }, [&observer](int error){ observer->ReceivedError(error); } );
124 void ManagerAsync::Impl::removeAlias(const ManagerAsync::ObserverPtr& observer,
127 observerCheck(observer);
129 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
132 try_catch_async([&] {
133 AliasSupport helper(alias);
134 sendToStorage(observer,
135 static_cast<int>(LogicCommand::REMOVE),
139 }, [&observer](int error){ observer->ReceivedError(error); } );
142 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
144 DataType sendDataType,
145 const Password &password)
147 observerCheck(observer);
149 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
152 try_catch_async([&] {
153 AliasSupport helper(alias);
154 sendToStorage(observer,
155 static_cast<int>(LogicCommand::GET),
157 static_cast<int>(sendDataType),
161 }, [&observer](int error){ observer->ReceivedError(error); } );
164 void ManagerAsync::Impl::getPKCS12(const ManagerAsync::ObserverPtr& observer,
166 const Password &passwordKey,
167 const Password &passwordCert)
169 observerCheck(observer);
171 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
174 try_catch_async([&] {
175 AliasSupport helper(alias);
176 sendToStorage(observer,
177 static_cast<int>(LogicCommand::GET_PKCS12),
183 }, [&observer](int error){ observer->ReceivedError(error); } );
186 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
187 const Alias& privateKeyAlias,
188 const Password& password,
189 const RawBuffer& message,
190 const HashAlgorithm hash,
191 const RSAPaddingAlgorithm padding)
193 observerCheck(observer);
194 if (privateKeyAlias.empty() || message.empty()) {
195 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
198 try_catch_async([&] {
199 AliasSupport helper(privateKeyAlias);
200 sendToStorage(observer,
201 static_cast<int>(LogicCommand::CREATE_SIGNATURE),
207 static_cast<int>(hash),
208 static_cast<int>(padding));
209 }, [&observer](int error) {observer->ReceivedError(error);});
212 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
213 const Alias& publicKeyOrCertAlias,
214 const Password& password,
215 const RawBuffer& message,
216 const RawBuffer& signature,
217 const HashAlgorithm hash,
218 const RSAPaddingAlgorithm padding)
220 observerCheck(observer);
221 if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
222 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
225 try_catch_async([&] {
226 AliasSupport helper(publicKeyOrCertAlias);
227 sendToStorage(observer,
228 static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
235 static_cast<int>(hash),
236 static_cast<int>(padding));
237 }, [&observer](int error){ observer->ReceivedError(error); } );
240 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
241 const CertificateShPtrVector& certificateChainVector)
243 observerCheck(observer);
244 if (certificateChainVector.empty()) {
245 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
248 try_catch_async([&] {
249 RawBufferVector rawCertChain;
250 for (auto &e: certificateChainVector) {
251 rawCertChain.push_back(e->getDER());
255 auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
257 thread()->sendMessage(AsyncRequest(observer,
261 }, [&observer](int error){ observer->ReceivedError(error); } );
264 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
266 const Label& accessor,
267 PermissionMask permissionMask)
269 observerCheck(observer);
270 if (alias.empty() || accessor.empty()) {
271 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
274 try_catch_async([&] {
275 AliasSupport helper(alias);
276 sendToStorage(observer,
277 static_cast<int>(LogicCommand::SET_PERMISSION),
283 }, [&observer](int error){ observer->ReceivedError(error); } );
286 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
289 observerCheck(observer);
290 try_catch_async([&] {
291 sendToStorage(observer,
292 static_cast<int>(LogicCommand::GET_LIST),
294 static_cast<int>(dataType));
295 }, [&observer](int error){ observer->ReceivedError(error); } );
298 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
299 const KeyType key_type,
300 const int additional_param,
301 const Alias &privateKeyAlias,
302 const Alias &publicKeyAlias,
303 const Policy &policyPrivateKey,
304 const Policy &policyPublicKey)
306 observerCheck(observer);
307 if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
308 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
312 CryptoAlgorithm keyGenAlgorithm;
315 case KeyType::KEY_RSA_PUBLIC:
316 case KeyType::KEY_RSA_PRIVATE:
317 keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
318 keyGenAlgorithm.addParam(ParamName::GEN_KEY_LEN, additional_param);
321 case KeyType::KEY_DSA_PUBLIC:
322 case KeyType::KEY_DSA_PRIVATE:
323 keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
324 keyGenAlgorithm.addParam(ParamName::GEN_KEY_LEN, additional_param);
327 case KeyType::KEY_ECDSA_PUBLIC:
328 case KeyType::KEY_ECDSA_PRIVATE:
329 keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
330 keyGenAlgorithm.addParam(ParamName::GEN_EC, additional_param);
334 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
338 try_catch_async([&] {
339 AliasSupport prvHelper(privateKeyAlias);
340 AliasSupport pubHelper(publicKeyAlias);
341 sendToStorage(observer,
342 static_cast<int>(LogicCommand::CREATE_KEY_PAIR),
344 CryptoAlgorithmSerializable(keyGenAlgorithm),
345 PolicySerializable(policyPrivateKey),
346 PolicySerializable(policyPublicKey),
348 prvHelper.getLabel(),
350 pubHelper.getLabel());
351 }, [&observer](int error){ observer->ReceivedError(error); } );
354 void ManagerAsync::Impl::createKeyAES(const ManagerAsync::ObserverPtr& observer,
356 const Alias &keyAlias,
357 const Policy &policyKey)
359 observerCheck(observer);
360 if (keyAlias.empty()) {
361 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
365 try_catch_async([&] {
366 AliasSupport aliasHelper(keyAlias);
367 sendToStorage(observer,
368 static_cast<int>(LogicCommand::CREATE_KEY_AES),
370 static_cast<int>(size),
371 PolicySerializable(policyKey),
372 aliasHelper.getName(),
373 aliasHelper.getLabel());
374 }, [&observer](int error){ observer->ReceivedError(error); } );
377 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
380 throw std::invalid_argument("Empty observer");