Skip to content

Commit

Permalink
Assert CPubKey::ValidLength to the pubkey's header-relevent size
Browse files Browse the repository at this point in the history
Previously this was an inline test where the specificity was probably judged
overly specific. As a class method it makes sense to maintain consistency.

And replace some magic values with their constant equivalents.

Excludes changes to the following functions we don't have:

- ExtractPubKey (bitcoin#6415)
- IsCompressedPubKey (bitcoin#8499)
  • Loading branch information
Empact authored and furszy committed Jun 18, 2020
1 parent 2f6b249 commit fe31b31
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/pubkey.h
Expand Up @@ -69,6 +69,11 @@ class CPubKey
}

public:

bool static ValidSize(const std::vector<unsigned char> &vch) {
return vch.size() > 0 && GetLen(vch[0]) == vch.size();
}

//! Construct an invalid public key.
CPubKey()
{
Expand Down
6 changes: 3 additions & 3 deletions src/script/interpreter.cpp
Expand Up @@ -64,17 +64,17 @@ static inline void popstack(std::vector<valtype>& 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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/script/standard.cpp
Expand Up @@ -124,7 +124,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
// Template matching opcodes:
if (opcode2 == OP_PUBKEYS)
{
while (vch1.size() >= 33 && vch1.size() <= 65)
while (CPubKey::ValidSize(vch1))
{
vSolutionsRet.push_back(vch1);
if (!script1.GetOp(pc1, opcode1, vch1))
Expand All @@ -138,7 +138,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v

if (opcode2 == OP_PUBKEY)
{
if (vch1.size() < 33 || vch1.size() > 65)
if (!CPubKey::ValidSize(vch1))
break;
vSolutionsRet.push_back(vch1);
}
Expand Down

0 comments on commit fe31b31

Please sign in to comment.