Skip to content

Commit

Permalink
Extract content in p7m
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Nov 22, 2023
1 parent 6d072ec commit 1e26f21
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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() );
} );
}

}

0 comments on commit 1e26f21

Please sign in to comment.