Skip to content

Commit 083165f

Browse files
committed
refactor: rename OpenSSLObjectWrapper to CryptoObjectWrapper
Rename the generic RAII wrapper from OpenSSLObjectWrapper to CryptoObjectWrapper and move it from impl/openssl_utils/ to impl/, since the wrapper is not OpenSSL-specific and is also used by libsecp256k1 types (Secp256k1Context). Changes: - Created impl/CryptoObjectWrapper.h with the renamed class - Deleted impl/openssl_utils/OpenSSLObjectWrapper.h - Updated all 12 subclass headers to use the new include path and inherit from CryptoObjectWrapper instead - Updated all docstrings to say 'crypto object' instead of 'OpenSSL object' Signed-off-by: hk2166 <9610hemant@gmail.com>
1 parent d1ed2d0 commit 083165f

14 files changed

Lines changed: 142 additions & 142 deletions
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
#ifndef HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_
3+
#define HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_
4+
5+
#include <functional>
6+
#include <memory>
7+
8+
namespace Hiero::internal::OpenSSLUtils
9+
{
10+
/**
11+
* Templated base wrapper class to be used for crypto library objects that require custom deleter and copier functions.
12+
*
13+
* @tparam ObjectType The type of crypto object this class should wrap.
14+
* @tparam CopierFunc The copier type (function signature) for the crypto object.
15+
*/
16+
template<typename ObjectType, typename CopierFunc = std::function<ObjectType*(const ObjectType*)>>
17+
class CryptoObjectWrapper
18+
{
19+
public:
20+
virtual ~CryptoObjectWrapper() = default;
21+
22+
/**
23+
* Copy constructor.
24+
*
25+
* @param other The CryptoObjectWrapper object to copy.
26+
*/
27+
CryptoObjectWrapper(const CryptoObjectWrapper& other)
28+
: mObject({ other.mCopier(other.mObject.get()), other.mObject.get_deleter() })
29+
, mCopier(other.mCopier)
30+
{
31+
}
32+
33+
/**
34+
* Copy assignment operator.
35+
*
36+
* @param other The CryptoObjectWrapper object to copy.
37+
*/
38+
CryptoObjectWrapper& operator=(const CryptoObjectWrapper& other)
39+
{
40+
if (this != &other)
41+
{
42+
mObject = { other.mCopier(other.mObject.get()), other.mObject.get_deleter() };
43+
mCopier = other.mCopier;
44+
}
45+
46+
return *this;
47+
}
48+
49+
CryptoObjectWrapper(CryptoObjectWrapper&&) noexcept = default;
50+
CryptoObjectWrapper& operator=(CryptoObjectWrapper&&) noexcept = default;
51+
52+
/**
53+
* Get the wrapped crypto object.
54+
*
55+
* @return A pointer to the wrapped crypto object. nullptr if no object exists.
56+
*/
57+
[[nodiscard]] ObjectType* get() { return mObject.get(); }
58+
[[nodiscard]] const ObjectType* get() const { return mObject.get(); }
59+
60+
/**
61+
* Release ownership of the wrapped crypto object. This will cause memory leaks if ownership has not already been
62+
* taken by another object.
63+
*/
64+
void release() { mObject.release(); }
65+
66+
/**
67+
* Determine if this CryptoObjectWrapper has a valid crypto object.
68+
*
69+
* @return \c TRUE if there exists a crypto object, otherwise \c FALSE.
70+
*/
71+
[[nodiscard]] explicit operator bool() const { return mObject != nullptr; }
72+
73+
protected:
74+
CryptoObjectWrapper() = default;
75+
76+
/**
77+
* Construct with values for the object, its custom deleter, and optionally a custom copier.
78+
*
79+
* @param object The crypto object to wrap.
80+
* @param deleter The deleter function for the crypto object.
81+
* @param copier The copier function for the crypto object.
82+
*/
83+
CryptoObjectWrapper(ObjectType* object,
84+
const std::function<void(ObjectType*)>& deleter,
85+
const CopierFunc& copier = CopierFunc())
86+
: mObject({ object, deleter })
87+
, mCopier(copier)
88+
{
89+
}
90+
91+
private:
92+
/**
93+
* Pointer to the crypto object with its associated deleter.
94+
*/
95+
std::unique_ptr<ObjectType, std::function<void(ObjectType*)>> mObject = { nullptr,
96+
std::function<void(ObjectType*)>() };
97+
98+
/**
99+
* The copier function to use to copy the wrapped crypto object.
100+
*/
101+
CopierFunc mCopier;
102+
};
103+
104+
} // namespace Hiero::internal::OpenSSLUtils
105+
106+
#endif // HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_

src/sdk/main/include/impl/openssl_utils/BIGNUM.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "exceptions/OpenSSLException.h"
66
#include "impl/HexConverter.h"
77
#include "impl/openssl_utils/BN_CTX.h"
8-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
8+
#include "impl/CryptoObjectWrapper.h"
99
#include "impl/openssl_utils/OpenSSLUtils.h"
1010

1111
#include <openssl/bn.h>
@@ -17,7 +17,7 @@ namespace Hiero::internal::OpenSSLUtils
1717
/**
1818
* Wrapper class for the OpenSSL BIGNUM object.
1919
*/
20-
class BIGNUM : public OpenSSLObjectWrapper<::BIGNUM>
20+
class BIGNUM : public CryptoObjectWrapper<::BIGNUM>
2121
{
2222
public:
2323
/**
@@ -26,7 +26,7 @@ class BIGNUM : public OpenSSLObjectWrapper<::BIGNUM>
2626
* @param bignum The BIGNUM OpenSSL object to wrap.
2727
*/
2828
explicit BIGNUM(::BIGNUM* bignum)
29-
: OpenSSLObjectWrapper(bignum, &BN_clear_free, &BN_dup)
29+
: CryptoObjectWrapper(bignum, &BN_clear_free, &BN_dup)
3030
{
3131
}
3232

src/sdk/main/include/impl/openssl_utils/BN_CTX.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_BN_CTX_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_BN_CTX_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/bn.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL BN_CTX object.
1313
*/
14-
class BN_CTX : public OpenSSLObjectWrapper<::BN_CTX>
14+
class BN_CTX : public CryptoObjectWrapper<::BN_CTX>
1515
{
1616
public:
1717
/**
@@ -29,7 +29,7 @@ class BN_CTX : public OpenSSLObjectWrapper<::BN_CTX>
2929
* @param bnCtx The BN_CTX OpenSSL object to wrap.
3030
*/
3131
explicit BN_CTX(::BN_CTX* bnCtx)
32-
: OpenSSLObjectWrapper(bnCtx, &BN_CTX_free)
32+
: CryptoObjectWrapper(bnCtx, &BN_CTX_free)
3333
{
3434
}
3535
};

src/sdk/main/include/impl/openssl_utils/ECDSA_SIG.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_ECDSA_SIG_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_ECDSA_SIG_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/ecdsa.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL ECDSA_SIG object.
1313
*/
14-
class ECDSA_SIG : public OpenSSLObjectWrapper<::ECDSA_SIG>
14+
class ECDSA_SIG : public CryptoObjectWrapper<::ECDSA_SIG>
1515
{
1616
public:
1717
/**
@@ -29,7 +29,7 @@ class ECDSA_SIG : public OpenSSLObjectWrapper<::ECDSA_SIG>
2929
* @param ecdsaSig The ECDSA_SIG OpenSSL object to wrap.
3030
*/
3131
explicit ECDSA_SIG(::ECDSA_SIG* ecdsaSig)
32-
: OpenSSLObjectWrapper(ecdsaSig, &ECDSA_SIG_free)
32+
: CryptoObjectWrapper(ecdsaSig, &ECDSA_SIG_free)
3333
{
3434
}
3535
};

src/sdk/main/include/impl/openssl_utils/EC_GROUP.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_GROUP_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_GROUP_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/ec.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL EC_GROUP object.
1313
*/
14-
class EC_GROUP : public OpenSSLObjectWrapper<::EC_GROUP>
14+
class EC_GROUP : public CryptoObjectWrapper<::EC_GROUP>
1515
{
1616
public:
1717
/**
@@ -20,7 +20,7 @@ class EC_GROUP : public OpenSSLObjectWrapper<::EC_GROUP>
2020
* @param ecGroup The EC_GROUP OpenSSL object to wrap.
2121
*/
2222
explicit EC_GROUP(::EC_GROUP* ecGroup)
23-
: OpenSSLObjectWrapper(ecGroup, &EC_GROUP_free, &EC_GROUP_dup)
23+
: CryptoObjectWrapper(ecGroup, &EC_GROUP_free, &EC_GROUP_dup)
2424
{
2525
}
2626
};

src/sdk/main/include/impl/openssl_utils/EC_POINT.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_POINT_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EC_POINT_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/ec.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL EC_POINT object.
1313
*/
14-
class EC_POINT : public OpenSSLObjectWrapper<::EC_POINT>
14+
class EC_POINT : public CryptoObjectWrapper<::EC_POINT>
1515
{
1616
public:
1717
/**
@@ -29,7 +29,7 @@ class EC_POINT : public OpenSSLObjectWrapper<::EC_POINT>
2929
* @param ecPoint The EC_POINT OpenSSL object to wrap.
3030
*/
3131
explicit EC_POINT(::EC_POINT* ecPoint)
32-
: OpenSSLObjectWrapper(ecPoint, &EC_POINT_free)
32+
: CryptoObjectWrapper(ecPoint, &EC_POINT_free)
3333
{
3434
}
3535
};

src/sdk/main/include/impl/openssl_utils/EVP_MD.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/evp.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL EVP_MD object.
1313
*/
14-
class EVP_MD : public OpenSSLObjectWrapper<::EVP_MD>
14+
class EVP_MD : public CryptoObjectWrapper<::EVP_MD>
1515
{
1616
public:
1717
/**
@@ -29,7 +29,7 @@ class EVP_MD : public OpenSSLObjectWrapper<::EVP_MD>
2929
* @param evpMd The EVP_MD OpenSSL object to wrap.
3030
*/
3131
explicit EVP_MD(::EVP_MD* evpMd)
32-
: OpenSSLObjectWrapper(evpMd, &EVP_MD_free)
32+
: CryptoObjectWrapper(evpMd, &EVP_MD_free)
3333
{
3434
}
3535
};

src/sdk/main/include/impl/openssl_utils/EVP_MD_CTX.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_CTX_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_MD_CTX_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/evp.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL EVP_MD_CTX object.
1313
*/
14-
class EVP_MD_CTX : public OpenSSLObjectWrapper<::EVP_MD_CTX>
14+
class EVP_MD_CTX : public CryptoObjectWrapper<::EVP_MD_CTX>
1515
{
1616
public:
1717
/**
@@ -29,7 +29,7 @@ class EVP_MD_CTX : public OpenSSLObjectWrapper<::EVP_MD_CTX>
2929
* @param evpMdCtx The EVP_MD_CTX OpenSSL object to wrap.
3030
*/
3131
explicit EVP_MD_CTX(::EVP_MD_CTX* evpMdCtx)
32-
: OpenSSLObjectWrapper(evpMdCtx, &EVP_MD_CTX_free)
32+
: CryptoObjectWrapper(evpMdCtx, &EVP_MD_CTX_free)
3333
{
3434
}
3535
};

src/sdk/main/include/impl/openssl_utils/EVP_PKEY.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/evp.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL EVP_PKEY object.
1313
*/
14-
class EVP_PKEY : public OpenSSLObjectWrapper<::EVP_PKEY, decltype(&EVP_PKEY_dup)>
14+
class EVP_PKEY : public CryptoObjectWrapper<::EVP_PKEY, decltype(&EVP_PKEY_dup)>
1515
{
1616
public:
1717
EVP_PKEY() = default;
@@ -22,7 +22,7 @@ class EVP_PKEY : public OpenSSLObjectWrapper<::EVP_PKEY, decltype(&EVP_PKEY_dup)
2222
* @param evpPkey The EVP_PKEY OpenSSL object to wrap.
2323
*/
2424
explicit EVP_PKEY(::EVP_PKEY* evpPkey)
25-
: OpenSSLObjectWrapper(evpPkey, &EVP_PKEY_free, &EVP_PKEY_dup)
25+
: CryptoObjectWrapper(evpPkey, &EVP_PKEY_free, &EVP_PKEY_dup)
2626
{
2727
}
2828
};

src/sdk/main/include/impl/openssl_utils/EVP_PKEY_CTX.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_CTX_H_
33
#define HIERO_SDK_CPP_IMPL_OPENSSL_UTILS_EVP_PKEY_CTX_H_
44

5-
#include "impl/openssl_utils/OpenSSLObjectWrapper.h"
5+
#include "impl/CryptoObjectWrapper.h"
66

77
#include <openssl/evp.h>
88

@@ -11,7 +11,7 @@ namespace Hiero::internal::OpenSSLUtils
1111
/**
1212
* Wrapper class for the OpenSSL EVP_PKEY_CTX object.
1313
*/
14-
class EVP_PKEY_CTX : public OpenSSLObjectWrapper<::EVP_PKEY_CTX>
14+
class EVP_PKEY_CTX : public CryptoObjectWrapper<::EVP_PKEY_CTX>
1515
{
1616
public:
1717
/**
@@ -21,7 +21,7 @@ class EVP_PKEY_CTX : public OpenSSLObjectWrapper<::EVP_PKEY_CTX>
2121
* @param evpPkeyCtx The EVP_PKEY_CTX OpenSSL object to wrap.
2222
*/
2323
explicit EVP_PKEY_CTX(::EVP_PKEY_CTX* evpPkeyCtx)
24-
: OpenSSLObjectWrapper(evpPkeyCtx, &EVP_PKEY_CTX_free, &EVP_PKEY_CTX_dup)
24+
: CryptoObjectWrapper(evpPkeyCtx, &EVP_PKEY_CTX_free, &EVP_PKEY_CTX_dup)
2525
{
2626
}
2727
};

0 commit comments

Comments
 (0)