Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Ssl*Stream creation. #1109

Merged
merged 2 commits into from Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions common/src/main/java/org/conscrypt/ConscryptEngineSocket.java
Expand Up @@ -197,9 +197,8 @@ public final void startHandshake() throws IOException {
state = STATE_HANDSHAKE_STARTED;
handshakeStartedMillis = Platform.getMillisSinceBoot();
engine.beginHandshake();
// Ensure streams are created
getInputStream();
getOutputStream();
createInputStream();
createOutputStream();
} else {
// We've either started the handshake already or have been closed.
// Do nothing in both cases.
Expand All @@ -212,9 +211,6 @@ public final void startHandshake() throws IOException {

doHandshake();
}
} catch (SSLException e) {
close();
throw e;
} catch (IOException e) {
close();
throw e;
Expand Down Expand Up @@ -282,7 +278,10 @@ private void doHandshake() throws IOException {
@Override
public final InputStream getInputStream() throws IOException {
checkOpen();
return createInputStream();
}

private SSLInputStream createInputStream() {
synchronized (stateLock) {
if (in == null) {
in = new SSLInputStream();
Expand All @@ -294,7 +293,10 @@ public final InputStream getInputStream() throws IOException {
@Override
public final OutputStream getOutputStream() throws IOException {
checkOpen();
return createOutputStream();
}

private SSLOutputStream createOutputStream() {
synchronized (stateLock) {
if (out == null) {
out = new SSLOutputStream();
Expand Down
Expand Up @@ -27,6 +27,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
Expand Down Expand Up @@ -59,7 +60,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -1028,6 +1028,119 @@ public void handshakeCompleted(HandshakeCompletedEvent e) {
}
pair.close();
}

@Test
public void test_SSLSocket_ShutdownInput() throws Exception {
// Fdsocket throws SslException rather than returning EOF after input shutdown
// on Windows, but we won't be fixing it as that implementation is already deprecated.
assumeFalse("Skipping shutdownInput() test on Windows", isWindows());

final TestSSLContext c = new TestSSLContext.Builder()
.clientProtocol(clientVersion)
.serverProtocol(serverVersion)
.build();
byte[] buffer = new byte[1];
TestSSLSocketPair pair = TestSSLSocketPair.create(c).connect();
SSLSocket server = pair.server;
SSLSocket client = pair.client;
assertFalse(server.isClosed());
assertFalse(client.isClosed());
InputStream input = client.getInputStream();
client.shutdownInput();
assertFalse(client.isClosed());
assertFalse(server.isClosed());
// Shutdown after shutdown is not OK
SocketException exception = assertThrows(SocketException.class, client::shutdownInput);
assertTrue(exception.getMessage().contains("already shutdown"));

// The following operations should succeed, same as after close()
HandshakeCompletedListener listener = e -> { };
client.addHandshakeCompletedListener(listener);
assertNotNull(client.getEnabledCipherSuites());
assertNotNull(client.getEnabledProtocols());
client.getEnableSessionCreation();
client.getNeedClientAuth();
assertNotNull(client.getSession());
assertNotNull(client.getSSLParameters());
assertNotNull(client.getSupportedProtocols());
client.getUseClientMode();
client.getWantClientAuth();
client.removeHandshakeCompletedListener(listener);
client.setEnabledCipherSuites(new String[0]);
client.setEnabledProtocols(new String[0]);
client.setEnableSessionCreation(false);
client.setNeedClientAuth(false);
client.setSSLParameters(client.getSSLParameters());
client.setWantClientAuth(false);

// The following operations should succeed, unlike after close()
client.startHandshake();
client.getInputStream();
client.getOutputStream();
assertEquals(-1, input.read());
assertEquals(-1, input.read(buffer));
assertEquals(0, input.available());

pair.close();
}

@Test
public void test_SSLSocket_ShutdownOutput() throws Exception {
final TestSSLContext c = new TestSSLContext.Builder()
.clientProtocol(clientVersion)
.serverProtocol(serverVersion)
.build();
byte[] buffer = new byte[1];
TestSSLSocketPair pair = TestSSLSocketPair.create(c).connect();
SSLSocket server = pair.server;
SSLSocket client = pair.client;
assertFalse(server.isClosed());
assertFalse(client.isClosed());
OutputStream output = client.getOutputStream();
client.shutdownOutput();
assertFalse(client.isClosed());
assertFalse(server.isClosed());
// Shutdown after shutdown is not OK
SocketException exception = assertThrows(SocketException.class, client::shutdownOutput);
assertTrue(exception.getMessage().contains("already shutdown"));

// The following operations should succeed, same as after close()
HandshakeCompletedListener listener = e -> { };
client.addHandshakeCompletedListener(listener);
assertNotNull(client.getEnabledCipherSuites());
assertNotNull(client.getEnabledProtocols());
client.getEnableSessionCreation();
client.getNeedClientAuth();
assertNotNull(client.getSession());
assertNotNull(client.getSSLParameters());
assertNotNull(client.getSupportedProtocols());
client.getUseClientMode();
client.getWantClientAuth();
client.removeHandshakeCompletedListener(listener);
client.setEnabledCipherSuites(new String[0]);
client.setEnabledProtocols(new String[0]);
client.setEnableSessionCreation(false);
client.setNeedClientAuth(false);
client.setSSLParameters(client.getSSLParameters());
client.setWantClientAuth(false);

// The following operations should succeed, unlike after close()
client.startHandshake();
client.getInputStream();
client.getOutputStream();

// Any output should fail
try {
output.write(buffer);
fail();
} catch (SocketException | SSLException expected) {
// Expected.
// SocketException is correct but the old fd-based implementation
// throws SSLException, and it's not worth changing it at this late stage.
}
pair.close();
prbprbprb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* b/3350645 Test to confirm that an SSLSocket.close() performing
* an SSL_shutdown does not throw an IOException if the peer
Expand Down