1 Reply Latest reply: Aug 19, 2013 7:24 AM by sonofre29 RSS

    Error sign PDF document

    1003360
      Hello.

      I am developing an applet for signing PDF document using the iText library.
      On some machines occurs an Exception during the signing
       java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ARGUMENTS_BAD 
      Here is the code used
      public void signPdf(InputStream pdfInputStream, String pdfFileNameOut, boolean visibleSignsPdf) {
                
                //providerPKCS11 is a variable in class 
                System.out.println("Provider  "+providerPKCS11);
                Security.addProvider(providerPKCS11);
                Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
      
                try {
                     Enumeration <String> aliases = keysotre.aliases();
                     while (aliases.hasMoreElements()) {
                          System.out.println("--->>> " + aliases.nextElement());
                     }
      
                     PrivateKey pk = (PrivateKey) keysotre.getKey(SIGNATURE_CERTIFICATE_LABLE, null);
                     Certificate[] chain = keysotre.getCertificateChain(SIGNATURE_CERTIFICATE_LABLE);
                     OcspClient ocspClient = new OcspClientBouncyCastle();
                     List <CrlClient> crlList = new ArrayList();
                     crlList.add(new CrlClientOnline(chain));
      
                     PdfReader reader = new PdfReader(pdfInputStream);
      
                     FileOutputStream os = new FileOutputStream(pdfFileNameOut);
                     PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
                     
                     PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
                     appearance.setReason("reasonnnnnn");
                     appearance.setLocation("locationnnnn");
                     appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
                     ExternalDigest digest = new BouncyCastleDigest();
                     ExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, providerPKCS11.getName());
                     TSAClient tsc = new TSAClientBouncyCastle("http://ts.cartaodecidadao.pt/tsa/server", null, null);
      
                     //Next line i have the Exception
                     MakeSignature.signDetached(appearance, digest, signature, chain, crlList, ocspClient, tsc, 0, MakeSignature.CryptoStandard.CMS);
                     
                } catch (UnrecoverableKeyException e) {
                     e.printStackTrace();
                } catch (KeyStoreException e) {
                     e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                     e.printStackTrace();
                } catch (IOException e) {
                     e.printStackTrace();
                } catch (DocumentException e) {
                     e.printStackTrace();
                } catch (GeneralSecurityException e) {
                     e.printStackTrace();
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }
      Exception:
      java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ARGUMENTS_BAD
           at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:531)
           at java.security.Signature$Delegate.engineSign(Unknown Source)
           at java.security.Signature.sign(Unknown Source)
           at com.itextpdf.text.pdf.security.PrivateKeySignature.sign(PrivateKeySignature.java:116)
           at com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:146)
           at com.pdfutils.documentsigner.PdfSignNewVersion.signPdf(Unknown Source)
           at applet.packcc.CartaoCidadaoConnector.pdfSign(Unknown Source)
           at applet.packcc.SignerThread.assinaDocumento(Unknown Source)
           at applet.packcc.SignerThread.run(Unknown Source)
      Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ARGUMENTS_BAD
           at sun.security.pkcs11.wrapper.PKCS11.C_Sign(Native Method)
           at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:522)
           ... 8 more
      Is there some solution to my problem?
      Any one can help me?

      Rui Almeida

      Edited by: RuiAlmeida on 17/Abr/2013 13:41

      Edited by: RuiAlmeida on 17/Abr/2013 14:00