use of javax.net.ssl.X509TrustManager in project zm-mailbox by Zimbra.
the class SSLUtil method getDummySSLContext.
/**
* Returns an SSLContext that can be used to create SSL connections without
* certificates. This is obviously insecure and should only be used for
* testing.
*
* @return an SSLContext that trusts all certificates
*/
public static SSLContext getDummySSLContext() {
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] cert, String authType) {
// trust all certs
}
public void checkServerTrusted(X509Certificate[] cert, String authType) {
// trust all certs
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { tm }, null);
return sc;
} catch (Exception e) {
throw new IllegalStateException("Could not create SSL context", e);
}
}
use of javax.net.ssl.X509TrustManager in project maven-plugins by apache.
the class ProjectInfoReportUtils method getURLConnection.
/**
* @param url not null
* @param project not null
* @param settings not null
* @return the url connection with auth if required. Don't check the certificate if SSL scheme.
* @throws IOException if any
*/
private static URLConnection getURLConnection(URL url, MavenProject project, Settings settings) throws IOException {
URLConnection conn = url.openConnection();
conn.setConnectTimeout(TIMEOUT);
conn.setReadTimeout(TIMEOUT);
//@formatter:off
if (settings.getServers() != null && !settings.getServers().isEmpty() && project != null && project.getDistributionManagement() != null && (project.getDistributionManagement().getRepository() != null || project.getDistributionManagement().getSnapshotRepository() != null) && (StringUtils.isNotEmpty(project.getDistributionManagement().getRepository().getUrl()) || StringUtils.isNotEmpty(project.getDistributionManagement().getSnapshotRepository().getUrl()))) //@formatter:on
{
Server server = null;
if (url.toString().contains(project.getDistributionManagement().getRepository().getUrl())) {
server = settings.getServer(project.getDistributionManagement().getRepository().getId());
}
if (server == null && url.toString().contains(project.getDistributionManagement().getSnapshotRepository().getUrl())) {
server = settings.getServer(project.getDistributionManagement().getSnapshotRepository().getId());
}
if (server != null && StringUtils.isNotEmpty(server.getUsername()) && StringUtils.isNotEmpty(server.getPassword())) {
String up = server.getUsername().trim() + ":" + server.getPassword().trim();
String upEncoded = new String(Base64.encodeBase64Chunked(up.getBytes())).trim();
conn.setRequestProperty("Authorization", "Basic " + upEncoded);
}
}
if (conn instanceof HttpsURLConnection) {
HostnameVerifier hostnameverifier = new HostnameVerifier() {
/** {@inheritDoc} */
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
((HttpsURLConnection) conn).setHostnameVerifier(hostnameverifier);
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
/** {@inheritDoc} */
public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
}
/** {@inheritDoc} */
public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
}
/** {@inheritDoc} */
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
} catch (NoSuchAlgorithmException e1) {
// ignore
} catch (KeyManagementException e) {
// ignore
}
}
return conn;
}
use of javax.net.ssl.X509TrustManager in project cloudstack by apache.
the class HttpClientWrapper method wrapClient.
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLUtils.getSSLContext();
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
X509HostnameVerifier verifier = new X509HostnameVerifier() {
@Override
public void verify(String string, SSLSocket ssls) throws IOException {
}
@Override
public void verify(String string, X509Certificate xc) throws SSLException {
}
@Override
public void verify(String string, String[] strings, String[] strings1) throws SSLException {
}
@Override
public boolean verify(String string, SSLSession ssls) {
return true;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(verifier);
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
use of javax.net.ssl.X509TrustManager in project cubrid-manager by CUBRID.
the class ClientHttp method setUpConnection.
/**
* Set up a http client
*
* @throws UnknownHostException a possible exception
* @throws IOException a possible exception
*/
private void setUpConnection() {
tearDownConnection();
this.requestUrl = "https://" + hostAddress + ":" + port + METHOD;
// support https
try {
// KeyStore trustStore =
// KeyStore.getInstance(KeyStore.getDefaultType());
// instream = new FileInputStream(new File("cm.keystore"));
// trustStore.load(instream, "admin1".toCharArray());
// SSLSocketFactory socketFactory = new
// SSLSocketFactory(trustStore);
// Scheme sch = new Scheme("https", 443, socketFactory);
// this.httpClient.getConnectionManager().getSchemeRegistry().register(sch);
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { tm }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
URL url = new URL(requestUrl);
conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(timeout);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
} catch (Exception e) {
LOGGER.error("Make to support HTTPS failed.", e);
}
}
use of javax.net.ssl.X509TrustManager in project cloudstack by apache.
the class NexentaNmsClient method getHttpsClient.
protected DefaultHttpClient getHttpsClient() {
try {
SSLContext sslContext = SSLUtils.getSSLContext();
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, new SecureRandom());
SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", nmsUrl.getPort(), socketFactory));
BasicClientConnectionManager mgr = new BasicClientConnectionManager(registry);
return new DefaultHttpClient(mgr);
} catch (NoSuchAlgorithmException ex) {
throw new CloudRuntimeException(ex.getMessage());
} catch (KeyManagementException ex) {
throw new CloudRuntimeException(ex.getMessage());
}
}
Aggregations