Bitcoin Core  22.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 <chainparamsbase.h>
7 #include <key.h>
8 #include <psbt.h>
9 #include <pubkey.h>
10 #include <script/keyorigin.h>
11 #include <script/sign.h>
12 #include <script/signingprovider.h>
13 #include <streams.h>
15 #include <test/fuzz/fuzz.h>
16 #include <test/fuzz/util.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  static const ECCVerifyHandle ecc_verify_handle;
30  ECC_Start();
32 }
33 
35 {
36  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
37  const std::vector<uint8_t> key = ConsumeRandomLengthByteVector(fuzzed_data_provider, 128);
38 
39  {
40  CDataStream random_data_stream = ConsumeDataStream(fuzzed_data_provider);
41  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
42  try {
43  DeserializeHDKeypaths(random_data_stream, key, hd_keypaths);
44  } catch (const std::ios_base::failure&) {
45  }
47  SerializeHDKeypaths(serialized, hd_keypaths, CompactSizeWriter(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
48  }
49 
50  {
51  std::map<CPubKey, KeyOriginInfo> hd_keypaths;
52  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
53  const std::optional<CPubKey> pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
54  if (!pub_key) {
55  break;
56  }
57  const std::optional<KeyOriginInfo> key_origin_info = ConsumeDeserializable<KeyOriginInfo>(fuzzed_data_provider);
58  if (!key_origin_info) {
59  break;
60  }
61  hd_keypaths[*pub_key] = *key_origin_info;
62  }
64  try {
65  SerializeHDKeypaths(serialized, hd_keypaths, CompactSizeWriter(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
66  } catch (const std::ios_base::failure&) {
67  }
68  std::map<CPubKey, KeyOriginInfo> deserialized_hd_keypaths;
69  try {
70  DeserializeHDKeypaths(serialized, key, hd_keypaths);
71  } catch (const std::ios_base::failure&) {
72  }
73  assert(hd_keypaths.size() >= deserialized_hd_keypaths.size());
74  }
75 
76  {
77  SignatureData signature_data_1{ConsumeScript(fuzzed_data_provider)};
78  SignatureData signature_data_2{ConsumeScript(fuzzed_data_provider)};
79  signature_data_1.MergeSignatureData(signature_data_2);
80  }
81 
82  FillableSigningProvider provider;
83  CKey k;
84  const std::vector<uint8_t> key_data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
85  k.Set(key_data.begin(), key_data.end(), fuzzed_data_provider.ConsumeBool());
86  if (k.IsValid()) {
87  provider.AddKey(k);
88  }
89 
90  {
91  const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
92  const std::optional<CTxOut> tx_out = ConsumeDeserializable<CTxOut>(fuzzed_data_provider);
93  const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
94  if (mutable_transaction && tx_out && mutable_transaction->vin.size() > n_in) {
95  SignatureData signature_data_1 = DataFromTransaction(*mutable_transaction, n_in, *tx_out);
96  CTxIn input;
97  UpdateInput(input, signature_data_1);
98  const CScript script = ConsumeScript(fuzzed_data_provider);
99  SignatureData signature_data_2{script};
100  signature_data_1.MergeSignatureData(signature_data_2);
101  }
102  if (mutable_transaction) {
103  CTransaction tx_from{*mutable_transaction};
104  CMutableTransaction tx_to;
105  const std::optional<CMutableTransaction> opt_tx_to = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
106  if (opt_tx_to) {
107  tx_to = *opt_tx_to;
108  }
109  CMutableTransaction script_tx_to = tx_to;
110  CMutableTransaction sign_transaction_tx_to = tx_to;
111  if (n_in < tx_to.vin.size() && tx_to.vin[n_in].prevout.n < tx_from.vout.size()) {
112  (void)SignSignature(provider, tx_from, tx_to, n_in, fuzzed_data_provider.ConsumeIntegral<int>());
113  }
114  if (n_in < script_tx_to.vin.size()) {
115  (void)SignSignature(provider, ConsumeScript(fuzzed_data_provider), script_tx_to, n_in, ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<int>());
116  MutableTransactionSignatureCreator signature_creator{&tx_to, n_in, ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<int>()};
117  std::vector<unsigned char> vch_sig;
118  CKeyID address;
119  if (fuzzed_data_provider.ConsumeBool()) {
120  if (k.IsValid()) {
121  address = k.GetPubKey().GetID();
122  }
123  } else {
124  address = CKeyID{ConsumeUInt160(fuzzed_data_provider)};
125  }
126  (void)signature_creator.CreateSig(provider, vch_sig, address, ConsumeScript(fuzzed_data_provider), fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0}));
127  }
128  std::map<COutPoint, Coin> coins;
129  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
130  const std::optional<COutPoint> outpoint = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
131  if (!outpoint) {
132  break;
133  }
134  const std::optional<Coin> coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
135  if (!coin) {
136  break;
137  }
138  coins[*outpoint] = *coin;
139  }
140  std::map<int, bilingual_str> input_errors;
141  (void)SignTransaction(sign_transaction_tx_to, &provider, coins, fuzzed_data_provider.ConsumeIntegral<int>(), input_errors);
142  }
143  }
144 
145  {
146  SignatureData signature_data_1;
147  (void)ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, ConsumeScript(fuzzed_data_provider), signature_data_1);
148  SignatureData signature_data_2;
149  (void)ProduceSignature(provider, DUMMY_MAXIMUM_SIGNATURE_CREATOR, ConsumeScript(fuzzed_data_provider), signature_data_2);
150  }
151 }
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:34
streams.h
DUMMY_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:580
SignatureData::MergeSignatureData
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:500
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:90
chainparamsbase.h
ConsumeScript
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:340
util.h
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
MutableTransactionSignatureCreator
A signature creator for transactions.
Definition: sign.h:37
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
DeserializeHDKeypaths
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: psbt.h:121
pubkey.h
chainparams.h
FillableSigningProvider::AddKey
virtual bool AddKey(const CKey &key)
Definition: signingprovider.h:151
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
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:581
SignatureData
Definition: sign.h:63
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:516
ConsumeDataStream
CDataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:76
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
FuzzedDataProvider.h
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:63
SigVersion::WITNESS_V0
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
ConsumeUInt160
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:146
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:580
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
SerializeHDKeypaths
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, CompactSizeWriter type)
Definition: psbt.h:156
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:333
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:391
key.h
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:332
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:27
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
UpdateInput
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:494
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:280
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:429
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:276
ByteUnit::k
@ k
CompactSizeWriter
Definition: serialize.h:530
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12