-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSignPDF.java
More file actions
86 lines (75 loc) · 3.01 KB
/
SignPDF.java
File metadata and controls
86 lines (75 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package PDFSignature;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Calendar;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import com.sun.xml.internal.txw2.Document;
public class SignPDF extends CreateSignatureBase {
public SignPDF(KeyStore keystore, char[] pin) throws KeyStoreException, UnrecoverableKeyException,
NoSuchAlgorithmException, IOException, CertificateException {
super(keystore, pin);
}
private static char[] password = "1111".toCharArray();
private static KeyStore keystore;
private static Provider provider;
public static void main(String[] args) throws IOException, GeneralSecurityException {
// Setup PKCS11 Provider
String pkcs11conf = "/home/l3m/javaApp/pkcs11.cfg";
// provider = new sun.security.pkcs11.SunPKCS11(pkcs11conf);
provider = new sun.security.pkcs11.SunPKCS11(pkcs11conf);
Security.addProvider(provider);
// Load keystore
keystore = KeyStore.getInstance("PKCS11", provider);
keystore.load(null, password);
SignPDF sign = new SignPDF(keystore, password);
File inFile = new File("/home/l3m/Smart_Card.pdf");
String name = inFile.getName();
String substring = name.substring(0, name.lastIndexOf('.'));
File outFile = new File(inFile.getParent(), substring + "_signed.pdf");
sign.signDocument(inFile, outFile);
}
private void signDocument(File inFile, File outFile) throws IOException {
if (inFile == null || !inFile.exists()) {
throw new FileNotFoundException("Document for signing does not exist");
}
FileOutputStream fileOS = new FileOutputStream(outFile);
System.out.println("[?] Loading PDF...");
try (PDDocument doc = PDDocument.load(inFile)) {
System.out.println("[+] Successfully Loaded");
try {
System.out.println("[?] Signing PDF...");
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName("Luis Moreira");
signature.setLocation("Massarelos, Porto");
signature.setReason("To get the job done.");
signature.setSignDate(Calendar.getInstance());
doc.addSignature(signature, this);
doc.saveIncremental(fileOS);
System.out.println("[+] Successfully Signed");
System.out.println();
System.out.println(outFile.getAbsolutePath());
} catch (Exception e) {
// TODO: handle exception
System.out.println("[-] Unsuccessfully Signed");
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("[-] Unsuccessfully Loaded");
e.printStackTrace();
}
}
}