use of javax.net.ssl.HostnameVerifier in project OpenGrok by OpenGrok.
the class Query method createHttpsUrlConnection.
private HttpsURLConnection createHttpsUrlConnection(URL url) {
try {
System.setProperty("jsse.enableSNIExtension", "false");
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
} };
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
return (HttpsURLConnection) url.openConnection();
} catch (Exception ex) {
handleException(ex);
}
return null;
}
use of javax.net.ssl.HostnameVerifier in project jersey by jersey.
the class HttpUrlConnector method secureConnection.
/**
* Secure connection if necessary.
* <p/>
* Provided implementation sets {@link HostnameVerifier} and {@link SSLSocketFactory} to give connection, if that
* is an instance of {@link HttpsURLConnection}.
*
* @param client client associated with this client runtime.
* @param uc http connection to be secured.
*/
protected void secureConnection(final JerseyClient client, final HttpURLConnection uc) {
if (uc instanceof HttpsURLConnection) {
HttpsURLConnection suc = (HttpsURLConnection) uc;
final HostnameVerifier verifier = client.getHostnameVerifier();
if (verifier != null) {
suc.setHostnameVerifier(verifier);
}
if (HttpsURLConnection.getDefaultSSLSocketFactory() == suc.getSSLSocketFactory()) {
// indicates that the custom socket factory was not set
suc.setSSLSocketFactory(sslSocketFactory.get());
}
}
}
use of javax.net.ssl.HostnameVerifier in project jersey by jersey.
the class SslFilterTest method testCustomHostameVerificationFail.
@Test
public void testCustomHostameVerificationFail() throws Throwable {
CountDownLatch latch = new CountDownLatch(1);
SslEchoServer server = new SslEchoServer();
try {
server.start();
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return false;
}
};
openClientSocket("localhost", ByteBuffer.allocate(0), latch, verifier);
fail();
} catch (SSLException e) {
// expected
} finally {
server.stop();
}
}
use of javax.net.ssl.HostnameVerifier in project apjp by jvansteirteghem.
the class HTTPSRequest method open.
public void open() throws HTTPSRequestException {
try {
url = new URL(APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i]);
Proxy proxy = Proxy.NO_PROXY;
if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTP_PROXY_SERVER_ADDRESS, APJP.APJP_HTTP_PROXY_SERVER_PORT));
}
} else {
if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS, APJP.APJP_HTTPS_PROXY_SERVER_PORT));
}
}
}
urlConnection = url.openConnection(proxy);
if (urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) urlConnection).setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
String value1 = APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i];
String[] values1 = value1.split("/", -1);
String value2 = values1[2];
String[] values2 = value2.split(":");
String value3 = values2[0];
if (value3.equalsIgnoreCase(hostname)) {
return true;
} else {
return false;
}
}
});
}
if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTP_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTP_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTP_PROXY_SERVER_PASSWORD).getBytes())));
}
} else {
if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTPS_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTPS_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD).getBytes())));
}
}
}
for (int j = 0; j < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
if (APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j].equalsIgnoreCase("") == false) {
urlConnection.setRequestProperty(APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j], APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE[i][j]);
}
}
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
} catch (Exception e) {
throw new HTTPSRequestException("HTTPS_REQUEST/OPEN", e);
}
}
use of javax.net.ssl.HostnameVerifier in project custom-cert-https by nelenkov.
the class MainActivity method urlConnConnect.
private void urlConnConnect() {
new GetHtmlTask() {
@Override
protected String doInBackground(Void... arg0) {
try {
boolean useClientAuth = useClientAuthCb.isChecked();
SSLContext sslCtx = createSslContext(useClientAuth);
URL url = new URL(useClientAuth ? CLIENT_AUTH_URL : SERVER_AUTH_URL);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setConnectTimeout(TIMEOUT);
urlConnection.setReadTimeout(TIMEOUT);
urlConnection.setSSLSocketFactory(sslCtx.getSocketFactory());
HostnameVerifier verifier = urlConnection.getHostnameVerifier();
Log.d(TAG, "hostname verifier: " + verifier.getClass().getName());
try {
urlConnection.connect();
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return urlConnection.getResponseMessage();
}
return readLines(urlConnection.getInputStream(), urlConnection.getContentEncoding());
} finally {
urlConnection.disconnect();
}
} catch (Exception e) {
Log.d(TAG, "Error: " + e.getMessage(), e);
error = e;
return null;
}
}
}.execute();
}
Aggregations