davsclaus commented on code in PR #28091:
URL: https://github.com/apache/camel/pull/28091#discussion_r4313837369
##########
components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCComponent.java:
##########
@@ -345,8 +354,13 @@ private void configureHybridKEMMaterial(PQCConfiguration
configuration) {
return;
}
+ // Captured before anything is auto-configured: the pre-built hybrid
materials below replace the PQC half as
+ // well, and must only do so when the route author did not supply it
+ boolean pqcMaterialMissing =
ObjectHelper.isEmpty(configuration.getKeyGenerator())
Review Comment:
This is never `true` when nothing was supplied: the generic MLKEM block in
`createEndpoint` has already set `PQCDefaultMLKEMMaterial` (ML-KEM-512). So the
matched `PQCDefaultX25519MLKEMMaterial` / `PQCDefaultECDHMLKEMMaterial` PQC
half (ML-KEM-768) below is no longer applied, and the default hybrid KEM drops
from ML-KEM-768 to ML-KEM-512.
##########
components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCHybridKeyPairPreservedTest.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.pqc;
+
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.SecureRandom;
+import java.security.Security;
+
+import javax.crypto.KeyGenerator;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.bouncycastle.jcajce.spec.MLDSAParameterSpec;
+import org.bouncycastle.jcajce.spec.MLKEMParameterSpec;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * Auto-configuring the classical half of a hybrid endpoint must not replace
PQC key material the route author supplied:
+ * signing with a silently substituted key produces signatures nobody can
verify against the real key.
+ */
+public class PQCHybridKeyPairPreservedTest {
+
+ // Per-test: until CAMEL-25795 lands, stopping a context de-registers the
providers for the whole JVM
+ @BeforeEach
+ void setup() {
+ if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
+ Security.addProvider(new BouncyCastleProvider());
+ }
+ if (Security.getProvider(BouncyCastlePQCProvider.PROVIDER_NAME) ==
null) {
+ Security.addProvider(new BouncyCastlePQCProvider());
+ }
+ }
+
+ @Test
+ void hybridSignKeepsTheConfiguredPqcKeyPair() throws Exception {
+ KeyPairGenerator kpg = KeyPairGenerator.getInstance("ML-DSA", "BC");
+ kpg.initialize(MLDSAParameterSpec.ml_dsa_65, new SecureRandom());
+ KeyPair myKeyPair = kpg.generateKeyPair();
+
+ try (CamelContext context = new DefaultCamelContext()) {
+ // keyPair and classicalKeyPair are both autowired, so a single
KeyPair bean would be injected into both
+ // and the classical branch under test would never run
+ context.setAutowiredEnabled(false);
+ context.getRegistry().bind("myKeyPair", myKeyPair);
+ context.start();
+
+ PQCEndpoint endpoint = context.getEndpoint(
+ "pqc:sign?operation=hybridSign&signatureAlgorithm=MLDSA"
+ +
"&classicalSignatureAlgorithm=ECDSA_P256&keyPair=#myKeyPair",
+ PQCEndpoint.class);
+
+ assertSame(myKeyPair, endpoint.getConfiguration().getKeyPair(),
+ "the configured PQC key pair must survive hybrid
auto-configuration");
+ assertNotNull(endpoint.getConfiguration().getClassicalKeyPair(),
+ "the classical half must still be auto-configured");
+ }
+ }
+
+ @Test
+ void hybridKemKeepsTheConfiguredPqcKeyPair() throws Exception {
+ KeyPairGenerator kpg = KeyPairGenerator.getInstance("ML-KEM", "BC");
+ kpg.initialize(MLKEMParameterSpec.ml_kem_768, new SecureRandom());
+ KeyPair myKeyPair = kpg.generateKeyPair();
+ KeyGenerator myKeyGenerator = KeyGenerator.getInstance("ML-KEM", "BC");
+
+ try (CamelContext context = new DefaultCamelContext()) {
+ // keyPair and classicalKeyPair are both autowired, so a single
KeyPair bean would be injected into both
+ // and the classical branch under test would never run
+ context.setAutowiredEnabled(false);
+ context.getRegistry().bind("myKeyPair", myKeyPair);
+ context.getRegistry().bind("myKeyGenerator", myKeyGenerator);
+ context.start();
+
+ PQCEndpoint endpoint = context.getEndpoint(
+
"pqc:kem?operation=hybridGenerateSecretKeyEncapsulation&keyEncapsulationAlgorithm=MLKEM"
+ +
"&classicalKEMAlgorithm=X25519&keyPair=#myKeyPair&keyGenerator=#myKeyGenerator",
+ PQCEndpoint.class);
+
+ assertSame(myKeyPair, endpoint.getConfiguration().getKeyPair(),
+ "the configured PQC key pair must survive hybrid
auto-configuration");
+ assertSame(myKeyGenerator,
endpoint.getConfiguration().getKeyGenerator(),
+ "the configured PQC key generator must survive hybrid
auto-configuration");
+ assertNotNull(endpoint.getConfiguration().getClassicalKeyPair(),
+ "the classical half must still be auto-configured");
+ }
+ }
+
+ @Test
+ void hybridSignStillAutoConfiguresWhenNothingIsSupplied() throws Exception
{
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.start();
+
+ PQCEndpoint endpoint = context.getEndpoint(
+
"pqc:sign?operation=hybridSign&signatureAlgorithm=MLDSA&classicalSignatureAlgorithm=ECDSA_P256",
+ PQCEndpoint.class);
+
+ assertNotNull(endpoint.getConfiguration().getKeyPair(), "the PQC
half must be auto-configured");
Review Comment:
`assertNotNull` passes with either material. Asserting the matched one
(`assertSame(PQCDefaultECDSAMLDSAMaterial.pqcKeyPair, ...)`), plus the same
check for hybrid KEM with `PQCDefaultX25519MLKEMMaterial.pqcKeyPair`, would
catch the default changing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]