Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8280181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:34:39+00:00 2026-06-08T09:34:39+00:00

I am attempting to digitally sign PDFs using a smart card and iText. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:34:42+00:00Added an answer on June 8, 2026 at 9:34 am

    It seems if I follow the example with authenticated attributes I can get the PIN dialog to only pop up once for each time signing a document. This is the final code that I ended up using, hopefully it will be helpfully for someone else.

    for(int i=0;i<2;i++) {
        String pkcs11ConfigSettings =
                    "name = AuthProvider\nlibrary = C:\\Program Files\\ActivIdentity\\ActivClient\\acpkcs201-ns.dll";
        AuthProvider p = (SunPKCS11)Security.getProvider("SunPKCS11-AuthProvider");
        if(p==null) {
            p = new SunPKCS11(new ByteArrayInputStream(pkcs11ConfigSettings.getBytes()));
            p.setCallbackHandler(new CallbackHandler() {    
                @Override
                public void handle(Callback[] callbacks) throws IOException,
                        UnsupportedCallbackException {
                    for(Callback c : callbacks)
                        if(c instanceof PasswordCallback) {
                            //HACK. if we set password to null it will bring up the drivers PIN dialog.
                            ((PasswordCallback) c).setPassword(null);
                        }
                }
            });
            Security.addProvider(p);
        }
        KeyStore ks = KeyStore.getInstance("PKCS11",p); 
        ks.load(null, null);
        Certificate[] cc = ks.getCertificateChain("Digital Signature Key");
        PrivateKey pk = (PrivateKey)ks.getKey("Digital Signature Key", null);
        OutputStream fos = new FileOutputStream("c:\\doc" + i + ".pdf"); ;
        PdfReader reader = new PdfReader(new FileInputStream(new File("C:\\1.pdf")));
        PdfStamper stamper = PdfStamper.createSignature(reader, fos, '\0');
        PdfSignatureAppearance sap = stamper.getSignatureAppearance();
        sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
        Calendar cal = Calendar.getInstance();
        PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
        dic.setDate(new PdfDate(cal));
        dic.setName(PdfPKCS7.getSubjectFields((X509Certificate)cc[0]).getField("CN"));
        sap.setCryptoDictionary(dic);
        sap.setLayer2Text("Digitally signed by "+ dic.get(PdfName.NAME) +"\n\nDate: " + cal.getTime().toString());
        HashMap<PdfName,Object> exc = new HashMap<PdfName,Object>();
        exc.put(PdfName.CONTENTS, new Integer(0x2502));
        sap.preClose(exc);
        PdfPKCS7 pk7 = new PdfPKCS7(pk, cc, null, "SHA1", "SunPKCS11-AuthProvider", false);
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        byte buf[] = new byte[8192];
        int n;
        InputStream inp = sap.getRangeStream();
        while ((n = inp.read(buf)) > 0) {
            messageDigest.update(buf, 0, n);
        }
        byte hash[] = messageDigest.digest();
        byte sh[] = pk7.getAuthenticatedAttributeBytes(hash, cal, null);
        pk7.update(sh, 0, sh.length);
        PdfDictionary dic2 = new PdfDictionary();
        byte sg[] = pk7.getEncodedPKCS7(hash, cal);
        byte out[] = new byte[0x2500 / 2];
        System.arraycopy(sg, 0, out, 0, sg.length);
        dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true));
        sap.close(dic2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a SAML gateway using Ruby/Rails and I'm attempting to write some
Attempting to use squarebracket notation to reference a dynamic variable. (I'm looping through a
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
Attempting/struggling to get registration and sign-up working within an active admin project. I have
Attempting to use JRuby 1.2.0 and Rails 2.3.2 with an embedded Derby database. I've
Attempting to set up Samba + OpenLDAP using nss_ldap. After joining Windows7 to Samba
Attempting to use the amazon API to obtain product data and currently failing miserably.
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
Attempting to use a custom hex color for my css triangle (border). However since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.