3c2eac7a428ca996f0e76feb18cfb5d8f7d35c0e
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / EasySSLSocketFactory.java
1 /*
2 * $HeadURL$
3 * $Revision$
4 * $Date$
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 eu.alefzero.owncloud.authenticator;
32
33 import java.io.IOException;
34 import java.net.InetAddress;
35 import java.net.InetSocketAddress;
36 import java.net.Socket;
37 import java.net.SocketAddress;
38 import java.net.UnknownHostException;
39
40 import javax.net.SocketFactory;
41 import javax.net.ssl.SSLContext;
42 import javax.net.ssl.TrustManager;
43
44 import org.apache.commons.httpclient.ConnectTimeoutException;
45 import org.apache.commons.httpclient.HttpClientError;
46 import org.apache.commons.httpclient.params.HttpConnectionParams;
47 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
48 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
49
50 import android.util.Log;
51
52 /**
53 * <p>
54 * EasySSLProtocolSocketFactory can be used to creats SSL {@link Socket}s that
55 * accept self-signed certificates.
56 * </p>
57 * <p>
58 * This socket factory SHOULD NOT be used for productive systems due to security
59 * reasons, unless it is a concious decision and you are perfectly aware of
60 * security implications of accepting self-signed certificates
61 * </p>
62 *
63 * <p>
64 * Example of using custom protocol socket factory for a specific host:
65 *
66 * <pre>
67 * Protocol easyhttps = new Protocol(&quot;https&quot;, new EasySSLProtocolSocketFactory(),
68 * 443);
69 *
70 * URI uri = new URI(&quot;https://localhost/&quot;, true);
71 * // use relative url only
72 * GetMethod httpget = new GetMethod(uri.getPathQuery());
73 * HostConfiguration hc = new HostConfiguration();
74 * hc.setHost(uri.getHost(), uri.getPort(), easyhttps);
75 * HttpClient client = new HttpClient();
76 * client.executeMethod(hc, httpget);
77 * </pre>
78 *
79 * </p>
80 * <p>
81 * Example of using custom protocol socket factory per default instead of the
82 * standard one:
83 *
84 * <pre>
85 * Protocol easyhttps = new Protocol(&quot;https&quot;, new EasySSLProtocolSocketFactory(),
86 * 443);
87 * Protocol.registerProtocol(&quot;https&quot;, easyhttps);
88 *
89 * HttpClient client = new HttpClient();
90 * GetMethod httpget = new GetMethod(&quot;https://localhost/&quot;);
91 * client.executeMethod(httpget);
92 * </pre>
93 *
94 * </p>
95 *
96 * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
97 *
98 * <p>
99 * DISCLAIMER: HttpClient developers DO NOT actively support this
100 * component. The component is provided as a reference material, which
101 * may be inappropriate for use without additional customization.
102 * </p>
103 */
104
105 public class EasySSLSocketFactory implements ProtocolSocketFactory {
106
107 private static final String TAG = "EasySSLSocketFactory";
108 private SSLContext sslcontext = null;
109
110 /**
111 * Constructor for EasySSLProtocolSocketFactory.
112 */
113 public EasySSLSocketFactory() {
114 super();
115 }
116
117 private static SSLContext createEasySSLContext() {
118 try {
119 SSLContext context = SSLContext.getInstance("TLS");
120 context.init(null, new TrustManager[] { new EasyX509TrustManager(
121 null) }, null);
122 return context;
123 } catch (Exception er) {
124 Log.e(TAG, er.getMessage() + "");
125 throw new HttpClientError(er.toString());
126 }
127 }
128
129 private SSLContext getSSLContext() {
130 if (this.sslcontext == null) {
131 this.sslcontext = createEasySSLContext();
132 }
133 return this.sslcontext;
134 }
135
136 /**
137 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
138 */
139 public Socket createSocket(String host, int port, InetAddress clientHost,
140 int clientPort) throws IOException, UnknownHostException {
141
142 return getSSLContext().getSocketFactory().createSocket(host, port,
143 clientHost, clientPort);
144 }
145
146 /**
147 * Attempts to get a new socket connection to the given host within the
148 * given time limit.
149 * <p>
150 * To circumvent the limitations of older JREs that do not support connect
151 * timeout a controller thread is executed. The controller thread attempts
152 * to create a new socket within the given limit of time. If socket
153 * constructor does not return until the timeout expires, the controller
154 * terminates and throws an {@link ConnectTimeoutException}
155 * </p>
156 *
157 * @param host
158 * the host name/IP
159 * @param port
160 * the port on the host
161 * @param clientHost
162 * the local host name/IP to bind the socket to
163 * @param clientPort
164 * the port on the local machine
165 * @param params
166 * {@link HttpConnectionParams Http connection parameters}
167 *
168 * @return Socket a new socket
169 *
170 * @throws IOException
171 * if an I/O error occurs while creating the socket
172 * @throws UnknownHostException
173 * if the IP address of the host cannot be determined
174 */
175 public Socket createSocket(final String host, final int port,
176 final InetAddress localAddress, final int localPort,
177 final HttpConnectionParams params) throws IOException,
178 UnknownHostException, ConnectTimeoutException {
179 if (params == null) {
180 throw new IllegalArgumentException("Parameters may not be null");
181 }
182 int timeout = params.getConnectionTimeout();
183 SocketFactory socketfactory = getSSLContext().getSocketFactory();
184 if (timeout == 0) {
185 return socketfactory.createSocket(host, port, localAddress,
186 localPort);
187 } else {
188 Socket socket = socketfactory.createSocket();
189 SocketAddress localaddr = new InetSocketAddress(localAddress,
190 localPort);
191 SocketAddress remoteaddr = new InetSocketAddress(host, port);
192 socket.bind(localaddr);
193 socket.connect(remoteaddr, timeout);
194 return socket;
195 }
196 }
197
198 /**
199 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
200 */
201 public Socket createSocket(String host, int port) throws IOException,
202 UnknownHostException {
203 return getSSLContext().getSocketFactory().createSocket(host, port);
204 }
205
206 /**
207 * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
208 */
209 public Socket createSocket(Socket socket, String host, int port,
210 boolean autoClose) throws IOException, UnknownHostException {
211 return getSSLContext().getSocketFactory().createSocket(socket, host,
212 port, autoClose);
213 }
214
215 public boolean equals(Object obj) {
216 return ((obj != null) && obj.getClass().equals(
217 EasySSLSocketFactory.class));
218 }
219
220 public int hashCode() {
221 return EasySSLSocketFactory.class.hashCode();
222 }
223
224 }