5 Replies Latest reply on May 9, 2011 3:00 AM by Sorin Silaghi

    Using bouncycastle for S/MIME throws ClassCastException

    Sorin Silaghi Newbie

      Hi,

       

               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));

           }

        • 1. Re: Using bouncycastle for S/MIME throws ClassCastException
          Freeman(Yue) Fang Master

          Hi,

           

          Could you tell us which exact FUSE ESB version you're using?

           

          You shouldn't put javamail jar in $FUSE_ESB/lib folder as we  already install java mail api bundle by default.

          Does your customer bundle already import package like javax.mail, javax.mail.util, javax.activation?

           

          Freeman

          • 2. Re: Using bouncycastle for S/MIME throws ClassCastException
            Sorin Silaghi Newbie

            Hi,

             

             

                  I'm using 4.3.1-fuse-01-09. I tried not deploying javamail and just importing everything and I get a different error:

             

            Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException

                 at java.lang.Class.getDeclaredConstructors0(Native Method)[:1.6.0_20]

                 at java.lang.Class.privateGetDeclaredConstructors(Class.java:2406)[:1.6.0_20]

                 at java.lang.Class.getConstructor0(Class.java:2716)[:1.6.0_20]

                 at java.lang.Class.newInstance0(Class.java:343)[:1.6.0_20]

                 at java.lang.Class.newInstance(Class.java:325)[:1.6.0_20]

                 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:35)

                 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

            Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException

                 at java.net.URLClassLoader$1.run(URLClassLoader.java:217)[:1.6.0_20]

                 at java.security.AccessController.doPrivileged(Native Method)[:1.6.0_20]

                 at java.net.URLClassLoader.findClass(URLClassLoader.java:205)[:1.6.0_20]

                 at java.lang.ClassLoader.loadClass(ClassLoader.java:321)[:1.6.0_20]

                 at java.lang.ClassLoader.loadClass(ClassLoader.java:266)[:1.6.0_20]

                 ... 29 more

             

            It looks like that happens when I do:

             

                           SMIMESigned s = new SMIMESigned((MimeMultipart)message.getContent());

             

             

            Sorin.

            • 3. Re: Using bouncycastle for S/MIME throws ClassCastException
              Freeman(Yue) Fang Master

              Hi,

               

              Does your customer bundle already import javax.mail package?

               

              Could you post the result of

              osgi:headers your_customer_bundle_id?

               

              Freeman

              • 4. Re: Using bouncycastle for S/MIME throws ClassCastException
                Sorin Silaghi Newbie

                Hi,

                 

                 

                     Yes, here are the packages I import:

                 

                javax.mail,

                org.bouncycastle.cert,

                org.bouncycastle.cms,

                org.bouncycastle.cms.jcajce,

                org.bouncycastle.mail.smime,

                org.bouncycastle.util,

                 

                      I don't think it would build this at all without them imported. Could it be that bcmail doesn't have javax in its classpath ?

                 

                Sorin.

                • 5. Re: Using bouncycastle for S/MIME throws ClassCastException
                  Sorin Silaghi Newbie

                  I added javax.mail.util to the import list (it was missing) and now I get a different error. I think this one is caused by some missing lines from the mailcap map:

                   

                  java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast to javax.mail.internet.MimeMultipart

                       at de.edigrid.util.Activator.start(Activator.java:35)

                       at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:783)

                       at java.security.AccessController.doPrivileged(Native Method)

                       at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:774)

                       at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:755)

                       at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:370)

                       at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)

                       at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)

                       at org.apache.karaf.shell.osgi.StartBundle.doExecute(StartBundle.java:29)

                       at org.apache.karaf.shell.osgi.BundlesCommand.doExecute(BundlesCommand.java:49)

                       at org.apache.karaf.shell.console.OsgiCommandSupport.execute(OsgiCommandSupport.java:38)

                       at org.apache.felix.gogo.commands.basic.AbstractCommand.execute(AbstractCommand.java:35)

                       at org.apache.felix.gogo.runtime.shell.CommandProxy.execute(CommandProxy.java:50)

                       at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:229)

                       at org.apache.felix.gogo.runtime.shell.Closure.executeStatement(Closure.java:162)

                       at org.apache.felix.gogo.runtime.shell.Pipe.run(Pipe.java:101)

                       at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:79)

                       at org.apache.felix.gogo.runtime.shell.CommandSessionImpl.execute(CommandSessionImpl.java:71)

                       at org.apache.karaf.shell.console.jline.Console.run(Console.java:170)

                       at java.lang.Thread.run(Thread.java:636)