Skip to content

Commit

Permalink
PGPSignature: Sanitize hash length when verifying
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Jul 1, 2024
1 parent 94cc3a4 commit 204cb7c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.HashUtils;
import org.bouncycastle.bcpg.MPInteger;
import org.bouncycastle.bcpg.Packet;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
Expand Down Expand Up @@ -163,14 +164,32 @@ PGPContentVerifierBuilder createVerifierProvider(PGPContentVerifierBuilderProvid
}

void init(PGPContentVerifier verifier)
throws PGPException
{
this.verifier = verifier;
this.lastb = 0;
this.sigOut = verifier.getOutputStream();

checkSaltSize();
updateWithSalt();
}

private void checkSaltSize()
throws PGPException
{
if (getVersion() != SignaturePacket.VERSION_6)
{
return;
}

int expectedSaltSize = HashUtils.getV6SignatureSaltSizeInBytes(getHashAlgorithm());
if (expectedSaltSize != sigPck.getSalt().length)
{
throw new PGPException("RFC9580 defines the salt size for " + PGPUtil.getDigestName(getHashAlgorithm()) +
" as " + expectedSaltSize + " octets, but signature has " + sigPck.getSalt().length + " octets.");
}
}

private void updateWithSalt()
{
if (getVersion() == SignaturePacket.VERSION_6)
Expand Down

0 comments on commit 204cb7c

Please sign in to comment.