Skip to content

Commit

Permalink
Properly validate recid
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Sep 4, 2024
1 parent cbbb505 commit dd80674
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NBitcoin/CompactSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public CompactSignature(int recoveryId, byte[] sig64)
throw new ArgumentNullException(nameof(sig64));
if (sig64.Length is not 64)
throw new ArgumentException("sig64 should be 64 bytes", nameof(sig64));
if (!IsValidRecId(recoveryId))
throw new ArgumentOutOfRangeException(nameof(recoveryId), "recoveryId should be recoveryId >= 0 && recoveryId < 4");
RecoveryId = recoveryId;
Signature = sig64;
}
public static bool IsValidRecId(int recid) => recid >= 0 && recid < 4;

/// <summary>
///
Expand Down
6 changes: 6 additions & 0 deletions NBitcoin/Secp256k1/Recovery/SecpRecoverableECDSASignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ public SecpRecoverableECDSASignature(SecpECDSASignature sig, int recid)
{
if (sig == null)
throw new ArgumentNullException(nameof(sig));
if (!IsValidRecId(recid))
throw new ArgumentOutOfRangeException(nameof(recid), "recid should be recid >= 0 && recid < 4");
this.r = sig.r;
this.s = sig.s;
this.recid = recid;
}

static bool IsValidRecId(int recid) => recid >= 0 && recid < 4;

public static bool TryCreateFromCompact(ReadOnlySpan<byte> in64, int recid, [MaybeNullWhen(false)] out SecpRecoverableECDSASignature sig)
{
sig = null;
if (!IsValidRecId(recid))
return false;
if (SecpECDSASignature.TryCreateFromCompact(in64, out var compact) && compact is SecpECDSASignature)
{
sig = new SecpRecoverableECDSASignature(compact, recid);
Expand Down

0 comments on commit dd80674

Please sign in to comment.