From ab9f0a0fda23e9c254cca9b18f5c19090b6c63ef Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 4 Jan 2018 03:32:18 +0100 Subject: [PATCH] Remove direct usage of JKS and SunX509 Motivation: When using netty on android or with for example a IBM JVM it may not be able to build a SslContext as we hardcoded the use of JKS and SunX509 (which both may not be present). Modifications: - Use the default algorithm / type which can be override via a System property - Remove System property check as its redundant with KeyManagerFactory.getDefaultAlgorithm() Result: More portable code. Fixes [#7546]. --- .../main/java/io/netty/handler/ssl/SslContext.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/SslContext.java b/handler/src/main/java/io/netty/handler/ssl/SslContext.java index 542a5475e3..ef5c4bfdca 100644 --- a/handler/src/main/java/io/netty/handler/ssl/SslContext.java +++ b/handler/src/main/java/io/netty/handler/ssl/SslContext.java @@ -998,7 +998,7 @@ public abstract class SslContext { static KeyStore buildKeyStore(X509Certificate[] certChain, PrivateKey key, char[] keyPasswordChars) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { - KeyStore ks = KeyStore.getInstance("JKS"); + KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null, null); ks.setKeyEntry("key", key, keyPasswordChars, certChain); return ks; @@ -1040,7 +1040,7 @@ public abstract class SslContext { return KeyFactory.getInstance("DSA").generatePrivate(encodedKeySpec); } catch (InvalidKeySpecException ignore2) { try { - return KeyFactory.getInstance("EC").generatePrivate(encodedKeySpec); + return KeyFactory.getInstance("EC").generatePrivate(encodedKeySpec); } catch (InvalidKeySpecException e) { throw new InvalidKeySpecException("Neither RSA, DSA nor EC worked", e); } @@ -1107,7 +1107,7 @@ public abstract class SslContext { static TrustManagerFactory buildTrustManagerFactory( X509Certificate[] certCollection, TrustManagerFactory trustManagerFactory) throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException { - KeyStore ks = KeyStore.getInstance("JKS"); + final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null, null); int i = 1; @@ -1146,11 +1146,7 @@ public abstract class SslContext { KeyManagerFactory kmf) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { - String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); - if (algorithm == null) { - algorithm = "SunX509"; - } - return buildKeyManagerFactory(certChain, algorithm, key, keyPassword, kmf); + return buildKeyManagerFactory(certChain, KeyManagerFactory.getDefaultAlgorithm(), key, keyPassword, kmf); } static KeyManagerFactory buildKeyManagerFactory(X509Certificate[] certChainFile,