You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ad...@apache.org on 2011/04/16 04:52:46 UTC
svn commit: r1092857 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: exceptions/
pdmodel/interactive/digitalsignature/ pdmodel/interactive/form/
Author: adam
Date: Sat Apr 16 02:52:46 2011
New Revision: 1092857
URL: http://svn.apache.org/viewvc?rev=1092857&view=rev
Log:
PDFBOX-912: PDF signing interface and improvements
Added:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/exceptions/SignatureException.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureInterface.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureOptions.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/exceptions/SignatureException.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/exceptions/SignatureException.java?rev=1092857&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/exceptions/SignatureException.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/exceptions/SignatureException.java Sat Apr 16 02:52:46 2011
@@ -0,0 +1,95 @@
+package org.apache.pdfbox.exceptions;
+
+/*
+ * 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.
+ */
+
+/**
+ * An exception that indicates a problem during the signing process.
+ *
+ * @author Thomas Chojecki
+ * @version $Revision: $
+ */
+public class SignatureException extends Exception
+{
+
+ public final static int WRONG_PASSWORD = 1;
+
+ public final static int UNSUPPORTED_OPERATION = 2;
+
+ public final static int CERT_PATH_CHECK_INVALID = 3;
+
+ public final static int NO_SUCH_ALGORITHM = 4;
+
+ public final static int INVALID_PAGE_FOR_SIGNATURE = 5;
+
+ public final static int VISUAL_SIGNATURE_INVALID = 6;
+
+ private int no;
+
+ /**
+ * Constructor.
+ *
+ * @param msg A msg to go with this exception.
+ */
+ public SignatureException( String msg )
+ {
+ super( msg );
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param errno A error number to fulfill this exception
+ * @param msg A msg to go with this exception.
+ */
+ public SignatureException( int errno , String msg )
+ {
+ super( msg );
+ no = errno;
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param e The exception that should be encapsulate.
+ */
+ public SignatureException(Throwable e)
+ {
+ super(e);
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param errno A error number to fulfill this exception
+ * @param e The exception that should be encapsulate.
+ */
+ public SignatureException( int errno, Throwable e)
+ {
+ super(e);
+ }
+
+ /**
+ * A error number to fulfill this exception
+ *
+ * @return the error number if available, otherwise 0
+ */
+ public int getErrNo()
+ {
+ return no;
+ }
+}
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureInterface.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureInterface.java?rev=1092857&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureInterface.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureInterface.java Sat Apr 16 02:52:46 2011
@@ -0,0 +1,25 @@
+package org.apache.pdfbox.pdmodel.interactive.digitalsignature;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.pdfbox.exceptions.SignatureException;
+
+/**
+ * Providing an interface for accessing necessary functions for signing a pdf document.
+ *
+ * @author <a href="mailto:mail@thomas-chojecki.de">Thomas Chojecki</a>
+ * @version $
+ */
+public interface SignatureInterface
+{
+
+ /**
+ * Creates a cms signature for the given content
+ *
+ * @param content is the content as a (Filter)InputStream
+ * @return signature as a byte array
+ */
+ public byte[] sign (InputStream content) throws SignatureException, IOException;
+
+}
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureOptions.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureOptions.java?rev=1092857&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureOptions.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/SignatureOptions.java Sat Apr 16 02:52:46 2011
@@ -0,0 +1,36 @@
+package org.apache.pdfbox.pdmodel.interactive.digitalsignature;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.pdfbox.cos.COSDocument;
+import org.apache.pdfbox.pdfparser.VisualSignatureParser;
+
+
+public class SignatureOptions
+{
+ private COSDocument visualSignature;
+
+ private int pageNo;
+
+ public void setPage(int pageNo)
+ {
+ this.pageNo = pageNo;
+ }
+
+ public int getPage() {
+ return pageNo;
+ }
+
+ public void setVisualSignature(InputStream is) throws IOException
+ {
+ VisualSignatureParser visParser = new VisualSignatureParser(is);
+ visParser.parse();
+ visualSignature = visParser.getDocument();
+ }
+
+ public COSDocument getVisualSignature()
+ {
+ return visualSignature;
+ }
+}
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java?rev=1092857&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java Sat Apr 16 02:52:46 2011
@@ -0,0 +1,165 @@
+/*
+ * 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.pdmodel.interactive.form;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * A class for handling the PDF field as a signature.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @author Thomas Chojecki
+ * @version $Revision: 1.5 $
+ */
+public class PDSignatureField extends PDField
+{
+
+ /**
+ * @see PDField#PDField(PDAcroForm,COSDictionary)
+ *
+ * @param theAcroForm The acroForm for this field.
+ * @param field The dictionary for the signature.
+ * @throws IOException If there is an error while resolving partital name for the signature field
+ */
+ public PDSignatureField( PDAcroForm theAcroForm, COSDictionary field) throws IOException
+ {
+ super(theAcroForm,field);
+ // dirty hack to avoid npe caused through getWidget() method
+ getDictionary().setName( COSName.TYPE, "Annot" );
+ getDictionary().setName( COSName.SUBTYPE, PDAnnotationWidget.SUB_TYPE);
+ }
+
+ /**
+ * @see PDField#PDField(PDAcroForm)
+ *
+ * @param theAcroForm The acroForm for this field.
+ * @throws IOException If there is an error while resolving partial name for the signature field
+ */
+ public PDSignatureField( PDAcroForm theAcroForm) throws IOException
+ {
+ super( theAcroForm );
+ getDictionary().setName("FT", "Sig");
+ getWidget().setLocked(true);
+ getWidget().setPrinted(true);
+ setPartialName(generatePartialName());
+ getDictionary().setName( COSName.TYPE, "Annot" );
+ getDictionary().setName( COSName.SUBTYPE, PDAnnotationWidget.SUB_TYPE);
+ }
+
+ /**
+ * Generate a unique name for the signature
+ * @return
+ * @throws IOException
+ */
+ private String generatePartialName() throws IOException
+ {
+ PDAcroForm acroForm = getAcroForm();
+ List fields = acroForm.getFields();
+
+ String fieldName = "Signature";
+ int i = 1;
+
+ Set<String> sigNames = new HashSet<String>();
+
+ for ( Object object : fields )
+ {
+ if(object instanceof PDSignatureField)
+ {
+ sigNames.add(((PDSignatureField)object).getPartialName());
+ }
+ }
+
+ while(sigNames.contains(fieldName+i))
+ ++i;
+
+ return fieldName+i;
+ }
+
+ /**
+ * @see PDField#setValue(java.lang.String)
+ *
+ * @param value The new value for the field.
+ *
+ * @throws IOException If there is an error creating the appearance stream.
+ * @deprecated use setSignature(PDSignature) instead
+ */
+ @Override
+ @Deprecated
+ public void setValue(String value) throws IOException
+ {
+ throw new RuntimeException( "Can't set signature as String, use setSignature(PDSignature) instead" );
+ }
+
+ /**
+ * @see PDField#setValue(java.lang.String)
+ *
+ * @return The string value of this field.
+ *
+ * @throws IOException If there is an error creating the appearance stream.
+ * @deprecated use getSignature() instead
+ */
+ @Override
+ @Deprecated
+ public String getValue() throws IOException
+ {
+ throw new RuntimeException( "Can't get signature as String, use getSignature() instead." );
+ }
+
+ /**
+ * Return a string rep of this object.
+ *
+ * @return A string rep of this object.
+ */
+ @Override
+ public String toString()
+ {
+ return "PDSignature";
+ }
+
+ /**
+ * Add a signature dictionary to the signature field
+ *
+ * @param value is the PDSignature
+ */
+ public void setSignature(PDSignature value)
+ {
+ getDictionary().setItem("V", value);
+ }
+
+ /**
+ * Get the signature dictionary
+ *
+ * @return the signature dictionary
+ *
+ */
+ public PDSignature getSignature()
+ {
+ COSBase dictionary = getDictionary().getDictionaryObject(COSName.V);
+ if (dictionary == null)
+ return null;
+ return new PDSignature((COSDictionary)dictionary);
+ }
+}