This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch CAMEL-21971 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 51a4a33200865ca9c8f77e74cb05d588a63bd51b Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Wed Apr 16 13:58:09 2025 +0200 CAMEL-21971 - camel-pqc - Provide default KeyPair and Signature Signed-off-by: Andrea Cosentino <anco...@gmail.com> --- .../apache/camel/component/pqc/PQCComponent.java | 30 +++++++++ .../pqc/crypto/PQCDefaultLMSMaterial.java | 46 +++++++++++++ .../pqc/crypto/PQCDefaultMLDSAMaterial.java | 44 +++++++++++++ .../pqc/crypto/PQCDefaultSLHDSAMaterial.java | 44 +++++++++++++ .../pqc/crypto/PQCDefaultXMSSMaterial.java | 44 +++++++++++++ .../pqc/PQCSignatureLMSNoAutowiredTest.java | 74 +++++++++++++++++++++ .../pqc/PQCSignatureMLDSANoAutowiredTest.java | 73 +++++++++++++++++++++ .../pqc/PQCSignatureSLHDSANoAutowiredTest.java | 74 +++++++++++++++++++++ .../pqc/PQCSignatureXMSSNoAutowiredTest.java | 76 ++++++++++++++++++++++ 9 files changed, 505 insertions(+) diff --git a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCComponent.java b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCComponent.java index 8028e79aafb..0007ef8d2a4 100644 --- a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCComponent.java +++ b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCComponent.java @@ -20,9 +20,14 @@ import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; +import org.apache.camel.component.pqc.crypto.PQCDefaultLMSMaterial; +import org.apache.camel.component.pqc.crypto.PQCDefaultMLDSAMaterial; +import org.apache.camel.component.pqc.crypto.PQCDefaultSLHDSAMaterial; +import org.apache.camel.component.pqc.crypto.PQCDefaultXMSSMaterial; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.HealthCheckComponent; +import org.apache.camel.util.ObjectHelper; /** * For working with Post Quantum Cryptography Algorithms @@ -48,6 +53,31 @@ public class PQCComponent extends HealthCheckComponent { PQCEndpoint endpoint = new PQCEndpoint(uri, this, configuration); setProperties(endpoint, parameters); + if (ObjectHelper.isEmpty(configuration.getSigner()) && ObjectHelper.isEmpty(configuration.getKeyPair())) { + switch (configuration.getSignatureAlgorithm()) { + case "MLDSA": + configuration.setSigner(PQCDefaultMLDSAMaterial.signer); + configuration.setKeyPair(PQCDefaultMLDSAMaterial.keyPair); + break; + case "SLHDSA": + configuration.setSigner(PQCDefaultSLHDSAMaterial.signer); + configuration.setKeyPair(PQCDefaultSLHDSAMaterial.keyPair); + break; + case "LMS": + configuration.setSigner(PQCDefaultLMSMaterial.signer); + configuration.setKeyPair(PQCDefaultLMSMaterial.keyPair); + break; + case "XMSS": + configuration.setSigner(PQCDefaultXMSSMaterial.signer); + configuration.setKeyPair(PQCDefaultXMSSMaterial.keyPair); + break; + default: + break; + } + ; + + } + return endpoint; } diff --git a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultLMSMaterial.java b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultLMSMaterial.java new file mode 100644 index 00000000000..2068f31d18c --- /dev/null +++ b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultLMSMaterial.java @@ -0,0 +1,46 @@ +/* + * 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.crypto; + +import java.security.*; + +import org.bouncycastle.pqc.crypto.lms.LMOtsParameters; +import org.bouncycastle.pqc.crypto.lms.LMSigParameters; +import org.bouncycastle.pqc.jcajce.spec.LMSKeyGenParameterSpec; + +public class PQCDefaultLMSMaterial { + public static final KeyPair keyPair; + public static final Signature signer; + + static { + KeyPairGenerator generator; + try { + generator = prepareKeyPair(); + keyPair = generator.generateKeyPair(); + signer = Signature.getInstance("LMS"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected static KeyPairGenerator prepareKeyPair() + throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + KeyPairGenerator kpGen = KeyPairGenerator.getInstance("LMS", "BC"); + kpGen.initialize(new LMSKeyGenParameterSpec(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w1)); + return kpGen; + } +} diff --git a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultMLDSAMaterial.java b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultMLDSAMaterial.java new file mode 100644 index 00000000000..cf72a0f96f0 --- /dev/null +++ b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultMLDSAMaterial.java @@ -0,0 +1,44 @@ +/* + * 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.crypto; + +import java.security.*; + +import org.bouncycastle.jcajce.spec.MLDSAParameterSpec; + +public class PQCDefaultMLDSAMaterial { + public static final KeyPair keyPair; + public static final Signature signer; + + static { + KeyPairGenerator generator; + try { + generator = prepareKeyPair(); + keyPair = generator.generateKeyPair(); + signer = Signature.getInstance("ML-DSA"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected static KeyPairGenerator prepareKeyPair() + throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + KeyPairGenerator kpGen = KeyPairGenerator.getInstance("ML-DSA", "BC"); + kpGen.initialize(MLDSAParameterSpec.ml_dsa_65, new SecureRandom()); + return kpGen; + } +} diff --git a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultSLHDSAMaterial.java b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultSLHDSAMaterial.java new file mode 100644 index 00000000000..16f6aa22e81 --- /dev/null +++ b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultSLHDSAMaterial.java @@ -0,0 +1,44 @@ +/* + * 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.crypto; + +import java.security.*; + +import org.bouncycastle.jcajce.spec.SLHDSAParameterSpec; + +public class PQCDefaultSLHDSAMaterial { + public static final KeyPair keyPair; + public static final Signature signer; + + static { + KeyPairGenerator generator; + try { + generator = prepareKeyPair(); + keyPair = generator.generateKeyPair(); + signer = Signature.getInstance("SLH-DSA"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected static KeyPairGenerator prepareKeyPair() + throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + KeyPairGenerator kpGen = KeyPairGenerator.getInstance("SLH-DSA", "BC"); + kpGen.initialize(SLHDSAParameterSpec.slh_dsa_sha2_128s, new SecureRandom()); + return kpGen; + } +} diff --git a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultXMSSMaterial.java b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultXMSSMaterial.java new file mode 100644 index 00000000000..75379b5a0a3 --- /dev/null +++ b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/PQCDefaultXMSSMaterial.java @@ -0,0 +1,44 @@ +/* + * 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.crypto; + +import java.security.*; + +import org.bouncycastle.pqc.jcajce.spec.XMSSParameterSpec; + +public class PQCDefaultXMSSMaterial { + public static final KeyPair keyPair; + public static final Signature signer; + + static { + KeyPairGenerator generator; + try { + generator = prepareKeyPair(); + keyPair = generator.generateKeyPair(); + signer = Signature.getInstance("XMSS"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected static KeyPairGenerator prepareKeyPair() + throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + KeyPairGenerator kpGen = KeyPairGenerator.getInstance("XMSS", "BCPQC"); + kpGen.initialize(new XMSSParameterSpec(10, XMSSParameterSpec.SHA256), new SecureRandom()); + return kpGen; + } +} diff --git a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureLMSNoAutowiredTest.java b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureLMSNoAutowiredTest.java new file mode 100644 index 00000000000..0a907a4127b --- /dev/null +++ b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureLMSNoAutowiredTest.java @@ -0,0 +1,74 @@ +/* + * 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.NoSuchAlgorithmException; +import java.security.Security; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PQCSignatureLMSNoAutowiredTest extends CamelTestSupport { + + @EndpointInject("mock:sign") + protected MockEndpoint resultSign; + + @EndpointInject("mock:verify") + protected MockEndpoint resultVerify; + + @Produce("direct:sign") + protected ProducerTemplate templateSign; + + public PQCSignatureLMSNoAutowiredTest() throws NoSuchAlgorithmException { + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:sign").to("pqc:sign?operation=sign&signatureAlgorithm=LMS").to("mock:sign") + .to("pqc:verify?operation=verify&signatureAlgorithm=LMS") + .to("mock:verify"); + } + }; + } + + @BeforeAll + public static void startup() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testSignAndVerify() throws Exception { + resultSign.expectedMessageCount(1); + resultVerify.expectedMessageCount(1); + templateSign.sendBody("Hello"); + resultSign.assertIsSatisfied(); + resultVerify.assertIsSatisfied(); + assertTrue(resultVerify.getExchanges().get(0).getMessage().getHeader(PQCConstants.VERIFY, Boolean.class)); + } +} diff --git a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureMLDSANoAutowiredTest.java b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureMLDSANoAutowiredTest.java new file mode 100644 index 00000000000..59453071d09 --- /dev/null +++ b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureMLDSANoAutowiredTest.java @@ -0,0 +1,73 @@ +/* + * 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.*; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PQCSignatureMLDSANoAutowiredTest extends CamelTestSupport { + + @EndpointInject("mock:sign") + protected MockEndpoint resultSign; + + @EndpointInject("mock:verify") + protected MockEndpoint resultVerify; + + @Produce("direct:sign") + protected ProducerTemplate templateSign; + + public PQCSignatureMLDSANoAutowiredTest() throws NoSuchAlgorithmException { + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:sign").to("pqc:sign?operation=sign&signatureAlgorithm=MLDSA").to("mock:sign") + .to("pqc:verify?operation=verify&signatureAlgorithm=MLDSA") + .to("mock:verify"); + } + }; + } + + @BeforeAll + public static void startup() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testSignAndVerify() throws Exception { + resultSign.expectedMessageCount(1); + resultVerify.expectedMessageCount(1); + templateSign.sendBody("Hello"); + resultSign.assertIsSatisfied(); + resultVerify.assertIsSatisfied(); + assertTrue(resultVerify.getExchanges().get(0).getMessage().getHeader(PQCConstants.VERIFY, Boolean.class)); + } +} diff --git a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureSLHDSANoAutowiredTest.java b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureSLHDSANoAutowiredTest.java new file mode 100644 index 00000000000..65086098f60 --- /dev/null +++ b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureSLHDSANoAutowiredTest.java @@ -0,0 +1,74 @@ +/* + * 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.NoSuchAlgorithmException; +import java.security.Security; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PQCSignatureSLHDSANoAutowiredTest extends CamelTestSupport { + + @EndpointInject("mock:sign") + protected MockEndpoint resultSign; + + @EndpointInject("mock:verify") + protected MockEndpoint resultVerify; + + @Produce("direct:sign") + protected ProducerTemplate templateSign; + + public PQCSignatureSLHDSANoAutowiredTest() throws NoSuchAlgorithmException { + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:sign").to("pqc:sign?operation=sign&signatureAlgorithm=SLHDSA").to("mock:sign") + .to("pqc:verify?operation=verify&signatureAlgorithm=SLHDSA") + .to("mock:verify"); + } + }; + } + + @BeforeAll + public static void startup() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testSignAndVerify() throws Exception { + resultSign.expectedMessageCount(1); + resultVerify.expectedMessageCount(1); + templateSign.sendBody("Hello"); + resultSign.assertIsSatisfied(); + resultVerify.assertIsSatisfied(); + assertTrue(resultVerify.getExchanges().get(0).getMessage().getHeader(PQCConstants.VERIFY, Boolean.class)); + } +} diff --git a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureXMSSNoAutowiredTest.java b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureXMSSNoAutowiredTest.java new file mode 100644 index 00000000000..f6e4c3b8f3c --- /dev/null +++ b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/PQCSignatureXMSSNoAutowiredTest.java @@ -0,0 +1,76 @@ +/* + * 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.NoSuchAlgorithmException; +import java.security.Security; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PQCSignatureXMSSNoAutowiredTest extends CamelTestSupport { + + @EndpointInject("mock:sign") + protected MockEndpoint resultSign; + + @EndpointInject("mock:verify") + protected MockEndpoint resultVerify; + + @Produce("direct:sign") + protected ProducerTemplate templateSign; + + public PQCSignatureXMSSNoAutowiredTest() throws NoSuchAlgorithmException { + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:sign").to("pqc:sign?operation=sign&signatureAlgorithm=XMSS").to("mock:sign") + .to("pqc:verify?operation=verify&signatureAlgorithm=XMSS") + .to("mock:verify"); + } + }; + } + + @BeforeAll + public static void startup() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + Security.addProvider(new BouncyCastlePQCProvider()); + } + + @Test + void testSignAndVerify() throws Exception { + resultSign.expectedMessageCount(1); + resultVerify.expectedMessageCount(1); + templateSign.sendBody("Hello"); + resultSign.assertIsSatisfied(); + resultVerify.assertIsSatisfied(); + assertTrue(resultVerify.getExchanges().get(0).getMessage().getHeader(PQCConstants.VERIFY, Boolean.class)); + } +}