mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 23:07:59 +01:00
Merge #8976: libconsensus: Add input validation of flags
5ca8ef2
libconsensus: Add input validation of flags (Wladimir J. van der Laan)
This commit is contained in:
commit
8c6218a28a
3 changed files with 21 additions and 5 deletions
|
@ -69,10 +69,19 @@ struct ECCryptoClosure
|
||||||
ECCryptoClosure instance_of_eccryptoclosure;
|
ECCryptoClosure instance_of_eccryptoclosure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Check that all specified flags are part of the libconsensus interface. */
|
||||||
|
static bool verify_flags(unsigned int flags)
|
||||||
|
{
|
||||||
|
return (flags & ~(bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
|
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
|
||||||
const unsigned char *txTo , unsigned int txToLen,
|
const unsigned char *txTo , unsigned int txToLen,
|
||||||
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
|
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
|
||||||
{
|
{
|
||||||
|
if (!verify_flags(flags)) {
|
||||||
|
return bitcoinconsensus_ERR_INVALID_FLAGS;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
|
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
|
||||||
CTransaction tx;
|
CTransaction tx;
|
||||||
|
|
|
@ -42,6 +42,7 @@ typedef enum bitcoinconsensus_error_t
|
||||||
bitcoinconsensus_ERR_TX_SIZE_MISMATCH,
|
bitcoinconsensus_ERR_TX_SIZE_MISMATCH,
|
||||||
bitcoinconsensus_ERR_TX_DESERIALIZE,
|
bitcoinconsensus_ERR_TX_DESERIALIZE,
|
||||||
bitcoinconsensus_ERR_AMOUNT_REQUIRED,
|
bitcoinconsensus_ERR_AMOUNT_REQUIRED,
|
||||||
|
bitcoinconsensus_ERR_INVALID_FLAGS,
|
||||||
} bitcoinconsensus_error;
|
} bitcoinconsensus_error;
|
||||||
|
|
||||||
/** Script verification flags */
|
/** Script verification flags */
|
||||||
|
@ -54,6 +55,9 @@ enum
|
||||||
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
|
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
|
||||||
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10), // enable CHECKSEQUENCEVERIFY (BIP112)
|
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10), // enable CHECKSEQUENCEVERIFY (BIP112)
|
||||||
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS = (1U << 11), // enable WITNESS (BIP141)
|
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS = (1U << 11), // enable WITNESS (BIP141)
|
||||||
|
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL = bitcoinconsensus_SCRIPT_FLAGS_VERIFY_P2SH | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_DERSIG |
|
||||||
|
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_NULLDUMMY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY |
|
||||||
|
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY | bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Returns 1 if the input nIn of the serialized transaction pointed to by
|
/// Returns 1 if the input nIn of the serialized transaction pointed to by
|
||||||
|
|
|
@ -173,11 +173,14 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
|
||||||
#if defined(HAVE_CONSENSUS_LIB)
|
#if defined(HAVE_CONSENSUS_LIB)
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
stream << tx2;
|
stream << tx2;
|
||||||
|
int libconsensus_flags = flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL;
|
||||||
|
if (libconsensus_flags == flags) {
|
||||||
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
|
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
|
||||||
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect, message);
|
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
|
||||||
} else {
|
} else {
|
||||||
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect, message);
|
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
|
||||||
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(begin_ptr(scriptPubKey), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect,message);
|
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(begin_ptr(scriptPubKey), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect,message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue