From 1e26f21a6d6371e7ae7d0a8b06cc4e302f17e603 Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Wed, 22 Nov 2023 11:52:36 +0100 Subject: [PATCH] Extract content in p7m --- CHANGELOG.md | 1 + .../fugerit/java/doc/val/p7m/P7MUtils.java | 28 +++++++++++ .../doc/val/p7m/TestExtractContentP7M.java | 50 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MUtils.java create mode 100644 fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestExtractContentP7M.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b81eb313..28a1fe2c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [val-p7m] utility to extract p7m content - [val-core] XmlValidator for simple xml doc type validation - [val] P7MValidator in full validator facade - [playground-quarkus] P7MValidator in validator feature diff --git a/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MUtils.java b/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MUtils.java new file mode 100644 index 000000000..7c679bf2e --- /dev/null +++ b/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MUtils.java @@ -0,0 +1,28 @@ +package org.fugerit.java.doc.val.p7m; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.bouncycastle.cms.CMSException; +import org.bouncycastle.cms.CMSProcessable; +import org.bouncycastle.cms.CMSSignedData; +import org.fugerit.java.core.io.StreamIO; + +public class P7MUtils { + + private P7MUtils() {} + + public static void extractContent( InputStream p7mContent, OutputStream contentStream ) throws CMSException, IOException { + CMSSignedData csd = new CMSSignedData( p7mContent ); + CMSProcessable cmsContent = csd.getSignedContent(); + if ( cmsContent != null ) { + byte[] content = (byte[])cmsContent.getContent(); + try ( ByteArrayInputStream is = new ByteArrayInputStream( content ) ) { + StreamIO.pipeStream(is, contentStream, StreamIO.MODE_CLOSE_BOTH); + } + } + } + +} diff --git a/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestExtractContentP7M.java b/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestExtractContentP7M.java new file mode 100644 index 000000000..7980a8e2c --- /dev/null +++ b/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestExtractContentP7M.java @@ -0,0 +1,50 @@ +package test.org.fugerit.java.doc.val.p7m; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; + +import org.bouncycastle.cms.CMSException; +import org.fugerit.java.core.function.SafeFunction; +import org.fugerit.java.core.io.FileIO; +import org.fugerit.java.doc.val.p7m.P7MUtils; +import org.junit.Assert; +import org.junit.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestExtractContentP7M { + + @Test + public void testP7MKo() { + Assert.assertThrows( CMSException.class , () -> { + String path = "src/test/resources/sample/png_as_p7m.p7m"; + File testP7M = new File( path ); + log.info( "test extract ko : {}", testP7M.getCanonicalPath() ); + try ( FileInputStream is = new FileInputStream( testP7M ); + ByteArrayOutputStream os = new ByteArrayOutputStream() ) { + P7MUtils.extractContent(is, os); + } + }) ; + } + + @Test + public void testP7MOk() { + SafeFunction.apply( () -> { + String path = "src/test/resources/sample/pdf_as_pdf.p7m"; + File testP7M = new File( path ); + log.info( "test extract : {}", testP7M.getCanonicalPath() ); + File outputBase = new File( "target" ); + File outputContent = new File( outputBase, "content.pdf" ); + outputContent.delete(); + try ( FileInputStream is = new FileInputStream( testP7M ); + ByteArrayOutputStream os = new ByteArrayOutputStream() ) { + P7MUtils.extractContent(is, os); + FileIO.writeBytes( os.toByteArray() , outputContent ); + } + Assert.assertTrue( outputContent.exists() ); + } ); + } + +}