Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,19 @@ public class TNonblockingSSLSocket extends TNonblockingSocket implements SocketA

protected TNonblockingSSLSocket(String host, int port, int timeout, SSLContext sslContext)
throws IOException, TTransportException {
this(host, port, timeout, sslContext, null);
}

TNonblockingSSLSocket(
String host, int port, int timeout, SSLContext sslContext, String[] enabledProtocols)
throws IOException, TTransportException {
this(host, port, timeout, createClientEngine(sslContext, host, port, enabledProtocols));
}

private TNonblockingSSLSocket(String host, int port, int timeout, SSLEngine sslEngine)
throws IOException, TTransportException {
super(host, port, timeout);
sslEngine_ = sslContext.createSSLEngine(host, port);
sslEngine_.setUseClientMode(true);
SSLParameters sslParams = sslEngine_.getSSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
sslEngine_.setSSLParameters(sslParams);
sslEngine_ = sslEngine;

int appBufferSize = sslEngine_.getSession().getApplicationBufferSize();
int netBufferSize = sslEngine_.getSession().getPacketBufferSize();
Expand All @@ -77,6 +84,27 @@ protected TNonblockingSSLSocket(String host, int port, int timeout, SSLContext s
isHandshakeCompleted = false;
}

private static SSLEngine createClientEngine(
SSLContext sslContext, String host, int port, String[] enabledProtocols)
throws TTransportException {
SSLEngine engine = sslContext.createSSLEngine(host, port);
engine.setUseClientMode(true);
SSLParameters sslParams = engine.getSSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
engine.setSSLParameters(sslParams);
if (enabledProtocols != null) {
try {
engine.setEnabledProtocols(enabledProtocols);
} catch (IllegalArgumentException e) {
throw new TTransportException(
TTransportException.NOT_OPEN,
"Could not configure TLS protocols for " + host + ":" + port,
e);
}
}
return engine;
}

/** {@inheritDoc} */
@Override
public SelectionKey registerSelector(Selector selector, int interests) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.thrift.transport;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -124,20 +125,27 @@ private static TServerSocket createServer(
InetAddress ifAddress,
TSSLTransportParameters params)
throws TTransportException {
SSLServerSocket serverSocket = null;
try {
SSLServerSocket serverSocket =
(SSLServerSocket) factory.createServerSocket(port, 100, ifAddress);
serverSocket = (SSLServerSocket) factory.createServerSocket(port, 100, ifAddress);
serverSocket.setSoTimeout(timeout);
serverSocket.setNeedClientAuth(clientAuth);
if (params != null && params.cipherSuites != null) {
serverSocket.setEnabledCipherSuites(params.cipherSuites);
if (params != null) {
if (params.cipherSuites != null) {
serverSocket.setEnabledCipherSuites(params.cipherSuites);
}
String[] enabledProtocols = params.enabledProtocols;
if (enabledProtocols != null) {
serverSocket.setEnabledProtocols(enabledProtocols);
}
}
return new TServerSocket(
new TServerSocket.ServerSocketTransportArgs()
.serverSocket(serverSocket)
.clientTimeout(timeout));
} catch (Exception e) {
throw new TTransportException("Could not bind to port " + port, e);
closeSocketOnFailure(serverSocket, e);
throw new TTransportException("Could not create SSL server socket on port " + port, e);
}
}

Expand All @@ -154,7 +162,7 @@ private static TServerSocket createServer(
public static TSocket getClientSocket(String host, int port, int timeout)
throws TTransportException {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
return createClient(factory, host, port, timeout);
return createClient(factory, host, port, timeout, null);
}

/**
Expand Down Expand Up @@ -190,7 +198,7 @@ public static TSocket getClientSocket(
}

SSLContext ctx = createSSLContext(params);
return createClient(ctx.getSocketFactory(), host, port, timeout);
return createClient(ctx.getSocketFactory(), host, port, timeout, params.enabledProtocols);
}

/**
Expand Down Expand Up @@ -246,7 +254,7 @@ public static TNonblockingSocket getNonblockingClientSocket(
"Either one of the KeyStore or TrustStore must be set for SSLTransportParameters");
}
SSLContext ctx = createSSLContext(params);
return new TNonblockingSSLSocket(host, port, timeout, ctx);
return new TNonblockingSSLSocket(host, port, timeout, ctx, params.enabledProtocols);
}

private static SSLContext createSSLContext(TSSLTransportParameters params)
Expand Down Expand Up @@ -339,23 +347,41 @@ private static InputStream getStoreAsStream(String store) throws IOException {
}
}

private static TSocket createClient(SSLSocketFactory factory, String host, int port, int timeout)
private static TSocket createClient(
SSLSocketFactory factory, String host, int port, int timeout, String[] enabledProtocols)
throws TTransportException {
SSLSocket socket = null;
try {
SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
socket = (SSLSocket) factory.createSocket(host, port);
socket.setSoTimeout(timeout);
SSLParameters sslParams = socket.getSSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
socket.setSSLParameters(sslParams);
if (enabledProtocols != null) {
socket.setEnabledProtocols(enabledProtocols);
}
return new TSocket(socket);
} catch (TTransportException tte) {
closeSocketOnFailure(socket, tte);
throw tte;
} catch (Exception e) {
closeSocketOnFailure(socket, e);
throw new TTransportException(
TTransportException.NOT_OPEN, "Could not connect to " + host + " on port " + port, e);
}
}

private static void closeSocketOnFailure(Closeable socket, Exception failure) {
if (socket == null) {
return;
}
try {
socket.close();
} catch (IOException closeError) {
failure.addSuppressed(closeError);
}
}

/** A Class to hold all the SSL parameters */
public static class TSSLTransportParameters {
protected String protocol = "TLS";
Expand All @@ -374,12 +400,14 @@ public static class TSSLTransportParameters {
protected boolean isKeyStoreSet = false;
protected boolean isTrustStoreSet = false;

private String[] enabledProtocols;

public TSSLTransportParameters() {}

/**
* Create parameters specifying the protocol and cipher suites
*
* @param protocol The specific protocol (TLS/SSL) can be specified with versions
* @param protocol the algorithm passed to {@link SSLContext#getInstance(String)}
* @param cipherSuites
*/
public TSSLTransportParameters(String protocol, String[] cipherSuites) {
Expand All @@ -390,7 +418,7 @@ public TSSLTransportParameters(String protocol, String[] cipherSuites) {
* Create parameters specifying the protocol, cipher suites and if client authentication is
* required
*
* @param protocol The specific protocol (TLS/SSL) can be specified with versions
* @param protocol the algorithm passed to {@link SSLContext#getInstance(String)}
* @param cipherSuites
* @param clientAuth
*/
Expand All @@ -403,6 +431,21 @@ public TSSLTransportParameters(String protocol, String[] cipherSuites, boolean c
this.clientAuth = clientAuth;
}

/**
* Sets the protocols enabled on server sockets and on synchronous and asynchronous client
* transports created with these parameters.
*
* @param protocols protocol names supported by the JSSE provider, or {@code null} to use the
* provider defaults
* @throws IllegalArgumentException if the array is empty
*/
public void setEnabledProtocols(String... protocols) {
if (protocols != null && protocols.length == 0) {
throw new IllegalArgumentException("At least one protocol must be specified");
}
enabledProtocols = protocols == null ? null : Arrays.copyOf(protocols, protocols.length);
}

/**
* Set the keystore, password, certificate type and the store type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -64,6 +65,7 @@
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.junit.jupiter.api.Test;

public class TestTAsyncSSLProgress {
Expand All @@ -87,6 +89,45 @@ public void testSunJsseTls13TaskDoesNotDelayTimeoutOrOtherCalls() throws Excepti
checkTaskTimeout("TLSv1.3");
}

@Test
public void testNonblockingClientNegotiatesConfiguredProtocol() throws Exception {
TSSLTransportParameters params = new TSSLTransportParameters();
params.setTrustStore(
System.getProperty("javax.net.ssl.trustStore"),
System.getProperty("javax.net.ssl.trustStorePassword"));
params.setEnabledProtocols("TLSv1.2");
try (SSLServerSocket listener =
(SSLServerSocket)
SSLContext.getDefault()
.getServerSocketFactory()
.createServerSocket(0, 1, InetAddress.getByName("localhost"));
TNonblockingTransport transport =
TSSLTransportFactory.getNonblockingClientSocket(
"localhost", listener.getLocalPort(), 0, params)) {
listener.setEnabledProtocols(new String[] {"TLSv1.3", "TLSv1.2"});
listener.setSoTimeout(5000);
CompletableFuture<Void> serverDone =
serve(
listener,
socket -> {
readRequest(socket);
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.writeInt(1);
output.writeByte(42);
output.flush();
assertEquals("TLSv1.2", ((SSLSocket) socket).getSession().getProtocol());
});

TAsyncClientManager manager = new TAsyncClientManager();
try {
assertArrayEquals(new byte[] {42}, call(manager, transport, 5000).get(6, TimeUnit.SECONDS));
serverDone.get(5, TimeUnit.SECONDS);
} finally {
manager.stop();
}
}
}

private void checkTaskTimeout(String protocol) throws Exception {
assumeTrue(Security.getProvider("SunJSSE") != null);
assumeTrue(
Expand Down
Loading
Loading