Skip to content

Commit 4366675

Browse files
authored
Merge pull request #23 from hapytex/issues/issue-21
make the tsa server configurable, fixing #21
2 parents a276e99 + fcac834 commit 4366675

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

lib/src/main/java/BatchPDFSign/lib/BatchPDFSign.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class BatchPDFSign {
2222

2323
private static PrivateKey privateKey;
2424
private static Certificate[] certificateChain;
25+
private static String defaultTsaUri = "https://freetsa.org/tsr";
2526

2627
private final String pkcs12FileName;
2728
private final String PkcsPassword;
@@ -61,14 +62,19 @@ public BatchPDFSign(String pkcs12FileName, String PkcsPassword, String pdfInputF
6162
* @throws GeneralSecurityException Some permissions aren't right.
6263
*/
6364
public void signFile(int page, float rx, float ry, float rw, float rh, float fs, String signtext) throws IOException, GeneralSecurityException {
64-
65+
signFile(page, rx, ry, rw, rh, fs, signtext, null);
66+
}
67+
public void signFile(int page, float rx, float ry, float rw, float rh, float fs, String signtext, String tsaUri) throws IOException, GeneralSecurityException {
68+
if(tsaUri == null) {
69+
tsaUri = defaultTsaUri;
70+
}
6571
// Check PDF input file
6672
if (!inputFile.exists() || inputFile.isDirectory()) {
6773
throw new FileNotFoundException("File: " + this.inputFile + " wasn't found");
6874
}
6975
readPrivateKeyFromPKCS12(pkcs12FileName, PkcsPassword);
7076
PdfReader reader = new PdfReader(pdfInputFileName);
71-
ITSAClient tsaClient = new TSAClientBouncyCastle("https://freetsa.org/tsr");
77+
ITSAClient tsaClient = new TSAClientBouncyCastle(tsaUri);
7278
StampingProperties properties = new StampingProperties().preserveEncryption();
7379
PdfSigner signer = new PdfSigner(reader, new FileOutputStream(pdfOutputFileName), properties);
7480
if (page > 0) {
@@ -96,8 +102,11 @@ public void signFile(int page, float rx, float ry, float rw, float rh, float fs,
96102
}
97103
}
98104
}
105+
public void signFile(String tsaUri) throws IOException, GeneralSecurityException {
106+
this.signFile(0, 0, 0, 0, 0, 10, "", tsaUri);
107+
}
99108
public void signFile() throws IOException, GeneralSecurityException {
100-
this.signFile(0, 0, 0, 0, 0, 10, "");
109+
this.signFile(null);
101110
}
102111

103112
/**

portable/src/main/java/BatchPDFSign/portable/Main.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public static void main(String[] args){
6262
output.setRequired(false);
6363
options.addOption(signTextOpt);
6464

65+
Option tsaOpt = new Option(null, "tsa", true, "URI of the time service authority (TSA)");
66+
output.setRequired(false);
67+
options.addOption(tsaOpt);
68+
69+
6570
CommandLineParser parser = new DefaultParser();
6671
HelpFormatter formatter = new HelpFormatter();
6772
CommandLine cmd;
@@ -80,6 +85,10 @@ public static void main(String[] args){
8085
}
8186
BatchPDFSign batchPDFSign;
8287
batchPDFSign = new BatchPDFSign(keyFilePath, passwordString, inputFilePath, outputFilePath);
88+
String tsaUri = null;
89+
if (cmd.hasOption("tsa")) {
90+
tsaUri = cmd.getOptionValue("tsa");
91+
}
8392
if (cmd.hasOption("page")) {
8493
if (!cmd.hasOption("rx") || !cmd.hasOption("ry") ||
8594
!cmd.hasOption("rw") || !cmd.hasOption("rh")) {
@@ -99,9 +108,9 @@ public static void main(String[] args){
99108
if (cmd.hasOption("signtext")) {
100109
signText = cmd.getOptionValue("signtext");
101110
}
102-
batchPDFSign.signFile(page, rectPosX, rectPosY, rectWidth, rectHeight, fontSize, signText);
111+
batchPDFSign.signFile(page, rectPosX, rectPosY, rectWidth, rectHeight, fontSize, signText, tsaUri);
103112
} else {
104-
batchPDFSign.signFile();
113+
batchPDFSign.signFile(tsaUri);
105114
}
106115
} catch (GeneralSecurityException | IOException | ParseException e) {
107116
System.out.println(e.getMessage());

0 commit comments

Comments
 (0)