diff options
author | Konrad Lipinski <k.lipinski2@samsung.com> | 2020-09-03 18:19:46 +0200 |
---|---|---|
committer | Konrad Lipinski <k.lipinski2@samsung.com> | 2020-09-16 13:55:57 +0200 |
commit | 2b74bf55d0daa7db5612742b195c95c61e462641 (patch) | |
tree | 89a6ceb4354683fbdab3d5473747d4eb12ccec7e | |
parent | 16693819f95695dcae7121b52c72800febb9575c (diff) | |
download | key-manager-2b74bf55d0daa7db5612742b195c95c61e462641.tar.gz key-manager-2b74bf55d0daa7db5612742b195c95c61e462641.tar.bz2 key-manager-2b74bf55d0daa7db5612742b195c95c61e462641.zip |
Shrink ManagerAsync::Impl code
Change-Id: I84d18c8e965c2a430332b9a6b55d95f48ae707eb
-rw-r--r-- | src/manager/client-async/client-manager-async-impl.cpp | 223 | ||||
-rw-r--r-- | src/manager/client-async/client-manager-async-impl.h | 46 |
2 files changed, 104 insertions, 165 deletions
diff --git a/src/manager/client-async/client-manager-async-impl.cpp b/src/manager/client-async/client-manager-async-impl.cpp index ea5aa546..e04d6054 100644 --- a/src/manager/client-async/client-manager-async-impl.cpp +++ b/src/manager/client-async/client-manager-async-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 - 2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-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. @@ -29,23 +29,46 @@ #include <client-manager-async-impl.h> namespace CKM { -int ManagerAsync::Impl::m_counter = 0; +namespace { -ManagerAsync::Impl::Impl() {} +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(); +} + +template <class T> +bool paramEmpty(const std::shared_ptr<T> &t) { + return !t || t->empty(); +} + +template <class...T> +bool anyParamEmpty(const ManagerAsync::ObserverPtr &observer, const T&...t) +{ + ManagerAsync::Impl::observerCheck(observer); + if ((... || paramEmpty(t))) { + observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + return true; + } + return false; +} -ManagerAsync::Impl::~Impl() {} +} // namespace void ManagerAsync::Impl::saveKey(const ObserverPtr &observer, const Alias &alias, const KeyShPtr &key, const Policy &policy) { - observerCheck(observer); - - if (alias.empty() || !key || key->empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias, key)) return; - } try { saveBinaryData(observer, alias, DataType(key->getType()), key->getDER(), @@ -60,12 +83,8 @@ void ManagerAsync::Impl::saveCertificate(const ObserverPtr &observer, const CertificateShPtr &cert, const Policy &policy) { - observerCheck(observer); - - if (alias.empty() || !cert || cert->empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias, cert)) return; - } saveBinaryData(observer, alias, DataType::CERTIFICATE, cert->getDER(), policy); } @@ -75,111 +94,84 @@ void ManagerAsync::Impl::saveData(const ObserverPtr &observer, const RawBuffer &data, const Policy &policy) { - observerCheck(observer); - - if (alias.empty() || data.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias, data)) return; - } saveBinaryData(observer, alias, DataType::BINARY_DATA, data, policy); } -void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr +void ManagerAsync::Impl::saveBinaryData(const ObserverPtr &observer, const Alias &alias, DataType dataType, const RawBuffer &rawData, const Policy &policy) { - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(alias); - sendToStorage(observer, static_cast<int>(LogicCommand::SAVE), m_counter, + sendToStorage(LogicCommand::SAVE, observer, dataType, helper.getName(), helper.getOwner(), rawData, PolicySerializable(policy)); - }, [&observer](int error) { - observer->ReceivedError(error); }); } -void ManagerAsync::Impl::savePKCS12(const ManagerAsync::ObserverPtr &observer, +void ManagerAsync::Impl::savePKCS12(const ObserverPtr &observer, const Alias &alias, const PKCS12ShPtr &pkcs, const Policy &keyPolicy, const Policy &certPolicy) { - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(alias); - sendToStorage(observer, static_cast<int>(LogicCommand::SAVE_PKCS12), - m_counter, helper.getName(), helper.getOwner(), PKCS12Serializable(*pkcs.get()), + sendToStorage(LogicCommand::SAVE_PKCS12, observer, + helper.getName(), helper.getOwner(), PKCS12Serializable(*pkcs.get()), PolicySerializable(keyPolicy), PolicySerializable(certPolicy)); - }, [&observer](int error) { - observer->ReceivedError(error); }); } -void ManagerAsync::Impl::removeAlias(const ManagerAsync::ObserverPtr &observer, +void ManagerAsync::Impl::removeAlias(const ObserverPtr &observer, const Alias &alias) { - observerCheck(observer); - - if (alias.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(alias); - sendToStorage(observer, static_cast<int>(LogicCommand::REMOVE), m_counter, + sendToStorage(LogicCommand::REMOVE, observer, helper.getName(), helper.getOwner()); - }, [&observer](int error) { - observer->ReceivedError(error); }); } -void ManagerAsync::Impl::getBinaryData(const ManagerAsync::ObserverPtr - &observer, +void ManagerAsync::Impl::getBinaryData(const ObserverPtr &observer, const Alias &alias, DataType sendDataType, const Password &password) { - observerCheck(observer); - - if (alias.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(alias); - sendToStorage(observer, static_cast<int>(LogicCommand::GET), m_counter, + sendToStorage(LogicCommand::GET, observer, sendDataType, helper.getName(), helper.getOwner(), password); - }, [&observer](int error) { - observer->ReceivedError(error); }); } -void ManagerAsync::Impl::getPKCS12(const ManagerAsync::ObserverPtr &observer, +void ManagerAsync::Impl::getPKCS12(const ObserverPtr &observer, const Alias &alias, const Password &passwordKey, const Password &passwordCert) { - observerCheck(observer); - - if (alias.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(alias); - sendToStorage(observer, static_cast<int>(LogicCommand::GET_PKCS12), m_counter, + sendToStorage(LogicCommand::GET_PKCS12, observer, helper.getName(), helper.getOwner(), passwordKey, passwordCert); - }, [&observer](int error) { - observer->ReceivedError(error); }); } @@ -189,20 +181,14 @@ void ManagerAsync::Impl::createSignature(const ObserverPtr &observer, const RawBuffer &message, const CryptoAlgorithm &cAlg) { - observerCheck(observer); - - if (privateKeyAlias.empty() || message.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, privateKeyAlias, message)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(privateKeyAlias); - sendToStorage(observer, static_cast<int>(LogicCommand::CREATE_SIGNATURE), - m_counter, helper.getName(), helper.getOwner(), password, message, + sendToStorage(LogicCommand::CREATE_SIGNATURE, observer, + helper.getName(), helper.getOwner(), password, message, CryptoAlgorithmSerializable(cAlg)); - }, [&observer](int error) { - observer->ReceivedError(error); }); } @@ -213,35 +199,25 @@ void ManagerAsync::Impl::verifySignature(const ObserverPtr &observer, const RawBuffer &signature, const CryptoAlgorithm &cAlg) { - observerCheck(observer); - - if (publicKeyOrCertAlias.empty() || message.empty() || signature.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, publicKeyOrCertAlias, message, signature)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(publicKeyOrCertAlias); - sendToStorage(observer, static_cast<int>(LogicCommand::VERIFY_SIGNATURE), - m_counter, helper.getName(), helper.getOwner(), password, + sendToStorage(LogicCommand::VERIFY_SIGNATURE, observer, + helper.getName(), helper.getOwner(), password, message, signature, CryptoAlgorithmSerializable(cAlg)); - }, [&observer](int error) { - observer->ReceivedError(error); }); } void ManagerAsync::Impl::ocspCheck(const ObserverPtr &observer, const CertificateShPtrVector &certificateChainVector) { - observerCheck(observer); - - if (certificateChainVector.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, certificateChainVector)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { RawBufferVector rawCertChain; for (auto &e : certificateChainVector) { @@ -256,8 +232,6 @@ void ManagerAsync::Impl::ocspCheck(const ObserverPtr &observer, thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_OCSP, std::move(send), m_counter, 0)); - }, [&observer](int error) { - observer->ReceivedError(error); }); } @@ -266,37 +240,27 @@ void ManagerAsync::Impl::setPermission(const ObserverPtr &observer, const ClientId &accessor, PermissionMask permissionMask) { - observerCheck(observer); - - if (alias.empty() || accessor.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, alias, accessor)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(alias); - sendToStorage(observer, static_cast<int>(LogicCommand::SET_PERMISSION), - m_counter, helper.getName(), helper.getOwner(), accessor, permissionMask); - }, [&observer](int error) { - observer->ReceivedError(error); + sendToStorage(LogicCommand::SET_PERMISSION, observer, + helper.getName(), helper.getOwner(), accessor, permissionMask); }); } -void ManagerAsync::Impl::getBinaryDataAliasVector(const - ManagerAsync::ObserverPtr &observer, - DataType dataType) +void ManagerAsync::Impl::getBinaryDataAliasVector(const ObserverPtr &observer, + DataType dataType) { observerCheck(observer); - try_catch_async([&]() { - sendToStorage(observer, static_cast<int>(LogicCommand::GET_LIST), m_counter, dataType); - }, [&observer](int error) { - observer->ReceivedError(error); + tryCatch(observer, [&] { + sendToStorage(LogicCommand::GET_LIST, observer, dataType); }); } -void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr - &observer, +void ManagerAsync::Impl::createKeyPair(const ObserverPtr &observer, const KeyType key_type, const int additional_param, const Alias &privateKeyAlias, @@ -304,12 +268,8 @@ void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr const Policy &policyPrivateKey, const Policy &policyPublicKey) { - observerCheck(observer); - - if (privateKeyAlias.empty() || publicKeyAlias.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, privateKeyAlias, publicKeyAlias)) return; - } // input type check CryptoAlgorithm keyGenAlgorithm; @@ -338,45 +298,36 @@ void ManagerAsync::Impl::createKeyPair(const ManagerAsync::ObserverPtr return; } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport prvHelper(privateKeyAlias); AliasSupport pubHelper(publicKeyAlias); - sendToStorage(observer, static_cast<int>(LogicCommand::CREATE_KEY_PAIR), - m_counter, CryptoAlgorithmSerializable(keyGenAlgorithm), + sendToStorage(LogicCommand::CREATE_KEY_PAIR, observer, + CryptoAlgorithmSerializable(keyGenAlgorithm), PolicySerializable(policyPrivateKey), PolicySerializable(policyPublicKey), prvHelper.getName(), prvHelper.getOwner(), pubHelper.getName(), pubHelper.getOwner()); - }, [&observer](int error) { - observer->ReceivedError(error); }); } -void ManagerAsync::Impl::createKeyAES(const ManagerAsync::ObserverPtr &observer, +void ManagerAsync::Impl::createKeyAES(const ObserverPtr &observer, const size_t size, const Alias &keyAlias, const Policy &policyKey) { - observerCheck(observer); - - if (keyAlias.empty()) { - observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, keyAlias)) return; - } - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport aliasHelper(keyAlias); - sendToStorage(observer, static_cast<int>(LogicCommand::CREATE_KEY_AES), - m_counter, static_cast<int>(size), PolicySerializable(policyKey), + sendToStorage(LogicCommand::CREATE_KEY_AES, observer, + static_cast<int>(size), PolicySerializable(policyKey), aliasHelper.getName(), aliasHelper.getOwner()); - }, [&observer](int error) { - observer->ReceivedError(error); }); } -void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr - &observer) +void ManagerAsync::Impl::observerCheck(const ObserverPtr &observer) { if (!observer) throw std::invalid_argument("Empty observer"); @@ -390,12 +341,10 @@ void ManagerAsync::Impl::crypt( const RawBuffer &input, bool encryption) { - observerCheck(observer); - - if (input.empty() || keyAlias.empty()) - return observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM); + if (anyParamEmpty(observer, input, keyAlias)) + return; - try_catch_async([&]() { + tryCatch(observer, [&] { AliasSupport helper(keyAlias); CryptoAlgorithmSerializable cas(algo); m_counter++; @@ -407,8 +356,6 @@ void ManagerAsync::Impl::crypt( thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_ENCRYPTION, std::move(send), m_counter, command)); - }, [&observer](int error) { - observer->ReceivedError(error); }); } diff --git a/src/manager/client-async/client-manager-async-impl.h b/src/manager/client-async/client-manager-async-impl.h index 1a836d76..eb354ef1 100644 --- a/src/manager/client-async/client-manager-async-impl.h +++ b/src/manager/client-async/client-manager-async-impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 - 2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-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. @@ -29,14 +29,12 @@ namespace CKM { -class ManagerAsync::Impl { +class ManagerAsync::Impl final { public: - Impl(); + Impl() {} NONCOPYABLE(Impl); - virtual ~Impl(); - void saveKey( const ObserverPtr &observer, const Alias &alias, @@ -85,34 +83,34 @@ public: // generic methods void saveBinaryData( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, const Alias &alias, DataType dataType, const RawBuffer &rawData, const Policy &policy); void removeAlias( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, const Alias &alias); void getBinaryData( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, const Alias &alias, DataType sendDataType, const Password &password); void getPKCS12( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, const Alias &alias, const Password &keyPassword, const Password &certPassword); void getBinaryDataAliasVector( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, DataType dataType); void createKeyPair( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, const KeyType key_type, const int additional_param, const Alias &privateKeyAlias, @@ -121,14 +119,14 @@ public: const Policy &policyPublicKey); void createKeyAES( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, const size_t size, const Alias &keyAlias, const Policy &policyKey); template <typename T> void getCertChain( - const ManagerAsync::ObserverPtr &observer, + const ObserverPtr &observer, LogicCommand command, const CertificateShPtr &certificate, const T &untrusted, @@ -138,7 +136,7 @@ public: if (!certificate || certificate->empty()) ThrowMsg(Exc::InputParam, "Empty certificate"); - sendToStorage(observer, static_cast<int>(command), m_counter, + sendToStorage(command, observer, certificate->getDER(), untrusted, trusted, useSystemTrustedCertificates); } @@ -150,22 +148,16 @@ public: const RawBuffer &input, bool encryption); - static void observerCheck(const ManagerAsync::ObserverPtr &observer); + static void observerCheck(const ObserverPtr &observer); private: template <typename... Args> - void sendToStorage(const ManagerAsync::ObserverPtr &observer, - int command, - const Args &... args) + void sendToStorage(LogicCommand command, const ObserverPtr &observer, Args &&... args) { - m_counter++; // yes, it changes m_counter argument passed in args - - auto send = SerializeMessage(command, args...); - thread()->sendMessage(AsyncRequest(observer, - SERVICE_SOCKET_CKM_STORAGE, - std::move(send), - m_counter, - command)); + m_counter++; + auto send = SerializeMessage(static_cast<int>(command), m_counter, std::forward<Args>(args)...); + thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_CKM_STORAGE, + std::move(send), m_counter, static_cast<int>(command))); } typedef std::unique_ptr<ConnectionThread> ConnectionThreadPtr; @@ -182,7 +174,7 @@ private: ConnectionThreadPtr m_thread; - static int m_counter; + static inline int m_counter = 0; }; } // namespace CKM |