diff --git a/include/jwt/algorithm.hpp b/include/jwt/algorithm.hpp index c7e59fb..439b505 100644 --- a/include/jwt/algorithm.hpp +++ b/include/jwt/algorithm.hpp @@ -236,6 +236,58 @@ enum algorithm str_to_alg(const string_view alg) noexcept 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; + +using evp_mdctx_deletor_t = decltype(&evp_md_ctx_deletor); +using EVP_MDCTX_uptr = std::unique_ptr; + +using eckey_deletor_t = decltype(&ec_key_deletor); +using EC_KEY_uptr = std::unique_ptr; + +using ecsig_deletor_t = decltype(&ec_sig_deletor); +using EC_SIG_uptr = std::unique_ptr; + +using evpkey_deletor_t = decltype(&ev_pkey_deletor); +using EC_PKEY_uptr = std::unique_ptr; + + /** * OpenSSL HMAC based signature and verfication. @@ -280,7 +332,6 @@ struct HMACSign data.length(), reinterpret_cast(&sign[0]), &len); - if (!res) { ec = AlgorithmErrc::SigningErr; } @@ -335,11 +386,13 @@ struct HMACSign static verify_result_t 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{}; + ec = AlgorithmErrc::NoneAlgorithmUsed; - //TODO: Set the appropriate error code for none - return { compare_res, ec }; + return { true, ec }; } }; @@ -378,13 +431,7 @@ public: { std::error_code ec{}; - static auto evpkey_deletor = [](EVP_PKEY* ptr) { - if (ptr) EVP_PKEY_free(ptr); - }; - - std::unique_ptr - pkey{load_key(key, ec), evpkey_deletor}; - + EC_PKEY_uptr pkey{load_key(key, ec), ev_pkey_deletor}; if (ec) return { std::string{}, ec }; //TODO: Use stack string here ? diff --git a/include/jwt/impl/algorithm.ipp b/include/jwt/impl/algorithm.ipp index 16b5a7e..df40b75 100644 --- a/include/jwt/impl/algorithm.ipp +++ b/include/jwt/impl/algorithm.ipp @@ -12,28 +12,19 @@ verify_result_t HMACSign::verify( const string_view jwt_sign) { 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 << "Head: " << head << std::endl; std::cout << "JWT: " << jwt_sign << std::endl; - using bio_deletor_t = decltype(bio_deletor); - using BIO_unique_ptr = std::unique_ptr; - - BIO_unique_ptr b64{BIO_new(BIO_f_base64()), bio_deletor}; + BIO_uptr b64{BIO_new(BIO_f_base64()), bio_deletor}; if (!b64) { - //TODO: set error code - return {false, ec}; + throw MemoryAllocationException("BIO_new failed"); } BIO* bmem = BIO_new(BIO_s_mem()); if (!bmem) { - //TODO: set error code - return {false, ec}; + throw MemoryAllocationException("BIO_new failed"); } BIO_push(b64.get(), bmem); @@ -50,7 +41,7 @@ verify_result_t HMACSign::verify( enc_buf, &enc_buf_len); if (!res) { - //TODO: set error code + ec = AlgorithmErrc::VerificationErr; return {false, ec}; } @@ -59,7 +50,7 @@ verify_result_t HMACSign::verify( int len = BIO_pending(bmem); if (len < 0) { - //TODO: set error code + ec = AlgorithmErrc::VerificationErr; return {false, ec}; } @@ -74,7 +65,9 @@ verify_result_t HMACSign::verify( cbuf.resize(new_len); std::cout << "cbuf: " << cbuf << std::endl; - return {string_view{cbuf} == jwt_sign, ec}; + bool ret = (string_view{cbuf} == jwt_sign); + + return { ret, ec }; } template @@ -82,14 +75,11 @@ EVP_PKEY* PEMSign::load_key( const string_view key, std::error_code& ec) { - static auto bio_deletor = [](BIO* ptr) { - if (ptr) BIO_free(ptr); - }; - ec.clear(); - std::unique_ptr - bio_ptr{BIO_new_mem_buf((void*)key.data(), key.length()), bio_deletor}; + BIO_uptr bio_ptr{ + BIO_new_mem_buf((void*)key.data(), key.length()), + bio_deletor}; if (!bio_ptr) { throw MemoryAllocationException("BIO_new_mem_buf failed"); @@ -111,14 +101,9 @@ std::string PEMSign::evp_digest( const string_view data, std::error_code& ec) { - static auto md_deletor = [](EVP_MD_CTX* ptr) { - if (ptr) EVP_MD_CTX_destroy(ptr); - }; - ec.clear(); - std::unique_ptr - mdctx_ptr{EVP_MD_CTX_create(), md_deletor}; + EVP_MDCTX_uptr mdctx_ptr{EVP_MD_CTX_create(), evp_md_ctx_deletor}; if (!mdctx_ptr) { throw MemoryAllocationException("EVP_MD_CTX_create failed"); @@ -167,16 +152,7 @@ std::string PEMSign::public_key_ser( std::string new_sign; ec.clear(); - static auto eckey_deletor = [](EC_KEY* ptr) { - if (ptr) EC_KEY_free(ptr); - }; - - static auto ecsig_deletor = [](ECDSA_SIG* ptr) { - if (ptr) ECDSA_SIG_free(ptr); - }; - - std::unique_ptr - ec_key{EVP_PKEY_get1_EC_KEY(pkey), eckey_deletor}; + EC_KEY_uptr ec_key{EVP_PKEY_get1_EC_KEY(pkey), ec_key_deletor}; if (!ec_key) { ec = AlgorithmErrc::SigningErr; @@ -185,11 +161,10 @@ std::string PEMSign::public_key_ser( uint32_t degree = EC_GROUP_get_degree(EC_KEY_get0_group(ec_key.get())); - std::unique_ptr - ec_sig{d2i_ECDSA_SIG(nullptr, - (const unsigned char**)&sign[0], - sign.length()), - ecsig_deletor}; + EC_SIG_uptr ec_sig{d2i_ECDSA_SIG(nullptr, + (const unsigned char**)&sign[0], + sign.length()), + ec_sig_deletor}; if (!ec_sig) { ec = AlgorithmErrc::SigningErr; diff --git a/include/jwt/impl/jwt.ipp b/include/jwt/impl/jwt.ipp index e115579..3112a91 100644 --- a/include/jwt/impl/jwt.ipp +++ b/include/jwt/impl/jwt.ipp @@ -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); + //throws verification error jobj.header(jwt_header{parts[0]}); + //throws verification error jobj.payload(jwt_payload{parts[1]}); jwt_signature jsign{key}; + //length of the encoded header and payload only. //Addition of '1' to account for the '.' character. 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; } diff --git a/include/jwt/test/test_jwt_object b/include/jwt/test/test_jwt_object index c625eff..b7b02b1 100755 Binary files a/include/jwt/test/test_jwt_object and b/include/jwt/test/test_jwt_object differ