This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 8dcddccc60 When using TLS-PSK with TLS 1.3 require peers to use PSK
8dcddccc60 is described below

commit 8dcddccc600c2cc315e649df2244bdc902e8801e
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Nov 19 21:01:26 2026 +0000

    When using TLS-PSK with TLS 1.3 require peers to use PSK
---
 .../tribes/transport/nio/LocalStrings.properties   |  1 +
 .../catalina/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 94914bbe20..08fdd1a825 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 8fb323d565..3e82ee8b12 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.io.OutputStream;
 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 dec997c544..5c7d90079b 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 {
 
@@ -84,6 +90,55 @@ public class TestTlsChannel {
         }
     }
 
+    /*
+     * 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<String> 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 aaf395afd9..2e388b7154 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 <pr>153</pr> by sainadh777. (markt)
       </docs>
+      <fix>
+        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)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Other">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to