package eu.europa.esig.dss.asic.validation;
import java.util.Date;
import org.junit.Assert;
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.ASiCWithCAdESSignatureParameters;
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.test.mock.MockTSPSource;
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.reports.Reports;
import eu.europa.esig.dss.validation.reports.wrapper.DiagnosticData;
import eu.europa.esig.dss.validation.reports.wrapper.TimestampWrapper;
public class ASiCS_CAdESLTACheckTimeStampIDTest {
@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);
ASiCWithCAdESSignatureParameters signatureParameters = new ASiCWithCAdESSignatureParameters();
signatureParameters.bLevel().setSigningDate(new Date());
signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
signatureParameters.setSignatureLevel(SignatureLevel.CAdES_BASELINE_LTA);
signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_S);
CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
ASiCWithCAdESService service = new ASiCWithCAdESService(certificateVerifier);
service.setTspSource(new MockTSPSource(certificateService.generateTspCertificate(SignatureAlgorithm.RSA_SHA1)));
ToBeSigned toBeSigned = service.getDataToSign(documentToSign, signatureParameters);
SignatureValue signatureValue = TestUtils.sign(signatureParameters.getSignatureAlgorithm(), privateKeyEntry, toBeSigned);
final DSSDocument signedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue);
SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(signedDocument);
validator.setCertificateVerifier(new CommonCertificateVerifier());
Reports report = validator.validateDocument();
// report.print();
DiagnosticData diagnostic = report.getDiagnosticData();
String signatureId = diagnostic.getFirstSignatureId();
for (TimestampWrapper wrapper : diagnostic.getTimestampList(signatureId)) {
Assert.assertEquals(signatureId, wrapper.getSignedObjects().getSignedSignature().get(0).getId());
}
}
}