Example usage for org.apache.pdfbox.pdmodel.interactive.digitalsignature PDSignature getSignDate

List of usage examples for org.apache.pdfbox.pdmodel.interactive.digitalsignature PDSignature getSignDate

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.digitalsignature PDSignature getSignDate.

Prototype

public Calendar getSignDate() 

Source Link

Document

Returns the sign date.

Usage

From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxCMSInfo.java

License:Open Source License

/**
 * @param validationCertPool/* w ww  . j  a va  2  s.  co  m*/
 * @param outerCatalog       the PDF Dict of the outer document, if the PDFDocument in a enclosed revision. Can be null.
 * @param document           the signed PDFDocument
 * @param cms                the CMS bytes (CAdES signature)
 * @param inputStream        the stream of the whole signed document
 * @throws IOException
 */
PdfBoxCMSInfo(CertificatePool validationCertPool, PdfDict outerCatalog, PDDocument document,
        PDSignature signature, byte[] cms, InputStream inputStream) throws DSSException, IOException {
    this.validationCertPool = validationCertPool;
    this.outerCatalog = PdfDssDict.build(outerCatalog);
    this.cms = cms;
    this.location = signature.getLocation();
    this.signingDate = signature.getSignDate() != null ? signature.getSignDate().getTime() : null;
    this.signatureByteRange = signature.getByteRange();
    final COSDictionary cosDictionary = document.getDocumentCatalog().getCOSDictionary();
    final PdfBoxDict documentDict = new PdfBoxDict(cosDictionary, document);
    documentDictionary = PdfDssDict.build(documentDict);
    try {
        if (cms == null) {
            // due to not very good revision extracting
            throw new DSSPadesNoSignatureFound();
        }
        signedBytes = signature.getSignedContent(inputStream);
    } catch (IOException e) {
        throw new DSSException(e);
    }
}

From source file:eu.europa.esig.dss.pades.signature.PAdESLevelBWithContentTimestampTest.java

License:Open Source License

@Override
protected void onDocumentSigned(byte[] byteArray) {
    super.onDocumentSigned(byteArray);

    try (PDDocument doc = PDDocument.load(byteArray)) {
        List<PDSignature> signatureDictionaries = doc.getSignatureDictionaries();
        assertEquals(1, signatureDictionaries.size());
        PDSignature pdSignature = signatureDictionaries.get(0);
        assertNotNull(pdSignature.getName());
        assertNotNull(pdSignature.getReason());
        assertNotNull(pdSignature.getLocation());
        assertNotNull(pdSignature.getContactInfo());
        assertNotNull(pdSignature.getSignDate()); // M
        assertEquals("Adobe.PPKLite", pdSignature.getFilter());
        assertEquals("ETSI.CAdES.detached", pdSignature.getSubFilter());
    } catch (IOException e) {
        throw new DSSException(e);
    }//from  w ww .java 2 s .  com
}

From source file:eu.europa.esig.dss.pdf.pdfbox.PdfBoxCMSInfo.java

License:Open Source License

/**
 *
 * @param signature The signature object
 * @param dssDictionary the DSS dictionary
 * @param cms the signature binary/*w w w .j a  v a  2s  .c o m*/
 * @param signedContent the signed content
 */
PdfBoxCMSInfo(PDSignature signature, PdfDssDict dssDictionary, byte[] cms, byte[] signedContent) {
    this.cms = cms;
    this.location = signature.getLocation();
    this.reason = signature.getReason();
    this.contactInfo = signature.getContactInfo();
    this.subFilter = signature.getSubFilter();
    this.signingDate = signature.getSignDate() != null ? signature.getSignDate().getTime() : null;
    this.signatureByteRange = signature.getByteRange();
    this.dssDictionary = dssDictionary;
    this.signedBytes = signedContent;
}

From source file:mj.ocraptor.extraction.tika.parser.pdf.PDF2XHTML.java

License:Apache License

private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField,
        XHTMLContentHandler handler) throws SAXException {

    PDSignature sig = sigField.getSignature();
    if (sig == null) {
        return;// ww w  .j  a  v a  2  s . co  m
    }
    Map<String, String> vals = new TreeMap<String, String>();
    vals.put("name", sig.getName());
    vals.put("contactInfo", sig.getContactInfo());
    vals.put("location", sig.getLocation());
    vals.put("reason", sig.getReason());

    Calendar cal = sig.getSignDate();
    if (cal != null) {
        dateFormat.setTimeZone(cal.getTimeZone());
        vals.put("date", dateFormat.format(cal.getTime()));
    }
    // see if there is any data
    int nonNull = 0;
    for (String val : vals.keySet()) {
        if (val != null && !val.equals("")) {
            nonNull++;
        }
    }
    // if there is, process it
    if (nonNull > 0) {
        handler.startElement("li", parentAttributes);

        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "type", "type", "CDATA", "signaturedata");

        handler.startElement("ol", attrs);
        for (Map.Entry<String, String> e : vals.entrySet()) {
            if (e.getValue() == null || e.getValue().equals("")) {
                continue;
            }
            attrs = new AttributesImpl();
            attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey());
            handler.startElement("li", attrs);
            handler.characters(e.getValue());
            handler.endElement("li");
        }
        handler.endElement("ol");
        handler.endElement("li");
    }
}

From source file:org.apache.tika.parser.pdf.AbstractPDF2XHTML.java

License:Apache License

private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField) throws SAXException {

    PDSignature sig = sigField.getSignature();
    if (sig == null) {
        return;/*from www .  ja va 2  s . c  o m*/
    }
    Map<String, String> vals = new TreeMap<>();
    vals.put("name", sig.getName());
    vals.put("contactInfo", sig.getContactInfo());
    vals.put("location", sig.getLocation());
    vals.put("reason", sig.getReason());

    Calendar cal = sig.getSignDate();
    if (cal != null) {
        dateFormat.setTimeZone(cal.getTimeZone());
        vals.put("date", dateFormat.format(cal.getTime()));
    }
    //see if there is any data
    int nonNull = 0;
    for (String val : vals.keySet()) {
        if (val != null && !val.equals("")) {
            nonNull++;
        }
    }
    //if there is, process it
    if (nonNull > 0) {
        xhtml.startElement("li", parentAttributes);

        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "type", "type", "CDATA", "signaturedata");

        xhtml.startElement("ol", attrs);
        for (Map.Entry<String, String> e : vals.entrySet()) {
            if (e.getValue() == null || e.getValue().equals("")) {
                continue;
            }
            attrs = new AttributesImpl();
            attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey());
            xhtml.startElement("li", attrs);
            xhtml.characters(e.getValue());
            xhtml.endElement("li");
        }
        xhtml.endElement("ol");
        xhtml.endElement("li");
    }
}

From source file:org.apache.tika.parser.pdf.EnhancedPDF2XHTML.java

License:Apache License

private void handleSignature(AttributesImpl parentAttributes, PDSignatureField sigField,
        XHTMLContentHandler handler) throws SAXException {

    PDSignature sig = sigField.getSignature();
    if (sig == null) {
        return;/*from   w ww . j  a  v  a2 s. co m*/
    }
    Map<String, String> vals = new TreeMap<String, String>();
    vals.put("name", sig.getName());
    vals.put("contactInfo", sig.getContactInfo());
    vals.put("location", sig.getLocation());
    vals.put("reason", sig.getReason());

    Calendar cal = sig.getSignDate();
    if (cal != null) {
        dateFormat.setTimeZone(cal.getTimeZone());
        vals.put("date", dateFormat.format(cal.getTime()));
    }
    //see if there is any data
    int nonNull = 0;
    for (String val : vals.keySet()) {
        if (val != null && !val.equals("")) {
            nonNull++;
        }
    }
    //if there is, process it
    if (nonNull > 0) {
        handler.startElement("li", parentAttributes);

        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "type", "type", "CDATA", "signaturedata");

        handler.startElement("ol", attrs);
        for (Map.Entry<String, String> e : vals.entrySet()) {
            if (e.getValue() == null || e.getValue().equals("")) {
                continue;
            }
            attrs = new AttributesImpl();
            attrs.addAttribute("", "signdata", "signdata", "CDATA", e.getKey());
            handler.startElement("li", attrs);
            handler.characters(e.getValue());
            handler.endElement("li");
        }
        handler.endElement("ol");
        handler.endElement("li");
    }
}

From source file:pdfbox.SignatureVerifier.java

License:Apache License

public Map<String, SignatureResult> extractSignatures(File infile) throws IOException, CertificateException,
        NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
    Map<String, SignatureResult> result = new HashMap<>();

    try (PDDocument document = PDDocument.load(infile)) {
        for (PDSignature sig : document.getSignatureDictionaries()) {
            COSDictionary sigDict = sig.getCOSObject();
            COSString contents = (COSString) sigDict.getDictionaryObject(COSName.CONTENTS);

            // download the signed content
            byte[] buf;
            try (FileInputStream fis = new FileInputStream(infile)) {
                buf = sig.getSignedContent(fis);
            }//from   w  w  w  .j  av  a2s  . c o  m

            System.out.println("Signature found");
            System.out.println("Name:     " + sig.getName());
            System.out.println("Modified: " + sdf.format(sig.getSignDate().getTime()));
            String subFilter = sig.getSubFilter();
            if (subFilter != null) {
                switch (subFilter) {
                case "adbe.pkcs7.detached": // COSName.ADBE_PKCS7_DETACHED
                    result.put(sig.getName(), verifyPKCS7(buf, contents, sig));

                    //TODO check certificate chain, revocation lists, timestamp...
                    break;
                case "adbe.pkcs7.sha1": // COSName.ADBE_PKCS7_SHA1
                {
                    // example: PDFBOX-1452.pdf
                    //COSString certString = (COSString) sigDict.getDictionaryObject(
                    //        COSName.CONTENTS);
                    byte[] certData = contents.getBytes();
                    Collection<? extends Certificate> certs = getCertificates(certData);
                    System.out.println("certs=" + certs);
                    byte[] hash = MessageDigest.getInstance("SHA1").digest(buf);
                    result.put(sig.getName(), verifyPKCS7(hash, contents, sig));

                    //TODO check certificate chain, revocation lists, timestamp...
                    break;
                }
                case "adbe.x509.rsa_sha1": // COSName.ADBE_PKCS7_SHA1
                {
                    // example: PDFBOX-2693.pdf
                    COSString certString = (COSString) sigDict.getDictionaryObject(COSName.getPDFName("Cert"));
                    byte[] certData = certString.getBytes();
                    Collection<? extends Certificate> certs = getCertificates(certData);
                    System.out.println("certs=" + certs);

                    //TODO verify signature
                    throw new IOException(subFilter + " verification not supported");
                    //break;
                }
                default:
                    throw new IOException("Unknown certificate type: " + subFilter);
                    //break;
                }
            } else {
                throw new IOException("Missing subfilter for cert dictionary");
            }
        }
    } catch (CMSException | OperatorCreationException ex) {
        throw new IOException(ex);
    }

    return result;
}

From source file:pdfbox.SignatureVerifier.java

License:Apache License

/**
 * Verify a PKCS7 signature.//ww  w  . j a  v  a 2s.  c om
 *
 * @param byteArray the byte sequence that has been signed
 * @param contents the /Contents field as a COSString
 * @param sig the PDF signature (the /V dictionary)
 * @throws CertificateException
 * @throws CMSException
 * @throws StoreException
 * @throws OperatorCreationException
 */
private SignatureResult verifyPKCS7(byte[] byteArray, COSString contents, PDSignature sig)
        throws CMSException, CertificateException, StoreException, OperatorCreationException {
    // inspiration:
    // http://stackoverflow.com/a/26702631/535646
    // http://stackoverflow.com/a/9261365/535646
    CMSProcessable signedContent = new CMSProcessableByteArray(byteArray);
    CMSSignedData signedData = new CMSSignedData(signedContent, contents.getBytes());
    Store certificatesStore = signedData.getCertificates();
    Collection<SignerInformation> signers = signedData.getSignerInfos().getSigners();
    SignerInformation signerInformation = signers.iterator().next();
    Collection matches = certificatesStore.getMatches(signerInformation.getSID());
    X509CertificateHolder certificateHolder = (X509CertificateHolder) matches.iterator().next();
    X509Certificate certFromSignedData = new JcaX509CertificateConverter().getCertificate(certificateHolder);
    //System.out.println("certFromSignedData: " + certFromSignedData);
    certFromSignedData.checkValidity(sig.getSignDate().getTime());

    JcaSimpleSignerInfoVerifierBuilder verifierBuilder = new JcaSimpleSignerInfoVerifierBuilder();
    if (provider != null) {
        verifierBuilder.setProvider(provider);
    }

    boolean validated = false;
    try {
        validated = signerInformation.verify(verifierBuilder.build(certFromSignedData));

    } catch (CMSSignerDigestMismatchException e) {
        System.out.println("Signature failed to validate: ");
        e.printStackTrace();
    }

    return new SignatureResult(certFromSignedData, validated);
}