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 sendToStorage(observer,
89 static_cast<int>(LogicCommand::SAVE),
91 static_cast<int>(dataType),
94 PolicySerializable(policy));
95 }, [&observer](int error){ observer->ReceivedError(error); } );
98 void ManagerAsync::Impl::removeBinaryData(const ManagerAsync::ObserverPtr& observer,
102 observerCheck(observer);
104 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
107 try_catch_async([&] {
108 AliasSupport helper(alias);
109 sendToStorage(observer,
110 static_cast<int>(LogicCommand::REMOVE),
112 static_cast<int>(dataType),
115 }, [&observer](int error){ observer->ReceivedError(error); } );
118 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
120 DBDataType sendDataType,
121 const Password &password)
123 observerCheck(observer);
125 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
128 try_catch_async([&] {
129 AliasSupport helper(alias);
130 sendToStorage(observer,
131 static_cast<int>(LogicCommand::GET),
133 static_cast<int>(sendDataType),
137 }, [&observer](int error){ observer->ReceivedError(error); } );
140 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
141 const Alias& privateKeyAlias,
142 const Password& password,
143 const RawBuffer& message,
144 const HashAlgorithm hash,
145 const RSAPaddingAlgorithm padding)
147 observerCheck(observer);
148 if (privateKeyAlias.empty() || message.empty()) {
149 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
152 try_catch_async([&] {
153 sendToStorage(observer,
154 static_cast<int>(LogicCommand::CREATE_SIGNATURE),
159 static_cast<int>(hash),
160 static_cast<int>(padding));
161 }, [&observer](int error) {observer->ReceivedError(error);});
164 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
165 const Alias& publicKeyOrCertAlias,
166 const Password& password,
167 const RawBuffer& message,
168 const RawBuffer& signature,
169 const HashAlgorithm hash,
170 const RSAPaddingAlgorithm padding)
172 observerCheck(observer);
173 if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
174 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
177 try_catch_async([&] {
178 sendToStorage(observer,
179 static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
181 publicKeyOrCertAlias,
185 static_cast<int>(hash),
186 static_cast<int>(padding));
187 }, [&observer](int error){ observer->ReceivedError(error); } );
190 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
191 const CertificateShPtrVector& certificateChainVector)
193 observerCheck(observer);
194 if (certificateChainVector.empty()) {
195 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
198 try_catch_async([&] {
199 RawBufferVector rawCertChain;
200 for (auto &e: certificateChainVector) {
201 rawCertChain.push_back(e->getDER());
205 auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
207 thread()->sendMessage(AsyncRequest(observer,
211 }, [&observer](int error){ observer->ReceivedError(error); } );
214 void ManagerAsync::Impl::allowAccess(const ObserverPtr& observer,
215 const std::string& alias,
216 const std::string& accessor,
219 observerCheck(observer);
220 if (alias.empty() || accessor.empty()) {
221 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
224 try_catch_async([&] {
225 sendToStorage(observer,
226 static_cast<int>(LogicCommand::ALLOW_ACCESS),
230 static_cast<int>(granted));
231 }, [&observer](int error){ observer->ReceivedError(error); } );
234 void ManagerAsync::Impl::denyAccess(const ObserverPtr& observer,
235 const std::string& alias,
236 const std::string& accessor)
238 observerCheck(observer);
239 if (alias.empty() || accessor.empty()) {
240 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
243 try_catch_async([&] {
244 sendToStorage(observer,
245 static_cast<int>(LogicCommand::DENY_ACCESS),
249 }, [&observer](int error){ observer->ReceivedError(error); } );
252 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
255 observerCheck(observer);
256 try_catch_async([&] {
257 sendToStorage(observer,
258 static_cast<int>(LogicCommand::GET_LIST),
260 static_cast<int>(dataType));
261 }, [&observer](int error){ observer->ReceivedError(error); } );
264 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
265 const KeyType key_type,
266 const int additional_param,
267 const Alias &privateKeyAlias,
268 const Alias &publicKeyAlias,
269 const Policy &policyPrivateKey,
270 const Policy &policyPublicKey)
272 observerCheck(observer);
273 if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
274 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
278 LogicCommand cmd_type;
281 case KeyType::KEY_RSA_PUBLIC:
282 case KeyType::KEY_RSA_PRIVATE:
283 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
286 case KeyType::KEY_DSA_PUBLIC:
287 case KeyType::KEY_DSA_PRIVATE:
288 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
291 case KeyType::KEY_ECDSA_PUBLIC:
292 case KeyType::KEY_ECDSA_PRIVATE:
293 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
297 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
301 try_catch_async([&] {
302 sendToStorage(observer,
303 static_cast<int>(cmd_type),
305 static_cast<int>(additional_param),
306 PolicySerializable(policyPrivateKey),
307 PolicySerializable(policyPublicKey),
310 }, [&observer](int error){ observer->ReceivedError(error); } );
313 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
316 throw std::invalid_argument("Empty observer");