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, DBDataType(key->getType()), key->getDER(), policy);
54 } Catch(DBDataType::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, DBDataType::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, DBDataType::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 DBDataType 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,
167 observerCheck(observer);
169 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
172 try_catch_async([&] {
173 AliasSupport helper(alias);
174 sendToStorage(observer,
175 static_cast<int>(LogicCommand::GET_PKCS12),
179 }, [&observer](int error){ observer->ReceivedError(error); } );
182 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
183 const Alias& privateKeyAlias,
184 const Password& password,
185 const RawBuffer& message,
186 const HashAlgorithm hash,
187 const RSAPaddingAlgorithm padding)
189 observerCheck(observer);
190 if (privateKeyAlias.empty() || message.empty()) {
191 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
194 try_catch_async([&] {
195 AliasSupport helper(privateKeyAlias);
196 sendToStorage(observer,
197 static_cast<int>(LogicCommand::CREATE_SIGNATURE),
203 static_cast<int>(hash),
204 static_cast<int>(padding));
205 }, [&observer](int error) {observer->ReceivedError(error);});
208 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
209 const Alias& publicKeyOrCertAlias,
210 const Password& password,
211 const RawBuffer& message,
212 const RawBuffer& signature,
213 const HashAlgorithm hash,
214 const RSAPaddingAlgorithm padding)
216 observerCheck(observer);
217 if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
218 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
221 try_catch_async([&] {
222 AliasSupport helper(publicKeyOrCertAlias);
223 sendToStorage(observer,
224 static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
231 static_cast<int>(hash),
232 static_cast<int>(padding));
233 }, [&observer](int error){ observer->ReceivedError(error); } );
236 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
237 const CertificateShPtrVector& certificateChainVector)
239 observerCheck(observer);
240 if (certificateChainVector.empty()) {
241 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
244 try_catch_async([&] {
245 RawBufferVector rawCertChain;
246 for (auto &e: certificateChainVector) {
247 rawCertChain.push_back(e->getDER());
251 auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
253 thread()->sendMessage(AsyncRequest(observer,
257 }, [&observer](int error){ observer->ReceivedError(error); } );
260 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
262 const Label& accessor,
263 Permission newPermission)
265 observerCheck(observer);
266 if (alias.empty() || accessor.empty()) {
267 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
270 try_catch_async([&] {
271 AliasSupport helper(alias);
272 sendToStorage(observer,
273 static_cast<int>(LogicCommand::SET_PERMISSION),
278 static_cast<int>(newPermission));
279 }, [&observer](int error){ observer->ReceivedError(error); } );
282 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
285 observerCheck(observer);
286 try_catch_async([&] {
287 sendToStorage(observer,
288 static_cast<int>(LogicCommand::GET_LIST),
290 static_cast<int>(dataType));
291 }, [&observer](int error){ observer->ReceivedError(error); } );
294 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
295 const KeyType key_type,
296 const int additional_param,
297 const Alias &privateKeyAlias,
298 const Alias &publicKeyAlias,
299 const Policy &policyPrivateKey,
300 const Policy &policyPublicKey)
302 observerCheck(observer);
303 if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
304 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
308 LogicCommand cmd_type;
311 case KeyType::KEY_RSA_PUBLIC:
312 case KeyType::KEY_RSA_PRIVATE:
313 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
316 case KeyType::KEY_DSA_PUBLIC:
317 case KeyType::KEY_DSA_PRIVATE:
318 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
321 case KeyType::KEY_ECDSA_PUBLIC:
322 case KeyType::KEY_ECDSA_PRIVATE:
323 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
327 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
331 try_catch_async([&] {
332 AliasSupport prvHelper(privateKeyAlias);
333 AliasSupport pubHelper(publicKeyAlias);
334 sendToStorage(observer,
335 static_cast<int>(cmd_type),
337 static_cast<int>(additional_param),
338 PolicySerializable(policyPrivateKey),
339 PolicySerializable(policyPublicKey),
341 prvHelper.getLabel(),
343 pubHelper.getLabel());
344 }, [&observer](int error){ observer->ReceivedError(error); } );
347 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
350 throw std::invalid_argument("Empty observer");