diff --git a/src/keystore.cpp b/src/keystore.cpp index fab1b81c9..e3f2a1700 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -131,7 +131,7 @@ static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut) CScript::const_iterator pc = dest.begin(); opcodetype opcode; std::vector vch; - if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65) + if (!dest.GetOp(pc, opcode, vch) || !CPubKey::ValidSize(vch)) return false; pubKeyOut = CPubKey(vch); if (!pubKeyOut.IsFullyValid()) diff --git a/src/pubkey.h b/src/pubkey.h index 59bf56395..9c6c6b085 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -70,6 +70,11 @@ private: } public: + + bool static ValidSize(const std::vector &vch) { + return vch.size() > 0 && GetLen(vch[0]) == vch.size(); + } + //! Construct an invalid public key. CPubKey() { diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index 2cdff7ee5..de9e3b3b1 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -61,17 +61,17 @@ static inline void popstack(std::vector& stack) } bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { - if (vchPubKey.size() < 33) { + if (vchPubKey.size() < CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) { // Non-canonical public key: too short return false; } if (vchPubKey[0] == 0x04) { - if (vchPubKey.size() != 65) { + if (vchPubKey.size() != CPubKey::PUBLIC_KEY_SIZE) { // Non-canonical public key: invalid length for uncompressed key return false; } } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) { - if (vchPubKey.size() != 33) { + if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) { // Non-canonical public key: invalid length for compressed key return false; } @@ -83,7 +83,7 @@ bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { } bool static IsCompressedPubKey(const valtype &vchPubKey) { - if (vchPubKey.size() != 33) { + if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) { // Non-canonical public key: invalid length for compressed key return false; } diff --git a/src/script/standard.cpp b/src/script/standard.cpp index cfb3c5858..0b9053d7f 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -132,7 +132,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector= 33 && vch1.size() <= 65) + while (CPubKey::ValidSize(vch1)) { vSolutionsRet.push_back(vch1); if (!script1.GetOp(pc1, opcode1, vch1)) @@ -146,7 +146,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector 65) + if (!CPubKey::ValidSize(vch1)) break; vSolutionsRet.push_back(vch1); }