[ 
https://issues.apache.org/jira/browse/CAMEL-12605?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16587013#comment-16587013
 ] 

ASF GitHub Bot commented on CAMEL-12605:
----------------------------------------

davsclaus closed pull request #2489: [CAMEL-12605] First cut at mime envelope 
entity
URL: https://github.com/apache/camel/pull/2489
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7Mime.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7Mime.java
new file mode 100644
index 00000000000..88d70b673ad
--- /dev/null
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7Mime.java
@@ -0,0 +1,106 @@
+/**
+ * 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.entity;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.camel.component.as2.api.AS2Charset;
+import org.apache.camel.component.as2.api.AS2Header;
+import org.apache.camel.component.as2.api.CanonicalOutputStream;
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+import org.apache.http.HttpException;
+import org.apache.http.entity.ContentType;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.Args;
+import org.bouncycastle.cms.CMSEnvelopedData;
+import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
+import org.bouncycastle.cms.CMSProcessableByteArray;
+import org.bouncycastle.cms.CMSTypedData;
+import org.bouncycastle.operator.OutputEncryptor;
+
+public class ApplicationPkcs7Mime extends MimeEntity {
+    
+    private static final String CONTENT_DISPOSITION = "attachment; 
filename=\"smime.p7m\"";
+    
+    private byte[] encryptedData;
+    
+    public ApplicationPkcs7Mime(MimeEntity entity2Encrypt,
+                                CMSEnvelopedDataGenerator dataGenerator,
+                                OutputEncryptor encryptor,
+                                String encryptedContentTransferEncoding,
+                                boolean isMainBody)
+            throws HttpException {
+        setContentType(ContentType.create("application/pkcs7-mime", new 
BasicNameValuePair("smime-type", "enveloped-datat"),
+                new BasicNameValuePair("name", "smime.p7m")));
+        setContentTransferEncoding(encryptedContentTransferEncoding);
+        addHeader(AS2Header.CONTENT_DISPOSITION, CONTENT_DISPOSITION);
+        setMainBody(isMainBody);
+        try {
+            this.encryptedData = createEncryptedData(entity2Encrypt, 
dataGenerator, encryptor);
+        } catch (Exception e) {
+            throw new HttpException("Failed to create encrypted data");
+        }
+    }
+    
+    public ApplicationPkcs7Mime(byte[] encryptedData, String 
encryptedContentTransferEncoding, boolean isMainBody) {
+        this.encryptedData = Args.notNull(encryptedData, "encryptedData");
+        
+        setContentType(ContentType.create("application/pkcs7-mime", new 
BasicNameValuePair("smime-type", "enveloped-datat"),
+                new BasicNameValuePair("name", "smime.p7m")));
+        setContentTransferEncoding(encryptedContentTransferEncoding);
+        addHeader(AS2Header.CONTENT_DISPOSITION, CONTENT_DISPOSITION);
+        setMainBody(isMainBody);
+    }
+    
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException {
+        NoCloseOutputStream ncos = new NoCloseOutputStream(outstream);
+
+        // Write out mime part headers if this is not the main body of message.
+        if (!isMainBody()) {
+            try (CanonicalOutputStream canonicalOutstream = new 
CanonicalOutputStream(ncos, AS2Charset.US_ASCII)) {
+
+                HeaderIterator it = headerIterator();
+                while (it.hasNext()) {
+                    Header header = it.nextHeader();
+                    canonicalOutstream.writeln(header.toString());
+                }
+                canonicalOutstream.writeln(); // ensure empty line between
+                                              // headers and body; RFC2046 -
+                                              // 5.1.1
+            }
+        }
+        
+    }
+    
+    private byte[] createEncryptedData(MimeEntity entity2Encrypt, 
CMSEnvelopedDataGenerator dataGenerator, OutputEncryptor encryptor) throws 
Exception {
+        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+            entity2Encrypt.writeTo(bos);
+            bos.flush();
+
+            CMSTypedData contentData = new 
CMSProcessableByteArray(bos.toByteArray());
+            CMSEnvelopedData  envelopedData = 
dataGenerator.generate(contentData, encryptor);
+            return envelopedData.getEncoded();
+        } catch (Exception e) {
+            throw new Exception("", e);
+        }
+    }
+
+}
diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7SignatureEntity.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7SignatureEntity.java
index 179d82b1854..30f0c95e465 100644
--- 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7SignatureEntity.java
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7SignatureEntity.java
@@ -60,12 +60,13 @@ public ApplicationPkcs7SignatureEntity(MimeEntity data, 
CMSSignedDataGenerator s
         }
     }
 
-    public ApplicationPkcs7SignatureEntity(String charset,
+    public ApplicationPkcs7SignatureEntity(byte[] signature,
+                                           String charset,
                                            String contentTransferEncoding,
-                                           byte[] signature,
                                            boolean isMainBody)
             throws HttpException {
-        this.signature = signature;
+        this.signature = Args.notNull(signature, "signature");
+        
         ContentType contentType = ContentType
                 
.parse(EntityUtils.appendParameter(AS2MediaType.APPLICATION_PKCS7_SIGNATURE, 
"charset", charset));
         setContentType(contentType.toString());
diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
index e4e74e0306b..4e86d148a85 100644
--- 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
@@ -752,7 +752,7 @@ public static ApplicationPkcs7SignatureEntity 
parseApplicationPkcs7SignatureEnti
 
             String charsetName = charset.toString();
             ApplicationPkcs7SignatureEntity applicationPkcs7SignatureEntity = 
new ApplicationPkcs7SignatureEntity(
-                    charsetName, contentTransferEncoding, signature, false);
+                    signature, charsetName, contentTransferEncoding, false);
             return applicationPkcs7SignatureEntity;
         } catch (Exception e) {
             ParseException parseException = new ParseException("failed to 
parse PKCS7 Signature entity");


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Enhance the AS2 Component to send and receive encrypted AS2 messages
> --------------------------------------------------------------------
>
>                 Key: CAMEL-12605
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12605
>             Project: Camel
>          Issue Type: Improvement
>    Affects Versions: 2.23.0
>            Reporter: William Collins
>            Assignee: William Collins
>            Priority: Major
>
> Enhance the AS2 Component to support encrypted AS2 messages per RFC4130



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to