Serialize AES key size as int
[platform/core/security/key-manager.git] / src / manager / client-async / client-manager-async-impl.cpp
1 /*
2  *  Copyright (c) 2014-2020 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 #include <exception.h>
28
29 #include <client-manager-async-impl.h>
30
31 namespace CKM {
32 namespace {
33
34 template <class T>
35 bool paramEmpty(const T &t) {
36         return t.empty();
37 }
38
39 template <class T>
40 bool paramEmpty(const std::shared_ptr<T> &t) {
41         return !t || t->empty();
42 }
43
44 template <class...T>
45 bool anyParamEmpty(const ManagerAsync::ObserverPtr &observer, const T&...t)
46 {
47         ManagerAsync::Impl::observerCheck(observer);
48         if ((... || paramEmpty(t))) {
49                 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
50                 return true;
51         }
52         return false;
53 }
54
55 } // namespace
56
57 void ManagerAsync::Impl::saveKey(const ObserverPtr &observer,
58                                                                  const Alias &alias,
59                                                                  const KeyShPtr &key,
60                                                                  const Policy &policy)
61 {
62         if (anyParamEmpty(observer, alias, key))
63                 return;
64
65         try {
66                 saveBinaryData(observer, alias, DataType(key->getType()), key->getDER(),
67                                            policy);
68         } catch (const Exc::Exception &e) {
69                 observer->ReceivedError(e.error());
70         }
71 }
72
73 void ManagerAsync::Impl::saveCertificate(const ObserverPtr &observer,
74                 const Alias &alias,
75                 const CertificateShPtr &cert,
76                 const Policy &policy)
77 {
78         if (anyParamEmpty(observer, alias, cert))
79                 return;
80
81         saveBinaryData(observer, alias, DataType::CERTIFICATE, cert->getDER(), policy);
82 }
83
84 void ManagerAsync::Impl::saveData(const ObserverPtr &observer,
85                                                                   const Alias &alias,
86                                                                   const RawBuffer &data,
87                                                                   const Policy &policy)
88 {
89         if (anyParamEmpty(observer, alias, data))
90                 return;
91
92         saveBinaryData(observer, alias, DataType::BINARY_DATA, data, policy);
93 }
94
95 void ManagerAsync::Impl::saveBinaryData(const ObserverPtr
96                                                                                 &observer,
97                                                                                 const Alias &alias,
98                                                                                 DataType dataType,
99                                                                                 const RawBuffer &rawData,
100                                                                                 const Policy &policy)
101 {
102         tryCatch(*observer, [&] {
103                 AliasSupport helper(alias);
104
105                 sendToStorage(LogicCommand::SAVE, observer,
106                                           dataType, helper.getName(), helper.getOwner(), rawData,
107                                           PolicySerializable(policy));
108         });
109 }
110
111 void ManagerAsync::Impl::savePKCS12(const ObserverPtr &observer,
112                                                                         const Alias &alias,
113                                                                         const PKCS12ShPtr &pkcs,
114                                                                         const Policy &keyPolicy,
115                                                                         const Policy &certPolicy)
116 {
117         tryCatch(*observer, [&] {
118                 AliasSupport helper(alias);
119                 sendToStorage(LogicCommand::SAVE_PKCS12, observer,
120                                           helper.getName(), helper.getOwner(), PKCS12Serializable(*pkcs.get()),
121                                           PolicySerializable(keyPolicy), PolicySerializable(certPolicy));
122         });
123 }
124
125 void ManagerAsync::Impl::removeAlias(const ObserverPtr &observer,
126                                                                          const Alias &alias)
127 {
128         if (anyParamEmpty(observer, alias))
129                 return;
130
131         tryCatch(*observer, [&] {
132                 AliasSupport helper(alias);
133                 sendToStorage(LogicCommand::REMOVE, observer,
134                                           helper.getName(), helper.getOwner());
135         });
136 }
137
138 void ManagerAsync::Impl::getBinaryData(const ObserverPtr &observer,
139                                                                            const Alias &alias,
140                                                                            DataType sendDataType,
141                                                                            const Password &password)
142 {
143         if (anyParamEmpty(observer, alias))
144                 return;
145
146         tryCatch(*observer, [&] {
147                 AliasSupport helper(alias);
148
149                 sendToStorage(LogicCommand::GET, observer,
150                                           sendDataType, helper.getName(), helper.getOwner(), password);
151         });
152 }
153
154 void ManagerAsync::Impl::getPKCS12(const ObserverPtr &observer,
155                                                                    const Alias &alias,
156                                                                    const Password &passwordKey,
157                                                                    const Password &passwordCert)
158 {
159         if (anyParamEmpty(observer, alias))
160                 return;
161
162         tryCatch(*observer, [&] {
163                 AliasSupport helper(alias);
164
165                 sendToStorage(LogicCommand::GET_PKCS12, observer,
166                                           helper.getName(), helper.getOwner(), passwordKey, passwordCert);
167         });
168 }
169
170 void ManagerAsync::Impl::createSignature(const ObserverPtr &observer,
171                 const Alias &privateKeyAlias,
172                 const Password &password,
173                 const RawBuffer &message,
174                 const CryptoAlgorithm &cAlg)
175 {
176         if (anyParamEmpty(observer, privateKeyAlias, message))
177                 return;
178
179         tryCatch(*observer, [&] {
180                 AliasSupport helper(privateKeyAlias);
181                 sendToStorage(LogicCommand::CREATE_SIGNATURE, observer,
182                                           helper.getName(), helper.getOwner(), password, message,
183                                           CryptoAlgorithmSerializable(cAlg));
184         });
185 }
186
187 void ManagerAsync::Impl::verifySignature(const ObserverPtr &observer,
188                 const Alias &publicKeyOrCertAlias,
189                 const Password &password,
190                 const RawBuffer &message,
191                 const RawBuffer &signature,
192                 const CryptoAlgorithm &cAlg)
193 {
194         if (anyParamEmpty(observer, publicKeyOrCertAlias, message, signature))
195                 return;
196
197         tryCatch(*observer, [&] {
198                 AliasSupport helper(publicKeyOrCertAlias);
199
200                 sendToStorage(LogicCommand::VERIFY_SIGNATURE, observer,
201                                           helper.getName(), helper.getOwner(), password,
202                                           message, signature, CryptoAlgorithmSerializable(cAlg));
203         });
204 }
205
206 void ManagerAsync::Impl::ocspCheck(const ObserverPtr &observer,
207                                                                    const CertificateShPtrVector &certificateChainVector)
208 {
209         if (anyParamEmpty(observer, certificateChainVector))
210                 return;
211
212         tryCatch(*observer, [&] {
213                 RawBufferVector rawCertChain;
214
215                 for (auto &e : certificateChainVector) {
216                         if (!e || e->empty())
217                                 return observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
218
219                         rawCertChain.push_back(e->getDER());
220                 }
221
222                 m_counter++;
223                 auto send = SerializeMessage(m_counter, rawCertChain);
224
225                 thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_OCSP, std::move(send),
226                                                                                    m_counter, 0));
227         });
228 }
229
230 void ManagerAsync::Impl::setPermission(const ObserverPtr &observer,
231                                                                            const Alias &alias,
232                                                                            const ClientId &accessor,
233                                                                            PermissionMask permissionMask)
234 {
235         if (anyParamEmpty(observer, alias, accessor))
236                 return;
237
238         tryCatch(*observer, [&] {
239                 AliasSupport helper(alias);
240
241                 sendToStorage(LogicCommand::SET_PERMISSION, observer,
242                               helper.getName(), helper.getOwner(), accessor, permissionMask);
243         });
244 }
245
246 void ManagerAsync::Impl::getBinaryDataAliasVector(const ObserverPtr &observer,
247                                                                                                   DataType dataType)
248 {
249         observerCheck(observer);
250         tryCatch(*observer, [&] {
251                 sendToStorage(LogicCommand::GET_LIST, observer, dataType);
252         });
253 }
254
255 void ManagerAsync::Impl::createKeyPair(const ObserverPtr &observer,
256                                                                            const KeyType key_type,
257                                                                            const int     additional_param,
258                                                                            const Alias  &privateKeyAlias,
259                                                                            const Alias  &publicKeyAlias,
260                                                                            const Policy &policyPrivateKey,
261                                                                            const Policy &policyPublicKey)
262 {
263         if (anyParamEmpty(observer, privateKeyAlias, publicKeyAlias))
264                 return;
265
266         // input type check
267         CryptoAlgorithm keyGenAlgorithm;
268
269         switch (key_type) {
270         case KeyType::KEY_RSA_PUBLIC:
271         case KeyType::KEY_RSA_PRIVATE:
272                 keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
273                 keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
274                 break;
275
276         case KeyType::KEY_DSA_PUBLIC:
277         case KeyType::KEY_DSA_PRIVATE:
278                 keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
279                 keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
280                 break;
281
282         case KeyType::KEY_ECDSA_PUBLIC:
283         case KeyType::KEY_ECDSA_PRIVATE:
284                 keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
285                 keyGenAlgorithm.setParam(ParamName::GEN_EC, additional_param);
286                 break;
287
288         default:
289                 observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
290                 return;
291         }
292
293         tryCatch(*observer, [&] {
294                 AliasSupport prvHelper(privateKeyAlias);
295                 AliasSupport pubHelper(publicKeyAlias);
296
297                 sendToStorage(LogicCommand::CREATE_KEY_PAIR, observer,
298                                           CryptoAlgorithmSerializable(keyGenAlgorithm),
299                                           PolicySerializable(policyPrivateKey), PolicySerializable(policyPublicKey),
300                                           prvHelper.getName(), prvHelper.getOwner(), pubHelper.getName(),
301                                           pubHelper.getOwner());
302         });
303 }
304
305 void ManagerAsync::Impl::createKeyAES(const ObserverPtr &observer,
306                                                                           const int  size,
307                                                                           const Alias  &keyAlias,
308                                                                           const Policy &policyKey)
309 {
310         if (anyParamEmpty(observer, keyAlias))
311                 return;
312
313         tryCatch(*observer, [&] {
314                 AliasSupport aliasHelper(keyAlias);
315
316                 sendToStorage(LogicCommand::CREATE_KEY_AES, observer,
317                                           size, PolicySerializable(policyKey),
318                                           aliasHelper.getName(), aliasHelper.getOwner());
319         });
320 }
321
322 void ManagerAsync::Impl::observerCheck(const ObserverPtr &observer)
323 {
324         if (!observer)
325                 throw std::invalid_argument("Empty observer");
326 }
327
328 void ManagerAsync::Impl::crypt(
329         const ObserverPtr &observer,
330         const CryptoAlgorithm &algo,
331         const Alias &keyAlias,
332         const Password &password,
333         const RawBuffer &input,
334         bool encryption)
335 {
336         if (anyParamEmpty(observer, input, keyAlias))
337                 return;
338
339         tryCatch(*observer, [&] {
340                 AliasSupport helper(keyAlias);
341                 CryptoAlgorithmSerializable cas(algo);
342                 m_counter++;
343
344                 auto command = encryption ? EncryptionCommand::ENCRYPT : EncryptionCommand::DECRYPT;
345                 auto send = SerializeMessage(command, m_counter, cas, helper.getName(), helper.getOwner(),
346                                                                          password, input);
347
348                 thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_ENCRYPTION,
349                                                                                    std::move(send), m_counter, static_cast<int>(command)));
350         });
351 }
352
353 } // namespace CKM