Skip to content

Commit

Permalink
fix: jwtpp::rsa::load_from file left key file open (#42)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>

Co-authored-by: Anton Schmidt <[email protected]>
  • Loading branch information
troian and sa2304 authored Apr 6, 2021
1 parent 58e80ce commit 8660f3b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 16 additions & 8 deletions include/export/jwtpp/jwtpp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ enum class alg_t {
#if defined(_MSC_VER) && (_MSC_VER < 1700)
# define final

typedef std::shared_ptr<class claims> sp_claims;
typedef std::unique_ptr<class claims> up_claims;
typedef std::shared_ptr<class crypto> sp_crypto;
typedef std::shared_ptr<class hmac> sp_hmac;
typedef std::shared_ptr<class rsa> sp_rsa;
typedef std::shared_ptr<class ecdsa> sp_ecdsa;
typedef std::shared_ptr<RSA> sp_rsa_key;
typedef std::shared_ptr<EC_KEY> sp_ecdsa_key;
typedef std::shared_ptr<class claims> sp_claims;
typedef std::unique_ptr<class claims> up_claims;
typedef std::shared_ptr<class crypto> sp_crypto;
typedef std::shared_ptr<class hmac> sp_hmac;
typedef std::shared_ptr<class rsa> sp_rsa;
typedef std::shared_ptr<class ecdsa> sp_ecdsa;
typedef std::shared_ptr<RSA> sp_rsa_key;
typedef std::shared_ptr<EC_KEY> sp_ecdsa_key;
typedef std::shared_ptr<EVP_PKEY> sp_evp_key;

typedef std::shared_ptr<EVP_MD_CTX> sp_evp_md_ctx;
typedef std::shared_ptr<EVP_PKEY_CTX> sp_evp_pkey_ctx;

typedef std::unique_ptr<std::FILE, int (*)(std::FILE *)> up_file;
#else
using sp_claims = typename std::shared_ptr<class claims>;
using up_claims = typename std::unique_ptr<class claims>;
Expand All @@ -106,6 +112,8 @@ enum class alg_t {

using sp_evp_md_ctx = typename std::shared_ptr<EVP_MD_CTX>;
using sp_evp_pkey_ctx = typename std::shared_ptr<EVP_PKEY_CTX>;

using up_file = typename std::unique_ptr<std::FILE, int (*)(std::FILE *)>;
#endif // defined(_MSC_VER) && (_MSC_VER < 1700)

template <class T>
Expand Down
6 changes: 3 additions & 3 deletions src/rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ sp_rsa_key rsa::gen(int size) {
sp_rsa_key rsa::load_from_file(const std::string &path, password_cb on_password) {
RSA *r;

auto f = std::fopen(path.c_str(), "r");
auto f = up_file(::std::fopen(path.c_str(), "re"), ::std::fclose);
if (!f) {
throw std::runtime_error("cannot open file");
throw std::runtime_error("cannot open file " + path);
}

on_password_wrap wrap(on_password);

r = PEM_read_RSAPrivateKey(f, nullptr, password_loader, &wrap);
r = PEM_read_RSAPrivateKey(f.get(), nullptr, password_loader, &wrap);
if (wrap.required) {
throw std::runtime_error("password required");
} else if (r == nullptr) {
Expand Down

0 comments on commit 8660f3b

Please sign in to comment.