2a37c24c07c33b128a9f88fce6716829d1ae5556
[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             if(!e || e->empty())
252                 return observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
253             rawCertChain.push_back(e->getDER());
254         }
255
256         m_counter++;
257         auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
258
259         thread()->sendMessage(AsyncRequest(observer,
260                                            SERVICE_SOCKET_OCSP,
261                                            send.Pop(),
262                                            m_counter));
263     }, [&observer](int error){ observer->ReceivedError(error); } );
264 }
265
266 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
267                                        const Alias& alias,
268                                        const Label& accessor,
269                                        PermissionMask permissionMask)
270 {
271     observerCheck(observer);
272     if (alias.empty() || accessor.empty()) {
273         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
274         return;
275     }
276     try_catch_async([&] {
277         AliasSupport helper(alias);
278         sendToStorage(observer,
279                       static_cast<int>(LogicCommand::SET_PERMISSION),
280                       m_counter,
281                       helper.getName(),
282                       helper.getLabel(),
283                       accessor,
284                       permissionMask);
285     }, [&observer](int error){ observer->ReceivedError(error); } );
286 }
287
288 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
289                                                   DataType dataType)
290 {
291     observerCheck(observer);
292     try_catch_async([&] {
293         sendToStorage(observer,
294                       static_cast<int>(LogicCommand::GET_LIST),
295                       m_counter,
296                       static_cast<int>(dataType));
297     }, [&observer](int error){ observer->ReceivedError(error); } );
298 }
299
300 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
301                                        const KeyType key_type,
302                                        const int     additional_param,
303                                        const Alias  &privateKeyAlias,
304                                        const Alias  &publicKeyAlias,
305                                        const Policy &policyPrivateKey,
306                                        const Policy &policyPublicKey)
307 {
308     observerCheck(observer);
309     if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
310         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
311         return;
312     }
313     // input type check
314     CryptoAlgorithm keyGenAlgorithm;
315     switch(key_type)
316     {
317         case KeyType::KEY_RSA_PUBLIC:
318         case KeyType::KEY_RSA_PRIVATE:
319             keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
320             keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
321             break;
322
323         case KeyType::KEY_DSA_PUBLIC:
324         case KeyType::KEY_DSA_PRIVATE:
325             keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
326             keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
327             break;
328
329         case KeyType::KEY_ECDSA_PUBLIC:
330         case KeyType::KEY_ECDSA_PRIVATE:
331             keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
332             keyGenAlgorithm.setParam(ParamName::GEN_EC, additional_param);
333             break;
334
335         default:
336             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
337             return;
338     }
339
340     try_catch_async([&] {
341         AliasSupport prvHelper(privateKeyAlias);
342         AliasSupport pubHelper(publicKeyAlias);
343         sendToStorage(observer,
344                       static_cast<int>(LogicCommand::CREATE_KEY_PAIR),
345                       m_counter,
346                       CryptoAlgorithmSerializable(keyGenAlgorithm),
347                       PolicySerializable(policyPrivateKey),
348                       PolicySerializable(policyPublicKey),
349                       prvHelper.getName(),
350                       prvHelper.getLabel(),
351                       pubHelper.getName(),
352                       pubHelper.getLabel());
353     }, [&observer](int error){ observer->ReceivedError(error); } );
354 }
355
356 void ManagerAsync::Impl::createKeyAES(const ManagerAsync::ObserverPtr& observer,
357                                       const size_t  size,
358                                       const Alias  &keyAlias,
359                                       const Policy &policyKey)
360 {
361     observerCheck(observer);
362     if (keyAlias.empty()) {
363         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
364         return;
365     }
366
367     try_catch_async([&] {
368         AliasSupport aliasHelper(keyAlias);
369         sendToStorage(observer,
370                       static_cast<int>(LogicCommand::CREATE_KEY_AES),
371                       m_counter,
372                       static_cast<int>(size),
373                       PolicySerializable(policyKey),
374                       aliasHelper.getName(),
375                       aliasHelper.getLabel());
376     }, [&observer](int error){ observer->ReceivedError(error); } );
377 }
378
379 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
380 {
381     if(!observer)
382         throw std::invalid_argument("Empty observer");
383 }
384
385 } // namespace CKM