From 725749c85e2c4a0dca235e7b89a5c3b2752e5d54 Mon Sep 17 00:00:00 2001 From: tknall Date: Tue, 29 Jan 2013 12:10:47 +0000 Subject: Added logging for BinarySignator. Compiler warnings (e.g. non mappable characters within the source code) are considered as errors. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/pdf-as/trunk@930 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../impl/signator/binary/BinarySignator_1_0_0.java | 276 +++++++-------------- pom.xml | 2 +- 2 files changed, 87 insertions(+), 191 deletions(-) diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java index 7f18f0a..0af6538 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/impl/signator/binary/BinarySignator_1_0_0.java @@ -40,6 +40,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -84,15 +85,12 @@ import com.lowagie.text.pdf.PdfPTable; * Signs the document binary. * *

- * In prepareSign, an Incremental Update is created that contains the Signature - * block and the egiz dictionary. For formatting the layout, variable values are - * filled with placeholders. After the layout has been fixed, all variable - * fields (all holes in the byte ranges) are replaced with 0. This document is - * then base64 encoded and signed. + * In prepareSign, an Incremental Update is created that contains the Signature block and the egiz dictionary. For + * formatting the layout, variable values are filled with placeholders. After the layout has been fixed, all variable + * fields (all holes in the byte ranges) are replaced with 0. This document is then base64 encoded and signed. *

*

- * In finishSign, the variable fields (values, /Cert) are replaced with the - * values according to the encoding. + * In finishSign, the variable fields (values, /Cert) are replaced with the values according to the encoding. *

* * @author wprinz @@ -111,8 +109,8 @@ public class BinarySignator_1_0_0 implements Signator { /** * The Pdf-AS ID of this Signator. */ - public static final PdfASID MY_ID = new PdfASID(SignatorFactory.VENDOR, - SignatorFactory.TYPE_BINARY, SignatorFactory.VERSION_1_0_0); + public static final PdfASID MY_ID = new PdfASID(SignatorFactory.VENDOR, SignatorFactory.TYPE_BINARY, + SignatorFactory.VERSION_1_0_0); private Normalizer normalizer; @@ -136,18 +134,15 @@ public class BinarySignator_1_0_0 implements Signator { } /** - * @see at.gv.egiz.pdfas.framework.signator.Signator#prepareSign(PdfDataSource, - * String, TablePos, TimeStamper) + * @see at.gv.egiz.pdfas.framework.signator.Signator#prepareSign(PdfDataSource, String, TablePos, TimeStamper) */ - public SignatorInformation prepareSign(PdfDataSource pdfDataSource, - String profile, TablePos pos, TimeStamper timeStamper) - throws SignatorException { + public SignatorInformation prepareSign(PdfDataSource pdfDataSource, String profile, TablePos pos, + TimeStamper timeStamper) throws SignatorException { try { // dferbas: has to be true everytime boolean has_SIG_ID = true; - String baikStr = SettingsReader.getInstance().getSetting( - "sig_obj." + profile + ".key." + SIG_BAIK_ENABLED, + String baikStr = SettingsReader.getInstance().getSetting("sig_obj." + profile + ".key." + SIG_BAIK_ENABLED, "default." + SIG_BAIK_ENABLED, "false"); boolean baikEnabled = "true".equalsIgnoreCase(baikStr); @@ -155,34 +150,25 @@ public class BinarySignator_1_0_0 implements Signator { log.debug("BAIK enabled signature"); } - SignatureObject signature_object = PdfAS - .createSignatureObjectFromType(profile); - signature_object.fillValues( - (char) BinarySignature.LAYOUT_PLACEHOLDER, has_SIG_ID, - baikEnabled); + SignatureObject signature_object = PdfAS.createSignatureObjectFromType(profile); + signature_object.fillValues((char) BinarySignature.LAYOUT_PLACEHOLDER, has_SIG_ID, baikEnabled); signature_object.setKZ(getMyId()); - PdfPTable pdf_table = PdfAS - .createPdfPTableFromSignatureObject(signature_object); + PdfPTable pdf_table = PdfAS.createPdfPTableFromSignatureObject(signature_object); - PositioningInstruction pi = PdfAS.determineTablePositioning(pos, - profile, pdfDataSource, pdf_table); + PositioningInstruction pi = PdfAS.determineTablePositioning(pos, profile, pdfDataSource, pdf_table); - List all_field_definitions = signature_object - .getSignatureTypeDefinition().getFieldDefinitions(); + List all_field_definitions = signature_object.getSignatureTypeDefinition().getFieldDefinitions(); List variable_field_definitions = new ArrayList(); for (int i = 0; i < all_field_definitions.size(); i++) { - SignatureFieldDefinition sfd = (SignatureFieldDefinition) all_field_definitions - .get(i); + SignatureFieldDefinition sfd = (SignatureFieldDefinition) all_field_definitions.get(i); if (sfd.placeholder_length > 0) { - if (sfd.field_name.equals(SignatureTypes.SIG_ID) - && has_SIG_ID == false) { + if (sfd.field_name.equals(SignatureTypes.SIG_ID) && has_SIG_ID == false) { continue; } - if (sfd.field_name.equals(SignatureTypes.SIG_ALG) - && baikEnabled == false) { + if (sfd.field_name.equals(SignatureTypes.SIG_ALG) && baikEnabled == false) { continue; } @@ -190,28 +176,24 @@ public class BinarySignator_1_0_0 implements Signator { } } - List all_invisible_field_definitions = signature_object - .getSignatureTypeDefinition() + List all_invisible_field_definitions = signature_object.getSignatureTypeDefinition() .getInvisibleFieldDefinitions(); List invisible_field_definitions = new ArrayList(); boolean isKZInvisible = false; String invKZString = null; for (int i = 0; i < all_invisible_field_definitions.size(); i++) { - SignatureFieldDefinition sfd = (SignatureFieldDefinition) all_invisible_field_definitions - .get(i); + SignatureFieldDefinition sfd = (SignatureFieldDefinition) all_invisible_field_definitions.get(i); if (sfd.field_name.equals(SignatureTypes.SIG_KZ)) { isKZInvisible = true; invKZString = signature_object.getKZ().toString(); continue; } - if (sfd.field_name.equals(SignatureTypes.SIG_ID) - && has_SIG_ID == false) { + if (sfd.field_name.equals(SignatureTypes.SIG_ID) && has_SIG_ID == false) { continue; } - if (sfd.field_name.equals(SignatureTypes.SIG_ALG) - && baikEnabled == false) { + if (sfd.field_name.equals(SignatureTypes.SIG_ALG) && baikEnabled == false) { continue; } invisible_field_definitions.add(sfd); @@ -222,8 +204,7 @@ public class BinarySignator_1_0_0 implements Signator { // on a new page, prevent pdf-as to do that, because why should make // a new page just for an invisible block // added by rpiazzi - if (signature_object.getSignatureTypeDefinition() - .getInvisibleFieldDefinitions().size() == SignatureTypes.REQUIRED_SIG_KEYS.length) { + if (signature_object.getSignatureTypeDefinition().getInvisibleFieldDefinitions().size() == SignatureTypes.REQUIRED_SIG_KEYS.length) { if (pi.isMakeNewPage()) { int pageNumber = pi.getPage(); pi = new PositioningInstruction(false, pageNumber - 1, 0, 0); @@ -231,26 +212,19 @@ public class BinarySignator_1_0_0 implements Signator { } // end added - IncrementalUpdateInformation iui = IncrementalUpdateHelper - .writeIncrementalUpdate(pdfDataSource, pdf_table, profile, - pi, variable_field_definitions, - all_field_definitions, invisible_field_definitions, - invKZString, timeStamper, null, signature_object); + IncrementalUpdateInformation iui = IncrementalUpdateHelper.writeIncrementalUpdate(pdfDataSource, pdf_table, + profile, pi, variable_field_definitions, all_field_definitions, invisible_field_definitions, + invKZString, timeStamper, null, signature_object); iui.invisible_field_definitions = invisible_field_definitions; iui.invisibleKZString = invKZString; - String temp_string = iui.temp_ir_number - + " " + iui.temp_ir_generation + " obj"; //$NON-NLS-1$//$NON-NLS-2$ - byte[] temp_bytes = ArrayUtils.add( - temp_string.getBytes("US-ASCII"), 0, (byte) 0x0A); - int temp_start = ByteArrayUtils.lastIndexOf(iui.signed_pdf, - temp_bytes); - byte[] stream_bytes = new byte[] { '>', '>', 's', 't', 'r', 'e', - 'a', 'm', 0x0A }; - int stream_start = ByteArrayUtils.indexOf(iui.signed_pdf, - temp_start, stream_bytes); + String temp_string = iui.temp_ir_number + " " + iui.temp_ir_generation + " obj"; //$NON-NLS-1$//$NON-NLS-2$ + byte[] temp_bytes = ArrayUtils.add(temp_string.getBytes("US-ASCII"), 0, (byte) 0x0A); + int temp_start = ByteArrayUtils.lastIndexOf(iui.signed_pdf, temp_bytes); + byte[] stream_bytes = new byte[] { '>', '>', 's', 't', 'r', 'e', 'a', 'm', 0x0A }; + int stream_start = ByteArrayUtils.indexOf(iui.signed_pdf, temp_start, stream_bytes); iui.content_stream_start = stream_start + stream_bytes.length; // update the stream indices @@ -276,8 +250,7 @@ public class BinarySignator_1_0_0 implements Signator { BinarySignature.markByteRanges(iui); // byte [] old_signed_pdf = iui.signed_pdf; - iui.signed_pdf = BinarySignature.prepareDataToSign(iui.signed_pdf, - iui.byte_ranges); + iui.signed_pdf = BinarySignature.prepareDataToSign(iui.signed_pdf, iui.byte_ranges); BinarySignatorInformation bsi = compressIUI(iui); return bsi; @@ -295,8 +268,7 @@ public class BinarySignator_1_0_0 implements Signator { * @see at.gv.egiz.pdfas.framework.signator.Signator#finishSign(at.gv.egiz.pdfas.framework.signator.SignatorInformation, * at.gv.egiz.pdfas.framework.output.DataSink) */ - public void finishSign(SignatorInformation signatorInformation, - DataSink dataSink) throws SignatorException { + public void finishSign(SignatorInformation signatorInformation, DataSink dataSink) throws SignatorException { try { IncrementalUpdateInformation iui = uncompressIUI((BinarySignatorInformation) signatorInformation); @@ -308,9 +280,8 @@ public class BinarySignator_1_0_0 implements Signator { // This is needed so that certificates are stored try { iui.signed_signature_object.kz = getMyId().toString(); - SignatureObjectHelper - .convertSignSignatureObjectToSignatureObject( - iui.signed_signature_object, iui.signProfile); + SignatureObjectHelper.convertSignSignatureObjectToSignatureObject(iui.signed_signature_object, + iui.signProfile); } catch (PresentableException e) { throw new SignatorException(e); } @@ -321,15 +292,13 @@ public class BinarySignator_1_0_0 implements Signator { // dferbas: alternative sign attrib creation // PdfReader reader = new PdfReader(iui.signed_pdf); // - // OutputStream os = - // dataSink.createOutputStream(PdfAS.PDF_MIME_TYPE); + // OutputStream os = dataSink.createOutputStream(PdfAS.PDF_MIME_TYPE); // // try { - // PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', - // null, true); + // PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true); // - // BinarySignature.createAdobeSigAttrib(stamper, - // signatorInformation, signatorInformation.getActualTablePos()); + // BinarySignature.createAdobeSigAttrib(stamper, signatorInformation, + // signatorInformation.getActualTablePos()); // // } catch (DocumentException e) { // log.error("pdf error", e); @@ -347,8 +316,8 @@ public class BinarySignator_1_0_0 implements Signator { } /** - * Reads the signature values from the signed signature object and fills the - * corresponding value in the Replaces array. + * Reads the signature values from the signed signature object and fills the corresponding value in the Replaces + * array. * * @param iui * The IncrementalUpdateInformation. @@ -360,15 +329,13 @@ public class BinarySignator_1_0_0 implements Signator { ognlCtx.put("iui", iui); ognlCtx.put("sso", iui.signed_signature_object); ognlCtx.put("issuer", iui.signed_signature_object.getIssuerDNMap()); - ognlCtx.put("subject", - iui.signed_signature_object.getSubjectDNMap()); + ognlCtx.put("subject", iui.signed_signature_object.getSubjectDNMap()); OgnlUtil ognl = new OgnlUtil(ognlCtx); OverridePropertyHolder.setOgnlUtil(ognl); while (it.hasNext()) { ReplaceInfo ri = (ReplaceInfo) it.next(); - String overrideVal = OverridePropertyHolder - .getProperty(ri.sfd.field_name); + String overrideVal = OverridePropertyHolder.getProperty(ri.sfd.field_name); if (overrideVal != null) { ri.sfd.value = overrideVal; ri.value = overrideVal; @@ -376,83 +343,20 @@ public class BinarySignator_1_0_0 implements Signator { // evaluate expression String res = ognl.compileMessage(ri.sfd.value); ri.value = this.normalizer.normalize(res, true); - // Workaround added by rpiazzi // a-trust wrongly encodes since July 2011, therefore some // special characters (e.g. Umlaute) have // to replaced by their right character - /*if ((ri.value.contains("ü") - || ri.value.contains("ä") - || ri.value.contains("ö") - || ri.value.contains("á") - || ri.value.contains("ß") - || ri.value.contains("Ö") - || ri.value.contains("à") - || ri.value.contains("é") - || ri.value.contains("ú") || ri.value - .contains("ç")) - && (ri.sfd.field_name - .equals(SignatureTypes.SIG_SUBJECT))) { - if (ri.value.contains("ü")) { - ri.sfd.value = ri.sfd.value.replace("ü", "�"); - ri.value = ri.value.replace("ü", "�"); - } - if (ri.value.contains("ä")) { - ri.sfd.value = ri.sfd.value.replace("ä", "�"); - ri.value = ri.value.replace("ä", "�"); - } - if (ri.value.contains("ö")) { - ri.sfd.value = ri.sfd.value.replace("ö", "�"); - ri.value = ri.value.replace("ö", "�"); - } - if (ri.value.contains("á")) { - ri.sfd.value = ri.sfd.value.replace("á", "�"); - ri.value = ri.value.replace("á", "�"); - } - if (ri.value.contains("ß")) { - ri.sfd.value = ri.sfd.value.replace("ß", "�"); - ri.value = ri.value.replace("ß", "�"); - } - if (ri.value.contains("Ö")) { - ri.sfd.value = ri.sfd.value.replace("Ö", "�"); - ri.value = ri.value.replace("Ö", "�"); - } - if (ri.value.contains("à")) { - ri.sfd.value = ri.sfd.value.replace("à", "�"); - ri.value = ri.value.replace("à", "�"); - } - if (ri.value.contains("é")) { - ri.sfd.value = ri.sfd.value.replace("é", "�"); - ri.value = ri.value.replace("é", "�"); - } - if (ri.value.contains("ú")) { - ri.sfd.value = ri.sfd.value.replace("ú", "�"); - ri.value = ri.value.replace("ú", "�"); - } - if (ri.value.contains("ç")) { - ri.sfd.value = ri.sfd.value.replace("ç", "�"); - ri.value = ri.value.replace("ç", "�"); - } - }*/ - FixHandyAnsiEncoding(ri); - // end added - } else if (overrideVal == null) { - // If SUBJECT is not overridden and and also isn't an - // expression + fixHandyAnsiEncoding(ri); + } else { + // If SUBJECT is not overridden and and also isn't an expression // check whether a set value for subject exists. // In this case take the value from the config file. - // Added by rpiazzi to make a static signator possible - // without having - // to override it any time - if (ri.sfd.field_name.equals(SignatureTypes.SIG_SUBJECT)) { - if (ri.sfd.value.length() != 0) { - ri.value = ri.sfd.value; - } else { - ri.value = iui.signed_signature_object - .retrieveStringValue(ri.sfd.field_name); - } + // Added by rpiazzi to make a static signator possible without + // having to override it any time + if (ri.sfd.field_name.equals(SignatureTypes.SIG_SUBJECT) && StringUtils.isNotEmpty(ri.sfd.value)) { + ri.value = ri.sfd.value; } else { - ri.value = iui.signed_signature_object - .retrieveStringValue(ri.sfd.field_name); + ri.value = iui.signed_signature_object.retrieveStringValue(ri.sfd.field_name); } } } @@ -461,31 +365,30 @@ public class BinarySignator_1_0_0 implements Signator { } } - private void FixHandyAnsiEncoding(ReplaceInfo ri) { - Pattern p = Pattern.compile("&#([0-9]+);"); - Matcher m = p.matcher(ri.value); - - int value = -1; - - while (m.find()) { - value = Integer.parseInt(m.group(1)); - - if (value > 0 && value < 256) { - - log.debug("Replacing Encoding &#" + m.group(1) + ";"); - - byte[] buffer = new byte[1]; - - buffer[0] = (byte) value; - - ByteBuffer bb = ByteBuffer.wrap(buffer); - - CharBuffer cb = Charset.forName("CP1252").decode(bb); - - String str = cb.toString(); - - ri.value = ri.value.replace("&#" + m.group(1) + ";", str); + private void fixHandyAnsiEncoding(ReplaceInfo ri) { + try { + boolean fixApplied = false; + Pattern p = Pattern.compile("&#([0-9]+);"); + Matcher m = p.matcher(ri.value); + int value = -1; + while (m.find()) { + value = Integer.parseInt(m.group(1)); + if (value > 0 && value < 256) { + log.debug("Replacing encoding '&#" + value + ";'."); + byte[] buffer = new byte[1]; + buffer[0] = (byte) value; + ByteBuffer bb = ByteBuffer.wrap(buffer); + CharBuffer cb = Charset.forName("Cp1252").decode(bb); + String str = cb.toString(); + ri.value = ri.value.replace("&#" + m.group(1) + ";", str); + fixApplied = true; + } + } + if (fixApplied) { + log.info("ANSI encoding fix applied."); } + } catch (Exception e) { + log.info("Error trying to fix ANSI encoding.", e); } } @@ -508,24 +411,19 @@ public class BinarySignator_1_0_0 implements Signator { // } // catch (UnsupportedEncodingException e) // { - // throw new RuntimeException("Very strange: UTF-8 character encoding - // not + // throw new RuntimeException("Very strange: UTF-8 character encoding not // supported.", e); //$NON-NLS-1$ // } - DataSource ds = new CompoundPdfDataSourceImpl(iui.original_document, - iui.sign_iui_block); - SignatureData signature_data = new SignatureDataImpl(ds, - PdfAS.PDF_MIME_TYPE); + DataSource ds = new CompoundPdfDataSourceImpl(iui.original_document, iui.sign_iui_block); + SignatureData signature_data = new SignatureDataImpl(ds, PdfAS.PDF_MIME_TYPE); return signature_data; } - protected BinarySignatorInformation compressIUI( - IncrementalUpdateInformation iui) { - iui.sign_iui_block = new byte[iui.signed_pdf.length - - iui.original_document.getLength()]; - System.arraycopy(iui.signed_pdf, iui.original_document.getLength(), - iui.sign_iui_block, 0, iui.sign_iui_block.length); + protected BinarySignatorInformation compressIUI(IncrementalUpdateInformation iui) { + iui.sign_iui_block = new byte[iui.signed_pdf.length - iui.original_document.getLength()]; + System.arraycopy(iui.signed_pdf, iui.original_document.getLength(), iui.sign_iui_block, 0, + iui.sign_iui_block.length); iui.signature_data = formSignatureData(iui); @@ -549,8 +447,7 @@ public class BinarySignator_1_0_0 implements Signator { return bsi; } - protected IncrementalUpdateInformation uncompressIUI( - BinarySignatorInformation bsi) { + protected IncrementalUpdateInformation uncompressIUI(BinarySignatorInformation bsi) { IncrementalUpdateInformation iui = new IncrementalUpdateInformation(); iui.original_document = bsi.originalDocument; @@ -574,8 +471,7 @@ public class BinarySignator_1_0_0 implements Signator { } protected void restoreSignedPdf(IncrementalUpdateInformation iui) { - iui.signed_pdf = new byte[iui.original_document.getLength() - + iui.sign_iui_block.length]; + iui.signed_pdf = new byte[iui.original_document.getLength() + iui.sign_iui_block.length]; try { InputStream is = iui.original_document.createInputStream(); @@ -585,8 +481,8 @@ public class BinarySignator_1_0_0 implements Signator { throw new RuntimeException(e); } - System.arraycopy(iui.sign_iui_block, 0, iui.signed_pdf, - iui.original_document.getLength(), iui.sign_iui_block.length); + System.arraycopy(iui.sign_iui_block, 0, iui.signed_pdf, iui.original_document.getLength(), + iui.sign_iui_block.length); } public String getEncoding() { diff --git a/pom.xml b/pom.xml index 867c89d..c591908 100644 --- a/pom.xml +++ b/pom.xml @@ -103,7 +103,7 @@ ${java.compiler.target} - false + true -- cgit v1.2.3