Skip to content

Commit 8b5ba93

Browse files
committed
Make it easier for the fuzzer to get a VerifiedInvoiceRequest
In the next commit we attempt to verify `InvoiceRequest`s when fuzzing so that we can test fetching the `InvoiceRequestFields`, but its useful to allow the verification to succeed more often first, which we do here.
1 parent c049d18 commit 8b5ba93

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

lightning/src/offers/signer.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,15 @@ pub(super) fn verify_recipient_metadata<'a, T: secp256k1::Signing>(
379379
signing_pubkey: PublicKey, tlv_stream: impl core::iter::Iterator<Item = TlvRecord<'a>>,
380380
secp_ctx: &Secp256k1<T>,
381381
) -> Result<Option<Keypair>, ()> {
382-
let mut hmac = hmac_for_message(metadata, expanded_key, iv_bytes, tlv_stream)?;
382+
let hmac_res = hmac_for_message(metadata, expanded_key, iv_bytes, tlv_stream);
383+
#[cfg(fuzzing)]
384+
if hmac_res.is_err() {
385+
// In fuzzing its relatively challenging for the fuzzer to find cases where we have issues
386+
// in a BOLT 12 object but also have a right-sized nonce. So instead we allow any size
387+
// nonce (i.e. `hmac_for_message` failing) and simply treat it as "no keypair").
388+
return Ok(None);
389+
}
390+
let mut hmac = hmac_res?;
383391
hmac.input(WITHOUT_ENCRYPTED_PAYMENT_ID_HMAC_INPUT);
384392

385393
verify_metadata(metadata, Hmac::from_engine(hmac), signing_pubkey, secp_ctx)
@@ -393,19 +401,32 @@ fn verify_metadata<T: secp256k1::Signing>(
393401
secp_ctx,
394402
&SecretKey::from_slice(hmac.as_byte_array()).unwrap(),
395403
);
396-
if fixed_time_eq(&signing_pubkey.serialize(), &derived_keys.public_key().serialize()) {
404+
#[allow(unused_mut)]
405+
let mut ok =
406+
fixed_time_eq(&signing_pubkey.serialize(), &derived_keys.public_key().serialize());
407+
#[cfg(fuzzing)]
408+
if metadata[0] & 1 == 0 {
409+
ok = true;
410+
}
411+
if ok {
397412
Ok(Some(derived_keys))
398413
} else {
399414
Err(())
400415
}
401-
} else if metadata[Nonce::LENGTH..].len() == Sha256::LEN {
402-
if fixed_time_eq(&metadata[Nonce::LENGTH..], &hmac.to_byte_array()) {
416+
} else {
417+
#[allow(unused_mut)]
418+
let mut ok =
419+
metadata.len() == Nonce::LENGTH + Sha256::LEN
420+
&& fixed_time_eq(&metadata[Nonce::LENGTH..], &hmac.to_byte_array());
421+
#[cfg(fuzzing)]
422+
if metadata[0] & 1 == 0 {
423+
ok = true;
424+
}
425+
if ok {
403426
Ok(None)
404427
} else {
405428
Err(())
406429
}
407-
} else {
408-
Err(())
409430
}
410431
}
411432

0 commit comments

Comments
 (0)