diff --git a/core/src/main/java/org/bouncycastle/util/io/pem/PemReader.java b/core/src/main/java/org/bouncycastle/util/io/pem/PemReader.java index 7156818784..2d681b9bee 100644 --- a/core/src/main/java/org/bouncycastle/util/io/pem/PemReader.java +++ b/core/src/main/java/org/bouncycastle/util/io/pem/PemReader.java @@ -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) @@ -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(); @@ -75,7 +75,7 @@ private PemObject loadObject(String type) continue; } - if (line.indexOf(endMarker) != -1) + if (line.indexOf(endMarker) == 0) { break; } diff --git a/core/src/test/java/org/bouncycastle/util/io/pem/test/AllTests.java b/core/src/test/java/org/bouncycastle/util/io/pem/test/AllTests.java index 0edf79f326..189d829b0f 100644 --- a/core/src/test/java/org/bouncycastle/util/io/pem/test/AllTests.java +++ b/core/src/test/java/org/bouncycastle/util/io/pem/test/AllTests.java @@ -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; @@ -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 { @@ -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 {