Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/sdk/main/include/impl/CryptoObjectWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: Apache-2.0
#ifndef HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_
#define HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_

#include <functional>
#include <memory>

namespace Hiero::internal::OpenSSLUtils
{
/**
* Templated base wrapper class to be used for crypto library objects that require custom deleter and copier functions.
*
* @tparam ObjectType The type of crypto object this class should wrap.
* @tparam CopierFunc The copier type (function signature) for the crypto object.
*/
template<typename ObjectType, typename CopierFunc = std::function<ObjectType*(const ObjectType*)>>
class CryptoObjectWrapper
{
public:
virtual ~CryptoObjectWrapper() = default;

/**
* Copy constructor.
*
* @param other The CryptoObjectWrapper object to copy.
*/
CryptoObjectWrapper(const CryptoObjectWrapper& other)
: mObject({ other.mCopier(other.mObject.get()), other.mObject.get_deleter() })
, mCopier(other.mCopier)
{
}

/**
* Copy assignment operator.
*
* @param other The CryptoObjectWrapper object to copy.
*/
CryptoObjectWrapper& operator=(const CryptoObjectWrapper& other)
{
if (this != &other)
{
mObject = { other.mCopier(other.mObject.get()), other.mObject.get_deleter() };
mCopier = other.mCopier;
}

return *this;
}

CryptoObjectWrapper(CryptoObjectWrapper&&) noexcept = default;
CryptoObjectWrapper& operator=(CryptoObjectWrapper&&) noexcept = default;

/**
* Get the wrapped crypto object.
*
* @return A pointer to the wrapped crypto object. nullptr if no object exists.
*/
[[nodiscard]] ObjectType* get() { return mObject.get(); }
[[nodiscard]] const ObjectType* get() const { return mObject.get(); }

/**
* Release ownership of the wrapped crypto object. This will cause memory leaks if ownership has not already been
* taken by another object.
*/
void release() { mObject.release(); }

/**
* Determine if this CryptoObjectWrapper has a valid crypto object.
*
* @return \c TRUE if there exists a crypto object, otherwise \c FALSE.
*/
[[nodiscard]] explicit operator bool() const { return mObject != nullptr; }

protected:
CryptoObjectWrapper() = default;

/**
* Construct with values for the object, its custom deleter, and optionally a custom copier.
*
* @param object The crypto object to wrap.
* @param deleter The deleter function for the crypto object.
* @param copier The copier function for the crypto object.
*/
CryptoObjectWrapper(ObjectType* object,
const std::function<void(ObjectType*)>& deleter,
const CopierFunc& copier = CopierFunc())
: mObject({ object, deleter })
, mCopier(copier)
{
}

private:
/**
* Pointer to the crypto object with its associated deleter.
*/
std::unique_ptr<ObjectType, std::function<void(ObjectType*)>> mObject = { nullptr,
std::function<void(ObjectType*)>() };

/**
* The copier function to use to copy the wrapped crypto object.
*/
CopierFunc mCopier;
};

} // namespace Hiero::internal::OpenSSLUtils

#endif // HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/BIGNUM.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_BIGNUM_H_

#include "exceptions/OpenSSLException.h"
#include "impl/CryptoObjectWrapper.h"
#include "impl/HexConverter.h"
#include "impl/openssl_utils/BN_CTX.h"
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/openssl_utils/OpenSSLUtils.h"

#include <openssl/bn.h>
Expand All @@ -17,7 +17,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL BIGNUM object.
*/
class BIGNUM : public OpenSSLObjectWrapper<::BIGNUM>
class BIGNUM : public CryptoObjectWrapper<::BIGNUM>
{
public:
/**
Expand All @@ -26,7 +26,7 @@ class BIGNUM : public OpenSSLObjectWrapper<::BIGNUM>
* @param bignum The BIGNUM OpenSSL object to wrap.
*/
explicit BIGNUM(::BIGNUM* bignum)
: OpenSSLObjectWrapper(bignum, &BN_clear_free, &BN_dup)
: CryptoObjectWrapper(bignum, &BN_clear_free, &BN_dup)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/BN_CTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_BN_CTX_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_BN_CTX_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/bn.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL BN_CTX object.
*/
class BN_CTX : public OpenSSLObjectWrapper<::BN_CTX>
class BN_CTX : public CryptoObjectWrapper<::BN_CTX>
{
public:
/**
Expand All @@ -29,7 +29,7 @@ class BN_CTX : public OpenSSLObjectWrapper<::BN_CTX>
* @param bnCtx The BN_CTX OpenSSL object to wrap.
*/
explicit BN_CTX(::BN_CTX* bnCtx)
: OpenSSLObjectWrapper(bnCtx, &BN_CTX_free)
: CryptoObjectWrapper(bnCtx, &BN_CTX_free)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/ECDSA_SIG.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_ECDSA_SIG_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_ECDSA_SIG_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/ecdsa.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL ECDSA_SIG object.
*/
class ECDSA_SIG : public OpenSSLObjectWrapper<::ECDSA_SIG>
class ECDSA_SIG : public CryptoObjectWrapper<::ECDSA_SIG>
{
public:
/**
Expand All @@ -29,7 +29,7 @@ class ECDSA_SIG : public OpenSSLObjectWrapper<::ECDSA_SIG>
* @param ecdsaSig The ECDSA_SIG OpenSSL object to wrap.
*/
explicit ECDSA_SIG(::ECDSA_SIG* ecdsaSig)
: OpenSSLObjectWrapper(ecdsaSig, &ECDSA_SIG_free)
: CryptoObjectWrapper(ecdsaSig, &ECDSA_SIG_free)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/EC_GROUP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_GROUP_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_GROUP_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/ec.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL EC_GROUP object.
*/
class EC_GROUP : public OpenSSLObjectWrapper<::EC_GROUP>
class EC_GROUP : public CryptoObjectWrapper<::EC_GROUP>
{
public:
/**
Expand All @@ -20,7 +20,7 @@ class EC_GROUP : public OpenSSLObjectWrapper<::EC_GROUP>
* @param ecGroup The EC_GROUP OpenSSL object to wrap.
*/
explicit EC_GROUP(::EC_GROUP* ecGroup)
: OpenSSLObjectWrapper(ecGroup, &EC_GROUP_free, &EC_GROUP_dup)
: CryptoObjectWrapper(ecGroup, &EC_GROUP_free, &EC_GROUP_dup)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/EC_POINT.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_POINT_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_POINT_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/ec.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL EC_POINT object.
*/
class EC_POINT : public OpenSSLObjectWrapper<::EC_POINT>
class EC_POINT : public CryptoObjectWrapper<::EC_POINT>
{
public:
/**
Expand All @@ -29,7 +29,7 @@ class EC_POINT : public OpenSSLObjectWrapper<::EC_POINT>
* @param ecPoint The EC_POINT OpenSSL object to wrap.
*/
explicit EC_POINT(::EC_POINT* ecPoint)
: OpenSSLObjectWrapper(ecPoint, &EC_POINT_free)
: CryptoObjectWrapper(ecPoint, &EC_POINT_free)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/EVP_MD.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/evp.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL EVP_MD object.
*/
class EVP_MD : public OpenSSLObjectWrapper<::EVP_MD>
class EVP_MD : public CryptoObjectWrapper<::EVP_MD>
{
public:
/**
Expand All @@ -29,7 +29,7 @@ class EVP_MD : public OpenSSLObjectWrapper<::EVP_MD>
* @param evpMd The EVP_MD OpenSSL object to wrap.
*/
explicit EVP_MD(::EVP_MD* evpMd)
: OpenSSLObjectWrapper(evpMd, &EVP_MD_free)
: CryptoObjectWrapper(evpMd, &EVP_MD_free)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/EVP_MD_CTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_CTX_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_CTX_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/evp.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL EVP_MD_CTX object.
*/
class EVP_MD_CTX : public OpenSSLObjectWrapper<::EVP_MD_CTX>
class EVP_MD_CTX : public CryptoObjectWrapper<::EVP_MD_CTX>
{
public:
/**
Expand All @@ -29,7 +29,7 @@ class EVP_MD_CTX : public OpenSSLObjectWrapper<::EVP_MD_CTX>
* @param evpMdCtx The EVP_MD_CTX OpenSSL object to wrap.
*/
explicit EVP_MD_CTX(::EVP_MD_CTX* evpMdCtx)
: OpenSSLObjectWrapper(evpMdCtx, &EVP_MD_CTX_free)
: CryptoObjectWrapper(evpMdCtx, &EVP_MD_CTX_free)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/EVP_PKEY.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/evp.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL EVP_PKEY object.
*/
class EVP_PKEY : public OpenSSLObjectWrapper<::EVP_PKEY, decltype(&EVP_PKEY_dup)>
class EVP_PKEY : public CryptoObjectWrapper<::EVP_PKEY, decltype(&EVP_PKEY_dup)>
{
public:
EVP_PKEY() = default;
Expand All @@ -22,7 +22,7 @@ class EVP_PKEY : public OpenSSLObjectWrapper<::EVP_PKEY, decltype(&EVP_PKEY_dup)
* @param evpPkey The EVP_PKEY OpenSSL object to wrap.
*/
explicit EVP_PKEY(::EVP_PKEY* evpPkey)
: OpenSSLObjectWrapper(evpPkey, &EVP_PKEY_free, &EVP_PKEY_dup)
: CryptoObjectWrapper(evpPkey, &EVP_PKEY_free, &EVP_PKEY_dup)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/EVP_PKEY_CTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_CTX_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_CTX_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/evp.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL EVP_PKEY_CTX object.
*/
class EVP_PKEY_CTX : public OpenSSLObjectWrapper<::EVP_PKEY_CTX>
class EVP_PKEY_CTX : public CryptoObjectWrapper<::EVP_PKEY_CTX>
{
public:
/**
Expand All @@ -21,7 +21,7 @@ class EVP_PKEY_CTX : public OpenSSLObjectWrapper<::EVP_PKEY_CTX>
* @param evpPkeyCtx The EVP_PKEY_CTX OpenSSL object to wrap.
*/
explicit EVP_PKEY_CTX(::EVP_PKEY_CTX* evpPkeyCtx)
: OpenSSLObjectWrapper(evpPkeyCtx, &EVP_PKEY_CTX_free, &EVP_PKEY_CTX_dup)
: CryptoObjectWrapper(evpPkeyCtx, &EVP_PKEY_CTX_free, &EVP_PKEY_CTX_dup)
{
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/main/include/impl/openssl_utils/OSSL_DECODER_CTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_OSSL_DECODER_CTX_H_
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_OSSL_DECODER_CTX_H_

#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
#include "impl/CryptoObjectWrapper.h"

#include <openssl/decoder.h>

Expand All @@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
/**
* Wrapper class for the OpenSSL OSSL_DECODER_CTX object.
*/
class OSSL_DECODER_CTX : public OpenSSLObjectWrapper<::OSSL_DECODER_CTX>
class OSSL_DECODER_CTX : public CryptoObjectWrapper<::OSSL_DECODER_CTX>
{
public:
/**
Expand All @@ -29,7 +29,7 @@ class OSSL_DECODER_CTX : public OpenSSLObjectWrapper<::OSSL_DECODER_CTX>
* @param osslDecoderCtx The OSSL_DECODER_CTX OpenSSL object to wrap.
*/
explicit OSSL_DECODER_CTX(::OSSL_DECODER_CTX* osslDecoderCtx)
: OpenSSLObjectWrapper(osslDecoderCtx, &OSSL_DECODER_CTX_free)
: CryptoObjectWrapper(osslDecoderCtx, &OSSL_DECODER_CTX_free)
{
}
};
Expand Down
Loading