Search in sources :

Example 66 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class MyProvider method test_engineGetTrustManagers.

/**
     * @throws NoSuchAlgorithmException
     * javax.net.ssl.TrustManagerFactorySpi#engineGetTrustManagers()
     */
public void test_engineGetTrustManagers() throws NoSuchAlgorithmException {
    factory.reset();
    Provider provider = new MyProvider();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
    TrustManager[] tm = tmf.getTrustManagers();
    assertTrue(factory.isEngineGetTrustManagersCalled());
    factory.reset();
    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        tmf.init(ks);
        tm = tmf.getTrustManagers();
        assertTrue(factory.isEngineGetTrustManagersCalled());
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider) TrustManager(javax.net.ssl.TrustManager)

Example 67 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class myTrustManagerFactory method test_getTrustManagers.

/**
     * Test for <code>geTrustManagers()</code>
     * @throws KeyStoreException
     * @throws IOException
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     */
public void test_getTrustManagers() {
    try {
        TrustManagerFactory trustMF = TrustManagerFactory.getInstance(getDefaultAlgorithm());
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        trustMF.init(ks);
        TrustManager[] tm = trustMF.getTrustManagers();
        assertNotNull("Result has not be null", tm);
        assertTrue("Length of result TrustManager array should not be 0", (tm.length > 0));
    } catch (Exception ex) {
        fail("Unexpected exception " + ex.toString());
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) TrustManager(javax.net.ssl.TrustManager)

Example 68 with TrustManager

use of javax.net.ssl.TrustManager in project ddf by codice.

the class CometDClient method doTrustAllCertificates.

private void doTrustAllCertificates() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    HostnameVerifier hostnameVerifier = (s, sslSession) -> s.equalsIgnoreCase(sslSession.getPeerHost());
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}
Also used : X509Certificate(java.security.cert.X509Certificate) SSLContext(javax.net.ssl.SSLContext) StringUtils(org.apache.commons.lang.StringUtils) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) LongPollingTransport(org.cometd.client.transport.LongPollingTransport) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) TrustManager(javax.net.ssl.TrustManager) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) HashSet(java.util.HashSet) HttpClient(org.eclipse.jetty.client.HttpClient) CollectionUtils(org.apache.commons.collections.CollectionUtils) ISO_DATE_TIME(java.time.format.DateTimeFormatter.ISO_DATE_TIME) Map(java.util.Map) LocalTime(java.time.LocalTime) ClientSessionChannel(org.cometd.bayeux.client.ClientSessionChannel) ConnectException(java.net.ConnectException) Channel(org.cometd.bayeux.Channel) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Logger(org.slf4j.Logger) JsonPath(com.jayway.restassured.path.json.JsonPath) Set(java.util.Set) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) X509TrustManager(javax.net.ssl.X509TrustManager) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) ClientTransport(org.cometd.client.transport.ClientTransport) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Comparator(java.util.Comparator) Collections(java.util.Collections) Message(org.cometd.bayeux.Message) BayeuxClient(org.cometd.client.BayeuxClient) X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 69 with TrustManager

use of javax.net.ssl.TrustManager in project oxCore by GluuFederation.

the class SslDefaultHttpClient method newSslSocketFactory.

private SSLSocketFactory newSslSocketFactory() {
    try {
        TrustManager[] trustManagers = this.trustManagers;
        if (useTrustManager) {
            trustManagers = getTrustManagers();
        }
        KeyManager[] keyManagers = null;
        if (useKeyManager) {
            keyManagers = getKeyManagers();
        }
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(keyManagers, trustManagers, new SecureRandom());
        // Pass the keystore to the SSLSocketFactory
        SSLSocketFactory sf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception ex) {
        throw new IllegalArgumentException("Failed to load keystore", ex);
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) KeyManager(javax.net.ssl.KeyManager) TrustManager(javax.net.ssl.TrustManager)

Example 70 with TrustManager

use of javax.net.ssl.TrustManager in project oxTrust by GluuFederation.

the class LinktrackService method newLink.

public String newLink(@NotNull String login, @NotNull String password, @NotNull String link) {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust managers
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(String.format(CREATE_LINK_URL_PATTERN, login, password, link));
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
    } catch (Exception e) {
        log.error(String.format("Exception happened during linktrack link " + "creation with username: %s, password: %s," + " link: %s.", login, password, link), e);
        return null;
    }
    String trackedLink = null;
    if (response.getStatusLine().getStatusCode() == 201) {
        try {
            trackedLink = IOUtils.toString(response.getEntity().getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return trackedLink;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient)

Aggregations

TrustManager (javax.net.ssl.TrustManager)229 SSLContext (javax.net.ssl.SSLContext)139 X509TrustManager (javax.net.ssl.X509TrustManager)139 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)90 X509Certificate (java.security.cert.X509Certificate)70 IOException (java.io.IOException)60 KeyStore (java.security.KeyStore)60 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)60 SecureRandom (java.security.SecureRandom)58 KeyManagementException (java.security.KeyManagementException)54 KeyManager (javax.net.ssl.KeyManager)52 CertificateException (java.security.cert.CertificateException)43 KeyStoreException (java.security.KeyStoreException)37 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)34 HostnameVerifier (javax.net.ssl.HostnameVerifier)23 URL (java.net.URL)22 GeneralSecurityException (java.security.GeneralSecurityException)22 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)20 InputStream (java.io.InputStream)18 FileInputStream (java.io.FileInputStream)16