public static SSLContext createSslContext(boolean allowInsecureConnection, Certificate[] trustCertficates, Certificate[] certificates, PrivateKey privateKey) throws GeneralSecurityException { KeyStoreHolder ksh = new KeyStoreHolder(); TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; trustManagers = setupTrustCerts(ksh, allowInsecureConnection, trustCertficates); keyManagers = setupKeyManager(ksh, privateKey, certificates); SSLContext sslCtx = SSLContext.getInstance("TLS"); sslCtx.init(keyManagers, trustManagers, new SecureRandom()); sslCtx.getDefaultSSLParameters(); return sslCtx; }
private static KeyManager[] setupKeyManager(KeyStoreHolder ksh, PrivateKey privateKey, Certificate[] certificates) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { KeyManager[] keyManagers = null; if (certificates != null && privateKey != null) { ksh.setPrivateKey("private", privateKey, certificates); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ksh.getKeyStore(), "".toCharArray()); keyManagers = kmf.getKeyManagers(); } return keyManagers; }
private static TrustManager[] setupTrustCerts(KeyStoreHolder ksh, boolean allowInsecureConnection, Certificate[] trustCertficates) throws NoSuchAlgorithmException, KeyStoreException { TrustManager[] trustManagers; if (allowInsecureConnection) { trustManagers = InsecureTrustManagerFactory.INSTANCE.getTrustManagers(); } else { TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); if (trustCertficates == null || trustCertficates.length == 0) { tmf.init((KeyStore) null); } else { for (int i = 0; i < trustCertficates.length; i++) { ksh.setCertificate("trust" + i, trustCertficates[i]); } tmf.init(ksh.getKeyStore()); } trustManagers = tmf.getTrustManagers(); } return trustManagers; }
private static KeyManager[] setupKeyManager(KeyStoreHolder ksh, PrivateKey privateKey, Certificate[] certificates) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { KeyManager[] keyManagers = null; if (certificates != null && privateKey != null) { ksh.setPrivateKey("private", privateKey, certificates); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ksh.getKeyStore(), "".toCharArray()); keyManagers = kmf.getKeyManagers(); } return keyManagers; }
private static TrustManager[] setupTrustCerts(KeyStoreHolder ksh, boolean allowInsecureConnection, Certificate[] trustCertficates) throws NoSuchAlgorithmException, KeyStoreException { TrustManager[] trustManagers; if (allowInsecureConnection) { trustManagers = InsecureTrustManagerFactory.INSTANCE.getTrustManagers(); } else { TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); if (trustCertficates == null || trustCertficates.length == 0) { tmf.init((KeyStore) null); } else { for (int i = 0; i < trustCertficates.length; i++) { ksh.setCertificate("trust" + i, trustCertficates[i]); } tmf.init(ksh.getKeyStore()); } trustManagers = tmf.getTrustManagers(); } return trustManagers; }
public static SSLContext createSslContext(boolean allowInsecureConnection, Certificate[] trustCertficates, Certificate[] certificates, PrivateKey privateKey) throws GeneralSecurityException { KeyStoreHolder ksh = new KeyStoreHolder(); TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; trustManagers = setupTrustCerts(ksh, allowInsecureConnection, trustCertficates); keyManagers = setupKeyManager(ksh, privateKey, certificates); SSLContext sslCtx = SSLContext.getInstance("TLS"); sslCtx.init(keyManagers, trustManagers, new SecureRandom()); sslCtx.getDefaultSSLParameters(); return sslCtx; }