Skip to content

Commit

Permalink
#120: added null check for custom JDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
firaja committed Aug 20, 2023
1 parent 2b01e8c commit ef9a125
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/password4j/AlgorithmFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,16 @@ public static List<String> getAllPBKDF2Variants()
List<String> result = new ArrayList<>();
for (Provider provider : Security.getProviders())
{
for (Provider.Service service : provider.getServices())
// Some JDK implementation may return null instead of an empty array.
// see https://github.com/Password4j/password4j/issues/120
if (provider.getServices() != null)
{
if ("SecretKeyFactory".equals(service.getType()) && service.getAlgorithm().startsWith("PBKDF2"))
for (Provider.Service service : provider.getServices())
{
result.add(service.getAlgorithm());
if ("SecretKeyFactory".equals(service.getType()) && service.getAlgorithm().startsWith("PBKDF2"))
{
result.add(service.getAlgorithm());
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/com/password4j/PasswordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.security.SecureRandom;
import java.security.Security;
import java.util.Random;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -1221,6 +1222,28 @@ public void afterMigrationTests()
assertThrows(BadParametersException.class, () -> Password.check(new byte[0], new Hash(null, new byte[0], new byte[0], new byte[0])));
}

@Test
public void issue120()
{
// GIVEN
String name = "issue120FakeProvider";
Provider emptyProvider = new Provider(name, 1, "info")
{
@Override
public synchronized Set<Service> getServices()
{
return null;
}
};
Security.addProvider(emptyProvider);

// WHEN
Password.hash("hash");

// THEN
Security.removeProvider(name);
}

private static String printBytesToString(byte[] bytes)
{
StringBuilder byteString= new StringBuilder();
Expand Down
3 changes: 2 additions & 1 deletion src/test/com/password4j/SaltGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,13 @@ public void testStrongRandom2()
setSecurityProperty(old);

}

@SuppressWarnings("removal")
private String getSecurityProperty()
{
return AccessController.doPrivileged((PrivilegedAction<String>) () -> Security.getProperty("securerandom.strongAlgorithms"));
}

@SuppressWarnings("removal")
private void setSecurityProperty(String value)
{
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
Expand Down

0 comments on commit ef9a125

Please sign in to comment.