You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Andreas Lehmkühler (JIRA)" <ji...@apache.org> on 2014/06/16 21:55:02 UTC

[jira] [Commented] (PDFBOX-2082) signing corrupts PDF when signature exactly fits allocated space

    [ https://issues.apache.org/jira/browse/PDFBOX-2082?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14032852#comment-14032852 ] 

Andreas Lehmkühler commented on PDFBOX-2082:
--------------------------------------------

I'm not a signature expert but as fas as I can know 
{code]
String signature = new COSString(sign).getHexString();
{code}
produces a string without the enclosing "<>". So that I can't see a need for your first patch

IMO the second one is also invalid. If a given signature is written as hex string we need twice as much bytes as in the signature itself. There are 2 addtional bytes neede for the enclosing "<>". Looks like 
{code}
preferredSize * 2 + 2 
{code}
is just the right thing to do.

Or am I missing something?

> signing corrupts PDF when signature exactly fits allocated space
> ----------------------------------------------------------------
>
>                 Key: PDFBOX-2082
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-2082
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Writing
>    Affects Versions: 1.8.5, 1.8.6, 2.0.0
>            Reporter: Štěpán Schejbal
>            Priority: Critical
>
> The current check does not take "<>" into account, so if you are (un)lucky, the signature overwrites ">" and corrupts the PDF.
> Fix for 1.8:
> {code}
> diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java b/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
> index 3165589..80fbad2 100644
> --- a/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
> +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
> @@ -778,13 +778,15 @@ public class COSWriter implements ICOSVisitor, Closeable
>          
>              SignatureInterface signatureInterface = doc.getSignatureInterface();
>              byte[] sign = signatureInterface.sign(new ByteArrayInputStream(pdfContent));
> +            // this assumes that the dummy signature has been writen as "<0000...>"
>              String signature = new COSString(sign).getHexString();
> -            int leftSignaturerange = signaturePosition[1]-signaturePosition[0]-signature.length();
> -            if(leftSignaturerange<0)
> +            int startPos = signaturePosition[0] + 1; // move past "<"
> +            int endPos = signaturePosition[1] - 1; // move in front of ">"
> +            if (startPos + signature.length() > endPos)
>              {
>                  throw new IOException("Can't write signature, not enough space");
>              }
> -            getStandardOutput().setPos(signaturePosition[0]+1);
> +            getStandardOutput().setPos(startPos);
>              getStandardOutput().write(signature.getBytes());
>          }
>      }
> {code}
> Another thing is that pdfbox now allocates (2 * preferedSize + 2) for a signature. It quite confused me to see 16k+4 bytes allocated when I called setPreferedSignatureSize(4k) - it should have allocated 8k (each signature byte takes 2 bytes in the pdf). 
> Fix for 1.8:
> {code}
> diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
> index 358364a..23dd3ab 100644
> --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
> +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
> @@ -309,7 +309,7 @@ public class PDDocument implements Pageable, Closeable
>          int preferedSignatureSize = options.getPreferedSignatureSize();
>          if (preferedSignatureSize > 0)
>          {
> -            sigObject.setContents(new byte[preferedSignatureSize * 2 + 2]);
> +            sigObject.setContents(new byte[preferedSignatureSize]);
>          }
>          else
>          {
> {code}



--
This message was sent by Atlassian JIRA
(v6.2#6252)