/**
* 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.xades.signature;
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.SignaturePackaging;
import eu.europa.esig.dss.SignatureValue;
import eu.europa.esig.dss.ToBeSigned;
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.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.xades.XAdESSignatureParameters;
@RunWith(Parameterized.class)
public class XAdESDoubleSignatureTest {
private static SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RSA_SHA256;
private static DSSDocument toBeSigned;
private static MockPrivateKeyEntry privateKeyEntry;
// Run 10 times this test
@Parameters
public static List<Object[]> data() {
return Arrays.asList(new Object[10][0]);
}
public XAdESDoubleSignatureTest() {
}
@BeforeClass
public static void setUp() throws Exception {
toBeSigned = new FileDocument(new File("src/test/resources/sample.xml"));
CertificateService certificateService = new CertificateService();
privateKeyEntry = certificateService.generateCertificateChain(signatureAlgorithm);
}
@Test
public void testDoubleSignature() {
CommonCertificateVerifier verifier = new CommonCertificateVerifier();
XAdESService service = new XAdESService(verifier);
XAdESSignatureParameters params = new XAdESSignatureParameters();
params.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B);
params.setSignaturePackaging(SignaturePackaging.ENVELOPED);
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 XAdESSignatureParameters();
params.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B);
params.setSignaturePackaging(SignaturePackaging.ENVELOPED);
params.setSigningCertificate(privateKeyEntry.getCertificate());
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();
DiagnosticData diagnosticData = reports.getDiagnosticData();
List<String> signatureIdList = diagnosticData.getSignatureIdList();
assertEquals(2, signatureIdList.size());
for (String signatureId : signatureIdList) {
assertTrue(diagnosticData.isBLevelTechnicallyValid(signatureId));
}
}
}