Skip to content

Commit 408f9b6

Browse files
author
Ross Nicoll
committed
OpenSSL 1.0.1k compatibility
OpenSSL 1.0.1k rejects non-canonical DER signatures, this patch decodes and then re-encodes the signature before verification to ensure a consistent result.
1 parent 22d0256 commit 408f9b6

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

bitcoin/core/key.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,24 @@ def sign(self, hash):
114114

115115
def verify(self, hash, sig):
116116
"""Verify a DER signature"""
117-
return _ssl.ECDSA_verify(0, hash, len(hash), sig, len(sig), self.k) == 1
117+
if not sig:
118+
return false
119+
120+
# New versions of OpenSSL will reject non-canonical DER signatures. de/re-serialize first.
121+
norm_sig = ctypes.c_void_p(0)
122+
_ssl.d2i_ECDSA_SIG(ctypes.byref(norm_sig), ctypes.byref(ctypes.c_char_p(sig)), len(sig))
123+
124+
derlen = _ssl.i2d_ECDSA_SIG(norm_sig, 0)
125+
if derlen == 0:
126+
_ssl.ECDSA_SIG_free(norm_sig)
127+
return false
128+
129+
norm_der = ctypes.create_string_buffer(derlen)
130+
_ssl.i2d_ECDSA_SIG(norm_sig, ctypes.byref(ctypes.pointer(norm_der)))
131+
_ssl.ECDSA_SIG_free(norm_sig)
132+
133+
# -1 = error, 0 = bad sig, 1 = good
134+
return _ssl.ECDSA_verify(0, hash, len(hash), norm_der, derlen, self.k) == 1
118135

119136
def set_compressed(self, compressed):
120137
if compressed:

0 commit comments

Comments
 (0)