use of javax.net.ssl.X509TrustManager in project yamcs-studio by yamcs.
the class ResourceUtil method openRawURLStream.
/**
* Open URL Stream from remote.
*
* @param url
* @return
* @throws IOException
*/
private static InputStream openRawURLStream(final URL url) throws IOException {
if (url.getProtocol().equals("https")) {
// $NON-NLS-1$
// The code to support https protocol is provided by Eric Berryman (eric.berryman@gmail.com) from Frib
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
try {
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
URLConnection connection = url.openConnection();
connection.setReadTimeout(PreferencesHelper.getURLFileLoadingTimeout());
return connection.getInputStream();
}
use of javax.net.ssl.X509TrustManager in project Payara by payara.
the class BaseTestGrizzlyConfig method getSSLSocketFactory.
public SSLSocketFactory getSSLSocketFactory() throws IOException {
try {
// ---------------------------------
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
// ---------------------------------
return sc.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e.getMessage());
}
}
use of javax.net.ssl.X509TrustManager in project baseio by generallycloud.
the class SslContextBuilder method newSSLContext.
private SSLContext newSSLContext(TrustManagerFactory trustManagerFactory, KeyManagerFactory keyManagerFactory, X509TrustManager x509TrustManager, boolean isServer, boolean trustAll, long sessionCacheSize, long sessionTimeout) throws SSLException {
if (isServer && keyManagerFactory == null) {
throw new SSLException("null keyManagerFactory on server");
}
try {
SSLContext ctx = SslContext.getSSLContext();
TrustManager[] tms = null;
KeyManager[] kms = null;
if (keyManagerFactory == null) {
// client
if (trustManagerFactory == null) {
if (x509TrustManager != null) {
tms = new X509TrustManager[] { x509TrustManager };
} else {
if (trustAll) {
X509TrustManager x509m = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
tms = new X509TrustManager[] { x509m };
}
}
} else {
tms = trustManagerFactory.getTrustManagers();
}
} else {
kms = keyManagerFactory.getKeyManagers();
if (trustManagerFactory != null) {
tms = trustManagerFactory.getTrustManagers();
}
}
ctx.init(kms, tms, new SecureRandom());
SSLSessionContext sessCtx = ctx.getClientSessionContext();
if (sessionCacheSize > 0) {
sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
}
if (sessionTimeout > 0) {
sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
}
return ctx;
} catch (Exception e) {
if (e instanceof SSLException) {
throw (SSLException) e;
}
throw new SSLException("failed to initialize the SSL context", e);
}
}
use of javax.net.ssl.X509TrustManager in project athenz by yahoo.
the class SocketTest method test.
@Test
public void test() throws Exception {
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
// setup socket for first call
SSLContext sslContext = Utils.buildSSLContext(keyRefresher.getKeyManagerProxy(), keyRefresher.getTrustManagerProxy());
SSLSocketFactory factory = (SSLSocketFactory) sslContext.getSocketFactory();
SSLSocket s = (SSLSocket) factory.createSocket("localhost", listenPort);
// send first call
s.getOutputStream().write("ping\n".getBytes());
String response = new BufferedReader(new InputStreamReader(s.getInputStream())).readLine();
assertEquals("pong", response);
assertEquals("athenz.production", getCN(s.getSession().getPeerCertificates()));
// update the ssl context on the server
keyRefresher.getKeyManagerProxy().setKeyManager(Utils.getKeyManagers(Resources.getResource("gdpr.aws.core.cert.pem").getPath(), Resources.getResource("gdpr.aws.core.key.pem").getPath()));
// setup socket for the second call
SSLContext sslContext2 = SSLContext.getInstance("TLSv1.2");
sslContext2.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory factory2 = (SSLSocketFactory) sslContext2.getSocketFactory();
SSLSocket s2 = (SSLSocket) factory2.createSocket("localhost", listenPort);
// send second call
s.getOutputStream().write("ping\n".getBytes());
response = new BufferedReader(new InputStreamReader(s.getInputStream())).readLine();
assertEquals("pong", response);
assertEquals("athenz.production", getCN(s2.getSession().getPeerCertificates()));
// retry the first call, it should still pass
s.getOutputStream().write("ping\n".getBytes());
response = new BufferedReader(new InputStreamReader(s.getInputStream())).readLine();
assertEquals("pong", response);
assertEquals("athenz.production", getCN(s.getSession().getPeerCertificates()));
}
use of javax.net.ssl.X509TrustManager in project athenz by yahoo.
the class TrustStoreTest method builtFromJKSFile.
@Test
public void builtFromJKSFile() throws Exception {
String filePath = Resources.getResource("truststore.jks").getFile();
JavaKeyStoreProvider provider = new JavaKeyStoreProvider(filePath, "123456");
TrustStore trustStore = new TrustStore(filePath, provider);
assertEquals(filePath, trustStore.getFilePath());
TrustManager[] trustManagers = trustStore.getTrustManagers();
assertEquals(1, trustManagers.length);
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
X509Certificate[] acceptedIssuers = trustManager.getAcceptedIssuers();
assertEquals(1, acceptedIssuers.length);
X509Certificate certificate = acceptedIssuers[0];
assertEquals("CN=athenz.production,OU=Testing Domain,O=Athenz,ST=CA,C=US", certificate.getIssuerX500Principal().getName());
}
Aggregations