EasySSLSocketFactory.java 7.9 KB

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