Skip to content

Commit c6b7b29

Browse files
committed
Improve robustness of DER recoding code
Add some defensive programming on top of dashpay#5634. This copies the respective OpenSSL code in ECDSA_verify in OpenSSL pre-1.0.1k (e.g. https://github.com/openssl/openssl/blob/OpenSSL_1_0_1j/crypto/ecdsa/ecs_vrf.c#L89) more closely. As reported by @SergioDemianLerner.
1 parent 4f73a8f commit c6b7b29

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/ecwrapper.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,18 @@ bool CECKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSi
124124
unsigned char *norm_der = NULL;
125125
ECDSA_SIG *norm_sig = ECDSA_SIG_new();
126126
const unsigned char* sigptr = &vchSig[0];
127-
d2i_ECDSA_SIG(&norm_sig, &sigptr, vchSig.size());
127+
assert(norm_sig);
128+
if (d2i_ECDSA_SIG(&norm_sig, &sigptr, vchSig.size()) == NULL)
129+
{
130+
/* As of OpenSSL 1.0.0p d2i_ECDSA_SIG frees and nulls the pointer on
131+
* error. But OpenSSL's own use of this function redundantly frees the
132+
* result. As ECDSA_SIG_free(NULL) is a no-op, and in the absence of a
133+
* clear contract for the function behaving the same way is more
134+
* conservative.
135+
*/
136+
ECDSA_SIG_free(norm_sig);
137+
return false;
138+
}
128139
int derlen = i2d_ECDSA_SIG(norm_sig, &norm_der);
129140
ECDSA_SIG_free(norm_sig);
130141
if (derlen <= 0)

0 commit comments

Comments
 (0)