use of org.apache.http.conn.ssl.BrowserCompatHostnameVerifier in project custom-cert-https by nelenkov.
the class MainActivity method httpClientConnect.
private void httpClientConnect() {
new GetHtmlTask() {
@Override
protected String doInBackground(Void... arg0) {
try {
boolean useClientAuth = useClientAuthCb.isChecked();
SSLContext sslContext = createSslContext(useClientAuth);
MySSLSocketFactory socketFactory = new MySSLSocketFactory(sslContext, new BrowserCompatHostnameVerifier());
HttpClient client = createHttpClient(socketFactory);
HttpGet get = new HttpGet(useClientAuth ? CLIENT_AUTH_URL : SERVER_AUTH_URL);
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() != 200) {
return "Error: " + response.getStatusLine();
} else {
return EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
Log.d(TAG, "Error: " + e.getMessage(), e);
error = e;
return null;
}
}
}.execute();
}
use of org.apache.http.conn.ssl.BrowserCompatHostnameVerifier in project httpclient by pixmob.
the class HttpRequestBuilder method setupSecureConnection.
/**
* Setup SSL connection.
*/
private static void setupSecureConnection(Context context, HttpsURLConnection conn) throws IOException {
final SSLContext sslContext;
try {
// https://github.com/guardianproject/cacert
if (trustManagers == null) {
// Load SSL certificates:
// http://nelenkov.blogspot.com/2011/12/using-custom-certificate-trust-store-on.html
// Earlier Android versions do not have updated root CA
// certificates, resulting in connection errors.
final KeyStore keyStore = loadCertificates(context);
final CustomTrustManager customTrustManager = new CustomTrustManager(keyStore);
trustManagers = new TrustManager[] { customTrustManager };
}
// Init SSL connection with custom certificates.
// The same SecureRandom instance is used for every connection to
// speed up initialization.
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, SECURE_RANDOM);
} catch (GeneralSecurityException e) {
final IOException ioe = new IOException("Failed to initialize SSL engine");
ioe.initCause(e);
throw ioe;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Fix slow read:
// http://code.google.com/p/android/issues/detail?id=13117
// Prior to ICS, the host name is still resolved even if we already
// know its IP address, for each connection.
final SSLSocketFactory delegate = sslContext.getSocketFactory();
final SSLSocketFactory socketFactory = new SSLSocketFactory() {
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
InetAddress addr = InetAddress.getByName(host);
injectHostname(addr, host);
return delegate.createSocket(addr, port);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return delegate.createSocket(host, port);
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return delegate.createSocket(host, port, localHost, localPort);
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return delegate.createSocket(address, port, localAddress, localPort);
}
private void injectHostname(InetAddress address, String host) {
try {
Field field = InetAddress.class.getDeclaredField("hostName");
field.setAccessible(true);
field.set(address, host);
} catch (Exception ignored) {
}
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
injectHostname(s.getInetAddress(), host);
return delegate.createSocket(s, host, port, autoClose);
}
@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}
};
conn.setSSLSocketFactory(socketFactory);
} else {
conn.setSSLSocketFactory(sslContext.getSocketFactory());
}
conn.setHostnameVerifier(new BrowserCompatHostnameVerifier());
}
Aggregations