javascriptjavarhino

Implement own TrustManager in Javascript (Rhino engine)


I am trying to do implement my own TrustManager in Javascript, but I have no idea how to implement it.

In Java I have the following :

    TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

I tried to use the following for the X509TrustManager :

js> obj = { getAcceptedIssuers : function() { return null; }, checkClientTrusted: function() { }, checkServerTrusted: function() { } }
[object Object]
js> x509tm = new javax.net.ssl.X509TrustManager(obj)
adapter1@2eee9593
js>

Then I dont know how to create the TrustManager. How to do this in Javascript (Rhino 1.6 release 7 2008 01 02)?


Solution

  • After many hours, I finally managed to implement it.

    Here is the code corresponding to above Java part :

        obj = { getAcceptedIssuers : function() { return null; }, checkClientTrusted: function() { return; }, checkServerTrusted: function() { return; } };
    var o = new JavaAdapter(javax.net.ssl.X509TrustManager, obj);
    var oo = new Array();
    oo.push(o);
    

    Then to use it, everything becomes easy :

    var sc = javax.net.ssl.SSLContext.getInstance("SSL");
        sc.init(null, oo, new java.security.SecureRandom());
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    

    Hope this will help somebody in the same situation.