Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to a class structure #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions include/cose/cose.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ typedef enum {
* Functions dealing with keys
*/

const int COSE_KEY_FL_OWN = 0x1; // Cede ownership of the key to the libraray
// Only neede for MBEDTLS as OpenSSL does reference counts

const int COSE_KEY_FL_OWN =
0x1; // Cede ownership of the key to the libraray
// Only neede for MBEDTLS as OpenSSL does reference counts

HCOSE_KEY COSE_KEY_FromCbor(cn_cbor* pcborKey,
CBOR_CONTEXT_COMMA cose_errback* perror);
Expand All @@ -235,8 +235,8 @@ HCOSE_KEY COSE_KEY_FromEVP(EVP_PKEY* opensslKey,
CBOR_CONTEXT_COMMA cose_errback* perror);
#endif
#ifdef COSE_C_USE_MBEDTLS
HCOSE_KEY COSE_KEY_FromMbedKeypair(mbedtls_ecp_keypair *,
cn_cbor * pcborKey,
HCOSE_KEY COSE_KEY_FromMbedKeypair(mbedtls_ecp_keypair*,
cn_cbor* pcborKey,
int flags,
CBOR_CONTEXT_COMMA cose_errback* perror);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Cose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ bool AreListsEmpty()
#if INCLUDE_MAC0
fRet &= Mac0Root == nullptr;
#endif
fRet &= KeysRoot == nullptr;
fRet &= COSE_KEY::KeysRoot == nullptr;
return fRet;
}

Expand Down
103 changes: 33 additions & 70 deletions src/CoseKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include "cose_int.h"
#include "cose_crypto.h"

COSE_KEY *KeysRoot = nullptr;
COSE_KEY *COSE_KEY::KeysRoot = nullptr;

/*! \private
* @brief Test if a HCOSE_ENVELOPED handle is valid
* @brief Test if a HCOSE_KEY handle is valid
*
* Internal function to test if a enveloped message handle is valid.
* Internal function to test if a key handle is valid.
* This will start returning invalid results and cause the code to
* crash if handles are not released before the memory that underlies them
* is deallocated. This is an issue of a block allocator is used since
Expand All @@ -21,9 +21,9 @@ COSE_KEY *KeysRoot = nullptr;
* @returns result of check
*/

bool IsValidKeyHandle(HCOSE_KEY h)
bool COSE_KEY::IsValidKeyHandle(HCOSE_KEY h)
{
COSE_KEY *p = (COSE_KEY *)h;
COSE_KEY *p = reinterpret_cast<COSE_KEY *>(h);
if (KeysRoot == nullptr) {
return false;
}
Expand All @@ -45,63 +45,53 @@ HCOSE_KEY COSE_KEY_FromCbor(cn_cbor *pcborKey,
{
COSE_KEY *pkey = nullptr;

pkey = (COSE_KEY *)COSE_CALLOC(1, sizeof(COSE_KEY), context);

pkey = new (std::nothrow, context) COSE_KEY();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think thia is the way forward...
Better create something like a factory patter which returns a shared ptr.

if (pkey == nullptr) {
if (perror != nullptr) {
perror->err = COSE_ERR_OUT_OF_MEMORY;
}
return nullptr;
}

#ifdef USE_CBOR_CONTEXT
if (context != nullptr) {
pkey->m_allocContext = *context;
}
#endif

pkey->m_refCount = 1;
pkey->m_cborKey = pcborKey;

pkey->m_nextKey = KeysRoot;
KeysRoot = pkey;

return (HCOSE_KEY)pkey;
return reinterpret_cast<HCOSE_KEY>(pkey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get rid of all those casts

}

bool COSE_KEY_Free(HCOSE_KEY h)
{
COSE_KEY *p = (COSE_KEY *)h;
if (!IsValidKeyHandle(h)) {
return false;
}
COSE_KEY::~COSE_KEY()

if (p->m_refCount > 1) {
p->m_refCount--;
return true;
{
if (m_cborKey != nullptr) {
CN_CBOR_FREE(m_cborKey, &m_allocContext);
}

if (KeysRoot == p) {
KeysRoot = p->m_nextKey;
p->m_nextKey = nullptr;
if (KeysRoot == this) {
KeysRoot = this->m_nextKey;
this->m_nextKey = nullptr;
;
}
else {
for (COSE_KEY *walk = KeysRoot; walk->m_nextKey != nullptr;
walk = walk->m_nextKey) {
if (walk->m_nextKey == p) {
walk->m_nextKey = p->m_nextKey;
p->m_nextKey = nullptr;
if (walk->m_nextKey == this) {
walk->m_nextKey = this->m_nextKey;
this->m_nextKey = nullptr;
break;
}
}
}
if (p->m_cborKey != nullptr && p->m_cborKey->parent == nullptr) {
CN_CBOR_FREE(p->m_cborKey, &p->m_allocContext);
if (m_cborKey != nullptr && m_cborKey->parent == nullptr) {
CN_CBOR_FREE(m_cborKey, &m_allocContext);
}
}

COSE_FREE(p, &p->m_allocContext);

bool COSE_KEY_Free(HCOSE_KEY h)
{
COSE_KEY *key = reinterpret_cast<COSE_KEY *>(h);
if (!COSE_KEY::IsValidKeyHandle(h)) {
return false;
}
key->Release();
return true;
}

Expand All @@ -110,63 +100,36 @@ HCOSE_KEY COSE_KEY_FromEVP(EVP_PKEY *opensslKey,
cn_cbor *pcborKey,
CBOR_CONTEXT_COMMA cose_errback *perror)
{
COSE_KEY *pkey = nullptr;

pkey = (COSE_KEY *)COSE_CALLOC(1, sizeof(COSE_KEY), context);

COSE_KEY *pkey = new (std::nothrow, context) COSE_KEY();
if (pkey == nullptr) {
perror->err = COSE_ERR_OUT_OF_MEMORY;
return nullptr;
}

#ifdef USE_CBOR_CONTEXT
if (context != nullptr) {
pkey->m_allocContext = *context;
}
#endif

pkey->m_refCount = 1;
pkey->m_cborKey = pcborKey;
pkey->m_opensslKey = opensslKey;
pkey->m_cborKey = pcborKey;
EVP_PKEY_up_ref(opensslKey);

pkey->m_nextKey = KeysRoot;
KeysRoot = pkey;

return (HCOSE_KEY)pkey;
return reinterpret_cast<HCOSE_KEY>(pkey);
}
#endif

#ifdef COSE_C_USE_MBEDTLS
HCOSE_KEY COSE_KEY_FromMbedKeypair(mbedtls_ecp_keypair * mbedtls_keypair,
HCOSE_KEY COSE_KEY_FromMbedKeypair(mbedtls_ecp_keypair *mbedtls_keypair,
cn_cbor *pcborKey,
int flags,
CBOR_CONTEXT_COMMA cose_errback *perror)
{
COSE_KEY *pkey = nullptr;

pkey = (COSE_KEY *)COSE_CALLOC(1, sizeof(COSE_KEY), context);

COSE_KEY *pkey = new (std::nothrow, context) COSE_KEY();
if (pkey == nullptr) {
perror->err = COSE_ERR_OUT_OF_MEMORY;
return nullptr;
}

#ifdef USE_CBOR_CONTEXT
if (context != nullptr) {
pkey->m_allocContext = *context;
}
#endif

pkey->m_refCount = 1;
pkey->m_cborKey = pcborKey;
pkey->m_mbedtls_keypair = mbedtls_keypair;
pkey->m_flags = flags;

pkey->m_nextKey = KeysRoot;
KeysRoot = pkey;

return (HCOSE_KEY)pkey;
return reinterpret_cast<HCOSE_KEY>(pkey);
}

#endif
2 changes: 1 addition & 1 deletion src/CounterSign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ bool COSE_CounterSign_SetKey2(HCOSE_COUNTERSIGN hSigner,
COSE_KEY* pKey = (COSE_KEY*)hKey;

CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(COSE_KEY::IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);

if (pSigner->m_signer.m_pkey != nullptr) {
COSE_KEY_Free((HCOSE_KEY)pSigner->m_signer.m_pkey);
Expand Down
2 changes: 1 addition & 1 deletion src/CounterSign1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ bool COSE_CounterSign1_SetKey(HCOSE_COUNTERSIGN1 hSigner,

CHECK_CONDITION(
IsValidCounterSign1Handle(hSigner), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(COSE_KEY::IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);

if (pSigner->m_signer.m_pkey != nullptr) {
COSE_KEY_Free((HCOSE_KEY)pSigner->m_signer.m_pkey);
Expand Down
3 changes: 2 additions & 1 deletion src/Recipient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,8 @@ bool COSE_Recipient_SetSenderKey2(HCOSE_RECIPIENT h,
}

CHECK_CONDITION(IsValidRecipientHandle(h), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_PARAMETER);
CHECK_CONDITION(
COSE_KEY::IsValidKeyHandle(hKey), COSE_ERR_INVALID_PARAMETER);

p = (COSE_RecipientInfo *)h;
COSE_KEY *pKey = (COSE_KEY *)hKey;
Expand Down
4 changes: 2 additions & 2 deletions src/Sign1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ bool COSE_Sign1_Sign2(HCOSE_SIGN1 h, HCOSE_KEY hKey, cose_errback *perr)
errorReturn:
return false;
}
CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(COSE_KEY::IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);

#ifdef USE_CBOR_CONTEXT
context = &pMessage->m_message.m_allocContext;
Expand Down Expand Up @@ -311,7 +311,7 @@ bool COSE_Sign1_validate2(HCOSE_SIGN1 hSign, HCOSE_KEY hKey, cose_errback *perr)
}

CHECK_CONDITION(IsValidSign1Handle(hSign), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(COSE_KEY::IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);

COSE_Sign1Message *pSign = (COSE_Sign1Message *)hSign;

Expand Down
2 changes: 1 addition & 1 deletion src/SignerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ bool COSE_Signer_SetKey2(HCOSE_SIGNER h, HCOSE_KEY pKey, cose_errback *perr)
}

CHECK_CONDITION(IsValidSignerHandle(h), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(IsValidKeyHandle(pKey), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(COSE_KEY::IsValidKeyHandle(pKey), COSE_ERR_INVALID_HANDLE);

p = (COSE_SignerInfo *)h;
if (p->m_pkey != nullptr) {
Expand Down
Loading