Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent PGP Signature Type 0xFF #1687

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public boolean isCertification()
public void init(PGPContentVerifierBuilderProvider verifierBuilderProvider, PGPPublicKey pubKey)
throws PGPException
{
if (sigType == 0xff)
{
throw new PGPException("Illegal signature type 0xFF encountered.");
}
PGPContentVerifierBuilder verifierBuilder = createVerifierProvider(verifierBuilderProvider);

init(verifierBuilder.build(pubKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void init(
PGPPrivateKey key)
throws PGPException
{
if (signatureType == 0xff)
{
throw new PGPException("Illegal signature type 0xFF provided.");
}
contentSigner = contentSignerBuilder.build(signatureType, key);
sigOut = contentSigner.getOutputStream();
sigType = contentSigner.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public void init(
PGPPrivateKey key)
throws PGPException
{
if (signatureType == 0xFF)
{
throw new PGPException("Illegal signature type 0xFF provided.");
}
contentSigner = contentSignerBuilder.build(signatureType, key);
sigOut = contentSigner.getOutputStream();
sigType = contentSigner.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Iterator;

import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
Expand Down Expand Up @@ -45,7 +46,10 @@
import org.bouncycastle.openpgp.PGPV3SignatureGenerator;
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
Expand Down Expand Up @@ -762,6 +766,7 @@ public void performTest()
testSignatureTarget();
testUserAttributeEncoding();
testExportNonExportableSignature();
testRejectionOfIllegalSignatureType0xFF();
}

private void testUserAttributeEncoding()
Expand Down Expand Up @@ -1367,6 +1372,44 @@ public void testExportNonExportableSignature()
isTrue(nonExportableSig.getEncoded(true).length == 0);
}

private void testRejectionOfIllegalSignatureType0xFF()
throws PGPException, IOException
{
PGPSecretKeyRing pgpPriv = new PGPSecretKeyRing(rsaKeyRing, new JcaKeyFingerprintCalculator());
PGPSecretKey secretKey = pgpPriv.getSecretKey();
PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(rsaPass));

PGPContentSignerBuilder sigBuilder = new BcPGPContentSignerBuilder(
PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA512);
PGPSignatureGenerator generator = new PGPSignatureGenerator(sigBuilder);
try
{
generator.init(0xFF, pgpPrivKey);
fail("Generating signature of type 0xff MUST fail.");
}
catch (PGPException e)
{
// Expected
}

PGPContentVerifierBuilderProvider verifBuilder = new BcPGPContentVerifierBuilderProvider();

// signature of type 0xff (illegal)
byte[] hexSig = Hex.decode("889c04ff010a000605026655fdbe000a0910b3c272c907c7f7b2133604008dc801695e0905a21a03b832dfd576d66dc23a6ac8715128aaa5cee941b36660efd3c47618c5e880b2dc5e8a34638f10061ae6a9724a2306b66eeb4aec79b49ce4ec48f6de0b5119fc7911e9e2a7677bc4a1f6dd783ce15949457872246e0b415c6f8e3390da90597b059009dcc64723adbc45530a1db0ef70fcffbfc97af6b6");
ByteArrayInputStream bIn = new ByteArrayInputStream(hexSig);
BCPGInputStream pIn = new BCPGInputStream(bIn);
PGPSignature s = new PGPSignature(pIn);
try
{
s.init(verifBuilder, secretKey.getPublicKey());
fail("Verifying signature of type 0xff MUST fail.");
}
catch (PGPException e)
{
// expected
}
}

private PGPSignatureList readSignatures(String armored)
throws IOException
{
Expand Down