Skip to content

Commit 5e126a4

Browse files
authored
Merge pull request #406 from arthurscchan/fix-empty-content
Fix: Fix possible Null Pointer Exception
2 parents 29266e3 + 982b88f commit 5e126a4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

sigstore-java/src/main/java/dev/sigstore/encryption/Keys.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public static PublicKey parsePublicKey(byte[] keyBytes)
7474
throw new InvalidKeySpecException("Invalid key, could not parse PEM section");
7575
}
7676
// special handling for PKCS1 (rsa) public key
77-
if ((section == null) || (section.getContent() == null)) {
77+
// TODO: The length checking is not necessary after https://github.com/bcgit/bc-java/issues/1370
78+
// has been merged. Remove it when bc-java is updated with the merge.
79+
if ((section == null) || (section.getContent() == null) || (section.getContent().length == 0)) {
7880
throw new InvalidKeySpecException("Invalid key, empty PEM section");
7981
}
8082
if (section.getType().equals("RSA PUBLIC KEY")) {

sigstore-java/src/test/java/dev/sigstore/encryption/KeysTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.io.Resources;
2121
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
2223
import java.security.NoSuchAlgorithmException;
2324
import java.security.NoSuchProviderException;
2425
import java.security.PublicKey;
@@ -208,6 +209,16 @@ void parsePkixPublicKey_ecdsa() throws NoSuchAlgorithmException, InvalidKeySpecE
208209
Assertions.assertNotNull(Keys.parsePkixPublicKey(Base64.decode(base64Key), "EC"));
209210
}
210211

212+
@Test
213+
void parsePublicKey_failOnNullSection()
214+
throws IOException, NoSuchAlgorithmException, NoSuchProviderException {
215+
// This unit test is used to test the fix for a bug discovered by oss-fuzz
216+
// The bug happens when a malformed byte array is passed to the method
217+
// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=57247
218+
byte[] byteArray = "-----BEGIN A-----\nBBBBB-----END A".getBytes(StandardCharsets.UTF_8);
219+
Assertions.assertThrows(InvalidKeySpecException.class, () -> Keys.parsePublicKey(byteArray));
220+
}
221+
211222
@Test
212223
void testGetJavaVersion() {
213224
assertEquals(1, Keys.getJavaVersion("1.6.0_23"));

0 commit comments

Comments
 (0)