aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java
diff options
context:
space:
mode:
authornetconomy <netconomy@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2007-08-17 06:10:56 +0000
committernetconomy <netconomy@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2007-08-17 06:10:56 +0000
commit3d982813b34f6f230baf4a467cdc37ec92a77595 (patch)
tree85319d39cee2ded1bb7a2b2dd9e8ea37e3778248 /src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java
parent07f6c8f33b2d700276fe6ec6339ff836c8710131 (diff)
downloadpdf-as-3-3d982813b34f6f230baf4a467cdc37ec92a77595.tar.gz
pdf-as-3-3d982813b34f6f230baf4a467cdc37ec92a77595.tar.bz2
pdf-as-3-3d982813b34f6f230baf4a467cdc37ec92a77595.zip
Performance
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@167 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java')
-rw-r--r--src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java463
1 files changed, 230 insertions, 233 deletions
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java
index fb93fbd..d253393 100644
--- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java
+++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java
@@ -24,6 +24,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -35,17 +36,16 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
+import at.gv.egiz.pdfas.exceptions.ErrorCode;
+import at.gv.egiz.pdfas.exceptions.pdf.KZSettingNotFoundException;
+import at.gv.egiz.pdfas.framework.input.PdfDataSource;
+import at.gv.egiz.pdfas.framework.output.DataSink;
import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
import at.knowcenter.wag.egov.egiz.exceptions.PlaceholderException;
import at.knowcenter.wag.egov.egiz.exceptions.PresentableException;
import at.knowcenter.wag.egov.egiz.exceptions.SettingNotFoundException;
-import at.knowcenter.wag.egov.egiz.exceptions.SignatureException;
-import at.knowcenter.wag.egov.egiz.exceptions.SignatureTypesException;
-import at.knowcenter.wag.egov.egiz.sig.SignatureBlock;
import at.knowcenter.wag.egov.egiz.sig.SignatureFieldDefinition;
-import at.knowcenter.wag.egov.egiz.sig.SignatureObject;
import at.knowcenter.wag.egov.egiz.sig.SignatureTypes;
-import at.knowcenter.wag.egov.egiz.sig.X509Cert;
import at.knowcenter.wag.egov.egiz.tools.CodingHelper;
import at.knowcenter.wag.exactparser.ByteArrayUtils;
@@ -288,8 +288,7 @@ public abstract class BinarySignature
* @return Returns the indirect reference of the Egiz Dictionary, or null, if
* none exists.
*/
- public static PdfIndirectReference getEgizDictIndRefFromReader(
- PdfReader reader)
+ public static PdfIndirectReference getEgizDictIndRefFromReader(PdfReader reader)
{
PdfDictionary catalog = reader.getCatalog();
PdfIndirectReference dict_ir = (PdfIndirectReference) catalog.get(EGIZ_DICT_NAME);
@@ -369,12 +368,11 @@ public abstract class BinarySignature
* The original document size.
* @return Returns the signable text.
*/
- public static String retrieveSignableTextFromData(final byte[] data,
- final int ods)
+ public static String retrieveSignableTextFromData(final byte[] data, final int ods)
{
// byte[] digest = buildDigest(data, ods);
String raw_text = CodingHelper.encodeBase64(data);// digest); // data);
-
+
return raw_text;
}
@@ -387,8 +385,7 @@ public abstract class BinarySignature
* The byte ranges.
* @return Returns the filled text.
*/
- public static byte[] prepareDataToSign(final byte[] data,
- final List byte_ranges)
+ public static byte[] prepareDataToSign(final byte[] data, final List byte_ranges)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Iterator it = byte_ranges.iterator();
@@ -411,174 +408,176 @@ public abstract class BinarySignature
return data_to_sign;
}
- /**
- * Extracts the binary 'text' of a document.
- *
- * <p>
- * 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.
- * </p>
- * <p>
- * 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.
- * </p>
- * <p>
- * This function is intented for being used instead of the "text extraction"
- * mechanism used in the plain text Egiz project.
- * </p>
- *
- * @param doc
- * The file.
- * @return Returns the binary text of the document.
- * @throws PDFDocumentException
- * Forwarded exception.
- */
- public static String extractTextBinary(File doc) throws PDFDocumentException
- {
- try
- {
- FileInputStream fis = new FileInputStream(doc);
- return extractTextBinary(fis);
- }
- catch (FileNotFoundException e)
- {
- throw new PDFDocumentException(202, e);
- }
- }
-
- /**
- * Extracts the text binary.
- *
- * @param is
- * @return Returns the binary text.
- * @throws PDFDocumentException
- */
- public static String extractTextBinary(InputStream is) throws PDFDocumentException
- {
- try
- {
- // for some stupid reason this produces a read error if the is comes from
- // a
- // multipart servlet form..???
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int i = -1;
- int acc = 0;
- byte[] b = new byte[1000];
- while ((i = is.read(b)) > 0)
- {
- acc += i;
- System.out.print(" " + i);
- baos.write(b, 0, i);
- }
- System.out.println("acc = " + acc);
- byte[] pdf = baos.toByteArray();
-
- return extractTextBinary(pdf);
- }
- catch (IOException e)
- {
- throw new PDFDocumentException(202, e);
- }
- }
-
- /**
- * Extracts the signable text from a binary pdf document.
- *
- * <p>
- * The signable text is the text that will be signed or verified afterwards.
- * </p>
- *
- * @param pdf
- * The pdf document.
- * @return Returns the extracted text String.
- * @throws PDFDocumentException
- * Forwarded exception.
- */
- public static String extractTextBinary(final byte[] pdf) throws PDFDocumentException
- {
- try
- {
- PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
- PdfDictionary egiz_dict = getEgizDictFromReader(reader);
- if (egiz_dict == null)
- {
- System.out.println("NO Egiz Dict found - whole doc = original doc");
-
- int ods = pdf.length;
- return retrieveSignableTextFromData(pdf, ods);
- }
-
- String sig_text = extractSignatureTextOnly(egiz_dict);
-
- int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict);
-
- String raw_text = retrieveSignableTextFromData(pdf, ods);
- raw_text += "\n";
- raw_text += sig_text;
-
- return raw_text;
- }
- catch (IOException e)
- {
- throw new PDFDocumentException(202, e);
- }
- }
-
- /**
- * Retrieves the List of SignatureHolders containing the information of all
- * digital signatures of the given document.
- *
- * <p>
- * If the List of SignatureHolders is empty, the document is not signed
- * anyways.
- * </p>
- *
- * @param pdf
- * The complete pdf document.
- * @return Returns the List of SignatureHolders.
- * @throws PDFDocumentException
- * @throws SignatureTypesException
- * @throws SignatureException
- */
- public static List extractSignatureHoldersBinary(final byte[] pdf) throws PDFDocumentException, SignatureTypesException, SignatureException
- {
- try
- {
- PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
- List chain = getEgizDictChainFromReader(reader);
-
- List signatures = new ArrayList();
- Iterator it = chain.iterator();
- while (it.hasNext())
- {
- PdfDictionary dict = (PdfDictionary) it.next();
-
- int ods = getOriginalDocumentSizeFromEgizDict(dict);
- String signature_text = extractSignatureTextOnly(dict);
-
- SignatureTypes sig_types = SignatureTypes.getInstance();
- List types = sig_types.getSignatureTypeDefinitions();
- SignatureBlock sig_block = new SignatureBlock(types);
- boolean could_separate = sig_block.separateBlockFromRawText(signature_text, false);
-
- if (could_separate)
- {
- SignatureObject sig_object = sig_block.getSignatureObject();
-
- SignatureHolder holder = new BinarySignatureHolder(pdf, ods, sig_object);
- signatures.add(holder);
- }
- }
-
- return signatures;
- }
- catch (IOException e)
- {
- throw new PDFDocumentException(201, e);
- }
- }
+ // TODO old code - remove
+ // /**
+ // * Extracts the binary 'text' of a document.
+ // *
+ // * <p>
+ // * 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.
+ // * </p>
+ // * <p>
+ // * 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.
+ // * </p>
+ // * <p>
+ // * This function is intented for being used instead of the "text extraction"
+ // * mechanism used in the plain text Egiz project.
+ // * </p>
+ // *
+ // * @param doc
+ // * The file.
+ // * @return Returns the binary text of the document.
+ // * @throws PDFDocumentException
+ // * Forwarded exception.
+ // */
+ // public static String extractTextBinary(File doc) throws PDFDocumentException
+ // {
+ // try
+ // {
+ // FileInputStream fis = new FileInputStream(doc);
+ // return extractTextBinary(fis);
+ // }
+ // catch (FileNotFoundException e)
+ // {
+ // throw new PDFDocumentException(202, e);
+ // }
+ // }
+ //
+ // /**
+ // * Extracts the text binary.
+ // *
+ // * @param is
+ // * @return Returns the binary text.
+ // * @throws PDFDocumentException
+ // */
+ // public static String extractTextBinary(InputStream is) throws PDFDocumentException
+ // {
+ // try
+ // {
+ // // for some stupid reason this produces a read error if the is comes from
+ // // a
+ // // multipart servlet form..???
+ // ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // int i = -1;
+ // int acc = 0;
+ // byte[] b = new byte[1000];
+ // while ((i = is.read(b)) > 0)
+ // {
+ // acc += i;
+ // System.out.print(" " + i);
+ // baos.write(b, 0, i);
+ // }
+ // System.out.println("acc = " + acc);
+ // byte[] pdf = baos.toByteArray();
+ //
+ // return extractTextBinary(pdf);
+ // }
+ // catch (IOException e)
+ // {
+ // throw new PDFDocumentException(202, e);
+ // }
+ // }
+ //
+ // /**
+ // * Extracts the signable text from a binary pdf document.
+ // *
+ // * <p>
+ // * The signable text is the text that will be signed or verified afterwards.
+ // * </p>
+ // *
+ // * @param pdf
+ // * The pdf document.
+ // * @return Returns the extracted text String.
+ // * @throws PDFDocumentException
+ // * Forwarded exception.
+ // */
+ // public static String extractTextBinary(final byte[] pdf) throws PDFDocumentException
+ // {
+ // try
+ // {
+ // PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
+ // PdfDictionary egiz_dict = getEgizDictFromReader(reader);
+ // if (egiz_dict == null)
+ // {
+ // System.out.println("NO Egiz Dict found - whole doc = original doc");
+ //
+ // int ods = pdf.length;
+ // return retrieveSignableTextFromData(pdf, ods);
+ // }
+ //
+ // String sig_text = extractSignatureTextOnly(egiz_dict);
+ //
+ // int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict);
+ //
+ // String raw_text = retrieveSignableTextFromData(pdf, ods);
+ // raw_text += "\n";
+ // raw_text += sig_text;
+ //
+ // return raw_text;
+ // }
+ // catch (IOException e)
+ // {
+ // throw new PDFDocumentException(202, e);
+ // }
+ // }
+
+ // TODO obsolete code - remove
+ // /**
+ // * Retrieves the List of SignatureHolders containing the information of all
+ // * digital signatures of the given document.
+ // *
+ // * <p>
+ // * If the List of SignatureHolders is empty, the document is not signed
+ // * anyways.
+ // * </p>
+ // *
+ // * @param pdf
+ // * The complete pdf document.
+ // * @return Returns the List of SignatureHolders.
+ // * @throws PDFDocumentException
+ // * @throws SignatureTypesException
+ // * @throws SignatureException
+ // */
+ // public static List extractSignatureHoldersBinary(final byte[] pdf) throws PDFDocumentException, SignatureTypesException, SignatureException
+ // {
+ // try
+ // {
+ // PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
+ // List chain = getEgizDictChainFromReader(reader);
+ //
+ // List signatures = new ArrayList();
+ // Iterator it = chain.iterator();
+ // while (it.hasNext())
+ // {
+ // PdfDictionary dict = (PdfDictionary) it.next();
+ //
+ // int ods = getOriginalDocumentSizeFromEgizDict(dict);
+ // String signature_text = extractSignatureTextOnly(dict);
+ //
+ // SignatureTypes sig_types = SignatureTypes.getInstance();
+ // List types = sig_types.getSignatureTypeDefinitions();
+ // SignatureBlock sig_block = new SignatureBlock(types);
+ // boolean could_separate = sig_block.separateBlockFromRawText(signature_text, false);
+ //
+ // if (could_separate)
+ // {
+ // SignatureObject sig_object = sig_block.getSignatureObject();
+ //
+ // SignatureHolder holder = new BinarySignatureHolder(pdf, ods, sig_object);
+ // signatures.add(holder);
+ // }
+ // }
+ //
+ // return signatures;
+ // }
+ // catch (IOException e)
+ // {
+ // throw new PDFDocumentException(201, e);
+ // }
+ // }
// /**
// * Signs a document with the given signature table using the Incremental
@@ -677,22 +676,26 @@ public abstract class BinarySignature
* @throws PresentableException
* Forwarded exception.
*/
- public static IncrementalUpdateInformation writeIncrementalUpdate(
- byte[] original_document, PdfPTable pdf_table, PositioningInstruction pi,
+ public static IncrementalUpdateInformation writeIncrementalUpdate(PdfDataSource original_document, DataSink written_pdf, PdfPTable pdf_table, PositioningInstruction pi,
List variable_field_definitions, List all_field_definitions) throws PresentableException
{
try
{
IncrementalUpdateInformation iui = new IncrementalUpdateInformation();
iui.original_document = original_document;
- iui.start_index = original_document.length;
+ iui.start_index = original_document.getLength();
Document.compress = false;
// System.out.println("wprinz: STAMPING PDF");
- PdfReader reader = new PdfReader(original_document);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ InputStream is = original_document.createInputStream();
+ PdfReader reader = new PdfReader(is);
+ is.close();
+
+ OutputStream baos = written_pdf.createOutputStream("application/pdf");
+ // ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
// IMPORTANT: append the new content to the original document using
// incremental updated
// The stamper allows this by setting append = true
@@ -786,19 +789,21 @@ public abstract class BinarySignature
stamper.close();
// System.out.println("wprinz: STAMPING FINISHED");
- iui.signed_pdf = baos.toByteArray();
+ // just to make sure the stream is really closed
+ baos.close();
+ // iui.signed_pdf = baos.toByteArray();
return iui;
}
catch (IOException e)
{
e.printStackTrace();
- throw new PresentableException(e);
+ throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF, e);
}
catch (DocumentException e)
{
e.printStackTrace();
- throw new PresentableException(e);
+ throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF, e);
}
}
@@ -816,9 +821,8 @@ public abstract class BinarySignature
* @throws IOException
* @throws SettingNotFoundException
*/
- protected static void createEgizDict(PdfStamper stamper,
- PdfTemplate table_template, IncrementalUpdateInformation iui,
- List variable_field_definitions, List all_field_definitions) throws IOException, SettingNotFoundException
+ protected static void createEgizDict(PdfStamper stamper, PdfTemplate table_template, IncrementalUpdateInformation iui, List variable_field_definitions, List all_field_definitions)
+ throws IOException, SettingNotFoundException
{
// iui.temp_ir = table_template.getIndirectReference();
iui.temp_ir_number = table_template.getIndirectReference().getNumber();
@@ -1302,8 +1306,7 @@ public abstract class BinarySignature
* @return Returns the list of ReplaceInfo objects specifying the variable
* areas.
*/
- protected static List determineReplacesInContentStream(final byte[] pdf,
- int begin, int end, List field_definitions)
+ protected static List determineReplacesInContentStream(final byte[] pdf, int begin, int end, List field_definitions)
{
List replaces = new ArrayList();
try
@@ -1343,8 +1346,7 @@ public abstract class BinarySignature
}
// sort replaces
- Collections.sort(replaces, new Comparator()
- {
+ Collections.sort(replaces, new Comparator() {
public int compare(Object arg0, Object arg1)
{
ReplaceInfo ri0 = (ReplaceInfo) arg0;
@@ -1373,8 +1375,7 @@ public abstract class BinarySignature
* @throws SettingNotFoundException
* F.e.
*/
- protected static List determineKZ(final byte[] pdf, int begin, int end,
- List field_definitions) throws SettingNotFoundException
+ protected static List determineKZ(final byte[] pdf, int begin, int end, List field_definitions) throws SettingNotFoundException
{
try
{
@@ -1414,7 +1415,7 @@ public abstract class BinarySignature
{
e.printStackTrace();
}
- throw new SettingNotFoundException(102, "Field " + SignatureTypes.SIG_KZ + " not found.");
+ throw new KZSettingNotFoundException("Field " + SignatureTypes.SIG_KZ + " not found.");
}
/**
@@ -1508,8 +1509,7 @@ public abstract class BinarySignature
return true;
}
- protected static boolean startsWithCaption(StringInfo si,
- List field_definitions)
+ protected static boolean startsWithCaption(StringInfo si, List field_definitions)
{
try
{
@@ -1568,8 +1568,7 @@ public abstract class BinarySignature
* @param placeholder
* The placeholder the string should be filled with.
*/
- public static void restorePlaceholder(final byte[] pdf, StringInfo si,
- final byte placeholder)
+ public static void restorePlaceholder(final byte[] pdf, StringInfo si, final byte placeholder)
{
byte[] ph = new byte[si.string_length];
for (int i = 0; i < ph.length; i++)
@@ -1592,8 +1591,7 @@ public abstract class BinarySignature
* values.
* @throws PDFDocumentException
*/
- public static List reconstructReplaces(final byte[] pdf, byte[][] brevs,
- StringInfo[] sis, byte[][] encodings) throws PDFDocumentException
+ public static List reconstructReplaces(final byte[] pdf, byte[][] brevs, StringInfo[] sis, byte[][] encodings) throws PDFDocumentException
{
try
{
@@ -1654,8 +1652,7 @@ public abstract class BinarySignature
* The number of digits.
* @return Returns the read number.
*/
- public static int readNumber(final byte[] pdf, final int start_index,
- final int num_digits)
+ public static int readNumber(final byte[] pdf, final int start_index, final int num_digits)
{
try
{
@@ -1685,8 +1682,7 @@ public abstract class BinarySignature
* @param num_digits
* The number of digits.
*/
- public static void replaceNumber(final byte[] pdf, final int start_index,
- final int number, final int num_digits)
+ public static void replaceNumber(final byte[] pdf, final int start_index, final int number, final int num_digits)
{
try
{
@@ -1717,30 +1713,31 @@ public abstract class BinarySignature
}
}
- /**
- * For debugging purposes.
- *
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException
- {
- File signed_doc = new File("C:/wprinz/temp.pdf");
-
- PdfReader reader = new PdfReader(new FileInputStream(signed_doc));
- PdfDictionary egiz_dict = getEgizDictFromReader(reader);
- if (egiz_dict == null)
- {
- System.out.println("NO Egiz Dict");
- return;
- }
-
- String sig_text = extractSignatureTextOnly(egiz_dict);
- System.out.println("Sig Text:");
- System.out.println(sig_text);
-
- int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict);
- System.out.println("Original Document Size = " + ods);
- }
+ // TODO old code - remove
+ // /**
+ // * For debugging purposes.
+ // *
+ // * @param args
+ // * @throws IOException
+ // */
+ // public static void main(String[] args) throws IOException
+ // {
+ // File signed_doc = new File("C:/wprinz/temp.pdf");
+ //
+ // PdfReader reader = new PdfReader(new FileInputStream(signed_doc));
+ // PdfDictionary egiz_dict = getEgizDictFromReader(reader);
+ // if (egiz_dict == null)
+ // {
+ // System.out.println("NO Egiz Dict");
+ // return;
+ // }
+ //
+ // String sig_text = extractSignatureTextOnly(egiz_dict);
+ // System.out.println("Sig Text:");
+ // System.out.println(sig_text);
+ //
+ // int ods = getOriginalDocumentSizeFromEgizDict(egiz_dict);
+ // System.out.println("Original Document Size = " + ods);
+ // }
}