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 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.InMemoryDocument;
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.ASiCWithXAdESSignatureParameters;
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;
/**
* @author axel.abinet
*
*/
public class ASiCEXAdESLevelBSignFourTimeTest {
@Test
public void test() throws Exception {
DSSDocument documentToSign = new InMemoryDocument("Hello World !".getBytes(), "test.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(documentToSign, signatureParameters);
SignatureValue signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
DSSDocument signedDocument = service.signDocument(documentToSign, 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);
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(resignedDocument, signatureParameters);
signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
resignedDocument = service.signDocument(resignedDocument, 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(resignedDocument, signatureParameters);
signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
resignedDocument = service.signDocument(resignedDocument, 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(4, signatureIdList.size());
for (String sigId : signatureIdList) {
assertTrue(diagnosticData.isBLevelTechnicallyValid(sigId));
assertNotEquals(Indication.FAILED, reports.getSimpleReport().getIndication(sigId));
}
}
}