diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java index 1f44fc182e..a92b907e4a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java @@ -125,6 +125,12 @@ public class MySqlProtocolHandler { // permanently one behind the client's for the rest of the connection phase. sequenceOffset = 1; clientResponseRaw[3] = (byte) (clientResponseRaw[3] - sequenceOffset); + // Real clients keep CLIENT_SSL set in the HandshakeResponse they send over the + // now-encrypted stream. The backend connection stays plaintext, and a MySQL server + // treats any first packet with CLIENT_SSL as an SSLRequest — it would sit waiting for + // a TLS ClientHello that never comes while the proxy waits for its auth verdict, + // deadlocking the connection. Clear the bit (the mirror of forceClientSslCapability). + clearClientSslCapability(clientResponseRaw); } else { clientResponseRaw = clientFirstRaw; if (clientResponseRaw.length < 40) { @@ -259,6 +265,15 @@ public class MySqlProtocolHandler { } } + /** + * Clears the {@code CLIENT_SSL} bit in a raw HandshakeResponse41 packet's capability flags. + * The client capabilities are the first 4 payload bytes, little-endian; {@code CLIENT_SSL} + * (0x0800) lives in the second byte. + */ + private static void clearClientSslCapability(byte[] raw) { + raw[5] &= (byte) ~(CLIENT_SSL >> 8); + } + /** * Sets the {@code CLIENT_SSL} bit in a raw Handshake V10 packet's capability flags, so the * client is offered TLS even when the (plaintext) backend didn't advertise it. Walks the same diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandlerTest.java index 141f4ed131..a17cd7629d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandlerTest.java @@ -153,8 +153,11 @@ class MySqlProtocolHandlerTest { byte[] scramble = scrambleNativePassword("secret", nonce); // Real clients send this at sequence 2 after the TLS upgrade — the proxy must - // rewrite it to 1 before forwarding to the (plaintext-only) backend. - byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2); + // rewrite it to 1 before forwarding to the (plaintext-only) backend. They also + // keep CLIENT_SSL set in it; a real backend would treat a first packet carrying + // that bit as an SSLRequest and hang waiting for a TLS handshake. + byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2, + CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_SSL); tlsOut.write(response); tlsOut.flush(); @@ -178,6 +181,11 @@ class MySqlProtocolHandlerTest { assertEquals((byte) 1, backendResponseSeq.get(), "backend never saw the SSLRequest, so it must receive the real response at sequence 1"); assertEquals("admin", extractUsername(backendResponsePayload.get())); + int backendCaps = (backendResponsePayload.get()[0] & 0xFF) + | ((backendResponsePayload.get()[1] & 0xFF) << 8); + assertEquals(0, backendCaps & CLIENT_SSL, + "the plaintext backend must not see CLIENT_SSL, or it treats the response as an " + + "SSLRequest and waits forever for a TLS handshake"); } @Test @@ -238,7 +246,8 @@ class MySqlProtocolHandlerTest { OutputStream tlsOut = sslClient.getOutputStream(); byte[] scramble = scrambleNativePassword("secret", nonce); - byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2); + byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2, + CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_SSL); tlsOut.write(response); tlsOut.flush(); @@ -352,8 +361,14 @@ class MySqlProtocolHandlerTest { private static byte[] buildHandshakeResponse41(String username, byte[] scramble, byte sequence) throws IOException { + return buildHandshakeResponse41(username, scramble, sequence, + CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION); + } + + private static byte[] buildHandshakeResponse41(String username, byte[] scramble, byte sequence, + int capabilities) throws IOException { ByteArrayOutputStream payload = new ByteArrayOutputStream(); - writeInt32LE(payload, CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION); + writeInt32LE(payload, capabilities); writeInt32LE(payload, 0); // max packet size payload.write(0x21); // charset payload.write(new byte[23]); // reserved