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 AliasSupport helper(privateKeyAlias);
154 sendToStorage(observer,
155 static_cast<int>(LogicCommand::CREATE_SIGNATURE),
161 static_cast<int>(hash),
162 static_cast<int>(padding));
163 }, [&observer](int error) {observer->ReceivedError(error);});
166 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
167 const Alias& publicKeyOrCertAlias,
168 const Password& password,
169 const RawBuffer& message,
170 const RawBuffer& signature,
171 const HashAlgorithm hash,
172 const RSAPaddingAlgorithm padding)
174 observerCheck(observer);
175 if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
176 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
179 try_catch_async([&] {
180 AliasSupport helper(publicKeyOrCertAlias);
181 sendToStorage(observer,
182 static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
189 static_cast<int>(hash),
190 static_cast<int>(padding));
191 }, [&observer](int error){ observer->ReceivedError(error); } );
194 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
195 const CertificateShPtrVector& certificateChainVector)
197 observerCheck(observer);
198 if (certificateChainVector.empty()) {
199 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
202 try_catch_async([&] {
203 RawBufferVector rawCertChain;
204 for (auto &e: certificateChainVector) {
205 rawCertChain.push_back(e->getDER());
209 auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
211 thread()->sendMessage(AsyncRequest(observer,
215 }, [&observer](int error){ observer->ReceivedError(error); } );
218 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
220 const Label& accessor,
221 Permission newPermission)
223 observerCheck(observer);
224 if (alias.empty() || accessor.empty()) {
225 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
228 try_catch_async([&] {
229 sendToStorage(observer,
230 static_cast<int>(LogicCommand::SET_PERMISSION),
234 static_cast<int>(newPermission));
235 }, [&observer](int error){ observer->ReceivedError(error); } );
238 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
241 observerCheck(observer);
242 try_catch_async([&] {
243 sendToStorage(observer,
244 static_cast<int>(LogicCommand::GET_LIST),
246 static_cast<int>(dataType));
247 }, [&observer](int error){ observer->ReceivedError(error); } );
250 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
251 const KeyType key_type,
252 const int additional_param,
253 const Alias &privateKeyAlias,
254 const Alias &publicKeyAlias,
255 const Policy &policyPrivateKey,
256 const Policy &policyPublicKey)
258 observerCheck(observer);
259 if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
260 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
264 LogicCommand cmd_type;
267 case KeyType::KEY_RSA_PUBLIC:
268 case KeyType::KEY_RSA_PRIVATE:
269 cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
272 case KeyType::KEY_DSA_PUBLIC:
273 case KeyType::KEY_DSA_PRIVATE:
274 cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
277 case KeyType::KEY_ECDSA_PUBLIC:
278 case KeyType::KEY_ECDSA_PRIVATE:
279 cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
283 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
287 try_catch_async([&] {
288 sendToStorage(observer,
289 static_cast<int>(cmd_type),
291 static_cast<int>(additional_param),
292 PolicySerializable(policyPrivateKey),
293 PolicySerializable(policyPublicKey),
296 }, [&observer](int error){ observer->ReceivedError(error); } );
299 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
302 throw std::invalid_argument("Empty observer");