DBCrypto access control re-factor: access control moved into additional layer.
[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.getName(),
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.getName(),
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         AliasSupport helper(privateKeyAlias);
154         sendToStorage(observer,
155                       static_cast<int>(LogicCommand::CREATE_SIGNATURE),
156                       m_counter,
157                       helper.getName(),
158                       helper.getLabel(),
159                       password,
160                       message,
161                       static_cast<int>(hash),
162                       static_cast<int>(padding));
163     }, [&observer](int error) {observer->ReceivedError(error);});
164 }
165
166 void ManagerAsync::Impl::verifySignature(const ObserverPtr& observer,
167                                          const Alias& publicKeyOrCertAlias,
168                                          const Password& password,
169                                          const RawBuffer& message,
170                                          const RawBuffer& signature,
171                                          const HashAlgorithm hash,
172                                          const RSAPaddingAlgorithm padding)
173 {
174     observerCheck(observer);
175     if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) {
176         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
177         return;
178     }
179     try_catch_async([&] {
180         AliasSupport helper(publicKeyOrCertAlias);
181         sendToStorage(observer,
182                       static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
183                       m_counter,
184                       helper.getName(),
185                       helper.getLabel(),
186                       password,
187                       message,
188                       signature,
189                       static_cast<int>(hash),
190                       static_cast<int>(padding));
191     }, [&observer](int error){ observer->ReceivedError(error); } );
192 }
193
194 void ManagerAsync::Impl::ocspCheck(const ObserverPtr& observer,
195                                    const CertificateShPtrVector& certificateChainVector)
196 {
197     observerCheck(observer);
198     if (certificateChainVector.empty()) {
199         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
200         return;
201     }
202     try_catch_async([&] {
203         RawBufferVector rawCertChain;
204         for (auto &e: certificateChainVector) {
205             rawCertChain.push_back(e->getDER());
206         }
207
208         m_counter++;
209         auto send = MessageBuffer::Serialize(m_counter, rawCertChain);
210
211         thread()->sendMessage(AsyncRequest(observer,
212                                            SERVICE_SOCKET_OCSP,
213                                            send.Pop(),
214                                            m_counter));
215     }, [&observer](int error){ observer->ReceivedError(error); } );
216 }
217
218 void ManagerAsync::Impl::setPermission(const ObserverPtr& observer,
219                                          const Alias& alias,
220                                          const Label& accessor,
221                                          Permission newPermission)
222 {
223     observerCheck(observer);
224     if (alias.empty() || accessor.empty()) {
225         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
226         return;
227     }
228     try_catch_async([&] {
229         sendToStorage(observer,
230                       static_cast<int>(LogicCommand::SET_PERMISSION),
231                       m_counter,
232                       alias,
233                       accessor,
234                       static_cast<int>(newPermission));
235     }, [&observer](int error){ observer->ReceivedError(error); } );
236 }
237
238 void ManagerAsync::Impl::getBinaryDataAliasVector(const ManagerAsync::ObserverPtr& observer,
239                                                   DBDataType dataType)
240 {
241     observerCheck(observer);
242     try_catch_async([&] {
243         sendToStorage(observer,
244                       static_cast<int>(LogicCommand::GET_LIST),
245                       m_counter,
246                       static_cast<int>(dataType));
247     }, [&observer](int error){ observer->ReceivedError(error); } );
248 }
249
250 void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr& observer,
251                                        const KeyType key_type,
252                                        const int     additional_param,
253                                        const Alias  &privateKeyAlias,
254                                        const Alias  &publicKeyAlias,
255                                        const Policy &policyPrivateKey,
256                                        const Policy &policyPublicKey)
257 {
258     observerCheck(observer);
259     if (privateKeyAlias.empty() || publicKeyAlias.empty()) {
260         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
261         return;
262     }
263     // input type check
264     LogicCommand cmd_type;
265     switch(key_type)
266     {
267         case KeyType::KEY_RSA_PUBLIC:
268         case KeyType::KEY_RSA_PRIVATE:
269             cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
270             break;
271
272         case KeyType::KEY_DSA_PUBLIC:
273         case KeyType::KEY_DSA_PRIVATE:
274             cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
275             break;
276
277         case KeyType::KEY_ECDSA_PUBLIC:
278         case KeyType::KEY_ECDSA_PRIVATE:
279             cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
280             break;
281
282         default:
283             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
284             return;
285     }
286
287     try_catch_async([&] {
288         sendToStorage(observer,
289                       static_cast<int>(cmd_type),
290                       m_counter,
291                       static_cast<int>(additional_param),
292                       PolicySerializable(policyPrivateKey),
293                       PolicySerializable(policyPublicKey),
294                       privateKeyAlias,
295                       publicKeyAlias);
296     }, [&observer](int error){ observer->ReceivedError(error); } );
297 }
298
299 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
300 {
301     if(!observer)
302         throw std::invalid_argument("Empty observer");
303 }
304
305 } // namespace CKM