From 40fc290721a9ab0c7a815dc60ea965d615860f21 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Thu, 12 Dec 2013 12:46:38 +0100 Subject: Moved hard coded error strings into common.propreties --- .../resources/resources/messages/common.properties | 18 +++++- .../egiz/pdfas/sigs/pades/PAdESSignerKeystore.java | 13 ++-- .../at/gv/egiz/pdfas/sigs/pades/PAdESVerifier.java | 3 +- .../sigs/pkcs7detached/PKCS7DetachedSigner.java | 14 ++-- .../sigs/pkcs7detached/PKCS7DetachedVerifier.java | 11 +--- .../at/gv/egiz/pdfas/stmp/itext/ITextStamper.java | 75 +++++++++++++--------- .../com/lowagie/text/pdf/ITextStamperAccess.java | 5 +- 7 files changed, 81 insertions(+), 58 deletions(-) diff --git a/pdf-as-common/src/main/resources/resources/messages/common.properties b/pdf-as-common/src/main/resources/resources/messages/common.properties index a2729e21..aac8d173 100644 --- a/pdf-as-common/src/main/resources/resources/messages/common.properties +++ b/pdf-as-common/src/main/resources/resources/messages/common.properties @@ -3,6 +3,22 @@ error.pdf.perm.01=You do not have permission to extract images. #PDF IO Errors error.pdf.io.01=Failed to read original PDF Document into PDDocument. +error.pdf.io.02=Failed to write PDF Document. + +#PDF Stamper Errors +error.pdf.stamp.01=Failed to load true type font. +error.pdf.stamp.02=PDF/A modus requires an embeddable true type font +error.pdf.stamp.03=Table is not defined. +error.pdf.stamp.04=Image file doesn't exist. +error.pdf.stamp.05=Unable to create PDF table. +error.pdf.stamp.06=Failed to create visual signature object. +error.pdf.stamp.07=The provided page is out of range. +error.pdf.stamp.08=Failed to write the visual signature object. #Signature errors -error.pdf.sig.01=Failed to create signature! \ No newline at end of file +error.pdf.sig.01=Failed to create signature! +error.pdf.sig.02=Failed to open keystore! + +#Signature verification errors +error.pdf.verify.01="No signed data available" +error.pdf.verify.02="Verification failed!" \ No newline at end of file diff --git a/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESSignerKeystore.java b/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESSignerKeystore.java index 5ae0ed76..89293fa1 100644 --- a/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESSignerKeystore.java +++ b/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESSignerKeystore.java @@ -53,7 +53,7 @@ public class PAdESSignerKeystore implements IPlainSigner { privKey = (PrivateKey) ks.getKey(alias, keypassword.toCharArray()); cert = new X509Certificate(ks.getCertificate(alias).getEncoded()); } catch (Throwable e) { - throw new PdfAsException("Failed to get KeyStore", e); + throw new PdfAsException("error.pdf.sig.02", e); } } @@ -124,6 +124,7 @@ public class PAdESSignerKeystore implements IPlainSigner { si.addSignerInfo(signer1); InputStream dataIs = si.getInputStream(); byte[] buf = new byte[1024]; + @SuppressWarnings("unused") int r; while ((r = dataIs.read(buf)) > 0) ; // skip data @@ -131,15 +132,15 @@ public class PAdESSignerKeystore implements IPlainSigner { return ci.getEncoded(); } catch (NoSuchAlgorithmException e) { - throw new PdfAsSignatureException("Failed to generate Signature", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } catch (iaik.cms.CMSException e) { - throw new PdfAsSignatureException("Failed to generate Signature", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } catch (IOException e) { - throw new PdfAsSignatureException("Failed to generate Signature", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } catch (CertificateException e) { - throw new PdfAsSignatureException("Failed to generate Signature", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } catch (CodingException e) { - throw new PdfAsSignatureException("Failed to generate Signature", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } } diff --git a/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESVerifier.java b/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESVerifier.java index 6e79a1a0..52a58d56 100644 --- a/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESVerifier.java +++ b/signature-standards/sigs-pades/src/main/java/at/gv/egiz/pdfas/sigs/pades/PAdESVerifier.java @@ -1,6 +1,5 @@ package at.gv.egiz.pdfas.sigs.pades; -import iaik.security.provider.IAIK; import iaik.x509.X509Certificate; import java.util.ArrayList; @@ -174,7 +173,7 @@ public class PAdESVerifier implements IVerifyFilter { resultList.add(result); } } catch (Throwable e) { - e.printStackTrace(); + throw new PdfAsException("error.pdf.verify.02", e); } return resultList; } diff --git a/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedSigner.java b/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedSigner.java index 6411fe67..df792256 100644 --- a/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedSigner.java +++ b/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedSigner.java @@ -14,7 +14,6 @@ import iaik.x509.X509Certificate; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.security.KeyStore; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; @@ -22,8 +21,6 @@ import java.security.cert.Certificate; import java.util.Date; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; -import org.bouncycastle.cms.CMSException; -import org.bouncycastle.cms.CMSProcessable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,7 +48,7 @@ public class PKCS7DetachedSigner implements IPlainSigner { privKey = (PrivateKey) ks.getKey(alias, keypassword.toCharArray()); cert = new X509Certificate(ks.getCertificate(alias).getEncoded()); } catch (Throwable e) { - throw new PdfAsException("Failed to get KeyStore", e); + throw new PdfAsException("error.pdf.sig.02", e); } } @@ -61,6 +58,7 @@ public class PKCS7DetachedSigner implements IPlainSigner { public byte[] sign(byte[] input, int[] byteRange) throws PdfAsException { try { + logger.info("Creating PKCS7 signature."); IssuerAndSerialNumber issuer = new IssuerAndSerialNumber(cert); SignerInfo signer1 = new SignerInfo(issuer, AlgorithmID.sha256, AlgorithmID.ecdsa_With_SHA256, @@ -84,14 +82,14 @@ public class PKCS7DetachedSigner implements IPlainSigner { while ((r = dataIs.read(buf)) > 0) ; // skip data ContentInfo ci = new ContentInfo(si); - + logger.info("PKCS7 signature done."); return ci.getEncoded(); } catch (NoSuchAlgorithmException e) { - throw new PdfAsSignatureException("", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } catch (iaik.cms.CMSException e) { - throw new PdfAsSignatureException("", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } catch (IOException e) { - throw new PdfAsSignatureException("", e); + throw new PdfAsSignatureException("error.pdf.sig.01", e); } } diff --git a/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedVerifier.java b/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedVerifier.java index 3d0aed42..7d5c029a 100644 --- a/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedVerifier.java +++ b/signature-standards/sigs-pkcs7detached/src/main/java/at/gv/egiz/pdfas/sigs/pkcs7detached/PKCS7DetachedVerifier.java @@ -40,16 +40,11 @@ public class PKCS7DetachedVerifier implements IVerifyFilter { SignedData signedData = new SignedData(contentData, new AlgorithmID[] { AlgorithmID.sha256 - }); - - FileOutputStream fos = new FileOutputStream("/tmp/verify.bin"); - fos.write(signatureContent); - fos.close(); - + }); ContentInfo ci = new ContentInfo(new ByteArrayInputStream( signatureContent)); if (!ci.getContentType().equals(ObjectID.cms_signedData)) { - throw new PdfAsException("No Signed DATA"); + throw new PdfAsException("error.pdf.verify.01"); } //SignedData signedData = (SignedData)ci.getContent(); //signedData.setContent(contentData); @@ -98,7 +93,7 @@ public class PKCS7DetachedVerifier implements IVerifyFilter { return result; } catch (Throwable e) { - throw new PdfAsException("Verify failed", e); + throw new PdfAsException("error.pdf.verify.02", e); } } diff --git a/stamper/stmp-itext/src/main/java/at/gv/egiz/pdfas/stmp/itext/ITextStamper.java b/stamper/stmp-itext/src/main/java/at/gv/egiz/pdfas/stmp/itext/ITextStamper.java index b0801a9c..1b026300 100644 --- a/stamper/stmp-itext/src/main/java/at/gv/egiz/pdfas/stmp/itext/ITextStamper.java +++ b/stamper/stmp-itext/src/main/java/at/gv/egiz/pdfas/stmp/itext/ITextStamper.java @@ -1,8 +1,18 @@ package at.gv.egiz.pdfas.stmp.itext; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; -import at.gv.egiz.pdfas.lib.impl.placeholder.SignaturePlaceholderData; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFStamper; import at.gv.egiz.pdfas.lib.impl.stamping.IPDFVisualObject; import at.gv.egiz.pdfas.lib.impl.status.PDFObject; @@ -11,18 +21,20 @@ import at.knowcenter.wag.egov.egiz.table.Entry; import at.knowcenter.wag.egov.egiz.table.Style; import at.knowcenter.wag.egov.egiz.table.Table; -import com.lowagie.text.*; -import com.lowagie.text.pdf.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.HashMap; +import com.lowagie.text.BadElementException; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.Font; +import com.lowagie.text.Image; +import com.lowagie.text.Phrase; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.BaseFont; +import com.lowagie.text.pdf.ITextStamperAccess; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfPCell; +import com.lowagie.text.pdf.PdfPTable; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfStamper; public class ITextStamper implements IPDFStamper { @@ -52,7 +64,7 @@ public class ITextStamper implements IPDFStamper { { if (abstractTable == null) { - PdfAsException pde = new PdfAsException("Table is not defined."); + PdfAsException pde = new PdfAsException("error.pdf.stamp.03"); throw pde; } PdfPTable pdf_table = null; @@ -72,10 +84,12 @@ public class ITextStamper implements IPDFStamper { Style table_style = abstractTable.getStyle(); setCellStyle(pdf_table.getDefaultCell(), table_style, Entry.TYPE_TABLE); - ArrayList rows = abstractTable.getRows(); + @SuppressWarnings("rawtypes") + ArrayList rows = abstractTable.getRows(); for (int row_idx = 0; row_idx < rows.size(); row_idx++) { - ArrayList row = (ArrayList) rows.get(row_idx); + @SuppressWarnings("rawtypes") + ArrayList row = (ArrayList) rows.get(row_idx); logger.debug("## Row:" + row_idx + " ## of table:" + abstractTable.getName()); for (int entry_idx = 0; entry_idx < row.size(); entry_idx++) { @@ -243,9 +257,9 @@ public class ITextStamper implements IPDFStamper { } return font; } catch (DocumentException e) { - throw new PdfAsException(e.getMessage()); + throw new PdfAsException("error.pdf.stamp.01", e); } catch (IOException e) { - throw new PdfAsException(e.getMessage()); + throw new PdfAsException("error.pdf.stamp.01", e); } } @@ -345,7 +359,7 @@ public class ITextStamper implements IPDFStamper { else { if (pdfaValid) { - throw new PdfAsException("PDF/A modus requires an embedable true type font"); + throw new PdfAsException("error.pdf.stamp.02"); } cell_font = getCellFont(font_string); @@ -367,17 +381,15 @@ public class ITextStamper implements IPDFStamper { File img_file = new File(img_ref); if (!img_file.isAbsolute()) { logger.debug("Image file declaration is relative. Prepending path of resources directory."); - //TODO: implement settings .... img_file = new File(settings.getWorkingDirectory() + File.separator + img_ref); } else { logger.debug("Image file declaration is absolute. Skipping file relocation."); } -// String img_location = SettingsReader.relocateFile(img_ref); -// File img_file = new File (img_location); + if (!img_file.exists()) { logger.debug("Image file \"" + img_file.getCanonicalPath() + "\" doesn't exist."); - throw new PdfAsException("Image file \"" + img_file.getCanonicalPath() + "\" doesn't exist."); + throw new PdfAsException("error.pdf.stamp.04"); } Image image = Image.getInstance(img_file.getCanonicalPath()); logger.debug("Using image file \"" + img_file.getCanonicalPath() + "\"."); @@ -396,19 +408,19 @@ public class ITextStamper implements IPDFStamper { catch (BadElementException e) { logger.error("BadElementException:" + e.getMessage()); - PdfAsException pde = new PdfAsException("Unable to create PDF table."); + PdfAsException pde = new PdfAsException("error.pdf.stamp.05", e); throw pde; } catch (MalformedURLException e) { logger.error("MalformedURLException:" + e.getMessage()); - PdfAsException pde = new PdfAsException("Unable to create PDF table."); + PdfAsException pde = new PdfAsException("error.pdf.stamp.05", e); throw pde; } catch (IOException e) { - logger.error("Error Code: 222, IOException:" + e.getMessage()); - PdfAsException pde = new PdfAsException("Unable to create PDF table, unable to load image."); + logger.error("IOException:" + e.getMessage()); + PdfAsException pde = new PdfAsException("error.pdf.stamp.05", e); throw pde; } break; @@ -454,8 +466,7 @@ public class ITextStamper implements IPDFStamper { } if(object == null) { - //TODO: exception! - return null; + throw new PdfAsException("error.pdf.stamp.06"); } PdfReader reader = new PdfReader(pdfData); @@ -477,8 +488,9 @@ public class ITextStamper implements IPDFStamper { if (positioningInstruction.getPage() < 1 || positioningInstruction.getPage() > stamper.getReader().getNumberOfPages()) { - throw new PdfAsException("The provided page (=" + + logger.error("The provided page (=" + positioningInstruction.getPage() + ") is out of range."); + throw new PdfAsException("error.pdf.stamp.07"); } if(placeholderName != null) { @@ -503,10 +515,11 @@ public class ITextStamper implements IPDFStamper { } catch (IOException e) { logger.error(e.getMessage(), e); + throw new PDFIOException("error.pdf.stamp.08", e); } catch (DocumentException e) { logger.error(e.getMessage(), e); + throw new PdfAsException("error.pdf.stamp.08", e); } - return null; } public void setSettings(ISettings settings) { diff --git a/stamper/stmp-itext/src/main/java/com/lowagie/text/pdf/ITextStamperAccess.java b/stamper/stmp-itext/src/main/java/com/lowagie/text/pdf/ITextStamperAccess.java index 65b4ba77..171e7420 100644 --- a/stamper/stmp-itext/src/main/java/com/lowagie/text/pdf/ITextStamperAccess.java +++ b/stamper/stmp-itext/src/main/java/com/lowagie/text/pdf/ITextStamperAccess.java @@ -6,6 +6,7 @@ import java.net.MalformedURLException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.PDFIOException; import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.stmp.itext.ITextStamper; @@ -35,12 +36,12 @@ public class ITextStamperAccess { } else { - throw new PdfAsException("failed to write PDF", new NullPointerException("Image dictionary not found in document structure!")); + throw new PDFIOException("error.pdf.io.02", new NullPointerException("Image dictionary not found in document structure!")); } } else { - throw new PdfAsException("failed to write PDF", new NullPointerException("Resource dictionary not found in document structure!")); + throw new PDFIOException("error.pdf.io.02", new NullPointerException("Resource dictionary not found in document structure!")); } } -- cgit v1.2.3