AES key creation API
[platform/core/security/key-manager.git] / src / manager / client / client-manager-impl.cpp
1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License
14  *
15  *
16  * @file        client-manager-impl.cpp
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version     1.0
19  * @brief       Manager implementation.
20  */
21 #include <openssl/evp.h>
22
23 #include <dpl/serialization.h>
24 #include <dpl/log/log.h>
25
26 #include <crypto-init.h>
27 #include <client-manager-impl.h>
28 #include <client-common.h>
29 #include <message-buffer.h>
30 #include <protocols.h>
31 #include <key-impl.h>
32 #include <certificate-impl.h>
33
34 namespace CKM {
35
36 namespace {
37 template <class T>
38 int getCertChain(
39     ServiceConnection & serviceConnection,
40     LogicCommand command,
41     int counter,
42     const CertificateShPtr &certificate,
43     const T &untrustedVector,
44     const T &trustedVector,
45     bool useTrustedSystemCertificates,
46     CertificateShPtrVector &certificateChainVector)
47 {
48     return try_catch([&] {
49
50         MessageBuffer recv;
51         auto send = MessageBuffer::Serialize(static_cast<int>(command),
52                                              counter,
53                                              certificate->getDER(),
54                                              untrustedVector,
55                                              trustedVector,
56                                              useTrustedSystemCertificates);
57
58         int retCode = serviceConnection.processRequest(send.Pop(), recv);
59         if (CKM_API_SUCCESS != retCode)
60             return retCode;
61
62         int retCommand;
63         int retCounter;
64         RawBufferVector rawBufferVector;
65         recv.Deserialize(retCommand, retCounter, retCode, rawBufferVector);
66
67         if ((counter != retCounter) || (static_cast<int>(command) != retCommand)) {
68             return CKM_API_ERROR_UNKNOWN;
69         }
70
71         if (retCode != CKM_API_SUCCESS) {
72             return retCode;
73         }
74
75         for (auto &e: rawBufferVector) {
76             CertificateShPtr cert(new CertificateImpl(e, DataFormat::FORM_DER));
77             if (cert->empty())
78                 return CKM_API_ERROR_BAD_RESPONSE;
79             certificateChainVector.push_back(cert);
80         }
81
82         return retCode;
83     });
84 }
85
86 } // namespace anonymous
87
88 ManagerImpl::ManagerImpl()
89   : m_counter(0),
90     m_storageConnection(SERVICE_SOCKET_CKM_STORAGE),
91     m_ocspConnection(SERVICE_SOCKET_OCSP),
92     m_encryptionConnection(SERVICE_SOCKET_ENCRYPTION)
93 {
94     initCryptoLib();
95 }
96
97
98 int ManagerImpl::saveBinaryData(
99     const Alias &alias,
100     DataType dataType,
101     const RawBuffer &rawData,
102     const Policy &policy)
103 {
104     int my_counter = ++m_counter;
105
106     return try_catch([&] {
107         if (alias.empty() || rawData.empty())
108             return CKM_API_ERROR_INPUT_PARAM;
109
110         MessageBuffer recv;
111         AliasSupport helper(alias);
112         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE),
113                                              my_counter,
114                                              static_cast<int>(dataType),
115                                              helper.getName(),
116                                              helper.getLabel(),
117                                              rawData,
118                                              PolicySerializable(policy));
119
120         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
121         if (CKM_API_SUCCESS != retCode)
122             return retCode;
123
124         int command;
125         int counter;
126         int opType;
127         recv.Deserialize(command, counter, retCode, opType);
128
129         if (counter != my_counter)
130             return CKM_API_ERROR_UNKNOWN;
131
132         return retCode;
133     });
134 }
135
136 int ManagerImpl::saveKey(const Alias &alias, const KeyShPtr &key, const Policy &policy) {
137     if (key.get() == NULL)
138         return CKM_API_ERROR_INPUT_PARAM;
139     Try {
140         return saveBinaryData(alias, DataType(key->getType()), key->getDER(), policy);
141     } Catch (DataType::Exception::Base) {
142         LogError("Error in key conversion. Could not convert KeyType::NONE to DBDataType!");
143     }
144     return CKM_API_ERROR_INPUT_PARAM;
145 }
146
147 int ManagerImpl::saveCertificate(
148     const Alias &alias,
149     const CertificateShPtr &cert,
150     const Policy &policy)
151 {
152     if (cert.get() == NULL)
153         return CKM_API_ERROR_INPUT_PARAM;
154     return saveBinaryData(alias, DataType::CERTIFICATE, cert->getDER(), policy);
155 }
156
157 int ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
158     if (!policy.extractable)
159         return CKM_API_ERROR_INPUT_PARAM;
160     return saveBinaryData(alias, DataType::BINARY_DATA, rawData, policy);
161 }
162
163
164 int ManagerImpl::savePKCS12(
165     const Alias & alias,
166     const PKCS12ShPtr &pkcs,
167     const Policy &keyPolicy,
168     const Policy &certPolicy)
169 {
170     if (alias.empty() || pkcs.get()==NULL)
171         return CKM_API_ERROR_INPUT_PARAM;
172
173     int my_counter = ++m_counter;
174
175     return try_catch([&] {
176         MessageBuffer recv;
177         AliasSupport helper(alias);
178         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SAVE_PKCS12),
179                                              my_counter,
180                                              helper.getName(),
181                                              helper.getLabel(),
182                                              PKCS12Serializable(*pkcs.get()),
183                                              PolicySerializable(keyPolicy),
184                                              PolicySerializable(certPolicy));
185
186         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
187         if (CKM_API_SUCCESS != retCode)
188             return retCode;
189
190         int command;
191         int counter;
192         recv.Deserialize(command, counter, retCode);
193
194         if (counter != my_counter)
195             return CKM_API_ERROR_UNKNOWN;
196
197         return retCode;
198     });
199 }
200
201 int ManagerImpl::getPKCS12(const Alias &alias, PKCS12ShPtr &pkcs)
202 {
203     return getPKCS12(alias, Password(), Password(), pkcs);
204 }
205
206 int ManagerImpl::getPKCS12(const Alias &alias, const Password &keyPass, const Password &certPass, PKCS12ShPtr &pkcs)
207 {
208     if (alias.empty())
209         return CKM_API_ERROR_INPUT_PARAM;
210
211     int my_counter = ++m_counter;
212
213     return try_catch([&] {
214         MessageBuffer recv;
215         AliasSupport helper(alias);
216         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_PKCS12),
217                                              my_counter,
218                                              helper.getName(),
219                                              helper.getLabel(),
220                                              keyPass,
221                                              certPass);
222
223         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
224         if (CKM_API_SUCCESS != retCode)
225             return retCode;
226
227         int command;
228         int counter;
229         PKCS12Serializable gotPkcs;
230         recv.Deserialize(command, counter, retCode, gotPkcs);
231
232         if (counter != my_counter)
233             return CKM_API_ERROR_UNKNOWN;
234
235         pkcs = std::make_shared<PKCS12Impl>(std::move(gotPkcs));
236
237         return retCode;
238     });
239 }
240
241
242 int ManagerImpl::removeAlias(const Alias &alias)
243 {
244     if (alias.empty())
245         return CKM_API_ERROR_INPUT_PARAM;
246
247     int my_counter = ++m_counter;
248
249     return try_catch([&] {
250         MessageBuffer recv;
251         AliasSupport helper(alias);
252         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::REMOVE),
253                                              my_counter,
254                                              helper.getName(),
255                                              helper.getLabel());
256
257         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
258         if (CKM_API_SUCCESS != retCode)
259             return retCode;
260
261         int command;
262         int counter;
263         recv.Deserialize(command, counter, retCode);
264
265         if (counter != my_counter)
266             return CKM_API_ERROR_UNKNOWN;
267
268         return retCode;
269     });
270 }
271
272 int ManagerImpl::getBinaryData(
273     const Alias &alias,
274     DataType sendDataType,
275     const Password &password,
276     DataType &recvDataType,
277     RawBuffer &rawData)
278 {
279     if (alias.empty())
280         return CKM_API_ERROR_INPUT_PARAM;
281
282     int my_counter = ++m_counter;
283
284     return try_catch([&] {
285         MessageBuffer recv;
286         AliasSupport helper(alias);
287         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET),
288                                              my_counter,
289                                              static_cast<int>(sendDataType),
290                                              helper.getName(),
291                                              helper.getLabel(),
292                                              password);
293
294         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
295         if (CKM_API_SUCCESS != retCode)
296             return retCode;
297
298         int command;
299         int counter;
300         int tmpDataType;
301         recv.Deserialize(command, counter, retCode, tmpDataType, rawData);
302         recvDataType = DataType(tmpDataType);
303
304         if (counter != my_counter)
305             return CKM_API_ERROR_UNKNOWN;
306
307         return retCode;
308     });
309 }
310
311 int ManagerImpl::getKey(const Alias &alias, const Password &password, KeyShPtr &key) {
312     DataType recvDataType;
313     RawBuffer rawData;
314
315     int retCode = getBinaryData(
316         alias,
317         DataType::KEY_RSA_PUBLIC,
318         password,
319         recvDataType,
320         rawData);
321
322     if (retCode != CKM_API_SUCCESS)
323         return retCode;
324
325     KeyShPtr keyParsed(new KeyImpl(rawData));
326
327     if (keyParsed->empty()) {
328         LogDebug("Key empty - failed to parse!");
329         return CKM_API_ERROR_BAD_RESPONSE;
330     }
331
332     key = keyParsed;
333
334     return CKM_API_SUCCESS;
335 }
336
337 int ManagerImpl::getCertificate(const Alias &alias, const Password &password, CertificateShPtr &cert)
338 {
339     DataType recvDataType;
340     RawBuffer rawData;
341
342     int retCode = getBinaryData(
343         alias,
344         DataType::CERTIFICATE,
345         password,
346         recvDataType,
347         rawData);
348
349     if (retCode != CKM_API_SUCCESS)
350         return retCode;
351
352     if (recvDataType != DataType::CERTIFICATE)
353         return CKM_API_ERROR_BAD_RESPONSE;
354
355     CertificateShPtr certParsed(new CertificateImpl(rawData, DataFormat::FORM_DER));
356
357     if (certParsed->empty())
358         return CKM_API_ERROR_BAD_RESPONSE;
359
360     cert = certParsed;
361
362     return CKM_API_SUCCESS;
363 }
364
365 int ManagerImpl::getData(const Alias &alias, const Password &password, RawBuffer &rawData)
366 {
367     DataType recvDataType = DataType::BINARY_DATA;
368
369     int retCode = getBinaryData(
370         alias,
371         DataType::BINARY_DATA,
372         password,
373         recvDataType,
374         rawData);
375
376     if (retCode != CKM_API_SUCCESS)
377         return retCode;
378
379     if (recvDataType != DataType::BINARY_DATA)
380         return CKM_API_ERROR_BAD_RESPONSE;
381
382     return CKM_API_SUCCESS;
383 }
384
385 int ManagerImpl::getBinaryDataAliasVector(DataType dataType, AliasVector &aliasVector)
386 {
387     int my_counter = ++m_counter;
388
389     return try_catch([&] {
390         MessageBuffer recv;
391         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
392                                              my_counter,
393                                              static_cast<int>(dataType));
394
395         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
396         if (CKM_API_SUCCESS != retCode)
397             return retCode;
398
399         int command;
400         int counter;
401         int tmpDataType;
402         LabelNameVector labelNameVector;
403         recv.Deserialize(command, counter, retCode, tmpDataType, labelNameVector);
404         if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != my_counter)) {
405             return CKM_API_ERROR_UNKNOWN;
406         }
407
408         for(const auto &it : labelNameVector)
409             aliasVector.push_back( AliasSupport::merge(it.first, it.second) );
410
411         return retCode;
412     });
413 }
414
415 int ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
416     // in fact datatype has no meaning here - if not certificate or binary data
417     // then manager decides to list all between DB_KEY_FIRST and DB_KEY_LAST
418     return getBinaryDataAliasVector(DataType::DB_KEY_LAST, aliasVector);
419 }
420
421 int ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
422     return getBinaryDataAliasVector(DataType::CERTIFICATE, aliasVector);
423 }
424
425 int ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
426     return getBinaryDataAliasVector(DataType::BINARY_DATA, aliasVector);
427 }
428
429 int ManagerImpl::createKeyPairRSA(
430     const int size,
431     const Alias &privateKeyAlias,
432     const Alias &publicKeyAlias,
433     const Policy &policyPrivateKey,
434     const Policy &policyPublicKey)
435 {
436     return this->createKeyPair(CKM::KeyType::KEY_RSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
437 }
438
439 int ManagerImpl::createKeyPairDSA(
440     const int size,
441     const Alias &privateKeyAlias,
442     const Alias &publicKeyAlias,
443     const Policy &policyPrivateKey,
444     const Policy &policyPublicKey)
445 {
446     return this->createKeyPair(CKM::KeyType::KEY_DSA_PUBLIC, size, privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
447 }
448
449 int ManagerImpl::createKeyPairECDSA(
450     ElipticCurve type,
451     const Alias &privateKeyAlias,
452     const Alias &publicKeyAlias,
453     const Policy &policyPrivateKey,
454     const Policy &policyPublicKey)
455 {
456     return this->createKeyPair(CKM::KeyType::KEY_ECDSA_PUBLIC, static_cast<int>(type), privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
457 }
458
459 int ManagerImpl::createKeyAES(
460     const int /*size*/,
461     const Alias &/*keyAlias*/,
462     const Policy &/*policyKey*/)
463 {
464     return 0;
465 }
466
467
468 int ManagerImpl::createKeyPair(
469     const KeyType key_type,
470     const int     additional_param,
471     const Alias  &privateKeyAlias,
472     const Alias  &publicKeyAlias,
473     const Policy &policyPrivateKey,
474     const Policy &policyPublicKey)
475 {
476     // input type check
477     CryptoAlgorithm keyGenAlgorithm;
478     switch(key_type)
479     {
480         case KeyType::KEY_RSA_PUBLIC:
481         case KeyType::KEY_RSA_PRIVATE:
482             keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
483             keyGenAlgorithm.addParam(ParamName::GEN_KEY_LEN, additional_param);
484             break;
485
486         case KeyType::KEY_DSA_PUBLIC:
487         case KeyType::KEY_DSA_PRIVATE:
488             keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
489             keyGenAlgorithm.addParam(ParamName::GEN_KEY_LEN, additional_param);
490             break;
491
492         case KeyType::KEY_ECDSA_PUBLIC:
493         case KeyType::KEY_ECDSA_PRIVATE:
494             keyGenAlgorithm.addParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
495             keyGenAlgorithm.addParam(ParamName::GEN_EC, additional_param);
496             break;
497
498         default:
499             return CKM_API_ERROR_INPUT_PARAM;
500     }
501
502     // proceed with sending request
503     int my_counter = ++m_counter;
504
505     return try_catch([&] {
506
507         MessageBuffer recv;
508         AliasSupport privateHelper(privateKeyAlias);
509         AliasSupport publicHelper(publicKeyAlias);
510         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_KEY_PAIR),
511                                              my_counter,
512                                              CryptoAlgorithmSerializable(keyGenAlgorithm),
513                                              PolicySerializable(policyPrivateKey),
514                                              PolicySerializable(policyPublicKey),
515                                              privateHelper.getName(),
516                                              privateHelper.getLabel(),
517                                              publicHelper.getName(),
518                                              publicHelper.getLabel());
519
520         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
521         if (CKM_API_SUCCESS != retCode)
522             return retCode;
523
524         int command;
525         int counter;
526         recv.Deserialize(command, counter, retCode);
527         if (counter != my_counter) {
528             return CKM_API_ERROR_UNKNOWN;
529         }
530
531         return retCode;
532     });
533 }
534
535 int ManagerImpl::getCertificateChain(
536     const CertificateShPtr &certificate,
537     const CertificateShPtrVector &untrustedCertificates,
538     const CertificateShPtrVector &trustedCertificates,
539     bool useTrustedSystemCertificates,
540     CertificateShPtrVector &certificateChainVector)
541 {
542     RawBufferVector untrustedVector;
543     RawBufferVector trustedVector;
544
545     for (auto &e: untrustedCertificates) {
546         untrustedVector.push_back(e->getDER());
547     }
548     for (auto &e: trustedCertificates) {
549         trustedVector.push_back(e->getDER());
550     }
551
552     return getCertChain(
553             m_storageConnection,
554             LogicCommand::GET_CHAIN_CERT,
555             ++m_counter,
556             certificate,
557             untrustedVector,
558             trustedVector,
559             useTrustedSystemCertificates,
560             certificateChainVector);
561 }
562
563 int ManagerImpl::getCertificateChain(
564     const CertificateShPtr &certificate,
565     const AliasVector &untrustedCertificates,
566     const AliasVector &trustedCertificates,
567     bool useTrustedSystemCertificates,
568     CertificateShPtrVector &certificateChainVector)
569 {
570     LabelNameVector untrustedVector;
571     LabelNameVector trustedVector;
572
573     for (auto &e: untrustedCertificates) {
574         AliasSupport helper(e);
575         untrustedVector.push_back(std::make_pair(helper.getLabel(), helper.getName()));
576     }
577     for (auto &e: trustedCertificates) {
578         AliasSupport helper(e);
579         trustedVector.push_back(std::make_pair(helper.getLabel(), helper.getName()));
580     }
581
582     return getCertChain(
583             m_storageConnection,
584             LogicCommand::GET_CHAIN_ALIAS,
585             ++m_counter,
586             certificate,
587             untrustedVector,
588             trustedVector,
589             useTrustedSystemCertificates,
590             certificateChainVector);
591 }
592
593 int ManagerImpl::createSignature(
594     const Alias &privateKeyAlias,
595     const Password &password,           // password for private_key
596     const RawBuffer &message,
597     const HashAlgorithm hash,
598     const RSAPaddingAlgorithm padding,
599     RawBuffer &signature)
600 {
601     int my_counter = ++m_counter;
602
603     return try_catch([&] {
604
605         MessageBuffer recv;
606         AliasSupport helper(privateKeyAlias);
607         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_SIGNATURE),
608                                              my_counter,
609                                              helper.getName(),
610                                              helper.getLabel(),
611                                              password,
612                                              message,
613                                              static_cast<int>(hash),
614                                              static_cast<int>(padding));
615
616         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
617         if (CKM_API_SUCCESS != retCode)
618             return retCode;
619
620         int command;
621         int counter;
622         recv.Deserialize(command, counter, retCode, signature);
623
624         if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
625             || (counter != my_counter))
626         {
627             return CKM_API_ERROR_UNKNOWN;
628         }
629
630         return retCode;
631     });
632 }
633
634 int ManagerImpl::verifySignature(
635     const Alias &publicKeyOrCertAlias,
636     const Password &password,           // password for public_key (optional)
637     const RawBuffer &message,
638     const RawBuffer &signature,
639     const HashAlgorithm hash,
640     const RSAPaddingAlgorithm padding)
641 {
642     int my_counter = ++m_counter;
643
644     return try_catch([&] {
645         MessageBuffer recv;
646         AliasSupport helper(publicKeyOrCertAlias);
647         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::VERIFY_SIGNATURE),
648                                              my_counter,
649                                              helper.getName(),
650                                              helper.getLabel(),
651                                              password,
652                                              message,
653                                              signature,
654                                              static_cast<int>(hash),
655                                              static_cast<int>(padding));
656
657         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
658         if (CKM_API_SUCCESS != retCode)
659             return retCode;
660
661         int command;
662         int counter;
663         recv.Deserialize(command, counter, retCode);
664
665         if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
666             || (counter != my_counter))
667         {
668             return CKM_API_ERROR_UNKNOWN;
669         }
670
671         return retCode;
672     });
673 }
674
675 int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspStatus)
676 {
677     return try_catch([&] {
678         int my_counter = ++m_counter;
679         MessageBuffer recv;
680
681         RawBufferVector rawCertChain;
682         for (auto &e: certChain) {
683             rawCertChain.push_back(e->getDER());
684         }
685
686         auto send = MessageBuffer::Serialize(my_counter, rawCertChain);
687
688         int retCode = m_ocspConnection.processRequest(send.Pop(), recv);
689         if (CKM_API_SUCCESS != retCode)
690             return retCode;
691
692         int counter;
693         recv.Deserialize(counter, retCode, ocspStatus);
694
695         if (my_counter != counter) {
696             return CKM_API_ERROR_UNKNOWN;
697         }
698
699         return retCode;
700     });
701 }
702
703 int ManagerImpl::setPermission(const Alias &alias,
704                                const Label &accessor,
705                                PermissionMask permissionMask)
706 {
707     int my_counter = ++m_counter;
708
709     return try_catch([&] {
710         MessageBuffer recv;
711         AliasSupport helper(alias);
712         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::SET_PERMISSION),
713                                              my_counter,
714                                              helper.getName(),
715                                              helper.getLabel(),
716                                              accessor,
717                                              permissionMask);
718
719         int retCode = m_storageConnection.processRequest(send.Pop(), recv);
720         if (CKM_API_SUCCESS != retCode)
721             return retCode;
722
723         int command;
724         int counter;
725         recv.Deserialize(command, counter, retCode);
726
727         if (my_counter != counter) {
728             return CKM_API_ERROR_UNKNOWN;
729         }
730
731         return retCode;
732     });
733 }
734
735 int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
736                          const Alias &keyAlias,
737                          const Password &password,
738                          const RawBuffer& plain,
739                          RawBuffer& encrypted)
740 {
741     int my_counter = ++m_counter;
742
743     return try_catch([&] {
744         MessageBuffer recv;
745         AliasSupport helper(keyAlias);
746         CryptoAlgorithmSerializable cas(algo);
747         auto send = MessageBuffer::Serialize(static_cast<int>(EncryptionCommand::ENCRYPT),
748                                              my_counter,
749                                              cas,
750                                              helper.getName(),
751                                              helper.getLabel(),
752                                              password,
753                                              plain);
754
755         int retCode = m_encryptionConnection.processRequest(send.Pop(), recv);
756         if (CKM_API_SUCCESS != retCode)
757             return retCode;
758
759         int command;
760         int counter;
761         recv.Deserialize(command, counter, encrypted);
762
763         if (my_counter != counter) {
764             return CKM_API_ERROR_UNKNOWN;
765         }
766
767         return retCode;
768     });
769 }
770
771 int ManagerImpl::decrypt(const CryptoAlgorithm &algo,
772                          const Alias &keyAlias,
773                          const Password &password,
774                          const RawBuffer& encrypted,
775                          RawBuffer& decrypted)
776 {
777     int my_counter = ++m_counter;
778
779     return try_catch([&] {
780         MessageBuffer recv;
781         AliasSupport helper(keyAlias);
782         CryptoAlgorithmSerializable cas(algo);
783         auto send = MessageBuffer::Serialize(static_cast<int>(EncryptionCommand::DECRYPT),
784                                              my_counter,
785                                              cas,
786                                              helper.getName(),
787                                              helper.getLabel(),
788                                              password,
789                                              encrypted);
790
791         int retCode = m_encryptionConnection.processRequest(send.Pop(), recv);
792         if (CKM_API_SUCCESS != retCode)
793             return retCode;
794
795         int command;
796         int counter;
797         recv.Deserialize(command, counter, decrypted);
798
799         if (my_counter != counter) {
800             return CKM_API_ERROR_UNKNOWN;
801         }
802
803         return retCode;
804     });
805 }
806
807 ManagerShPtr Manager::create() {
808     try {
809         return std::make_shared<ManagerImpl>();
810     } catch (const std::bad_alloc &) {
811         LogDebug("Bad alloc was caught during ManagerImpl creation.");
812     } catch (...) {
813         LogError("Critical error: Unknown exception was caught during ManagerImpl creation!");
814     }
815     return ManagerShPtr();
816 }
817
818 } // namespace CKM