Skip to content

Commit

Permalink
Merge pull request #23 from hapytex/issues/issue-21
Browse files Browse the repository at this point in the history
make the tsa server configurable, fixing #21
  • Loading branch information
jmarxuach authored Nov 20, 2022
2 parents a276e99 + fcac834 commit 4366675
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
15 changes: 12 additions & 3 deletions lib/src/main/java/BatchPDFSign/lib/BatchPDFSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions portable/src/main/java/BatchPDFSign/portable/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")) {
Expand All @@ -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());
Expand Down

0 comments on commit 4366675

Please sign in to comment.