Changing the SSL socket factory for https:// URLs

Florian Weimer <[email protected]> Tue, 07 Jul 2009 16:17:44 +0000
Newsgroups gmane.comp.web.httpunit.devel
Message-ID <[email protected]>
I don't quite understand why httpunit defaults to an old collection of
protocol handlers with a different inheritance hierarchy.

If I change the following line:

Index: src/com/meterware/httpunit/HttpsProtocolSupport.java
===================================================================
--- src/com/meterware/httpunit/HttpsProtocolSupport.java	(revision 3326)
+++ src/com/meterware/httpunit/HttpsProtocolSupport.java	(working copy)
@@ -48,7 +48,7 @@
     /** The name of the JSSE class which provides support for SSL. **/
     private static String JSSE_PROVIDER_CLASS=SunJSSE_PROVIDER_CLASS;
     /** The name of the JSSE class which supports the https protocol. **/
-    private static String SSL_PROTOCOL_HANDLER   = SunSSL_PROTOCOL_HANDLER ;
+    private static String SSL_PROTOCOL_HANDLER   = "sun.net.ssl.internal.www.protocol";
 
     private static Class _httpsProviderClass;
 

the URLConnection object returned by URL#openConnection() is of the
right type (a subclass of the public class
javax.net.ssl.HttpsURLConnection), so that it's possible to apply the
following patch to add a new client property which permits changing
the SSL socket factory (thus providing a way to enable client-side
certificates or to disable server certificate checking):

Index: src/com/meterware/httpunit/ClientProperties.java
===================================================================
--- src/com/meterware/httpunit/ClientProperties.java	(revision 3326)
+++ src/com/meterware/httpunit/ClientProperties.java	(working copy)
@@ -1,4 +1,6 @@
 package com.meterware.httpunit;
+
+import javax.net.ssl.SSLSocketFactory;
 /********************************************************************************************************************
  * $Id: ClientProperties.java 797 2007-12-28 15:16:07Z wolfgang_fahl $
  *
@@ -237,7 +239,6 @@
         _dnsListener = dnsListener;
     }
 
-
     /**
      * Returns the listener for DNS requests to be used by the client.
      * @return the currently specified DNS listener, or null if none is specified.
@@ -246,6 +247,14 @@
         return _dnsListener;
     }
     
+    public void setSSLSocketFactory( SSLSocketFactory sslSocketFactory ) {
+    _sslSocketFactory = sslSocketFactory;
+    }
+    
+    public SSLSocketFactory getSSLSocketFactory() {
+    return _sslSocketFactory;
+    }
+
		/**
			 * @return the whether Referer information should be stripped from the
 			    * header
@@ -285,6 +294,7 @@
     private boolean _autoRefresh   = false;
 
     private DNSListener _dnsListener;
+    private SSLSocketFactory _sslSocketFactory;
     private boolean _sendReferer;
 
     private static ClientProperties _defaultProperties = new ClientProperties();

Index: src/com/meterware/httpunit/WebConversation.java
===================================================================
--- src/com/meterware/httpunit/WebConversation.java	(revision 3326)
+++ src/com/meterware/httpunit/WebConversation.java	(working copy)
@@ -30,7 +30,9 @@
 import java.util.Enumeration;
 import java.util.Properties;
 
+import javax.net.ssl.HttpsURLConnection;
 
+
 /**
  * The context for a series of HTTP requests. This class manages cookies used to maintain
  * session context, computes relative URLs, and generally emulates the browser behavior
@@ -163,7 +165,12 @@
      * @param url - the url to use
      */
     private URLConnection openConnection( URL url ) throws MalformedURLException, IOException {
-        URLConnection connection = url.openConnection();
+	 URLConnection connection = url.openConnection();
+        if (connection instanceof HttpsURLConnection 
+           && getClientProperties().getSSLSocketFactory() != null) {
+            ((HttpsURLConnection) connection).setSSLSocketFactory(
+                getClientProperties().getSSLSocketFactory());
+        }
         if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection).setInstanceFollowRedirects( false );
         connection.setUseCaches( false );
         return connection;

Are there really still JVMs where SSL/TLS doesn't work out of the box
for https:// URLs?  Is it still necessary to tamper with URL protocol
handlers?

-- 
Florian Weimer                <[email protected]>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have 
the opportunity to enter the BlackBerry Developer Challenge. See full prize 
details at: http://p.sf.net/sfu/blackberry