diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f7af66b2..f00155b90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [fj-doc-val-p7m] P7MPemValidator and P7MRawValidator +### Changed + +- [fj-doc-val-p7m] Optimized P7MValidator (for previous behaviour use P7MLegacyValidator) + ## [8.7.6] - 2024-09-02 ### Fixed diff --git a/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MLegacyValidator.java b/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MLegacyValidator.java new file mode 100644 index 000000000..930be2b30 --- /dev/null +++ b/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MLegacyValidator.java @@ -0,0 +1,25 @@ +package org.fugerit.java.doc.val.p7m; + +import lombok.extern.slf4j.Slf4j; +import org.fugerit.java.doc.val.core.DocTypeValidationResult; +import org.fugerit.java.doc.val.core.basic.AbstractDocTypeValidator; + +import java.io.InputStream; + +@Slf4j +public class P7MLegacyValidator extends AbstractDocTypeValidator { + + public static final String EXTENSION = "P7M"; + + public static final String MIME_TYPE = "application/pkcs7-mime"; + + public P7MLegacyValidator() { + super( MIME_TYPE, EXTENSION ); + } + + @Override + public DocTypeValidationResult validate(InputStream is) { + return this.validationHelper(() -> P7MUtils.extractContent( is ) ); + } + +} diff --git a/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MValidator.java b/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MValidator.java index e9cf20136..048f01c1e 100644 --- a/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MValidator.java +++ b/fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MValidator.java @@ -1,9 +1,11 @@ package org.fugerit.java.doc.val.p7m; import lombok.extern.slf4j.Slf4j; +import org.fugerit.java.core.io.StreamIO; import org.fugerit.java.doc.val.core.DocTypeValidationResult; import org.fugerit.java.doc.val.core.DocTypeValidator; import org.fugerit.java.doc.val.core.basic.AbstractDocTypeValidator; +import org.fugerit.java.doc.val.core.io.NopOutputStream; import java.io.InputStream; @@ -20,9 +22,21 @@ public P7MValidator() { super( MIME_TYPE, EXTENSION ); } + private static final String BEGIN_PKCS7 = "-----BEGIN PKCS7-----"; + @Override public DocTypeValidationResult validate(InputStream is) { - return this.validationHelper(() -> P7MUtils.extractContent( is ) ); + return this.validationHelper(() -> { + byte[] data = StreamIO.readBytes( is ); + try (NopOutputStream out = new NopOutputStream()) { + String start = new String( data, 0, BEGIN_PKCS7.length() ); + if ( BEGIN_PKCS7.equals( start ) ) { + P7MUtils.extractContentPEMParser( data, out ); + } else { + P7MUtils.extractContentCMSSignedData( data, out ); + } + } + } ); } } diff --git a/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestP7MValidator.java b/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestP7MValidator.java index 75f831210..7eced1252 100644 --- a/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestP7MValidator.java +++ b/fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestP7MValidator.java @@ -1,6 +1,7 @@ package test.org.fugerit.java.doc.val.p7m; import org.fugerit.java.doc.val.core.DocValidatorFacade; +import org.fugerit.java.doc.val.p7m.P7MLegacyValidator; import org.fugerit.java.doc.val.p7m.P7MPemValidator; import org.fugerit.java.doc.val.p7m.P7MRawValidator; import org.fugerit.java.doc.val.p7m.P7MValidator; @@ -9,38 +10,44 @@ public class TestP7MValidator extends TestDocValidatorFacade { - private static final DocValidatorFacade FACADE = DocValidatorFacade.newFacadeStrict( - P7MValidator.DEFAULT - ); + private static final DocValidatorFacade FACADE = DocValidatorFacade.newFacadeStrict( P7MValidator.DEFAULT ); + + private static final DocValidatorFacade FACADE_LEGACY = DocValidatorFacade.newFacadeStrict( new P7MLegacyValidator() ); @Test public void testP7MAsP7M() { - boolean ok = this.worker(FACADE, "pdf_as_pdf.p7m", true ); - Assert.assertTrue( ok ); + String path = "pdf_as_pdf.p7m"; + Assert.assertTrue( this.worker(FACADE_LEGACY, path, true ) ); + Assert.assertTrue( this.worker(FACADE, path, true ) ); + } @Test public void testPNGAsP7M() { - boolean ok = this.worker(FACADE, "png_as_p7m.p7m", false ); - Assert.assertTrue( ok ); + String path = "pdf_as_png.p7m"; + Assert.assertTrue( this.worker(FACADE_LEGACY, path, false ) ); + Assert.assertTrue( this.worker(FACADE, path, false ) ); } @Test public void testPkcs7Ok1() { - boolean ok = this.worker(FACADE, "pkcs7_test_ok1.p7m", true ); - Assert.assertTrue( ok ); + String path = "pkcs7_test_ok1.p7m"; + Assert.assertTrue( this.worker(FACADE_LEGACY, path, true ) ); + Assert.assertTrue( this.worker(FACADE, path, true ) ); } @Test public void testPkcs7Ok2() { - boolean ok = this.worker(FACADE, "pkcs7_test_ok2.p7m", true ); - Assert.assertTrue( ok ); + String path = "pkcs7_test_ok2.p7m"; + Assert.assertTrue( this.worker(FACADE_LEGACY, path, true ) ); + Assert.assertTrue( this.worker(FACADE, path, true ) ); } @Test public void testPkcs7Ko1() { - boolean ok = this.worker(FACADE, "pkcs7_test_ko1.p7m", false ); - Assert.assertTrue( ok ); + String path = "pkcs7_test_ko1.p7m"; + Assert.assertTrue( this.worker(FACADE_LEGACY, path, false ) ); + Assert.assertTrue( this.worker(FACADE, path, false ) ); } @Test