Skip to content

Commit 55d09fd

Browse files
committed
Merge branch 'pr-1687-prevent-0xFF-PGP-signature' into 'main'
pr-1687-prevent-0xFF-PGP-signature See merge request root/bc-java!14
2 parents 96af91b + 14fcbad commit 55d09fd

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public boolean isCertification()
149149
public void init(PGPContentVerifierBuilderProvider verifierBuilderProvider, PGPPublicKey pubKey)
150150
throws PGPException
151151
{
152+
if (sigType == 0xFF)
153+
{
154+
throw new PGPException("Illegal signature type 0xFF provided.");
155+
}
152156
PGPContentVerifierBuilder verifierBuilder = createVerifierProvider(verifierBuilderProvider);
153157

154158
init(verifierBuilder.build(pubKey));

pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public void init(
5353
PGPPrivateKey key)
5454
throws PGPException
5555
{
56+
if (signatureType == 0xFF)
57+
{
58+
throw new PGPException("Illegal signature type 0xFF provided.");
59+
}
5660
contentSigner = contentSignerBuilder.build(signatureType, key);
5761
sigOut = contentSigner.getOutputStream();
5862
sigType = contentSigner.getType();

pg/src/main/java/org/bouncycastle/openpgp/PGPV3SignatureGenerator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public void init(
4444
PGPPrivateKey key)
4545
throws PGPException
4646
{
47+
if (signatureType == 0xFF)
48+
{
49+
throw new PGPException("Illegal signature type 0xFF provided.");
50+
}
51+
4752
contentSigner = contentSignerBuilder.build(signatureType, key);
4853
sigOut = contentSigner.getOutputStream();
4954
sigType = contentSigner.getType();

pg/src/test/java/org/bouncycastle/openpgp/test/PGPSignatureTest.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@
1111
import java.util.Date;
1212
import java.util.Iterator;
1313

14-
import org.bouncycastle.bcpg.ArmoredInputStream;
15-
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
16-
import org.bouncycastle.bcpg.HashAlgorithmTags;
17-
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
18-
import org.bouncycastle.bcpg.SignatureSubpacket;
19-
import org.bouncycastle.bcpg.SignatureSubpacketInputStream;
20-
import org.bouncycastle.bcpg.SignatureSubpacketTags;
21-
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
14+
import org.bouncycastle.bcpg.*;
2215
import org.bouncycastle.bcpg.sig.IntendedRecipientFingerprint;
2316
import org.bouncycastle.bcpg.sig.IssuerFingerprint;
2417
import org.bouncycastle.bcpg.sig.KeyFlags;
@@ -45,7 +38,10 @@
4538
import org.bouncycastle.openpgp.PGPV3SignatureGenerator;
4639
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
4740
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
41+
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
42+
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
4843
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
44+
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
4945
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
5046
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
5147
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
@@ -762,6 +758,7 @@ public void performTest()
762758
testSignatureTarget();
763759
testUserAttributeEncoding();
764760
testExportNonExportableSignature();
761+
testRejectionOfIllegalSignatureType0xFF();
765762
}
766763

767764
private void testUserAttributeEncoding()
@@ -1367,6 +1364,55 @@ public void testExportNonExportableSignature()
13671364
isTrue(nonExportableSig.getEncoded(true).length == 0);
13681365
}
13691366

1367+
private void testRejectionOfIllegalSignatureType0xFF()
1368+
throws PGPException, IOException
1369+
{
1370+
PGPSecretKeyRing pgpPriv = new PGPSecretKeyRing(rsaKeyRing, new JcaKeyFingerprintCalculator());
1371+
PGPSecretKey secretKey = pgpPriv.getSecretKey();
1372+
PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(rsaPass));
1373+
1374+
PGPContentSignerBuilder sigBuilder = new BcPGPContentSignerBuilder(
1375+
PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA512);
1376+
PGPSignatureGenerator generator = new PGPSignatureGenerator(sigBuilder);
1377+
try
1378+
{
1379+
generator.init(0xFF, pgpPrivKey);
1380+
fail("Generating signature of type 0xff MUST fail.");
1381+
}
1382+
catch (PGPException e)
1383+
{
1384+
// Expected
1385+
}
1386+
1387+
PGPV3SignatureGenerator generatorV3 = new PGPV3SignatureGenerator(sigBuilder);
1388+
try
1389+
{
1390+
generatorV3.init(0xFF, pgpPrivKey);
1391+
fail("Generating V3 signature of type 0xff MUST fail.");
1392+
}
1393+
catch (PGPException e)
1394+
{
1395+
// Expected
1396+
}
1397+
1398+
PGPContentVerifierBuilderProvider verifBuilder = new BcPGPContentVerifierBuilderProvider();
1399+
1400+
// signature of type 0xff (illegal)
1401+
byte[] hexSig = Hex.decode("889c04ff010a000605026655fdbe000a0910b3c272c907c7f7b2133604008dc801695e0905a21a03b832dfd576d66dc23a6ac8715128aaa5cee941b36660efd3c47618c5e880b2dc5e8a34638f10061ae6a9724a2306b66eeb4aec79b49ce4ec48f6de0b5119fc7911e9e2a7677bc4a1f6dd783ce15949457872246e0b415c6f8e3390da90597b059009dcc64723adbc45530a1db0ef70fcffbfc97af6b6");
1402+
ByteArrayInputStream bIn = new ByteArrayInputStream(hexSig);
1403+
BCPGInputStream pIn = new BCPGInputStream(bIn);
1404+
PGPSignature s = new PGPSignature(pIn);
1405+
try
1406+
{
1407+
s.init(verifBuilder, secretKey.getPublicKey());
1408+
fail("Verifying signature of type 0xff MUST fail.");
1409+
}
1410+
catch (PGPException e)
1411+
{
1412+
// expected
1413+
}
1414+
}
1415+
13701416
private PGPSignatureList readSignatures(String armored)
13711417
throws IOException
13721418
{

0 commit comments

Comments
 (0)