package eu.europa.esig.dss.asic.signature.asics;
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.ASiCWithCAdESContainerExtractor;
import eu.europa.esig.dss.asic.ASiCWithCAdESSignatureParameters;
import eu.europa.esig.dss.asic.AbstractASiCContainerExtractor;
import eu.europa.esig.dss.asic.signature.ASiCWithCAdESService;
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 ASiCSCAdESLevelBMultiFilesParallelTest {
@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);
ASiCWithCAdESSignatureParameters signatureParameters = new ASiCWithCAdESSignatureParameters();
signatureParameters.bLevel().setSigningDate(new Date());
signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
signatureParameters.setSignatureLevel(SignatureLevel.CAdES_BASELINE_B);
signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_S);
CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
ASiCWithCAdESService service = new ASiCWithCAdESService(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.CAdES_BASELINE_B);
signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_S);
certificateVerifier = new CommonCertificateVerifier();
service = new ASiCWithCAdESService(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 ASiCWithCAdESContainerExtractor(resignedDocument);
ASiCExtractResult result = extractor.extract();
assertEquals(0, result.getUnsupportedDocuments().size());
List<DSSDocument> signatureDocuments = result.getSignatureDocuments();
assertEquals(1, signatureDocuments.size());
String signatureFilename = signatureDocuments.get(0).getName();
assertTrue(signatureFilename.startsWith("META-INF/signature"));
assertTrue(signatureFilename.endsWith(".p7s"));
List<DSSDocument> manifestDocuments = result.getManifestDocuments();
assertEquals(0, manifestDocuments.size());
List<DSSDocument> signedDocuments = result.getSignedDocuments();
assertEquals(1, signedDocuments.size());
assertEquals("package.zip", signedDocuments.get(0).getName());
DSSDocument mimeTypeDocument = result.getMimeTypeDocument();
byte[] mimeTypeContent = DSSUtils.toByteArray(mimeTypeDocument);
try {
assertEquals(MimeType.ASICS.getMimeTypeString(), new String(mimeTypeContent, "UTF-8"));
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}
}
}