text: /**
   * 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.cades.extension;
  
  import static org.junit.jupiter.api.Assertions.assertNotNull;
  import static org.junit.jupiter.api.Assertions.assertTrue;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.Arrays;
  import java.util.UUID;
  
  import eu.europa.esig.dss.cades.CAdESSignatureParameters;
  import eu.europa.esig.dss.cades.signature.CAdESService;
  import eu.europa.esig.dss.cades.signature.CAdESTimestampParameters;
  import eu.europa.esig.dss.diagnostic.DiagnosticData;
  import eu.europa.esig.dss.diagnostic.SignatureWrapper;
  import eu.europa.esig.dss.enumerations.SignaturePackaging;
  import eu.europa.esig.dss.model.DSSDocument;
  import eu.europa.esig.dss.model.DSSException;
  import eu.europa.esig.dss.model.FileDocument;
  import eu.europa.esig.dss.model.SignatureValue;
  import eu.europa.esig.dss.model.ToBeSigned;
  import eu.europa.esig.dss.signature.DocumentSignatureService;
  import eu.europa.esig.dss.spi.x509.tsp.TSPSource;
  import eu.europa.esig.dss.test.extension.AbstractTestExtension;
  import eu.europa.esig.dss.validation.reports.Reports;
  import eu.europa.esig.validationreport.jaxb.SignatureIdentifierType;
  import eu.europa.esig.validationreport.jaxb.SignatureValidationReportType;
  import eu.europa.esig.validationreport.jaxb.ValidationReportType;
  
  public abstract class AbstractCAdESTestExtension extends AbstractTestExtension<CAdESSignatureParameters, CAdESTimestampParameters> {
  
  	@Override
  	protected TSPSource getUsedTSPSourceAtSignatureTime() {
  		return getGoodTsa();
  	}
  
  	@Override
  	protected TSPSource getUsedTSPSourceAtExtensionTime() {
  		return getAlternateGoodTsa();
  	}
  
  	@Override
  	protected DSSDocument getOriginalDocument() {
  		File originalDoc = new File("target/original-" + UUID.randomUUID().toString() + ".bin");
  		try (FileOutputStream fos = new FileOutputStream(originalDoc)) {
  			fos.write("Hello world!".getBytes());
  		} catch (IOException e) {
  			throw new DSSException("Unable to create the original document", e);
  		}
  		return new FileDocument(originalDoc);
  	}
  	
  	@Override
  	protected void checkSignatureIdentifier(DiagnosticData diagnosticData) {
  		for (SignatureWrapper signatureWrapper : diagnosticData.getSignatures()) {
  			assertNotNull(signatureWrapper.getSignatureValue());
  		}
  	}
  	
  	@Override
  	protected void checkReportsSignatureIdentifier(Reports reports) {
  		DiagnosticData diagnosticData = reports.getDiagnosticData();
  		ValidationReportType etsiValidationReport = reports.getEtsiValidationReportJaxb();
  		for (SignatureValidationReportType signatureValidationReport : etsiValidationReport.getSignatureValidationReport()) {
  			SignatureWrapper signature = diagnosticData.getSignatureById(signatureValidationReport.getSignatureIdentifier().getId());
  			
  			SignatureIdentifierType signatureIdentifier = signatureValidationReport.getSignatureIdentifier();
  			assertNotNull(signatureIdentifier);
  			
  			assertNotNull(signatureIdentifier.getSignatureValue());
  			assertTrue(Arrays.equals(signature.getSignatureValue(), signatureIdentifier.getSignatureValue().getValue()));
  		}
  	}
  
  	@Override
  	protected DSSDocument getSignedDocument(DSSDocument doc) {
  		// Sign
  		CAdESSignatureParameters signatureParameters = new CAdESSignatureParameters();
  		signatureParameters.setSigningCertificate(getSigningCert());
  		signatureParameters.setCertificateChain(getCertificateChain());
  		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
  		signatureParameters.setSignatureLevel(getOriginalSignatureLevel());
  
  		CAdESService service = new CAdESService(getCompleteCertificateVerifier());
  		service.setTspSource(getUsedTSPSourceAtSignatureTime());
  
  		ToBeSigned dataToSign = service.getDataToSign(doc, signatureParameters);
  		SignatureValue signatureValue = getToken().sign(dataToSign, signatureParameters.getDigestAlgorithm(), getPrivateKeyEntry());
  		return service.signDocument(doc, signatureParameters, signatureValue);
  
  	}
  
  	@Override
  	protected DocumentSignatureService<CAdESSignatureParameters, CAdESTimestampParameters> getSignatureServiceToExtend() {
  		CAdESService service = new CAdESService(getCompleteCertificateVerifier());
  		service.setTspSource(getUsedTSPSourceAtExtensionTime());
  		return service;
  	}
  
  	@Override
  	protected CAdESSignatureParameters getExtensionParameters() {
  		CAdESSignatureParameters extensionParameters = new CAdESSignatureParameters();
  		extensionParameters.setSignatureLevel(getFinalSignatureLevel());
  		return extensionParameters;
  	}
  
  	@Override
  	protected String getSigningAlias() {
  		return GOOD_USER;
  	}
  
  }
  