Repository navigation
Description
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:
- JRE 1.8.0_74-b02
- macOS Sierra 10.12.1
- As I could not find a pre-compiled jar of the newest version (the link in the description yields no results), I'm using a 1.0.0-Snapshot from late January I found here: https://oss.sonatype.org/content/repositories/snapshots/org/conscrypt/conscrypt-openjdk/1.0.0-SNAPSHOT/
Steps to reproduce the problem:
(Using a SocketFactory obtained from the Conscrypt Provider)
- Create an SSLSocket over a normal TCP Socket (via
SSLSocketFactory.createSocket()) - Over the newly created SSLSocket, create another SSLSocket (via
SSLSocketFactory.createSocket()) - 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
FD- commentedon Feb 27, 2017
Is SSL over SSL not expected to work?
kruton commentedon Feb 27, 2017
It should work, but I haven't had a chance to look at this yet.
FD- commentedon Mar 2, 2017
I hope my sample project is of some help for testing and debugging!
bsidhom commentedon Apr 3, 2017
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:
(If I give the redirected URL, it prints the expected contents.)
Additional notes:
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.FD- commentedon Apr 4, 2017
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:
Exact error when running on Android 4.1:
For reproducing the issue, you can use the Android project in the master branch of my sample repository. Just use the
doSSLHandshakemethod from the conscrypt branch (+ add and import Conscrypt).nmittler commentedon Jun 20, 2017
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.FD- commentedon Jun 20, 2017
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)):
For the non-engine default socket, I get an exception that is very similar to the one from earlier versions:
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.
FD- commentedon Jun 20, 2017
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.
hypercube1024 commentedon Dec 4, 2017
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 moreI use the
conscrypt-openjdk-uber-1.0.0.RC13But the jdk8 default SSL Provider is OK.
nmittler commentedon Dec 4, 2017
@hypercube1024 which version of Conscrypt are you using? Is your app closing the socket asynchronously from the reading thread?
hypercube1024 commentedon Dec 5, 2017
@nmittler I use the conscrypt-openjdk-uber-1.0.0.RC13. My app does not close socket from the reading thread.
hypercube1024 commentedon Dec 5, 2017
@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
nmittler commentedon Dec 5, 2017
@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?
lukecwik commentedon Nov 12, 2018
This issue impacted Google Cloud Dataflow customers as well.
takidau commentedon Jan 23, 2019
For context on severity, this continues to be a problem for Cloud Dataflow customers.
nicowilliams-work commentedon Jul 23, 2020
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
intFD can't be given to Conscrypt, and so the JDBC'sSocketSslderivative 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
SocketSslinstance 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 theSocketSsl.)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.prbprbprb commentedon Jul 24, 2020
I kind of assumed this was working, for the
SSLEngine-based implementation ofSSLSocketat 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.
FD- commentedon Jul 24, 2020
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).
prbprbprb commentedon Jul 24, 2020
Thanks, @FD-! It sounds like @nicowilliams-work should definitely try with the engine-based socket.
medb commentedon Dec 28, 2022
Is there a plan to fix this issue in the next release?
prbprbprb commentedon Jan 1, 2023
In general SSL over SSL should work for every combination except the old file-descriptor-based
SslSocketimplementation (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.
medb commentedon Jan 4, 2023
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.