Alias is not unique user-wide: (alias, label) pair is unique now.
[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         AliasSupport helper(alias);
109         sendToStorage(observer,
110                       static_cast<int>(LogicCommand::REMOVE),
111                       m_counter,
112                       static_cast<int>(dataType),
113                       helper.getAlias(),
114                       helper.getLabel());
115     }, [&observer](int error){ observer->ReceivedError(error); } );
116 }
117
118 void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr& observer,
119                                        const Alias &alias,
120                                        DBDataType sendDataType,
121                                        const Password &password)
122 {
123     observerCheck(observer);
124     if (alias.empty()) {
125         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
126         return;
127     }
128     try_catch_async([&] {
129         AliasSupport helper(alias);
130         sendToStorage(observer,
131                       static_cast<int>(LogicCommand::GET),
132                       m_counter,
133                       static_cast<int>(sendDataType),
134                       helper.getAlias(),
135                       helper.getLabel(),
136                       password);
137     }, [&observer](int error){ observer->ReceivedError(error); } );
138 }
139
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)
146 {
147     observerCheck(observer);
148     if (privateKeyAlias.empty() || message.empty()) {
149         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
150         return;
151     }
152     try_catch_async([&] {
153         sendToStorage(observer,
154                       static_cast<int>(LogicCommand::CREATE_SIGNATURE),
155                       m_counter,
156                       privateKeyAlias,
157                       password,
158                       message,
159                       static_cast<int>(hash),
160                       static_cast<int>(padding));
161     }, [&observer](int error) {observer->ReceivedError(error);});
162 }
163
164 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
165                                          const Alias& publicKeyOrCertAlias,
166                                          const Password& password,
167                                          const RawBuffer& message,
168                                          const RawBuffer& signature,
169                                          const HashAlgorithm hash,
170                                          const RSAPaddingAlgorithm padding)
171 {
172     observerCheck(observer);
173     if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
174         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
175         return;
176     }
177     try_catch_async([&] {
178         sendToStorage(observer,
179                       static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
180                       m_counter,
181                       publicKeyOrCertAlias,
182                       password,
183                       message,
184                       signature,
185                       static_cast<int>(hash),
186                       static_cast<int>(padding));
187     }, [&observer](int error){ observer->ReceivedError(error); } );
188 }
189
190 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
191                                    const CertificateShPtrVector& certificateChainVector)
192 {
193     observerCheck(observer);
194     if (certificateChainVector.empty()) {
195         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
196         return;
197     }
198     try_catch_async([&] {
199         RawBufferVector rawCertChain;
200         for (auto &e: certificateChainVector) {
201             rawCertChain.push_back(e->getDER());
202         }
203
204         m_counter++;
205         auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
206
207         thread()->sendMessage(AsyncRequest(observer,
208                                            SERVICE_SOCKET_OCSP,
209                                            send.Pop(),
210                                            m_counter));
211     }, [&observer](int error){ observer->ReceivedError(error); } );
212 }
213
214 void ManagerAsync::Impl::allowAccess(const ObserverPtr& observer,
215                                      const std::string& alias,
216                                      const std::string& accessor,
217                                      AccessRight granted)
218 {
219     observerCheck(observer);
220     if (alias.empty() || accessor.empty()) {
221         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
222         return;
223     }
224     try_catch_async([&] {
225         sendToStorage(observer,
226                       static_cast<int>(LogicCommand::ALLOW_ACCESS),
227                       m_counter,
228                       alias,
229                       accessor,
230                       static_cast<int>(granted));
231     }, [&observer](int error){ observer->ReceivedError(error); } );
232 }
233
234 void ManagerAsync::Impl::denyAccess(const ObserverPtr& observer,
235                                     const std::string& alias,
236                                     const std::string& accessor)
237 {
238     observerCheck(observer);
239     if (alias.empty() || accessor.empty()) {
240         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
241         return;
242     }
243     try_catch_async([&] {
244         sendToStorage(observer,
245                       static_cast<int>(LogicCommand::DENY_ACCESS),
246                       m_counter,
247                       alias,
248                       accessor);
249     }, [&observer](int error){ observer->ReceivedError(error); } );
250 }
251
252 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
253                                                   DBDataType dataType)
254 {
255     observerCheck(observer);
256     try_catch_async([&] {
257         sendToStorage(observer,
258                       static_cast<int>(LogicCommand::GET_LIST),
259                       m_counter,
260                       static_cast<int>(dataType));
261     }, [&observer](int error){ observer->ReceivedError(error); } );
262 }
263
264 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
265                                        const KeyType key_type,
266                                        const int     additional_param,
267                                        const Alias  &privateKeyAlias,
268                                        const Alias  &publicKeyAlias,
269                                        const Policy &policyPrivateKey,
270                                        const Policy &policyPublicKey)
271 {
272     observerCheck(observer);
273     if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
274         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
275         return;
276     }
277     // input type check
278     LogicCommand cmd_type;
279     switch(key_type)
280     {
281         case KeyType::KEY_RSA_PUBLIC:
282         case KeyType::KEY_RSA_PRIVATE:
283             cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
284             break;
285
286         case KeyType::KEY_DSA_PUBLIC:
287         case KeyType::KEY_DSA_PRIVATE:
288             cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
289             break;
290
291         case KeyType::KEY_ECDSA_PUBLIC:
292         case KeyType::KEY_ECDSA_PRIVATE:
293             cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
294             break;
295
296         default:
297             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
298             return;
299     }
300
301     try_catch_async([&] {
302         sendToStorage(observer,
303                       static_cast<int>(cmd_type),
304                       m_counter,
305                       static_cast<int>(additional_param),
306                       PolicySerializable(policyPrivateKey),
307                       PolicySerializable(policyPublicKey),
308                       privateKeyAlias,
309                       publicKeyAlias);
310     }, [&observer](int error){ observer->ReceivedError(error); } );
311 }
312
313 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
314 {
315     if(!observer)
316         throw std::invalid_argument("Empty observer");
317 }
318
319 } // namespace CKM