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