Search in sources :

Example 61 with KeyManagementException

use of java.security.KeyManagementException 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 62 with KeyManagementException

use of java.security.KeyManagementException in project opennms by OpenNMS.

the class AsyncBasicDetectorMinaImpl method isServiceDetected.

/**
 * {@inheritDoc}
 */
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
    final DetectFutureMinaImpl detectFuture = new DetectFutureMinaImpl(this);
    try {
        // Set this up here because it can throw an Exception, which we want
        // to throw now, not in initializeSession
        final SSLContext c = createClientSSLContext();
        // Create an IoSessionInitializer that will configure this individual
        // session. Previously, all this was done on a new Connector each time
        // but that was leaking file handles all over the place. This way gives
        // us per-connection settings without the overhead of creating new
        // Connectors each time
        IoSessionInitializer<ConnectFuture> init = new IoSessionInitializer<ConnectFuture>() {

            @Override
            public void initializeSession(IoSession session, ConnectFuture future) {
                // Add filters to the session
                if (isUseSSLFilter()) {
                    final SslFilter filter = new SslFilter(c);
                    filter.setUseClientMode(true);
                    session.getFilterChain().addFirst("SSL", filter);
                }
                session.getFilterChain().addLast("logger", getLoggingFilter() != null ? getLoggingFilter() : new SlightlyMoreVerboseLoggingFilter());
                session.getFilterChain().addLast("codec", getProtocolCodecFilter());
                // Make the minimum idle timeout 1 second
                int idleTimeInSeconds = Math.max(1, Math.round(getIdleTime() / 1000.0f));
                // Set all of the idle time limits. Make sure to specify values in
                // seconds!!!
                session.getConfig().setReaderIdleTime(idleTimeInSeconds);
                session.getConfig().setWriterIdleTime(idleTimeInSeconds);
                session.getConfig().setBothIdleTime(idleTimeInSeconds);
            }
        };
        // Start communication
        final InetSocketAddress socketAddress = new InetSocketAddress(address, getPort());
        final ConnectFuture cf = m_connectionFactory.connect(socketAddress, init, createDetectorHandler(detectFuture));
        cf.addListener(retryAttemptListener(detectFuture, socketAddress, init, getRetries()));
    } catch (KeyManagementException e) {
        detectFuture.setException(e);
    } catch (NoSuchAlgorithmException e) {
        detectFuture.setException(e);
    } catch (Throwable e) {
        detectFuture.setException(e);
    }
    return detectFuture;
}
Also used : SslFilter(org.apache.mina.filter.ssl.SslFilter) InetSocketAddress(java.net.InetSocketAddress) SSLContext(javax.net.ssl.SSLContext) ConnectFuture(org.apache.mina.core.future.ConnectFuture) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) IoSessionInitializer(org.apache.mina.core.session.IoSessionInitializer) IoSession(org.apache.mina.core.session.IoSession)

Example 63 with KeyManagementException

use of java.security.KeyManagementException in project android_frameworks_base by AOSPA.

the class SSLCertificateSocketFactory method makeSocketFactory.

private SSLSocketFactory makeSocketFactory(KeyManager[] keyManagers, TrustManager[] trustManagers) {
    try {
        OpenSSLContextImpl sslContext = OpenSSLContextImpl.getPreferred();
        sslContext.engineInit(keyManagers, trustManagers, null);
        sslContext.engineGetClientSessionContext().setPersistentCache(mSessionCache);
        return sslContext.engineGetSocketFactory();
    } catch (KeyManagementException e) {
        Log.wtf(TAG, e);
        // Fallback
        return (SSLSocketFactory) SSLSocketFactory.getDefault();
    }
}
Also used : OpenSSLContextImpl(com.android.org.conscrypt.OpenSSLContextImpl) KeyManagementException(java.security.KeyManagementException)

Example 64 with KeyManagementException

use of java.security.KeyManagementException in project K6nele by Kaljurand.

the class WebSocketRecognitionService method startSocket.

/**
 * Opens the socket and starts recording/sending.
 *
 * @param url Webservice URL
 */
void startSocket(String url) {
    mIsEosSent = false;
    Log.i(url);
    AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
    if (false) {
        // http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
        AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
        SSLContext sslContext = null;
        try {
            sslContext = getSSLContext();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        sslSocketMiddleware.setSSLContext(sslContext);
        client.insertMiddleware(sslSocketMiddleware);
    }
    client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, final WebSocket webSocket) {
            mWebSocket = webSocket;
            if (ex != null) {
                handleException(ex);
                return;
            }
            webSocket.setStringCallback(new WebSocket.StringCallback() {

                public void onStringAvailable(String s) {
                    Log.i(s);
                    handleResult(s);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("ClosedCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("ClosedCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("EndCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("EndCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            startSending(webSocket);
        }
    });
}
Also used : AsyncSSLSocketMiddleware(com.koushikdutta.async.http.AsyncSSLSocketMiddleware) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient) WebSocket(com.koushikdutta.async.http.WebSocket)

Example 65 with KeyManagementException

use of java.security.KeyManagementException in project J2ME-Loader by nikita36078.

the class Connection method openConnection.

@Override
public javax.microedition.io.Connection openConnection(String name, int mode, boolean timeouts) throws IOException {
    if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
        throw new IOException("No network");
    }
    int portSepIndex = name.lastIndexOf(':');
    int port = Integer.parseInt(name.substring(portSepIndex + 1));
    String host = name.substring("ssl://".length(), portSepIndex);
    // TODO 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) {
        }
    } };
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        SSLSocketFactory factory = sc.getSocketFactory();
        socket = factory.createSocket(host, port);
    } catch (NoSuchAlgorithmException ex) {
        throw new IOException(ex.toString());
    } catch (KeyManagementException ex) {
        throw new IOException(ex.toString());
    }
    return this;
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Aggregations

KeyManagementException (java.security.KeyManagementException)157 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)111 SSLContext (javax.net.ssl.SSLContext)83 KeyStoreException (java.security.KeyStoreException)60 IOException (java.io.IOException)55 TrustManager (javax.net.ssl.TrustManager)45 CertificateException (java.security.cert.CertificateException)35 X509TrustManager (javax.net.ssl.X509TrustManager)28 SecureRandom (java.security.SecureRandom)27 X509Certificate (java.security.cert.X509Certificate)26 UnrecoverableKeyException (java.security.UnrecoverableKeyException)24 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)24 KeyStore (java.security.KeyStore)22 KeyManager (javax.net.ssl.KeyManager)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)16 HostnameVerifier (javax.net.ssl.HostnameVerifier)15 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)15 InputStream (java.io.InputStream)12 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)11 SSLSession (javax.net.ssl.SSLSession)10