Skip to content

Commit bfaed1a

Browse files
author
MarcoFalke
committed
Merge #12460: Assert CPubKey::ValidLength to the pubkey's header-relevant size
f8c249a Assert CPubKey::ValidLength to the pubkey's header-relevent size (Ben Woosley) Pull request description: A pubkey's length is specific to its type which is indicated by its header value. GetLen returns the header-indicated length, so this change ensures that a key matches its header-indicated length. And replace some magic values with their constant equivalents. Tree-SHA512: b727b39a631babe0932326396fc4d796ade8ec1e37454ff0c709ae9b78ecbd0cfdf59d84089ba8415e6efa7bc180e3cd39a14ddaf0871cbac54b96851e1b7b44
2 parents 2106c4c + f8c249a commit bfaed1a

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

src/keystore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
127127
CScript::const_iterator pc = dest.begin();
128128
opcodetype opcode;
129129
std::vector<unsigned char> vch;
130-
if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
130+
if (!dest.GetOp(pc, opcode, vch) || !CPubKey::ValidSize(vch))
131131
return false;
132132
pubKeyOut = CPubKey(vch);
133133
if (!pubKeyOut.IsFullyValid())

src/pubkey.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class CPubKey
7070
}
7171

7272
public:
73+
74+
bool static ValidSize(const std::vector<unsigned char> &vch) {
75+
return vch.size() > 0 && GetLen(vch[0]) == vch.size();
76+
}
77+
7378
//! Construct an invalid public key.
7479
CPubKey()
7580
{

src/script/interpreter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ static inline void popstack(std::vector<valtype>& stack)
6161
}
6262

6363
bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
64-
if (vchPubKey.size() < 33) {
64+
if (vchPubKey.size() < CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
6565
// Non-canonical public key: too short
6666
return false;
6767
}
6868
if (vchPubKey[0] == 0x04) {
69-
if (vchPubKey.size() != 65) {
69+
if (vchPubKey.size() != CPubKey::PUBLIC_KEY_SIZE) {
7070
// Non-canonical public key: invalid length for uncompressed key
7171
return false;
7272
}
7373
} else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
74-
if (vchPubKey.size() != 33) {
74+
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
7575
// Non-canonical public key: invalid length for compressed key
7676
return false;
7777
}
@@ -83,7 +83,7 @@ bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
8383
}
8484

8585
bool static IsCompressedPubKey(const valtype &vchPubKey) {
86-
if (vchPubKey.size() != 33) {
86+
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
8787
// Non-canonical public key: invalid length for compressed key
8888
return false;
8989
}

src/script/standard.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
132132
// Template matching opcodes:
133133
if (opcode2 == OP_PUBKEYS)
134134
{
135-
while (vch1.size() >= 33 && vch1.size() <= 65)
135+
while (CPubKey::ValidSize(vch1))
136136
{
137137
vSolutionsRet.push_back(vch1);
138138
if (!script1.GetOp(pc1, opcode1, vch1))
@@ -146,7 +146,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
146146

147147
if (opcode2 == OP_PUBKEY)
148148
{
149-
if (vch1.size() < 33 || vch1.size() > 65)
149+
if (!CPubKey::ValidSize(vch1))
150150
break;
151151
vSolutionsRet.push_back(vch1);
152152
}

0 commit comments

Comments
 (0)