All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.europa.esig.dss.cades.validation.scope.CAdESSignatureScopeFinder Maven / Gradle / Ivy

Go to download

DSS CAdES contains the code for the creation and validation of CAdES signatures.

There is a newer version: 6.0.d4j.2
Show newest version
/**
 * 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.validation.scope;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.europa.esig.dss.cades.validation.CAdESSignature;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.DSSException;
import eu.europa.esig.dss.model.DigestDocument;
import eu.europa.esig.dss.spi.DSSUtils;
import eu.europa.esig.dss.utils.Utils;
import eu.europa.esig.dss.validation.ReferenceValidation;
import eu.europa.esig.dss.validation.scope.AbstractSignatureScopeFinder;
import eu.europa.esig.dss.validation.scope.DigestSignatureScope;
import eu.europa.esig.dss.validation.scope.FullSignatureScope;
import eu.europa.esig.dss.validation.scope.SignatureScope;

public class CAdESSignatureScopeFinder extends AbstractSignatureScopeFinder {

	private static final Logger LOG = LoggerFactory.getLogger(CAdESSignatureScopeFinder.class);

    @Override
    public List findSignatureScope(final CAdESSignature cadesSignature) {
        List referenceValidations = cadesSignature.getReferenceValidations();
        if (Utils.isCollectionNotEmpty(referenceValidations)) {
        	ReferenceValidation reference = referenceValidations.iterator().next(); // only one Reference is allowed in CAdES
        	if (reference.isIntact()) {
                DSSDocument originalDocument = getOriginalDocument(cadesSignature);
                return getSignatureScopeFromOriginalDocument(originalDocument);
        	} else {
                return getSignatureScopeFromReferenceValidation(reference);
        	}
        }
    	return Collections.emptyList();
    }
    
    protected List getSignatureScopeFromOriginalDocument(DSSDocument originalDocument) {
        List result = new ArrayList<>();
        if (originalDocument == null) {
        	return result;
        }
        
        String fileName = originalDocument.getName();
        if (originalDocument instanceof DigestDocument) {
        	DigestDocument digestDocument = (DigestDocument) originalDocument;
            result.add(new DigestSignatureScope(fileName != null ? fileName : "Digest document", 
            		digestDocument.getExistingDigest()));
            
        } else {
			result.add(new FullSignatureScope(fileName != null ? fileName : "Full document", 
					DSSUtils.getDigest(getDefaultDigestAlgorithm(), originalDocument)));
        }
        
        return result;
    }
    
    protected List getSignatureScopeFromReferenceValidation(ReferenceValidation reference) {
        List result = new ArrayList<>();
		result.add(new FullSignatureScope("Full document", reference.getDigest()));
		return result;
    }
    
    /**
     * Returns original document for the given CAdES signature
     * @param cadesSignature {@link CAdESSignature} to get original document for
     * @return {@link DSSDocument} original document
     */
    protected DSSDocument getOriginalDocument(final CAdESSignature cadesSignature) {
    	try {
            return cadesSignature.getOriginalDocument();
        } catch (DSSException e) {
        	LOG.warn("A CAdES signer's original document is not found [{}].", e.getMessage());
        	return null;
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy