Add PKCS12 support.
[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, DBDataType(key->getType()), key->getDER(), policy);
54     } Catch(DBDataType::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, DBDataType::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, DBDataType::BINARY_DATA, data, policy);
83 }
84
85 void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr& observer,
86                                         const Alias& alias,
87                                         DBDataType 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                                        DBDataType 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 {
167     observerCheck(observer);
168     if (alias.empty()) {
169         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
170         return;
171     }
172     try_catch_async([&] {
173         AliasSupport helper(alias);
174         sendToStorage(observer,
175                       static_cast<int>(LogicCommand::GET_PKCS12),
176                       m_counter,
177                       helper.getName(),
178                       helper.getLabel());
179     }, [&observer](int error){ observer->ReceivedError(error); } );
180 }
181
182 void ManagerAsync::Impl::createSignature(const ObserverPtr& observer,
183                                          const Alias& privateKeyAlias,
184                                          const Password& password,
185                                          const RawBuffer& message,
186                                          const HashAlgorithm hash,
187                                          const RSAPaddingAlgorithm padding)
188 {
189     observerCheck(observer);
190     if (privateKeyAlias.empty() || message.empty()) {
191         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
192         return;
193     }
194     try_catch_async([&] {
195         AliasSupport helper(privateKeyAlias);
196         sendToStorage(observer,
197                       static_cast<int>(LogicCommand::CREATE_SIGNATURE),
198                       m_counter,
199                       helper.getName(),
200                       helper.getLabel(),
201                       password,
202                       message,
203                       static_cast<int>(hash),
204                       static_cast<int>(padding));
205     }, [&observer](int error) {observer->ReceivedError(error);});
206 }
207
208 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
209                                          const Alias& publicKeyOrCertAlias,
210                                          const Password& password,
211                                          const RawBuffer& message,
212                                          const RawBuffer& signature,
213                                          const HashAlgorithm hash,
214                                          const RSAPaddingAlgorithm padding)
215 {
216     observerCheck(observer);
217     if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
218         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
219         return;
220     }
221     try_catch_async([&] {
222         AliasSupport helper(publicKeyOrCertAlias);
223         sendToStorage(observer,
224                       static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
225                       m_counter,
226                       helper.getName(),
227                       helper.getLabel(),
228                       password,
229                       message,
230                       signature,
231                       static_cast<int>(hash),
232                       static_cast<int>(padding));
233     }, [&observer](int error){ observer->ReceivedError(error); } );
234 }
235
236 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
237                                    const CertificateShPtrVector& certificateChainVector)
238 {
239     observerCheck(observer);
240     if (certificateChainVector.empty()) {
241         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
242         return;
243     }
244     try_catch_async([&] {
245         RawBufferVector rawCertChain;
246         for (auto &e: certificateChainVector) {
247             rawCertChain.push_back(e->getDER());
248         }
249
250         m_counter++;
251         auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
252
253         thread()->sendMessage(AsyncRequest(observer,
254                                            SERVICE_SOCKET_OCSP,
255                                            send.Pop(),
256                                            m_counter));
257     }, [&observer](int error){ observer->ReceivedError(error); } );
258 }
259
260 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
261                                          const Alias& alias,
262                                          const Label& accessor,
263                                          Permission newPermission)
264 {
265     observerCheck(observer);
266     if (alias.empty() || accessor.empty()) {
267         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
268         return;
269     }
270     try_catch_async([&] {
271         AliasSupport helper(alias);
272         sendToStorage(observer,
273                       static_cast<int>(LogicCommand::SET_PERMISSION),
274                       m_counter,
275                       helper.getName(),
276                       helper.getLabel(),
277                       accessor,
278                       static_cast<int>(newPermission));
279     }, [&observer](int error){ observer->ReceivedError(error); } );
280 }
281
282 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
283                                                   DBDataType dataType)
284 {
285     observerCheck(observer);
286     try_catch_async([&] {
287         sendToStorage(observer,
288                       static_cast<int>(LogicCommand::GET_LIST),
289                       m_counter,
290                       static_cast<int>(dataType));
291     }, [&observer](int error){ observer->ReceivedError(error); } );
292 }
293
294 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
295                                        const KeyType key_type,
296                                        const int     additional_param,
297                                        const Alias  &privateKeyAlias,
298                                        const Alias  &publicKeyAlias,
299                                        const Policy &policyPrivateKey,
300                                        const Policy &policyPublicKey)
301 {
302     observerCheck(observer);
303     if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
304         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
305         return;
306     }
307     // input type check
308     LogicCommand cmd_type;
309     switch(key_type)
310     {
311         case KeyType::KEY_RSA_PUBLIC:
312         case KeyType::KEY_RSA_PRIVATE:
313             cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
314             break;
315
316         case KeyType::KEY_DSA_PUBLIC:
317         case KeyType::KEY_DSA_PRIVATE:
318             cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
319             break;
320
321         case KeyType::KEY_ECDSA_PUBLIC:
322         case KeyType::KEY_ECDSA_PRIVATE:
323             cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
324             break;
325
326         default:
327             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
328             return;
329     }
330
331     try_catch_async([&] {
332         AliasSupport prvHelper(privateKeyAlias);
333         AliasSupport pubHelper(publicKeyAlias);
334         sendToStorage(observer,
335                       static_cast<int>(cmd_type),
336                       m_counter,
337                       static_cast<int>(additional_param),
338                       PolicySerializable(policyPrivateKey),
339                       PolicySerializable(policyPublicKey),
340                       prvHelper.getName(),
341                       prvHelper.getLabel(),
342                       pubHelper.getName(),
343                       pubHelper.getLabel());
344     }, [&observer](int error){ observer->ReceivedError(error); } );
345 }
346
347 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
348 {
349     if(!observer)
350         throw std::invalid_argument("Empty observer");
351 }
352
353 } // namespace CKM