2 Replies Latest reply on Sep 22, 2011 3:00 AM by Claus Ibsen

    Using certificates within Fuse

    Rogelio Sevilla Novice

      Good day everyone:

       

       

      I'm currently developing an application that makes use of ssl certificates to extract information from a https website. I tested the app as a stand alone and everything works as it should. My code look like this:

       

      URL ur = new URL("my https url");

      System.setProperty("javax.net.ssl.trustStore", "config/myKeyStoreFile")

      URLConnection conn = ur.openConnection();

      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

      String outStr=templateProcessorHelper.getRequestTemplate(runtimeValue);

      logger.info("Sending POST Request:\n"+outStr);

       

      Note: I created the config/myKeyStoreFile file into my Fuse installation folder

       

      However, when deploying the same application into the ESB, the command conn.getOutputStream() (which is on the fourth line), throws this exception:

       

      Exception occurred in target VM: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)

      java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)

          at javax.net.ssl.DefaultSSLSocketFactory.throwException(SSLSocketFactory.java:179)

          at javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:186)

          at sun.net.www.protocol.https.HttpsClient.createSocket(HttpsClient.java:362)

          at sun.net.NetworkClient.doConnect(NetworkClient.java:145)

          at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)

          at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)

          at sun.net.www.protocol.https.HttpsClient.(HttpsURLConnectionImpl.java:65)

          at sun.net.www.protocol.https.Handler.openConnection(Handler.java:42)

          at sun.net.www.protocol.https.Handler.openConnection(Handler.java:37)

          at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

          at java.lang.reflect.Method.invoke(Method.java:597)

          at org.apache.felix.framework.URLHandlersStreamHandlerProxy.openConnection(URLHandlersStreamHandlerProxy.java:303)

          at java.net.URL.openConnection(URL.java:945)

          at mycompany.feed.processor.loaders.URLLoaderHelper.loadData(URLLoaderHelper.java:63)

          ... 37 more

      Caused by: java.security.cert.CertificateException: Unable to initialize, java.io.IOException: DerInputStream.getLength(): lengthTag=6, too big.

          at sun.security.x509.X509CertImpl.(X509CertImpl.java:179)

          ... 68 more

       

       

      I thought it could be a problem with my certificate, however, i'm using the same jdk version in the stand alone app and on the feature I deployed on fuse but only the latter gives me this problem. I wonder if there's any special process to correctly deploy a certificate into a bundle within fuse or am i mising something else in here??.

       

       

      Thanks a lot in advance  :-D

        • 1. Re: Using certificates within Fuse
          Rogelio Sevilla Novice

          This was kind of a weird problem, it was pretty hard to find the solution. To be honest, I don't know if it has anything to do with Fuse. Anyway, just in case anyone else is experiencing the same, I solved it by removing this line from my code

           

          System.setProperty("javax.net.ssl.trustStore", "config/myKeyStoreFile")

           

           

          And loading the trustore using my own code, like this:

           

           

               System.clearProperty("javax.net.ssl.trustStore");

               SSLSocketFactory sslSocketFactory = null;

                  KeyStore ts = KeyStore.getInstance("JKS");

                  ts.load(new FileInputStream("myKeyStoreFile"),null);

                  TrustManager[] tm2;

                  TrustManagerFactory tmf2 =TrustManagerFactory.getInstance("SunX509",  "SunJSSE");

                  tmf2.init(ts);

                  tm2 = tmf2.getTrustManagers();

           

                  SSLContext sslContext = SSLContext.getInstance("SSL");

                  sslContext.init(null, tm2, null);

           

                  sslSocketFactory = sslContext.getSocketFactory();

             URL ur = new URL("my https url");       

             HttpsURLConnection conn = (HttpsURLConnection) ur.openConnection();

                   conn.setSSLSocketFactory(sslSocketFactory);

                  OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

           

           

          Doing this, I was able to visit and send requests to my https site.

           

          Hope this helps anyone out there :-D

           

          Edited by: rogelio_sevilla1 on Sep 21, 2011 10:57 PM

           

          Edited by: rogelio_sevilla1 on Sep 21, 2011 10:57 PM

          • 2. Re: Using certificates within Fuse
            Claus Ibsen Master

            Thanks for sharing the solution.