BAEL-7190 implementation (#16464)
This commit is contained in:
parent
9e74ea8ea1
commit
2735ad0e84
|
@ -23,6 +23,7 @@
|
|||
<module>jwt</module>
|
||||
<module>oauth2-framework-impl</module>
|
||||
<module>sql-injection-samples</module>
|
||||
<module>unrecoverablekeyexception</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
.idea
|
||||
target
|
||||
*.iml
|
|
@ -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>
|
|
@ -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");
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue