Search in sources :

Example 1 with SecureSSLSocketFactory

use of org.apache.cloudstack.utils.security.SecureSSLSocketFactory in project cloudstack by apache.

the class RawHTTP method _getSocket.

private Socket _getSocket() throws IOException {
    if (useSSL) {
        SSLContext context = null;
        try {
            context = SSLUtils.getSSLContext("SunJSSE");
        } catch (NoSuchAlgorithmException e) {
            s_logger.error("Unexpected exception ", e);
        } catch (NoSuchProviderException e) {
            s_logger.error("Unexpected exception ", e);
        }
        if (context == null)
            throw new IOException("Unable to setup SSL context");
        SSLSocket ssl = null;
        try {
            context.init(null, trustAllCerts, new SecureRandom());
            SocketFactory factory = new SecureSSLSocketFactory(context);
            ssl = (SSLSocket) factory.createSocket(host, port);
            ssl.setEnabledProtocols(SSLUtils.getSupportedProtocols(ssl.getEnabledProtocols()));
        /* ssl.setSSLParameters(context.getDefaultSSLParameters()); */
        } catch (IOException e) {
            s_logger.error("IOException: " + e.getMessage(), e);
            throw e;
        } catch (KeyManagementException e) {
            s_logger.error("KeyManagementException: " + e.getMessage(), e);
        } catch (NoSuchAlgorithmException e) {
            s_logger.error("NoSuchAlgorithmException: " + e.getMessage(), e);
        }
        return ssl;
    } else {
        return new Socket(host, port);
    }
}
Also used : SocketFactory(javax.net.SocketFactory) SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchProviderException(java.security.NoSuchProviderException) KeyManagementException(java.security.KeyManagementException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 2 with SecureSSLSocketFactory

use of org.apache.cloudstack.utils.security.SecureSSLSocketFactory in project cloudstack by apache.

the class SocketWrapperImpl method upgradeToSsl.

@Override
public void upgradeToSsl() {
    if (sslSocket != null)
        // Already upgraded
        return;
    if (verbose)
        System.out.println("[" + this + "] INFO: Upgrading socket to SSL.");
    try {
        // Use most secure implementation of SSL available now.
        // JVM will try to negotiate TLS1.2, then will fallback to TLS1.0, if
        // TLS1.2 is not supported.
        SSLContext sslContext = SSLUtils.getSSLContext();
        // Trust all certificates (FIXME: insecure)
        sslContext.init(null, new TrustManager[] { new TrustAllX509TrustManager(sslState) }, null);
        SSLSocketFactory sslSocketFactory = new SecureSSLSocketFactory(sslContext);
        sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), true);
        sslSocket.setEnabledProtocols(SSLUtils.getSupportedProtocols(sslSocket.getEnabledProtocols()));
        sslSocket.startHandshake();
        InputStream sis = sslSocket.getInputStream();
        source.setInputStream(sis);
        OutputStream sos = sslSocket.getOutputStream();
        sink.setOutputStream(sos);
    } catch (Exception e) {
        throw new RuntimeException("Cannot upgrade socket to SSL: " + e.getMessage(), e);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory) SSLContext(javax.net.ssl.SSLContext) TrustAllX509TrustManager(streamer.ssl.TrustAllX509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory) IOException(java.io.IOException)

Example 3 with SecureSSLSocketFactory

use of org.apache.cloudstack.utils.security.SecureSSLSocketFactory in project cloudstack by apache.

the class VmwareClient method trustAllHttpsCertificates.

private static void trustAllHttpsCertificates() throws Exception {
    // Create a trust manager that does not validate certificate chains:
    javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
    javax.net.ssl.TrustManager tm = new TrustAllTrustManager();
    trustAllCerts[0] = tm;
    javax.net.ssl.SSLContext sc = SSLUtils.getSSLContext();
    javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
    sslsc.setSessionTimeout(0);
    sc.init(null, trustAllCerts, null);
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(new SecureSSLSocketFactory(sc));
}
Also used : SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory)

Aggregations

SecureSSLSocketFactory (org.apache.cloudstack.utils.security.SecureSSLSocketFactory)3 IOException (java.io.IOException)2 SSLContext (javax.net.ssl.SSLContext)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SecureRandom (java.security.SecureRandom)1 SocketFactory (javax.net.SocketFactory)1 SSLSocket (javax.net.ssl.SSLSocket)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 TrustAllX509TrustManager (streamer.ssl.TrustAllX509TrustManager)1