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 sendToStorage(observer,
109 static_cast<int>(LogicCommand::REMOVE),
111 static_cast<int>(dataType),
113 }, [&observer](int error){ observer->ReceivedError(error); } );
116 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
118 DBDataType sendDataType,
119 const Password &password)
121 observerCheck(observer);
123 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
126 try_catch_async([&] {
127 sendToStorage(observer,
128 static_cast<int>(LogicCommand::GET),
130 static_cast<int>(sendDataType),
133 }, [&observer](int error){ observer->ReceivedError(error); } );
136 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
137 const Alias& privateKeyAlias,
138 const Password& password,
139 const RawBuffer& message,
140 const HashAlgorithm hash,
141 const RSAPaddingAlgorithm padding)
143 observerCheck(observer);
144 if (privateKeyAlias.empty() || message.empty()) {
145 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
148 try_catch_async([&] {
149 sendToStorage(observer,
150 static_cast<int>(LogicCommand::CREATE_SIGNATURE),
155 static_cast<int>(hash),
156 static_cast<int>(padding));
157 }, [&observer](int error) {observer->ReceivedError(error);});
160 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
161 const Alias& publicKeyOrCertAlias,
162 const Password& password,
163 const RawBuffer& message,
164 const RawBuffer& signature,
165 const HashAlgorithm hash,
166 const RSAPaddingAlgorithm padding)
168 observerCheck(observer);
169 if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
170 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
173 try_catch_async([&] {
174 sendToStorage(observer,
175 static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
177 publicKeyOrCertAlias,
181 static_cast<int>(hash),
182 static_cast<int>(padding));
183 }, [&observer](int error){ observer->ReceivedError(error); } );
186 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
187 const CertificateShPtrVector& certificateChainVector)
189 observerCheck(observer);
190 if (certificateChainVector.empty()) {
191 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
194 try_catch_async([&] {
195 RawBufferVector rawCertChain;
196 for (auto &e: certificateChainVector) {
197 rawCertChain.push_back(e->getDER());
201 auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
203 thread()->sendMessage(AsyncRequest(observer,
207 }, [&observer](int error){ observer->ReceivedError(error); } );
210 void ManagerAsync::Impl::allowAccess(const ObserverPtr& observer,
211 const std::string& alias,
212 const std::string& accessor,
215 observerCheck(observer);
216 if (alias.empty() || accessor.empty()) {
217 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
220 try_catch_async([&] {
221 sendToStorage(observer,
222 static_cast<int>(LogicCommand::ALLOW_ACCESS),
226 static_cast<int>(granted));
227 }, [&observer](int error){ observer->ReceivedError(error); } );
230 void ManagerAsync::Impl::denyAccess(const ObserverPtr& observer,
231 const std::string& alias,
232 const std::string& accessor)
234 observerCheck(observer);
235 if (alias.empty() || accessor.empty()) {
236 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
239 try_catch_async([&] {
240 sendToStorage(observer,
241 static_cast<int>(LogicCommand::DENY_ACCESS),
245 }, [&observer](int error){ observer->ReceivedError(error); } );
248 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
251 observerCheck(observer);
252 try_catch_async([&] {
253 sendToStorage(observer,
254 static_cast<int>(LogicCommand::GET_LIST),
256 static_cast<int>(dataType));
257 }, [&observer](int error){ observer->ReceivedError(error); } );
260 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
261 const KeyType key_type,
262 const int additional_param,
263 const Alias &privateKeyAlias,
264 const Alias &publicKeyAlias,
265 const Policy &policyPrivateKey,
266 const Policy &policyPublicKey)
268 observerCheck(observer);
269 if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
270 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
274 LogicCommand cmd_type;
277 case KeyType::KEY_RSA_PUBLIC:
278 case KeyType::KEY_RSA_PRIVATE:
279 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
282 case KeyType::KEY_DSA_PUBLIC:
283 case KeyType::KEY_DSA_PRIVATE:
284 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
287 case KeyType::KEY_ECDSA_PUBLIC:
288 case KeyType::KEY_ECDSA_PRIVATE:
289 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
293 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
297 try_catch_async([&] {
298 sendToStorage(observer,
299 static_cast<int>(cmd_type),
301 static_cast<int>(additional_param),
302 PolicySerializable(policyPrivateKey),
303 PolicySerializable(policyPublicKey),
306 }, [&observer](int error){ observer->ReceivedError(error); } );
309 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
312 throw std::invalid_argument("Empty observer");