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);
52 saveBinaryData(observer, alias, toDBDataType(key->getType()), key->getDER(), policy);
55 void ManagerAsync::Impl::saveCertificate(const ObserverPtr& observer,
57 const CertificateShPtr& cert,
60 observerCheck(observer);
61 if (alias.empty() || !cert) {
62 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
65 saveBinaryData(observer, alias, DBDataType::CERTIFICATE, cert->getDER(), policy);
68 void ManagerAsync::Impl::saveData(const ObserverPtr& observer,
70 const RawBuffer& data,
73 observerCheck(observer);
74 if (alias.empty() || data.empty()) {
75 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
78 saveBinaryData(observer, alias, DBDataType::BINARY_DATA, data, policy);
81 void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr& observer,
84 const RawBuffer& rawData,
88 AliasSupport helper(alias);
89 sendToStorage(observer,
90 static_cast<int>(LogicCommand::SAVE),
92 static_cast<int>(dataType),
96 PolicySerializable(policy));
97 }, [&observer](int error){ observer->ReceivedError(error); } );
100 void ManagerAsync::Impl::removeBinaryData(const ManagerAsync::ObserverPtr& observer,
104 observerCheck(observer);
106 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
109 try_catch_async([&] {
110 AliasSupport helper(alias);
111 sendToStorage(observer,
112 static_cast<int>(LogicCommand::REMOVE),
114 static_cast<int>(dataType),
117 }, [&observer](int error){ observer->ReceivedError(error); } );
120 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
122 DBDataType sendDataType,
123 const Password &password)
125 observerCheck(observer);
127 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
130 try_catch_async([&] {
131 AliasSupport helper(alias);
132 sendToStorage(observer,
133 static_cast<int>(LogicCommand::GET),
135 static_cast<int>(sendDataType),
139 }, [&observer](int error){ observer->ReceivedError(error); } );
142 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
143 const Alias& privateKeyAlias,
144 const Password& password,
145 const RawBuffer& message,
146 const HashAlgorithm hash,
147 const RSAPaddingAlgorithm padding)
149 observerCheck(observer);
150 if (privateKeyAlias.empty() || message.empty()) {
151 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
154 try_catch_async([&] {
155 AliasSupport helper(privateKeyAlias);
156 sendToStorage(observer,
157 static_cast<int>(LogicCommand::CREATE_SIGNATURE),
163 static_cast<int>(hash),
164 static_cast<int>(padding));
165 }, [&observer](int error) {observer->ReceivedError(error);});
168 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
169 const Alias& publicKeyOrCertAlias,
170 const Password& password,
171 const RawBuffer& message,
172 const RawBuffer& signature,
173 const HashAlgorithm hash,
174 const RSAPaddingAlgorithm padding)
176 observerCheck(observer);
177 if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
178 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
181 try_catch_async([&] {
182 AliasSupport helper(publicKeyOrCertAlias);
183 sendToStorage(observer,
184 static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
191 static_cast<int>(hash),
192 static_cast<int>(padding));
193 }, [&observer](int error){ observer->ReceivedError(error); } );
196 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
197 const CertificateShPtrVector& certificateChainVector)
199 observerCheck(observer);
200 if (certificateChainVector.empty()) {
201 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
204 try_catch_async([&] {
205 RawBufferVector rawCertChain;
206 for (auto &e: certificateChainVector) {
207 rawCertChain.push_back(e->getDER());
211 auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
213 thread()->sendMessage(AsyncRequest(observer,
217 }, [&observer](int error){ observer->ReceivedError(error); } );
220 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
222 const Label& accessor,
223 Permission newPermission)
225 observerCheck(observer);
226 if (alias.empty() || accessor.empty()) {
227 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
230 try_catch_async([&] {
231 AliasSupport helper(alias);
232 sendToStorage(observer,
233 static_cast<int>(LogicCommand::SET_PERMISSION),
238 static_cast<int>(newPermission));
239 }, [&observer](int error){ observer->ReceivedError(error); } );
242 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
245 observerCheck(observer);
246 try_catch_async([&] {
247 sendToStorage(observer,
248 static_cast<int>(LogicCommand::GET_LIST),
250 static_cast<int>(dataType));
251 }, [&observer](int error){ observer->ReceivedError(error); } );
254 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
255 const KeyType key_type,
256 const int additional_param,
257 const Alias &privateKeyAlias,
258 const Alias &publicKeyAlias,
259 const Policy &policyPrivateKey,
260 const Policy &policyPublicKey)
262 observerCheck(observer);
263 if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
264 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
268 LogicCommand cmd_type;
271 case KeyType::KEY_RSA_PUBLIC:
272 case KeyType::KEY_RSA_PRIVATE:
273 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
276 case KeyType::KEY_DSA_PUBLIC:
277 case KeyType::KEY_DSA_PRIVATE:
278 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
281 case KeyType::KEY_ECDSA_PUBLIC:
282 case KeyType::KEY_ECDSA_PRIVATE:
283 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
287 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
291 try_catch_async([&] {
292 AliasSupport prvHelper(privateKeyAlias);
293 AliasSupport pubHelper(publicKeyAlias);
294 sendToStorage(observer,
295 static_cast<int>(cmd_type),
297 static_cast<int>(additional_param),
298 PolicySerializable(policyPrivateKey),
299 PolicySerializable(policyPublicKey),
301 prvHelper.getLabel(),
303 pubHelper.getLabel());
304 }, [&observer](int error){ observer->ReceivedError(error); } );
307 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
310 throw std::invalid_argument("Empty observer");