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