Skip to content

Commit 25a49a4

Browse files
committed
added check for corrupted stream and escaping NullPointer - relates to github #1888
1 parent 3d0c6a4 commit 25a49a4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pg/src/main/java/org/bouncycastle/gpg/SExpression.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ private static SExpression parseExpression(InputStream src, SExpression expr, By
104104

105105
if (c == ':')
106106
{
107+
if (expr == null)
108+
{
109+
throw new IOException("invalid input stream at ':'");
110+
}
107111
try
108112
{
109113
int len = Integer.parseInt(Strings.fromByteArray(accumulator.toByteArray()));

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package org.bouncycastle.openpgp.test;
22

33
import java.io.ByteArrayInputStream;
4+
import java.io.IOException;
45
import java.security.Security;
56

67
import org.bouncycastle.gpg.SExprParser;
8+
import org.bouncycastle.gpg.SExpression;
79
import org.bouncycastle.jce.provider.BouncyCastleProvider;
810
import org.bouncycastle.openpgp.PGPPrivateKey;
911
import org.bouncycastle.openpgp.PGPSecretKey;
1012
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
1113
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
1214
import org.bouncycastle.openpgp.operator.jcajce.JcePBEProtectionRemoverFactory;
15+
import org.bouncycastle.util.Strings;
1316
import org.bouncycastle.util.encoders.Base64;
1417
import org.bouncycastle.util.test.SimpleTest;
1518

@@ -139,9 +142,25 @@ public String getName()
139142
return "SExprTest";
140143
}
141144

145+
private void corruptStreamTest()
146+
throws Exception
147+
{
148+
try
149+
{
150+
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("2:3abc")), 2);
151+
fail("no exception");
152+
}
153+
catch (IOException e)
154+
{
155+
isEquals("invalid input stream at ':'", e.getMessage());
156+
}
157+
}
158+
142159
public void performTest()
143160
throws Exception
144161
{
162+
corruptStreamTest();
163+
145164
SExprParser parser = new SExprParser(new JcaPGPDigestCalculatorProviderBuilder().build());
146165

147166
PGPSecretKey k1 = parser.parseSecretKey(new ByteArrayInputStream(key1), new JcePBEProtectionRemoverFactory("fred".toCharArray()), new JcaKeyFingerprintCalculator());

0 commit comments

Comments
 (0)