Search in sources :

Example 11 with HandshakeCompletedEvent

use of javax.net.ssl.HandshakeCompletedEvent in project robovm by robovm.

the class HandshakeCompletedEventTest method test_getSocket.

/**
     * @throws IOException
     * javax.net.ssl.HandshakeCompletedEvent#getSocket()
     */
public final void test_getSocket() throws IOException {
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, null);
    SSLSocket ss = event.getSocket();
    assertNotNull(ss);
    assertEquals(socket, ss);
}
Also used : HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) SSLSocket(javax.net.ssl.SSLSocket)

Example 12 with HandshakeCompletedEvent

use of javax.net.ssl.HandshakeCompletedEvent in project robovm by robovm.

the class HandshakeCompletedEventTest method test_Constructor.

/**
     * @throws IOException
     * javax.net.ssl.HandshakeCompletedEvent#HandshakeCompletedEvent(SSLSocket sock, SSLSession s)
     */
public final void test_Constructor() throws Exception {
    mySSLSession session = new mySSLSession();
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
    try {
        new HandshakeCompletedEvent(null, null);
        fail("Any exception wasn't thrown for null parameters");
    } catch (Exception expected) {
    }
}
Also used : HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) SSLSocket(javax.net.ssl.SSLSocket) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 13 with HandshakeCompletedEvent

use of javax.net.ssl.HandshakeCompletedEvent in project robovm by robovm.

the class HandshakeCompletedEventTest method test_getPeerPrincipal.

/**
     * @throws IOException
     * javax.net.ssl.HandshakeCompletedEvent#getPeerPrincipal()
     */
public final void test_getPeerPrincipal() throws IOException {
    mySSLSession session = new mySSLSession("localhost", 1080, null);
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
    assertNull(event.getPeerPrincipal());
}
Also used : HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) SSLSocket(javax.net.ssl.SSLSocket) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession)

Example 14 with HandshakeCompletedEvent

use of javax.net.ssl.HandshakeCompletedEvent in project XobotOS by xamarin.

the class SSLSocketImpl method doHandshake.

/*
     * Performs handshake process over this connection. The handshake
     * process is directed by the handshake status code provided by
     * handshake protocol. If this status is NEED_WRAP, method retrieves
     * handshake message from handshake protocol and sends it to another peer.
     * If this status is NEED_UNWRAP, method receives and processes handshake
     * message from another peer. Each of this stages (wrap/unwrap) change
     * the state of handshake protocol and this process is performed
     * until handshake status is FINISHED. After handshake process is finished
     * handshake completed event are sent to the registered listeners.
     * For more information about the handshake process see
     * TLS v1 specification (http://www.ietf.org/rfc/rfc2246.txt) p 7.3.
     */
private void doHandshake() throws IOException {
    SSLEngineResult.HandshakeStatus status;
    int type;
    try {
        while (!(status = handshakeProtocol.getStatus()).equals(SSLEngineResult.HandshakeStatus.FINISHED)) {
            if (logger != null) {
                String s = (status.equals(SSLEngineResult.HandshakeStatus.NEED_WRAP)) ? "NEED_WRAP" : (status.equals(SSLEngineResult.HandshakeStatus.NEED_UNWRAP)) ? "NEED_UNWRAP" : "STATUS: OTHER!";
                logger.println("SSLSocketImpl: HS status: " + s + " " + status);
            }
            if (status.equals(SSLEngineResult.HandshakeStatus.NEED_WRAP)) {
                output.write(handshakeProtocol.wrap());
            } else if (status.equals(SSLEngineResult.HandshakeStatus.NEED_UNWRAP)) {
                // and retrieve the type of unwrapped data
                switch(type = recordProtocol.unwrap()) {
                    case ContentType.HANDSHAKE:
                    case ContentType.CHANGE_CIPHER_SPEC:
                        break;
                    case ContentType.APPLICATION_DATA:
                        // constraints (do not expect buffer overflow).
                        break;
                    case ContentType.ALERT:
                        processAlert();
                        if (socket_was_closed) {
                            return;
                        }
                        break;
                    default:
                        // will throw exception
                        reportFatalAlert(AlertProtocol.UNEXPECTED_MESSAGE, new SSLException("Unexpected message of type " + type + " has been got"));
                }
            } else {
                // will throw exception
                reportFatalAlert(AlertProtocol.INTERNAL_ERROR, new SSLException("Handshake passed unexpected status: " + status));
            }
            if (alertProtocol.hasAlert()) {
                // warning alert occurred during wrap or unwrap
                // (note: fatal alert causes AlertException
                // to be thrown)
                output.write(alertProtocol.wrap());
                alertProtocol.setProcessed();
            }
        }
    } catch (EndOfSourceException e) {
        appDataIS.setEnd();
        throw new IOException("Connection was closed");
    } catch (AlertException e) {
        // will throw exception
        reportFatalAlert(e.getDescriptionCode(), e.getReason());
    }
    session = recordProtocol.getSession();
    if (listeners != null) {
        // notify the listeners
        HandshakeCompletedEvent event = new HandshakeCompletedEvent(this, session);
        int size = listeners.size();
        for (int i = 0; i < size; i++) {
            listeners.get(i).handshakeCompleted(event);
        }
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 15 with HandshakeCompletedEvent

use of javax.net.ssl.HandshakeCompletedEvent in project robovm by robovm.

the class SSLSocketTest method test_SSLSocket_HandshakeCompletedListener_RuntimeException.

public void test_SSLSocket_HandshakeCompletedListener_RuntimeException() throws Exception {
    final Thread self = Thread.currentThread();
    final UncaughtExceptionHandler original = self.getUncaughtExceptionHandler();
    final RuntimeException expectedException = new RuntimeException("expected");
    final TestUncaughtExceptionHandler test = new TestUncaughtExceptionHandler();
    self.setUncaughtExceptionHandler(test);
    final TestSSLContext c = TestSSLContext.create();
    final SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    client.addHandshakeCompletedListener(new HandshakeCompletedListener() {

        public void handshakeCompleted(HandshakeCompletedEvent event) {
            throw expectedException;
        }
    });
    client.startHandshake();
    future.get();
    client.close();
    server.close();
    c.close();
    assertSame(expectedException, test.actualException);
    self.setUncaughtExceptionHandler(original);
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) ExecutorService(java.util.concurrent.ExecutorService) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Aggregations

HandshakeCompletedEvent (javax.net.ssl.HandshakeCompletedEvent)17 SSLSocket (javax.net.ssl.SSLSocket)14 org.apache.harmony.xnet.tests.support.mySSLSession (org.apache.harmony.xnet.tests.support.mySSLSession)8 IOException (java.io.IOException)7 HandshakeCompletedListener (javax.net.ssl.HandshakeCompletedListener)6 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)5 SocketException (java.net.SocketException)4 SSLException (javax.net.ssl.SSLException)4 CertificateException (java.security.cert.CertificateException)3 SocketTimeoutException (java.net.SocketTimeoutException)2 Certificate (java.security.cert.Certificate)2 ExecutorService (java.util.concurrent.ExecutorService)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 SSLProtocolException (javax.net.ssl.SSLProtocolException)2 SSLServerSocket (javax.net.ssl.SSLServerSocket)2 SSLSession (javax.net.ssl.SSLSession)2 X509Certificate (javax.security.cert.X509Certificate)2 SharedPreferences (android.content.SharedPreferences)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1