0

I'm having trouble when Iḿ trying to decrypt a document, I'm using Public/Private pair key to do this. I'm using a Token to do this.

This is the error that I'm getting:

java.security.ProviderException: java.security.KeyException: An internal error occurred.

at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:297)
at sun.security.mscapi.RSACipher.engineDoFinal(RSACipher.java:321)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at org.bouncycastle.operator.jcajce.JceAsymmetricKeyUnwrapper.generateUnwrappedKey(Unknown Source)
at org.bouncycastle.cms.jcajce.JceKeyTransRecipient.extractSecretKey(Unknown Source)
at org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.KeyTransRecipientInformation.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContentStream(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContent(Unknown Source)
at ec.gov.informatica.firmadigital.cms.CMSEncryption.decrypt(CMSEncryption.java:198)
at ec.mil.gestordocumental.security.test.encryption.DecryptFileWithPublicCertificateToken.mainTest(DecryptFileWithPublicCertificateToken.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.security.KeyException: An internal error occurred.

at sun.security.mscapi.RSACipher.encryptDecrypt(Native Method)
at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:289)
... 32

And this is the code I'm using to decrypt:

public static byte[] decrypt(byte[] encrypted, X509Certificate cert, PrivateKey privateKey, Provider provider) {
    try {
        CMSEnvelopedData enveloped = new CMSEnvelopedData(encrypted);

        RecipientInformationStore recipients = enveloped.getRecipientInfos();
        X509CollectionStoreParameters s = new X509CollectionStoreParameters(Collections.singleton(new JcaX509CertificateHolder(cert)));

        X509StoreCertCollection s1 = new X509StoreCertCollection();
        s1.engineInit(s);

        Iterator it = recipients.getRecipients().iterator();

        RecipientInformation recipient = null;

        while (it.hasNext()) {
            recipient = (RecipientInformation) it.next();

            if (recipient instanceof KeyTransRecipientInformation) {
                Collection matches = s1.engineGetMatches(recipient.getRID());

                if (!matches.isEmpty()) {
                      JceKeyTransEnvelopedRecipient ter = null;

                      if ("sun.security.mscapi.RSAPrivateKey".equals(privateKey.getClass().getCanonicalName() ) ) {
                            ter = new JceKeyTransEnvelopedRecipient(privateKey);
                            ter.setProvider( "SunMSCAPI" );
                            ter.setContentProvider(BouncyCastleProvider.PROVIDER_NAME);
                        } else {
                            ter = new JceKeyTransEnvelopedRecipient(privateKey);
                            ter.setProvider(BouncyCastleProvider.PROVIDER_NAME);
                        } 

                    return recipient.getContent(ter);
                }
            } else {
                throw new RuntimeException("Wrong type of RecipientInformation: " + recipient.getClass());
            }
            recipient=null;
        }

        if (recipient == null) {
            throw new RuntimeException("Could not find a matching recipient"); 
        }

    } catch (CMSException e) {
        throw new RuntimeException(e); // FIXME
    } catch (CertificateEncodingException e) {
        throw new RuntimeException(e);
    }
}

Please help me what it could be.

Thanks a lot.

lion316
  • 3
  • 1
  • 1
  • 4
  • 1
    Error interno is not a normal Java Exception, it seems to be thrown by the library around the token. Try and dig up debugging information from the library talking to the token. – Maarten Bodewes Aug 13 '13 at 23:29
  • Thanks for your reply, but that error wasn't for any library around token, it was for certificate that send me to include into the token, if somene has someday the same error validate that certificated included into token is correct. Thanks anyway!! – lion316 Aug 22 '13 at 14:47

1 Answers1

0

I'm having the same problem for decryption, using both MSCAPI and PKCS#11. I found that the P11RSAChiper implemented in SunPKCS11 doesn't regard the wrap/unwrap methods, and it uses encrypt/decrypt for this purpose, which in my case conflicts with underlying layer of security, where the private key is marked just for unwrap by the SmartCard profile.

SevanEl
  • 11
  • 2