-
Notifications
You must be signed in to change notification settings - Fork 23
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
jimsch
wants to merge
1
commit into
cose-wg:master
Choose a base branch
from
jimsch:class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
@@ -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(); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.