Skip to content

SSLSocket via another SSLSocket not working #104

Open

Description

@FD-

It seems like creating an SSLSocket on top of another SSLSocket does not work using the Conscrypt SSL Provider.

When creating an SSLSocket over an existing SSLSocket via SSLSocketFactory.createSocket(), the call to SSLSocket.startHandshake() never returns.

The handshake for the underlying SSLSocket and the SSLSocket on top is done like shown in the following snippet. While it works fine for the first SSLSocket (where a normal tcp Socket is passed as socket parameter), it doesn’t return for the second SSLSocket (where the first SSLSocket is passed as socket parameter). Neither the line printing "Handshake finished" nor the catch block are reached.
The very same code works fine with the JRE default SSL Provider.

private Socket doSSLHandshake(Socket socket, String host, int port) throws IOException {
    // For easier debugging purpose, trust all certificates
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager(){
                public X509Certificate[] getAcceptedIssuers(){ return null; }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            }
    };

    System.out.println("Doing SSL handshake with " + host + ":" + port);

    try {
        Provider provider = new OpenSSLProvider();
        SSLContext sslContext = SSLContext.getInstance("TLS", provider);
        sslContext.init(null, trustAllCerts, new SecureRandom());
        SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(socket, host, port, true);
        sslSocket.startHandshake();
        System.out.println("Handshake finished");

        return sslSocket;
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        throw new IOException("Could not do SSL handshake: " + e);
    }
}

Configuration:

Steps to reproduce the problem:
(Using a SocketFactory obtained from the Conscrypt Provider)

  1. Create an SSLSocket over a normal TCP Socket (via SSLSocketFactory.createSocket())
  2. Over the newly created SSLSocket, create another SSLSocket (via SSLSocketFactory.createSocket())
  3. The second handshake will fail with the exception listed above

I have created a sample project that tries to connect to an HTTPS server via a Secure Web Proxy: https://github.com/FD-/SSLviaSSL/tree/conscrypt.

The issue might be related to Android issue #234291 (I actually came across the Conscrypt project by investigating this issue).
While the conscrypt branch in my repository contains details about the Conscrypt issue, the master branch describes the Android issue.

Expected result:
I'd expect creating an SSLSocket over an existing SSLSocket to work. The second handshake should return successfully.

Please let me know if you need further details on the issue!

Activity

  1. FD- commented on Feb 27, 2017

    @FD-
    Author

    Is SSL over SSL not expected to work?

  2. kruton commented on Feb 27, 2017

    @kruton
    Contributor

    It should work, but I haven't had a chance to look at this yet.

  3. modified the milestone: 1.0.1 on Feb 27, 2017
  4. FD- commented on Mar 2, 2017

    @FD-
    Author

    I hope my sample project is of some help for testing and debugging!

  5. bsidhom commented on Apr 3, 2017

    @bsidhom
    Contributor

    Can you confirm whether you're still seeing this problem? I thought it might be the cause of a different issue I've encountered, but was unable to reproduce this with your project:

    Doing SSL handshake with 127.0.0.1:10443
    Enabled protocols are: [TLSv1, TLSv1.1, TLSv1.2]
    Handshake finished
    Doing SSL handshake with google.com:443
    Enabled protocols are: [TLSv1, TLSv1.1, TLSv1.2]
    Handshake finished
    Have result: <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>301 Moved</TITLE></HEAD><BODY>
    <H1>301 Moved</H1>
    The document has moved
    <A HREF="https://www.google.com/">here</A>.
    </BODY></HTML>
    

    (If I give the redirected URL, it prints the expected contents.)

    Additional notes:

    • I had to add some missing imports to get it to compile.
    • I changed the target proxy host to 127.0.0.1, as I'm running client and server on the same machine. It's possible this somehow bypasses the issue you're seeing.
  6. FD- commented on Apr 4, 2017

    @FD-
    Author

    While I can confirm the issue no longer shows on macOS, unfortunately, it still shows on Android (both using Conscrypt 1.0.0 RC7 from Maven Central). I get an exception reporting the Socket to be closed when I call sslSocket.startHandshake() the second time. The code does exactly the same as the one in the conscrypt branch, so it definitely does not close the Socket.

    Exact error when running on Android 7.1:

    I/System.out: Doing SSL handshake with 192.168.178.85:10443
    I/System.out: Enabled protocols are: [TLSv1, TLSv1.1, TLSv1.2]
    I/System.out: Handshake finished
    I/System.out: Doing SSL handshake with google.com:443
    I/System.out: Enabled protocols are: [TLSv1, TLSv1.1, TLSv1.2]
    W/System.err: java.net.SocketException: Socket closed
    W/System.err:     at org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
    W/System.err:     at org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:351)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.doSSLHandshake(SecureWebProxyThread.java:160)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.run(SecureWebProxyThread.java:232)
    

    Exact error when running on Android 4.1:

    I/System.out: Doing SSL handshake with 192.168.178.85:10443
    I/System.out: Enabled protocols are: [TLSv1, TLSv1.1, TLSv1.2]
    I/System.out: Handshake finished
    I/System.out: Doing SSL handshake with google.com:443
    I/System.out: Enabled protocols are: [TLSv1, TLSv1.1, TLSv1.2]
    W/System.err: java.net.SocketException: Socket closed
    W/System.err:     at org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
    W/System.err:     at org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:351)
    W/System.err:     at org.conscrypt.PreKitKatPlatformOpenSSLSocketImplAdapter.startHandshake(PreKitKatPlatformOpenSSLSocketImplAdapter.java:324)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.doSSLHandshake(SecureWebProxyThread.java:160)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.run(SecureWebProxyThread.java:232)
    

    For reproducing the issue, you can use the Android project in the master branch of my sample repository. Just use the doSSLHandshake method from the conscrypt branch (+ add and import Conscrypt).

  7. nmittler commented on Jun 20, 2017

    @nmittler
    Contributor

    I've recently done some major refactoring of our socket classes. Probably worth seeing if this is still an issue with 1.0.0.RC8.

  8. FD- commented on Jun 20, 2017

    @FD-
    Author

    Thanks for the update! Unfortunately, it still doesn't work:

    I just tested it with the newest build from AppVeyor (seems to be from a few minutes ago).

    On a device running Android 7.0.1, I get this exception for the engine socket
    (Conscrypt.SocketFactories.setUseEngineSocketByDefault(true)):

    W/System.err: javax.net.ssl.SSLException: java.io.EOFException: Read error
    W/System.err:     at org.conscrypt.SSLUtils.toSSLException(SSLUtils.java:324)
    W/System.err:     at org.conscrypt.ConscryptEngine.convertException(ConscryptEngine.java:1011)
    W/System.err:     at org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:831)
    W/System.err:     at org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:657)
    W/System.err:     at org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:623)
    W/System.err:     at org.conscrypt.ConscryptEngineSocket$SSLInputStream.readInternal(ConscryptEngineSocket.java:652)
    W/System.err:     at org.conscrypt.ConscryptEngineSocket$SSLInputStream.read(ConscryptEngineSocket.java:617)
    W/System.err:     at org.conscrypt.ConscryptEngineSocket$SSLInputStream.readFromSocket(ConscryptEngineSocket.java:721)
    W/System.err:     at org.conscrypt.ConscryptEngineSocket$SSLInputStream.readInternal(ConscryptEngineSocket.java:693)
    W/System.err:     at org.conscrypt.ConscryptEngineSocket$SSLInputStream.read(ConscryptEngineSocket.java:617)
    W/System.err:     at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:287)
    W/System.err:     at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:350)
    W/System.err:     at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:179)
    W/System.err:     at java.io.InputStreamReader.read(InputStreamReader.java:184)
    W/System.err:     at java.io.BufferedReader.fill(BufferedReader.java:172)
    W/System.err:     at java.io.BufferedReader.readLine(BufferedReader.java:335)
    W/System.err:     at java.io.BufferedReader.readLine(BufferedReader.java:400)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.readResponseData(SecureWebProxyThread.java:213)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.run(SecureWebProxyThread.java:242)
    W/System.err: Caused by: java.io.EOFException: Read error
    W/System.err:     at org.conscrypt.NativeCrypto.ENGINE_SSL_read_direct(Native Method)
    W/System.err:     at org.conscrypt.SslWrapper.readDirectByteBuffer(SslWrapper.java:493)
    W/System.err:     at org.conscrypt.ConscryptEngine.readPlaintextData(ConscryptEngine.java:985)
    W/System.err:     at org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:785)
    W/System.err: 	... 16 more
    

    For the non-engine default socket, I get an exception that is very similar to the one from earlier versions:

    W/System.err: java.net.SocketException: Socket closed
    W/System.err:     at org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
    W/System.err:     at org.conscrypt.SslWrapper.doHandshake(SslWrapper.java:375)
    W/System.err:     at org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:217)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.doSSLHandshake(SecureWebProxyThread.java:165)
    W/System.err:     at com.bugreport.sslviassl.SecureWebProxyThread.run(SecureWebProxyThread.java:237)
    

    Let me know if I can help debug this in any way! You might also want to have a look at the sample project I linked in the description above. Maybe it's possible to build a test case from it.

  9. FD- commented on Jun 20, 2017

    @FD-
    Author

    I've now created a conscrypt-android branch of my sample project so you don't have to manually merge from the conscrypt and master branches.

  10. hypercube1024 commented on Dec 4, 2017

    @hypercube1024

    I also encounter this exception.
    Caused by: java.io.EOFException: Read error at org.conscrypt.NativeCrypto.ENGINE_SSL_read_direct(Native Method) at org.conscrypt.SslWrapper.readDirectByteBuffer(SslWrapper.java:494) at org.conscrypt.ConscryptEngine.readPlaintextDataDirect(ConscryptEngine.java:1060) at org.conscrypt.ConscryptEngine.readPlaintextData(ConscryptEngine.java:1044) at org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:838) ... 56 more
    I use the conscrypt-openjdk-uber-1.0.0.RC13
    But the jdk8 default SSL Provider is OK.

  11. nmittler commented on Dec 4, 2017

    @nmittler
    Contributor

    @hypercube1024 which version of Conscrypt are you using? Is your app closing the socket asynchronously from the reading thread?

  12. hypercube1024 commented on Dec 5, 2017

    @hypercube1024

    @nmittler I use the conscrypt-openjdk-uber-1.0.0.RC13. My app does not close socket from the reading thread.

  13. hypercube1024 commented on Dec 5, 2017

    @hypercube1024

    @nmittler When I call sslEngine.unwrap(src, dst) method, if the dst.remaining() is 0. the sslEngine will throw EOFException. But the JDK default sslEngine will return the result status is BUFFER_OVERFLOW. I can use that status to resize the dst buffer capacity

  14. nmittler commented on Dec 5, 2017

    @nmittler
    Contributor

    @hypercube1024 does your issue occur when wrapping another SSLSocket? If not, I suspect yours may be a separate issue.

    Can you post a PR with a new test that replicates the error?

  15. lukecwik commented on Nov 12, 2018

    @lukecwik

    This issue impacted Google Cloud Dataflow customers as well.

  16. takidau commented on Jan 23, 2019

    @takidau

    For context on severity, this continues to be a problem for Cloud Dataflow customers.

  17. nicowilliams-work commented on Jul 23, 2020

    @nicowilliams-work

    I believe this is the same issue I'm seeing where we're trying to use Conscrypt in an app that uses plain TLS (HTTPS) and also uses SQL Server, whose protocol (TDS) requires framing of TLS records, therefore the raw socket int FD can't be given to Conscrypt, and so the JDBC's SocketSsl derivative doesn't provide an FD.

    I suspect fixing this is just a small matter of adding missing code in Conscrypt's JNI bindings for BoringSSL to lookup and invoke the I/O methods on the SocketSsl instance when the FD isn't available. (Obviously it will be better for performance to use the FD if it's available, so that must remain supported when it is supported by the SocketSsl.)

    Does that sound about right?

    As a workaround we're considering the use of Providers.beginThreadProviderList()/Providers.endThreadProviderList() to bracket around accepting HTTPS connections. I'm not sure it will work, but it sounds like that API exists for this sort of thing (the only examples I can find have to do with using (and then not using) FIPS providers instead of the default.

  18. prbprbprb commented on Jul 24, 2020

    @prbprbprb
    Contributor

    I kind of assumed this was working, for the SSLEngine-based implementation of SSLSocket at least, which doesn't deal with raw file descriptors. Mainly because I came across the tests added in #243 but it sounds like that may still have problems especially on Android.

    If you're on OpenJDK can you try calling Conscrypt.setUseEngineSocketByDefault(true), or if you're developing for Android then try one of the Android 11 betas which should also use the engine based socket and report back any errors, please?

    Repro instructions would be helpful too for anyone still seeing this, although ideally not involving JDBC for simplicity... I guess the "connect via an HTTPS proxy" suggestion above is the simplest.

  19. FD- commented on Jul 24, 2020

    @FD-
    Author

    Sorry, I thought I'd reported this back when I worked on this project: At some point, the engine sockets started to work in a nested configuration in my (Android) project. As for test case, feel free to use the components from my repository, they include a self-contained demo (including a minimalistic Http Server, Http Proxy Server and stunnel-like tool).

  20. prbprbprb commented on Jul 24, 2020

    @prbprbprb
    Contributor

    Thanks, @FD-! It sounds like @nicowilliams-work should definitely try with the engine-based socket.

  21. medb commented on Dec 28, 2022

    @medb
    Member

    Is there a plan to fix this issue in the next release?

  22. prbprbprb commented on Jan 1, 2023

    @prbprbprb
    Contributor

    In general SSL over SSL should work for every combination except the old file-descriptor-based SslSocket implementation (which is no longer the default), including channels... I expanded the tests to cover this in #971.

    However this bug got left open as we never verified it was working with SQL Server's TDS protocol... If you have an example where that is failing, please let us know and we'll dig into it.

  23. medb commented on Jan 4, 2023

    @medb
    Member

    Thanks for explanation, we got new report for Conscrypt 2.5.2 about this issue. But looking deeper into it, it looks that it could be a false positive. We will dig into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions