List of usage examples for javax.net.ssl TrustManagerFactory getInstance
public static final TrustManagerFactory getInstance(String algorithm) throws NoSuchAlgorithmException
TrustManagerFactory
object that acts as a factory for trust managers. From source file:Main.java
private static TrustManager[] prepareTrustManager(InputStream... certificates) { if (certificates != null && certificates.length > 0) { try {/*www . j av a 2s .co m*/ CertificateFactory e = CertificateFactory.getInstance("X.509"); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load((KeyStore.LoadStoreParameter) null); int index = 0; InputStream[] trustManagerFactory = certificates; int trustManagers = certificates.length; for (int i$ = 0; i$ < trustManagers; ++i$) { InputStream certificate = trustManagerFactory[i$]; String certificateAlias = Integer.toString(index++); keyStore.setCertificateEntry(certificateAlias, e.generateCertificate(certificate)); try { if (certificate != null) { certificate.close(); } } catch (IOException var10) { ; } } trustManagerFactory = null; TrustManagerFactory var15 = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); var15.init(keyStore); TrustManager[] var16 = var15.getTrustManagers(); return var16; } catch (NoSuchAlgorithmException var11) { var11.printStackTrace(); } catch (CertificateException var12) { var12.printStackTrace(); } catch (KeyStoreException var13) { var13.printStackTrace(); } catch (Exception var14) { var14.printStackTrace(); } return null; } else { return null; } }
From source file:android.apn.androidpn.server.xmpp.ssl.SSLTrustManagerFactory.java
public static TrustManager[] getTrustManagers(String storeType, String truststore, String trustpass) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException { TrustManager[] trustManagers; if (truststore == null) { trustManagers = null;/* w w w. j a v a 2 s. c om*/ } else { TrustManagerFactory trustFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); if (trustpass == null) { trustpass = ""; } KeyStore keyStore = KeyStore.getInstance(storeType); keyStore.load(new FileInputStream(truststore), trustpass.toCharArray()); trustFactory.init(keyStore); trustManagers = trustFactory.getTrustManagers(); } return trustManagers; }
From source file:Main.java
public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException { // Load CAs from an InputStream // (could be from a resource or ByteArrayInputStream or ...) CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream caInput = new BufferedInputStream(stream); Certificate ca;/*ww w .ja va 2s.c o m*/ try { ca = cf.generateCertificate(caInput); System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); } finally { try { caInput.close(); } catch (IOException e) { e.printStackTrace(); } } // Create a KeyStore containing our trusted CAs String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca); // Create a TrustManager that trusts the CAs in our KeyStore String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); // Create an SSLContext that uses our TrustManager SSLContext context = SSLContext.getInstance("TLS"); context.init(null, tmf.getTrustManagers(), null); return context.getSocketFactory(); }
From source file:Main.java
/** * Generate a SSLSocketFactory wich checks the certificate given * @param context Context to use/* www . j av a 2 s . co m*/ * @param rResource int with url of the resource to read the certificate * @parma password String to use with certificate * @return SSLSocketFactory generated to validate this certificate */ public static SSLSocketFactory newSslSocketFactory(Context context, int rResource, String password) throws CertificateException, NoSuchProviderException, KeyStoreException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { // Get an instance of the Bouncy Castle KeyStore format KeyStore trusted = KeyStore.getInstance("BKS"); // Get the raw resource, which contains the keystore with // your trusted certificates (root and any intermediate certs) InputStream is = context.getApplicationContext().getResources().openRawResource(rResource); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC"); X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(is); String alias = "alias";//cert.getSubjectX500Principal().getName(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null); trustStore.setCertificateEntry(alias, cert); KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509"); kmf.init(trustStore, null); KeyManager[] keyManagers = kmf.getKeyManagers(); TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); tmf.init(trustStore); TrustManager[] trustManagers = tmf.getTrustManagers(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyManagers, trustManagers, null); return sslContext.getSocketFactory(); }
From source file:com.microsoft.tfs.core.config.httpclient.internal.SelfSignedX509TrustManager.java
/** * Creates a trust manager capable of accepting self-signed certificates. * * @param keyStore/*from ww w .jav a 2 s . c om*/ * The {@link KeyStore} to use for user-specified keys (or * <code>null</code>) * @throws NoSuchAlgorithmException * @throws KeyStoreException */ public SelfSignedX509TrustManager(final KeyStore keyStore) throws NoSuchAlgorithmException, KeyStoreException { final TrustManagerFactory factory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); factory.init(keyStore); final TrustManager[] trustManagers = factory.getTrustManagers(); if (trustManagers.length == 0) { throw new NoSuchAlgorithmException("No trust manager found"); //$NON-NLS-1$ } if (!(trustManagers[0] instanceof X509TrustManager)) { throw new NoSuchAlgorithmException("No X509 trust manager found"); //$NON-NLS-1$ } standardTrustManager = (X509TrustManager) trustManagers[0]; }
From source file:info.guardianproject.cacert.CustomTrust.java
public CustomTrust(Context context, int rawResource, String password) throws IOException, KeyStoreException, KeyManagementException, NoSuchAlgorithmException, CertificateException { // Setup the SSL context to use the truststore ssl_ctx = SSLContext.getInstance("TLS"); // Setup truststore KeyStore ksCACert = KeyStore.getInstance("BKS"); TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); InputStream trustStoreStream = context.getResources().openRawResource(rawResource); ksCACert.load(trustStoreStream, password.toCharArray()); //init factory with custom cacert trustManagerFactory.init(ksCACert);/*from w w w .j a v a 2 s . c o m*/ Log.d("SSL", "CACerts " + ksCACert.size()); Log.d("SSL", "trustManagerFactory " + trustManagerFactory.getTrustManagers().length); // Setup client keystore /* KeyStore keyStore = KeyStore.getInstance("BKS"); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); InputStream keyStoreStream = context.getResources().openRawResource(R.raw.clientkeystore); keyStore.load(keyStoreStream, "testtest".toCharArray()); keyManagerFactory.init(keyStore, "testtest".toCharArray()); Log.d("SSL", "Key " + keyStore.size()); Log.d("SSL", "keyManagerFactory " + keyManagerFactory.getKeyManagers().length); */ //nothing implemented yet SecureRandom secRand = SecureRandom.getInstance(RANDOM_ALGORITHM); ssl_ctx.init(null, trustManagerFactory.getTrustManagers(), secRand); socketFactory = (SSLSocketFactory) ssl_ctx.getSocketFactory(); }
From source file:com.swisscom.safeconnect.backend.SwisscomSslSocketFactory.java
public SwisscomSslSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(truststore); String alg = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg); tmFact.init(truststore);/*from www .ja va 2s . c om*/ sslContext.init(null, tmFact.getTrustManagers(), null); }
From source file:android.apn.androidpn.server.xmpp.ssl.SSLTrustManagerFactory.java
public static TrustManager[] getTrustManagers(KeyStore truststore, String trustpass) { TrustManager[] trustManagers; try {//from w w w .j ava 2 s. co m if (truststore == null) { trustManagers = null; } else { TrustManagerFactory trustFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); if (trustpass == null) { trustpass = SSLConfig.getc2sTrustPassword(); } trustFactory.init(truststore); trustManagers = trustFactory.getTrustManagers(); } } catch (KeyStoreException e) { trustManagers = null; log.error("SSLTrustManagerFactory startup problem.", e); } catch (NoSuchAlgorithmException e) { trustManagers = null; log.error("SSLTrustManagerFactory startup problem.", e); } return trustManagers; }
From source file:org.rhq.enterprise.server.plugins.rhnhosted.RHNSSLSocketFactory.java
/** * * @param sslCerts these certs will be used to validate the ssl connection * @return/* w ww. j a va 2 s . c o m*/ * @throws IOException * @throws GeneralSecurityException */ static public SSLSocketFactory getSSLSocketFactory(List<X509Certificate> sslCerts) throws IOException, GeneralSecurityException { SSLContext sc = null; KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null); //Important, this intializes the keystore int counter = 0; for (X509Certificate cert : sslCerts) { ks.setCertificateEntry("rhn-key-" + counter, cert); counter++; } sc = SSLContext.getInstance("SSL"); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); sc.init(null, tmf.getTrustManagers(), new java.security.SecureRandom()); return sc.getSocketFactory(); }
From source file:org.anhonesteffort.flock.sync.AppSecureSocketFactory.java
private static SSLContext createAppStoreSSLContext(Context appContext, boolean useFlockTrustStore) throws HttpClientError { if (appContext == null) throw new HttpClientError("application context is null :("); KeyStore trustStore;//w w w.j a v a2 s .co m try { if (useFlockTrustStore) { AssetManager assetManager = appContext.getAssets(); InputStream keyStoreInputStream = assetManager.open("flock.store"); trustStore = KeyStore.getInstance("BKS"); trustStore.load(keyStoreInputStream, "owsflock".toCharArray()); } else { trustStore = KeyStore.getInstance("AndroidCAStore"); trustStore.load(null, null); } TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); tmf.init(trustStore); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tmf.getTrustManagers(), null); return sslContext; } catch (Exception e) { Log.e(TAG, "createAppStoreSSLContext() - flock store? " + useFlockTrustStore, e); throw new HttpClientError(e.toString()); } }