Bitcoin Core  22.99.0
P2P Digital Currency
script_sign.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <chainparams.h>
6 #include <chainparamsbase.h>
7 #include <key.h>
8 #include <pubkey.h>
9 #include <script/keyorigin.h>
10 #include <script/sign.h>
11 #include <script/signingprovider.h>
12 #include <streams.h>
14 #include <test/fuzz/fuzz.h>
15 #include <test/fuzz/util.h>
16 #include <util/translation.h>
17 
18 #include <cassert>
19 #include <cstdint>
20 #include <iostream>
21 #include <map>
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
27 {
28  static const ECCVerifyHandle ecc_verify_handle;
29  ECC_Start();
31 }
32 
34 {
35  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
36  const std::vector<uint8_t> key = ConsumeRandomLengthByteVector(fuzzed_data_provider, 128);
37 
38  {
39  CDataStream random_data_stream = ConsumeDataStream(fuzzed_data_provider);
40  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
41  try {
42  DeserializeHDKeypaths(random_data_stream, key, hd_keypaths);
43  } catch (const std::ios_base::failure&) {
44  }
46  SerializeHDKeypaths(serialized, hd_keypaths, fuzzed_data_provider.ConsumeIntegral<uint8_t>());
47  }
48 
49  {
50  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
51  while (fuzzed_data_provider.ConsumeBool()) {
52  const std::optional<CPubKey> pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
53  if (!pub_key) {
54  break;
55  }
56  const std::optional<KeyOriginInfo> key_origin_info = ConsumeDeserializable<KeyOriginInfo>(fuzzed_data_provider);
57  if (!key_origin_info) {
58  break;
59  }
60  hd_keypaths[*pub_key] = *key_origin_info;
61  }
63  try {
64  SerializeHDKeypaths(serialized, hd_keypaths, fuzzed_data_provider.ConsumeIntegral<uint8_t>());
65  } catch (const std::ios_base::failure&) {
66  }
67  std::map<CPubKey, KeyOriginInfo> deserialized_hd_keypaths;
68  try {
69  DeserializeHDKeypaths(serialized, key, hd_keypaths);
70  } catch (const std::ios_base::failure&) {
71  }
72  assert(hd_keypaths.size() >= deserialized_hd_keypaths.size());
73  }
74 
75  {
76  SignatureData signature_data_1{ConsumeScript(fuzzed_data_provider)};
77  SignatureData signature_data_2{ConsumeScript(fuzzed_data_provider)};
78  signature_data_1.MergeSignatureData(signature_data_2);
79  }
80 
81  FillableSigningProvider provider;
82  CKey k;
83  const std::vector<uint8_t> key_data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
84  k.Set(key_data.begin(), key_data.end(), fuzzed_data_provider.ConsumeBool());
85  if (k.IsValid()) {
86  provider.AddKey(k);
87  }
88 
89  {
90  const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
91  const std::optional<CTxOut> tx_out = ConsumeDeserializable<CTxOut>(fuzzed_data_provider);
92  const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
93  if (mutable_transaction && tx_out && mutable_transaction->vin.size() > n_in) {
94  SignatureData signature_data_1 = DataFromTransaction(*mutable_transaction, n_in, *tx_out);
95  CTxIn input;
96  UpdateInput(input, signature_data_1);
97  const CScript script = ConsumeScript(fuzzed_data_provider);
98  SignatureData signature_data_2{script};
99  signature_data_1.MergeSignatureData(signature_data_2);
100  }
101  if (mutable_transaction) {
102  CTransaction tx_from{*mutable_transaction};
103  CMutableTransaction tx_to;
104  const std::optional<CMutableTransaction> opt_tx_to = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
105  if (opt_tx_to) {
106  tx_to = *opt_tx_to;
107  }
108  CMutableTransaction script_tx_to = tx_to;
109  CMutableTransaction sign_transaction_tx_to = tx_to;
110  if (n_in < tx_to.vin.size() && tx_to.vin[n_in].prevout.n < tx_from.vout.size()) {
111  (void)SignSignature(provider, tx_from, tx_to, n_in, fuzzed_data_provider.ConsumeIntegral<int>());
112  }
113  if (n_in < script_tx_to.vin.size()) {
114  (void)SignSignature(provider, ConsumeScript(fuzzed_data_provider), script_tx_to, n_in, ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<int>());
115  MutableTransactionSignatureCreator signature_creator{&tx_to, n_in, ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<int>()};
116  std::vector<unsigned char> vch_sig;
117  CKeyID address;
118  if (fuzzed_data_provider.ConsumeBool()) {
119  if (k.IsValid()) {
120  address = k.GetPubKey().GetID();
121  }
122  } else {
123  address = CKeyID{ConsumeUInt160(fuzzed_data_provider)};
124  }
125  (void)signature_creator.CreateSig(provider, vch_sig, address, ConsumeScript(fuzzed_data_provider), fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0}));
126  }
127  std::map<COutPoint, Coin> coins;
128  while (fuzzed_data_provider.ConsumeBool()) {
129  const std::optional<COutPoint> outpoint = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
130  if (!outpoint) {
131  break;
132  }
133  const std::optional<Coin> coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
134  if (!coin) {
135  break;
136  }
137  coins[*outpoint] = *coin;
138  }
139  std::map<int, bilingual_str> input_errors;
140  (void)SignTransaction(sign_transaction_tx_to, &provider, coins, fuzzed_data_provider.ConsumeIntegral<int>(), input_errors);
141  }
142  }
143 
144  {
145  SignatureData signature_data_1;
146  (void)ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, ConsumeScript(fuzzed_data_provider), signature_data_1);
147  SignatureData signature_data_2;
148  (void)ProduceSignature(provider, DUMMY_MAXIMUM_SIGNATURE_CREATOR, ConsumeScript(fuzzed_data_provider), signature_data_2);
149  }
150 }
CTxIn
An input of a transaction.
Definition: transaction.h:65
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
assert
assert(!tx.IsCoinBase())
SigVersion::BASE
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(script_sign, initialize_script_sign)
Definition: script_sign.cpp:33
streams.h
DUMMY_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:577
SignatureData::MergeSignatureData
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:497
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:91
chainparamsbase.h
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
util.h
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
MutableTransactionSignatureCreator
A signature creator for transactions.
Definition: sign.h:39
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
pubkey.h
chainparams.h
FillableSigningProvider::AddKey
virtual bool AddKey(const CKey &key)
Definition: signingprovider.h:152
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
ConsumeScript
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length, const bool maybe_p2wsh) noexcept
Definition: util.cpp:285
signingprovider.h
DUMMY_MAXIMUM_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:578
SignatureData
Definition: sign.h:65
SignSignature
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType)
Produce a script signature for a transaction.
Definition: sign.cpp:513
ConsumeDataStream
CDataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:74
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
FuzzedDataProvider.h
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:92
sign.h
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
SigVersion::WITNESS_V0
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
ConsumeUInt160
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:144
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:555
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
DeserializeHDKeypaths
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: sign.h:111
ProduceSignature
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:330
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:370
key.h
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:315
CKey
An encapsulated private key.
Definition: key.h:26
translation.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
initialize_script_sign
void initialize_script_sign()
Definition: script_sign.cpp:26
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
UpdateInput
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:491
SignTransaction
void SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, const UniValue &hashType, UniValue &result)
Sign a transaction with the given keystore and previous transactions.
Definition: rawtransaction_util.cpp:279
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
keyorigin.h
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
DataFromTransaction
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:426
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:221
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
SerializeHDKeypaths
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, uint8_t type)
Definition: sign.h:146