package eu.europa.esig.dss.asic.signature.asice; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.junit.Test; import eu.europa.esig.dss.ASiCContainerType; import eu.europa.esig.dss.DSSDocument; import eu.europa.esig.dss.DSSUtils; import eu.europa.esig.dss.InMemoryDocument; import eu.europa.esig.dss.MimeType; import eu.europa.esig.dss.SignatureAlgorithm; import eu.europa.esig.dss.SignatureLevel; import eu.europa.esig.dss.SignatureValue; import eu.europa.esig.dss.ToBeSigned; import eu.europa.esig.dss.asic.ASiCExtractResult; import eu.europa.esig.dss.asic.ASiCWithXAdESContainerExtractor; import eu.europa.esig.dss.asic.ASiCWithXAdESSignatureParameters; import eu.europa.esig.dss.asic.AbstractASiCContainerExtractor; import eu.europa.esig.dss.asic.signature.ASiCWithXAdESService; import eu.europa.esig.dss.test.TestUtils; import eu.europa.esig.dss.test.gen.CertificateService; import eu.europa.esig.dss.test.mock.MockPrivateKeyEntry; import eu.europa.esig.dss.validation.CertificateVerifier; import eu.europa.esig.dss.validation.CommonCertificateVerifier; import eu.europa.esig.dss.validation.SignedDocumentValidator; import eu.europa.esig.dss.validation.policy.rules.Indication; import eu.europa.esig.dss.validation.reports.Reports; import eu.europa.esig.dss.validation.reports.wrapper.DiagnosticData; public class ASiCEXAdESLevelBMultiFilesParallelTest { @Test public void test() throws Exception { List<DSSDocument> documentToSigns = new ArrayList<DSSDocument>(); documentToSigns.add(new InMemoryDocument("Hello World !".getBytes(), "test.text", MimeType.TEXT)); documentToSigns.add(new InMemoryDocument("Bye World !".getBytes(), "test2.text", MimeType.TEXT)); CertificateService certificateService = new CertificateService(); MockPrivateKeyEntry privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256); ASiCWithXAdESSignatureParameters signatureParameters = new ASiCWithXAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate()); signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_E); CertificateVerifier certificateVerifier = new CommonCertificateVerifier(); ASiCWithXAdESService service = new ASiCWithXAdESService(certificateVerifier); ToBeSigned dataToSign = service.getDataToSign(documentToSigns, signatureParameters); SignatureValue signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign); DSSDocument signedDocument = service.signDocument(documentToSigns, signatureParameters, signatureValue); privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate()); signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_E); certificateVerifier = new CommonCertificateVerifier(); service = new ASiCWithXAdESService(certificateVerifier); dataToSign = service.getDataToSign(signedDocument, signatureParameters); signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign); DSSDocument resignedDocument = service.signDocument(signedDocument, signatureParameters, signatureValue); SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(resignedDocument); validator.setCertificateVerifier(new CommonCertificateVerifier()); Reports reports = validator.validateDocument(); DiagnosticData diagnosticData = reports.getDiagnosticData(); List<String> signatureIdList = diagnosticData.getSignatureIdList(); assertEquals(2, signatureIdList.size()); for (String sigId : signatureIdList) { assertTrue(diagnosticData.isBLevelTechnicallyValid(sigId)); assertNotEquals(Indication.FAILED, reports.getSimpleReport().getIndication(sigId)); } AbstractASiCContainerExtractor extractor = new ASiCWithXAdESContainerExtractor(resignedDocument); ASiCExtractResult result = extractor.extract(); assertEquals(0, result.getUnsupportedDocuments().size()); List<DSSDocument> signatureDocuments = result.getSignatureDocuments(); assertEquals(2, signatureDocuments.size()); String signatureFilename = signatureDocuments.get(0).getName(); assertTrue(signatureFilename.startsWith("META-INF/signature")); assertTrue(signatureFilename.endsWith(".xml")); List<DSSDocument> manifestDocuments = result.getManifestDocuments(); assertEquals(1, manifestDocuments.size()); assertEquals("META-INF/manifest.xml", manifestDocuments.get(0).getName()); List<DSSDocument> signedDocuments = result.getSignedDocuments(); assertEquals(2, signedDocuments.size()); DSSDocument mimeTypeDocument = result.getMimeTypeDocument(); byte[] mimeTypeContent = DSSUtils.toByteArray(mimeTypeDocument); try { assertEquals(MimeType.ASICE.getMimeTypeString(), new String(mimeTypeContent, "UTF-8")); } catch (UnsupportedEncodingException e) { fail(e.getMessage()); } } }