public SSLContext createSSLContextFromKeystores ( boolean isServer ) { String protocol = sslConfig . getSSLProtocol ( ) ; try { KeyManager [ ] keyManagers = null ; if ( sslConfig . getKeyStore ( ) != null ) { KeyStore ks = getKeyStore ( sslConfig . getKeyStore ( ) , sslConfig . getKeyStorePass ( ) ) ; // Set up key manager factory to use our key store kmf = KeyManagerFactory . getInstance ( KeyManagerFactory . getDefaultAlgorithm ( ) ) ; if ( ks != null ) { kmf . init ( ks , sslConfig . getCertPass ( ) != null ? sslConfig . getCertPass ( ) . toCharArray ( ) : sslConfig . getKeyStorePass ( ) . toCharArray ( ) ) ; keyManagers = kmf . getKeyManagers ( ) ; } } TrustManager [ ] trustManagers = null ; if ( sslConfig . getTrustStore ( ) != null ) { KeyStore tks = getKeyStore ( sslConfig . getTrustStore ( ) , sslConfig . getTrustStorePass ( ) ) ; tmf = TrustManagerFactory . getInstance ( TrustManagerFactory . getDefaultAlgorithm ( ) ) ; tmf . init ( tks ) ; trustManagers = tmf . getTrustManagers ( ) ; } sslContext = SSLContext . getInstance ( protocol ) ; sslContext . init ( keyManagers , trustManagers , null ) ; int sessionTimeout = sslConfig . getSessionTimeOut ( ) ; if ( isServer ) { if ( sessionTimeout > 0 ) { sslContext . getServerSessionContext ( ) . setSessionTimeout ( sessionTimeout ) ; } } else { if ( sessionTimeout > 0 ) { sslContext . getClientSessionContext ( ) . setSessionTimeout ( sessionTimeout ) ; } } return sslContext ; } catch ( UnrecoverableKeyException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException | IOException e ) { throw new IllegalArgumentException ( "Failed to initialize the SSLContext" , e ) ; } }
This is uset to create the sslContext from keystores .