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

Introduce system property for lax PEM parsing. #1771

Closed
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions core/src/main/java/org/bouncycastle/util/io/pem/PemReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.bouncycastle.util.encoders.Base64;

Expand All @@ -16,6 +18,8 @@ public class PemReader
{
private static final String BEGIN = "-----BEGIN ";
private static final String END = "-----END ";
public static final String LAX_PARSING_SYSTEM_PROPERTY_NAME = "org.bouncycastle.pemreader.lax";
private static final Logger LOG = Logger.getLogger(PemReader.class.getName());

public PemReader(Reader reader)
{
Expand Down Expand Up @@ -75,6 +79,16 @@ private PemObject loadObject(String type)
continue;
}

if (System.getProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME, "false").equalsIgnoreCase("true"))
{
String trimmedLine = line.trim();
if (!trimmedLine.equals(line) && LOG.isLoggable(Level.WARNING))
{
LOG.log(Level.WARNING, "PEM object contains whitespaces on -----END line", new Exception("trace"));
}
line = trimmedLine;
}

if (line.indexOf(endMarker) == 0)
{
break;
Expand Down
30 changes: 30 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 @@ -18,6 +18,8 @@
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.util.io.pem.PemWriter;

import static org.bouncycastle.util.io.pem.PemReader.LAX_PARSING_SYSTEM_PROPERTY_NAME;

public class AllTests
extends TestCase
{
Expand Down Expand Up @@ -122,6 +124,34 @@ public void testRegularBlobEndFault()
}
}

public void testRegularBlobEndLaxParsing()
throws IOException
{
String original = System.setProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME, "true");
PemReader rd = new PemReader(new StringReader(blob4));

PemObject obj;
try
{
obj = rd.readPemObject();
}
finally
{
if (original != null)
{
System.setProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME, original);
}
else
{
System.clearProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME);
}
}

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

}

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