From 6ec5fa9caed45d170d35f468ac3397191d725355 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 19 Jun 2015 15:53:08 -0700 Subject: [PATCH] Fix null pointer in ec wrapper --- src/ripple/crypto/impl/ECDSA.cpp | 2 +- src/ripple/crypto/impl/ECDSAKey.cpp | 15 +++++++------- src/ripple/crypto/impl/ec_key.cpp | 2 -- src/ripple/crypto/impl/ec_key.h | 31 +++++++++++++++-------------- src/ripple/crypto/impl/openssl.cpp | 2 +- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/ripple/crypto/impl/ECDSA.cpp b/src/ripple/crypto/impl/ECDSA.cpp index 671ef044d0..4da7effabf 100644 --- a/src/ripple/crypto/impl/ECDSA.cpp +++ b/src/ripple/crypto/impl/ECDSA.cpp @@ -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) { - 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, diff --git a/src/ripple/crypto/impl/ECDSAKey.cpp b/src/ripple/crypto/impl/ECDSAKey.cpp index 96dd06209d..afa05095e8 100644 --- a/src/ripple/crypto/impl/ECDSAKey.cpp +++ b/src/ripple/crypto/impl/ECDSAKey.cpp @@ -55,33 +55,34 @@ ec_key ECDSAPrivateKey (uint256 const& serialized) } EC_KEY* key = new_initialized_EC_KEY(); + ec_key::pointer_t ptr = nullptr; const bool ok = EC_KEY_set_private_key (key, bn); BN_clear_free (bn); - if (! ok) - { + if (ok) + ptr = (ec_key::pointer_t) key; + else 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* key = new_initialized_EC_KEY(); + ec_key::pointer_t ptr = nullptr; if (o2i_ECPublicKey (&key, &data, size) != nullptr) { EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED); + ptr = (ec_key::pointer_t) key; } else - { EC_KEY_free (key); - } - return ec_key::acquire ((ec_key::pointer_t) key); + return ec_key(ptr); } ec_key ECDSAPublicKey (Blob const& serialized) diff --git a/src/ripple/crypto/impl/ec_key.cpp b/src/ripple/crypto/impl/ec_key.cpp index 6b8fc985a0..b81e1a7ee3 100644 --- a/src/ripple/crypto/impl/ec_key.cpp +++ b/src/ripple/crypto/impl/ec_key.cpp @@ -34,8 +34,6 @@ static inline EC_KEY* get_EC_KEY (const ec_key& that) return (EC_KEY*) that.get(); } -const ec_key ec_key::invalid = ec_key::acquire (nullptr); - ec_key::ec_key (const ec_key& that) { if (that.ptr == nullptr) diff --git a/src/ripple/crypto/impl/ec_key.h b/src/ripple/crypto/impl/ec_key.h index 9c2aaba96b..217fbfe578 100644 --- a/src/ripple/crypto/impl/ec_key.h +++ b/src/ripple/crypto/impl/ec_key.h @@ -31,32 +31,28 @@ class ec_key public: using pointer_t = struct opaque_EC_KEY*; -private: - pointer_t ptr; - - void destroy(); + ec_key () : ptr(nullptr) + { + } 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() { destroy(); } + bool valid() const + { + return ptr != nullptr; + } + pointer_t get() const { return ptr; } + ec_key (const ec_key&); + pointer_t release() { pointer_t released = ptr; @@ -66,7 +62,12 @@ public: return released; } - bool valid() const { return ptr != nullptr; } +private: + pointer_t ptr; + + void destroy(); + + ec_key& operator= (const ec_key&) = delete; }; } // openssl diff --git a/src/ripple/crypto/impl/openssl.cpp b/src/ripple/crypto/impl/openssl.cpp index 768560ed58..7305162ac7 100644 --- a/src/ripple/crypto/impl/openssl.cpp +++ b/src/ripple/crypto/impl/openssl.cpp @@ -133,7 +133,7 @@ static ec_key ec_key_new_secp256k1_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)