/** * DSS - Digital Signature Services * Copyright (C) 2015 European Commission, provided under the CEF programme * * This file is part of the "DSS - Digital Signature Services" project. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ package eu.europa.esig.dss.pades; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.File; import java.util.Arrays; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import eu.europa.esig.dss.DSSDocument; import eu.europa.esig.dss.FileDocument; 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.pades.signature.PAdESService; 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.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; @RunWith(Parameterized.class) public class PAdESDoubleSignatureTest { private static SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RSA_SHA256; private static DSSDocument toBeSigned; private static MockPrivateKeyEntry privateKeyEntry; @Parameters public static List<Object[]> data() { return Arrays.asList(new Object[10][0]); } public PAdESDoubleSignatureTest() { } @BeforeClass public static void setUp() throws Exception { toBeSigned = new FileDocument(new File("src/test/resources/sample.pdf")); CertificateService certificateService = new CertificateService(); privateKeyEntry = certificateService.generateCertificateChain(signatureAlgorithm); } @Test public void testDoubleSignature() throws Exception { CommonCertificateVerifier verifier = new CommonCertificateVerifier(); PAdESService service = new PAdESService(verifier); CertificateService certificateService = new CertificateService(); service.setTspSource(new MockTSPSource(certificateService.generateTspCertificate(SignatureAlgorithm.RSA_SHA1))); PAdESSignatureParameters params = new PAdESSignatureParameters(); params.setSignatureLevel(SignatureLevel.PAdES_BASELINE_LTA); params.setSigningCertificate(privateKeyEntry.getCertificate()); ToBeSigned dataToSign = service.getDataToSign(toBeSigned, params); SignatureValue signatureValue = TestUtils.sign(signatureAlgorithm, privateKeyEntry, dataToSign); DSSDocument signedDocument = service.signDocument(toBeSigned, params, signatureValue); params = new PAdESSignatureParameters(); params.setSignatureLevel(SignatureLevel.PAdES_BASELINE_LTA); params.setSigningCertificate(privateKeyEntry.getCertificate()); service.setTspSource(new MockTSPSource(certificateService.generateTspCertificate(SignatureAlgorithm.RSA_SHA1))); dataToSign = service.getDataToSign(signedDocument, params); signatureValue = TestUtils.sign(signatureAlgorithm, privateKeyEntry, dataToSign); DSSDocument doubleSignedDocument = service.signDocument(signedDocument, params, signatureValue); SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(doubleSignedDocument); validator.setCertificateVerifier(new CommonCertificateVerifier()); Reports reports = validator.validateDocument(); // reports.print(); DiagnosticData diagnosticData = reports.getDiagnosticData(); // Bug with 2 signatures which have the same ID List<String> signatureIdList = diagnosticData.getSignatureIdList(); assertEquals(2, signatureIdList.size()); for (String signatureId : signatureIdList) { assertTrue(diagnosticData.isBLevelTechnicallyValid(signatureId)); } } }