Skip to content

Commit

Permalink
[FOLD] Use unique_ptr to manage resources in rsa_generate_key
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardHinnant committed Jun 22, 2017
1 parent 86e9c04 commit 720aa29
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/ripple/basics/impl/make_SSLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,40 @@ struct custom_delete <X509>
}
};

template <>
struct custom_delete <BIGNUM>
{
void operator() (BIGNUM* bn) const
{
BN_free(bn);
}
};

template <class T>
using custom_delete_unique_ptr = std::unique_ptr <T, custom_delete <T>>;

// RSA

using rsa_ptr = custom_delete_unique_ptr <RSA>;
using bignum_ptr = custom_delete_unique_ptr<BIGNUM>;

static rsa_ptr rsa_generate_key (int n_bits)
{
#if OPENSSL_VERSION_NUMBER >= 0x00908000L
BIGNUM *bn = BN_new();
BN_set_word(bn, RSA_F4);

RSA* rsa = RSA_new();
if (RSA_generate_key_ex(rsa, n_bits, bn, nullptr) != 1)
{
RSA_free(rsa);
rsa = nullptr;
}
bignum_ptr bn{BN_new()};
BN_set_word(bn.get(), RSA_F4);

BN_free(bn);
rsa_ptr rsa{RSA_new()};
if (RSA_generate_key_ex(rsa.get(), n_bits, bn.get(), nullptr) != 1)
rsa.reset();
#else
RSA* rsa = RSA_generate_key (n_bits, RSA_F4, nullptr, nullptr);
rsa_ptr rsa{RSA_generate_key (n_bits, RSA_F4, nullptr, nullptr)};
#endif

if (rsa == nullptr)
LogicError ("RSA_generate_key failed");

return rsa_ptr (rsa);
return rsa;
}

// EVP_PKEY
Expand Down

0 comments on commit 720aa29

Please sign in to comment.