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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new bb9a5945adc5 CAMEL-25650: camel-api - Add 
endpointIdentificationAlgorithm to SSLContextClientParameters (#31066)
bb9a5945adc5 is described below

commit bb9a5945adc5371f21b7b3f78baa4a39297ea6ac
Author: Ayush Kulshreshtha <[email protected]>
AuthorDate: Mon Nov 30 14:01:49 2026 +0530

    CAMEL-25650: camel-api - Add endpointIdentificationAlgorithm to 
SSLContextClientParameters (#31066)
    
    * CAMEL-25650: camel-api - Add endpointIdentificationAlgorithm to 
SSLContextClientParameters
    * CAMEL-25650: Correct which connections fail host name verification, and 
add @since 4.24
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../support/jsse/BaseSSLContextParameters.java     |  17 ++
 .../support/jsse/SSLContextClientParameters.java   |  44 ++++++
 ...ClientParametersEndpointIdentificationTest.java | 172 +++++++++++++++++++++
 .../ROOT/pages/camel-configuration-utilities.adoc  |  14 ++
 4 files changed, 247 insertions(+)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/BaseSSLContextParameters.java
 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/BaseSSLContextParameters.java
index e7b9988f1ce3..e4d3c240859e 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/BaseSSLContextParameters.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/BaseSSLContextParameters.java
@@ -251,6 +251,14 @@ public abstract class BaseSSLContextParameters extends 
JsseParameters {
      */
     private @Nullable String sessionTimeout;
 
+    /**
+     * Returns the endpoint identification algorithm to apply to the client 
sockets that the {@code SSLSocketFactory}
+     * creates, or {@code null} to leave it unset. Only {@link 
SSLContextClientParameters} sets one.
+     */
+    protected @Nullable String getSocketEndpointIdentificationAlgorithm() {
+        return null;
+    }
+
     protected List<SNIServerName> getSNIHostNames() {
         return Collections.emptyList();
     }
@@ -834,6 +842,8 @@ public abstract class BaseSSLContextParameters extends 
JsseParameters {
 
         final boolean allowPassthrough = getAllowPassthrough();
 
+        final String endpointIdentificationAlgorithm = 
getSocketEndpointIdentificationAlgorithm();
+
         //////
 
         Configurer<SSLSocket> sslSocketConfigurer = new Configurer<>() {
@@ -847,6 +857,13 @@ public abstract class BaseSSLContextParameters extends 
JsseParameters {
                     socket.setSSLParameters(sslParameters);
                 }
 
+                if (endpointIdentificationAlgorithm != null) {
+                    // host name verification: the JDK checks the certificate 
of the server against the host of the socket
+                    SSLParameters sslParameters = socket.getSSLParameters();
+                    
sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm);
+                    socket.setSSLParameters(sslParameters);
+                }
+
                 Collection<String> filteredCipherSuites = 
BaseSSLContextParameters.this
                         .filter(enabledCipherSuites, 
Arrays.asList(socket.getSSLParameters().getCipherSuites()),
                                 Arrays.asList(socket.getEnabledCipherSuites()),
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextClientParameters.java
 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextClientParameters.java
index c1dd4a72d3f2..6ede026d0000 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextClientParameters.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextClientParameters.java
@@ -29,6 +29,7 @@ import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLServerSocketFactory;
 
+import org.jspecify.annotations.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,6 +45,11 @@ public class SSLContextClientParameters extends 
BaseSSLContextParameters {
      */
     private final List<String> sniHostNames = new ArrayList<>();
 
+    /**
+     * The endpoint identification algorithm, such as HTTPS or LDAPS
+     */
+    private @Nullable String endpointIdentificationAlgorithm;
+
     public void addAllSniHostNames(List<String> sniHostNames) {
         Objects.requireNonNull(sniHostNames, "sniHostNames");
         this.sniHostNames.addAll(sniHostNames);
@@ -54,6 +60,42 @@ public class SSLContextClientParameters extends 
BaseSSLContextParameters {
         this.sniHostNames.add(sniHostName);
     }
 
+    /**
+     * Returns the endpoint identification algorithm that client sockets use 
to check that the certificate of the server
+     * matches the host they connect to, or {@code null} if it is not set.
+     *
+     * @since 4.24
+     */
+    public @Nullable String getEndpointIdentificationAlgorithm() {
+        return endpointIdentificationAlgorithm;
+    }
+
+    /**
+     * Sets the endpoint identification algorithm, such as {@code HTTPS} or 
{@code LDAPS}, that the client uses to check
+     * that the certificate of the server matches the host it connects to 
(host name verification). It is applied to the
+     * {@code SSLSocket} instances that the {@code SSLSocketFactory} of the 
context creates, which are client sockets.
+     * Unset by default, which keeps the behavior of the JDK: no host name 
verification.
+     * <p>
+     * The JDK checks the host name that the socket connects to: the host 
given to {@code createSocket}, or the host of
+     * the {@code InetSocketAddress} given to {@code connect}. Connecting to 
an IP address, or to an {@code InetAddress}
+     * without a host name, fails the handshake unless the certificate lists 
that address. An {@code SSLEngine} starts
+     * in server mode and is switched to client mode by the component, so this 
setting is not applied to engines: a
+     * component that uses an {@code SSLEngine} applies it to the {@code 
SSLParameters} of the engine once it is in
+     * client mode, as for its own host name verification option.
+     *
+     * @param endpointIdentificationAlgorithm the algorithm, or {@code null} 
to not verify the host name
+     * @since                                 4.24
+     */
+    public void setEndpointIdentificationAlgorithm(@Nullable String 
endpointIdentificationAlgorithm) {
+        this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
+    }
+
+    @Override
+    protected @Nullable String getSocketEndpointIdentificationAlgorithm() {
+        // the algorithm may use a property placeholder
+        return parsePropertyValue(endpointIdentificationAlgorithm);
+    }
+
     @Override
     protected List<SNIServerName> getSNIHostNames() {
         // the host names may use property placeholders
@@ -123,6 +165,8 @@ public class SSLContextClientParameters extends 
BaseSSLContextParameters {
         builder.append(getSecureSocketProtocolsFilter());
         builder.append(", getSessionTimeout()=");
         builder.append(getSessionTimeout());
+        builder.append(", getEndpointIdentificationAlgorithm()=");
+        builder.append(getEndpointIdentificationAlgorithm());
         builder.append("]");
         return builder.toString();
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextClientParametersEndpointIdentificationTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextClientParametersEndpointIdentificationTest.java
new file mode 100644
index 000000000000..1661c410b131
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextClientParametersEndpointIdentificationTest.java
@@ -0,0 +1,172 @@
+/*
+ * 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.camel.support.jsse;
+
+import java.util.Properties;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLHandshakeException;
+import javax.net.ssl.SSLServerSocket;
+import javax.net.ssl.SSLSocket;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The endpointIdentificationAlgorithm of the client parameters turns on host 
name verification for the client sockets
+ * of the context. The test server's certificate is issued to localhost only, 
so a client that connects to 127.0.0.1 is
+ * rejected with the algorithm set, and accepted without it, as before.
+ */
+public class SSLContextClientParametersEndpointIdentificationTest extends 
AbstractJsseParametersTest {
+
+    private final CamelContext camelContext = new DefaultCamelContext();
+    private ExecutorService executor;
+    private SSLServerSocket serverSocket;
+
+    @BeforeEach
+    public void startServer() throws Exception {
+        SSLContext serverContext = 
createSSLContextParameters(null).createSSLContext(camelContext);
+        // on all addresses, as localhost may resolve to ::1
+        serverSocket = (SSLServerSocket) 
serverContext.getServerSocketFactory().createServerSocket(0);
+        executor = Executors.newSingleThreadExecutor();
+    }
+
+    @AfterEach
+    public void stopServer() throws Exception {
+        serverSocket.close();
+        executor.shutdownNow();
+        executor.awaitTermination(10, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testUnsetKeepsTheDefaultOfTheJdk() throws Exception {
+        SSLContextParameters scp = createSSLContextParameters(new 
SSLContextClientParameters());
+        SSLContext context = scp.createSSLContext(camelContext);
+
+        try (SSLSocket socket = (SSLSocket) 
context.getSocketFactory().createSocket("127.0.0.1", port())) {
+            
assertNull(socket.getSSLParameters().getEndpointIdentificationAlgorithm());
+            handshake(socket);
+        }
+    }
+
+    @Test
+    public void testHttpsVerifiesTheHostName() throws Exception {
+        SSLContextClientParameters sccp = new SSLContextClientParameters();
+        sccp.setEndpointIdentificationAlgorithm("HTTPS");
+        SSLContext context = 
createSSLContextParameters(sccp).createSSLContext(camelContext);
+
+        // the certificate is issued to localhost
+        try (SSLSocket socket = (SSLSocket) 
context.getSocketFactory().createSocket("localhost", port())) {
+            assertEquals("HTTPS", 
socket.getSSLParameters().getEndpointIdentificationAlgorithm());
+            handshake(socket);
+        }
+
+        // and not to 127.0.0.1
+        try (SSLSocket socket = (SSLSocket) 
context.getSocketFactory().createSocket("127.0.0.1", port())) {
+            assertThrows(SSLHandshakeException.class, () -> handshake(socket));
+        }
+    }
+
+    @Test
+    public void testPropertyPlaceholder() throws Exception {
+        Properties properties = new Properties();
+        properties.setProperty("tls.identification", "HTTPS");
+        CamelContext placeholderContext = 
createPropertiesPlaceholderAwareContext(properties);
+
+        SSLContextClientParameters sccp = new SSLContextClientParameters();
+        sccp.setEndpointIdentificationAlgorithm("{{tls.identification}}");
+        SSLContextParameters scp = createSSLContextParameters(sccp);
+        SSLContext context = scp.createSSLContext(placeholderContext);
+
+        try (SSLSocket socket = (SSLSocket) 
context.getSocketFactory().createSocket()) {
+            assertEquals("HTTPS", 
socket.getSSLParameters().getEndpointIdentificationAlgorithm());
+        }
+    }
+
+    @Test
+    public void testNotAppliedToEngines() throws Exception {
+        SSLContextClientParameters sccp = new SSLContextClientParameters();
+        sccp.setEndpointIdentificationAlgorithm("HTTPS");
+        SSLContext context = 
createSSLContextParameters(sccp).createSSLContext(camelContext);
+
+        // an engine starts in server mode, where the JDK may check the 
certificate of a client against its address
+        SSLEngine engine = context.createSSLEngine();
+        
assertNull(engine.getSSLParameters().getEndpointIdentificationAlgorithm());
+        
assertTrue(sccp.toString().contains("getEndpointIdentificationAlgorithm()=HTTPS"),
 sccp.toString());
+    }
+
+    private SSLContextParameters 
createSSLContextParameters(SSLContextClientParameters clientParameters) {
+        KeyStoreParameters ksp = new KeyStoreParameters();
+        ksp.setResource("org/apache/camel/support/jsse/localhost.p12");
+        ksp.setPassword("changeit");
+
+        KeyManagersParameters kmp = new KeyManagersParameters();
+        kmp.setKeyPassword("changeit");
+        kmp.setKeyStore(ksp);
+
+        // the client trusts the certificate of the server, so only the host 
name check can fail
+        TrustManagersParameters tmp = new TrustManagersParameters();
+        tmp.setKeyStore(ksp);
+
+        SSLContextParameters scp = new SSLContextParameters();
+        scp.setKeyManagers(kmp);
+        scp.setTrustManagers(tmp);
+        scp.setClientParameters(clientParameters);
+        return scp;
+    }
+
+    private int port() {
+        return serverSocket.getLocalPort();
+    }
+
+    /**
+     * Runs the handshake of the client socket against one connection that the 
server accepts.
+     */
+    private void handshake(SSLSocket client) throws Exception {
+        Future<?> server = executor.submit(() -> {
+            try (SSLSocket accepted = (SSLSocket) serverSocket.accept()) {
+                accepted.setSoTimeout(10000);
+                accepted.startHandshake();
+            }
+            return null;
+        });
+        client.setSoTimeout(10000);
+        try {
+            client.startHandshake();
+        } finally {
+            client.close();
+            try {
+                server.get(10, TimeUnit.SECONDS);
+            } catch (Exception e) {
+                // the server side fails too when the client rejects the 
certificate
+            }
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-configuration-utilities.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-configuration-utilities.adoc
index 3a36de6daa77..97204ab45d41 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-configuration-utilities.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-configuration-utilities.adoc
@@ -349,6 +349,7 @@ SSLEngine configuration.
 SSLContextClientParameters contains the following elements:
 
 * sniHostNames (elements)
+* endpointIdentificationAlgorithm (Java API)
 * sessionTimeout (attribute)
 * cipherSuites (element)
 * cipherSuitesFilter (element)
@@ -360,6 +361,19 @@ SSLContextClientParameters contains the following elements:
 sniHostNames::
 Contains a list of sniHostName elements which provides a list
 of SNIHostNames to be used for SSL.
+endpointIdentificationAlgorithm::
+This optional property turns on host name verification for the client
+sockets that the `SSLSocketFactory` of the context creates: with `HTTPS`
+(or `LDAPS`), the handshake fails unless the certificate of the server
+matches the host that the socket connects to. It is unset by default,
+which keeps the behavior of the JDK: no host name verification. It may
+use a property placeholder. The JDK checks the host name that the socket
+connects to (the host given to `createSocket`, or of the `InetSocketAddress`
+given to `connect`), so connecting by IP address fails the handshake unless
+the certificate lists that address. It is
+not applied to an `SSLEngine`, which starts in server mode: a component
+that uses an engine applies it to the `SSLParameters` of the engine once
+the engine is in client mode.
 sessionTimeout::
 See above
 cipherSuites::

Reply via email to