Migrated BAEL-7190 to a new module (#16518)
* BAEL-7190 implementation * Migrated BAEL-7190 to a new module * Revert "BAEL-7190 implementation" This reverts commit dd947aefda7b12a0eb06030b5d89f2ada77834a5.
This commit is contained in:
committed by
GitHub
parent
ba73008ee9
commit
bb0116b5d5
+32
@@ -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.
+45
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user