Remove the use of static deletors

This commit is contained in:
Arun M 2017-11-23 15:10:35 +05:30
parent 53a281640f
commit 0751d033dc
4 changed files with 79 additions and 54 deletions

View file

@ -236,6 +236,58 @@ enum algorithm str_to_alg(const string_view alg) noexcept
assert (0 && "Code not reached"); assert (0 && "Code not reached");
} }
/**
*/
inline void bio_deletor(BIO* ptr)
{
if (ptr) BIO_free_all(ptr);
}
/**
*/
inline void evp_md_ctx_deletor(EVP_MD_CTX* ptr)
{
if (ptr) EVP_MD_CTX_destroy(ptr);
}
/**
*/
inline void ec_key_deletor(EC_KEY* ptr)
{
if (ptr) EC_KEY_free(ptr);
}
/**
*/
inline void ec_sig_deletor(ECDSA_SIG* ptr)
{
if (ptr) ECDSA_SIG_free(ptr);
}
/**
*/
inline void ev_pkey_deletor(EVP_PKEY* ptr)
{
if (ptr) EVP_PKEY_free(ptr);
};
/// Useful typedefs
using bio_deletor_t = decltype(&bio_deletor);
using BIO_uptr = std::unique_ptr<BIO, bio_deletor_t>;
using evp_mdctx_deletor_t = decltype(&evp_md_ctx_deletor);
using EVP_MDCTX_uptr = std::unique_ptr<EVP_MD_CTX, evp_mdctx_deletor_t>;
using eckey_deletor_t = decltype(&ec_key_deletor);
using EC_KEY_uptr = std::unique_ptr<EC_KEY, eckey_deletor_t>;
using ecsig_deletor_t = decltype(&ec_sig_deletor);
using EC_SIG_uptr = std::unique_ptr<ECDSA_SIG, ecsig_deletor_t>;
using evpkey_deletor_t = decltype(&ev_pkey_deletor);
using EC_PKEY_uptr = std::unique_ptr<EVP_PKEY, evpkey_deletor_t>;
/** /**
* OpenSSL HMAC based signature and verfication. * OpenSSL HMAC based signature and verfication.
@ -280,7 +332,6 @@ struct HMACSign
data.length(), data.length(),
reinterpret_cast<unsigned char*>(&sign[0]), reinterpret_cast<unsigned char*>(&sign[0]),
&len); &len);
if (!res) { if (!res) {
ec = AlgorithmErrc::SigningErr; ec = AlgorithmErrc::SigningErr;
} }
@ -335,11 +386,13 @@ struct HMACSign<algo::NONE>
static verify_result_t static verify_result_t
verify(const string_view key, const string_view head, const string_view sign) verify(const string_view key, const string_view head, const string_view sign)
{ {
bool compare_res = 0; (void)key;
(void)head;
(void)sign;
std::error_code ec{}; std::error_code ec{};
ec = AlgorithmErrc::NoneAlgorithmUsed;
//TODO: Set the appropriate error code for none return { true, ec };
return { compare_res, ec };
} }
}; };
@ -378,13 +431,7 @@ public:
{ {
std::error_code ec{}; std::error_code ec{};
static auto evpkey_deletor = [](EVP_PKEY* ptr) { EC_PKEY_uptr pkey{load_key(key, ec), ev_pkey_deletor};
if (ptr) EVP_PKEY_free(ptr);
};
std::unique_ptr<EVP_PKEY, decltype(evpkey_deletor)>
pkey{load_key(key, ec), evpkey_deletor};
if (ec) return { std::string{}, ec }; if (ec) return { std::string{}, ec };
//TODO: Use stack string here ? //TODO: Use stack string here ?

View file

@ -12,28 +12,19 @@ verify_result_t HMACSign<Hasher>::verify(
const string_view jwt_sign) const string_view jwt_sign)
{ {
std::error_code ec{}; std::error_code ec{};
//TODO: remove these static deletors.
static auto bio_deletor = [](BIO* ptr) {
if (ptr) BIO_free_all(ptr);
};
std::cout << "Key: " << key << std::endl; std::cout << "Key: " << key << std::endl;
std::cout << "Head: " << head << std::endl; std::cout << "Head: " << head << std::endl;
std::cout << "JWT: " << jwt_sign << std::endl; std::cout << "JWT: " << jwt_sign << std::endl;
using bio_deletor_t = decltype(bio_deletor); BIO_uptr b64{BIO_new(BIO_f_base64()), bio_deletor};
using BIO_unique_ptr = std::unique_ptr<BIO, bio_deletor_t>;
BIO_unique_ptr b64{BIO_new(BIO_f_base64()), bio_deletor};
if (!b64) { if (!b64) {
//TODO: set error code throw MemoryAllocationException("BIO_new failed");
return {false, ec};
} }
BIO* bmem = BIO_new(BIO_s_mem()); BIO* bmem = BIO_new(BIO_s_mem());
if (!bmem) { if (!bmem) {
//TODO: set error code throw MemoryAllocationException("BIO_new failed");
return {false, ec};
} }
BIO_push(b64.get(), bmem); BIO_push(b64.get(), bmem);
@ -50,7 +41,7 @@ verify_result_t HMACSign<Hasher>::verify(
enc_buf, enc_buf,
&enc_buf_len); &enc_buf_len);
if (!res) { if (!res) {
//TODO: set error code ec = AlgorithmErrc::VerificationErr;
return {false, ec}; return {false, ec};
} }
@ -59,7 +50,7 @@ verify_result_t HMACSign<Hasher>::verify(
int len = BIO_pending(bmem); int len = BIO_pending(bmem);
if (len < 0) { if (len < 0) {
//TODO: set error code ec = AlgorithmErrc::VerificationErr;
return {false, ec}; return {false, ec};
} }
@ -74,7 +65,9 @@ verify_result_t HMACSign<Hasher>::verify(
cbuf.resize(new_len); cbuf.resize(new_len);
std::cout << "cbuf: " << cbuf << std::endl; std::cout << "cbuf: " << cbuf << std::endl;
return {string_view{cbuf} == jwt_sign, ec}; bool ret = (string_view{cbuf} == jwt_sign);
return { ret, ec };
} }
template <typename Hasher> template <typename Hasher>
@ -82,14 +75,11 @@ EVP_PKEY* PEMSign<Hasher>::load_key(
const string_view key, const string_view key,
std::error_code& ec) std::error_code& ec)
{ {
static auto bio_deletor = [](BIO* ptr) {
if (ptr) BIO_free(ptr);
};
ec.clear(); ec.clear();
std::unique_ptr<BIO, decltype(bio_deletor)> BIO_uptr bio_ptr{
bio_ptr{BIO_new_mem_buf((void*)key.data(), key.length()), bio_deletor}; BIO_new_mem_buf((void*)key.data(), key.length()),
bio_deletor};
if (!bio_ptr) { if (!bio_ptr) {
throw MemoryAllocationException("BIO_new_mem_buf failed"); throw MemoryAllocationException("BIO_new_mem_buf failed");
@ -111,14 +101,9 @@ std::string PEMSign<Hasher>::evp_digest(
const string_view data, const string_view data,
std::error_code& ec) std::error_code& ec)
{ {
static auto md_deletor = [](EVP_MD_CTX* ptr) {
if (ptr) EVP_MD_CTX_destroy(ptr);
};
ec.clear(); ec.clear();
std::unique_ptr<EVP_MD_CTX, decltype(md_deletor)> EVP_MDCTX_uptr mdctx_ptr{EVP_MD_CTX_create(), evp_md_ctx_deletor};
mdctx_ptr{EVP_MD_CTX_create(), md_deletor};
if (!mdctx_ptr) { if (!mdctx_ptr) {
throw MemoryAllocationException("EVP_MD_CTX_create failed"); throw MemoryAllocationException("EVP_MD_CTX_create failed");
@ -167,16 +152,7 @@ std::string PEMSign<Hasher>::public_key_ser(
std::string new_sign; std::string new_sign;
ec.clear(); ec.clear();
static auto eckey_deletor = [](EC_KEY* ptr) { EC_KEY_uptr ec_key{EVP_PKEY_get1_EC_KEY(pkey), ec_key_deletor};
if (ptr) EC_KEY_free(ptr);
};
static auto ecsig_deletor = [](ECDSA_SIG* ptr) {
if (ptr) ECDSA_SIG_free(ptr);
};
std::unique_ptr<EC_KEY, decltype(eckey_deletor)>
ec_key{EVP_PKEY_get1_EC_KEY(pkey), eckey_deletor};
if (!ec_key) { if (!ec_key) {
ec = AlgorithmErrc::SigningErr; ec = AlgorithmErrc::SigningErr;
@ -185,11 +161,10 @@ std::string PEMSign<Hasher>::public_key_ser(
uint32_t degree = EC_GROUP_get_degree(EC_KEY_get0_group(ec_key.get())); uint32_t degree = EC_GROUP_get_degree(EC_KEY_get0_group(ec_key.get()));
std::unique_ptr<ECDSA_SIG, decltype(ecsig_deletor)> EC_SIG_uptr ec_sig{d2i_ECDSA_SIG(nullptr,
ec_sig{d2i_ECDSA_SIG(nullptr,
(const unsigned char**)&sign[0], (const unsigned char**)&sign[0],
sign.length()), sign.length()),
ecsig_deletor}; ec_sig_deletor};
if (!ec_sig) { if (!ec_sig) {
ec = AlgorithmErrc::SigningErr; ec = AlgorithmErrc::SigningErr;

View file

@ -304,14 +304,17 @@ jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool
auto parts = jwt_object::three_parts(encoded_str); auto parts = jwt_object::three_parts(encoded_str);
//throws verification error
jobj.header(jwt_header{parts[0]}); jobj.header(jwt_header{parts[0]});
//throws verification error
jobj.payload(jwt_payload{parts[1]}); jobj.payload(jwt_payload{parts[1]});
jwt_signature jsign{key}; jwt_signature jsign{key};
//length of the encoded header and payload only. //length of the encoded header and payload only.
//Addition of '1' to account for the '.' character. //Addition of '1' to account for the '.' character.
auto l = parts[0].length() + 1 + parts[1].length(); auto l = parts[0].length() + 1 + parts[1].length();
jsign.verify(jobj.header(), encoded_str.substr(0, l), encoded_str); jsign.verify(jobj.header(), encoded_str.substr(0, l), parts[2]);
return jobj; return jobj;
} }

Binary file not shown.