Using bouncycastle for S/MIME throws ClassCastException
sorin7486 May 4, 2011 9:34 AMHi,
I've been trying to get some signatures verified using bouncycastle but nothing I do seems to work: I tried adding bouncycastle as a security provider (link below) but that throws an because it can't find javax.mail.Part
http://servicemix.apache.org/kernel/65-deploying-security-providers.html
If I also deploy javax.mail along with the bouncycastle jars I get a different error:
Caused by: java.lang.ClassCastException: com.sun.mail.handlers.multipart_mixed cannot be cast to javax.activation.DataContentHandler at org.apache.servicemix.specs.activation.OsgiMailcapCommandMap.createDataContentHandler(OsgiMailcapCommandMap.java:60)
at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:249)[:1.6.0_20]
at javax.activation.DataHandler.getContent(DataHandler.java:142)[:1.6.0_20]
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1398)[268:org.apache.servicemix.bundles.javax.mail:1.4.1.4]
at de.edigrid.util.Activator.start(Activator.java:34)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:783)[osgi-3.6.0.v20100517.jar:]
at java.security.AccessController.doPrivileged(Native Method)[:1.6.0_20]
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:774)[osgi-3.6.0.v20100517.jar:]
... 16 more
I tried running the code in a SU or in a bundle and I always get the folowing exception:
Caused by: java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast to javax.mail.internet.MimeMultipart
at de.edigrid.util.Activator.start(Activator.java:34)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:783)[osgi-3.6.0.v20100517.jar:]
at java.security.AccessController.doPrivileged(Native Method)[:1.6.0_20]
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:774)[osgi-3.6.0.v20100517.jar:]
... 16 more
I took the code from one of the test cases that comes with bouncycastle and if I run it with JUnit it works. I only get this problem when I run it in ServiceMix. Here's the code:
if (Security.getProvider("BC") == null){
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
MimeMessage message = loadMessage("/basicAS2.message");
SMIMESigned s = new SMIMESigned((MimeMultipart)message.getContent()); // error occurs here
verifySigners(s.getCertificates(), s.getSignerInfos());
.....
private MimeMessage loadMessage(String name)
throws MessagingException, FileNotFoundException
{
Session session = Session.getDefaultInstance(System.getProperties(), null);
return new MimeMessage(session, getClass().getResourceAsStream(name));
}