1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/ssl/SimpleSSLTestProtocolSocketFactory.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient.ssl;
32  
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.net.InetAddress;
36  import java.net.Socket;
37  import java.net.URL;
38  import java.net.UnknownHostException;
39  import java.security.KeyStore;
40  
41  import org.apache.commons.httpclient.ConnectTimeoutException;
42  import org.apache.commons.httpclient.params.HttpConnectionParams;
43  import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
44  import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
45  import org.apache.commons.httpclient.server.SimpleSocketFactory;
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import com.sun.net.ssl.SSLContext;
50  import com.sun.net.ssl.TrustManager;
51  import com.sun.net.ssl.TrustManagerFactory;
52  
53  public class SimpleSSLTestProtocolSocketFactory implements SecureProtocolSocketFactory {
54  
55      private static final Log LOG = LogFactory.getLog(SimpleSSLTestProtocolSocketFactory.class);
56  
57      private static SSLContext SSLCONTEXT = null;
58      
59      private static SSLContext createSSLContext() {
60          try {
61              ClassLoader cl = SimpleSocketFactory.class.getClassLoader();
62              URL url = cl.getResource("org/apache/commons/httpclient/ssl/simpleserver.keystore");
63              KeyStore keystore  = KeyStore.getInstance("jks");
64              InputStream is = null;
65              try {
66              	is = url.openStream();
67              	keystore.load(is, "nopassword".toCharArray());
68              } finally {
69              	if (is != null) is.close();
70              }
71              TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
72                      TrustManagerFactory.getDefaultAlgorithm());
73              tmfactory.init(keystore);
74              TrustManager[] trustmanagers = tmfactory.getTrustManagers(); 
75              SSLContext sslcontext = SSLContext.getInstance("TLS");
76              sslcontext.init(null, trustmanagers, null);
77              return sslcontext;
78          } catch (Exception ex) {
79              // this is not the way a sane exception handling should be done
80              // but for our simple HTTP testing framework this will suffice
81              LOG.error(ex.getMessage(), ex);
82              throw new IllegalStateException(ex.getMessage());
83          }
84      
85      }
86      
87      private static SSLContext getSSLContext() {
88          if (SSLCONTEXT == null) {
89              SSLCONTEXT = createSSLContext();
90          }
91          return SSLCONTEXT;
92      }
93      
94      public SimpleSSLTestProtocolSocketFactory() {
95          super();
96      }
97      
98      public Socket createSocket(
99          final String host,
100         final int port,
101         final InetAddress localAddress,
102         final int localPort,
103         final HttpConnectionParams params
104     ) throws IOException, UnknownHostException, ConnectTimeoutException {
105         if (params == null) {
106             throw new IllegalArgumentException("Parameters may not be null");
107         }
108         int timeout = params.getConnectionTimeout();
109         if (timeout == 0) {
110             return createSocket(host, port, localAddress, localPort);
111         } else {
112             // To be eventually deprecated when migrated to Java 1.4 or above
113             return ControllerThreadSocketFactory.createSocket(
114                     this, host, port, localAddress, localPort, timeout);
115         }
116     }
117 
118     public Socket createSocket(
119         String host,
120         int port,
121         InetAddress clientHost,
122         int clientPort)
123         throws IOException, UnknownHostException
124    {
125        return getSSLContext().getSocketFactory().createSocket(
126             host,
127             port,
128             clientHost,
129             clientPort
130         );
131     }
132 
133     public Socket createSocket(String host, int port)
134         throws IOException, UnknownHostException
135     {
136         return getSSLContext().getSocketFactory().createSocket(
137             host,
138             port
139         );
140     }
141 
142     public Socket createSocket(
143         Socket socket,
144         String host,
145         int port,
146         boolean autoClose)
147         throws IOException, UnknownHostException
148     {
149         return getSSLContext().getSocketFactory().createSocket(
150             socket,
151             host,
152             port,
153             autoClose
154         );
155     }
156 }