use of javax.net.ssl.SSLSocketFactory in project cosmic by MissionCriticalCloud.
the class SecureSSLSocketFactory method createSocket.
@Override
public Socket createSocket(final String host, final int port, final InetAddress inetAddress, final int localPort) throws IOException, UnknownHostException {
final SSLSocketFactory factory = _sslContext.getSocketFactory();
final Socket socket = factory.createSocket(host, port, inetAddress, localPort);
if (socket instanceof SSLSocket) {
((SSLSocket) socket).setEnabledProtocols(SSLUtils.getSupportedProtocols(((SSLSocket) socket).getEnabledProtocols()));
}
return socket;
}
use of javax.net.ssl.SSLSocketFactory in project matrix-android-sdk by matrix-org.
the class CertUtil method newPinnedSSLSocketFactory.
/**
* Create a SSLSocket factory for a HS config.
*
* @param hsConfig the HS config.
* @return SSLSocket factory
*/
public static SSLSocketFactory newPinnedSSLSocketFactory(HomeServerConnectionConfig hsConfig) {
try {
X509TrustManager defaultTrustManager = null;
// X509 checks if fingerprints don't match.
if (!hsConfig.shouldPin()) {
TrustManagerFactory tf = null;
// get the PKIX instance
try {
tf = TrustManagerFactory.getInstance("PKIX");
} catch (Exception e) {
Log.e(LOG_TAG, "## newPinnedSSLSocketFactory() : TrustManagerFactory.getInstance failed " + e.getMessage());
}
// it doesn't exist, use the default one.
if (null == tf) {
try {
tf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
} catch (Exception e) {
Log.e(LOG_TAG, "## addRule : onBingRuleUpdateFailure failed " + e.getMessage());
}
}
tf.init((KeyStore) null);
TrustManager[] trustManagers = tf.getTrustManagers();
for (int i = 0; i < trustManagers.length; i++) {
if (trustManagers[i] instanceof X509TrustManager) {
defaultTrustManager = (X509TrustManager) trustManagers[i];
break;
}
}
}
TrustManager[] trustPinned = new TrustManager[] { new PinnedTrustManager(hsConfig.getAllowedFingerprints(), defaultTrustManager) };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustPinned, new java.security.SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
return sslSocketFactory;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.net.ssl.SSLSocketFactory in project java-sdk by watson-developer-cloud.
the class HttpClientSingleton method setupTLSProtocol.
/**
* Specifically enable all TLS protocols. See: https://github.com/watson-developer-cloud/java-sdk/issues/610
*
* @param builder the {@link OkHttpClient} builder.
*/
private void setupTLSProtocol(final OkHttpClient.Builder builder) {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
System.setProperty("com.ibm.jsse2.overrideDefaultTLS", "true");
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = new DelegatingSSLSocketFactory(sslContext.getSocketFactory()) {
@Override
protected SSLSocket configureSocket(SSLSocket socket) throws IOException {
socket.setEnabledProtocols(new String[] { TlsVersion.TLS_1_0.javaName(), TlsVersion.TLS_1_1.javaName(), TlsVersion.TLS_1_2.javaName() });
return socket;
}
};
builder.sslSocketFactory(sslSocketFactory, trustManager);
} catch (NoSuchAlgorithmException e) {
LOG.log(Level.SEVERE, "The cryptographic algorithm requested is not available in the environment.", e);
} catch (KeyStoreException e) {
LOG.log(Level.SEVERE, "Error using the keystore.", e);
} catch (KeyManagementException e) {
LOG.log(Level.SEVERE, "Error initializing the SSL Context.", e);
}
}
use of javax.net.ssl.SSLSocketFactory in project calcite by apache.
the class TrustAllSslSocketFactory method createSSLSocketFactory.
/**
* Creates an "accept-all" SSLSocketFactory - ssl sockets will accept ANY
* certificate sent to them - thus effectively just securing the
* communications. This could be set in a HttpsURLConnection using
* HttpsURLConnection.setSSLSocketFactory(.....)
*
* @return SSLSocketFactory
*/
public static SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory sslsocketfactory = null;
TrustManager[] trustAllCerts = { new DummyTrustManager() };
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
sslsocketfactory = sc.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return sslsocketfactory;
}
use of javax.net.ssl.SSLSocketFactory in project mule by mulesoft.
the class TlsConfigurationTestCase method overrideDefaultProtocolFromConfigFile.
@Test
public void overrideDefaultProtocolFromConfigFile() throws Exception {
File configFile = createDefaultProtocolConfigFile();
try {
TlsConfiguration tlsConfiguration = new TlsConfiguration(DEFAULT_KEYSTORE);
tlsConfiguration.setSslType("TLSv1.2");
tlsConfiguration.initialise(true, JSSE_NAMESPACE);
SSLSocketFactory socketFactory = tlsConfiguration.getSocketFactory();
SSLContext sslContext = SSLContext.getInstance(SUPPORTED_PROTOCOL);
sslContext.init(null, null, null);
SSLSocketFactory protocolSocketFactory = sslContext.getSocketFactory();
assertThat(socketFactory.getDefaultCipherSuites(), not(arrayWithSize(protocolSocketFactory.getDefaultCipherSuites().length)));
} finally {
configFile.delete();
}
}
Aggregations