diff options
author | Konrad Lipinski <k.lipinski2@samsung.com> | 2020-09-03 19:29:58 +0200 |
---|---|---|
committer | Konrad Lipinski <k.lipinski2@samsung.com> | 2020-09-17 12:28:55 +0200 |
commit | d128a1d5f80597fcf8b410a40f80de43e5ecdd7c (patch) | |
tree | 1855e665c78aec84d259a35f7ea94641904ad79f | |
parent | 0d3d2d662edb1c80e25408b145a30da294fce66b (diff) | |
download | key-manager-d128a1d5f80597fcf8b410a40f80de43e5ecdd7c.tar.gz key-manager-d128a1d5f80597fcf8b410a40f80de43e5ecdd7c.tar.bz2 key-manager-d128a1d5f80597fcf8b410a40f80de43e5ecdd7c.zip |
Make custom unique_ptr deleters stateless
Change-Id: Ic82b018c4c9c0ca3d3e10f1f9a0b3632aa79d670
23 files changed, 129 insertions, 151 deletions
diff --git a/common/base64_generic.cpp b/common/base64_generic.cpp index 57879049..33a6ce4d 100644 --- a/common/base64_generic.cpp +++ b/common/base64_generic.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 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. @@ -15,6 +15,7 @@ */ #include <base64_generic.h> +#include <utils.h> #include <algorithm> #include <cctype> @@ -31,11 +32,11 @@ bool isBase64Char(char c) return isalnum(c) || c == '+' || c == '/' || c == '='; } -std::unique_ptr<BIO, decltype(&BIO_free)> makeBioPtr(BIO* ptr) +auto makeBioPtr(BIO* ptr) { if (!ptr) throw std::bad_alloc(); - return {ptr, &BIO_free}; + return uptr<BIO_free>(ptr); } } // anonymous namespace diff --git a/misc/ckm_initial_values/main.cpp b/misc/ckm_initial_values/main.cpp index eccb8058..755ebcb6 100644 --- a/misc/ckm_initial_values/main.cpp +++ b/misc/ckm_initial_values/main.cpp @@ -42,6 +42,7 @@ #include <libxml/parser.h> #include <base64_generic.h> +#include <utils.h> typedef std::vector<unsigned char> Buffer; typedef std::istreambuf_iterator<char> InputIterator; @@ -167,8 +168,7 @@ bool encrypt(const Buffer& data, const Buffer& key, Buffer& output, Buffer& iv, // FIXIT stream.read(reinterpret_cast<char*>(iv.data()), DEFAULT_IV_LEN); - std::unique_ptr<EVP_CIPHER_CTX, void (*)(EVP_CIPHER_CTX *)> ctx(EVP_CIPHER_CTX_new(), - EVP_CIPHER_CTX_free); + auto ctx = uptr<EVP_CIPHER_CTX_free>(EVP_CIPHER_CTX_new()); if (!ctx) { std::cerr << "EVP_CIPHER_CTX_new() failed" << std::endl; diff --git a/src/manager/client-capi/ckmc-manager.cpp b/src/manager/client-capi/ckmc-manager.cpp index 39bb8542..86cc153f 100644 --- a/src/manager/client-capi/ckmc-manager.cpp +++ b/src/manager/client-capi/ckmc-manager.cpp @@ -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. @@ -31,6 +31,7 @@ #include <functional> #include <iostream> #include <string.h> +#include <utils.h> namespace { const CKM::CertificateShPtrVector EMPTY_CERT_VECTOR; @@ -511,10 +512,8 @@ int ckmc_get_pkcs12(const char *alias, const char *key_password, int ret; CKM::PKCS12ShPtr pkcs; auto mgr = CKM::Manager::create(); - std::unique_ptr<ckmc_key_s, decltype(&ckmc_key_free)> private_key_uptr( - NULL, ckmc_key_free); - std::unique_ptr<ckmc_cert_s, decltype(&ckmc_cert_free)> cert_uptr( - NULL, ckmc_cert_free); + Uptr<ckmc_key_free, ckmc_key_s> private_key_uptr; + Uptr<ckmc_cert_free, ckmc_cert_s> cert_uptr; if ((ret = mgr->getPKCS12(alias, _tostring(key_password), _tostring(cert_password), pkcs)) != CKM_API_SUCCESS) @@ -557,9 +556,8 @@ int ckmc_get_pkcs12(const char *alias, const char *key_password, cert_uptr.reset(cert); } - std::unique_ptr<ckmc_cert_list_s, decltype(&ckmc_cert_list_free)> cert_list_uptr( - _toNewCkmCertList(pkcs->getCaCertificateShPtrVector()), - ckmc_cert_list_free); + auto cert_list_uptr = uptr<ckmc_cert_list_free>( + _toNewCkmCertList(pkcs->getCaCertificateShPtrVector())); ret = ckmc_pkcs12_new(private_key_uptr.get(), cert_uptr.get(), cert_list_uptr.get(), pkcs12); if (ret == CKMC_ERROR_NONE) { diff --git a/src/manager/common/key-impl.cpp b/src/manager/common/key-impl.cpp index bc917ec5..8a4ef619 100644 --- a/src/manager/common/key-impl.cpp +++ b/src/manager/common/key-impl.cpp @@ -1,4 +1,4 @@ -/* 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. @@ -34,12 +34,11 @@ #include <ckm/ckm-type.h> #include <key-impl.h> +#include <utils.h> namespace CKM { namespace { -typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr; - int passcb(char *buff, int size, int /*rwflag*/, void *userdata) { auto ptr = static_cast<Password *>(userdata); @@ -56,7 +55,7 @@ typedef int(*I2D_CONV)(BIO *, EVP_PKEY *); CKM::RawBuffer i2d(I2D_CONV fun, EVP_PKEY *pkey) { - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem())); if (pkey == nullptr || !bio) return RawBuffer(); @@ -94,7 +93,7 @@ KeyImpl::KeyImpl(const RawBuffer &buf, const Password &password) : return; } - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem())); LogDebug("Start to parse key:"); diff --git a/src/manager/common/openssl-error-handler.cpp b/src/manager/common/openssl-error-handler.cpp index 9d2bf372..a43c4247 100644 --- a/src/manager/common/openssl-error-handler.cpp +++ b/src/manager/common/openssl-error-handler.cpp @@ -26,6 +26,7 @@ #include <ckm/ckm-error.h> #include <dpl/log/log.h> +#include <utils.h> #include "openssl-error-handler.h" #include <exception> @@ -59,8 +60,7 @@ const char *ckm_debug_translate_error(int err) void errorDump() { - typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr; - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem())); if (!bio.get()) return; diff --git a/src/manager/common/openssl_utils.h b/src/manager/common/openssl_utils.h index a552b22a..d3e25f0c 100644 --- a/src/manager/common/openssl_utils.h +++ b/src/manager/common/openssl_utils.h @@ -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. @@ -23,24 +23,13 @@ #include <openssl/x509.h> -#include <memory> +#include <utils.h> namespace CKM { -typedef std::unique_ptr<X509_STORE_CTX, void(*)(X509_STORE_CTX *)> -X509_STORE_CTX_PTR; -typedef std::unique_ptr<STACK_OF(X509), void(*)(STACK_OF(X509) *)> -X509_STACK_PTR; - -inline X509_STACK_PTR create_x509_stack() -{ - return X509_STACK_PTR(sk_X509_new_null(), [](STACK_OF(X509) * stack) { - sk_X509_free(stack); - }); -} -inline X509_STORE_CTX_PTR create_x509_store_ctx() +inline auto create_x509_stack() { - return X509_STORE_CTX_PTR(X509_STORE_CTX_new(), X509_STORE_CTX_free); + return uptr<sk_X509_free>(sk_X509_new_null()); } } // namespace CKM diff --git a/src/manager/common/pkcs12-impl.cpp b/src/manager/common/pkcs12-impl.cpp index 814781bb..4191e726 100644 --- a/src/manager/common/pkcs12-impl.cpp +++ b/src/manager/common/pkcs12-impl.cpp @@ -24,6 +24,7 @@ #include <openssl/x509.h> #include <dpl/log/log.h> +#include <utils.h> #include <pkcs12-impl.h> @@ -33,11 +34,6 @@ #include <functional> namespace CKM { -namespace { - -typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr; - -} // anonymous namespace PKCS12Impl::PKCS12Impl(const KeyShPtr &key, const CertificateShPtr &cert, const CertificateShPtrVector &caChain) @@ -54,7 +50,7 @@ PKCS12Impl::PKCS12Impl(const RawBuffer &buffer, const Password &password) STACK_OF(X509) *ca = NULL; ::PKCS12 *pkcs12 = NULL; - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem())); LogDebug("Start to parse PKCS12"); int result = BIO_write(bio.get(), buffer.data(), buffer.size()); diff --git a/src/manager/common/utils.h b/src/manager/common/utils.h new file mode 100644 index 00000000..70420c3a --- /dev/null +++ b/src/manager/common/utils.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#pragma once + +#include <memory> + +template <auto Free, class Pointee> +auto uptr(Pointee *p) +{ + struct Deleter + { + void operator()(Pointee *p) + { + Free(p); + } + }; + return std::unique_ptr<Pointee, Deleter>(p); +} + +template <auto Free, class Pointee> +using Uptr = decltype(uptr<Free, Pointee>(nullptr)); diff --git a/src/manager/crypto/sw-backend/internals.cpp b/src/manager/crypto/sw-backend/internals.cpp index a2a90542..2b08293e 100644 --- a/src/manager/crypto/sw-backend/internals.cpp +++ b/src/manager/crypto/sw-backend/internals.cpp @@ -37,6 +37,7 @@ #include <ckm/ckm-error.h> #include <dpl/log/log.h> +#include <utils.h> #include <generic-backend/exception.h> #include <generic-backend/algo-validation.h> @@ -55,19 +56,14 @@ namespace SW { namespace Internals { namespace { -typedef std::unique_ptr<EVP_MD_CTX, std::function<void(EVP_MD_CTX *)>> - EvpMdCtxUPtr; -typedef std::unique_ptr<EVP_PKEY_CTX, std::function<void(EVP_PKEY_CTX *)>> - EvpPkeyCtxUPtr; -typedef std::unique_ptr<EVP_PKEY, std::function<void(EVP_PKEY *)>> EvpPkeyUPtr; +typedef Uptr<EVP_PKEY_CTX_free, EVP_PKEY_CTX> EvpPkeyCtxUPtr; -typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr; typedef int(*I2D_CONV)(BIO *, EVP_PKEY *); RawBuffer i2d(I2D_CONV fun, EVP_PKEY *pkey) { - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem())); if (NULL == pkey) ThrowErr(Exc::Crypto::InternalError, "attempt to parse an empty key!"); @@ -383,7 +379,7 @@ int getRsaPadding(const RSAPaddingAlgorithm padAlgo) EvpPkeyCtxUPtr newCtx(int id) { if (auto ctx = EVP_PKEY_CTX_new_id(id, NULL)) - return EvpPkeyCtxUPtr(ctx, EVP_PKEY_CTX_free); + return EvpPkeyCtxUPtr(ctx); ThrowErr(Exc::Crypto::InternalError, "Error in EVP_PKEY_CTX_new_id function"); } @@ -391,7 +387,7 @@ EvpPkeyCtxUPtr newCtx(int id) EvpPkeyCtxUPtr newCtx(EVP_PKEY *pkey) { if (auto ctx = EVP_PKEY_CTX_new(pkey, NULL)) - return EvpPkeyCtxUPtr(ctx, EVP_PKEY_CTX_free); + return EvpPkeyCtxUPtr(ctx); ThrowErr(Exc::Crypto::InternalError, "Error in EVP_PKEY_CTX_new function"); } @@ -402,7 +398,7 @@ DataPair keyPair(const EvpPkeyCtxUPtr &ctx, KeyType prv, KeyType pub) OPENSSL_ERROR_HANDLE(EVP_PKEY_keygen(ctx.get(), &pkeyTmp)); - auto pkey = EvpPkeyUPtr(pkeyTmp, EVP_PKEY_free); + auto pkey = uptr<EVP_PKEY_free>(pkeyTmp); return std::make_pair<Data, Data>( {DataType(prv), i2d(i2d_PrivateKey_bio, pkey.get())}, @@ -429,7 +425,7 @@ DataPair paramgenKeyPair(const EvpPkeyCtxUPtr &pctx, KeyType prv, KeyType pub) EVP_PKEY *pparamTmp = NULL; OPENSSL_ERROR_HANDLE(EVP_PKEY_paramgen(pctx.get(), &pparamTmp)); - auto pparam = EvpPkeyUPtr(pparamTmp, EVP_PKEY_free); + auto pparam = uptr<EVP_PKEY_free>(pparamTmp); // Start to generate key auto kctx = newCtx(pparam.get()); @@ -539,7 +535,7 @@ RawBuffer digestSignMessage(EVP_PKEY *privKey, EVP_PKEY_CTX *pctx = NULL; // Create the Message Digest Context - EvpMdCtxUPtr mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free); + auto mdctx = uptr<EVP_MD_CTX_free>(EVP_MD_CTX_new()); if (!mdctx.get()) ThrowErr(Exc::Crypto::InternalError, "Error in EVP_MD_CTX_new function"); @@ -603,7 +599,7 @@ int digestVerifyMessage(EVP_PKEY *pubKey, EVP_PKEY_CTX *pctx = NULL; // Create the Message Digest Context - EvpMdCtxUPtr mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free); + auto mdctx = uptr<EVP_MD_CTX_free>(EVP_MD_CTX_new()); if (!mdctx.get()) ThrowErr(Exc::Crypto::InternalError, "Error in EVP_MD_CTX_new function"); diff --git a/src/manager/crypto/sw-backend/obj.cpp b/src/manager/crypto/sw-backend/obj.cpp index fb8d007c..c14998f0 100644 --- a/src/manager/crypto/sw-backend/obj.cpp +++ b/src/manager/crypto/sw-backend/obj.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 - 2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015-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. @@ -18,13 +18,12 @@ * @author Bartłomiej Grzelewski (b.grzelewski@samsung.com) * @version 1.0 */ -#include <memory> - #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/x509.h> #include <dpl/log/log.h> +#include <utils.h> #include <generic-backend/exception.h> #include <sw-backend/obj.h> @@ -58,8 +57,6 @@ AlgoType key2algo(DataType type) } // namespace anonymous -typedef std::unique_ptr<BIO, std::function<void(BIO *)>> BioUniquePtr; - RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data) { return Internals::symmetricEncrypt(getBinary(), alg, data); @@ -128,7 +125,7 @@ EvpShPtr AKey::getEvpShPtr() return m_evp; EVP_PKEY *pkey = NULL; - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem())); LogDebug("Start to parse key:"); diff --git a/src/manager/crypto/sw-backend/obj.h b/src/manager/crypto/sw-backend/obj.h index aeed086a..17a69f40 100644 --- a/src/manager/crypto/sw-backend/obj.h +++ b/src/manager/crypto/sw-backend/obj.h @@ -31,8 +31,6 @@ namespace CKM { namespace Crypto { namespace SW { -typedef std::unique_ptr<EVP_PKEY_CTX, std::function<void(EVP_PKEY_CTX *)>> - ContextUPtr; typedef std::shared_ptr<EVP_PKEY> EvpShPtr; class BData : public GObj { diff --git a/src/manager/crypto/tz-backend/internals.cpp b/src/manager/crypto/tz-backend/internals.cpp index 9d8efe93..8b72f48e 100644 --- a/src/manager/crypto/tz-backend/internals.cpp +++ b/src/manager/crypto/tz-backend/internals.cpp @@ -24,6 +24,7 @@ #include <generic-backend/algo-validation.h> #include <generic-backend/crypto-params.h> #include <dpl/log/log.h> +#include <utils.h> #include <openssl/evp.h> #include <openssl/dsa.h> #include <openssl/bio.h> @@ -48,8 +49,6 @@ namespace { -using DSAPtr = std::unique_ptr<DSA, std::function<void(DSA*)>>; - CKM::RawBuffer extractBignumData(const BIGNUM* bn) { size_t size = static_cast<size_t>(BN_num_bytes(bn)); @@ -68,7 +67,7 @@ CKM::RawBuffer extractBignumData(const BIGNUM* bn) void generateDSAParams(const int sizeBits, CKM::RawBuffer &prime, CKM::RawBuffer &subprime, CKM::RawBuffer &base) { - DSAPtr dsa(DSA_new(), DSA_free); + auto dsa = uptr<DSA_free>(DSA_new()); if (!dsa) { ThrowErr(CKM::Exc::Crypto::InternalError, "Failed to create DSA context for parameter gen"); diff --git a/src/manager/dpl/core/include/dpl/scoped_ptr.h b/src/manager/dpl/core/include/dpl/scoped_ptr.h index 0c0a1910..7902b85f 100644 --- a/src/manager/dpl/core/include/dpl/scoped_ptr.h +++ b/src/manager/dpl/core/include/dpl/scoped_ptr.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 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. @@ -23,16 +23,9 @@ #ifndef SCOPED_PTR_H #define SCOPED_PTR_H -#include <memory> +#include <utils.h> namespace CKM { -struct free_deleter { - void operator()(char *p) - { - free(p); - } -}; - -typedef std::unique_ptr<char, free_deleter> CharUniquePtr; +typedef Uptr<free, char> CharUniquePtr; } #endif // SCOPED_PTR_H diff --git a/src/manager/initial-values/parser.cpp b/src/manager/initial-values/parser.cpp index 4bb0b6ab..aa324bac 100644 --- a/src/manager/initial-values/parser.cpp +++ b/src/manager/initial-values/parser.cpp @@ -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. @@ -30,6 +30,7 @@ #include <parser.h> #include <xml-utils.h> #include <dpl/log/log.h> +#include <utils.h> namespace CKM { namespace XML { @@ -62,11 +63,6 @@ Parser::~Parser() xmlThrDefSetGenericErrorFunc(NULL, NULL); } -using SchemaParserCtxt = - std::unique_ptr<xmlSchemaParserCtxt, void(*)(xmlSchemaParserCtxtPtr)>; -using Schema = std::unique_ptr<xmlSchema, void(*)(xmlSchemaPtr)>; -using SchemaValidCtxt = - std::unique_ptr<xmlSchemaValidCtxt, void(*)(xmlSchemaValidCtxtPtr)>; int Parser::Validate(const std::string &XSD_schema) { if (XSD_schema.empty()) { @@ -75,10 +71,7 @@ int Parser::Validate(const std::string &XSD_schema) } int retCode; - SchemaParserCtxt parserCtxt(xmlSchemaNewParserCtxt(XSD_schema.c_str()), - [](xmlSchemaParserCtxtPtr ctx) { - xmlSchemaFreeParserCtxt(ctx); - }); + auto parserCtxt = uptr<xmlSchemaFreeParserCtxt>(xmlSchemaNewParserCtxt(XSD_schema.c_str())); if (!parserCtxt) { LogError("XSD file path is invalid"); @@ -88,19 +81,14 @@ int Parser::Validate(const std::string &XSD_schema) xmlSetGenericErrorFunc(this, &Parser::ErrorValidate); xmlThrDefSetGenericErrorFunc(this, &Parser::ErrorValidate); - Schema schema(xmlSchemaParse(parserCtxt.get()), [](xmlSchemaPtr schemaPtr) { - xmlSchemaFree(schemaPtr); - }); + auto schema = uptr<xmlSchemaFree>(xmlSchemaParse(parserCtxt.get())); if (!schema) { LogError("Parsing XSD file failed"); return ERROR_XSD_PARSE_FAILED; } - SchemaValidCtxt validCtxt(xmlSchemaNewValidCtxt(schema.get()), []( - xmlSchemaValidCtxtPtr validCtxPtr) { - xmlSchemaFreeValidCtxt(validCtxPtr); - }); + auto validCtxt = uptr<xmlSchemaFreeValidCtxt>(xmlSchemaNewValidCtxt(schema.get())); if (!validCtxt) { LogError("Internal parser error"); diff --git a/src/manager/service/certificate-store.cpp b/src/manager/service/certificate-store.cpp index 195316b5..2a8a44cb 100644 --- a/src/manager/service/certificate-store.cpp +++ b/src/manager/service/certificate-store.cpp @@ -1,4 +1,4 @@ -/* 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. @@ -62,7 +62,7 @@ int CertificateStore::verifyCertificate( " and system certificates set to: " << useTrustedSystemCertificates); - X509_STORE_CTX_PTR csc = create_x509_store_ctx(); + auto csc = uptr<X509_STORE_CTX_free>(X509_STORE_CTX_new()); if (!csc) { LogError("failed to create csc"); @@ -87,7 +87,7 @@ int CertificateStore::verifyCertificate( return ret; // create stack of untrusted certificates - X509_STACK_PTR untrusted = create_x509_stack(); + auto untrusted = create_x509_stack(); if (!untrustedVector.empty()) { for (auto &e : untrustedVector) { diff --git a/src/manager/service/db-crypto.cpp b/src/manager/service/db-crypto.cpp index c115d052..d7f7c1c0 100644 --- a/src/manager/service/db-crypto.cpp +++ b/src/manager/service/db-crypto.cpp @@ -32,6 +32,7 @@ #include <dpl/scoped_ptr.h> #include <ckm/ckm-error.h> #include <exception.h> +#include <utils.h> #pragma GCC diagnostic push #pragma GCC diagnostic warning "-Wdeprecated-declarations" @@ -182,8 +183,7 @@ void convertLegacyDatabase(const std::string &legacyPath, const std::string &pat ThrowErr(Exc::DatabaseFailed, "unlink failed: " << GetErrnoString()); // in no way to I condone the use of unique_ptr in this context; see: review - struct Dlclose { void operator()(void *p) { dlclose(p); } }; - const auto handle = std::unique_ptr<void, Dlclose>( + const auto handle = uptr<dlclose>( dlopen(LIB_INSTALL_DIR "/lib" DUMP_LEGACY_DB_LIBNAME ".so", RTLD_LAZY) ?: ThrowErr(Exc::DatabaseFailed, "dlopen failed: " << dlerror())); const auto dumpLegacyDb = (char *(*)(const char *, const unsigned char *, size_t)) diff --git a/src/manager/service/for-each-file.cpp b/src/manager/service/for-each-file.cpp index 01215473..f2d9d486 100644 --- a/src/manager/service/for-each-file.cpp +++ b/src/manager/service/for-each-file.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016-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,7 +21,6 @@ */ #include "for-each-file.h" -#include <memory> #include <cstddef> #include <dirent.h> #include <unistd.h> @@ -29,13 +28,13 @@ #include <dpl/errno_string.h> #include <exception.h> +#include <utils.h> namespace CKM { void forEachFile(const std::string &dirpath, ActionFunc func) { - std::unique_ptr<DIR, std::function<int(DIR *)>> - dirp(::opendir(dirpath.c_str()), ::closedir); + auto dirp = uptr<::closedir>(::opendir(dirpath.c_str())); if (!dirp.get()) ThrowErr(Exc::FileSystemFailed, @@ -44,8 +43,7 @@ void forEachFile(const std::string &dirpath, ActionFunc func) size_t len = offsetof(struct dirent, d_name) + pathconf(dirpath.c_str(), _PC_NAME_MAX) + 1; - std::unique_ptr<struct dirent, std::function<void(void *)>> - pEntry(static_cast<struct dirent *>(::malloc(len)), ::free); + auto pEntry = uptr<::free>(static_cast<struct dirent *>(::malloc(len))); if (!pEntry) ThrowErr(Exc::InternalError, "Memory allocation failed for dir entry"); diff --git a/src/manager/service/glib-logic.cpp b/src/manager/service/glib-logic.cpp index b64cb14e..aef22194 100644 --- a/src/manager/service/glib-logic.cpp +++ b/src/manager/service/glib-logic.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2016 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. @@ -23,12 +23,12 @@ #include <unistd.h> #include <set> -#include <memory> #include <glib.h> #include <package-manager.h> #include <dpl/log/log.h> +#include <utils.h> #include <glib-logic.h> #ifdef WATCHDOG_ENABLED @@ -86,8 +86,7 @@ void GLIBLogic::LoopStart() { LogDebug("Register uninstalledApp event callback start"); - std::unique_ptr<pkgmgr_client, int(*)(pkgmgr_client *)> client( - pkgmgr_client_new(PC_LISTENING), pkgmgr_client_free); + auto client = uptr<pkgmgr_client_free>(pkgmgr_client_new(PC_LISTENING)); if (!client) { LogError("Error in pkgmgr_client_new"); diff --git a/src/manager/service/key-provider.cpp b/src/manager/service/key-provider.cpp index 6ba0bb81..ddf9adf4 100644 --- a/src/manager/service/key-provider.cpp +++ b/src/manager/service/key-provider.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 - 2019 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. @@ -18,10 +18,10 @@ #include <key-provider.h> #include <dpl/log/log.h> #include <ckm/ckm-zero-memory.h> +#include <utils.h> #include <string.h> #include <array> -#include <memory> using namespace CKM; @@ -47,8 +47,6 @@ RawBuffer toRawBuffer(T *) return RawBuffer(); } -typedef std::unique_ptr<EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)> CipherCtxPtr; - int encryptAes256Gcm(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, const unsigned char *iv, unsigned char *ciphertext, unsigned char *tag) @@ -56,7 +54,7 @@ int encryptAes256Gcm(const unsigned char *plaintext, int len; int ciphertext_len = 0; - CipherCtxPtr ctx(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); + auto ctx = uptr<EVP_CIPHER_CTX_free>(EVP_CIPHER_CTX_new()); if (!ctx) return OPENSSL_ENGINE_ERROR; @@ -93,7 +91,7 @@ int decryptAes256Gcm(const unsigned char *ciphertext, int plaintext_len; int ret; - CipherCtxPtr ctx(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); + auto ctx = uptr<EVP_CIPHER_CTX_free>(EVP_CIPHER_CTX_new()); if (!ctx) return OPENSSL_ENGINE_ERROR; diff --git a/src/manager/service/ocsp.cpp b/src/manager/service/ocsp.cpp index 72b67e7e..aac943a9 100644 --- a/src/manager/service/ocsp.cpp +++ b/src/manager/service/ocsp.cpp @@ -34,6 +34,7 @@ #include <dpl/log/log.h> #include <openssl_utils.h> #include <ckm/ckm-error.h> +#include <utils.h> #include <vconf.h> @@ -64,18 +65,12 @@ void BIO_write_and_free(BIO *bio) BIO_free_all(bio); } -template <class Pointee, class Free> -auto uptr(Pointee *p, Free f) +void opensslFree(char *p) { - return std::unique_ptr<Pointee, Free>(p, f); + OPENSSL_free(p); // it's a macro.. } -auto opensslStrUptr() -{ - return uptr<char>(NULL, [](auto p) { OPENSSL_free(p); }); -} - -using OpensslStrUptr = decltype(opensslStrUptr()); +using OpensslStrUptr = Uptr<opensslFree, char>; int parseUrl(const char *url, OpensslStrUptr &host, OpensslStrUptr &port, OpensslStrUptr &path, int &use_ssl) { @@ -94,7 +89,7 @@ int parseUrl(const char *url, OpensslStrUptr &host, OpensslStrUptr &port, Openss return 1; } -int canonicalizeProxy(std::unique_ptr<char, decltype(free)*> &proxy) +int canonicalizeProxy(Uptr<free, char> &proxy) { if (!proxy || proxy.get()[0] == '\0') { proxy.reset(); @@ -121,15 +116,15 @@ int canonicalizeProxy(std::unique_ptr<char, decltype(free)*> &proxy) int ocspDoVerify(X509 *cert, X509 *issuer, STACK_OF(X509) *trustedCerts, const std::string &url) { - const auto bioLogger = uptr(BIO_new(BIO_s_mem()), BIO_write_and_free); + const auto bioLogger = uptr<BIO_write_and_free>(BIO_new(BIO_s_mem())); if (!bioLogger) { LogDebug("Error in BIO_new(BIO_s_mem())"); return CKM_API_OCSP_STATUS_INTERNAL_ERROR; } - auto host = opensslStrUptr(); - auto port = opensslStrUptr(); - auto path = opensslStrUptr(); + OpensslStrUptr host; + OpensslStrUptr port; + OpensslStrUptr path; int use_ssl = 0; if (!parseUrl(url.c_str(), host, port, path, use_ssl)) { @@ -143,19 +138,19 @@ int ocspDoVerify(X509 *cert, X509 *issuer, LogDebug("Path: " << path.get()); LogDebug("Use_ssl: " << use_ssl); - auto proxy = uptr(vconf_get_str(VCONFKEY_NETWORK_PROXY), free); + auto proxy = uptr<free>(vconf_get_str(VCONFKEY_NETWORK_PROXY)); if (!canonicalizeProxy(proxy)) { return CKM_API_OCSP_STATUS_INTERNAL_ERROR; } if (proxy) { - auto dummyPath = opensslStrUptr(); + OpensslStrUptr dummyPath; if (!parseUrl(proxy.get(), host, port, dummyPath, use_ssl)) { return CKM_API_OCSP_STATUS_INVALID_URL; } } - auto cbio = uptr(BIO_new_connect(host.get()), BIO_free_all); + auto cbio = uptr<BIO_free_all>(BIO_new_connect(host.get())); if (!cbio) { LogError("Connection to ocsp host failed: " << host.get()); return CKM_API_OCSP_STATUS_INTERNAL_ERROR; @@ -164,7 +159,7 @@ int ocspDoVerify(X509 *cert, X509 *issuer, if (port.get()[0] != '\0') BIO_set_conn_port(cbio.get(), port.get()); - auto use_ssl_ctx = uptr<SSL_CTX>(NULL, SSL_CTX_free); + Uptr<SSL_CTX_free, SSL_CTX> use_ssl_ctx; if (use_ssl) { use_ssl_ctx.reset(SSL_CTX_new(SSLv23_client_method())); @@ -191,7 +186,7 @@ int ocspDoVerify(X509 *cert, X509 *issuer, return CKM_API_OCSP_STATUS_NET_ERROR; } - const auto req = uptr(OCSP_REQUEST_new(), OCSP_REQUEST_free); + const auto req = uptr<OCSP_REQUEST_free>(OCSP_REQUEST_new()); if (!req) { LogDebug("Error in OCPS_REQUEST_new"); return CKM_API_OCSP_STATUS_INTERNAL_ERROR; @@ -208,8 +203,8 @@ int ocspDoVerify(X509 *cert, X509 *issuer, return CKM_API_OCSP_STATUS_INTERNAL_ERROR; } - const auto ctx = uptr(OCSP_sendreq_new(cbio.get(), proxy ? url.c_str() : path.get(), NULL, -1), - OCSP_REQ_CTX_free); + const auto ctx = uptr<OCSP_REQ_CTX_free>( + OCSP_sendreq_new(cbio.get(), proxy ? url.c_str() : path.get(), NULL, -1)); if (!ctx) { LogError("Error creating OCSP_REQ_CTX"); return CKM_API_OCSP_STATUS_INTERNAL_ERROR; @@ -231,7 +226,7 @@ int ocspDoVerify(X509 *cert, X509 *issuer, return CKM_API_OCSP_STATUS_INTERNAL_ERROR; } - auto resp = uptr<OCSP_RESPONSE>(NULL, OCSP_RESPONSE_free); + Uptr<OCSP_RESPONSE_free, OCSP_RESPONSE> resp; for (;;) { OCSP_RESPONSE *tmpResp = NULL; int rv = OCSP_sendreq_nbio(&tmpResp, ctx.get()); @@ -272,14 +267,14 @@ int ocspDoVerify(X509 *cert, X509 *issuer, return CKM_API_OCSP_STATUS_REMOTE_ERROR; } - const auto bs = uptr(OCSP_response_get1_basic(resp.get()), OCSP_BASICRESP_free); + const auto bs = uptr<OCSP_BASICRESP_free>(OCSP_response_get1_basic(resp.get())); if (!bs) { ERR_print_errors(bioLogger.get()); LogDebug("Error in OCSP_response_get1_basic"); return CKM_API_OCSP_STATUS_INVALID_RESPONSE; } - auto trustedStore = uptr<X509_STORE>(NULL, X509_STORE_free); + Uptr<X509_STORE_free, X509_STORE> trustedStore; if (trustedCerts != NULL) { trustedStore.reset(X509_STORE_new()); @@ -292,7 +287,7 @@ int ocspDoVerify(X509 *cert, X509 *issuer, // Additional certificates to search for signer. // OCSP response may not contain issuer certificate in this case // we must pass it by 'other' certificates. - X509_STACK_PTR verifyOther = create_x509_stack(); + auto verifyOther = create_x509_stack(); sk_X509_push(verifyOther.get(), issuer); if (OCSP_basic_verify(bs.get(), verifyOther.get(), trustedStore.get(), 0) <= 0) { @@ -352,7 +347,7 @@ int ocspVerify(const CertificateImplVector &certificateChain) false; // ocsp is unsupported in certificate in chain (except root CA) // create trusted store - X509_STACK_PTR trustedCerts = create_x509_stack(); + auto trustedCerts = create_x509_stack(); // skip first 2 certificates for (auto it = certificateChain.cbegin() + 2; it < certificateChain.cend(); diff --git a/src/manager/service/ss-crypto.cpp b/src/manager/service/ss-crypto.cpp index f6a2de22..fe49f06e 100644 --- a/src/manager/service/ss-crypto.cpp +++ b/src/manager/service/ss-crypto.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016-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,6 +29,7 @@ #include <openssl/hmac.h> #include <dpl/log/log.h> +#include <utils.h> // lengths defined as macro to be used independent to type (size_t, unsigned int, int) #define SALT_SIZE 32 @@ -96,8 +97,7 @@ RawBuffer _decrypt(const RawBuffer &key, const RawBuffer &iv, const RawBuffer &c RawBuffer plaintext(tmp_len, 0); - std::unique_ptr<EVP_CIPHER_CTX, void(*)(EVP_CIPHER_CTX *)> ctxptr( - ::EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free); + auto ctxptr = uptr<::EVP_CIPHER_CTX_free>(::EVP_CIPHER_CTX_new()); if (ctxptr == nullptr) throw std::bad_alloc(); diff --git a/src/manager/service/ss-migrate.cpp b/src/manager/service/ss-migrate.cpp index cb446d9b..88f2cbf4 100644 --- a/src/manager/service/ss-migrate.cpp +++ b/src/manager/service/ss-migrate.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016-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. @@ -22,7 +22,6 @@ #include <ss-migrate.h> #include <fstream> -#include <memory> #include <cerrno> #include <cstddef> #include <unistd.h> @@ -32,6 +31,7 @@ #include <dpl/log/log.h> #include <ss-crypto.h> +#include <utils.h> namespace CKM { namespace SsMigration { @@ -90,7 +90,7 @@ void visit_dir(const std::string &dirpath, struct dirent *buf, size_t depth, return; } - std::unique_ptr<DIR, int(*)(DIR *)> dirptr(::opendir(dirpath.c_str()), ::closedir); + auto dirptr = uptr<::closedir>(::opendir(dirpath.c_str())); if (dirptr == nullptr) { LogError("Failed to open dir: " << dirpath << " with errno: " << errno); return; @@ -178,9 +178,9 @@ void migrate(bool isAdminUser, const Saver &saver) return; } - std::unique_ptr<struct dirent, void(*)(void *)> bufptr( + auto bufptr = uptr<::free>( static_cast<struct dirent *>(::malloc( - offsetof(struct dirent, d_name) + NAME_MAX + 1)), ::free); + offsetof(struct dirent, d_name) + NAME_MAX + 1))); if (bufptr == nullptr) throw std::bad_alloc(); diff --git a/unit-tests/test_descriptor-set.cpp b/unit-tests/test_descriptor-set.cpp index fb985bbc..83b6b653 100644 --- a/unit-tests/test_descriptor-set.cpp +++ b/unit-tests/test_descriptor-set.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. @@ -39,6 +39,7 @@ #include <descriptor-set.h> #include <dpl/errno_string.h> +#include <utils.h> using namespace CKM; @@ -47,8 +48,6 @@ namespace { const int POLL_TIMEOUT = 8000; const int POLL_TIMEOUT_SHORT = 1000; -typedef std::unique_ptr<int[], std::function<void(int *)>> PipePtr; - const short POLLALL = std::numeric_limits<short>::max(); void closePipe(int *fd) @@ -65,7 +64,7 @@ void closePipe(int *fd) #define PIPE(fd) \ int (fd)[2]; \ BOOST_REQUIRE_MESSAGE(0 == pipe((fd)), "Pipe creation failed: " << GetErrnoString()); \ - PipePtr fd##Ptr((fd), closePipe); + auto fd##Ptr = uptr<closePipe>((fd)); void unexpectedCallback(int, short) { |