problems reloading web applet. keyStore or Provider.!

First of all, thanks for reading.

INTRODUCTION

We are implementing a Document Signing applet and got some serious problems when reloading the applet with the following structure:

1 Applet loads, and if no card is inserted, it shows a warning message and waits.

2 Applet download a document to be signed.

3 Applet tries to load some PKCS11 Providers, if one match with the card inserted, Pin Dialog is shown on applet, else execution ends.

4 Applet shows the Certificates on Card, waitting to select one and sign with. (only shows Signing valid certificates).

5 Signs the applet.

6 Upload the applet to a host.

7 Shows the warning, error or succefully success of the execution and redirect the web to some webpage.

The first execution of the applet works correctly, then we reload the webpage, to execute it already and get the execution above:

1 Applet loads, and if no card is inserted, it shows a warning message and waits.

2 Applet download a document to be signed.

3 Applet tries to load some PKCS11 Providers, if one match with the card inserted, Pin Dialog is shown on applet, else execution ends.

4 Applet shows the Certificates on Card, waitting to select one and sign with. (only shows Signing valid certificates).

5 Applet fails because there are two private keys sharing same CKA_ID (logically an error if this happened in the same SmartCard ).

java.security.ProviderException: java.security.KeyStoreException: invalid KeyStore state: found 2 private keys sharing CKA_ID 0xXXXXXXXXXXXX

at sun.security.pkcs11.P11KeyStore.engineGetKey(P11KeyStore.java:317)

-

SOME TESTS:

[TEST NAME: ] [TEST RESULT: ]

[HISTORY]

[test name: Ejecting SmartCard after execution] [test result: WORKING FINE]

[HISTORY]

After running the applet the first time, we send a message to user to eject the SmartCard a moment, then insert it another time, and the applet can be runned with no problem a second time, also a third, fourth if we eject the card after every execution.

[test name: Inserting a different card] [test result: WORKING FINE]

[HISTORY]

After running the applet one time, we insert a different card, using a different PKCS11 provider, and it works as intended. That test can be located at test1.

[test name: Inserting a same pkcs11 provider card] [test result: WORKING FINE]

[HISTORY]

After running the applet one time, we insert a different card but with the same PKCS11 Provider, and as shown in test1,it works correctly cause the "removing the card".

[test name: Reloading multiple times applet] [test result: FAILS]

[HISTORY]

We run the applet, with the same SmartCard multiple times, just to see if the problem persist and we got the result:

...ProviderException: java.security.KeyStoreException: invalid KeyStore state: found2 private keys sharing CKA_ID

...ProviderException: java.security.KeyStoreException: invalid KeyStore state: found3 private keys sharing CKA_ID

...ProviderException: java.security.KeyStoreException: invalid KeyStore state: found4 private keys sharing CKA_ID

...ProviderException: java.security.KeyStoreException: invalid KeyStore state: found5 private keys sharing CKA_ID

...ProviderException: java.security.KeyStoreException: invalid KeyStore state: found6 private keys sharing CKA_ID

...

As seen at tests, we think the fail is not deregistering the keyStore, also setting it tonull doesn't solves it, calling this.destroy() at the end of the execution, also no.

But the applet works fine when it thinks the SmartCard has been removed, maybe when setting the KeyStore, or when adding the Provider to Security, but we are out of ideas at the moment.

Our new moves:

Adding at the start of the execution a new "basic provider" to make our applet thinks its working with another card, but the problem may be will persist because the SmartCard is not released from the reader, and the KeyStore is a singleton in java implementation, as intended, and will retain the information.

Any Ideas or solutions ? :)

Thanks !!!

Message was edited by:

Boza

[4474 byte] By [Bozaa] at [2007-11-27 10:41:15]
# 1

Added another test:

1. Opened the applet with Internet Explorer

2. Executed it correctly

3. Clicked the button to redirect to the applet to the webresult.

4. Opened the applet in another Internet Explorer.

5. Failed with the same error.

There are 2 private keys sharing same CKA_ID, as thought, KeyStore is a singleton, so while Java VM is up, that instance will get the same element.

1. Opened the applet with Internet Explorer

2. Executed it correctly

3. Clicked the button to redirect to the applet to the webresult.

4. Opened the applet Firefox.

5. Executed it correctly.

So, the JAVA VM is different if working with Firefox or Internet Explorer ?

Bozaa at 2007-7-28 19:11:35 > top of Java-index,Security,Signed Applets...
# 2

Incoming solutions !!!!

Well, as usual looking for code resources, I saw allone using the native Provider from

java.security.Provider

and working with it, as they were not doing an applet and reloading it many times, the code worked correctly.

Anyway, to solve it its needed to use AuthProvider instead of Provider, and at the end of the Applet

destroy()

or finalize()

invoke myProvider.logout();

- BUT ANOTHER PROBLEM IS IN

Playing with the new code (5 lines changed), now it works with four or five reloads of the applet, the first four or five reloads the applet give no problem, but the next one gets an exception (Im looking on it yet):

sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ATTRIBUTE_TYPE_INVALID !

Uncommon exception when we are working all the time with the same SmartCard just to use the certificates, neither to change or modify them!

Any other ideas? :)

Bozaa at 2007-7-28 19:11:35 > top of Java-index,Security,Signed Applets...