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 c0781c299bc Camel 21097 (#15218)
c0781c299bc is described below
commit c0781c299bcad722ec74e8f159b6b537ef6e2cce
Author: Jürgen Link <[email protected]>
AuthorDate: Tue Aug 20 19:37:15 2024 +0200
Camel 21097 (#15218)
* Update AS2MicAlgorithm.java
Add MICALG's
- sha256
- sha384
- sha512
Allow to retrieve JdkAlgorithmName both with or without dash (i.e. by
"sha1" or "sha-1")
* Update MicUtilsTest.java
Add parameterized test to verify MICALG's
- md5
- sha1
- sha256
- sha384
- sha512
are handled correctly
* Regen
* Remove effectless map step
* Add unit test to verify common AS2 algorithm identifier are found
* Regen
---------
Co-authored-by: Claus Ibsen <[email protected]>
Co-authored-by: jlink <[email protected]>
---
.../camel/component/as2/api/AS2MicAlgorithm.java | 48 ++++++++++++------
.../component/as2/api/AS2MicAlgorithmTest.java | 59 ++++++++++++++++++++++
.../camel/component/as2/api/util/MicUtilsTest.java | 43 +++++++++++++++-
3 files changed, 132 insertions(+), 18 deletions(-)
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2MicAlgorithm.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2MicAlgorithm.java
index 32d2607b877..ce0e8795506 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2MicAlgorithm.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2MicAlgorithm.java
@@ -16,9 +16,17 @@
*/
package org.apache.camel.component.as2.api;
+import java.util.Optional;
+
interface Constants {
String SHA_1_AS2_ALGORITHM_NAME = "sha1";
+ String SHA_256_AS2_ALGORITHM_NAME = "sha256";
+ String SHA_384_AS2_ALGORITHM_NAME = "sha384";
+ String SHA_512_AS2_ALGORITHM_NAME = "sha512";
String SHA_1_JDK_ALGORITHM_NAME = "SHA-1";
+ String SHA_256_JDK_ALGORITHM_NAME = "SHA-256";
+ String SHA_384_JDK_ALGORITHM_NAME = "SHA-384";
+ String SHA_512_JDK_ALGORITHM_NAME = "SHA-512";
String MD5_AS2_ALGORITHM_NAME = "md5";
String MD5_JDK_ALGORITHM_NAME = "MD5";
@@ -26,6 +34,9 @@ interface Constants {
public enum AS2MicAlgorithm {
SHA_1(Constants.SHA_1_JDK_ALGORITHM_NAME,
Constants.SHA_1_AS2_ALGORITHM_NAME),
+ SHA_256(Constants.SHA_256_JDK_ALGORITHM_NAME,
Constants.SHA_256_AS2_ALGORITHM_NAME),
+ SHA_384(Constants.SHA_384_JDK_ALGORITHM_NAME,
Constants.SHA_384_AS2_ALGORITHM_NAME),
+ SHA_512(Constants.SHA_512_JDK_ALGORITHM_NAME,
Constants.SHA_512_AS2_ALGORITHM_NAME),
MD5(Constants.MD5_JDK_ALGORITHM_NAME, Constants.MD5_AS2_ALGORITHM_NAME);
private final String jdkAlgorithmName;
@@ -45,25 +56,30 @@ public enum AS2MicAlgorithm {
}
public static String getJdkAlgorithmName(String as2AlgorithmName) {
- switch (as2AlgorithmName) {
- case Constants.SHA_1_AS2_ALGORITHM_NAME:
- return Constants.SHA_1_JDK_ALGORITHM_NAME;
- case Constants.MD5_AS2_ALGORITHM_NAME:
- return Constants.MD5_JDK_ALGORITHM_NAME;
- default:
- return null;
- }
+ return Optional.ofNullable(as2AlgorithmName)
+ .map(alg -> alg.replaceAll("-", ""))
+ .map(alg -> switch (alg) {
+ case Constants.MD5_AS2_ALGORITHM_NAME ->
Constants.MD5_JDK_ALGORITHM_NAME;
+ case Constants.SHA_1_AS2_ALGORITHM_NAME ->
Constants.SHA_1_JDK_ALGORITHM_NAME;
+ case Constants.SHA_256_AS2_ALGORITHM_NAME ->
Constants.SHA_256_JDK_ALGORITHM_NAME;
+ case Constants.SHA_384_AS2_ALGORITHM_NAME ->
Constants.SHA_384_JDK_ALGORITHM_NAME;
+ case Constants.SHA_512_AS2_ALGORITHM_NAME ->
Constants.SHA_512_JDK_ALGORITHM_NAME;
+ default -> null;
+ })
+ .orElse(null);
}
public static String getAS2AlgorithmName(String jdkAlgorithmName) {
- switch (jdkAlgorithmName) {
- case Constants.MD5_JDK_ALGORITHM_NAME:
- return Constants.MD5_AS2_ALGORITHM_NAME;
- case Constants.SHA_1_JDK_ALGORITHM_NAME:
- return Constants.SHA_1_AS2_ALGORITHM_NAME;
- default:
- return null;
- }
+ return Optional.ofNullable(jdkAlgorithmName)
+ .map(alg -> switch (alg) {
+ case Constants.MD5_JDK_ALGORITHM_NAME ->
Constants.MD5_AS2_ALGORITHM_NAME;
+ case Constants.SHA_1_JDK_ALGORITHM_NAME ->
Constants.SHA_1_AS2_ALGORITHM_NAME;
+ case Constants.SHA_256_JDK_ALGORITHM_NAME ->
Constants.SHA_256_AS2_ALGORITHM_NAME;
+ case Constants.SHA_384_JDK_ALGORITHM_NAME ->
Constants.SHA_384_AS2_ALGORITHM_NAME;
+ case Constants.SHA_512_JDK_ALGORITHM_NAME ->
Constants.SHA_512_AS2_ALGORITHM_NAME;
+ default -> null;
+ })
+ .orElse(null);
}
}
diff --git
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MicAlgorithmTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MicAlgorithmTest.java
new file mode 100644
index 00000000000..41bcf0df7dc
--- /dev/null
+++
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MicAlgorithmTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.as2.api;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class AS2MicAlgorithmTest {
+
+ @Test
+ void test_md5() {
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("md5"),
AS2MicAlgorithm.MD5.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("md-5"),
AS2MicAlgorithm.MD5.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getAS2AlgorithmName("MD5"),
AS2MicAlgorithm.MD5.getAs2AlgorithmName());
+ }
+
+ @Test
+ void test_sha1() {
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha1"),
AS2MicAlgorithm.SHA_1.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha-1"),
AS2MicAlgorithm.SHA_1.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getAS2AlgorithmName("SHA-1"),
AS2MicAlgorithm.SHA_1.getAs2AlgorithmName());
+ }
+
+ @Test
+ void test_sha256() {
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha256"),
AS2MicAlgorithm.SHA_256.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha-256"),
AS2MicAlgorithm.SHA_256.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getAS2AlgorithmName("SHA-256"),
AS2MicAlgorithm.SHA_256.getAs2AlgorithmName());
+ }
+
+ @Test
+ void test_sha384() {
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha384"),
AS2MicAlgorithm.SHA_384.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha-384"),
AS2MicAlgorithm.SHA_384.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getAS2AlgorithmName("SHA-384"),
AS2MicAlgorithm.SHA_384.getAs2AlgorithmName());
+ }
+
+ @Test
+ void test_sha512() {
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha512"),
AS2MicAlgorithm.SHA_512.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getJdkAlgorithmName("sha-512"),
AS2MicAlgorithm.SHA_512.getJdkAlgorithmName());
+ assertEquals(AS2MicAlgorithm.getAS2AlgorithmName("SHA-512"),
AS2MicAlgorithm.SHA_512.getAs2AlgorithmName());
+ }
+}
diff --git
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
index e16abdc3f5f..a55183d2dcf 100644
---
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
+++
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.as2.api.util;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.Security;
+import java.util.Base64;
import org.apache.camel.component.as2.api.AS2Header;
import org.apache.camel.component.as2.api.AS2MimeType;
@@ -32,9 +33,12 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import static org.apache.camel.component.as2.api.util.MicUtils.createMic;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -76,12 +80,12 @@ public class MicUtilsTest {
private static final String EXPECTED_ENCODED_MESSAGE_DIGEST =
"XUt+ug5GEDD0X9+Nv8DGYZZThOQ=";
@BeforeEach
- public void setUp() throws Exception {
+ public void setUp() {
Security.addProvider(new BouncyCastleProvider());
}
@AfterEach
- public void tearDown() throws Exception {
+ public void tearDown() {
}
@Test
@@ -108,4 +112,39 @@ public class MicUtilsTest {
"Unexpected encoded message digest value");
}
+ @ParameterizedTest
+ @ValueSource(strings = { "md5", "sha1", "sha256", "sha384", "sha512" })
+ public void createReceivedContentMicTest(String requestedMicalg) throws
Exception {
+ String DNO_TEMPLATE = "signed-receipt-protocol=optional,
pkcs7-signature; signed-receipt-micalg = required, %s";
+
+ BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST",
"/");
+ request.addHeader(AS2Header.DISPOSITION_NOTIFICATION_OPTIONS,
DNO_TEMPLATE.formatted(requestedMicalg));
+ request.addHeader(AS2Header.CONTENT_TYPE, CONTENT_TYPE_VALUE);
+
+ ApplicationEDIFACTEntity edifactEntity
+ = new ApplicationEDIFACTEntity(
+ EDI_MESSAGE, StandardCharsets.US_ASCII.name(),
AS2TransferEncoding.NONE, true, "filename.txt");
+ InputStream is = edifactEntity.getContent();
+ BasicHttpEntity basicEntity = new BasicHttpEntity(is,
ContentType.create(CONTENT_TYPE_VALUE));
+ request.setEntity(basicEntity);
+
+ ReceivedContentMic receivedContentMic =
MicUtils.createReceivedContentMic(request, null, null);
+ assertNotNull(receivedContentMic, "Failed to create Received Content
MIC");
+ LOG.debug("Digest Algorithm: {}",
receivedContentMic.getDigestAlgorithmId());
+ assertEquals(requestedMicalg,
receivedContentMic.getDigestAlgorithmId(),
+ "Unexpected digest algorithm value");
+ LOG.debug("Encoded Message Digest: {}",
receivedContentMic.getEncodedMessageDigest());
+ String expectedMic = getMicContent(EDI_MESSAGE, requestedMicalg);
+ assertEquals(expectedMic, receivedContentMic.getEncodedMessageDigest(),
+ "Unexpected encoded message digest value");
+ }
+
+ private String getMicContent(String content, String algorithm) {
+ return new String(
+ Base64.getEncoder().encode(
+ createMic(content
+ .replaceAll("\\n", "\r\n")
+ .getBytes(StandardCharsets.US_ASCII),
algorithm)),
+ StandardCharsets.US_ASCII);
+ }
}