Skip to content

Commit 7517595

Browse files
committed
Respect the KMF algorithm property
1 parent dd3640d commit 7517595

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"description": "This change makes the `FileStoreTlsKeyManagersProvider` and `SystemPropertyTlsKeyManagersProvider` respect the `ssl.KeyManagerFactory.algorithm` when instantiating the `KeyManagerFactory` rather than always using the hardcoded value of `SunX509`."
5+
}

http-client-spi/src/main/java/software/amazon/awssdk/http/FileStoreTlsKeyManagersProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.nio.file.Path;
1919
import javax.net.ssl.KeyManager;
20+
import javax.net.ssl.KeyManagerFactory;
2021
import software.amazon.awssdk.annotations.SdkPublicApi;
2122
import software.amazon.awssdk.internal.http.AbstractFileStoreTlsKeyManagersProvider;
2223
import software.amazon.awssdk.utils.Logger;
@@ -25,6 +26,9 @@
2526
/**
2627
* Implementation of {@link FileStoreTlsKeyManagersProvider} that loads a the
2728
* key store from a file.
29+
* <p>
30+
* This uses {@link KeyManagerFactory#getDefaultAlgorithm()} to determine the
31+
* {@code KeyManagerFactory} algorithm to use.
2832
*/
2933
@SdkPublicApi
3034
public final class FileStoreTlsKeyManagersProvider extends AbstractFileStoreTlsKeyManagersProvider {

http-client-spi/src/main/java/software/amazon/awssdk/http/SystemPropertyTlsKeyManagersProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.security.KeyStore;
2525
import java.util.Optional;
2626
import javax.net.ssl.KeyManager;
27+
import javax.net.ssl.KeyManagerFactory;
2728
import software.amazon.awssdk.annotations.SdkPublicApi;
2829
import software.amazon.awssdk.internal.http.AbstractFileStoreTlsKeyManagersProvider;
2930
import software.amazon.awssdk.utils.Logger;
@@ -37,6 +38,9 @@
3738
* {@code javax.net.ssl.keyStorePassword}, and
3839
* {@code javax.net.ssl.keyStoreType} properties defined by the
3940
* <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html">JSSE</a>.
41+
* <p>
42+
* This uses {@link KeyManagerFactory#getDefaultAlgorithm()} to determine the
43+
* {@code KeyManagerFactory} algorithm to use.
4044
*/
4145
@SdkPublicApi
4246
public final class SystemPropertyTlsKeyManagersProvider extends AbstractFileStoreTlsKeyManagersProvider {

http-client-spi/src/main/java/software/amazon/awssdk/internal/http/AbstractFileStoreTlsKeyManagersProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
3232
/**
3333
* Abstract {@link TlsKeyManagersProvider} that loads the key store from a
3434
* a given file path.
35+
* <p>
36+
* This uses {@link KeyManagerFactory#getDefaultAlgorithm()} to determine the
37+
* {@code KeyManagerFactory} algorithm to use.
3538
*/
3639
@SdkInternalApi
3740
public abstract class AbstractFileStoreTlsKeyManagersProvider implements TlsKeyManagersProvider {
3841

3942
protected final KeyManager[] createKeyManagers(Path storePath, String storeType, char[] password)
4043
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, UnrecoverableKeyException {
4144
KeyStore ks = createKeyStore(storePath, storeType, password);
42-
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
45+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
4346
kmf.init(ks, password);
4447
return kmf.getKeyManagers();
4548
}

http-client-spi/src/test/java/software/amazon/awssdk/http/FileStoreTlsKeyManagersProviderTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import java.io.IOException;
2020
import java.nio.file.Paths;
21+
import java.security.Security;
2122
import org.junit.AfterClass;
2223
import org.junit.BeforeClass;
2324
import org.junit.Test;
@@ -77,4 +78,26 @@ public void passwordIncorrect_returnsNull() {
7778
FileStoreTlsKeyManagersProvider provider = FileStoreTlsKeyManagersProvider.create(clientKeyStore, CLIENT_STORE_TYPE, "not correct password");
7879
assertThat(provider.keyManagers()).isNull();
7980
}
81+
82+
@Test
83+
public void customKmfAlgorithmSetInProperty_usesAlgorithm() {
84+
FileStoreTlsKeyManagersProvider beforePropSetProvider = FileStoreTlsKeyManagersProvider.create(clientKeyStore,
85+
CLIENT_STORE_TYPE, STORE_PASSWORD);
86+
87+
assertThat(beforePropSetProvider.keyManagers()).isNotNull();
88+
89+
String property = "ssl.KeyManagerFactory.algorithm";
90+
String previousValue = Security.getProperty(property);
91+
Security.setProperty(property, "some-bogus-value");
92+
try {
93+
FileStoreTlsKeyManagersProvider afterPropSetProvider = FileStoreTlsKeyManagersProvider.create(
94+
clientKeyStore, CLIENT_STORE_TYPE, STORE_PASSWORD);
95+
// This would otherwise be non-null if using the right algorithm,
96+
// i.e. not setting the algorithm property will cause the assertion
97+
// to fail
98+
assertThat(afterPropSetProvider.keyManagers()).isNull();
99+
} finally {
100+
Security.setProperty(property, previousValue);
101+
}
102+
}
80103
}

http-client-spi/src/test/java/software/amazon/awssdk/http/SystemPropertyTlsKeyManagersProviderTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE_TYPE;
2222
import java.io.IOException;
2323
import java.nio.file.Paths;
24+
import java.security.Security;
2425
import org.junit.After;
2526
import org.junit.AfterClass;
2627
import org.junit.BeforeClass;
@@ -86,4 +87,26 @@ public void passwordIncorrect_returnsNull() {
8687

8788
assertThat(PROVIDER.keyManagers()).isNull();
8889
}
90+
91+
@Test
92+
public void customKmfAlgorithmSetInProperty_usesAlgorithm() {
93+
System.setProperty(SSL_KEY_STORE.property(), clientKeyStore.toAbsolutePath().toString());
94+
System.setProperty(SSL_KEY_STORE_TYPE.property(), CLIENT_STORE_TYPE);
95+
System.setProperty(SSL_KEY_STORE_PASSWORD.property(), STORE_PASSWORD);
96+
97+
assertThat(PROVIDER.keyManagers()).isNotNull();
98+
99+
String property = "ssl.KeyManagerFactory.algorithm";
100+
String previousValue = Security.getProperty(property);
101+
Security.setProperty(property, "some-bogus-value");
102+
103+
try {
104+
// This would otherwise be non-null if using the right algorithm,
105+
// i.e. not setting the algorithm property will cause the assertion
106+
// to fail
107+
assertThat(PROVIDER.keyManagers()).isNull();
108+
} finally {
109+
Security.setProperty(property, previousValue);
110+
}
111+
}
89112
}

0 commit comments

Comments
 (0)