eu.europa.esig.dss.pdf.pdfbox.PdfBoxDocTimestampInfo.java Source code

Java tutorial

Introduction

Here is the source code for eu.europa.esig.dss.pdf.pdfbox.PdfBoxDocTimestampInfo.java

Source

/**
 * 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.pdf.pdfbox;

import java.security.cert.X509Certificate;

import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.tsp.TimeStampToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.europa.esig.dss.DSSException;
import eu.europa.esig.dss.pdf.PdfDocTimestampInfo;
import eu.europa.esig.dss.pdf.PdfDssDict;
import eu.europa.esig.dss.validation.SignatureCryptographicVerification;
import eu.europa.esig.dss.validation.TimestampToken;
import eu.europa.esig.dss.x509.CertificatePool;
import eu.europa.esig.dss.x509.CertificateToken;
import eu.europa.esig.dss.x509.TimestampType;

/**
 * Signature timestamp representation
 * This class is only used in case of Document Timestamp (not signature-timestamp from CAdES/CMS)
 */
class PdfBoxDocTimestampInfo extends PdfBoxCMSInfo implements PdfDocTimestampInfo {

    private static final Logger logger = LoggerFactory.getLogger(PdfBoxDocTimestampInfo.class);

    private final TimestampToken timestampToken;

    /**
     * @param validationCertPool
     * @param dssDictionary
     *            the DSS dictionary
     * @param cms
     *            the CMS (CAdES) bytes
     * @param isArchiveTimestamp
     * @param inputStream
     *            the stream of the whole signed document
     * @throws DSSException
     */
    PdfBoxDocTimestampInfo(CertificatePool validationCertPool, PDSignature signature, PdfDssDict dssDictionary,
            byte[] cms, byte[] signedContent, boolean isArchiveTimestamp) throws DSSException {
        super(signature, dssDictionary, cms, signedContent);
        try {
            TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(cms));
            TimestampType timestampType = TimestampType.SIGNATURE_TIMESTAMP;
            if (isArchiveTimestamp) {
                timestampType = TimestampType.ARCHIVE_TIMESTAMP;
            }
            timestampToken = new TimestampToken(timeStampToken, timestampType, validationCertPool);
            logger.debug("Created PdfBoxDocTimestampInfo {} : {}", timestampType, uniqueId());
        } catch (Exception e) {
            throw new DSSException(e);
        }
    }

    @Override
    public SignatureCryptographicVerification checkIntegrityOnce() {

        final SignatureCryptographicVerification signatureCryptographicVerification = new SignatureCryptographicVerification();
        signatureCryptographicVerification.setReferenceDataFound(false);
        signatureCryptographicVerification.setReferenceDataIntact(false);
        signatureCryptographicVerification.setSignatureIntact(false);
        if (getSignedDocumentBytes() != null) {
            signatureCryptographicVerification.setReferenceDataFound(true);
        }
        signatureCryptographicVerification
                .setReferenceDataIntact(timestampToken.matchData(getSignedDocumentBytes()));
        signatureCryptographicVerification.setSignatureIntact(timestampToken.isSignatureValid());
        return signatureCryptographicVerification;
    }

    @Override
    public X509Certificate getSigningCertificate() {
        final CertificateToken signingCertificate = timestampToken.getIssuerToken();
        return signingCertificate == null ? null : signingCertificate.getCertificate();
    }

    @Override
    public boolean isTimestamp() {
        return true;
    }

    @Override
    public TimestampToken getTimestampToken() {
        return timestampToken;
    }
}