pCOS Cookbook

cookbook

interactive/signatures

Retrieve digital signature information.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.interactive;

import com.pdflib.IpCOS;

import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/**
 * Retrieve digital signature information. The following types of signatures
 * are checked:
 * <p>
 * - plain (approval) signatures
 * - certification signatures
 * - document timestamp signatures
 * - signatures for Reader-enabling a document
 * - DSS (Document Security Store) information is also dumped
 * <p>
 * Required software: pCOS interface 3 (pCOS 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document
 * 
 * @version $Id: signatures.java,v 1.11 2016/06/10 12:35:49 stm Exp $
 */
public class signatures extends pcos_cookbook_example {

    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    public void example_code(IpCOS p, int doc) throws Exception {

        System.out.println("File name: " + p.pcos_get_string(doc, "filename"));

        // -------------------------------------------------------------
        // Check Reader-enabled document

        String objtype = p.pcos_get_string(doc, "type:/Root/Perms");
        String objtype2 = p.pcos_get_string(doc,
            "type:/Root/ViewerPreferences/Rights");

        if (objtype.equals("dict") || objtype2.equals("dict"))
        {
            System.out.println("Reader-enabled: yes");
        }
        
        // -------------------------------------------------------------
        // Check Document Security Store (DSS)
        
        objtype = p.pcos_get_string(doc, "type:/Root/DSS");
        if (objtype.equals("dict"))
        {
            System.out.println("Document Security Store (DSS) present:");
            
            int val = 0;
            objtype = p.pcos_get_string(doc, "type:/Root/DSS/VRI");
            if (objtype.equals("dict"))
                val = (int) p.pcos_get_number(doc, "length:/Root/DSS/VRI");
            System.out.println("\t" + val +
                " VRI entries (validation-related information)");
            
            val = 0;
            objtype = p.pcos_get_string(doc, "type:/Root/DSS/Certs");
            if (objtype.equals("array"))
                val = (int) p.pcos_get_number(doc, "length:/Root/DSS/Certs");
            System.out.println("\t" + val + " certificates");
            
            val = 0;
            objtype = p.pcos_get_string(doc, "type:/Root/DSS/OCSPs");
            if (objtype.equals("array"))
                val = (int) p.pcos_get_number(doc, "length:/Root/DSS/OCSPs");
            System.out.println("\t" + val + " OCSP responses");
            
            val = 0;
            objtype = p.pcos_get_string(doc, "type:/Root/DSS/CRLs");
            if (objtype.equals("array"))
                val = (int) p.pcos_get_number(doc, "length:/Root/DSS/CRLs");
            System.out.println("\t" + val + " CRLs");
        }

        int pcosinterface = (int) p.pcos_get_number(doc, "pcosinterface");
        
        if (pcosinterface >= 9) {
            print_signatures_pcosinterface_9(p, doc);
        }
        else {
            print_signatures_pcosinterface_8(p, doc);
        }
    }

    /**
     * Print information about signature fields with the information available
     * in the "signaturefields" pseudo object array. This is the recommended
     * approach if pcosinterface version 9 or higher is available.
     * 
     * @param p
     *            IpCOS context
     * @param doc
     *            document handle
     * @throws Exception
     */
    private void print_signatures_pcosinterface_9(IpCOS p, int doc)
        throws Exception {
        int fieldcount = (int) p.pcos_get_number(doc, "length:signaturefields");

        int sigcount = 0;

        for (int f = 0; f < fieldcount; f++) {
            sigcount++;

            /* invisible signatures have /Rect [0 0 0 0] */
            boolean invisible = p.pcos_get_number(doc,
                "signaturefields[" + f + "]/Rect[0]") == 0
                && p.pcos_get_number(doc,
                    "signaturefields[" + f + "]/Rect[1]") == 0
                && p.pcos_get_number(doc,
                    "signaturefields[" + f + "]/Rect[2]") == 0
                && p.pcos_get_number(doc,
                    "signaturefields[" + f + "]/Rect[3]") == 0;
            System.out.print(invisible ? "Invisible" : "Visible");

            String res = p.pcos_get_string(doc,
                "signaturefields[" + f + "]/fullname");
            System.out.print(" signature field '" + res + "': ");

            String objtype = p.pcos_get_string(doc,
                "type:signaturefields[" + f + "]/V");

            if (objtype.equals("dict")) {
                // -----------------------------------------------
                // Check whether this is a certification signature
                // must be the first in the document

                if (sigcount == 1) {
                    objtype = p.pcos_get_string(doc, "type:/Root/Perms/DocMDP");

                    if (objtype.equals("dict")) {
                        int objid = (int) p.pcos_get_number(doc,
                            "pcosid:signaturefields[" + f + "]/V");
                        int objid2 = (int) p.pcos_get_number(doc,
                            "pcosid:/Root/Perms/DocMDP");

                        if (objid == objid2)
                            System.out.println("certification signature");

                        // Check which kinds of changes are allowed
                        objtype = p.pcos_get_string(doc, "type:signaturefields["
                            + f + "]/V/Reference[0]/TransformParams/P");

                        if (objtype.equals("number")) {
                            int val = (int) p.pcos_get_number(doc,
                                "signaturefields[" + f
                                    + "]/V/Reference[0]/TransformParams/P");

                            if (val == 1) {
                                System.out.println("no changes allowed");
                            }
                            else if (val == 2) {
                                System.out.println(
                                    "only form filling and signing allowed");
                            }
                            else if (val == 2) {
                                System.out.println(
                                    "form filling and signing as well as annotation creation, deletion, and modification allowed");
                            }
                        }
                    }
                }

                // ------------------------------------------------
                // Check Document Timestamp signature

                res = p.pcos_get_string(doc,
                    "signaturefields[" + f + "]/V/Type");

                if (res.equals("DocTimeStamp")) {
                    System.out.println("time-stamp signature");
                    continue; // nothing more to show
                }
                else if (res.equals("Sig"))
                    System.out.println("signed");
                else {
                    System.out.println("unknown signature type " + res);
                    continue;
                }

                /* dump various pieces of other signature information */

                res = p.pcos_get_string(doc,
                    "signaturefields[" + f + "]/V/Filter");
                System.out.println("Filter: '" + res + "'");

                objtype = p.pcos_get_string(doc,
                    "type:signaturefields[" + f + "]/V/Name");
                if (objtype.equals("string")) {
                    res = p.pcos_get_string(doc,
                        "signaturefields[" + f + "]/V/Name");
                    System.out.println("Name: '" + res + "'");
                }
                objtype = p.pcos_get_string(doc,
                    "type:signaturefields[" + f + "]/V/Reason");
                if (objtype.equals("string")) {
                    res = p.pcos_get_string(doc,
                        "signaturefields[" + f + "]/V/Reason");
                    System.out.println("Reason: '" + res + "'");
                }
                objtype = p.pcos_get_string(doc,
                    "type:signaturefields[" + f + "]/V/M");
                if (objtype.equals("string")) {
                    res = p.pcos_get_string(doc,
                        "signaturefields[" + f + "]/V/M");
                    System.out.println("Time of signing: '" + res + "'");
                }

                /* a document may contain more than one signature */
                System.out.println();
            }
            else {
                System.out.println("unsigned");
            }
        }
        if (sigcount == 0)
            System.out.println("no signature fields");
    }
    
    /**
     * Print information about signature fields with the information available
     * in the "fields" pseudo object array. This is the approach
     * that works with pcosinterface versions up to 8.
     * 
     * @param p
     *            IpCOS context
     * @param doc
     *            document handle
     * @throws Exception
     */
    private void print_signatures_pcosinterface_8(IpCOS p, int doc)
        throws Exception {
        int fieldcount = (int) p.pcos_get_number(doc, "length:fields");

        int sigcount = 0;

        for (int f = 0; f < fieldcount; f++) {
            String objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/FT");
            if (!objtype.equals("name"))
                continue;

            String res = p.pcos_get_string(doc, "fields[" + f + "]/FT");
            if (!res.equals("Sig"))
                continue;

            sigcount++;

            /* invisible signatures have /Rect [0 0 0 0] */
            boolean invisible = p.pcos_get_number(doc,
                "fields[" + f + "]/Rect[0]") == 0
                && p.pcos_get_number(doc, "fields[" + f + "]/Rect[1]") == 0
                && p.pcos_get_number(doc, "fields[" + f + "]/Rect[2]") == 0
                && p.pcos_get_number(doc, "fields[" + f + "]/Rect[3]") == 0;
            System.out.print(invisible ? "Invisible" : "Visible");

            res = p.pcos_get_string(doc, "fields[" + f + "]/fullname");
            System.out.print(" signature field '" + res + "': ");

            objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/V");

            if (objtype.equals("dict")) {
                // -----------------------------------------------
                // Check whether this is a certification signature
                // must be the first in the document

                if (sigcount == 1) {
                    objtype = p.pcos_get_string(doc, "type:/Root/Perms/DocMDP");

                    if (objtype.equals("dict")) {
                        int objid = (int) p.pcos_get_number(doc,
                            "pcosid:fields[" + f + "]/V");
                        int objid2 = (int) p.pcos_get_number(doc,
                            "pcosid:/Root/Perms/DocMDP");

                        if (objid == objid2)
                            System.out.println("certification signature");

                        // Check which kinds of changes are allowed
                        objtype = p.pcos_get_string(doc, "type:fields[" + f
                            + "]/V/Reference[0]/TransformParams/P");

                        if (objtype.equals("number")) {
                            int val = (int) p.pcos_get_number(doc, "fields[" + f
                                + "]/V/Reference[0]/TransformParams/P");

                            if (val == 1) {
                                System.out.println("no changes allowed");
                            }
                            else if (val == 2) {
                                System.out.println(
                                    "only form filling and signing allowed");
                            }
                            else if (val == 2) {
                                System.out.println(
                                    "form filling and signing as well as annotation creation, deletion, and modification allowed");
                            }
                        }
                    }
                }

                // ------------------------------------------------
                // Check Document Timestamp signature

                res = p.pcos_get_string(doc, "fields[" + f + "]/V/Type");

                if (res.equals("DocTimeStamp")) {
                    System.out.println("time-stamp signature");
                    continue; // nothing more to show
                }
                else if (res.equals("Sig"))
                    System.out.println("signed");
                else {
                    System.out.println("unknown signature type " + res);
                    continue;
                }

                /* dump various pieces of other signature information */

                res = p.pcos_get_string(doc, "fields[" + f + "]/V/Filter");
                System.out.println("Filter: '" + res + "'");

                objtype = p.pcos_get_string(doc,
                    "type:fields[" + f + "]/V/Name");
                if (objtype.equals("string")) {
                    res = p.pcos_get_string(doc, "fields[" + f + "]/V/Name");
                    System.out.println("Name: '" + res + "'");
                }
                objtype = p.pcos_get_string(doc,
                    "type:fields[" + f + "]/V/Reason");
                if (objtype.equals("string")) {
                    res = p.pcos_get_string(doc, "fields[" + f + "]/V/Reason");
                    System.out.println("Reason: '" + res + "'");
                }
                objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/V/M");
                if (objtype.equals("string")) {
                    res = p.pcos_get_string(doc, "fields[" + f + "]/V/M");
                    System.out.println("Time of signing: '" + res + "'");
                }

                /* a document may contain more than one signature */
                System.out.println();
            }
            else {
                System.out.println("unsigned");
            }
        }
        if (sigcount == 0)
            System.out.println("no signature fields");
    }

    public signatures(String[] argv, String readable_name, String search_path,
        String full_rcs_file_name, String revision) {
        super(argv, readable_name, search_path, full_rcs_file_name, revision);
    }

    public static void main(String argv[]) {
        signatures example = new signatures(argv, "Digital signature",
            SEARCH_PATH, "$RCSfile: signatures.java,v $", "$Revision: 1.11 $");
        example.execute();
    }
}