@@ -59,6 +59,7 @@ public SSLConnectionSocketFactory create() {
5959      SSLContext  sslContext  = SSLContext .getInstance ("TLS" ); // "TLS" means rely system properties 
6060      sslContext .init (getKeyManagers (), getTrustManagers (), null );
6161
62+ 
6263      return  new  SSLConnectionSocketFactory (sslContext , config .getTransportProtocolsList ().toArray (new  String [0 ]),
6364                                            cipherSuites , SSLConnectionSocketFactory .getDefaultHostnameVerifier ());
6465    } catch  (KeyManagementException  | CertificateException  | NoSuchAlgorithmException  | KeyStoreException 
@@ -70,27 +71,30 @@ public SSLConnectionSocketFactory create() {
7071  private  KeyManager [] getKeyManagers () throws  CertificateException , NoSuchAlgorithmException ,
7172    KeyStoreException , IOException , UnrecoverableKeyException  {
7273
73-     KeyStore  keystore  = loadKeystore (config .getKeystoreFile (), config .getKeystoreType ().name (),
74-                                      config .getKeystorePassword ());
75- 
7674    String  keyStorePassword  = config .getKeystorePassword ();
75+     KeyStore  keystore  = loadKeystore (config .getKeystoreFile (), config .getKeystoreType ().name (), keyStorePassword );
7776
7877    // we have to manually fall back to default keystore. SSLContext won't provide such a functionality. 
7978    if  (keystore  == null ) {
8079      String  keyStore  = System .getProperty ("javax.net.ssl.keyStore" );
8180      String  keyStoreType  = System .getProperty ("javax.net.ssl.keyStoreType" , KeyStore .getDefaultType ());
8281      keyStorePassword  = System .getProperty ("javax.net.ssl.keyStorePassword" , "" );
83- 
8482      keystore  = loadKeystore (keyStore , keyStoreType , keyStorePassword );
8583    }
8684
87-     String  keystoreAlgorithm  =
88-       ( Strings . isNullOrEmpty ( config . getKeystoreKeyAlgorithm ()))  ? KeyManagerFactory .getDefaultAlgorithm ()
85+     String  keystoreAlgorithm  = ( Strings . isNullOrEmpty ( config . getKeystoreKeyAlgorithm ())) 
86+          ? KeyManagerFactory .getDefaultAlgorithm ()
8987        : config .getKeystoreKeyAlgorithm ();
88+ 
9089    KeyManagerFactory  keyManagerFactory  = KeyManagerFactory .getInstance (keystoreAlgorithm );
91-     char [] passwordArr  = (keyStorePassword  == null ) ? null  : keyStorePassword .toCharArray ();
92-     keyManagerFactory .init (keystore , passwordArr );
93-     return  keyManagerFactory .getKeyManagers ();
90+     keyManagerFactory .init (
91+       keystore ,
92+       (keyStorePassword  == null ) ? null  : keyStorePassword .toCharArray ()
93+     );
94+ 
95+     return  (Strings .isNullOrEmpty (config .getKeystoreCertAliasName ()))
96+       ? keyManagerFactory .getKeyManagers ()
97+       : X509KeyManagerAliasWrapper .getKeyManagers (keyManagerFactory , config .getKeystoreCertAliasName ());
9498  }
9599
96100  private  TrustManager [] getTrustManagers ()
@@ -100,13 +104,17 @@ private TrustManager[] getTrustManagers()
100104      return  new  TrustManager [] { new  TrustAllTrustManager () };
101105    }
102106
103-     KeyStore  trustStore  = loadKeystore (config .getTrustStoreFile (), config .getTrustStoreType ().name (),
104-                                        config .getTrustStorePassword ());
107+     KeyStore  trustStore  = loadKeystore (
108+       config .getTrustStoreFile (),
109+       config .getTrustStoreType ().name (),
110+       config .getTrustStorePassword ()
111+     );
112+ 
105113    TrustManager [] trustManagers  = null ;
106114    if  (trustStore  != null ) {
107-       String  trustStoreAlgorithm  =
108-         ( Strings . isNullOrEmpty ( config . getTrustStoreKeyAlgorithm ()))  ? TrustManagerFactory .getDefaultAlgorithm ()
109-            : config .getTrustStoreKeyAlgorithm ();
115+       String  trustStoreAlgorithm  = ( Strings . isNullOrEmpty ( config . getTrustStoreKeyAlgorithm ())) 
116+         ? TrustManagerFactory .getDefaultAlgorithm ()
117+         : config .getTrustStoreKeyAlgorithm ();
110118      TrustManagerFactory  trustManagerFactory  = TrustManagerFactory .getInstance (trustStoreAlgorithm );
111119      trustManagerFactory .init (trustStore );
112120      trustManagers  = trustManagerFactory .getTrustManagers ();
@@ -117,13 +125,15 @@ private TrustManager[] getTrustManagers()
117125  private  static  KeyStore  loadKeystore (String  keystoreFile , String  type , String  password )
118126    throws  IOException , CertificateException , NoSuchAlgorithmException , KeyStoreException  {
119127
120-     KeyStore  keystore  = null ;
121-     if  (keystoreFile  != null ) {
122-       keystore  = KeyStore .getInstance (type );
123-       char [] passwordArr  = (password  == null ) ? null  : password .toCharArray ();
124-       try  (InputStream  is  = Files .newInputStream (Paths .get (keystoreFile ))) {
125-         keystore .load (is , passwordArr );
126-       }
128+     if  (keystoreFile  == null ) {
129+       return  null ;
130+     }
131+ 
132+     KeyStore  keystore  = KeyStore .getInstance (type );
133+     char [] passwordArr  = (password  == null ) ? null  : password .toCharArray ();
134+ 
135+     try  (InputStream  is  = Files .newInputStream (Paths .get (keystoreFile ))) {
136+       keystore .load (is , passwordArr );
127137    }
128138    return  keystore ;
129139  }
0 commit comments