/*
 * Copyright 2017 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.pdfbox.examples.signature;

import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;

/**
 *
 * @author Tilman Hausherr
 */
public class SigUtils
{

    /**
     * Get the access permissions granted for this document in the DocMDP transform parameters dictionary. Details are
     * described in the table "Entries in the DocMDP transform parameters dictionary" in the PDF specification.
     *
     * @param doc document.
     * @return the permission value. 0 means no DocMDP transform parameters dictionary exists. Other return values are
     * 1, 2 or 3. 2 is also returned if the DocMDP transform parameters dictionary is found but did not contain a /P
     * entry, or if the value is outside the valid range.
     */
    static public int getMDPPermission(PDDocument doc)
    {
        COSBase base = doc.getDocumentCatalog().getCOSObject().getDictionaryObject(COSName.PERMS);
        if (base instanceof COSDictionary)
        {
            COSDictionary permsDict = (COSDictionary) base;
            base = permsDict.getDictionaryObject(COSName.DOCMDP);
            if (base instanceof COSDictionary)
            {
                COSDictionary signatureDict = (COSDictionary) base;
                base = signatureDict.getDictionaryObject("Reference");
                if (base instanceof COSArray)
                {
                    COSArray refArray = (COSArray) base;
                    for (int i = 0; i < refArray.size(); ++i)
                    {
                        base = refArray.getObject(i);
                        if (base instanceof COSDictionary)
                        {
                            COSDictionary sigRefDict = (COSDictionary) base;
                            if (COSName.DOCMDP.equals(sigRefDict.getDictionaryObject("TransformMethod")))
                            {
                                base = sigRefDict.getDictionaryObject("TransformParams");
                                if (base instanceof COSDictionary)
                                {
                                    COSDictionary transformDict = (COSDictionary) base;
                                    int accessPermissions = transformDict.getInt(COSName.P, 2);
                                    if (accessPermissions < 1 || accessPermissions > 3)
                                    {
                                        accessPermissions = 2;
                                    }
                                    return accessPermissions;
                                }
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }

    static public void setMDPPermission(PDDocument doc, PDSignature signature, int accessPermissions)
    {
        COSDictionary sigDict = signature.getCOSObject();

        // DocMDP specific stuff
        COSDictionary transformParameters = new COSDictionary();
        transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
        transformParameters.setInt(COSName.P, accessPermissions);
        transformParameters.setName(COSName.V, "1.2");
        transformParameters.setNeedToBeUpdated(true);

        COSDictionary referenceDict = new COSDictionary();
        referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
        referenceDict.setItem("TransformMethod", COSName.getPDFName("DocMDP"));
        referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
        referenceDict.setItem("TransformParams", transformParameters);
        referenceDict.setNeedToBeUpdated(true);

        COSArray referenceArray = new COSArray();
        referenceArray.add(referenceDict);
        sigDict.setItem("Reference", referenceArray);
        referenceArray.setNeedToBeUpdated(true);

        // Catalog
        COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
        COSDictionary permsDict = new COSDictionary();
        catalogDict.setItem(COSName.PERMS, permsDict);
        permsDict.setItem(COSName.DOCMDP, signature);
        catalogDict.setNeedToBeUpdated(true);
        permsDict.setNeedToBeUpdated(true);
    }
}

@@ -943,16 +943,74 @@
 %0A%0A/**%0A *
+ Utility class for the signature / timestamp examples.%0A * 
 %0A * @aut
@@ -1056,16 +1056,51 @@
 gUtils%0A%7B
+%0A    private SigUtils()%0A    %7B%0A    %7D
 %0A%0A    /*
@@ -1187,32 +1187,39 @@
 sform parameters
+%0A     *
  dictionary. Det
@@ -1226,23 +1226,16 @@
 ails are
-%0A     *
  describ
@@ -1285,32 +1285,39 @@
 sform parameters
+%0A     *
  dictionary%22 in 
@@ -1474,16 +1474,23 @@
 s. Other
+%0A     *
  return 
@@ -1499,23 +1499,16 @@
 lues are
-%0A     *
  1, 2 or
@@ -1575,16 +1575,23 @@
 ctionary
+%0A     *
  is foun
@@ -1616,23 +1616,16 @@
 ain a /P
-%0A     *
  entry, 
@@ -3611,24 +3611,429 @@
 n 0;%0A    %7D%0A%0A
+    /**%0A     * Set the access permissions granted for this document in the DocMDP transform parameters%0A     * dictionary. Details are described in the table %22Entries in the DocMDP transform parameters%0A     * dictionary%22 in the PDF specification.%0A     *%0A     * @param doc The document.%0A     * @param signature The signature object.%0A     * @param accessPermissions The permission value (1, 2 or 3).%0A     */%0A
     static p
@@ -4746,28 +4746,14 @@
 ame.
-getPDFName(%22Doc
+DOC
 MDP
-%22)
 );%0A 

improve javadoc, sonar fix