Update parameter list API
[platform/core/security/key-manager.git] / src / manager / client-async / client-manager-async-impl.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       client-manager-async-impl.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <stdexcept>
23
24 #include <ckm/ckm-error.h>
25 #include <message-buffer.h>
26 #include <client-common.h>
27
28 #include <client-manager-async-impl.h>
29
30 namespace CKM {
31
32 int ManagerAsync::Impl::m_counter = 0;
33
34 ManagerAsync::Impl::Impl()
35 {
36 }
37
38 ManagerAsync::Impl::~Impl()
39 {
40 }
41
42 void ManagerAsync::Impl::saveKey(const ObserverPtr& observer,
43                            const Alias& alias,
44                            const KeyShPtr& key,
45                            const Policy& policy)
46 {
47     observerCheck(observer);
48     if (alias.empty() || !key) {
49         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
50         return;
51     }
52     Try {
53         saveBinaryData(observer, alias, DataType(key->getType()), key->getDER(), policy);
54     } Catch(DataType::Exception::Base) {
55         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
56     }
57 }
58
59 void ManagerAsync::Impl::saveCertificate(const ObserverPtr& observer,
60                                    const Alias& alias,
61                                    const CertificateShPtr& cert,
62                                    const Policy& policy)
63 {
64     observerCheck(observer);
65     if (alias.empty() || !cert) {
66         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
67         return;
68     }
69     saveBinaryData(observer, alias, DataType::CERTIFICATE, cert->getDER(), policy);
70 }
71
72 void ManagerAsync::Impl::saveData(const ObserverPtr& observer,
73                             const Alias& alias,
74                             const RawBuffer& data,
75                             const Policy& policy)
76 {
77     observerCheck(observer);
78     if (alias.empty() || data.empty()) {
79         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
80         return;
81     }
82     saveBinaryData(observer, alias, DataType::BINARY_DATA, data, policy);
83 }
84
85 void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr& observer,
86                                         const Alias& alias,
87                                         DataType dataType,
88                                         const RawBuffer& rawData,
89                                         const Policy& policy)
90 {
91     try_catch_async([&] {
92         AliasSupport helper(alias);
93         sendToStorage(observer,
94                       static_cast<int>(LogicCommand::SAVE),
95                       m_counter,
96                       static_cast<int>(dataType),
97                       helper.getName(),
98                       helper.getLabel(),
99                       rawData,
100                       PolicySerializable(policy));
101     }, [&observer](int error){ observer->ReceivedError(error); } );
102 }
103
104 void ManagerAsync::Impl::savePKCS12(const ManagerAsync::ObserverPtr& observer,
105                                     const Alias &alias,
106                                     const PKCS12ShPtr &pkcs,
107                                     const Policy &keyPolicy,
108                                     const Policy &certPolicy)
109 {
110     try_catch_async([&] {
111         AliasSupport helper(alias);
112         sendToStorage(observer,
113                       static_cast<int>(LogicCommand::SAVE_PKCS12),
114                       m_counter,
115                       helper.getName(),
116                       helper.getLabel(),
117                       PKCS12Serializable(*pkcs.get()),
118                       PolicySerializable(keyPolicy),
119                       PolicySerializable(certPolicy));
120
121     }, [&observer](int error){ observer->ReceivedError(error); } );
122 }
123
124 void ManagerAsync::Impl::removeAlias(const ManagerAsync::ObserverPtr& observer,
125                                      const Alias& alias)
126 {
127     observerCheck(observer);
128     if (alias.empty()) {
129         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
130         return;
131     }
132     try_catch_async([&] {
133         AliasSupport helper(alias);
134         sendToStorage(observer,
135                       static_cast<int>(LogicCommand::REMOVE),
136                       m_counter,
137                       helper.getName(),
138                       helper.getLabel());
139     }, [&observer](int error){ observer->ReceivedError(error); } );
140 }
141
142 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
143                                        const Alias &alias,
144                                        DataType sendDataType,
145                                        const Password &password)
146 {
147     observerCheck(observer);
148     if (alias.empty()) {
149         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
150         return;
151     }
152     try_catch_async([&] {
153         AliasSupport helper(alias);
154         sendToStorage(observer,
155                       static_cast<int>(LogicCommand::GET),
156                       m_counter,
157                       static_cast<int>(sendDataType),
158                       helper.getName(),
159                       helper.getLabel(),
160                       password);
161     }, [&observer](int error){ observer->ReceivedError(error); } );
162 }
163
164 void ManagerAsync::Impl::getPKCS12(const ManagerAsync::ObserverPtr& observer,
165                                    const Alias &alias,
166                                    const Password &passwordKey,
167                                    const Password &passwordCert)
168 {
169     observerCheck(observer);
170     if (alias.empty()) {
171         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
172         return;
173     }
174     try_catch_async([&] {
175         AliasSupport helper(alias);
176         sendToStorage(observer,
177                       static_cast<int>(LogicCommand::GET_PKCS12),
178                       m_counter,
179                       helper.getName(),
180                       helper.getLabel(),
181                       passwordKey,
182                       passwordCert);
183     }, [&observer](int error){ observer->ReceivedError(error); } );
184 }
185
186 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
187                                          const Alias& privateKeyAlias,
188                                          const Password& password,
189                                          const RawBuffer& message,
190                                          const HashAlgorithm hash,
191                                          const RSAPaddingAlgorithm padding)
192 {
193     observerCheck(observer);
194     if (privateKeyAlias.empty() || message.empty()) {
195         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
196         return;
197     }
198     try_catch_async([&] {
199         AliasSupport helper(privateKeyAlias);
200         sendToStorage(observer,
201                       static_cast<int>(LogicCommand::CREATE_SIGNATURE),
202                       m_counter,
203                       helper.getName(),
204                       helper.getLabel(),
205                       password,
206                       message,
207                       static_cast<int>(hash),
208                       static_cast<int>(padding));
209     }, [&observer](int error) {observer->ReceivedError(error);});
210 }
211
212 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
213                                          const Alias& publicKeyOrCertAlias,
214                                          const Password& password,
215                                          const RawBuffer& message,
216                                          const RawBuffer& signature,
217                                          const HashAlgorithm hash,
218                                          const RSAPaddingAlgorithm padding)
219 {
220     observerCheck(observer);
221     if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
222         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
223         return;
224     }
225     try_catch_async([&] {
226         AliasSupport helper(publicKeyOrCertAlias);
227         sendToStorage(observer,
228                       static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
229                       m_counter,
230                       helper.getName(),
231                       helper.getLabel(),
232                       password,
233                       message,
234                       signature,
235                       static_cast<int>(hash),
236                       static_cast<int>(padding));
237     }, [&observer](int error){ observer->ReceivedError(error); } );
238 }
239
240 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
241                                    const CertificateShPtrVector& certificateChainVector)
242 {
243     observerCheck(observer);
244     if (certificateChainVector.empty()) {
245         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
246         return;
247     }
248     try_catch_async([&] {
249         RawBufferVector rawCertChain;
250         for (auto &e: certificateChainVector) {
251             rawCertChain.push_back(e->getDER());
252         }
253
254         m_counter++;
255         auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
256
257         thread()->sendMessage(AsyncRequest(observer,
258                                            SERVICE_SOCKET_OCSP,
259                                            send.Pop(),
260                                            m_counter));
261     }, [&observer](int error){ observer->ReceivedError(error); } );
262 }
263
264 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
265                                        const Alias& alias,
266                                        const Label& accessor,
267                                        PermissionMask permissionMask)
268 {
269     observerCheck(observer);
270     if (alias.empty() || accessor.empty()) {
271         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
272         return;
273     }
274     try_catch_async([&] {
275         AliasSupport helper(alias);
276         sendToStorage(observer,
277                       static_cast<int>(LogicCommand::SET_PERMISSION),
278                       m_counter,
279                       helper.getName(),
280                       helper.getLabel(),
281                       accessor,
282                       permissionMask);
283     }, [&observer](int error){ observer->ReceivedError(error); } );
284 }
285
286 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
287                                                   DataType dataType)
288 {
289     observerCheck(observer);
290     try_catch_async([&] {
291         sendToStorage(observer,
292                       static_cast<int>(LogicCommand::GET_LIST),
293                       m_counter,
294                       static_cast<int>(dataType));
295     }, [&observer](int error){ observer->ReceivedError(error); } );
296 }
297
298 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
299                                        const KeyType key_type,
300                                        const int     additional_param,
301                                        const Alias  &privateKeyAlias,
302                                        const Alias  &publicKeyAlias,
303                                        const Policy &policyPrivateKey,
304                                        const Policy &policyPublicKey)
305 {
306     observerCheck(observer);
307     if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
308         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
309         return;
310     }
311     // input type check
312     CryptoAlgorithm keyGenAlgorithm;
313     switch(key_type)
314     {
315         case KeyType::KEY_RSA_PUBLIC:
316         case KeyType::KEY_RSA_PRIVATE:
317             keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
318             keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
319             break;
320
321         case KeyType::KEY_DSA_PUBLIC:
322         case KeyType::KEY_DSA_PRIVATE:
323             keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
324             keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
325             break;
326
327         case KeyType::KEY_ECDSA_PUBLIC:
328         case KeyType::KEY_ECDSA_PRIVATE:
329             keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
330             keyGenAlgorithm.setParam(ParamName::GEN_EC, additional_param);
331             break;
332
333         default:
334             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
335             return;
336     }
337
338     try_catch_async([&] {
339         AliasSupport prvHelper(privateKeyAlias);
340         AliasSupport pubHelper(publicKeyAlias);
341         sendToStorage(observer,
342                       static_cast<int>(LogicCommand::CREATE_KEY_PAIR),
343                       m_counter,
344                       CryptoAlgorithmSerializable(keyGenAlgorithm),
345                       PolicySerializable(policyPrivateKey),
346                       PolicySerializable(policyPublicKey),
347                       prvHelper.getName(),
348                       prvHelper.getLabel(),
349                       pubHelper.getName(),
350                       pubHelper.getLabel());
351     }, [&observer](int error){ observer->ReceivedError(error); } );
352 }
353
354 void ManagerAsync::Impl::createKeyAES(const ManagerAsync::ObserverPtr& observer,
355                                       const size_t  size,
356                                       const Alias  &keyAlias,
357                                       const Policy &policyKey)
358 {
359     observerCheck(observer);
360     if (keyAlias.empty()) {
361         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
362         return;
363     }
364
365     try_catch_async([&] {
366         AliasSupport aliasHelper(keyAlias);
367         sendToStorage(observer,
368                       static_cast<int>(LogicCommand::CREATE_KEY_AES),
369                       m_counter,
370                       static_cast<int>(size),
371                       PolicySerializable(policyKey),
372                       aliasHelper.getName(),
373                       aliasHelper.getLabel());
374     }, [&observer](int error){ observer->ReceivedError(error); } );
375 }
376
377 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
378 {
379     if(!observer)
380         throw std::invalid_argument("Empty observer");
381 }
382
383 } // namespace CKM