Skip to content

Commit

Permalink
allowed for trailing whitespace in PEM seperators - relates to github b…
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Dec 8, 2023
1 parent 96ea1c4 commit 5ca7b06
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PemObject readPemObject()

if (line != null)
{
line = line.substring(BEGIN.length());
line = line.substring(BEGIN.length()).trim();
int index = line.indexOf('-');

if (index > 0 && line.endsWith("-----") && (line.length() - index) == 5)
Expand All @@ -58,7 +58,7 @@ private PemObject loadObject(String type)
throws IOException
{
String line;
String endMarker = END + type;
String endMarker = END + type + "-----";
StringBuffer buf = new StringBuffer();
List headers = new ArrayList();

Expand All @@ -75,7 +75,7 @@ private PemObject loadObject(String type)
continue;
}

if (line.indexOf(endMarker) != -1)
if (line.indexOf(endMarker) == 0)
{
break;
}
Expand Down
68 changes: 68 additions & 0 deletions core/src/test/java/org/bouncycastle/util/io/pem/test/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.bouncycastle.test.PrintTestResult;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.io.pem.PemHeader;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
Expand All @@ -20,6 +21,26 @@
public class AllTests
extends TestCase
{
private static final String blob1 =
"-----BEGIN BLOB-----\r\n"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\r\n"
+ "-----END BLOB-----\r\n";

private static final String blob2 =
"-----BEGIN BLOB----- \r\n"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\r\n"
+ "-----END BLOB----- \r\n";

private static final String blob3 =
" -----BEGIN BLOB-----\r\n"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\r\n"
+ "-----END BLOB-----\r\n";

private static final String blob4 =
"-----BEGIN BLOB-----\r\n"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\r\n"
+ " -----END BLOB-----\r\n";

public void testPemLength()
throws IOException
{
Expand Down Expand Up @@ -54,6 +75,53 @@ public void testMalformed()
assertNull(rd.readPemObject());
}

public void testRegularBlob()
throws IOException
{
PemReader rd = new PemReader(new StringReader(blob1));

PemObject obj = rd.readPemObject();

assertEquals("BLOB", obj.getType());
assertTrue(Arrays.areEqual(new byte[64], obj.getContent()));
}

public void testRegularBlobTrailing()
throws IOException
{
PemReader rd = new PemReader(new StringReader(blob2));

PemObject obj = rd.readPemObject();

assertEquals("BLOB", obj.getType());
assertTrue(Arrays.areEqual(new byte[64], obj.getContent()));
}

public void testRegularBlobBeginFault()
throws IOException
{
PemReader rd = new PemReader(new StringReader(blob3));

PemObject obj = rd.readPemObject();

assertNull(rd.readPemObject());
}

public void testRegularBlobEndFault()
throws IOException
{
PemReader rd = new PemReader(new StringReader(blob4));

try
{
PemObject obj = rd.readPemObject();
}
catch (IOException e)
{
assertEquals("-----END BLOB----- not found", e.getMessage());
}
}

private void lengthTest(String type, List headers, byte[] data)
throws IOException
{
Expand Down

0 comments on commit 5ca7b06

Please sign in to comment.