6 * ====================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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 * ====================================================================
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/>.
31 package eu
.alefzero
.owncloud
.authenticator
;
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
;
40 import javax
.net
.SocketFactory
;
41 import javax
.net
.ssl
.SSLContext
;
42 import javax
.net
.ssl
.TrustManager
;
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
;
50 import android
.util
.Log
;
54 * EasySSLProtocolSocketFactory can be used to creats SSL {@link Socket}s
55 * that accept self-signed certificates.
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
65 * Example of using custom protocol socket factory for a specific host:
67 * Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
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);
79 * Example of using custom protocol socket factory per default instead of the standard one:
81 * Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
82 * Protocol.registerProtocol("https", easyhttps);
84 * HttpClient client = new HttpClient();
85 * GetMethod httpget = new GetMethod("https://localhost/");
86 * client.executeMethod(httpget);
90 * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
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.
99 public class EasySSLSocketFactory
implements ProtocolSocketFactory
{
101 private static final String TAG
= "EasySSLSocketFactory";
102 private SSLContext sslcontext
= null
;
105 * Constructor for EasySSLProtocolSocketFactory.
107 public EasySSLSocketFactory() {
111 private static SSLContext
createEasySSLContext() {
113 SSLContext context
= SSLContext
.getInstance("TLS");
116 new TrustManager
[] {new EasyX509TrustManager(null
)},
119 } catch (Exception er
) {
120 Log
.e(TAG
, er
.getMessage()+"");
121 throw new HttpClientError(er
.toString());
125 private SSLContext
getSSLContext() {
126 if (this.sslcontext
== null
) {
127 this.sslcontext
= createEasySSLContext();
129 return this.sslcontext
;
133 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
135 public Socket
createSocket(
138 InetAddress clientHost
,
140 throws IOException
, UnknownHostException
{
142 return getSSLContext().getSocketFactory().createSocket(
151 * Attempts to get a new socket connection to the given host within the given time limit.
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}
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}
165 * @return Socket a new socket
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
171 public Socket
createSocket(
174 final InetAddress localAddress
,
176 final HttpConnectionParams params
177 ) throws IOException
, UnknownHostException
, ConnectTimeoutException
{
178 if (params
== null
) {
179 throw new IllegalArgumentException("Parameters may not be null");
181 int timeout
= params
.getConnectionTimeout();
182 SocketFactory socketfactory
= getSSLContext().getSocketFactory();
184 return socketfactory
.createSocket(host
, port
, localAddress
, localPort
);
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
);
196 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
198 public Socket
createSocket(String host
, int port
)
199 throws IOException
, UnknownHostException
{
200 return getSSLContext().getSocketFactory().createSocket(
207 * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
209 public Socket
createSocket(
214 throws IOException
, UnknownHostException
{
215 return getSSLContext().getSocketFactory().createSocket(
223 public boolean equals(Object obj
) {
224 return ((obj
!= null
) && obj
.getClass().equals(EasySSLSocketFactory
.class));
227 public int hashCode() {
228 return EasySSLSocketFactory
.class.hashCode();