Constrain async tryCatch to Observer::ReceivedError 71/244271/1
authorKonrad Lipinski <k.lipinski2@samsung.com>
Wed, 16 Sep 2020 12:42:23 +0000 (14:42 +0200)
committerKonrad Lipinski <k.lipinski2@samsung.com>
Wed, 16 Sep 2020 12:45:16 +0000 (14:45 +0200)
Change-Id: I360bc2a70aafa7436f4b7a7f4917c26d32a10e13

src/include/ckm/ckm-manager-async.h
src/manager/client-async/client-manager-async-impl.cpp
src/manager/client-async/client-manager-async.cpp
src/manager/client/client-common.cpp
src/manager/client/client-common.h

index e8fabaa..c436be2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
  */
 #pragma once
 
+#include <functional>
 #include <memory>
 
 #include <ckm/ckm-certificate.h>
@@ -222,6 +223,16 @@ public:
 
 private:
        std::unique_ptr<Impl> m_impl;
+
+       template <class F>
+       static void tryCatch(Observer &observer, F &&f)
+       {
+               static_assert(std::is_same_v<void, decltype(std::forward<F>(f)())>);
+               tryCatchHelper(observer, std::forward<F>(f));
+       }
+
+       static void tryCatchHelper(Observer &observer, const std::function<void()> &func);
+
 };
 
 } // namespace CKM
index e04d605..f5082ec 100644 (file)
 namespace CKM {
 namespace {
 
-template <class F>
-void tryCatch(const ManagerAsync::ObserverPtr &observer, F &&f)
-{
-       static_assert(std::is_same_v<void, decltype(std::forward<F>(f)())>);
-       try_catch_async(std::forward<F>(f),
-                       [&](int error) { observer->ReceivedError(error); });
-}
-
 template <class T>
 bool paramEmpty(const T &t) {
        return t.empty();
@@ -107,7 +99,7 @@ void ManagerAsync::Impl::saveBinaryData(const ObserverPtr
                                                                                const RawBuffer &rawData,
                                                                                const Policy &policy)
 {
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(alias);
 
                sendToStorage(LogicCommand::SAVE, observer,
@@ -122,7 +114,7 @@ void ManagerAsync::Impl::savePKCS12(const ObserverPtr &observer,
                                                                        const Policy &keyPolicy,
                                                                        const Policy &certPolicy)
 {
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(alias);
                sendToStorage(LogicCommand::SAVE_PKCS12, observer,
                                          helper.getName(), helper.getOwner(), PKCS12Serializable(*pkcs.get()),
@@ -136,7 +128,7 @@ void ManagerAsync::Impl::removeAlias(const ObserverPtr &observer,
        if (anyParamEmpty(observer, alias))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(alias);
                sendToStorage(LogicCommand::REMOVE, observer,
                                          helper.getName(), helper.getOwner());
@@ -151,7 +143,7 @@ void ManagerAsync::Impl::getBinaryData(const ObserverPtr &observer,
        if (anyParamEmpty(observer, alias))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(alias);
 
                sendToStorage(LogicCommand::GET, observer,
@@ -167,7 +159,7 @@ void ManagerAsync::Impl::getPKCS12(const ObserverPtr &observer,
        if (anyParamEmpty(observer, alias))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(alias);
 
                sendToStorage(LogicCommand::GET_PKCS12, observer,
@@ -184,7 +176,7 @@ void ManagerAsync::Impl::createSignature(const ObserverPtr &observer,
        if (anyParamEmpty(observer, privateKeyAlias, message))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(privateKeyAlias);
                sendToStorage(LogicCommand::CREATE_SIGNATURE, observer,
                                          helper.getName(), helper.getOwner(), password, message,
@@ -202,7 +194,7 @@ void ManagerAsync::Impl::verifySignature(const ObserverPtr &observer,
        if (anyParamEmpty(observer, publicKeyOrCertAlias, message, signature))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(publicKeyOrCertAlias);
 
                sendToStorage(LogicCommand::VERIFY_SIGNATURE, observer,
@@ -217,7 +209,7 @@ void ManagerAsync::Impl::ocspCheck(const ObserverPtr &observer,
        if (anyParamEmpty(observer, certificateChainVector))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                RawBufferVector rawCertChain;
 
                for (auto &e : certificateChainVector) {
@@ -243,7 +235,7 @@ void ManagerAsync::Impl::setPermission(const ObserverPtr &observer,
        if (anyParamEmpty(observer, alias, accessor))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(alias);
 
                sendToStorage(LogicCommand::SET_PERMISSION, observer,
@@ -255,7 +247,7 @@ void ManagerAsync::Impl::getBinaryDataAliasVector(const ObserverPtr &observer,
                                                                                                  DataType dataType)
 {
        observerCheck(observer);
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                sendToStorage(LogicCommand::GET_LIST, observer, dataType);
        });
 }
@@ -298,7 +290,7 @@ void ManagerAsync::Impl::createKeyPair(const ObserverPtr &observer,
                return;
        }
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport prvHelper(privateKeyAlias);
                AliasSupport pubHelper(publicKeyAlias);
 
@@ -318,7 +310,7 @@ void ManagerAsync::Impl::createKeyAES(const ObserverPtr &observer,
        if (anyParamEmpty(observer, keyAlias))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport aliasHelper(keyAlias);
 
                sendToStorage(LogicCommand::CREATE_KEY_AES, observer,
@@ -344,7 +336,7 @@ void ManagerAsync::Impl::crypt(
        if (anyParamEmpty(observer, input, keyAlias))
                return;
 
-       tryCatch(observer, [&] {
+       tryCatch(*observer, [&] {
                AliasSupport helper(keyAlias);
                CryptoAlgorithmSerializable cas(algo);
                m_counter++;
index 6227b8d..7dee6d9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  * @version    1.0
  */
 
+#include <cxxabi.h>
+
 #include <ckm/ckm-manager-async.h>
 #include <client-manager-async-impl.h>
+#include <dpl/log/log.h>
 #include <exception.h>
 
 namespace CKM {
@@ -211,15 +214,13 @@ void ManagerAsync::getCertificateChain(const ObserverPtr &observer,
 {
        Impl::observerCheck(observer);
 
-       try_catch_async([&]() {
+       tryCatch(*observer, [&] {
                m_impl->getCertChain(observer,
                                                         LogicCommand::GET_CHAIN_CERT,
                                                         certificate,
                                                         toRawBufferVector(untrustedCertificates),
                                                         toRawBufferVector(trustedCertificates),
                                                         useSystemTrustedCertificates);
-       }, [&observer](int error) {
-               observer->ReceivedError(error);
        });
 }
 
@@ -231,15 +232,13 @@ void ManagerAsync::getCertificateChain(const ObserverPtr &observer,
 {
        Impl::observerCheck(observer);
 
-       try_catch_async([&]() {
+       tryCatch(*observer, [&] {
                m_impl->getCertChain(observer,
                                                         LogicCommand::GET_CHAIN_ALIAS,
                                                         certificate,
                                                         toOwnerNameVector(untrustedCertificates),
                                                         toOwnerNameVector(trustedCertificates),
                                                         useSystemTrustedCertificates);
-       }, [&observer](int error) {
-               observer->ReceivedError(error);
        });
 }
 
@@ -305,5 +304,28 @@ void ManagerAsync::decrypt(
        m_impl->crypt(observer, algo, keyAlias, password, encrypted, false);
 }
 
+void ManagerAsync::tryCatchHelper(Observer &observer,
+               const std::function<void()> &func)
+{
+       try {
+               func();
+       } catch (const MessageBuffer::Exception::Base &e) {
+               LogError("CKM::MessageBuffer::Exception " << e.DumpToString());
+               observer.ReceivedError(CKM_API_ERROR_BAD_REQUEST);
+       } catch (const Exc::Exception &e) {
+               LogError("Exception: " << e.what());
+               observer.ReceivedError(e.error());
+       } catch (const std::exception &e) {
+               LogError("STD exception " << e.what());
+               observer.ReceivedError(CKM_API_ERROR_UNKNOWN);
+       } catch (const abi::__forced_unwind &) {
+               LogDebug("abi::__forced_unwind caught. Thread cancelation.");
+               throw;
+       } catch (...) {
+               LogError("Unknown exception occured");
+               observer.ReceivedError(CKM_API_ERROR_UNKNOWN);
+       }
+}
+
 } // namespace CKM
 
index f440b95..e565138 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -362,29 +362,6 @@ int try_catch_enclosure(const std::function<int()> &func)
        }
 }
 
-void try_catch_async(const std::function<void()> &func,
-                                        const std::function<void(int)> &error)
-{
-       try {
-               func();
-       } catch (const MessageBuffer::Exception::Base &e) {
-               LogError("CKM::MessageBuffer::Exception " << e.DumpToString());
-               error(CKM_API_ERROR_BAD_REQUEST);
-       } catch (const Exc::Exception &e) {
-               LogError("Exception: " << e.what());
-               error(e.error());
-       } catch (const std::exception &e) {
-               LogError("STD exception " << e.what());
-               error(CKM_API_ERROR_UNKNOWN);
-       } catch (const abi::__forced_unwind &) {
-               LogDebug("abi::__forced_unwind caught. Thread cancelation.");
-               throw;
-       } catch (...) {
-               LogError("Unknown exception occured");
-               error(CKM_API_ERROR_UNKNOWN);
-       }
-}
-
 } // namespace CKM
 
 static void init_lib(void) __attribute__((constructor));
index d285b5d..35f57f9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
+ *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -115,10 +115,6 @@ int try_catch(const std::function<int()> &func);
 // for c api layer
 int try_catch_enclosure(const std::function<int()> &func);
 
-// for c++ async api layer
-void try_catch_async(const std::function<void()> &func,
-                                        const std::function<void(int)> &error);
-
 } // namespace CKM
 
 #endif // _KEY_MANAGER_CLIENT_