eu.europa.esig.dss.asic.cades.validation.scope.ASiCWithCAdESTimestampScopeFinder Maven / Gradle / Ivy
/**
* 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.asic.cades.validation.scope;
import eu.europa.esig.dss.asic.common.ASiCUtils;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.spi.DSSUtils;
import eu.europa.esig.dss.utils.Utils;
import eu.europa.esig.dss.validation.ManifestEntry;
import eu.europa.esig.dss.validation.ManifestFile;
import eu.europa.esig.dss.validation.scope.ContainerContentSignatureScope;
import eu.europa.esig.dss.validation.scope.ContainerSignatureScope;
import eu.europa.esig.dss.validation.scope.DetachedTimestampScopeFinder;
import eu.europa.esig.dss.validation.scope.ManifestSignatureScope;
import eu.europa.esig.dss.validation.scope.SignatureScope;
import eu.europa.esig.dss.validation.timestamp.TimestampToken;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* This class is used to find a timestamp source for a detached timestamp within an ASiC with CAdES container
*
*/
public class ASiCWithCAdESTimestampScopeFinder extends DetachedTimestampScopeFinder {
/** Represents a list of documents encapsulated within an ASiC container */
private List containerDocuments;
/** Represents a list of documents encapsulated within a package.zip archive, when applicable (ASiC-S) */
private List archiveDocuments;
/**
* Default constructor instantiating object with empty lists of documents
*/
public ASiCWithCAdESTimestampScopeFinder() {
// empty
}
/**
* Sets a list of container original documents
*
* @param containerDocuments a list of {@link DSSDocument}s
*/
public void setContainerDocuments(List containerDocuments) {
this.containerDocuments = containerDocuments;
}
/**
* Sets a list of package.zip archive documents
*
* @param archiveDocuments a list of {@link DSSDocument}s
*/
public void setArchiveDocuments(List archiveDocuments) {
this.archiveDocuments = archiveDocuments;
}
@Override
public List findTimestampScope(TimestampToken timestampToken) {
if (timestampToken.isMessageImprintDataIntact()) {
if (timestampToken.getManifestFile() != null) {
return getTimestampSignatureScopeForManifest(timestampToken.getManifestFile());
} else {
return getTimestampSignatureScopeForDocument(timestampedData);
}
}
return Collections.emptyList();
}
/**
* Extracts timestamped signature scopes from a {@code ManifestFile}
*
* @param manifestFile {@link ManifestFile} to extract entries from
* @return a list of timestamped {@link SignatureScope}s
*/
private List getTimestampSignatureScopeForManifest(ManifestFile manifestFile) {
List result = new ArrayList<>();
result.add(new ManifestSignatureScope(manifestFile.getFilename(), getDigest(manifestFile.getDocument())));
if (Utils.isCollectionNotEmpty(containerDocuments)) {
List rootLevelDocuments = ASiCUtils.getRootLevelDocuments(containerDocuments);
for (ManifestEntry manifestEntry : manifestFile.getEntries()) {
result.addAll(getTimestampSignatureScopeForManifestEntry(manifestEntry, rootLevelDocuments));
}
}
return result;
}
private List getTimestampSignatureScopeForManifestEntry(ManifestEntry manifestEntry,
List rootLevelDocuments) {
if (manifestEntry.isIntact()) {
for (DSSDocument document : containerDocuments) {
if (Utils.areStringsEqual(manifestEntry.getFileName(), document.getName())) {
if (Utils.collectionSize(rootLevelDocuments) == 1 && isASiCSContainer(document)) {
return getTimestampSignatureScopeForZipPackage(document);
} else {
return super.getTimestampSignatureScopeForDocument(document);
}
}
}
}
return Collections.emptyList();
}
@Override
protected List getTimestampSignatureScopeForDocument(DSSDocument document) {
if (isASiCSContainer(document)) {
return getTimestampSignatureScopeForZipPackage(document);
} else {
return super.getTimestampSignatureScopeForDocument(document);
}
}
private List getTimestampSignatureScopeForZipPackage(DSSDocument document) {
List result = new ArrayList<>();
result.add(new ContainerSignatureScope(document.getName(), getDigest(document)));
if (Utils.isCollectionNotEmpty(archiveDocuments)) {
for (DSSDocument archivedDocument : archiveDocuments) {
result.add(new ContainerContentSignatureScope(DSSUtils.decodeURI(archivedDocument.getName()),
getDigest(archivedDocument)));
}
}
return result;
}
/**
* This method verifies whether the given document is an ASiC-S ZIP container
*
* @param document {@link DSSDocument} to check
* @return TRUE of the document is an ASiC-S data container, FALSE otherwise
*/
private boolean isASiCSContainer(DSSDocument document) {
return document.getName() != null && !document.getName().contains("/") && ASiCUtils.isZip(document);
}
}