From fcac8346965362ff9dba616ed47526b190bbfa53 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sun, 20 Nov 2022 00:39:42 +0100 Subject: [PATCH] make the tsa server configurable, fixing #21 --- .../main/java/BatchPDFSign/lib/BatchPDFSign.java | 15 ++++++++++++--- .../src/main/java/BatchPDFSign/portable/Main.java | 13 +++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/src/main/java/BatchPDFSign/lib/BatchPDFSign.java b/lib/src/main/java/BatchPDFSign/lib/BatchPDFSign.java index c05a62d..3160b3d 100644 --- a/lib/src/main/java/BatchPDFSign/lib/BatchPDFSign.java +++ b/lib/src/main/java/BatchPDFSign/lib/BatchPDFSign.java @@ -22,6 +22,7 @@ public class BatchPDFSign { private static PrivateKey privateKey; private static Certificate[] certificateChain; + private static String defaultTsaUri = "https://freetsa.org/tsr"; private final String pkcs12FileName; private final String PkcsPassword; @@ -61,14 +62,19 @@ public BatchPDFSign(String pkcs12FileName, String PkcsPassword, String pdfInputF * @throws GeneralSecurityException Some permissions aren't right. */ public void signFile(int page, float rx, float ry, float rw, float rh, float fs, String signtext) throws IOException, GeneralSecurityException { - + signFile(page, rx, ry, rw, rh, fs, signtext, null); + } + public void signFile(int page, float rx, float ry, float rw, float rh, float fs, String signtext, String tsaUri) throws IOException, GeneralSecurityException { + if(tsaUri == null) { + tsaUri = defaultTsaUri; + } // Check PDF input file if (!inputFile.exists() || inputFile.isDirectory()) { throw new FileNotFoundException("File: " + this.inputFile + " wasn't found"); } readPrivateKeyFromPKCS12(pkcs12FileName, PkcsPassword); PdfReader reader = new PdfReader(pdfInputFileName); - ITSAClient tsaClient = new TSAClientBouncyCastle("https://freetsa.org/tsr"); + ITSAClient tsaClient = new TSAClientBouncyCastle(tsaUri); StampingProperties properties = new StampingProperties().preserveEncryption(); PdfSigner signer = new PdfSigner(reader, new FileOutputStream(pdfOutputFileName), properties); if (page > 0) { @@ -96,8 +102,11 @@ public void signFile(int page, float rx, float ry, float rw, float rh, float fs, } } } + public void signFile(String tsaUri) throws IOException, GeneralSecurityException { + this.signFile(0, 0, 0, 0, 0, 10, "", tsaUri); + } public void signFile() throws IOException, GeneralSecurityException { - this.signFile(0, 0, 0, 0, 0, 10, ""); + this.signFile(null); } /** diff --git a/portable/src/main/java/BatchPDFSign/portable/Main.java b/portable/src/main/java/BatchPDFSign/portable/Main.java index 79746f5..1ef43a6 100644 --- a/portable/src/main/java/BatchPDFSign/portable/Main.java +++ b/portable/src/main/java/BatchPDFSign/portable/Main.java @@ -62,6 +62,11 @@ public static void main(String[] args){ output.setRequired(false); options.addOption(signTextOpt); + Option tsaOpt = new Option(null, "tsa", true, "URI of the time service authority (TSA)"); + output.setRequired(false); + options.addOption(tsaOpt); + + CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd; @@ -80,6 +85,10 @@ public static void main(String[] args){ } BatchPDFSign batchPDFSign; batchPDFSign = new BatchPDFSign(keyFilePath, passwordString, inputFilePath, outputFilePath); + String tsaUri = null; + if (cmd.hasOption("tsa")) { + tsaUri = cmd.getOptionValue("tsa"); + } if (cmd.hasOption("page")) { if (!cmd.hasOption("rx") || !cmd.hasOption("ry") || !cmd.hasOption("rw") || !cmd.hasOption("rh")) { @@ -99,9 +108,9 @@ public static void main(String[] args){ if (cmd.hasOption("signtext")) { signText = cmd.getOptionValue("signtext"); } - batchPDFSign.signFile(page, rectPosX, rectPosY, rectWidth, rectHeight, fontSize, signText); + batchPDFSign.signFile(page, rectPosX, rectPosY, rectWidth, rectHeight, fontSize, signText, tsaUri); } else { - batchPDFSign.signFile(); + batchPDFSign.signFile(tsaUri); } } catch (GeneralSecurityException | IOException | ParseException e) { System.out.println(e.getMessage());