Skip to content

Commit

Permalink
[FIX JENKINS-41978] - Fix NPE when an invalid PEM is being decoded.
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarolobato committed Feb 13, 2017
1 parent 0307dba commit 3811e98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/jenkins/bouncycastle/api/PEMEncodable.java
Expand Up @@ -152,6 +152,10 @@ public static PEMEncodable decode(@Nonnull String pem, @Nullable final char[] pa

Object object = parser.readObject();

if (object == null) {
throw new IOException("Could not parse PEM, only key pairs, private keys, public keys and certificates are supported");
}

JcaPEMKeyConverter kConv = new JcaPEMKeyConverter().setProvider("BC");

// handle supported PEM formats.
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/jenkins/bouncycastle/EncodignDecodingTest.java
Expand Up @@ -29,6 +29,7 @@
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.PublicKey;
Expand Down Expand Up @@ -237,4 +238,9 @@ public void testReadKeyPairFromPCKS8PEM() throws Exception {
assertNotNull(pemEnc.toPublicKey());
}

}
@Test(expected = IOException.class)
@Issue(value = "JENKINS-41978")
public void testInvalidPEM() throws Exception {
PEMEncodable.decode(FileUtils.readFileToString(getResourceFile("invalid.pem")));
}
}
1 change: 1 addition & 0 deletions src/test/resources/invalid.pem
@@ -0,0 +1 @@
---INVALID PEM---

0 comments on commit 3811e98

Please sign in to comment.