at.knowcenter.wag.egov.egiz.pdf
Class BinarySignature

java.lang.Object
  extended by at.knowcenter.wag.egov.egiz.pdf.BinarySignature

public abstract class BinarySignature
extends Object

Contains various extension functions to digitally sign documents.

These functions are used to replace parts of the original Egiz plain text signature mechanism.

Author:
wprinz

Field Summary
static byte[] BREV_DAT
          The date brev.
static byte[] BREV_ISS
          The issure brev.
static byte[] BREV_NIL
          The nil brev used to define an unrecognized value.
static byte[] BREV_SID
          The SIG_ID brev.
static byte[] BREV_SNR
          The serial number brev.
static byte[] BREV_VAL
          The value brev.
static int CERTIFICATE_PLACEHOLDER_LENGTH
          The number of bytes left out for the certificate placeholder.
static PdfName EGIZ_BYTERANGES_NAME
          The PDFName of the byte ranges array.
static PdfName EGIZ_CERTIFICATE_NAME
          The PdfName of the certificate array.
static PdfName EGIZ_DICT_NAME
          The PDFName of the Egiz Dictionary.
static PdfName EGIZ_ENCODINGS_NAME
          The PDFName of the /encodings field in an Egiz Dictionary.
static PdfName EGIZ_KZ_NAME
          The PDFName of the Kennzeichnung attribute.
static PdfName EGIZ_ODS_NAME
          The PDFName of the Original Document Size (ODS) field in an Egiz Dictionary.
static PdfName EGIZ_REPLACES_NAME
          The PDFName of the /replaces field in an Egiz Dictionary.
static PdfName EGIZ_XOBJ_NAME
          The PDFName of the Signature XObject field in an Egiz Dictionary.
static byte[] ENCODING_NIL
          No explicit encoding.
static byte[] ENCODING_URL
          URL encoding.
static byte[] ENCODING_WIN
          PDF WinAnsiEncoding.
static byte LAYOUT_PLACEHOLDER
          The placeholder character used to fill out Strings in the layout process.
static int LINE_BREAK_TOLERANCE
          The tolerance area of the line break algorithm.
protected static PdfNumber NUMBER_PLACEHOLDER
          The number placeholder that is used to give numbers a fixed length.
static byte SIGN_PLACEHOLDER
          This placeholder is used to fill out holes between the byte ranges before the document is signed.
 
Constructor Summary
BinarySignature()
           
 
Method Summary
static byte[] buildDigest(byte[] data, int length)
          Builds a digest of the given data.
protected static int calcNumReps(List replaces)
          Calculates the number of actual String replaces from a given ReplaceInfo list.
protected static void createEgizDict(PdfStamper stamper, PdfTemplate table_template, IncrementalUpdateInformation iui, List variable_field_definitions, List all_field_definitions)
          Creates the EGIZ Dictionary and adds it to the document.
protected static List determineKZ(byte[] pdf, int begin, int end, List field_definitions)
          Determines the Kennzeichnug in the content stream.
protected static List determineReplacesInContentStream(byte[] pdf, int begin, int end, List field_definitions)
          Determines the List of ReplaceInfo objects of replaces in the content stream regarding the given field definitions.
static List extractSignatureHoldersBinary(byte[] pdf)
          Retrieves the List of SignatureHolders containing the information of all digital signatures of the given document.
static String extractSignatureTextOnly(PdfDictionary egiz_dict)
          Extracts the signature text only.
static String extractTextBinary(byte[] pdf)
          Extracts the signable text from a binary pdf document.
static String extractTextBinary(File doc)
          Extracts the binary 'text' of a document.
static String extractTextBinary(InputStream is)
          Extracts the text binary.
protected static int findFirstNotPlaceholder(List strings, int start)
          Finds the first string after and at the given index not being a placeholder string.
protected static int findIndex(List strings, byte[] caption)
          Finds the index of the StringInfo within the StringInfo list that has the given content (caption).
static List getEgizDictChainFromReader(PdfReader reader)
          Retrieves the chain of Egiz Dictionaries from the reader.
static PdfDictionary getEgizDictFromReader(PdfReader reader)
          Retrieves the Egiz Dictionary from the document if present.
static PdfIndirectReference getEgizDictIndRefFromReader(PdfReader reader)
          Retrieves the Egiz Dictionary's indirect reference from the reader.
static int getOriginalDocumentSizeFromEgizDict(PdfDictionary egiz_dict)
          Retrieves the size of the original document from the Egiz Dictionary.
static PdfDictionary getPreviousFromEgizDict(PdfDictionary egiz_dict)
          Retrieves the previous Egiz dictionary from the given one, if a previous dictionary exists.
protected static boolean isCaption(List strings, int index, byte[] caption)
           
protected static boolean isPlaceholder(StringInfo si, byte placeholder)
          Tells, if the given StringInfo contains only placeholder characters.
static void main(String[] args)
          For debugging purposes.
static void markByteRanges(IncrementalUpdateInformation iui)
          Updates the information in the egiz dictionary to reflect the real offsets of the byte ranges.
static byte[] prepareDataToSign(byte[] data, List byte_ranges)
          Fills the holes in the byte ranges with the SIGN_PLACEHOLDER.
static int readNumber(byte[] pdf, int start_index, int num_digits)
          Reads an unsigned integer number.
static List reconstructReplaces(byte[] pdf, byte[][] brevs, StringInfo[] sis, byte[][] encodings)
          Reconstructs the replaces from the PDF and forms suitable value strings.
static void replaceCertificate(IncrementalUpdateInformation iui)
          Replaces the certificate placeholder with the certificate from the signed Signature Object.
static void replaceNumber(byte[] pdf, int start_index, int number, int num_digits)
          Replaces a number by the new value.
static void replacePlaceholders(IncrementalUpdateInformation iui)
          Replaces the placeholders with values from the signed SignatureObject.
static void restorePlaceholder(byte[] pdf, StringInfo si, byte placeholder)
          Restores the given String to its placeholder.
static String retrieveSignableTextFromData(byte[] data, int ods)
          Retrieves the signable text from the given document.
protected static int skipStrings(List strings, int index, byte[] caption)
           
protected static boolean startsWithCaption(StringInfo si, List field_definitions)
           
protected static byte[] typeToBrev(String type)
          Converts a field name (type) to the corresponding BREV.
static IncrementalUpdateInformation writeIncrementalUpdate(byte[] original_document, PdfPTable pdf_table, PositioningInstruction pi, List variable_field_definitions, List all_field_definitions)
          Signs a document with the given signature table using the Incremental Update method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_BREAK_TOLERANCE

public static final int LINE_BREAK_TOLERANCE
The tolerance area of the line break algorithm.

See Also:
Placeholder.replacePlaceholderWithTolerance(byte[], List, byte[], int), Constant Field Values

CERTIFICATE_PLACEHOLDER_LENGTH

public static final int CERTIFICATE_PLACEHOLDER_LENGTH
The number of bytes left out for the certificate placeholder.

See Also:
Constant Field Values

LAYOUT_PLACEHOLDER

public static final byte LAYOUT_PLACEHOLDER
The placeholder character used to fill out Strings in the layout process.

See Also:
Constant Field Values

SIGN_PLACEHOLDER

public static final byte SIGN_PLACEHOLDER
This placeholder is used to fill out holes between the byte ranges before the document is signed.

See Also:
Constant Field Values

BREV_NIL

public static final byte[] BREV_NIL
The nil brev used to define an unrecognized value.


BREV_DAT

public static final byte[] BREV_DAT
The date brev.


BREV_ISS

public static final byte[] BREV_ISS
The issure brev.


BREV_SNR

public static final byte[] BREV_SNR
The serial number brev.


BREV_VAL

public static final byte[] BREV_VAL
The value brev.


BREV_SID

public static final byte[] BREV_SID
The SIG_ID brev.


ENCODING_NIL

public static final byte[] ENCODING_NIL
No explicit encoding.


ENCODING_WIN

public static final byte[] ENCODING_WIN
PDF WinAnsiEncoding.


ENCODING_URL

public static final byte[] ENCODING_URL
URL encoding.


EGIZ_DICT_NAME

public static final PdfName EGIZ_DICT_NAME
The PDFName of the Egiz Dictionary.

Used to locate and identify the Egiz Dictionary in the document.


EGIZ_ODS_NAME

public static final PdfName EGIZ_ODS_NAME
The PDFName of the Original Document Size (ODS) field in an Egiz Dictionary.

The ODS must be a positive integral number.


EGIZ_KZ_NAME

public static final PdfName EGIZ_KZ_NAME
The PDFName of the Kennzeichnung attribute.


EGIZ_REPLACES_NAME

public static final PdfName EGIZ_REPLACES_NAME
The PDFName of the /replaces field in an Egiz Dictionary.


EGIZ_ENCODINGS_NAME

public static final PdfName EGIZ_ENCODINGS_NAME
The PDFName of the /encodings field in an Egiz Dictionary.


EGIZ_BYTERANGES_NAME

public static final PdfName EGIZ_BYTERANGES_NAME
The PDFName of the byte ranges array.


EGIZ_CERTIFICATE_NAME

public static final PdfName EGIZ_CERTIFICATE_NAME
The PdfName of the certificate array.


EGIZ_XOBJ_NAME

public static final PdfName EGIZ_XOBJ_NAME
The PDFName of the Signature XObject field in an Egiz Dictionary.

This must be an indirect reference to the XObject containing the Signature table.


NUMBER_PLACEHOLDER

protected static final PdfNumber NUMBER_PLACEHOLDER
The number placeholder that is used to give numbers a fixed length.

Constructor Detail

BinarySignature

public BinarySignature()
Method Detail

extractSignatureTextOnly

public static String extractSignatureTextOnly(PdfDictionary egiz_dict)
                                       throws IOException
Extracts the signature text only.

The signature text is the text of the Signature XObject.

Parameters:
egiz_dict - The Egiz Dictionary.
Returns:
Returns the signature text.
Throws:
IOException

getOriginalDocumentSizeFromEgizDict

public static int getOriginalDocumentSizeFromEgizDict(PdfDictionary egiz_dict)
Retrieves the size of the original document from the Egiz Dictionary.

Parameters:
egiz_dict - The Egiz Dictionary.
Returns:
Returns the size (in bytes) of the original document.

getPreviousFromEgizDict

public static PdfDictionary getPreviousFromEgizDict(PdfDictionary egiz_dict)
Retrieves the previous Egiz dictionary from the given one, if a previous dictionary exists.

Parameters:
egiz_dict - The Egiz Dictionary.
Returns:
Returns the previous Egiz Dictionary, or null if there is none.

getEgizDictFromReader

public static PdfDictionary getEgizDictFromReader(PdfReader reader)
Retrieves the Egiz Dictionary from the document if present.

Parameters:
reader - The reader to retrieve the dictionary from.
Returns:
Returns the Egiz Dictionary, if present, or returns null, if no egiz dictionary was found.

getEgizDictIndRefFromReader

public static PdfIndirectReference getEgizDictIndRefFromReader(PdfReader reader)
Retrieves the Egiz Dictionary's indirect reference from the reader.

Parameters:
reader - The reader.
Returns:
Returns the indirect reference of the Egiz Dictionary, or null, if none exists.

getEgizDictChainFromReader

public static List getEgizDictChainFromReader(PdfReader reader)
Retrieves the chain of Egiz Dictionaries from the reader.

The first element in the List will be the top most (oldest) Egiz Dictionary. The last element in the List will be the bottom most (latest) Egiz Dictionary. If the list is empty, no dictionary could be found at all, which means that the document is not digitally signed.

Parameters:
reader - The reader.
Returns:
Returns the List of PdfDictionaries from the document.

buildDigest

public static byte[] buildDigest(byte[] data,
                                 int length)
                          throws PDFDocumentException
Builds a digest of the given data.

Parameters:
data - The data to be digested.
length - The length of the data portion that should be used for digesting. This allows to build the digest only over parts of the data.
Returns:
Returns the created digest.
Throws:
PDFDocumentException - Forwarded exception.

retrieveSignableTextFromData

public static String retrieveSignableTextFromData(byte[] data,
                                                  int ods)
Retrieves the signable text from the given document.

Parameters:
data - The data.
ods - The original document size.
Returns:
Returns the signable text.

prepareDataToSign

public static byte[] prepareDataToSign(byte[] data,
                                       List byte_ranges)
Fills the holes in the byte ranges with the SIGN_PLACEHOLDER.

Parameters:
data - The given byte ranged data.
byte_ranges - The byte ranges.
Returns:
Returns the filled text.

extractTextBinary

public static String extractTextBinary(File doc)
                                throws PDFDocumentException
Extracts the binary 'text' of a document.

If the document contains an Egiz Dictionary, which means that it is already signed, the binary text is the Base64 coded string of the original document followed by the Ascii representation of the signature block.

If the document does not contain an Egiz Dictionary, which means that it is unsigned, only the binary Base64 coded original document is returned as binary text.

This function is intented for being used instead of the "text extraction" mechanism used in the plain text Egiz project.

Parameters:
doc - The file.
Returns:
Returns the binary text of the document.
Throws:
PDFDocumentException - Forwarded exception.

extractTextBinary

public static String extractTextBinary(InputStream is)
                                throws PDFDocumentException
Extracts the text binary.

Parameters:
is -
Returns:
Returns the binary text.
Throws:
PDFDocumentException

extractTextBinary

public static String extractTextBinary(byte[] pdf)
                                throws PDFDocumentException
Extracts the signable text from a binary pdf document.

The signable text is the text that will be signed or verified afterwards.

Parameters:
pdf - The pdf document.
Returns:
Returns the extracted text String.
Throws:
PDFDocumentException - Forwarded exception.

extractSignatureHoldersBinary

public static List extractSignatureHoldersBinary(byte[] pdf)
                                          throws PDFDocumentException,
                                                 SignatureTypesException,
                                                 SignatureException
Retrieves the List of SignatureHolders containing the information of all digital signatures of the given document.

If the List of SignatureHolders is empty, the document is not signed anyways.

Parameters:
pdf - The complete pdf document.
Returns:
Returns the List of SignatureHolders.
Throws:
PDFDocumentException
SignatureTypesException
SignatureException

writeIncrementalUpdate

public static IncrementalUpdateInformation writeIncrementalUpdate(byte[] original_document,
                                                                  PdfPTable pdf_table,
                                                                  PositioningInstruction pi,
                                                                  List variable_field_definitions,
                                                                  List all_field_definitions)
                                                           throws PresentableException
Signs a document with the given signature table using the Incremental Update method.

The table containing the signature text will be appended. As specified by the parameters, the signature will be appended to the last page, or a plain new page will be created for the signature to hold.

The table will be completely wrapped by an XObject, which will also be indirectly referenced by the Egiz Dictionary. This will ease the verification process.

An Egiz Dictionary will be added to the new document that contains information about the signature. Basically the size of the original document and the reference of the signature table.

Parameters:
original_document - The original document.
pdf_table - The PdfPTable that contains the signature block.
pi - The PositioningInstruction telling the algorithm where to place the signature block.
Returns:
Returns the new document.
Throws:
PresentableException - Forwarded exception.

createEgizDict

protected static void createEgizDict(PdfStamper stamper,
                                     PdfTemplate table_template,
                                     IncrementalUpdateInformation iui,
                                     List variable_field_definitions,
                                     List all_field_definitions)
                              throws IOException,
                                     SettingNotFoundException
Creates the EGIZ Dictionary and adds it to the document.

Parameters:
stamper - The PdfStamper.
table_template - The Template of the Signature block.
iui - The IncrementalUpdateInformation.
variable_field_definitions - The field definitions.
Throws:
IOException
SettingNotFoundException

typeToBrev

protected static byte[] typeToBrev(String type)
Converts a field name (type) to the corresponding BREV.

Parameters:
type - The field name (type).
Returns:
Returns the corresponding BREV, or BREV_NIL if the type is not recognized.

markByteRanges

public static void markByteRanges(IncrementalUpdateInformation iui)
                           throws PDFDocumentException
Updates the information in the egiz dictionary to reflect the real offsets of the byte ranges.

This replaces the "dummy numbers" in the egiz dictionary with the correct values.

Parameters:
iui - The IncrementalUpdateInformation.
Throws:
PDFDocumentException

replaceCertificate

public static void replaceCertificate(IncrementalUpdateInformation iui)
                               throws PDFDocumentException
Replaces the certificate placeholder with the certificate from the signed Signature Object.

Parameters:
iui - The IncrementalUpdateInformation.
Throws:
PDFDocumentException

replacePlaceholders

public static void replacePlaceholders(IncrementalUpdateInformation iui)
                                throws PDFDocumentException
Replaces the placeholders with values from the signed SignatureObject.

Parameters:
iui - The IncrementalUpdateInformation.
Throws:
PDFDocumentException

calcNumReps

protected static int calcNumReps(List replaces)
Calculates the number of actual String replaces from a given ReplaceInfo list.

This is used to determine the number of actual replaces that has to be carried out. Accordingly to this number, entries in the dictionary are created.

Parameters:
replaces - The ReplaceInfo list.
Returns:
Returns the number of string replaces.

determineReplacesInContentStream

protected static List determineReplacesInContentStream(byte[] pdf,
                                                       int begin,
                                                       int end,
                                                       List field_definitions)
Determines the List of ReplaceInfo objects of replaces in the content stream regarding the given field definitions.

This method collects all variable String fields in a content stream and orders them according to their start offset.

Parameters:
pdf - The PDF.
begin - The start of the content stream.
end - The end of the content stream.
field_definitions - The field definitions that are counceled to find out which and where varaible strings are.
Returns:
Returns the list of ReplaceInfo objects specifying the variable areas.

determineKZ

protected static List determineKZ(byte[] pdf,
                                  int begin,
                                  int end,
                                  List field_definitions)
                           throws SettingNotFoundException
Determines the Kennzeichnug in the content stream.

Parameters:
pdf - The PDF.
begin - The start of the content stream.
end - The end of the content stream.
field_definitions - The field definitions.
Returns:
Returns the List of StringInfo objects representing the KZ field.
Throws:
SettingNotFoundException - F.e.

findIndex

protected static int findIndex(List strings,
                               byte[] caption)
Finds the index of the StringInfo within the StringInfo list that has the given content (caption).

Parameters:
strings - The list of StringInfos.
caption - The text to be matched to the strings.
Returns:
Returns the index of the found string, or -1 if no string matched.

isCaption

protected static boolean isCaption(List strings,
                                   int index,
                                   byte[] caption)

skipStrings

protected static int skipStrings(List strings,
                                 int index,
                                 byte[] caption)

isPlaceholder

protected static boolean isPlaceholder(StringInfo si,
                                       byte placeholder)
Tells, if the given StringInfo contains only placeholder characters.

Parameters:
si - The StringInfo.
placeholder - The placeholder character.
Returns:
Returns true, if the string contains only the given placeholder characters, false otherwise.

startsWithCaption

protected static boolean startsWithCaption(StringInfo si,
                                           List field_definitions)

findFirstNotPlaceholder

protected static int findFirstNotPlaceholder(List strings,
                                             int start)
Finds the first string after and at the given index not being a placeholder string.

Parameters:
strings - The list of StringInfos.
start - The index where to start the search.
Returns:
Returns the index of the first not placeholder string, or strings.size() if no more non placeholder strings could be found.

restorePlaceholder

public static void restorePlaceholder(byte[] pdf,
                                      StringInfo si,
                                      byte placeholder)
Restores the given String to its placeholder.

Parameters:
pdf - The PDF.
si - The string.
placeholder - The placeholder the string should be filled with.

reconstructReplaces

public static List reconstructReplaces(byte[] pdf,
                                       byte[][] brevs,
                                       StringInfo[] sis,
                                       byte[][] encodings)
                                throws PDFDocumentException
Reconstructs the replaces from the PDF and forms suitable value strings.

Parameters:
pdf - The PDF.
brevs - The brevs.
sis - The StringInfo objects of the strings.
Returns:
Returns the List of ReplaceInfo objects containing the restored values.
Throws:
PDFDocumentException

readNumber

public static int readNumber(byte[] pdf,
                             int start_index,
                             int num_digits)
Reads an unsigned integer number.

Parameters:
pdf - The PDF.
start_index - The start index of the number.
num_digits - The number of digits.
Returns:
Returns the read number.

replaceNumber

public static void replaceNumber(byte[] pdf,
                                 int start_index,
                                 int number,
                                 int num_digits)
Replaces a number by the new value.

Parameters:
pdf - The PDF.
start_index - The start index of the number.
number - The new number.
num_digits - The number of digits.

main

public static void main(String[] args)
                 throws IOException
For debugging purposes.

Parameters:
args -
Throws:
IOException


Copyright © 2006-2007 EGIZ - E-Government Innovationszentrum. All Rights Reserved.