Implementation of remaining async 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     saveBinaryData(observer, alias, toDBDataType(key->getType()), key->getDER(), policy);
53 }
54
55 void ManagerAsync::Impl::saveCertificate(const ObserverPtr& observer,
56                                    const Alias& alias,
57                                    const CertificateShPtr& cert,
58                                    const Policy& policy)
59 {
60     observerCheck(observer);
61     if (alias.empty() || !cert) {
62         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
63         return;
64     }
65     saveBinaryData(observer, alias, DBDataType::CERTIFICATE, cert->getDER(), policy);
66 }
67
68 void ManagerAsync::Impl::saveData(const ObserverPtr& observer,
69                             const Alias& alias,
70                             const RawBuffer& data,
71                             const Policy& policy)
72 {
73     observerCheck(observer);
74     if (alias.empty() || data.empty()) {
75         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
76         return;
77     }
78     saveBinaryData(observer, alias, DBDataType::BINARY_DATA, data, policy);
79 }
80
81 void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr& observer,
82                                         const Alias& alias,
83                                         DBDataType dataType,
84                                         const RawBuffer& rawData,
85                                         const Policy& policy)
86 {
87     try_catch_async([&] {
88         sendToStorage(observer,
89                       static_cast<int>(LogicCommand::SAVE),
90                       m_counter,
91                       static_cast<int>(dataType),
92                       alias,
93                       rawData,
94                       PolicySerializable(policy));
95     }, [&observer](int error){ observer->ReceivedError(error); } );
96 }
97
98 void ManagerAsync::Impl::removeBinaryData(const ManagerAsync::ObserverPtr& observer,
99                                           const Alias& alias,
100                                           DBDataType dataType)
101 {
102     observerCheck(observer);
103     if (alias.empty()) {
104         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
105         return;
106     }
107     try_catch_async([&] {
108         sendToStorage(observer,
109                       static_cast<int>(LogicCommand::REMOVE),
110                       m_counter,
111                       static_cast<int>(dataType),
112                       alias);
113     }, [&observer](int error){ observer->ReceivedError(error); } );
114 }
115
116 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
117                                        const Alias &alias,
118                                        DBDataType sendDataType,
119                                        const Password &password)
120 {
121     observerCheck(observer);
122     if (alias.empty()) {
123         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
124         return;
125     }
126     try_catch_async([&] {
127         sendToStorage(observer,
128                       static_cast<int>(LogicCommand::GET),
129                       m_counter,
130                       static_cast<int>(sendDataType),
131                       alias,
132                       password);
133     }, [&observer](int error){ observer->ReceivedError(error); } );
134 }
135
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)
142 {
143     observerCheck(observer);
144     if (privateKeyAlias.empty() || message.empty()) {
145         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
146         return;
147     }
148     try_catch_async([&] {
149         sendToStorage(observer,
150                       static_cast<int>(LogicCommand::CREATE_SIGNATURE),
151                       m_counter,
152                       privateKeyAlias,
153                       password,
154                       message,
155                       static_cast<int>(hash),
156                       static_cast<int>(padding));
157     }, [&observer](int error) {observer->ReceivedError(error);});
158 }
159
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)
167 {
168     observerCheck(observer);
169     if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
170         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
171         return;
172     }
173     try_catch_async([&] {
174         sendToStorage(observer,
175                       static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
176                       m_counter,
177                       publicKeyOrCertAlias,
178                       password,
179                       message,
180                       signature,
181                       static_cast<int>(hash),
182                       static_cast<int>(padding));
183     }, [&observer](int error){ observer->ReceivedError(error); } );
184 }
185
186 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
187                                    const CertificateShPtrVector& certificateChainVector)
188 {
189     observerCheck(observer);
190     if (certificateChainVector.empty()) {
191         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
192         return;
193     }
194     try_catch_async([&] {
195         RawBufferVector rawCertChain;
196         for (auto &e: certificateChainVector) {
197             rawCertChain.push_back(e->getDER());
198         }
199
200         m_counter++;
201         auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
202
203         thread()->sendMessage(AsyncRequest(observer,
204                                            SERVICE_SOCKET_OCSP,
205                                            send.Pop(),
206                                            m_counter));
207     }, [&observer](int error){ observer->ReceivedError(error); } );
208 }
209
210 void ManagerAsync::Impl::allowAccess(const ObserverPtr& observer,
211                                      const std::string& alias,
212                                      const std::string& accessor,
213                                      AccessRight granted)
214 {
215     observerCheck(observer);
216     if (alias.empty() || accessor.empty()) {
217         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
218         return;
219     }
220     try_catch_async([&] {
221         sendToStorage(observer,
222                       static_cast<int>(LogicCommand::ALLOW_ACCESS),
223                       m_counter,
224                       alias,
225                       accessor,
226                       static_cast<int>(granted));
227     }, [&observer](int error){ observer->ReceivedError(error); } );
228 }
229
230 void ManagerAsync::Impl::denyAccess(const ObserverPtr& observer,
231                                     const std::string& alias,
232                                     const std::string& accessor)
233 {
234     observerCheck(observer);
235     if (alias.empty() || accessor.empty()) {
236         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
237         return;
238     }
239     try_catch_async([&] {
240         sendToStorage(observer,
241                       static_cast<int>(LogicCommand::DENY_ACCESS),
242                       m_counter,
243                       alias,
244                       accessor);
245     }, [&observer](int error){ observer->ReceivedError(error); } );
246 }
247
248 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
249                                                   DBDataType dataType)
250 {
251     observerCheck(observer);
252     try_catch_async([&] {
253         sendToStorage(observer,
254                       static_cast<int>(LogicCommand::GET_LIST),
255                       m_counter,
256                       static_cast<int>(dataType));
257     }, [&observer](int error){ observer->ReceivedError(error); } );
258 }
259
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)
267 {
268     observerCheck(observer);
269     if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
270         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
271         return;
272     }
273     // input type check
274     LogicCommand cmd_type;
275     switch(key_type)
276     {
277         case KeyType::KEY_RSA_PUBLIC:
278         case KeyType::KEY_RSA_PRIVATE:
279             cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
280             break;
281
282         case KeyType::KEY_DSA_PUBLIC:
283         case KeyType::KEY_DSA_PRIVATE:
284             cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
285             break;
286
287         case KeyType::KEY_ECDSA_PUBLIC:
288         case KeyType::KEY_ECDSA_PRIVATE:
289             cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
290             break;
291
292         default:
293             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
294             return;
295     }
296
297     try_catch_async([&] {
298         sendToStorage(observer,
299                       static_cast<int>(cmd_type),
300                       m_counter,
301                       static_cast<int>(additional_param),
302                       PolicySerializable(policyPrivateKey),
303                       PolicySerializable(policyPublicKey),
304                       privateKeyAlias,
305                       publicKeyAlias);
306     }, [&observer](int error){ observer->ReceivedError(error); } );
307 }
308
309 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
310 {
311     if(!observer)
312         throw std::invalid_argument("Empty observer");
313 }
314
315 } // namespace CKM