Search in sources :

Example 36 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project OpenAttestation by OpenAttestation.

the class SslUtil method createX509TrustManagerWithCertificates.

public static X509TrustManager createX509TrustManagerWithCertificates(X509Certificate[] certificates) throws KeyManagementException {
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(createTrustedSslKeystore(certificates));
        TrustManager[] tms = tmf.getTrustManagers();
        for (TrustManager tm : tms) {
            if (tm instanceof X509TrustManager) {
                return (X509TrustManager) tm;
            }
        }
    } catch (NoSuchAlgorithmException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (IOException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (CertificateException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (UnrecoverableEntryException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    } catch (KeyStoreException e) {
        throw new KeyManagementException("Cannot create X509TrustManager", e);
    }
    throw new IllegalArgumentException("TrustManagerFactory did not return an X509TrustManager instance");
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 37 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project qi4j-sdk by Qi4j.

the class AbstractSecureJettyTest method beforeSecureClass.

@BeforeClass
public static void beforeSecureClass() throws IOException, GeneralSecurityException {
    defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String string, SSLSession ssls) {
            return true;
        }
    });
    KeyStore truststore = KeyStore.getInstance("JCEKS");
    truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance(getX509Algorithm());
    caTrustManagerFactory.init(truststore);
    sslCtx.init(null, caTrustManagerFactory.getTrustManagers(), null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) BeforeClass(org.junit.BeforeClass)

Example 38 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project custom-cert-https by nelenkov.

the class MainActivity method dumpTrustedCerts.

private void dumpTrustedCerts() {
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init((KeyStore) null);
        X509TrustManager xtm = (X509TrustManager) tmf.getTrustManagers()[0];
        StringBuffer buff = new StringBuffer();
        for (X509Certificate cert : xtm.getAcceptedIssuers()) {
            String certStr = "S:" + cert.getSubjectDN().getName() + "\nI:" + cert.getIssuerDN().getName();
            Log.d(TAG, certStr);
            buff.append(certStr + "\n\n");
        }
        resultText.setText(buff.toString());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) GeneralSecurityException(java.security.GeneralSecurityException) X509Certificate(java.security.cert.X509Certificate)

Example 39 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project scdl by passy.

the class PinningTrustManagerImpl method initializeSystemTrustManagers.

private TrustManager[] initializeSystemTrustManagers() throws CertificateException {
    try {
        final TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init((KeyStore) null);
        return tmf.getTrustManagers();
    } catch (final NoSuchAlgorithmException nsae) {
        throw new CertificateException(nsae);
    } catch (final KeyStoreException e) {
        throw new CertificateException(e);
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException)

Example 40 with TrustManagerFactory

use of javax.net.ssl.TrustManagerFactory in project spark by perwendel.

the class SparkTestUtil method getSslFactory.

/**
     * Convenience method to use own truststore on SSL Sockets. Will default to
     * the self signed keystore provided in resources, but will respect
     * <p/>
     * -Djavax.net.ssl.keyStore=serverKeys
     * -Djavax.net.ssl.keyStorePassword=password
     * -Djavax.net.ssl.trustStore=serverTrust
     * -Djavax.net.ssl.trustStorePassword=password SSLApplication
     * <p/>
     * So these can be used to specify other key/trust stores if required.
     *
     * @return an SSL Socket Factory using either provided keystore OR the
     * keystore specified in JVM params
     */
private SSLSocketFactory getSslFactory() {
    KeyStore keyStore = null;
    try {
        keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        FileInputStream fis = new FileInputStream(getTrustStoreLocation());
        keyStore.load(fis, getTrustStorePassword().toCharArray());
        fis.close();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, tmf.getTrustManagers(), null);
        return ctx.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

TrustManagerFactory (javax.net.ssl.TrustManagerFactory)233 KeyStore (java.security.KeyStore)145 SSLContext (javax.net.ssl.SSLContext)111 TrustManager (javax.net.ssl.TrustManager)89 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)82 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)55 X509TrustManager (javax.net.ssl.X509TrustManager)55 FileInputStream (java.io.FileInputStream)53 IOException (java.io.IOException)48 KeyStoreException (java.security.KeyStoreException)47 InputStream (java.io.InputStream)45 CertificateException (java.security.cert.CertificateException)35 KeyManagementException (java.security.KeyManagementException)30 X509Certificate (java.security.cert.X509Certificate)25 SecureRandom (java.security.SecureRandom)22 KeyManager (javax.net.ssl.KeyManager)17 UnrecoverableKeyException (java.security.UnrecoverableKeyException)16 CertificateFactory (java.security.cert.CertificateFactory)15 GeneralSecurityException (java.security.GeneralSecurityException)13 File (java.io.File)11