Skip to content

Commit 748e670

Browse files
committed
Merge branch 'pemparser_fix' of https://github.com/TaZbon/bc-java into TaZbon-pemparser_fix
2 parents c0d6922 + d53e39e commit 748e670

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

core/src/main/java/org/bouncycastle/util/io/pem/PemReader.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.Reader;
66
import java.util.ArrayList;
77
import java.util.List;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
810

911
import org.bouncycastle.util.encoders.Base64;
1012

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

2024
public PemReader(Reader reader)
2125
{
@@ -75,6 +79,16 @@ private PemObject loadObject(String type)
7579
continue;
7680
}
7781

82+
if (System.getProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME, "false").equalsIgnoreCase("true"))
83+
{
84+
String trimmedLine = line.trim();
85+
if (!trimmedLine.equals(line) && LOG.isLoggable(Level.WARNING))
86+
{
87+
LOG.log(Level.WARNING, "PEM object contains whitespaces on -----END line", new Exception("trace"));
88+
}
89+
line = trimmedLine;
90+
}
91+
7892
if (line.indexOf(endMarker) == 0)
7993
{
8094
break;

core/src/test/java/org/bouncycastle/util/io/pem/test/AllTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.bouncycastle.util.io.pem.PemReader;
1919
import org.bouncycastle.util.io.pem.PemWriter;
2020

21+
import static org.bouncycastle.util.io.pem.PemReader.LAX_PARSING_SYSTEM_PROPERTY_NAME;
22+
2123
public class AllTests
2224
extends TestCase
2325
{
@@ -122,6 +124,34 @@ public void testRegularBlobEndFault()
122124
}
123125
}
124126

127+
public void testRegularBlobEndLaxParsing()
128+
throws IOException
129+
{
130+
String original = System.setProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME, "true");
131+
PemReader rd = new PemReader(new StringReader(blob4));
132+
133+
PemObject obj;
134+
try
135+
{
136+
obj = rd.readPemObject();
137+
}
138+
finally
139+
{
140+
if (original != null)
141+
{
142+
System.setProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME, original);
143+
}
144+
else
145+
{
146+
System.clearProperty(LAX_PARSING_SYSTEM_PROPERTY_NAME);
147+
}
148+
}
149+
150+
assertEquals("BLOB", obj.getType());
151+
assertTrue(Arrays.areEqual(new byte[64], obj.getContent()));
152+
153+
}
154+
125155
private void lengthTest(String type, List headers, byte[] data)
126156
throws IOException
127157
{

0 commit comments

Comments
 (0)