Bitcoin Core  26.99.0
P2P Digital Currency
script_sign.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <key.h>
7 #include <psbt.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/chaintype.h>
17 #include <util/translation.h>
18 
19 #include <cassert>
20 #include <cstdint>
21 #include <iostream>
22 #include <map>
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
28 {
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  DataStream 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  }
45  DataStream serialized{};
46  SerializeHDKeypaths(serialized, hd_keypaths, CompactSizeWriter(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
47  }
48 
49  {
50  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
51  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
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  }
62  DataStream serialized{};
63  try {
64  SerializeHDKeypaths(serialized, hd_keypaths, CompactSizeWriter(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 = ConsumePrivateKey(fuzzed_data_provider);
83  if (k.IsValid()) {
84  provider.AddKey(k);
85  }
86 
87  {
88  const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
89  const std::optional<CTxOut> tx_out = ConsumeDeserializable<CTxOut>(fuzzed_data_provider);
90  const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
91  if (mutable_transaction && tx_out && mutable_transaction->vin.size() > n_in) {
92  SignatureData signature_data_1 = DataFromTransaction(*mutable_transaction, n_in, *tx_out);
93  CTxIn input;
94  UpdateInput(input, signature_data_1);
95  const CScript script = ConsumeScript(fuzzed_data_provider);
96  SignatureData signature_data_2{script};
97  signature_data_1.MergeSignatureData(signature_data_2);
98  }
99  if (mutable_transaction) {
100  CTransaction tx_from{*mutable_transaction};
101  CMutableTransaction tx_to;
102  const std::optional<CMutableTransaction> opt_tx_to = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
103  if (opt_tx_to) {
104  tx_to = *opt_tx_to;
105  }
106  CMutableTransaction script_tx_to = tx_to;
107  CMutableTransaction sign_transaction_tx_to = tx_to;
108  if (n_in < tx_to.vin.size() && tx_to.vin[n_in].prevout.n < tx_from.vout.size()) {
109  SignatureData empty;
110  (void)SignSignature(provider, tx_from, tx_to, n_in, fuzzed_data_provider.ConsumeIntegral<int>(), empty);
111  }
112  if (n_in < script_tx_to.vin.size()) {
113  SignatureData empty;
114  (void)SignSignature(provider, ConsumeScript(fuzzed_data_provider), script_tx_to, n_in, ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<int>(), empty);
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{ConsumeCoins(fuzzed_data_provider)};
128  std::map<int, bilingual_str> input_errors;
129  (void)SignTransaction(sign_transaction_tx_to, &provider, coins, fuzzed_data_provider.ConsumeIntegral<int>(), input_errors);
130  }
131  }
132 
133  {
134  SignatureData signature_data_1;
135  (void)ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, ConsumeScript(fuzzed_data_provider), signature_data_1);
136  SignatureData signature_data_2;
137  (void)ProduceSignature(provider, DUMMY_MAXIMUM_SIGNATURE_CREATOR, ConsumeScript(fuzzed_data_provider), signature_data_2);
138  }
139 }
ECC_Start()
Definition: key.cpp:435
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
An encapsulated private key.
Definition: key.h:33
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
An input of a transaction.
Definition: transaction.h:67
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
Fillable signing provider that keeps keys in an address->secret map.
virtual bool AddKey(const CKey &key)
T PickValueInArray(const T(&array)[size])
A signature creator for transactions.
Definition: sign.h:40
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:23
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, CompactSizeWriter type)
Definition: psbt.h:181
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: psbt.h:139
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.
FUZZ_TARGET(script_sign,.init=initialize_script_sign)
Definition: script_sign.cpp:33
void initialize_script_sign()
Definition: script_sign.cpp:27
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:499
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:672
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:763
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:607
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:762
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType, SignatureData &sig_data)
Produce a satisfying script (scriptSig or witness).
Definition: sign.cpp:694
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxIn > vin
Definition: transaction.h:379
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:678
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:93
std::map< COutPoint, Coin > ConsumeCoins(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:166
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
Definition: util.cpp:227
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:29
DataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:73
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:160
assert(!tx.IsCoinBase())