From 8dac862e720a9b2ce681ee160e87666aba796192 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Thu, 19 Nov 2026 21:01:26 +0000 Subject: [PATCH] When using TLS-PSK with TLS 1.3 require peers to use PSK --- .../transport/nio/LocalStrings.properties | 1 + .../tribes/transport/nio/TlsChannel.java | 33 +++++++++++ .../tribes/transport/nio/TestTlsChannel.java | 55 +++++++++++++++++++ webapps/docs/changelog.xml | 5 ++ 4 files changed, 94 insertions(+) diff --git a/java/org/apache/catalina/tribes/transport/nio/LocalStrings.properties b/java/org/apache/catalina/tribes/transport/nio/LocalStrings.properties index 94914bbe205e..08fdd1a82580 100644 --- a/java/org/apache/catalina/tribes/transport/nio/LocalStrings.properties +++ b/java/org/apache/catalina/tribes/transport/nio/LocalStrings.properties @@ -73,3 +73,4 @@ pooledParallelSender.unable.open=Unable to open NIO selector. pooledParallelSender.unable.retrieveSender=Unable to retrieve a sender from the sender pool pooledParallelSender.unable.retrieveSender.interrupted=Unable to retrieve a data sender, the thread was interrupted while waiting. pooledParallelSender.unable.retrieveSender.timeout=Unable to retrieve a data sender, time out([{0}] ms) error. +tlsChannel.pskNotUsed=The TLS handshake completed with a peer certificate rather than the pre-shared key. The connection has been rejected. diff --git a/java/org/apache/catalina/tribes/transport/nio/TlsChannel.java b/java/org/apache/catalina/tribes/transport/nio/TlsChannel.java index b5fd79b9cba6..642586fa3aa9 100644 --- a/java/org/apache/catalina/tribes/transport/nio/TlsChannel.java +++ b/java/org/apache/catalina/tribes/transport/nio/TlsChannel.java @@ -23,14 +23,21 @@ import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; +import java.security.cert.Certificate; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngineResult; import javax.net.ssl.SSLEngineResult.HandshakeStatus; import javax.net.ssl.SSLEngineResult.Status; +import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.SSLPeerUnverifiedException; + +import org.apache.catalina.tribes.util.StringManager; final class TlsChannel implements ByteChannel { + private static final StringManager sm = StringManager.getManager(TlsChannel.class); + private static final ByteBuffer EMPTY = ByteBuffer.allocate(0); private final Socket socket; @@ -53,6 +60,32 @@ final class TlsChannel implements ByteChannel { applicationInput.flip(); engine.beginHandshake(); handshake(); + verifyPskAuthentication(); + } + + /* + * Tribes authenticates peers solely through the pre-shared key. In TLS 1.3 the PSK is only offered and the peer is + * free to ignore it and fall back to a certificate based handshake. Certificate verification is not configured + * so such a peer would be accepted. A handshake authenticated with a PSK never includes a peer certificate so + * treat the presence of one as evidence that the PSK was not used and reject the connection. + */ + private void verifyPskAuthentication() throws IOException { + Certificate[] peerCertificates; + try { + peerCertificates = engine.getSession().getPeerCertificates(); + } catch (SSLPeerUnverifiedException e) { + // No peer certificate. Expected when the PSK was used. + return; + } + if (peerCertificates != null && peerCertificates.length > 0) { + try { + engine.closeOutbound(); + engine.closeInbound(); + } catch (IOException ignore) { + // Closing anyway + } + throw new SSLHandshakeException(sm.getString("tlsChannel.pskNotUsed")); + } } private void handshake() throws IOException { diff --git a/test/org/apache/catalina/tribes/transport/nio/TestTlsChannel.java b/test/org/apache/catalina/tribes/transport/nio/TestTlsChannel.java index ab27c711659b..62f0d4ab87e4 100644 --- a/test/org/apache/catalina/tribes/transport/nio/TestTlsChannel.java +++ b/test/org/apache/catalina/tribes/transport/nio/TestTlsChannel.java @@ -16,21 +16,27 @@ */ package org.apache.catalina.tribes.transport.nio; +import java.io.FileInputStream; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.StandardCharsets; +import java.security.KeyStore; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLHandshakeException; import org.junit.Assert; import org.junit.Assume; import org.junit.Test; import org.apache.catalina.tribes.transport.TribesSslContext; +import org.apache.tomcat.util.net.TesterSupport; public class TestTlsChannel { @@ -85,6 +91,55 @@ private static void doTestPskRoundTrip(TribesSslContext context, String protocol } } + /* + * A TLS 1.3 server is free to ignore the offered PSK and authenticate with a certificate. The client must not + * complete the handshake (and therefore must not send any data) in that case. + */ + @Test + public void testServerIgnoresPskTls13() throws Exception { + KeyStore keyStore = KeyStore.getInstance("JKS"); + try (FileInputStream fis = new FileInputStream(TesterSupport.LOCALHOST_RSA_JKS)) { + keyStore.load(fis, TesterSupport.JKS_PASS.toCharArray()); + } + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(keyStore, TesterSupport.JKS_PASS.toCharArray()); + SSLContext serverContext = SSLContext.getInstance("TLSv1.3"); + serverContext.init(kmf.getKeyManagers(), null, null); + + try (TribesSslContext context = createContext("SHA256", "TLSv1.3"); + ServerSocketChannel server = ServerSocketChannel.open()) { + server.bind(new InetSocketAddress("localhost", 0)); + // The JSSE server knows nothing of the PSK so will present its certificate + FutureTask serverTask = new FutureTask<>(() -> { + SSLEngine engine = serverContext.createSSLEngine(); + engine.setUseClientMode(false); + try (SocketChannel socket = server.accept()) { + socket.socket().setSoTimeout(5000); + try (TlsChannel tls = new TlsChannel(socket.socket(), engine)) { + // Only reached if the client wrongly proceeded + return read(tls); + } catch (Exception | AssertionError e) { + // Expected. The client aborts the connection so the server sees an alert or EOF. + // (read() reports EOF via an AssertionError) + return null; + } + } + }); + new Thread(serverTask).start(); + + try (SocketChannel socket = SocketChannel.open(server.getLocalAddress())) { + socket.socket().setSoTimeout(5000); + try (TlsChannel tls = new TlsChannel(socket.socket(), context.createClientEngine())) { + tls.write(ByteBuffer.wrap("secret".getBytes(StandardCharsets.UTF_8))); + Assert.fail("Handshake with a server that ignored the PSK should have been rejected"); + } catch (SSLHandshakeException expected) { + // Expected + } + } + Assert.assertNull("The server must not receive any data", serverTask.get(10, TimeUnit.SECONDS)); + } + } + @Test public void testMultipleRecords() throws Exception { byte[] payload = new byte[128 * 1024]; diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 97a1065dae30..2ac5755f0c2a 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -267,6 +267,11 @@ Tribes, including failure detection, security and troubleshooting guidance. Based on pull request 153 by sainadh777. (markt) + + When using TLS-PSK with TLS 1.3 ensure that a node does not accept a + connection from a peer that presents a certificate rather than the PSK. + (markt) +