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

Adds the possibility to change the USE_TLS_RESPONSE HTTP response #709

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion java/org/apache/tomcat/util/net/SecureNio2Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ protected int processSNI() throws IOException {
break;
case NON_SECURE:
netOutBuffer.clear();
netOutBuffer.put(TLSClientHelloExtractor.USE_TLS_RESPONSE);
netOutBuffer.put(extractor.getUseTlsResponse());
netOutBuffer.flip();
flush();
throw new IOException(sm.getString("channel.nio.ssl.foundHttp"));
Expand Down
2 changes: 1 addition & 1 deletion java/org/apache/tomcat/util/net/SecureNioChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ protected int processSNI() throws IOException {
break;
case NON_SECURE:
netOutBuffer.clear();
netOutBuffer.put(TLSClientHelloExtractor.USE_TLS_RESPONSE);
netOutBuffer.put(extractor.getUseTlsResponse());
netOutBuffer.flip();
flushOutbound();
throw new IOException(sm.getString("channel.nio.ssl.foundHttp"));
Expand Down
39 changes: 38 additions & 1 deletion java/org/apache/tomcat/util/net/TLSClientHelloExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class TLSClientHelloExtractor {
private static final Log log = LogFactory.getLog(TLSClientHelloExtractor.class);
private static final StringManager sm = StringManager.getManager(TLSClientHelloExtractor.class);

private final ByteBuffer netInBuffer;
private final ExtractorResult result;
private final List<Cipher> clientRequestedCiphers;
private final List<String> clientRequestedCipherNames;
Expand All @@ -60,6 +61,7 @@ public class TLSClientHelloExtractor {
"Bad Request\r\n" +
"This combination of host and port requires TLS.\r\n").getBytes(StandardCharsets.UTF_8);

public static ResponseCustomizer RESPONSE_CUSTOMIZER = new ResponseCustomizer() {};

/**
* Creates the instance of the parser and processes the provided buffer. The
Expand All @@ -71,6 +73,7 @@ public class TLSClientHelloExtractor {
* @throws IOException If the client hello message is malformed
*/
public TLSClientHelloExtractor(ByteBuffer netInBuffer) throws IOException {
this.netInBuffer = netInBuffer;
// Buffer is in write mode at this point. Record the current position so
// the buffer state can be restored at the end of this method.
int pos = netInBuffer.position();
Expand Down Expand Up @@ -245,6 +248,20 @@ public List<String> getClientRequestedProtocols() {
}
}

public byte[] getUseTlsResponse() {
// Buffer is in write mode at this point. Record the current position so
// the buffer state can be restored at the end of this method.
int pos = netInBuffer.position();
int limit = netInBuffer.limit();
try {
netInBuffer.flip();
return RESPONSE_CUSTOMIZER.customize(netInBuffer);
} finally {
// Whatever happens, return the buffer to its original state
netInBuffer.limit(limit);
netInBuffer.position(pos);
}
}

private static ExtractorResult handleIncompleteRead(ByteBuffer bb) {
if (bb.limit() == bb.capacity()) {
Expand Down Expand Up @@ -433,12 +450,32 @@ private static void readSupportedVersions(ByteBuffer bb, List<String> protocolNa
}
}


public enum ExtractorResult {
COMPLETE,
NOT_PRESENT,
UNDERFLOW,
NEED_READ,
NON_SECURE
}

/**
* Allows the HTTP response to be changed.
* <pre>
* {@code
* TLSClientHelloExtractor.RESPONSE_CUSTOMIZER = new ResponseCustomizer() {
* @Override
* public byte[] customize(ByteBuffer netInBuffer) {
* return ("HTTP/1.1 302 \r\n" +
* "Location: https://www.example.org/index.html\r\n" +
* "\r\n").getBytes(StandardCharsets.UTF_8);
* }
* };
* }
* </pre>
*/
public interface ResponseCustomizer {
default byte[] customize(ByteBuffer netInBuffer) {
return TLSClientHelloExtractor.USE_TLS_RESPONSE;
}
}
}