I am attempting to digitally sign PDFs using a smart card and iText. I read through the documentation on how to use iText to sign a document and tried to use some of their code myself. Below is the code I am using:
String pkcs11ConfigSettings =
"name = SmartCard\nlibrary = C:\\Program Files\\ActivIdentity\\ActivClient\\acpkcs201-ns.dll";
AuthProvider p =
new SunPKCS11(new ByteArrayInputStream(pkcs11ConfigSettings.getBytes()));
Security.addProvider(p);
KeyStore.PasswordProtection pp =
new KeyStore.PasswordProtection("012345".toCharArray());
KeyStore.Builder builder =
KeyStore.Builder.newInstance("PKCS11",p ,pp);
KeyStore ks = builder.getKeyStore();
Certificate[] cc = ks.getCertificateChain("Digital Signature Key");
PrivateKey pk = (PrivateKey)ks.getKey("Digital Signature Key", null);
OutputStream fos = new FileOutputStream("c:\\2.pdf");
PdfReader reader = new PdfReader(new FileInputStream(new File("C:\\1.pdf")));
PdfStamper stamper = PdfStamper.createSignature(reader, fos, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCrypto(pk, cc, null,PdfSignatureAppearance.SELF_SIGNED);
appearance.setVisibleSignature(new Rectangle(0, 0, 100, 100), 1,null);
stamper.close();
The problem with this method is when iText closes the PDFStamper it does a call to C_Sign() which invokes the driver's prompt for a PIN.
So if this were an application it would require me to enter my PIN prior to signing, in order to obtain the KeyStore and PrivateKey, as well as when the driver's PIN input prompt comes up. Is there anyway around asking for the PIN twice? I'm kind of new to this stuff, am I going about this the wrong way?