From 06c309caa1b94a25913b141ed688758bc88afa35 Mon Sep 17 00:00:00 2001 From: dhairya05 Date: Thu, 12 Nov 2026 15:50:57 +0530 Subject: [PATCH] camel-crypto: resolve secureRandomName without needing a keystore The five reference-name setters of DigitalSignatureConfiguration look their object up in the registry and apply it when the lookup found something. Four of them guard on the value they just looked up: KeyStore keystore = context.getRegistry().lookupByNameAndType(keystoreName, KeyStore.class); if (keystore != null) { setKeystore(keystore); } setSecureRandomName copied that guard but named its local `random`, so `keystore` there is the *field*: SecureRandom random = context.getRegistry().lookupByNameAndType(randomName, SecureRandom.class); if (keystore != null) { // the field, unrelated to the lookup setSecureRandom(random); } So secureRandomName only takes effect when a KeyStore happens to be set on the configuration already - which signing or verifying with a plain key never does - and the resolved SecureRandom is dropped on the floor. When a keystore is set and the name resolves to nothing, the opposite happens and setSecureRandom(null) wipes a SecureRandom that was configured directly. The guard now tests `random`, like its four siblings. --- .../crypto/DigitalSignatureConfiguration.java | 2 +- .../DigitalSignatureConfigurationTest.java | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 components/camel-crypto/src/test/java/org/apache/camel/component/crypto/DigitalSignatureConfigurationTest.java diff --git a/components/camel-crypto/src/main/java/org/apache/camel/component/crypto/DigitalSignatureConfiguration.java b/components/camel-crypto/src/main/java/org/apache/camel/component/crypto/DigitalSignatureConfiguration.java index 9dd5bf58d4965..fca14cf568348 100644 --- a/components/camel-crypto/src/main/java/org/apache/camel/component/crypto/DigitalSignatureConfiguration.java +++ b/components/camel-crypto/src/main/java/org/apache/camel/component/crypto/DigitalSignatureConfiguration.java @@ -389,7 +389,7 @@ public String getSecureRandomName() { public void setSecureRandomName(String randomName) { if (context != null && randomName != null) { SecureRandom random = context.getRegistry().lookupByNameAndType(randomName, SecureRandom.class); - if (keystore != null) { + if (random != null) { setSecureRandom(random); } } diff --git a/components/camel-crypto/src/test/java/org/apache/camel/component/crypto/DigitalSignatureConfigurationTest.java b/components/camel-crypto/src/test/java/org/apache/camel/component/crypto/DigitalSignatureConfigurationTest.java new file mode 100644 index 0000000000000..25c65c2acdfeb --- /dev/null +++ b/components/camel-crypto/src/test/java/org/apache/camel/component/crypto/DigitalSignatureConfigurationTest.java @@ -0,0 +1,84 @@ +/* + * 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.component.crypto; + +import java.security.SecureRandom; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +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.assertSame; + +/** + * The reference-name options resolve their object from the registry, and each of them has to do so on its own: a + * SecureRandom reference does not depend on a keystore being configured. + */ +public class DigitalSignatureConfigurationTest { + + @Test + public void secureRandomNameShouldResolveWithoutAKeystore() throws Exception { + try (CamelContext context = new DefaultCamelContext()) { + SecureRandom random = new SecureRandom(); + context.getRegistry().bind("mySecureRandom", random); + + DigitalSignatureConfiguration configuration = new DigitalSignatureConfiguration(); + configuration.setCamelContext(context); + configuration.setSecureRandomName("mySecureRandom"); + + assertSame(random, configuration.getSecureRandom(), + "the SecureRandom named by secureRandomName has to be used"); + assertEquals("mySecureRandom", configuration.getSecureRandomName()); + } + } + + @Test + public void secureRandomNameShouldResolveTheSameWayAsTheOtherReferenceNames() throws Exception { + try (CamelContext context = new DefaultCamelContext()) { + SecureRandom random = new SecureRandom(); + context.getRegistry().bind("mySecureRandom", random); + + // the order the endpoint's configurer happens to set the options in must not matter + DigitalSignatureConfiguration configuration = new DigitalSignatureConfiguration(); + configuration.setSecureRandomName("mySecureRandom"); + assertNull(configuration.getSecureRandom(), "nothing can be resolved before the context is known"); + + configuration.setCamelContext(context); + + assertSame(random, configuration.getSecureRandom(), + "setCamelContext retries the reference names, so the SecureRandom has to be resolved then"); + } + } + + @Test + public void anUnknownSecureRandomNameShouldNotClearTheConfiguredOne() throws Exception { + try (CamelContext context = new DefaultCamelContext()) { + SecureRandom random = new SecureRandom(); + + DigitalSignatureConfiguration configuration = new DigitalSignatureConfiguration(); + configuration.setCamelContext(context); + configuration.setSecureRandom(random); + configuration.setSecureRandomName("notInTheRegistry"); + + assertSame(random, configuration.getSecureRandom(), + "a name that resolves to nothing must leave the configured SecureRandom alone"); + } + } + +}