Skip to content

Commit 5909bcd

Browse files
committed
Add bounds checks in key_io before DecodeBase58Check
1 parent 2bcf1fc commit 5909bcd

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

src/base58.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <attributes.h>
1818

19-
#include <limits>
2019
#include <string>
2120
#include <vector>
2221

@@ -36,13 +35,13 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch);
3635
* return true if decoding is successful.
3736
* psz cannot be nullptr.
3837
*/
39-
NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
38+
NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len);
4039

4140
/**
4241
* Decode a base58-encoded string (str) into a byte vector (vchRet).
4342
* return true if decoding is successful.
4443
*/
45-
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
44+
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len);
4645

4746
/**
4847
* Encode a byte vector into a base58-encoded string, including checksum
@@ -53,12 +52,12 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
5352
* Decode a base58-encoded string (psz) that includes a checksum into a byte
5453
* vector (vchRet), return true if decoding is successful
5554
*/
56-
NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
55+
NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len);
5756

5857
/**
5958
* Decode a base58-encoded string (str) that includes a checksum into a byte
6059
* vector (vchRet), return true if decoding is successful
6160
*/
62-
NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
61+
NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len);
6362

6463
#endif // BITCOIN_BASE58_H

src/bench/base58.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void Base58Decode(benchmark::State& state)
4747
const char* addr = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem";
4848
std::vector<unsigned char> vch;
4949
while (state.KeepRunning()) {
50-
(void) DecodeBase58(addr, vch);
50+
(void) DecodeBase58(addr, vch, 64);
5151
}
5252
}
5353

src/key_io.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
7373
{
7474
std::vector<unsigned char> data;
7575
uint160 hash;
76-
if (DecodeBase58Check(str, data)) {
76+
if (DecodeBase58Check(str, data, 21)) {
7777
// base58-encoded Bitcoin addresses.
7878
// Public-key-hash-addresses have version 0 (or 111 testnet).
7979
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
@@ -133,7 +133,7 @@ CKey DecodeSecret(const std::string& str)
133133
{
134134
CKey key;
135135
std::vector<unsigned char> data;
136-
if (DecodeBase58Check(str, data)) {
136+
if (DecodeBase58Check(str, data, 34)) {
137137
const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
138138
if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
139139
std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
@@ -164,7 +164,7 @@ CExtPubKey DecodeExtPubKey(const std::string& str)
164164
{
165165
CExtPubKey key;
166166
std::vector<unsigned char> data;
167-
if (DecodeBase58Check(str, data)) {
167+
if (DecodeBase58Check(str, data, 78)) {
168168
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
169169
if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
170170
key.Decode(data.data() + prefix.size());
@@ -187,7 +187,7 @@ CExtKey DecodeExtKey(const std::string& str)
187187
{
188188
CExtKey key;
189189
std::vector<unsigned char> data;
190-
if (DecodeBase58Check(str, data)) {
190+
if (DecodeBase58Check(str, data, 78)) {
191191
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
192192
if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
193193
key.Decode(data.data() + prefix.size());

src/test/base58_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
5454
}
5555
std::vector<unsigned char> expected = ParseHex(test[0].get_str());
5656
std::string base58string = test[1].get_str();
57-
BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result), strTest);
57+
BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);
5858
BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
5959
}
6060

61-
BOOST_CHECK(!DecodeBase58("invalid", result));
61+
BOOST_CHECK(!DecodeBase58("invalid", result, 100));
6262

6363
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
64-
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result));
65-
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result));
64+
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
65+
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
6666
std::vector<unsigned char> expected = ParseHex("971a55");
6767
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
6868
}

0 commit comments

Comments
 (0)