Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: src/main/java/org/apache/pdfbox/examples/signature/.java
- ===================================================================
- --- src/main/java/org/apache/pdfbox/examples/signature/CreateSignedTimestamp.java (nonexistent)
- +++ src/main/java/org/apache/pdfbox/examples/signature/CreateSignedTimestamp.java (working copy)
- @@ -0,0 +1,177 @@
- +/*
- + * Licensed to the Apache Software Foundation (ASF) under one or more
- + * contributor license agreements. See the NOTICE file distributed with
- + * this work for additional information regarding copyright ownership.
- + * The ASF licenses this file to You 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 java.io.File;
- +import java.io.FileNotFoundException;
- +import java.io.FileOutputStream;
- +import java.io.IOException;
- +import java.io.OutputStream;
- +import java.net.URL;
- +import java.security.GeneralSecurityException;
- +import java.security.MessageDigest;
- +
- +import org.apache.pdfbox.cos.COSName;
- +import org.apache.pdfbox.pdmodel.PDDocument;
- +import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
- +
- +/**
- + * An example for singing a PDF with bouncy castle. A keystore can be created with the java keytool, for example:
- + *
- + * {@code keytool -genkeypair -storepass 123456 -storetype pkcs12 -alias test -validity 365
- + * -v -keyalg RSA -keystore keystore.p12 }
- + *
- + * @author Thomas Chojecki
- + * @author Vakhtang Koroghlishvili
- + * @author John Hewson
- + */
- +public class CreateSignedTimestamp extends CreateSignedTimestampBase
- +{
- +
- + /**
- + * Initialize the signature creator with a keystore and certficate password.
- + */
- + public CreateSignedTimestamp()
- + {
- + super();
- + }
- +
- + /**
- + * Signs the given PDF file. Alters the original file on disk.
- + *
- + * @param file the PDF file to sign
- + * @throws IOException if the file could not be read or written
- + */
- + public void signDetached(File file) throws IOException
- + {
- + signDetached(file, file, null);
- + }
- +
- + /**
- + * Signs the given PDF file.
- + *
- + * @param inFile input PDF file
- + * @param outFile output PDF file
- + * @throws IOException if the input file could not be read
- + */
- + public void signDetached(File inFile, File outFile) throws IOException
- + {
- + signDetached(inFile, outFile, null);
- + }
- +
- + /**
- + * Signs the given PDF file.
- + *
- + * @param inFile input PDF file
- + * @param outFile output PDF file
- + * @param tsaClient optional TSA client
- + * @throws IOException if the input file could not be read
- + */
- + public void signDetached(File inFile, File outFile, TSAClient tsaClient) throws IOException
- + {
- + if (inFile == null || !inFile.exists())
- + {
- + throw new FileNotFoundException("Document for signing does not exist");
- + }
- +
- + FileOutputStream fos = new FileOutputStream(outFile);
- +
- + // sign
- + try (PDDocument doc = PDDocument.load(inFile))
- + {
- + signDetached(doc, fos, tsaClient);
- + }
- + }
- +
- + public void signDetached(PDDocument document, OutputStream output, TSAClient tsaClient)
- + throws IOException
- + {
- + setTsaClient(tsaClient);
- +
- + int accessPermissions = getMDPPermission(document);
- + if (accessPermissions == 1)
- + {
- + throw new IllegalStateException(
- + "No changes to the document are permitted due to DocMDP transform parameters dictionary");
- + }
- +
- + // create signature dictionary
- + PDSignature signature = new PDSignature();
- + signature.setType(COSName.DOC_TIME_STAMP);
- + signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
- + signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161"));//ETSI.RFC3161
- +
- + // the signing date, needed for valid signature
- + //signature.setSignDate(Calendar.getInstance());
- +
- + // Optional: certify
- + if (accessPermissions == 0)
- + {
- + setMDPPermission(document, signature, 2);
- + }
- +
- + // register signature dictionary and sign interface
- + document.addSignature(signature, this);
- +
- + // write incremental (only for signing purpose)
- + document.saveIncremental(output);
- + }
- +
- + public static void main(String[] args) throws IOException, GeneralSecurityException
- + {
- + if (args.length != 3)
- + {
- + usage();
- + System.exit(1);
- + }
- +
- + String tsaUrl = null;
- + if (args[1].equals("-tsa"))
- + {
- + tsaUrl = args[2];
- + } else
- + {
- + usage();
- + System.exit(1);
- + }
- +
- + // TSA client
- + TSAClient tsaClient = null;
- + if (tsaUrl != null)
- + {
- + MessageDigest digest = MessageDigest.getInstance("SHA-256");
- + tsaClient = new TSAClient(new URL(tsaUrl), null, null, digest);
- + }
- +
- + // sign PDF
- + CreateSignedTimestamp signing = new CreateSignedTimestamp();
- +
- + File inFile = new File(args[0]);
- + String name = inFile.getName();
- + String substring = name.substring(0, name.lastIndexOf('.'));
- +
- + File outFile = new File(inFile.getParent(), substring + "_timestamped.pdf");
- + signing.signDetached(inFile, outFile, tsaClient);
- + }
- +
- + private static void usage()
- + {
- + System.err.println("usage: java " + CreateSignedTimestamp.class.getName() + " "
- + + "<pdf_to_sign>\n" + "" + "options:\n"
- + + " -tsa <url> sign timestamp using the given TSA server\n");
- + }
- +}
- Property changes on: src\main\java\org\apache\pdfbox\examples\signature\CreateSignedTimestamp.java
- ___________________________________________________________________
- Added: svn:mime-type
- ## -0,0 +1 ##
- +text/plain
- Index: src/main/java/org/apache/pdfbox/examples/signature/CreateSignedTimestampBase.java
- ===================================================================
- --- src/main/java/org/apache/pdfbox/examples/signature/CreateSignedTimestampBase.java (nonexistent)
- +++ src/main/java/org/apache/pdfbox/examples/signature/CreateSignedTimestampBase.java (working copy)
- @@ -0,0 +1,152 @@
- +/*
- + * Copyright 2015 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 java.io.IOException;
- +import java.io.InputStream;
- +
- +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.io.IOUtils;
- +import org.apache.pdfbox.pdmodel.PDDocument;
- +import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
- +import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
- +
- +public abstract class CreateSignedTimestampBase implements SignatureInterface
- +{
- + private TSAClient tsaClient;
- +
- + public void setTsaClient(TSAClient tsaClient)
- + {
- + this.tsaClient = tsaClient;
- + }
- +
- + public TSAClient getTsaClient()
- + {
- + return tsaClient;
- + }
- +
- + private byte[] getSignedTimeStampByte(InputStream content)
- + throws IOException
- + {
- + return getTsaClient().getTimeStampToken(IOUtils.toByteArray(content));
- + }
- +
- + /**
- + * SignatureInterface implementation.
- + *
- + * This method will be called from inside of the pdfbox and create the PKCS #7 signature. The given InputStream
- + * contains the bytes that are given by the byte range.
- + *
- + * This method is for internal use only.
- + *
- + * Use your favorite cryptographic library to implement PKCS #7 signature creation.
- + *
- + * @throws IOException
- + */
- + @Override
- + public byte[] sign(InputStream content) throws IOException
- + {
- + return getSignedTimeStampByte(content);
- + }
- +
- + /**
- + * 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.
- + */
- + 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;
- + }
- +
- + 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);
- + }
- +}
- Property changes on: src\main\java\org\apache\pdfbox\examples\signature\CreateSignedTimestampBase.java
- ___________________________________________________________________
- Added: svn:mime-type
- ## -0,0 +1 ##
- +text/plain
Advertisement
Add Comment
Please, Sign In to add comment