BAEL-7190 implementation (#16464)

This commit is contained in:
Mikhail Polivakha 2024-04-21 23:00:04 +03:00 committed by GitHub
parent 9e74ea8ea1
commit 2735ad0e84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 100 additions and 0 deletions

View File

@ -23,6 +23,7 @@
<module>jwt</module>
<module>oauth2-framework-impl</module>
<module>sql-injection-samples</module>
<module>unrecoverablekeyexception</module>
</modules>
</project>

View File

@ -0,0 +1,3 @@
.idea
target
*.iml

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.unrecoverablekeyexception</groupId>
<artifactId>unrecoverablekeyexception</artifactId>
<name>unrecoverablekeyexception</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>security-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
</project>

View File

@ -0,0 +1,32 @@
package com.baeldung.unrecoverablekeyexception;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.X509ExtendedKeyManager;
public class KeyManagerInitializer {
public static X509ExtendedKeyManager initializeKeyManager(String privateKeyPassword, String keystoreLocation)
throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException, URISyntaxException {
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore instance = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream resourceAsStream = Files.newInputStream(Paths.get(ClassLoader.getSystemResource(keystoreLocation).toURI()));
instance.load(resourceAsStream, "admin123".toCharArray());
kmf.init(instance, privateKeyPassword.toCharArray());
return (X509ExtendedKeyManager) kmf.getKeyManagers()[0];
}
public static X509ExtendedKeyManager initializeKeyManager(String privateKeyPassword)
throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException, URISyntaxException {
return initializeKeyManager(privateKeyPassword, "single_entry_keystore.jks");
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.unrecoverablekeyexception;
import java.security.UnrecoverableKeyException;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.Test;
public class KeyManagerInitializerUnitTest {
@Test
public void givenPasswordIsCorrect_whenInitializingTheKeyManager_thenNoExceptionIsThrown() {
// Given.
String privateKeyPassword = "privateKeyPassword";
// When.
ThrowingCallable initializeKeyManager = () -> KeyManagerInitializer.initializeKeyManager(privateKeyPassword);
// Then.
Assertions.assertThatCode(initializeKeyManager).doesNotThrowAnyException();;
}
@Test
public void givenPasswordIsWrong_whenInitializingTheKeyManager_thenUnrecoverableKeyExceptionExceptionIsThrown() {
// Given.
String privateKeyPassword = "wrongPassword";
// When.
ThrowingCallable initializeKeyManager = () -> KeyManagerInitializer.initializeKeyManager(privateKeyPassword);
// Then.
Assertions.assertThatThrownBy(initializeKeyManager).isInstanceOf(UnrecoverableKeyException.class);
}
@Test
public void givenMultipleKeysWithDifferentPasswordsInKeystore_whenInitializingTheKeyManager_thenUnrecoverableKeyExceptionIsThrown() {
// Given.
String firstPrivateKeyPassword = "abc123";
// When.
ThrowingCallable initializeKeyManager = () -> KeyManagerInitializer.initializeKeyManager(firstPrivateKeyPassword, "multi_entry_keystore.jks");
// Then.
Assertions.assertThatThrownBy(initializeKeyManager).isInstanceOf(UnrecoverableKeyException.class);
}
}