Fix null pointer in ec wrapper

This commit is contained in:
JoelKatz
2015-06-19 15:53:08 -07:00
committed by Vinnie Falco
parent 72832c0fa2
commit 6ec5fa9cae
5 changed files with 26 additions and 26 deletions

View File

@@ -71,7 +71,7 @@ static bool ECDSAVerify (uint256 const& hash, std::uint8_t const* sig, size_t si
static bool ECDSAVerify (uint256 const& hash, Blob const& sig, const openssl::ec_key& key) static bool ECDSAVerify (uint256 const& hash, Blob const& sig, const openssl::ec_key& key)
{ {
return ECDSAVerify (hash, sig.data(), sig.size(), (EC_KEY*) key.get()); return key.valid() && ECDSAVerify (hash, sig.data(), sig.size(), (EC_KEY*) key.get());
} }
bool ECDSAVerify (uint256 const& hash, bool ECDSAVerify (uint256 const& hash,

View File

@@ -55,33 +55,34 @@ ec_key ECDSAPrivateKey (uint256 const& serialized)
} }
EC_KEY* key = new_initialized_EC_KEY(); EC_KEY* key = new_initialized_EC_KEY();
ec_key::pointer_t ptr = nullptr;
const bool ok = EC_KEY_set_private_key (key, bn); const bool ok = EC_KEY_set_private_key (key, bn);
BN_clear_free (bn); BN_clear_free (bn);
if (! ok) if (ok)
{ ptr = (ec_key::pointer_t) key;
else
EC_KEY_free (key); EC_KEY_free (key);
}
return ec_key::acquire ((ec_key::pointer_t) key); return ec_key(ptr);
} }
ec_key ECDSAPublicKey (std::uint8_t const* data, std::size_t size) ec_key ECDSAPublicKey (std::uint8_t const* data, std::size_t size)
{ {
EC_KEY* key = new_initialized_EC_KEY(); EC_KEY* key = new_initialized_EC_KEY();
ec_key::pointer_t ptr = nullptr;
if (o2i_ECPublicKey (&key, &data, size) != nullptr) if (o2i_ECPublicKey (&key, &data, size) != nullptr)
{ {
EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED); EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED);
ptr = (ec_key::pointer_t) key;
} }
else else
{
EC_KEY_free (key); EC_KEY_free (key);
}
return ec_key::acquire ((ec_key::pointer_t) key); return ec_key(ptr);
} }
ec_key ECDSAPublicKey (Blob const& serialized) ec_key ECDSAPublicKey (Blob const& serialized)

View File

@@ -34,8 +34,6 @@ static inline EC_KEY* get_EC_KEY (const ec_key& that)
return (EC_KEY*) that.get(); return (EC_KEY*) that.get();
} }
const ec_key ec_key::invalid = ec_key::acquire (nullptr);
ec_key::ec_key (const ec_key& that) ec_key::ec_key (const ec_key& that)
{ {
if (that.ptr == nullptr) if (that.ptr == nullptr)

View File

@@ -31,32 +31,28 @@ class ec_key
public: public:
using pointer_t = struct opaque_EC_KEY*; using pointer_t = struct opaque_EC_KEY*;
private: ec_key () : ptr(nullptr)
pointer_t ptr; {
}
void destroy();
ec_key (pointer_t raw) : ptr(raw) ec_key (pointer_t raw) : ptr(raw)
{ {
} }
public:
static const ec_key invalid;
static ec_key acquire (pointer_t raw) { return ec_key (raw); }
//ec_key() : ptr() {}
ec_key (const ec_key&);
ec_key& operator= (const ec_key&) = delete;
~ec_key() ~ec_key()
{ {
destroy(); destroy();
} }
bool valid() const
{
return ptr != nullptr;
}
pointer_t get() const { return ptr; } pointer_t get() const { return ptr; }
ec_key (const ec_key&);
pointer_t release() pointer_t release()
{ {
pointer_t released = ptr; pointer_t released = ptr;
@@ -66,7 +62,12 @@ public:
return released; return released;
} }
bool valid() const { return ptr != nullptr; } private:
pointer_t ptr;
void destroy();
ec_key& operator= (const ec_key&) = delete;
}; };
} // openssl } // openssl

View File

@@ -133,7 +133,7 @@ static ec_key ec_key_new_secp256k1_compressed()
EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED); EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED);
return ec_key::acquire ((ec_key::pointer_t) key); return ec_key((ec_key::pointer_t) key);
} }
void serialize_ec_point (ec_point const& point, std::uint8_t* ptr) void serialize_ec_point (ec_point const& point, std::uint8_t* ptr)