Java Code Examples for javax.net.ssl.HttpsURLConnection#getSSLSocketFactory()

The following examples show how to use javax.net.ssl.HttpsURLConnection#getSSLSocketFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SslUtils.java    From AndroidWallet with GNU General Public License v3.0 7 votes vote down vote up
/**
 * Makes an URL connection to accept a server-side certificate with specific
 * thumbprint and ignore host name verification. This is useful and safe if
 * you have a client with a hard coded well-known certificate
 * 
 * @param connection
 *           The connection to configure
 * @param serverThumbprint
 *           The X509 thumbprint of the server side certificate
 */
public static void configureTrustedCertificate(URLConnection connection, final String serverThumbprint) {
   if (!(connection instanceof HttpsURLConnection)) {
      return;
   }

   HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) connection;

   if (httpsUrlConnection.getHostnameVerifier() != HOST_NAME_VERIFIER_ACCEPT_ALL) {
      httpsUrlConnection.setHostnameVerifier(HOST_NAME_VERIFIER_ACCEPT_ALL);
   }
   SSLSocketFactory sslSocketFactory = getSsLSocketFactory(serverThumbprint);
   if (httpsUrlConnection.getSSLSocketFactory() != sslSocketFactory) {
      httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
   }
}
 
Example 2
Source File: SslUtils.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Makes an URL connection to accept a server-side certificate with specific
 * thumbprint and ignore host name verification. This is useful and safe if
 * you have a client with a hard coded well-known certificate
 * 
 * @param connection
 *           The connection to configure
 * @param serverThumbprint
 *           The X509 thumbprint of the server side certificate
 */
public static void configureTrustedCertificate(URLConnection connection, final String serverThumbprint) {
   if (!(connection instanceof HttpsURLConnection)) {
      return;
   }

   HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) connection;

   if (httpsUrlConnection.getHostnameVerifier() != HOST_NAME_VERIFIER_ACCEPT_ALL) {
      httpsUrlConnection.setHostnameVerifier(HOST_NAME_VERIFIER_ACCEPT_ALL);
   }
   SSLSocketFactory sslSocketFactory = getSsLSocketFactory(serverThumbprint);
   if (httpsUrlConnection.getSSLSocketFactory() != sslSocketFactory) {
      httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
   }
}
 
Example 3
Source File: SslUtils.java    From bitshares_wallet with MIT License 6 votes vote down vote up
/**
 * Makes an URL connection to accept a server-side certificate with specific
 * thumbprint and ignore host name verification. This is useful and safe if
 * you have a client with a hard coded well-known certificate
 * 
 * @param connection
 *           The connection to configure
 * @param serverThumbprint
 *           The X509 thumbprint of the server side certificate
 */
public static void configureTrustedCertificate(URLConnection connection, final String serverThumbprint) {
   if (!(connection instanceof HttpsURLConnection)) {
      return;
   }

   HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) connection;

   if (httpsUrlConnection.getHostnameVerifier() != HOST_NAME_VERIFIER_ACCEPT_ALL) {
      httpsUrlConnection.setHostnameVerifier(HOST_NAME_VERIFIER_ACCEPT_ALL);
   }
   SSLSocketFactory sslSocketFactory = getSsLSocketFactory(serverThumbprint);
   if (httpsUrlConnection.getSSLSocketFactory() != sslSocketFactory) {
      httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
   }
}
 
Example 4
Source File: SslUtils.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Makes an URL connection to accept a server-side certificate with specific
 * thumbprint and ignore host name verification. This is useful and safe if
 * you have a client with a hard coded well-known certificate
 *
 * @param connection       The connection to configure
 * @param serverThumbprint The X509 thumbprint of the server side certificate
 */
public static void configureTrustedCertificate(URLConnection connection, final String serverThumbprint) {
    if (!(connection instanceof HttpsURLConnection)) {
        return;
    }

    HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) connection;

    if (httpsUrlConnection.getHostnameVerifier() != HOST_NAME_VERIFIER_ACCEPT_ALL) {
        httpsUrlConnection.setHostnameVerifier(HOST_NAME_VERIFIER_ACCEPT_ALL);
    }
    SSLSocketFactory sslSocketFactory = getSsLSocketFactory(serverThumbprint);
    if (httpsUrlConnection.getSSLSocketFactory() != sslSocketFactory) {
        // SSL socket factory is nonnull in Android Q
        httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
    }
}
 
Example 5
Source File: ClientTest.java    From moip-sdk-java-le with MIT License 6 votes vote down vote up
@Test
public void testSSLSupport() throws Exception {
	URL urlMock = mock(URL.class);
	HttpsURLConnection httpsMock = mock(HttpsURLConnection.class);
	
	whenNew(URL.class).withAnyArguments().thenReturn(urlMock);
	when(urlMock.openConnection()).thenReturn(httpsMock);
	
	doCallRealMethod().when(httpsMock).setSSLSocketFactory(any(SSLSocketFactory.class));
	when(httpsMock.getSSLSocketFactory()).thenCallRealMethod();
	when(httpsMock.getURL()).thenReturn(urlMock);
	when(httpsMock.getOutputStream()).thenReturn(mock(DataOutputStream.class));
	
	SSLContext context = mock(SSLContext.class);

    mockStatic(SSLContext.class);
    when(SSLContext.getInstance("TLS")).thenReturn(context);
	
    client.post("/200", new Order(), Order.class);
    
    SSLSocketFactory sslSocketFactory = httpsMock.getSSLSocketFactory();
    assertTrue(sslSocketFactory instanceof SSLSupport);
    
}
 
Example 6
Source File: FileTransfer.java    From jpHolo with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 7
Source File: FileTransfer.java    From cordova-android-chromeview with Apache License 2.0 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 8
Source File: FileTransfer.java    From reader with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 9
Source File: FileTransfer.java    From reader with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 10
Source File: FileTransfer.java    From reader with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 11
Source File: FileTransfer.java    From reader with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 12
Source File: FileTransfer.java    From reader with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 13
Source File: FileTransfer.java    From reader with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 14
Source File: FileTransfer.java    From reacteu-app with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 15
Source File: FileTransfer.java    From reacteu-app with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 16
Source File: FileTransfer.java    From react-native-file-transfer with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        LOG.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 17
Source File: FileTransfer.java    From L.TileLayer.Cordova with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 18
Source File: FileTransfer.java    From L.TileLayer.Cordova with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 19
Source File: FileTransfer.java    From L.TileLayer.Cordova with MIT License 5 votes vote down vote up
/**
 * This function will install a trust manager that will blindly trust all SSL
 * certificates.  The reason this code is being added is to enable developers
 * to do development using self signed SSL certificates on their web server.
 *
 * The standard HttpsURLConnection class will throw an exception on self
 * signed certificates if this code is not run.
 */
private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    // Install the all-trusting trust manager
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
        // Install our all trusting manager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        SSLSocketFactory newFactory = sc.getSocketFactory();
        connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return oldFactory;
}
 
Example 20
Source File: UrlUtils.java    From TorrentEngine with GNU General Public License v3.0 5 votes vote down vote up
public static void
DHHackIt(
	HttpsURLConnection	ssl_con )
{
	final SSLSocketFactory factory = ssl_con.getSSLSocketFactory();

	SSLSocketFactory hack = DHHackIt( factory );

	ssl_con.setSSLSocketFactory( hack );
}