Skip to content
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.
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 @@ -71,6 +71,8 @@ private enum State {
CLOSING
}

private static final String TLS13 = "TLSv1.3";

private final String channelId;
private final SSLEngine sslEngine;
private final SelectionKey key;
Expand Down Expand Up @@ -449,7 +451,7 @@ private void handshakeFinished() throws IOException {
if (netWriteBuffer.hasRemaining())
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
else {
state = sslEngine.getSession().getProtocol().equals("TLSv1.3") ? State.POST_HANDSHAKE : State.READY;
state = sslEngine.getSession().getProtocol().equals(TLS13) ? State.POST_HANDSHAKE : State.READY;
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
SSLSession session = sslEngine.getSession();
log.debug("SSL handshake completed successfully with peerHost '{}' peerPort {} peerPrincipal '{}' cipherSuite '{}'",
Expand Down Expand Up @@ -585,10 +587,11 @@ public int read(ByteBuffer dst) throws IOException {
throw e;
}
netReadBuffer.compact();
// handle ssl renegotiation.
// reject renegotiation if TLS < 1.3, key updates for TLS 1.3 are allowed
if (unwrapResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
unwrapResult.getHandshakeStatus() != HandshakeStatus.FINISHED &&
unwrapResult.getStatus() == Status.OK) {
unwrapResult.getStatus() == Status.OK &&
!sslEngine.getSession().getProtocol().equals(TLS13)) {
log.error("Renegotiation requested, but it is not supported, channelId {}, " +
"appReadBuffer pos {}, netReadBuffer pos {}, netWriteBuffer pos {} handshakeStatus {}", channelId,
appReadBuffer.position(), netReadBuffer.position(), netWriteBuffer.position(), unwrapResult.getHandshakeStatus());
Expand Down Expand Up @@ -706,9 +709,12 @@ public int write(ByteBuffer src) throws IOException {
SSLEngineResult wrapResult = sslEngine.wrap(src, netWriteBuffer);
netWriteBuffer.flip();

//handle ssl renegotiation
if (wrapResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING && wrapResult.getStatus() == Status.OK)
// reject renegotiation if TLS < 1.3, key updates for TLS 1.3 are allowed
if (wrapResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
wrapResult.getStatus() == Status.OK &&
!sslEngine.getSession().getProtocol().equals(TLS13)) {
throw renegotiationException();
}

if (wrapResult.getStatus() == Status.OK) {
written += wrapResult.bytesConsumed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public void tearDown() throws Exception {
}
}

public SecurityProtocol securityProtocol() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not used.

return SecurityProtocol.PLAINTEXT;
}

protected Map<String, Object> clientConfigs() {
return new HashMap<>();
}
Expand Down Expand Up @@ -1015,7 +1011,6 @@ public void testChannelCloseWhileProcessingReceives() throws Exception {

private String blockingRequest(String node, String s) throws IOException {
selector.send(createSend(node, s));
selector.poll(1000L);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed since it's redundant.

while (true) {
selector.poll(1000L);
for (NetworkReceive receive : selector.completedReceives())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.kafka.common.network;

import java.nio.channels.SelectionKey;
import java.security.GeneralSecurityException;
import javax.net.ssl.SSLEngine;

import org.apache.kafka.common.config.SecurityConfig;
Expand All @@ -43,11 +44,9 @@
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.security.Security;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -61,7 +60,7 @@
/**
* A set of tests for the selector. These use a test harness that runs a simple socket server that echos back responses.
*/
public class SslSelectorTest extends SelectorTest {
public abstract class SslSelectorTest extends SelectorTest {

private Map<String, Object> sslClientConfigs;

Expand All @@ -73,34 +72,30 @@ public void setUp() throws Exception {
this.server = new EchoServer(SecurityProtocol.SSL, sslServerConfigs);
this.server.start();
this.time = new MockTime();
sslClientConfigs = TestSslUtils.createSslConfig(false, false, Mode.CLIENT, trustStoreFile, "client");
sslClientConfigs = createSslClientConfigs(trustStoreFile);
LogContext logContext = new LogContext();
this.channelBuilder = new SslChannelBuilder(Mode.CLIENT, null, false, logContext);
this.channelBuilder.configure(sslClientConfigs);
this.metrics = new Metrics();
this.selector = new Selector(5000, metrics, time, "MetricGroup", channelBuilder, logContext);
}

protected abstract Map<String, Object> createSslClientConfigs(File trustStoreFile) throws GeneralSecurityException, IOException;

@AfterEach
public void tearDown() throws Exception {
this.selector.close();
this.server.close();
this.metrics.close();
}

@Override
public SecurityProtocol securityProtocol() {
return SecurityProtocol.PLAINTEXT;
}

@Override
protected Map<String, Object> clientConfigs() {
return sslClientConfigs;
}

@Test
public void testConnectionWithCustomKeyManager() throws Exception {

TestProviderCreator testProviderCreator = new TestProviderCreator();

int requestSize = 100 * 1024;
Expand Down Expand Up @@ -249,35 +244,6 @@ void pollSelectionKeys(Set<SelectionKey> selectionKeys, boolean isImmediatelyCon
verifySelectorEmpty();
}

/**
* Renegotiation is not supported since it is potentially unsafe and it has been removed in TLS 1.3
*/
@Test
public void testRenegotiationFails() throws Exception {
String node = "0";
// create connections
InetSocketAddress addr = new InetSocketAddress("localhost", server.port);
selector.connect(node, addr, BUFFER_SIZE, BUFFER_SIZE);

// send echo requests and receive responses
while (!selector.isChannelReady(node)) {
selector.poll(1000L);
}
selector.send(createSend(node, node + "-" + 0));
selector.poll(0L);
server.renegotiate();
selector.send(createSend(node, node + "-" + 1));
long expiryTime = System.currentTimeMillis() + 2000;

List<String> disconnected = new ArrayList<>();
while (!disconnected.contains(node) && System.currentTimeMillis() < expiryTime) {
selector.poll(10);
disconnected.addAll(selector.disconnected().keySet());
}
assertTrue(disconnected.contains(node), "Renegotiation should cause disconnection");

}

@Override
@Test
public void testMuteOnOOM() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kafka.common.network;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.kafka.common.config.SslConfigs;
import org.apache.kafka.test.TestSslUtils;
import org.junit.jupiter.api.Test;

public class Tls12SelectorTest extends SslSelectorTest {

@Override
protected Map<String, Object> createSslClientConfigs(File trustStoreFile)
throws GeneralSecurityException, IOException {
Map<String, Object> configs = TestSslUtils.createSslConfig(false, false, Mode.CLIENT,
trustStoreFile, "client");
configs.put(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG, asList("TLSv1.2"));
return configs;
}

/**
* Renegotiation is not supported when TLS 1.2 is used (renegotiation was removed from TLS 1.3)
*/
@Test
public void testRenegotiationFails() throws Exception {
String node = "0";
// create connections
InetSocketAddress addr = new InetSocketAddress("localhost", server.port);
selector.connect(node, addr, BUFFER_SIZE, BUFFER_SIZE);

// send echo requests and receive responses
while (!selector.isChannelReady(node)) {
selector.poll(1000L);
}
selector.send(createSend(node, node + "-" + 0));
selector.poll(0L);
server.renegotiate();
selector.send(createSend(node, node + "-" + 1));
long expiryTime = System.currentTimeMillis() + 2000;

List<String> disconnected = new ArrayList<>();
while (!disconnected.contains(node) && System.currentTimeMillis() < expiryTime) {
selector.poll(10);
disconnected.addAll(selector.disconnected().keySet());
}
assertTrue(disconnected.contains(node), "Renegotiation should cause disconnection");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kafka.common.network;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.kafka.common.config.SslConfigs;
import org.apache.kafka.test.TestSslUtils;
import org.apache.kafka.test.TestUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

@EnabledForJreRange(min = JRE.JAVA_11) // TLS 1.3 is only supported with Java 11 and newer
public class Tls13SelectorTest extends SslSelectorTest {

@Override
protected Map<String, Object> createSslClientConfigs(File trustStoreFile) throws GeneralSecurityException, IOException {
Map<String, Object> configs = TestSslUtils.createSslConfig(false, false, Mode.CLIENT,
trustStoreFile, "client");
configs.put(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG, asList("TLSv1.3"));
return configs;
}

/**
* TLS 1.3 has a post-handshake key and IV update, which will update the sending and receiving keys
* for one side of the connection.
*
* Key Usage Limits will trigger an update when the algorithm limits are reached, but the default
* value is too large (2^37 bytes of plaintext data) for a unit test. This value can be overridden
* via the security property `jdk.tls.keyLimits`, but that's also difficult to achieve in a unit
* test.
*
* Applications can also trigger an update by calling `SSLSocket.startHandshake()` or
* `SSLEngine.beginHandshake()` (this would trigger `renegotiation` with TLS 1.2) and that's the
* approach we take here.
*/
@Test
public void testKeyUpdate() throws Exception {
String node = "0";
// create connections
InetSocketAddress addr = new InetSocketAddress("localhost", server.port);
selector.connect(node, addr, BUFFER_SIZE, BUFFER_SIZE);
// send echo requests and receive responses
while (!selector.isChannelReady(node)) {
selector.poll(1000L);
}
selector.send(createSend(node, node + "-" + 0));
selector.poll(0L);
server.renegotiate();
selector.send(createSend(node, node + "-" + 1));
List<NetworkReceive> received = new ArrayList<>();
TestUtils.waitForCondition(() -> {
try {
selector.poll(1000L);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (NetworkReceive receive : selector.completedReceives()) {
if (receive.source().equals(node))
received.add(receive);
}
return received.size() == 2;
}, "Expected two receives, got " + received.size());

assertEquals(asList("0-0", "0-1"), received.stream().map(this::asString).collect(Collectors.toList()));
}
}