EasySSLSocketFactory.java 7.8 KB

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