Providers load order conflict (ExceptionInInitializerError)

Hello, I have a problem when using several java.security.Provider (BouncyCastleProvider and SunJCE), by the sight, the order in which they are loaded is the problem.

If load BouncyCastleProvider at position 1, and SunJCE in the 2, (following code) is throwed java.lang.ExceptionInInitializerError

java.security.Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 1);

java.security.Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 2);

SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMDAndDES");// throws ExceptionInInitializerError

If the position are another (SunJCE at 1, and BouncyCastle 2)

java.security.Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1);

java.security.Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 2);

SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMDAndDES");// it works well!

yes works well (need that BouncyCastle exist at position 1), but I have problems soon, with my jar library that uses BouncyCastleProvider (which I cannot modify).

I have tried to do the following:

java.security.Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 1);

java.security.Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 2);

Provider sunJCEProv = java.security.Security.getProvider("SunJCE");

SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMDAndDES", sunJCEProv);// does not work either (ExceptionInInitializerError)

here I am indicating him the Provider (SunJCE), but seems to take in any case bouncycastle (the Provider in the position 1).

The full stackTrace of the exception is:

java.lang.ExceptionInInitializerError

AT javax.crypto.SecretKeyFactory.getInstance(DashoA12275)

...

Caused by: java.lang.SecurityException:

Cannotfor Seth up certs trusted CAs AT javax.crypto.SunJCE_b. (DashoA12275)

... 25 and more

Caused: java.security.PrivilegedActionException: java.security.InvalidKeyException: Publicfor key presented not certificate signature AT java.security.AccessController.doPrivileged(Native Method)

... 26 and more

Caused by: java.security.InvalidKeyException: Publicfor key presented not certificate signature

AT org.bouncycastle.jce.provider.X509CertificateObject.checkSignature(Unknown Source)

AT org.bouncycastle.jce.provider.X509CertificateObject.verify(Unknown Source)

AT javax.crypto.SunJCE_b.c(DashoA12275)

AT javax.crypto.SunJCE_b.b(DashoA12275)

AT javax.crypto.SunJCE_q.run(DashoA12275)

Some idea?

Thanks to all!

[3229 byte] By [jpadrona] at [2007-10-2 20:32:10]
# 1

First off, Im assuming the algorithm you're dealing with is actually

PBEWithMD5AndDES (not MD without the 5).

With that, Ive done some experiments and it seems to be dependent on

where in the java.security file you have the BC provider.

$JAVA_HOME/jre/lib/security/java.security

If my java.security settings look like this (other providers omitted from post)

all 3 of your tests work:

security.provider.1=sun.security.provider.Sun

security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider

However, if its reversed like this none work.

security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider

security.provider.2=sun.security.provider.Sun

I thought the insertProviderAt() call worked independently of the java.security

settings and would override them. In this case it seems like something

else is going on...

So try making BouncyCastleProvider #2 behind Sun in your java.security file and see what happens.

cdelikata at 2007-7-13 23:15:20 > top of Java-index,Security,Cryptography...
# 2

you should avoid situations where you need to put your provider in first position:

http://java.sun.com/products/jce/jce122_knownbugs.html

But if your design requires you to insert a provider in first position look at

http://taxido.blogspot.com/2004/05/set-java-security-provider-in-first.html

you can write me if you need further helps on this

babakNa at 2007-7-13 23:15:20 > top of Java-index,Security,Cryptography...
# 3
Try one of the recent versions of mustang. This was fixed a while back.You know how to obtain the weekly mustang snapshots at mustang.dev.java.net, right?
wetmorea at 2007-7-13 23:15:20 > top of Java-index,Security,Cryptography...