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 aa52c5abba21 CAMEL-27427: camel-api - Allow KeyManagersParameters to 
carry a KeyManager directly (#30705)
aa52c5abba21 is described below

commit aa52c5abba21d5bc8cdec0d8f4a30995a62ba302
Author: Adriano Machado <[email protected]>
AuthorDate: Tue Nov 24 01:44:02 2026 -0500

    CAMEL-27427: camel-api - Allow KeyManagersParameters to carry a KeyManager 
directly (#30705)
    
    KeyManagersParameters gains a keyManager option, mirroring
    TrustManagersParameters.trustManager. When set, createKeyManagers()
    returns it instead of building one from the key store, so frameworks
    that already manage key material as an X509KeyManager can hand it to
    Camel.
    
    - XML DSL: new keyManager attribute on keyManagers, resolved from the
      registry like trustManager.
    - camel-vertx-common: VertxHelper wraps a carried X509KeyManager with
      KeyCertOptions.wrap, and rejects other KeyManager types instead of
      silently ignoring them.
    - Documented in the JSSE utility page; camel-spring.xsd regenerated.
    
    _Claude Code on behalf of Adriano Machado (@ammachado)_
    
    _This was generated by an AI agent and may contain inaccuracies.
    Please verify before relying on it._
    
    Co-authored-by: Claude <[email protected]>
---
 .../apache/camel/catalog/schemas/camel-spring.xsd  |  1 +
 .../java/org/apache/camel/coap/CoAPEndpoint.java   |  7 ++
 .../camel/coap/CoAPEndpointKeyManagerTest.java     | 61 +++++++++++++++++
 .../component/kafka/KafkaClientConfiguration.java  |  7 ++
 .../kafka/KafkaClientConfigurationTest.java        | 20 ++++++
 .../KeyManagersParametersFactoryBeanTest.java      | 54 +++++++++++++++
 ...eyManagersParametersFactoryBeanTest-context.xml | 37 ++++++++++
 .../camel/component/vertx/common/VertxHelper.java  | 19 +++++-
 .../component/vertx/common/VertxHelperTest.java    | 78 ++++++++++++++++++++++
 .../camel/support/jsse/KeyManagersParameters.java  | 44 +++++++++++-
 .../AbstractKeyManagersParametersFactoryBean.java  | 21 ++++++
 .../support/jsse/KeyManagersParametersTest.java    | 17 +++++
 .../support/jsse/SSLContextParametersTest.java     | 68 +++++++++++++++++++
 .../ROOT/pages/camel-configuration-utilities.adoc  | 10 +++
 14 files changed, 440 insertions(+), 4 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
index 0d87b0839229..e1a0ab687ab8 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
@@ -3001,6 +3001,7 @@ Whether to use a scheduled thread pool. Default value: 
false
         <xs:attribute name="keyPassword" type="xs:string"/>
         <xs:attribute name="provider" type="xs:string"/>
         <xs:attribute name="algorithm" type="xs:string"/>
+        <xs:attribute name="keyManager" type="xs:string"/>
       </xs:extension>
     </xs:complexContent>
   </xs:complexType>
diff --git 
a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java
index 7a2bf2e1c3db..a254cc3cc21d 100644
--- 
a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java
+++ 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java
@@ -560,6 +560,13 @@ public class CoAPEndpoint extends DefaultEndpoint 
implements EndpointServiceLoca
 
     private void configureIdentity(DtlsConnectorConfig.Builder builder) throws 
GeneralSecurityException, IOException {
         KeyManagersParameters keyManagers = 
sslContextParameters.getKeyManagers();
+        if (keyManagers.getKeyManager() != null) {
+            // DTLS reads the private key and certificate chain from the key 
store, so an existing key manager
+            // cannot be used (coap+tcp uses the SSLContext of the 
sslContextParameters, which supports it)
+            throw new IllegalArgumentException(
+                    "A KeyManager set on the key managers of 
sslContextParameters is not supported with DTLS (coaps):"
+                                               + " configure a keyStore 
instead");
+        }
         KeyStore keyStore = keyManagers.getKeyStore().createKeyStore();
 
         // Use the configured alias or fall back to the first alias in
diff --git 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPEndpointKeyManagerTest.java
 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPEndpointKeyManagerTest.java
new file mode 100644
index 000000000000..b618160e1e1b
--- /dev/null
+++ 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPEndpointKeyManagerTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.coap;
+
+import java.net.InetSocketAddress;
+
+import javax.net.ssl.KeyManager;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.jsse.KeyManagersParameters;
+import org.apache.camel.support.jsse.KeyStoreParameters;
+import org.apache.camel.support.jsse.SSLContextParameters;
+import org.eclipse.californium.elements.config.Configuration;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class CoAPEndpointKeyManagerTest {
+
+    @Test
+    void existingKeyManagerIsRejectedWithDTLS() throws Exception {
+        KeyManagersParameters keyManagers = new KeyManagersParameters();
+        keyManagers.setKeyManager(new KeyManager() {
+        });
+        // DTLS used to read this key store even though a key manager was set, 
contradicting the key manager
+        // precedence, so the key manager must be rejected even when a key 
store is configured too
+        KeyStoreParameters keyStore = new KeyStoreParameters();
+        keyStore.setResource("service.jks");
+        keyStore.setPassword("security");
+        keyManagers.setKeyStore(keyStore);
+        keyManagers.setKeyPassword("security");
+        SSLContextParameters sslContextParameters = new SSLContextParameters();
+        sslContextParameters.setKeyManagers(keyManagers);
+
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.start();
+            CoAPEndpoint endpoint = 
context.getEndpoint("coaps://localhost:5684/TestResource", CoAPEndpoint.class);
+            endpoint.setSslContextParameters(sslContextParameters);
+
+            IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class,
+                    () -> endpoint.createDTLSConnector(new 
InetSocketAddress(0), false, Configuration.getStandard()));
+            assertTrue(e.getMessage().contains("KeyManager"), e.getMessage());
+        }
+    }
+}
diff --git 
a/components/camel-kafka-common/src/main/java/org/apache/camel/component/kafka/KafkaClientConfiguration.java
 
b/components/camel-kafka-common/src/main/java/org/apache/camel/component/kafka/KafkaClientConfiguration.java
index 08e011c73e7b..addd9f89c6ac 100644
--- 
a/components/camel-kafka-common/src/main/java/org/apache/camel/component/kafka/KafkaClientConfiguration.java
+++ 
b/components/camel-kafka-common/src/main/java/org/apache/camel/component/kafka/KafkaClientConfiguration.java
@@ -662,6 +662,13 @@ public abstract class KafkaClientConfiguration implements 
Cloneable, HeaderFilte
 
         KeyManagersParameters keyManagers = 
sslContextParameters.getKeyManagers();
         if (keyManagers != null) {
+            if (keyManagers.getKeyManager() != null) {
+                // the Kafka client loads the key store itself from the 
ssl.keystore.* properties, so an existing
+                // key manager cannot be passed on, and ignoring it would 
connect without the client certificate
+                throw new IllegalArgumentException(
+                        "A KeyManager set on the key managers of 
sslContextParameters is not supported by the Kafka"
+                                                   + " client: configure a 
keyStore instead");
+            }
             addPropertyIfNotNull(props, 
SslConfigs.SSL_KEYMANAGER_ALGORITHM_CONFIG, keyManagers.getAlgorithm());
             addPropertyIfNotNull(props, SslConfigs.SSL_KEY_PASSWORD_CONFIG, 
keyManagers.getKeyPassword());
             KeyStoreParameters keyStore = keyManagers.getKeyStore();
diff --git 
a/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaClientConfigurationTest.java
 
b/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaClientConfigurationTest.java
index 36a042533e94..626f69d5adff 100644
--- 
a/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaClientConfigurationTest.java
+++ 
b/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaClientConfigurationTest.java
@@ -20,6 +20,8 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.TreeMap;
 
+import javax.net.ssl.KeyManager;
+
 import org.apache.camel.component.kafka.security.KafkaAuthType;
 import org.apache.camel.support.jsse.KeyManagersParameters;
 import org.apache.camel.support.jsse.KeyStoreParameters;
@@ -33,6 +35,7 @@ import org.junit.jupiter.api.Test;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
@@ -130,6 +133,23 @@ class KafkaClientConfigurationTest {
         assertEquals("TLSv1.3", consumer.get(SslConfigs.SSL_PROTOCOL_CONFIG));
     }
 
+    @Test
+    void existingKeyManagerIsRejected() {
+        KeyManagersParameters keyManagers = new KeyManagersParameters();
+        keyManagers.setKeyManager(new KeyManager() {
+        });
+        SSLContextParameters sslContextParameters = new SSLContextParameters();
+        sslContextParameters.setKeyManagers(keyManagers);
+
+        KafkaConfiguration config = new KafkaConfiguration();
+        config.setSslContextParameters(sslContextParameters);
+
+        // the Kafka client cannot use a KeyManager: ignoring it would connect 
without the client certificate
+        IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class, config::createProducerProperties);
+        assertTrue(e.getMessage().contains("KeyManager"), e.getMessage());
+        assertThrows(IllegalArgumentException.class, 
config::createConsumerProperties);
+    }
+
     @Test
     void saslAuthTypeIsAppliedTheSameToProducerAndConsumer() {
         KafkaConfiguration config = new KafkaConfiguration();
diff --git 
a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/util/spring/KeyManagersParametersFactoryBeanTest.java
 
b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/util/spring/KeyManagersParametersFactoryBeanTest.java
new file mode 100644
index 000000000000..34f9fa985e31
--- /dev/null
+++ 
b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/util/spring/KeyManagersParametersFactoryBeanTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.util.spring;
+
+import jakarta.annotation.Resource;
+
+import javax.net.ssl.KeyManager;
+
+import org.apache.camel.support.jsse.SSLContextParameters;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration
+public class KeyManagersParametersFactoryBeanTest {
+
+    @Resource
+    SSLContextParameters scp;
+
+    @Resource
+    KeyManager myKeyManager;
+
+    @Test
+    public void testKeyManagerReference() throws Exception {
+        assertNotNull(scp.getKeyManagers());
+        // the keyManager attribute is resolved from the registry
+        assertSame(myKeyManager, scp.getKeyManagers().getKeyManager());
+        // and used instead of the key store, which does not exist and would 
fail to load
+        assertArrayEquals(new KeyManager[] { myKeyManager }, 
scp.getKeyManagers().createKeyManagers());
+    }
+
+    public static class MyKeyManager implements KeyManager {
+    }
+}
diff --git 
a/components/camel-spring-parent/camel-spring-xml/src/test/resources/org/apache/camel/util/spring/KeyManagersParametersFactoryBeanTest-context.xml
 
b/components/camel-spring-parent/camel-spring-xml/src/test/resources/org/apache/camel/util/spring/KeyManagersParametersFactoryBeanTest-context.xml
new file mode 100644
index 000000000000..dcc926a251f3
--- /dev/null
+++ 
b/components/camel-spring-parent/camel-spring-xml/src/test/resources/org/apache/camel/util/spring/KeyManagersParametersFactoryBeanTest-context.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns:camel="http://camel.apache.org/schema/spring";
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
+
+  <camel:camelContext id="test"/>
+
+  <bean id="myKeyManager" 
class="org.apache.camel.util.spring.KeyManagersParametersFactoryBeanTest$MyKeyManager"/>
+
+  <camel:sslContextParameters id="scp">
+    <camel:keyManagers keyManager="myKeyManager">
+      <camel:keyStore resource="does-not-exist.p12" password="password"/>
+    </camel:keyManagers>
+  </camel:sslContextParameters>
+
+</beans>
diff --git 
a/components/camel-vertx/camel-vertx-common/src/main/java/org/apache/camel/component/vertx/common/VertxHelper.java
 
b/components/camel-vertx/camel-vertx-common/src/main/java/org/apache/camel/component/vertx/common/VertxHelper.java
index 8141e724645e..ac32dd9336d1 100644
--- 
a/components/camel-vertx/camel-vertx-common/src/main/java/org/apache/camel/component/vertx/common/VertxHelper.java
+++ 
b/components/camel-vertx/camel-vertx-common/src/main/java/org/apache/camel/component/vertx/common/VertxHelper.java
@@ -21,12 +21,15 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Locale;
 
+import javax.net.ssl.KeyManager;
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509KeyManager;
 
 import io.vertx.core.http.ClientAuth;
+import io.vertx.core.net.KeyCertOptions;
 import io.vertx.core.net.NetServerOptions;
 import io.vertx.core.net.TCPSSLOptions;
 import io.vertx.core.net.TrustOptions;
@@ -68,9 +71,19 @@ public final class VertxHelper {
 
         tcpsslOptions.setSsl(true);
 
-        KeyManagerFactory keyManagerFactory = 
createKeyManagerFactory(camelContext, sslContextParameters);
-        if (keyManagerFactory != null) {
-            tcpsslOptions.setKeyCertOptions(new 
KeyManagerFactoryOptions(keyManagerFactory));
+        KeyManager keyManager = sslContextParameters.getKeyManagers() != null
+                ? sslContextParameters.getKeyManagers().getKeyManager() : null;
+        if (keyManager instanceof X509KeyManager x509KeyManager) {
+            
tcpsslOptions.setKeyCertOptions(KeyCertOptions.wrap(x509KeyManager));
+        } else if (keyManager != null) {
+            throw new IllegalArgumentException(
+                    "Vert.x requires an X509KeyManager, but the configured key 
manager is a "
+                                               + 
keyManager.getClass().getName());
+        } else {
+            KeyManagerFactory keyManagerFactory = 
createKeyManagerFactory(camelContext, sslContextParameters);
+            if (keyManagerFactory != null) {
+                tcpsslOptions.setKeyCertOptions(new 
KeyManagerFactoryOptions(keyManagerFactory));
+            }
         }
 
         TrustManager trustManager = sslContextParameters.getTrustManagers() != 
null
diff --git 
a/components/camel-vertx/camel-vertx-common/src/test/java/org/apache/camel/component/vertx/common/VertxHelperTest.java
 
b/components/camel-vertx/camel-vertx-common/src/test/java/org/apache/camel/component/vertx/common/VertxHelperTest.java
index 539d09a15318..024ac5c5d7bb 100644
--- 
a/components/camel-vertx/camel-vertx-common/src/test/java/org/apache/camel/component/vertx/common/VertxHelperTest.java
+++ 
b/components/camel-vertx/camel-vertx-common/src/test/java/org/apache/camel/component/vertx/common/VertxHelperTest.java
@@ -16,15 +16,21 @@
  */
 package org.apache.camel.component.vertx.common;
 
+import java.net.Socket;
+import java.security.Principal;
+import java.security.PrivateKey;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.X509KeyManager;
 import javax.net.ssl.X509TrustManager;
 
 import io.vertx.core.net.NetClientOptions;
 import io.vertx.core.net.TCPSSLOptions;
 import org.apache.camel.CamelContext;
 import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.jsse.KeyManagersParameters;
 import org.apache.camel.support.jsse.KeyStoreParameters;
 import org.apache.camel.support.jsse.SSLContextParameters;
 import org.apache.camel.support.jsse.TrustAllTrustManager;
@@ -34,6 +40,7 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 public class VertxHelperTest {
 
@@ -119,6 +126,77 @@ public class VertxHelperTest {
         
assertThat(options.getTrustOptions()).isInstanceOf(TrustManagerFactoryOptions.class);
     }
 
+    @Test
+    public void testCustomKeyManagerApplied() throws Exception {
+        SSLContextParameters sslContextParameters = new SSLContextParameters();
+        KeyManagersParameters keyManagers = new KeyManagersParameters();
+        // a key store that does not exist fails if the KeyManagerFactory is 
used, so the
+        // existing key manager must be used without reading the key store
+        KeyStoreParameters keyStore = new KeyStoreParameters();
+        keyStore.setResource("jsse/does-not-exist.p12");
+        keyManagers.setKeyStore(keyStore);
+
+        X509KeyManager customKeyManager = new X509KeyManager() {
+            @Override
+            public String[] getClientAliases(String keyType, Principal[] 
issuers) {
+                return null;
+            }
+
+            @Override
+            public String chooseClientAlias(String[] keyType, Principal[] 
issuers, Socket socket) {
+                return null;
+            }
+
+            @Override
+            public String[] getServerAliases(String keyType, Principal[] 
issuers) {
+                return null;
+            }
+
+            @Override
+            public String chooseServerAlias(String keyType, Principal[] 
issuers, Socket socket) {
+                return null;
+            }
+
+            @Override
+            public X509Certificate[] getCertificateChain(String alias) {
+                return null;
+            }
+
+            @Override
+            public PrivateKey getPrivateKey(String alias) {
+                return null;
+            }
+        };
+
+        keyManagers.setKeyManager(customKeyManager);
+        sslContextParameters.setKeyManagers(keyManagers);
+
+        TCPSSLOptions options = new NetClientOptions();
+        VertxHelper.setupSSLOptions(context, sslContextParameters, options);
+
+        assertThat(options.isSsl()).isTrue();
+        assertThat(options.getKeyCertOptions()).isNotNull();
+        
assertThat(options.getKeyCertOptions().getKeyManagerFactory(null).getKeyManagers())
+                .containsExactly(customKeyManager);
+    }
+
+    @Test
+    public void testNonX509KeyManagerRejected() {
+        SSLContextParameters sslContextParameters = new SSLContextParameters();
+        KeyManagersParameters keyManagers = new KeyManagersParameters();
+        keyManagers.setKeyManager(new KeyManager() {
+            // noop
+        });
+        sslContextParameters.setKeyManagers(keyManagers);
+
+        TCPSSLOptions options = new NetClientOptions();
+
+        // Vert.x can only wrap an X509KeyManager: any other key manager must 
fail loudly rather than be ignored
+        assertThatThrownBy(() -> VertxHelper.setupSSLOptions(context, 
sslContextParameters, options))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("X509KeyManager");
+    }
+
     @Test
     public void testNoTrustOptionsWhenNoneConfigured() throws Exception {
         SSLContextParameters sslContextParameters = new SSLContextParameters();
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyManagersParameters.java
 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyManagersParameters.java
index 7c140d5f7a9f..41b98de9fcbe 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyManagersParameters.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/KeyManagersParameters.java
@@ -39,12 +39,17 @@ public class KeyManagersParameters extends JsseParameters {
     protected @Nullable String keyPassword;
     protected @Nullable String provider;
     protected @Nullable String algorithm;
+    protected @Nullable KeyManager keyManager;
 
     /**
      * Creates {@link KeyManager}s based on this instance's configuration and 
the {@code KeyStore} produced by the
      * configuration returned from {@link #getKeyStore()}. The {@code 
KeyManager}s are produced from a factory created
      * by using the provider and algorithm identifiers returned by {@link 
#getProvider()} and {@link #getAlgorithm()},
      * respectively. If either of these methods returns null, the default JSSE 
value is used instead.
+     * <p>
+     * If an existing {@code KeyManager} is set with {@link 
#setKeyManager(KeyManager)}, it is returned instead, and the
+     * key store, key password, provider and algorithm are not used. The cert 
alias of {@link SSLContextParameters}, if
+     * set, still applies to an {@code X509KeyManager}.
      *
      * @return                          the initialized {@code KeyManager}s
      * @throws GeneralSecurityException if there is an error creating the 
{@code KeyManager}s or in creating the
@@ -54,6 +59,15 @@ public class KeyManagersParameters extends JsseParameters {
      * @see                             KeyStoreParameters#createKeyStore()
      */
     public KeyManager[] createKeyManagers() throws GeneralSecurityException, 
IOException {
+        if (keyManager != null) {
+            if (keyStore != null || keyPassword != null || provider != null || 
algorithm != null) {
+                LOG.debug("Using the existing KeyManager [{}]: the keyStore, 
keyPassword, provider and algorithm"
+                          + " of KeyManagersParameters [{}] are not used.",
+                        keyManager, this);
+            }
+            return new KeyManager[] { keyManager };
+        }
+
         LOG.trace("Creating KeyManager[] from KeyManagersParameters [{}].", 
this);
         KeyManager[] keyManagers;
 
@@ -150,10 +164,38 @@ public class KeyManagersParameters extends JsseParameters 
{
         this.algorithm = value;
     }
 
+    /**
+     * The existing key manager to use instead of creating one with a {@link 
KeyManagerFactory}.
+     *
+     * @since 4.24
+     */
+    public @Nullable KeyManager getKeyManager() {
+        return keyManager;
+    }
+
+    /**
+     * To use an existing configured key manager instead of using {@link 
KeyManagerFactory} to get the
+     * {@link KeyManager}. When set, the key store, key password, provider and 
algorithm are not used. The cert alias of
+     * {@link SSLContextParameters}, if set, still applies to an {@code 
X509KeyManager}.
+     *
+     * @param keyManager the key manager to use. Vert.x based components and 
the cert alias require an
+     *                   {@code X509KeyManager}
+     * @since            4.24
+     */
+    public void setKeyManager(@Nullable KeyManager keyManager) {
+        this.keyManager = keyManager;
+    }
+
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder();
-        builder.append("KeyManagersParameters[keyStore=");
+        builder.append("KeyManagersParameters[");
+        if (keyManager != null) {
+            builder.append("keyManager=");
+            builder.append(keyManager);
+            builder.append(", ");
+        }
+        builder.append("keyStore=");
         builder.append(keyStore);
         builder.append(", keyPassword=");
         builder.append("********");
diff --git 
a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/util/jsse/AbstractKeyManagersParametersFactoryBean.java
 
b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/util/jsse/AbstractKeyManagersParametersFactoryBean.java
index f1e07c403e83..d93a60007821 100644
--- 
a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/util/jsse/AbstractKeyManagersParametersFactoryBean.java
+++ 
b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/util/jsse/AbstractKeyManagersParametersFactoryBean.java
@@ -25,6 +25,7 @@ import javax.net.ssl.KeyManager;
 
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.jsse.KeyManagersParameters;
 
 /**
@@ -49,6 +50,13 @@ public abstract class 
AbstractKeyManagersParametersFactoryBean extends AbstractJ
                                                 + " See 
https://docs.oracle.com/en/java/javase/21/docs/specs/security/standard-names.html";)
     protected String algorithm;
 
+    @XmlAttribute
+    @Metadata(label = "advanced",
+              description = "Reference to an existing KeyManager bean in the 
registry, to use instead of using KeyManagerFactory"
+                            + " to get the KeyManager. When set, the key 
store, key password, provider and algorithm are not used."
+                            + " The certAlias of the SSL context parameters, 
if set, still applies to an X509KeyManager.")
+    protected String keyManager;
+
     @XmlTransient
     private KeyManagersParameters instance;
 
@@ -76,6 +84,14 @@ public abstract class 
AbstractKeyManagersParametersFactoryBean extends AbstractJ
         this.algorithm = value;
     }
 
+    public String getKeyManager() {
+        return keyManager;
+    }
+
+    public void setKeyManager(String keyManager) {
+        this.keyManager = keyManager;
+    }
+
     @Override
     public KeyManagersParameters getObject() throws Exception {
         if (this.isSingleton()) {
@@ -106,6 +122,11 @@ public abstract class 
AbstractKeyManagersParametersFactoryBean extends AbstractJ
         newInstance.setProvider(provider);
         CamelContextAware.trySetCamelContext(newInstance, getCamelContext());
 
+        if (keyManager != null) {
+            KeyManager km = 
CamelContextHelper.mandatoryLookup(getCamelContext(), keyManager, 
KeyManager.class);
+            newInstance.setKeyManager(km);
+        }
+
         return newInstance;
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/jsse/KeyManagersParametersTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/jsse/KeyManagersParametersTest.java
index 35faf6a57794..e981d102c08f 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/support/jsse/KeyManagersParametersTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/jsse/KeyManagersParametersTest.java
@@ -144,6 +144,23 @@ public class KeyManagersParametersTest extends 
AbstractJsseParametersTest {
         }
     }
 
+    @Test
+    public void testCustomKeyManager() throws Exception {
+        KeyManager myKm = new KeyManager() {
+            // noop
+        };
+
+        // a key store with a wrong key password fails if the 
KeyManagerFactory is used, so the
+        // existing key manager must be returned without reading the key store
+        KeyManagersParameters kmp = this.createMinimalKeyManagersParameters();
+        kmp.setKeyPassword("");
+        kmp.setKeyManager(myKm);
+
+        KeyManager[] kms = kmp.createKeyManagers();
+        assertEquals(1, kms.length);
+        assertSame(myKm, kms[0]);
+    }
+
     @Test
     public void testAliasedKeyManager() throws Exception {
         KeyManagersParameters kmp = this.createMinimalKeyManagersParameters();
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextParametersTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextParametersTest.java
index 20c129f83ae2..57f73961a336 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextParametersTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/jsse/SSLContextParametersTest.java
@@ -16,6 +16,12 @@
  */
 package org.apache.camel.support.jsse;
 
+import java.io.IOException;
+import java.net.Socket;
+import java.security.GeneralSecurityException;
+import java.security.Principal;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -29,10 +35,12 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 
+import javax.net.ssl.KeyManager;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLServerSocket;
 import javax.net.ssl.SSLSocket;
+import javax.net.ssl.X509KeyManager;
 
 import org.apache.camel.CamelContext;
 import org.junit.jupiter.api.Disabled;
@@ -51,6 +59,66 @@ public class SSLContextParametersTest extends 
AbstractJsseParametersTest {
      */
     private static final List<String> PQC_GROUPS = List.of("X25519MLKEM768", 
"SecP256r1MLKEM768", "SecP384r1MLKEM1024");
 
+    @Test
+    public void testCertAliasAppliesToExistingKeyManager() throws Exception {
+        X509KeyManager myKm = new X509KeyManager() {
+            @Override
+            public String[] getClientAliases(String keyType, Principal[] 
issuers) {
+                return new String[] { "mine" };
+            }
+
+            @Override
+            public String chooseClientAlias(String[] keyType, Principal[] 
issuers, Socket socket) {
+                return "other";
+            }
+
+            @Override
+            public String[] getServerAliases(String keyType, Principal[] 
issuers) {
+                return new String[] { "mine" };
+            }
+
+            @Override
+            public String chooseServerAlias(String keyType, Principal[] 
issuers, Socket socket) {
+                return "other";
+            }
+
+            @Override
+            public X509Certificate[] getCertificateChain(String alias) {
+                return null;
+            }
+
+            @Override
+            public PrivateKey getPrivateKey(String alias) {
+                return null;
+            }
+        };
+
+        // keep the key managers the SSLContext is initialized with, as 
SSLContext does not expose them
+        KeyManager[][] used = new KeyManager[1][];
+        KeyManagersParameters kmp = new KeyManagersParameters() {
+            @Override
+            public KeyManager[] createKeyManagers() throws 
GeneralSecurityException, IOException {
+                used[0] = super.createKeyManagers();
+                return used[0];
+            }
+        };
+        kmp.setKeyManager(myKm);
+
+        SSLContextParameters scp = new SSLContextParameters();
+        scp.setKeyManagers(kmp);
+        scp.setCertAlias("localhost");
+
+        assertNotNull(scp.createSSLContext(null));
+
+        // the existing key manager is wrapped so the certAlias is chosen 
instead of the alias it would choose
+        assertEquals(1, used[0].length);
+        X509KeyManager km = 
assertInstanceOf(AliasedX509ExtendedKeyManager.class, used[0][0]);
+        assertEquals("localhost", km.chooseServerAlias("RSA", null, null));
+        assertEquals("localhost", km.chooseClientAlias(new String[] { "RSA" }, 
null, null));
+        // and the rest is still answered by the existing key manager
+        assertArrayEquals(new String[] { "mine" }, km.getServerAliases("RSA", 
null));
+    }
+
     @Test
     public void testFilter() {
         SSLContextParameters parameters = new SSLContextParameters();
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 79f5d1150282..9d65afd6a42b 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-configuration-utilities.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-configuration-utilities.adoc
@@ -172,6 +172,7 @@ KeyManagersParameters contain the following elements:
 * keyPassword (attribute)
 * provider (attribute)
 * algorithm (attribute)
+* keyManager (attribute)
 * keyStore (element)
 
 keyPassword::
@@ -190,6 +191,15 @@ KeyManager represented by this object's configuration. See 
the
 
https://docs.oracle.com/en/java/javase/11/security/java-secure-socket-extension-jsse-reference-guide.html[Java
 Secure Socket Extension Reference Guide] for information about standard
 algorithm names.
+keyManager::
+Reference to an existing KeyManager bean in the registry, to use instead
+of using KeyManagerFactory to get the KeyManager. This is useful when
+key material is managed outside Camel, for example by a runtime that
+exposes it as an `X509KeyManager`. When set, the keyPassword, provider,
+algorithm and keyStore are not used. The certAlias of the
+SSLContextParameters, if set, still applies to an `X509KeyManager`.
+Components that load the key store themselves, such as Kafka and CoAP
+over DTLS, do not support it and fail at startup.
 keyStore::
 This optional property represents the key store that provides key
 material to the key manager. This is typically configured with a key

Reply via email to