Example usage for com.itextpdf.text.pdf.security PdfPKCS7 getReason

List of usage examples for com.itextpdf.text.pdf.security PdfPKCS7 getReason

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf.security PdfPKCS7 getReason.

Prototype

public String getReason() 

Source Link

Document

Getter for property reason.

Usage

From source file:cz.hobrasoft.pdfmu.operation.OperationInspect.java

License:Open Source License

private Signature display(PdfPKCS7 pkcs7) {
    Signature signature = new Signature();

    // digitalsignatures20130304.pdf : Code sample 5.3
    to.println("Signature metadata:");
    {//w ww .  j  a v a2s . c  o  m
        SignatureMetadata metadata = new SignatureMetadata();

        to.indentMore();

        // Only name may be null.
        // The values are set in {@link PdfPKCS7#verifySignature}.
        { // name
            String name = pkcs7.getSignName(); // May be null
            metadata.name = name;
            if (name == null) {
                to.println("Name is not set.");
            } else {
                to.println(String.format("Name: %s", name));
            }
        }

        // TODO?: Print "N/A" if the value is an empty string
        // TODO?: Determine whether the value is set in the signature
        to.println(String.format("Reason: %s", pkcs7.getReason()));
        metadata.reason = pkcs7.getReason();
        to.println(String.format("Location: %s", pkcs7.getLocation()));
        metadata.location = pkcs7.getLocation();

        { // Date
            Date date = pkcs7.getSignDate().getTime();
            to.println(String.format("Date and time: %s", date));
            metadata.date = date.toString();
        }

        to.indentLess();

        signature.metadata = metadata;
    }
    { // Certificate chain
        to.indentMore("Certificate chain:");
        Certificate[] certificates = pkcs7.getSignCertificateChain();
        to.println(String.format("Number of certificates: %d", certificates.length));
        int i = 0;
        List<CertificateResult> certificatesResult = new ArrayList<>();
        for (Certificate certificate : certificates) {
            to.indentMore(String.format("Certificate %d%s:", i, (i == 0 ? " (the signing certificate)" : "")));
            CertificateResult certRes;
            String type = certificate.getType();
            to.println(String.format("Type: %s", type));
            // http://docs.oracle.com/javase/1.5.0/docs/guide/security/CryptoSpec.html#AppA
            if ("X.509".equals(type)) {
                X509Certificate certificateX509 = (X509Certificate) certificate;
                certRes = showCertInfo(certificateX509);
            } else {
                certRes = new CertificateResult();
            }
            certRes.type = type;
            to.indentLess();
            certificatesResult.add(certRes);
            ++i;
        }
        signature.certificates = certificatesResult;
        to.indentLess();
    }

    return signature;
}

From source file:org.roda.common.certification.PDFSignatureUtils.java

private static StringBuilder getExtractionInformation(AcroFields fields, ArrayList<?> names,
        Path outputContents, String filename) throws IOException {

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < names.size(); i++) {
        String name = (String) names.get(i);
        Item item = fields.getFieldItem(name);

        PdfDictionary widget = item.getWidget(0);
        PdfDictionary infoDictionary = widget.getAsDict(PdfName.V);
        sb.append("<signature>\n");

        try {//from ww  w .jav a2s. co  m
            PdfPKCS7 pk = fields.verifySignature(name);
            sb = addElementToExtractionResult(sb, "name", name);
            sb = addElementToExtractionResult(sb, "sign-name", pk.getSignName());
            sb = addElementToExtractionResult(sb, "version", Integer.toString(pk.getVersion()));
            sb = addElementToExtractionResult(sb, "reason", pk.getReason());
            sb = addElementToExtractionResult(sb, "location", pk.getLocation());

            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

            if (pk.getTimeStampDate() != null) {
                String timestamp = formatter.format(pk.getTimeStampDate().getTime());
                sb = addElementToExtractionResult(sb, "timestamp-time", timestamp);
            }

            if (pk.getSignDate() != null) {
                String sign = formatter.format(pk.getSignDate().getTime());
                sb = addElementToExtractionResult(sb, "sign-time", sign);
            }

            sb = addElementToExtractionResult(sb, "digest-algorithm", pk.getDigestAlgorithm());
            sb = addElementToExtractionResult(sb, "hash-algorithm", pk.getHashAlgorithm());
            sb = addElementToExtractionResult(sb, "covers-whole-document",
                    Boolean.toString(fields.signatureCoversWholeDocument(name)));
            sb = addElementToExtractionResult(sb, "ft", widget.get(PdfName.FT).toString());

            if (infoDictionary.contains(PdfName.CONTACTINFO))
                sb = addElementToExtractionResult(sb, "contact-info",
                        infoDictionary.getAsString(PdfName.CONTACTINFO).toString());

            if (infoDictionary.contains(PdfName.FILTER))
                sb = addElementToExtractionResult(sb, "filter", infoDictionary.get(PdfName.FILTER).toString());

            if (infoDictionary.contains(PdfName.SUBFILTER))
                sb = addElementToExtractionResult(sb, "subfilter",
                        infoDictionary.get(PdfName.SUBFILTER).toString());

            if (infoDictionary.contains(PdfName.LOCK))
                sb = addElementToExtractionResult(sb, "lock", "true");

            if (infoDictionary.contains(PdfName.CONTENTS)) {
                PdfString elementName = infoDictionary.getAsString(PdfName.CONTENTS);
                Files.write(outputContents, elementName.toUnicodeString().getBytes());
                sb = addElementToExtractionResult(sb, "contents", filename + ".pkcs7");
            }

        } catch (NoSuchFieldError e) {
            LOGGER.warn("DS information extraction did not execute properly");
        }

        sb.append("</signature>");
    }

    return sb;
}

From source file:org.roda.core.plugins.plugins.characterization.PDFSignatureUtils.java

private static StringBuilder getExtractionInformation(AcroFields fields, ArrayList<?> names,
        Path outputContents, String filename) throws IOException {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < names.size(); i++) {
        String name = (String) names.get(i);
        Item item = fields.getFieldItem(name);

        PdfDictionary widget = item.getWidget(0);
        PdfDictionary infoDictionary = widget.getAsDict(PdfName.V);
        sb.append("<signature>\n");

        try {// w  w w.ja  v  a 2s  .  com
            PdfPKCS7 pk = fields.verifySignature(name);
            sb = addElementToExtractionResult(sb, "name", name);
            sb = addElementToExtractionResult(sb, "sign-name", pk.getSignName());
            sb = addElementToExtractionResult(sb, "version", Integer.toString(pk.getVersion()));
            sb = addElementToExtractionResult(sb, "reason", pk.getReason());
            sb = addElementToExtractionResult(sb, "location", pk.getLocation());

            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

            if (pk.getTimeStampDate() != null) {
                String timestamp = formatter.format(pk.getTimeStampDate().getTime());
                sb = addElementToExtractionResult(sb, "timestamp-time", timestamp);
            }

            if (pk.getSignDate() != null) {
                String sign = formatter.format(pk.getSignDate().getTime());
                sb = addElementToExtractionResult(sb, "sign-time", sign);
            }

            sb = addElementToExtractionResult(sb, "digest-algorithm", pk.getDigestAlgorithm());
            sb = addElementToExtractionResult(sb, "hash-algorithm", pk.getHashAlgorithm());
            sb = addElementToExtractionResult(sb, "covers-whole-document",
                    Boolean.toString(fields.signatureCoversWholeDocument(name)));
            sb = addElementToExtractionResult(sb, "ft", widget.get(PdfName.FT).toString());

            if (infoDictionary.contains(PdfName.CONTACTINFO))
                sb = addElementToExtractionResult(sb, "contact-info",
                        infoDictionary.getAsString(PdfName.CONTACTINFO).toString());

            if (infoDictionary.contains(PdfName.FILTER))
                sb = addElementToExtractionResult(sb, "filter", infoDictionary.get(PdfName.FILTER).toString());

            if (infoDictionary.contains(PdfName.SUBFILTER))
                sb = addElementToExtractionResult(sb, "subfilter",
                        infoDictionary.get(PdfName.SUBFILTER).toString());

            if (infoDictionary.contains(PdfName.LOCK))
                sb = addElementToExtractionResult(sb, "lock", "true");

            if (infoDictionary.contains(PdfName.CONTENTS)) {
                PdfString elementName = infoDictionary.getAsString(PdfName.CONTENTS);
                Files.write(outputContents, elementName.toUnicodeString().getBytes());
                sb = addElementToExtractionResult(sb, "contents", filename + ".pkcs7");
            }

        } catch (NoSuchFieldError e) {
            LOGGER.warn("DS information extraction did not execute properly");
        }

        sb.append("</signature>");
    }

    return sb;
}