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 2368049fdc24 CAMEL-28104 / CAMEL-28105 / CAMEL-28106: camel-pqc - 
MAYO/SNOVA defaults, fail-closed generation, and rotation ordering (#31350)
2368049fdc24 is described below

commit 2368049fdc24c7a8f6cee3a27d7b72f078f8890a
Author: Andrea Cosentino <[email protected]>
AuthorDate: Fri Dec 4 11:48:05 2026 +0100

    CAMEL-28104 / CAMEL-28105 / CAMEL-28106: camel-pqc - MAYO/SNOVA defaults, 
fail-closed generation, and rotation ordering (#31350)
    
    - Add default parameter specs for MAYO (mayo5) and SNOVA 
(SNOVA_24_5_5_SHAKE_SSK) in KeyAlgorithmSupport.
    - Fail with an IllegalArgumentException instead of the silent 
initialize(256) fallback when an algorithm has no default parameter spec.
    - AWS Secrets Manager and HashiCorp Vault key lifecycle managers delegate 
key generation to KeyAlgorithmSupport.
    - rotateKey generates the new key before deprecating the old one in all 
five managers, and rejects oldKeyId equal to newKeyId.
    - Upgrade guide entry in camel-4x-upgrade-guide-4_25.adoc.
    
    AI-generated with Antigravity on behalf of oscerd.
---
 .../AwsSecretsManagerKeyLifecycleManager.java      |  62 +--------
 .../AzureKeyVaultKeyLifecycleManager.java          |   7 +-
 .../lifecycle/FileBasedKeyLifecycleManager.java    |   7 +-
 .../HashicorpVaultKeyLifecycleManager.java         |  62 +--------
 .../pqc/lifecycle/InMemoryKeyLifecycleManager.java |   7 +-
 .../pqc/lifecycle/KeyAlgorithmSupport.java         |  26 ++--
 .../pqc/lifecycle/KeyAlgorithmSupportTest.java     | 142 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |   8 ++
 8 files changed, 189 insertions(+), 132 deletions(-)

diff --git 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AwsSecretsManagerKeyLifecycleManager.java
 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AwsSecretsManagerKeyLifecycleManager.java
index 9e1984810aea..ed2c57731ac3 100644
--- 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AwsSecretsManagerKeyLifecycleManager.java
+++ 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AwsSecretsManagerKeyLifecycleManager.java
@@ -20,10 +20,8 @@ import java.io.ByteArrayInputStream;
 import java.io.ObjectInputStream;
 import java.net.URI;
 import java.security.KeyPair;
-import java.security.KeyPairGenerator;
 import java.security.PrivateKey;
 import java.security.PublicKey;
-import java.security.spec.AlgorithmParameterSpec;
 import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Base64;
@@ -33,7 +31,6 @@ import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.camel.component.pqc.PQCKeyEncapsulationAlgorithms;
 import org.apache.camel.component.pqc.PQCSignatureAlgorithms;
-import org.apache.camel.util.SecureRandomHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
@@ -170,33 +167,7 @@ public class AwsSecretsManagerKeyLifecycleManager 
implements KeyLifecycleManager
     public KeyPair generateKeyPair(String algorithm, String keyId, Object 
parameterSpec) throws Exception {
         LOG.info("Generating key pair for algorithm: {}, keyId: {}", 
algorithm, keyId);
 
-        KeyPairGenerator generator;
-        String provider = determineProvider(algorithm);
-
-        if (provider != null) {
-            generator = 
KeyPairGenerator.getInstance(getAlgorithmName(algorithm), provider);
-        } else {
-            generator = 
KeyPairGenerator.getInstance(getAlgorithmName(algorithm));
-        }
-
-        // Initialize with parameter spec if provided
-        if (parameterSpec != null) {
-            if (parameterSpec instanceof AlgorithmParameterSpec 
algorithmParamSpec) {
-                generator.initialize(algorithmParamSpec, 
SecureRandomHelper.getSecureRandom());
-            } else if (parameterSpec instanceof Integer keySize) {
-                generator.initialize(keySize, 
SecureRandomHelper.getSecureRandom());
-            }
-        } else {
-            // Use default parameter spec for the algorithm
-            AlgorithmParameterSpec defaultSpec = 
getDefaultParameterSpec(algorithm);
-            if (defaultSpec != null) {
-                generator.initialize(defaultSpec, 
SecureRandomHelper.getSecureRandom());
-            } else {
-                generator.initialize(getDefaultKeySize(algorithm), 
SecureRandomHelper.getSecureRandom());
-            }
-        }
-
-        KeyPair keyPair = generator.generateKeyPair();
+        KeyPair keyPair = KeyAlgorithmSupport.generateKeyPair(algorithm, 
parameterSpec);
 
         // Create metadata
         KeyMetadata metadata = new KeyMetadata(keyId, algorithm);
@@ -236,6 +207,7 @@ public class AwsSecretsManagerKeyLifecycleManager 
implements KeyLifecycleManager
     @Override
     public KeyPair rotateKey(String oldKeyId, String newKeyId, String 
algorithm) throws Exception {
         LOG.info("Rotating key from {} to {}", oldKeyId, newKeyId);
+        KeyAlgorithmSupport.validateRotationKeyIds(oldKeyId, newKeyId);
 
         // Get old key metadata
         KeyMetadata oldMetadata = getKeyMetadata(oldKeyId);
@@ -243,13 +215,13 @@ public class AwsSecretsManagerKeyLifecycleManager 
implements KeyLifecycleManager
             throw new IllegalArgumentException("Old key not found: " + 
oldKeyId);
         }
 
+        // Generate new key before deprecating the old one so a failed 
generation leaves the old key active
+        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
+
         // Mark old key as deprecated
         oldMetadata.setStatus(KeyMetadata.KeyStatus.DEPRECATED);
         updateKeyMetadata(oldKeyId, oldMetadata);
 
-        // Generate new key
-        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
-
         LOG.info("Key rotation completed in AWS Secrets Manager: {} -> {}", 
oldKeyId, newKeyId);
         return newKeyPair;
     }
@@ -545,20 +517,6 @@ public class AwsSecretsManagerKeyLifecycleManager 
implements KeyLifecycleManager
         }
     }
 
-    private String determineProvider(String algorithm) {
-        try {
-            PQCSignatureAlgorithms sigAlg = 
PQCSignatureAlgorithms.valueOf(algorithm);
-            return sigAlg.getBcProvider("KeyPairGenerator");
-        } catch (IllegalArgumentException e1) {
-            try {
-                PQCKeyEncapsulationAlgorithms kemAlg = 
PQCKeyEncapsulationAlgorithms.valueOf(algorithm);
-                return kemAlg.getBcProvider("KeyPairGenerator");
-            } catch (IllegalArgumentException e2) {
-                return null;
-            }
-        }
-    }
-
     private String getAlgorithmName(String algorithm) {
         try {
             return PQCSignatureAlgorithms.valueOf(algorithm).getAlgorithm();
@@ -571,16 +529,6 @@ public class AwsSecretsManagerKeyLifecycleManager 
implements KeyLifecycleManager
         }
     }
 
-    private AlgorithmParameterSpec getDefaultParameterSpec(String algorithm) {
-        return KeyAlgorithmSupport.getDefaultParameterSpec(algorithm);
-    }
-
-    private int getDefaultKeySize(String algorithm) {
-        // Default key sizes for different algorithms
-        // For PQC algorithms, key size is usually determined by parameter 
specs
-        return 256;
-    }
-
     /**
      * Get the underlying SecretsManagerClient for advanced operations
      */
diff --git 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AzureKeyVaultKeyLifecycleManager.java
 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AzureKeyVaultKeyLifecycleManager.java
index b3315bcbf045..deaa5b86fb4c 100644
--- 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AzureKeyVaultKeyLifecycleManager.java
+++ 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/AzureKeyVaultKeyLifecycleManager.java
@@ -185,6 +185,7 @@ public class AzureKeyVaultKeyLifecycleManager implements 
KeyLifecycleManager {
     @Override
     public KeyPair rotateKey(String oldKeyId, String newKeyId, String 
algorithm) throws Exception {
         LOG.info("Rotating key from {} to {}", oldKeyId, newKeyId);
+        KeyAlgorithmSupport.validateRotationKeyIds(oldKeyId, newKeyId);
 
         // Get old key metadata
         KeyMetadata oldMetadata = getKeyMetadata(oldKeyId);
@@ -192,13 +193,13 @@ public class AzureKeyVaultKeyLifecycleManager implements 
KeyLifecycleManager {
             throw new IllegalArgumentException("Old key not found: " + 
oldKeyId);
         }
 
+        // Generate new key before deprecating the old one so a failed 
generation leaves the old key active
+        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
+
         // Mark old key as deprecated
         oldMetadata.setStatus(KeyMetadata.KeyStatus.DEPRECATED);
         updateKeyMetadata(oldKeyId, oldMetadata);
 
-        // Generate new key
-        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
-
         LOG.info("Key rotation completed in Azure Key Vault: {} -> {}", 
oldKeyId, newKeyId);
         return newKeyPair;
     }
diff --git 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
index d8f8b1940060..613f44e72e88 100644
--- 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
+++ 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/FileBasedKeyLifecycleManager.java
@@ -153,6 +153,7 @@ public class FileBasedKeyLifecycleManager implements 
KeyLifecycleManager {
     @Override
     public KeyPair rotateKey(String oldKeyId, String newKeyId, String 
algorithm) throws Exception {
         LOG.info("Rotating key from {} to {}", oldKeyId, newKeyId);
+        KeyAlgorithmSupport.validateRotationKeyIds(oldKeyId, newKeyId);
 
         // Get old key metadata
         KeyMetadata oldMetadata = getKeyMetadata(oldKeyId);
@@ -160,13 +161,13 @@ public class FileBasedKeyLifecycleManager implements 
KeyLifecycleManager {
             throw new IllegalArgumentException("Old key not found: " + 
oldKeyId);
         }
 
+        // Generate new key before deprecating the old one so a failed 
generation leaves the old key active
+        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
+
         // Mark old key as deprecated
         oldMetadata.setStatus(KeyMetadata.KeyStatus.DEPRECATED);
         updateKeyMetadata(oldKeyId, oldMetadata);
 
-        // Generate new key
-        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
-
         LOG.info("Key rotation completed: {} -> {}", oldKeyId, newKeyId);
         return newKeyPair;
     }
diff --git 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/HashicorpVaultKeyLifecycleManager.java
 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/HashicorpVaultKeyLifecycleManager.java
index 8d1a1fe830fe..c96058050bea 100644
--- 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/HashicorpVaultKeyLifecycleManager.java
+++ 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/HashicorpVaultKeyLifecycleManager.java
@@ -19,10 +19,8 @@ package org.apache.camel.component.pqc.lifecycle;
 import java.io.ByteArrayInputStream;
 import java.io.ObjectInputStream;
 import java.security.KeyPair;
-import java.security.KeyPairGenerator;
 import java.security.PrivateKey;
 import java.security.PublicKey;
-import java.security.spec.AlgorithmParameterSpec;
 import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Base64;
@@ -34,7 +32,6 @@ import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.camel.component.pqc.PQCKeyEncapsulationAlgorithms;
 import org.apache.camel.component.pqc.PQCSignatureAlgorithms;
-import org.apache.camel.util.SecureRandomHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.vault.authentication.TokenAuthentication;
@@ -183,33 +180,7 @@ public class HashicorpVaultKeyLifecycleManager implements 
KeyLifecycleManager {
     public KeyPair generateKeyPair(String algorithm, String keyId, Object 
parameterSpec) throws Exception {
         LOG.info("Generating key pair for algorithm: {}, keyId: {}", 
algorithm, keyId);
 
-        KeyPairGenerator generator;
-        String provider = determineProvider(algorithm);
-
-        if (provider != null) {
-            generator = 
KeyPairGenerator.getInstance(getAlgorithmName(algorithm), provider);
-        } else {
-            generator = 
KeyPairGenerator.getInstance(getAlgorithmName(algorithm));
-        }
-
-        // Initialize with parameter spec if provided
-        if (parameterSpec != null) {
-            if (parameterSpec instanceof AlgorithmParameterSpec 
algorithmParamSpec) {
-                generator.initialize(algorithmParamSpec, 
SecureRandomHelper.getSecureRandom());
-            } else if (parameterSpec instanceof Integer keySize) {
-                generator.initialize(keySize, 
SecureRandomHelper.getSecureRandom());
-            }
-        } else {
-            // Use default parameter spec for the algorithm
-            AlgorithmParameterSpec defaultSpec = 
getDefaultParameterSpec(algorithm);
-            if (defaultSpec != null) {
-                generator.initialize(defaultSpec, 
SecureRandomHelper.getSecureRandom());
-            } else {
-                generator.initialize(getDefaultKeySize(algorithm), 
SecureRandomHelper.getSecureRandom());
-            }
-        }
-
-        KeyPair keyPair = generator.generateKeyPair();
+        KeyPair keyPair = KeyAlgorithmSupport.generateKeyPair(algorithm, 
parameterSpec);
 
         // Create metadata
         KeyMetadata metadata = new KeyMetadata(keyId, algorithm);
@@ -249,6 +220,7 @@ public class HashicorpVaultKeyLifecycleManager implements 
KeyLifecycleManager {
     @Override
     public KeyPair rotateKey(String oldKeyId, String newKeyId, String 
algorithm) throws Exception {
         LOG.info("Rotating key from {} to {}", oldKeyId, newKeyId);
+        KeyAlgorithmSupport.validateRotationKeyIds(oldKeyId, newKeyId);
 
         // Get old key metadata
         KeyMetadata oldMetadata = getKeyMetadata(oldKeyId);
@@ -256,13 +228,13 @@ public class HashicorpVaultKeyLifecycleManager implements 
KeyLifecycleManager {
             throw new IllegalArgumentException("Old key not found: " + 
oldKeyId);
         }
 
+        // Generate new key before deprecating the old one so a failed 
generation leaves the old key active
+        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
+
         // Mark old key as deprecated
         oldMetadata.setStatus(KeyMetadata.KeyStatus.DEPRECATED);
         updateKeyMetadata(oldKeyId, oldMetadata);
 
-        // Generate new key
-        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
-
         LOG.info("Key rotation completed in Vault: {} -> {}", oldKeyId, 
newKeyId);
         return newKeyPair;
     }
@@ -575,20 +547,6 @@ public class HashicorpVaultKeyLifecycleManager implements 
KeyLifecycleManager {
         }
     }
 
-    private String determineProvider(String algorithm) {
-        try {
-            PQCSignatureAlgorithms sigAlg = 
PQCSignatureAlgorithms.valueOf(algorithm);
-            return sigAlg.getBcProvider("KeyPairGenerator");
-        } catch (IllegalArgumentException e1) {
-            try {
-                PQCKeyEncapsulationAlgorithms kemAlg = 
PQCKeyEncapsulationAlgorithms.valueOf(algorithm);
-                return kemAlg.getBcProvider("KeyPairGenerator");
-            } catch (IllegalArgumentException e2) {
-                return null;
-            }
-        }
-    }
-
     private String getAlgorithmName(String algorithm) {
         try {
             return PQCSignatureAlgorithms.valueOf(algorithm).getAlgorithm();
@@ -601,16 +559,6 @@ public class HashicorpVaultKeyLifecycleManager implements 
KeyLifecycleManager {
         }
     }
 
-    private AlgorithmParameterSpec getDefaultParameterSpec(String algorithm) {
-        return KeyAlgorithmSupport.getDefaultParameterSpec(algorithm);
-    }
-
-    private int getDefaultKeySize(String algorithm) {
-        // Default key sizes for different algorithms
-        // For PQC algorithms, key size is usually determined by parameter 
specs
-        return 256;
-    }
-
     /**
      * Get the underlying VaultTemplate for advanced operations
      */
diff --git 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/InMemoryKeyLifecycleManager.java
 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/InMemoryKeyLifecycleManager.java
index 1ce8c7eb7a3a..909aee4b2a9f 100644
--- 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/InMemoryKeyLifecycleManager.java
+++ 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/InMemoryKeyLifecycleManager.java
@@ -90,18 +90,19 @@ public class InMemoryKeyLifecycleManager implements 
KeyLifecycleManager {
     @Override
     public KeyPair rotateKey(String oldKeyId, String newKeyId, String 
algorithm) throws Exception {
         LOG.info("Rotating key from {} to {}", oldKeyId, newKeyId);
+        KeyAlgorithmSupport.validateRotationKeyIds(oldKeyId, newKeyId);
 
         KeyMetadata oldMetadata = getKeyMetadata(oldKeyId);
         if (oldMetadata == null) {
             throw new IllegalArgumentException("Old key not found: " + 
oldKeyId);
         }
 
-        // Deprecate the old key, then generate its replacement
+        // Generate new key before deprecating the old one so a failed 
generation leaves the old key active
+        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
+
         oldMetadata.setStatus(KeyMetadata.KeyStatus.DEPRECATED);
         updateKeyMetadata(oldKeyId, oldMetadata);
 
-        KeyPair newKeyPair = generateKeyPair(algorithm, newKeyId);
-
         LOG.info("Key rotation completed: {} -> {}", oldKeyId, newKeyId);
         return newKeyPair;
     }
diff --git 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupport.java
 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupport.java
index 2501d812ae3c..433ba3ded367 100644
--- 
a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupport.java
+++ 
b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupport.java
@@ -32,13 +32,17 @@ import org.apache.camel.util.SecureRandomHelper;
  */
 public final class KeyAlgorithmSupport {
 
+    private KeyAlgorithmSupport() {
+    }
+
     /**
-     * Fallback key size, used only when an algorithm has no default parameter 
set. For PQC algorithms the key size is
-     * determined by the parameter set.
+     * Validates that rotation targets a distinct key id so generating the new 
key cannot be overwritten when the old
+     * key metadata is marked deprecated.
      */
-    private static final int DEFAULT_KEY_SIZE = 256;
-
-    private KeyAlgorithmSupport() {
+    public static void validateRotationKeyIds(String oldKeyId, String 
newKeyId) {
+        if (oldKeyId != null && oldKeyId.equals(newKeyId)) {
+            throw new IllegalArgumentException("New key id must differ from 
old key id: " + oldKeyId);
+        }
     }
 
     /**
@@ -66,11 +70,11 @@ public final class KeyAlgorithmSupport {
             }
         } else {
             AlgorithmParameterSpec defaultSpec = 
getDefaultParameterSpec(algorithm);
-            if (defaultSpec != null) {
-                generator.initialize(defaultSpec, 
SecureRandomHelper.getSecureRandom());
-            } else {
-                generator.initialize(DEFAULT_KEY_SIZE, 
SecureRandomHelper.getSecureRandom());
+            if (defaultSpec == null) {
+                throw new IllegalArgumentException(
+                        "No default parameter set for algorithm: " + algorithm 
+ "; configure the parameterSpec option");
             }
+            generator.initialize(defaultSpec, 
SecureRandomHelper.getSecureRandom());
         }
 
         return generator.generateKeyPair();
@@ -124,6 +128,10 @@ public final class KeyAlgorithmSupport {
                     return PQCParameterSpecResolver.resolve("SLHDSA", 
"SLH-DSA-SHA2-128S");
                 case "FALCON":
                     return PQCParameterSpecResolver.resolve("FALCON", 
"FALCON-512");
+                case "MAYO":
+                    return PQCParameterSpecResolver.resolve("MAYO", "mayo5");
+                case "SNOVA":
+                    return PQCParameterSpecResolver.resolve("SNOVA", 
"SNOVA_24_5_5_SHAKE_SSK");
                 case "XMSS":
                     return BouncyCastlePqcCompatibility.xmssParameterSpec(10, 
"SHA256");
                 case "XMSSMT":
diff --git 
a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupportTest.java
 
b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupportTest.java
new file mode 100644
index 000000000000..e49bf28346a2
--- /dev/null
+++ 
b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/lifecycle/KeyAlgorithmSupportTest.java
@@ -0,0 +1,142 @@
+/*
+ * 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.lifecycle;
+
+import java.nio.file.Path;
+import java.security.KeyPair;
+import java.security.Security;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.camel.component.pqc.PQCKeyEncapsulationAlgorithms;
+import org.apache.camel.component.pqc.PQCSignatureAlgorithms;
+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 org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class KeyAlgorithmSupportTest {
+
+    @BeforeEach
+    void ensureProviders() {
+        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
+            Security.addProvider(new BouncyCastleProvider());
+        }
+        if (Security.getProvider(BouncyCastlePQCProvider.PROVIDER_NAME) == 
null) {
+            Security.addProvider(new BouncyCastlePQCProvider());
+        }
+    }
+
+    @Test
+    void defaultParameterSpecCoversAllSignatureAndKemAlgorithms() {
+        for (PQCSignatureAlgorithms algorithm : 
PQCSignatureAlgorithms.values()) {
+            AlgorithmParameterSpec spec = 
KeyAlgorithmSupport.getDefaultParameterSpec(algorithm.name());
+            assertNotNull(spec, "Missing default parameter spec for signature 
algorithm " + algorithm.name());
+        }
+        for (PQCKeyEncapsulationAlgorithms algorithm : 
PQCKeyEncapsulationAlgorithms.values()) {
+            AlgorithmParameterSpec spec = 
KeyAlgorithmSupport.getDefaultParameterSpec(algorithm.name());
+            assertNotNull(spec, "Missing default parameter spec for KEM 
algorithm " + algorithm.name());
+        }
+    }
+
+    @Test
+    void defaultParameterSpecResolvesMayoAndSnova() {
+        
assertNotNull(KeyAlgorithmSupport.getDefaultParameterSpec(PQCSignatureAlgorithms.MAYO.name()));
+        
assertNotNull(KeyAlgorithmSupport.getDefaultParameterSpec(PQCSignatureAlgorithms.SNOVA.name()));
+        assertNull(KeyAlgorithmSupport.getDefaultParameterSpec("RSA"));
+    }
+
+    @Test
+    void generateKeyPairFailsWithoutDefaultParameterSpec() {
+        IllegalArgumentException ex = 
assertThrows(IllegalArgumentException.class,
+                () -> KeyAlgorithmSupport.generateKeyPair("RSA", null));
+        assertTrue(ex.getMessage().contains("parameterSpec"));
+    }
+
+    @Test
+    void generateAndRotateKeyPairUsesDefaultParameterSetForMayoAndSnova() 
throws Exception {
+        InMemoryKeyLifecycleManager keyManager = new 
InMemoryKeyLifecycleManager();
+
+        KeyPair mayoKey = 
keyManager.generateKeyPair(PQCSignatureAlgorithms.MAYO.name(), "mayo-1");
+        assertNotNull(mayoKey);
+        assertNotNull(mayoKey.getPublic());
+        assertNotNull(mayoKey.getPrivate());
+
+        KeyPair mayoRotated = keyManager.rotateKey("mayo-1", "mayo-2", 
PQCSignatureAlgorithms.MAYO.name());
+        assertNotNull(mayoRotated);
+        assertEquals(KeyMetadata.KeyStatus.DEPRECATED, 
keyManager.getKeyMetadata("mayo-1").getStatus());
+        assertEquals(KeyMetadata.KeyStatus.ACTIVE, 
keyManager.getKeyMetadata("mayo-2").getStatus());
+
+        KeyPair snovaKey = 
keyManager.generateKeyPair(PQCSignatureAlgorithms.SNOVA.name(), "snova-1");
+        assertNotNull(snovaKey);
+        assertNotNull(snovaKey.getPublic());
+        assertNotNull(snovaKey.getPrivate());
+
+        KeyPair snovaRotated = keyManager.rotateKey("snova-1", "snova-2", 
PQCSignatureAlgorithms.SNOVA.name());
+        assertNotNull(snovaRotated);
+        assertEquals(KeyMetadata.KeyStatus.DEPRECATED, 
keyManager.getKeyMetadata("snova-1").getStatus());
+        assertEquals(KeyMetadata.KeyStatus.ACTIVE, 
keyManager.getKeyMetadata("snova-2").getStatus());
+    }
+
+    @Test
+    void failedRotationLeavesOldKeyActive() throws Exception {
+        InMemoryKeyLifecycleManager keyManager = new 
InMemoryKeyLifecycleManager();
+        keyManager.generateKeyPair(PQCSignatureAlgorithms.MAYO.name(), 
"active-key");
+
+        assertThrows(IllegalArgumentException.class,
+                () -> keyManager.rotateKey("active-key", "replacement-key", 
"RSA"));
+
+        assertEquals(KeyMetadata.KeyStatus.ACTIVE, 
keyManager.getKeyMetadata("active-key").getStatus());
+        assertNull(keyManager.getKeyMetadata("replacement-key"));
+    }
+
+    @Test
+    void rotationRejectsSameOldAndNewKeyId() throws Exception {
+        InMemoryKeyLifecycleManager keyManager = new 
InMemoryKeyLifecycleManager();
+        keyManager.generateKeyPair(PQCSignatureAlgorithms.MAYO.name(), 
"same-key");
+
+        IllegalArgumentException ex = 
assertThrows(IllegalArgumentException.class,
+                () -> keyManager.rotateKey("same-key", "same-key", 
PQCSignatureAlgorithms.MAYO.name()));
+        assertTrue(ex.getMessage().contains("must differ"));
+        assertEquals(KeyMetadata.KeyStatus.ACTIVE, 
keyManager.getKeyMetadata("same-key").getStatus());
+    }
+
+    @Test
+    void fileBasedManagerGeneratesAndRotatesMayoAndSnova(@TempDir Path 
tempDir) throws Exception {
+        FileBasedKeyLifecycleManager keyManager = new 
FileBasedKeyLifecycleManager(tempDir.toString());
+
+        KeyPair mayoKey = 
keyManager.generateKeyPair(PQCSignatureAlgorithms.MAYO.name(), "fb-mayo-1");
+        assertNotNull(mayoKey);
+        KeyPair mayoRotated = keyManager.rotateKey("fb-mayo-1", "fb-mayo-2", 
PQCSignatureAlgorithms.MAYO.name());
+        assertNotNull(mayoRotated);
+        assertEquals(KeyMetadata.KeyStatus.DEPRECATED, 
keyManager.getKeyMetadata("fb-mayo-1").getStatus());
+        assertEquals(KeyMetadata.KeyStatus.ACTIVE, 
keyManager.getKeyMetadata("fb-mayo-2").getStatus());
+
+        KeyPair snovaKey = 
keyManager.generateKeyPair(PQCSignatureAlgorithms.SNOVA.name(), "fb-snova-1");
+        assertNotNull(snovaKey);
+        KeyPair snovaRotated = keyManager.rotateKey("fb-snova-1", 
"fb-snova-2", PQCSignatureAlgorithms.SNOVA.name());
+        assertNotNull(snovaRotated);
+        assertEquals(KeyMetadata.KeyStatus.DEPRECATED, 
keyManager.getKeyMetadata("fb-snova-1").getStatus());
+        assertEquals(KeyMetadata.KeyStatus.ACTIVE, 
keyManager.getKeyMetadata("fb-snova-2").getStatus());
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index 48065d855320..5b6a1d50e997 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -155,6 +155,14 @@ The producer body metadata for KEM operations now declares 
the standard `javax.c
 Bouncy Castle-specific carrier type. Applications should accept the JCA 
`SecretKey` interface; the runtime may still
 return Bouncy Castle's encapsulation-carrying implementation when that 
provider class is available.
 
+=== camel-pqc - key lifecycle default parameter sets and rotation validation
+
+`KeyAlgorithmSupport.generateKeyPair(algorithm, null)` and the 
`KeyLifecycleManager` implementations no longer fall
+back to `KeyPairGenerator.initialize(256, random)` when an algorithm has no 
default `AlgorithmParameterSpec` mapping.
+Calling key generation without an explicit `parameterSpec` for algorithms 
without a default parameter set (for example
+`EC`) now throws an `IllegalArgumentException`. In addition, 
`KeyLifecycleManager.rotateKey` now rejects calls where
+`oldKeyId` equals `newKeyId`.
+
 === camel-kafka, camel-kafka-share - sslContextParameters now configures the 
complete TLS engine
 
 The Kafka components now create their TLS engines from the complete 
`SSLContext` produced by Camel's

Reply via email to