Search in sources :

Example 11 with SSLHandshakeException

use of javax.net.ssl.SSLHandshakeException in project java-apns by notnoop.

the class ApnsConnectionImpl method sendMessage.

private synchronized void sendMessage(ApnsNotification m, boolean fromBuffer) throws NetworkIOException {
    logger.debug("sendMessage {} fromBuffer: {}", m, fromBuffer);
    if (delegate instanceof StartSendingApnsDelegate) {
        ((StartSendingApnsDelegate) delegate).startSending(m, fromBuffer);
    }
    int attempts = 0;
    while (true) {
        try {
            attempts++;
            Socket socket = getOrCreateSocket(fromBuffer);
            socket.getOutputStream().write(m.marshall());
            socket.getOutputStream().flush();
            cacheNotification(m);
            delegate.messageSent(m, fromBuffer);
            //logger.debug("Message \"{}\" sent", m);
            attempts = 0;
            break;
        } catch (SSLHandshakeException e) {
            // No use retrying this, it's dead Jim
            throw new NetworkIOException(e);
        } catch (IOException e) {
            Utilities.close(socket);
            if (attempts >= RETRIES) {
                logger.error("Couldn't send message after " + RETRIES + " retries." + m, e);
                delegate.messageSendFailed(m, e);
                Utilities.wrapAndThrowAsRuntimeException(e);
            }
            if (attempts != 1) {
                logger.info("Failed to send message " + m + "... trying again after delay", e);
                Utilities.sleep(DELAY_IN_MS);
            }
        }
    }
}
Also used : StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) NetworkIOException(com.notnoop.exceptions.NetworkIOException) IOException(java.io.IOException) Socket(java.net.Socket) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NetworkIOException(com.notnoop.exceptions.NetworkIOException)

Example 12 with SSLHandshakeException

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

the class URLConnectionTest method testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache.

/**
     * Tolerate bad https proxy response when using HttpResponseCache. http://b/6754912
     */
public void testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache() throws Exception {
    ProxyConfig proxyConfig = ProxyConfig.PROXY_SYSTEM_PROPERTY;
    TestSSLContext testSSLContext = TestSSLContext.create();
    initResponseCache();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    MockResponse badProxyResponse = new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders().setBody(// Key to reproducing b/6754912
    "bogus proxy connect response content");
    // We enqueue the bad response twice because the connection will
    // be retried with TLS_MODE_COMPATIBLE after the first connection
    // fails.
    server.enqueue(badProxyResponse);
    server.enqueue(badProxyResponse);
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    try {
        connection.connect();
        fail();
    } catch (SSLHandshakeException expected) {
    // Thrown when the connect causes SSLSocket.startHandshake() to throw
    // when it sees the "bogus proxy connect response content"
    // instead of a ServerHello handshake message.
    }
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertContains(connect.getHeaders(), "Host: android.com");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 13 with SSLHandshakeException

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

the class HandshakeIODataStream method append.

private void append(byte[] src, int from, int length) {
    if (read_pos == read_pos_end) {
        // start reading state after writing
        if (write_pos_beg != write_pos) {
            // but inbound handshake data has been received.
            throw new AlertException(AlertProtocol.UNEXPECTED_MESSAGE, new SSLHandshakeException("Handshake message has been received before " + "the last oubound message had been sent."));
        }
        if (read_pos < write_pos) {
            read_pos = write_pos;
            read_pos_end = read_pos;
        }
    }
    if (read_pos_end + length > buff_size) {
        enlargeBuffer(read_pos_end + length - buff_size);
    }
    System.arraycopy(src, from, buffer, read_pos_end, length);
    read_pos_end += length;
}
Also used : SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 14 with SSLHandshakeException

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

the class HandshakeIODataStream method check.

// checks if the data can be written in the buffer
private void check(int length) {
    // 2. all written data was demanded by getData methods
    if (write_pos == write_pos_beg) {
        // just started to write after the reading
        if (read_pos != read_pos_end) {
            // all the inbound handshake data had been read
            throw new AlertException(AlertProtocol.INTERNAL_ERROR, new SSLHandshakeException("Data was not fully read: " + read_pos + " " + read_pos_end));
        }
        // set up the write positions
        if (write_pos_beg < read_pos_end) {
            write_pos_beg = read_pos_end;
            write_pos = write_pos_beg;
        }
    }
    // if there is not enought free space in the buffer - enlarge it:
    if (write_pos + length >= buff_size) {
        enlargeBuffer(length);
    }
}
Also used : SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 15 with SSLHandshakeException

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

the class SSLSocketTest method test_SSLSocket_setUseClientMode.

private void test_SSLSocket_setUseClientMode(final boolean clientClientMode, final boolean serverClientMode) throws Exception {
    TestSSLContext c = TestSSLContext.create();
    SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<IOException> future = executor.submit(new Callable<IOException>() {

        @Override
        public IOException call() throws Exception {
            try {
                if (!serverClientMode) {
                    server.setSoTimeout(1 * 1000);
                }
                server.setUseClientMode(serverClientMode);
                server.startHandshake();
                return null;
            } catch (SSLHandshakeException e) {
                return e;
            } catch (SocketTimeoutException e) {
                return e;
            }
        }
    });
    executor.shutdown();
    if (!clientClientMode) {
        client.setSoTimeout(1 * 1000);
    }
    client.setUseClientMode(clientClientMode);
    client.startHandshake();
    IOException ioe = future.get();
    if (ioe != null) {
        throw ioe;
    }
    client.close();
    server.close();
    c.close();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) 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) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Aggregations

SSLHandshakeException (javax.net.ssl.SSLHandshakeException)84 IOException (java.io.IOException)26 Test (org.junit.Test)21 CertificateException (java.security.cert.CertificateException)17 URL (java.net.URL)15 SSLException (javax.net.ssl.SSLException)14 SocketException (java.net.SocketException)12 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)12 SSLProtocolException (javax.net.ssl.SSLProtocolException)10 Socket (java.net.Socket)8 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)8 SSLSocket (javax.net.ssl.SSLSocket)8 InputStream (java.io.InputStream)6 SSLSession (javax.net.ssl.SSLSession)6 Channel (io.netty.channel.Channel)5 InetSocketAddress (java.net.InetSocketAddress)5 SocketTimeoutException (java.net.SocketTimeoutException)5 ClosedChannelException (java.nio.channels.ClosedChannelException)5 Bootstrap (io.netty.bootstrap.Bootstrap)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4