Skip to content

Commit

Permalink
Optimized P7MValidator
Browse files Browse the repository at this point in the history
for previous behaviour use P7MLegacyValidator
  • Loading branch information
fugerit79 committed Sep 2, 2024
1 parent aa28e48 commit c458ba4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ) );
}

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

Expand All @@ -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 );
}
}
} );
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down

0 comments on commit c458ba4

Please sign in to comment.