~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/transport/http/ssl/HTTPSClientInvoker.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
 
23
 
package org.jboss.remoting.transport.http.ssl;
24
 
 
25
 
import org.jboss.remoting.Client;
26
 
import org.jboss.remoting.InvokerLocator;
27
 
import org.jboss.remoting.serialization.ClassLoaderUtility;
28
 
import org.jboss.remoting.security.CustomSSLSocketFactory;
29
 
import org.jboss.remoting.security.SSLSocketBuilder;
30
 
import org.jboss.remoting.security.SSLSocketBuilderMBean;
31
 
import org.jboss.remoting.socketfactory.SocketFactoryWrapper;
32
 
import org.jboss.remoting.transport.http.HTTPClientInvoker;
33
 
import javax.net.SocketFactory;
34
 
import javax.net.ssl.HandshakeCompletedListener;
35
 
import javax.net.ssl.HostnameVerifier;
36
 
import javax.net.ssl.HttpsURLConnection;
37
 
import javax.net.ssl.SSLSession;
38
 
import javax.net.ssl.SSLSocketFactory;
39
 
 
40
 
import java.io.IOException;
41
 
import java.lang.reflect.Constructor;
42
 
import java.net.HttpURLConnection;
43
 
import java.security.AccessController;
44
 
import java.security.PrivilegedAction;
45
 
import java.util.Map;
46
 
 
47
 
/**
48
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
49
 
 */
50
 
public class HTTPSClientInvoker extends HTTPClientInvoker
51
 
{
52
 
   /**
53
 
    * A property to override the default https url host verification
54
 
    */
55
 
   public static final String IGNORE_HTTPS_HOST = "org.jboss.security.ignoreHttpsHost";
56
 
   public static final String HOSTNAME_VERIFIER = "hostnameVerifier";
57
 
 
58
 
   public HTTPSClientInvoker(InvokerLocator locator)
59
 
   {
60
 
      super(locator);
61
 
   }
62
 
 
63
 
   public HTTPSClientInvoker(InvokerLocator locator, Map configuration)
64
 
   {
65
 
      super(locator, configuration);
66
 
   }
67
 
 
68
 
   protected String validateURL(String url)
69
 
   {
70
 
      String validatedUrl = url;
71
 
 
72
 
      if (validatedUrl.startsWith("servlet"))
73
 
      {
74
 
         // servlet:// is a valid protocol, but only in the remoting world, so need to convert to http
75
 
         validatedUrl = "http" + validatedUrl.substring("servlet".length());
76
 
      }
77
 
      else if(validatedUrl.startsWith("sslservlet"))
78
 
      {
79
 
         // sslservlet:// is a valid protocol, but only in the remoting world, so need to convert to https
80
 
         validatedUrl = "https" + validatedUrl.substring("sslservlet".length());
81
 
      }
82
 
      return validatedUrl;
83
 
   }
84
 
 
85
 
 
86
 
   /**
87
 
    * Checks to see if org.jboss.security.ignoreHttpHost property is set to true, and if it
88
 
    * is, will ste the host name verifier so that will accept any host.
89
 
    *
90
 
    * @return
91
 
    * @throws IOException
92
 
    */
93
 
   protected HttpURLConnection createURLConnection(String url, Map metadata) throws IOException
94
 
   {
95
 
      HttpURLConnection conn = super.createURLConnection(url, metadata);
96
 
 
97
 
      if (conn instanceof HttpsURLConnection)
98
 
      {
99
 
         HttpsURLConnection sconn = (HttpsURLConnection) conn;
100
 
 
101
 
         SocketFactory socketFactory = getSocketFactory();
102
 
         if (socketFactory != null && socketFactory instanceof SSLSocketFactory)
103
 
         {
104
 
            SSLSocketFactory sslSocketFactory = getHandshakeCompatibleFactory((SSLSocketFactory) socketFactory, metadata);
105
 
            sconn.setSSLSocketFactory(sslSocketFactory);
106
 
         }
107
 
 
108
 
         setHostnameVerifier(sconn, metadata);
109
 
      }
110
 
 
111
 
      return conn;
112
 
   }
113
 
 
114
 
   private SSLSocketFactory getHandshakeCompatibleFactory(SSLSocketFactory socketFactory, Map metadata)
115
 
   {
116
 
      SSLSocketFactory sslSocketFactory = socketFactory;
117
 
 
118
 
      // need to check for handshake listener and add them if there is one
119
 
      Object obj = configuration.get(Client.HANDSHAKE_COMPLETED_LISTENER);
120
 
      if (obj != null && obj instanceof HandshakeCompletedListener)
121
 
      {
122
 
         HandshakeCompletedListener listener = (HandshakeCompletedListener) obj;
123
 
         sslSocketFactory = new HTTPSSocketFactory(socketFactory, listener);
124
 
      }
125
 
      return sslSocketFactory;
126
 
   }
127
 
 
128
 
 
129
 
   protected SocketFactory createSocketFactory(Map configuration)
130
 
   {
131
 
      SocketFactory sf = super.createSocketFactory(configuration);
132
 
      
133
 
      if (isCompleteSocketFactory(sf))
134
 
         return sf;
135
 
      
136
 
      SocketFactory wrapper = sf;
137
 
 
138
 
      try
139
 
      {
140
 
         SSLSocketBuilder server = new SSLSocketBuilder(configuration);
141
 
         server.setUseSSLSocketFactory(false);
142
 
         sf = server.createSSLSocketFactory();
143
 
      }
144
 
      catch (Exception e)
145
 
      {
146
 
         log.error("Error creating SSL Socket Factory for client invoker: " + e.getMessage());
147
 
         log.debug("Error creating SSL Socket Factory for client invoker.", e);
148
 
      }
149
 
 
150
 
      if (wrapper != null)
151
 
      {
152
 
         ((SocketFactoryWrapper) wrapper).setSocketFactory(sf);
153
 
         return wrapper;
154
 
      }
155
 
      
156
 
      return sf;
157
 
   }
158
 
 
159
 
   protected void setHostnameVerifier(HttpsURLConnection conn, Map metadata)
160
 
   {
161
 
      HostnameVerifier hostnameVerifier = null;
162
 
 
163
 
      // First look for specific HostnameVerifier classname.
164
 
      String hostnameVerifierString = (String)metadata.get(HOSTNAME_VERIFIER);
165
 
      if (hostnameVerifierString == null || hostnameVerifierString.length() == 0)
166
 
         hostnameVerifierString = (String)configuration.get(HOSTNAME_VERIFIER);
167
 
      if(hostnameVerifierString != null && hostnameVerifierString.length() > 0)
168
 
      {
169
 
         try
170
 
         {
171
 
            Class cl = ClassLoaderUtility.loadClass(hostnameVerifierString, getClass());
172
 
            Constructor constructor = cl.getConstructor(new Class[]{});
173
 
            hostnameVerifier = (HostnameVerifier) constructor.newInstance(new Object[] {});
174
 
            log.trace("HostnameVerifier (" + hostnameVerifierString + ") loaded");
175
 
         }
176
 
         catch(Exception e)
177
 
         {
178
 
            log.debug("Could not create server socket factory by classname (" + hostnameVerifierString + ").  Error message: " + e.getMessage());
179
 
         }
180
 
      }
181
 
 
182
 
      // If we still don't have a HostnameVerifier, look for directive to ignore host name.
183
 
      if (hostnameVerifier == null)
184
 
      {  
185
 
         Boolean b = (Boolean) AccessController.doPrivileged( new PrivilegedAction()
186
 
         {
187
 
            public Object run()
188
 
            {
189
 
               return new Boolean(Boolean.getBoolean(IGNORE_HTTPS_HOST));
190
 
            }
191
 
         });
192
 
         
193
 
         boolean ignoreHTTPSHost = b.booleanValue();
194
 
         String ignoreHost = (String) metadata.get(IGNORE_HTTPS_HOST);
195
 
         if (ignoreHost != null && ignoreHost.length() > 0)
196
 
         {
197
 
            ignoreHTTPSHost = Boolean.valueOf(ignoreHost).booleanValue();
198
 
         }
199
 
         else
200
 
         {
201
 
            ignoreHost = (String) configuration.get(IGNORE_HTTPS_HOST);
202
 
            if (ignoreHost != null && ignoreHost.length() > 0)
203
 
            {
204
 
               ignoreHTTPSHost = Boolean.valueOf(ignoreHost).booleanValue();
205
 
            }
206
 
         }
207
 
         if (ignoreHTTPSHost)
208
 
         {
209
 
            hostnameVerifier = new AnyhostVerifier();
210
 
         }
211
 
      }
212
 
 
213
 
      // If we still don't have a HostnameVerifier, see if the SocketFactory is an instance of
214
 
      // org.jboss.remoting.security.CustomSSLSocketFactory, and, if so, if it has turned off
215
 
      // authentication.
216
 
      if (hostnameVerifier == null)
217
 
      {
218
 
         if (getSocketFactory() instanceof CustomSSLSocketFactory)
219
 
         {
220
 
            CustomSSLSocketFactory sf = (CustomSSLSocketFactory) getSocketFactory();
221
 
            SSLSocketBuilderMBean builder = sf.getSSLSocketBuilder();
222
 
            if (( builder.isSocketUseClientMode() && !builder.isServerAuthMode())
223
 
             || (!builder.isSocketUseClientMode() &&  builder.isClientAuthModeNone()))
224
 
               hostnameVerifier = new AnyhostVerifier();
225
 
         }
226
 
      }
227
 
 
228
 
      if (hostnameVerifier != null)
229
 
         conn.setHostnameVerifier(hostnameVerifier);
230
 
   }
231
 
 
232
 
   protected class AnyhostVerifier implements HostnameVerifier
233
 
   {
234
 
 
235
 
      public boolean verify(String s, SSLSession sslSession)
236
 
      {
237
 
         return true;
238
 
      }
239
 
   }
240
 
}
 
 
b'\\ No newline at end of file'