https for unsigned certificates in logging and uploading
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / EasySSLSocketFactory.java
1 package eu.alefzero.owncloud.authenticator;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.net.UnknownHostException;
27
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.SSLSocket;
30 import javax.net.ssl.TrustManager;
31
32 import org.apache.http.conn.ConnectTimeoutException;
33 import org.apache.http.conn.scheme.LayeredSocketFactory;
34 import org.apache.http.conn.scheme.SocketFactory;
35 import org.apache.http.params.HttpConnectionParams;
36 import org.apache.http.params.HttpParams;
37
38 /**
39 * This socket factory will create ssl socket that accepts self signed
40 * certificate
41 *
42 * @author olamy
43 * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse
44 * $
45 * @since 1.2.3
46 */
47 public class EasySSLSocketFactory implements SocketFactory,
48 LayeredSocketFactory {
49
50 private SSLContext sslcontext = null;
51
52 private static SSLContext createEasySSLContext() throws IOException {
53 try {
54 SSLContext context = SSLContext.getInstance("TLS");
55 context.init(null, new TrustManager[] { new EasyX509TrustManager(
56 null) }, null);
57 return context;
58 } catch (Exception e) {
59 throw new IOException(e.getMessage());
60 }
61 }
62
63 private SSLContext getSSLContext() throws IOException {
64 if (this.sslcontext == null) {
65 this.sslcontext = createEasySSLContext();
66 }
67 return this.sslcontext;
68 }
69
70 /**
71 * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket,
72 * java.lang.String, int, java.net.InetAddress, int,
73 * org.apache.http.params.HttpParams)
74 */
75 public Socket connectSocket(Socket sock, String host, int port,
76 InetAddress localAddress, int localPort, HttpParams params)
77 throws IOException, UnknownHostException, ConnectTimeoutException {
78 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
79 int soTimeout = HttpConnectionParams.getSoTimeout(params);
80
81 InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
82 SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
83
84 if ((localAddress != null) || (localPort > 0)) {
85 // we need to bind explicitly
86 if (localPort < 0) {
87 localPort = 0; // indicates "any"
88 }
89 InetSocketAddress isa = new InetSocketAddress(localAddress,
90 localPort);
91 sslsock.bind(isa);
92 }
93
94 sslsock.connect(remoteAddress, connTimeout);
95 sslsock.setSoTimeout(soTimeout);
96 return sslsock;
97
98 }
99
100 /**
101 * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
102 */
103 public Socket createSocket() throws IOException {
104 return getSSLContext().getSocketFactory().createSocket();
105 }
106
107 /**
108 * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket)
109 */
110 public boolean isSecure(Socket socket) throws IllegalArgumentException {
111 return true;
112 }
113
114 /**
115 * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket,
116 * java.lang.String, int, boolean)
117 */
118 public Socket createSocket(Socket socket, String host, int port,
119 boolean autoClose) throws IOException, UnknownHostException {
120 return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
121 }
122
123 // -------------------------------------------------------------------
124 // javadoc in org.apache.http.conn.scheme.SocketFactory says :
125 // Both Object.equals() and Object.hashCode() must be overridden
126 // for the correct operation of some connection managers
127 // -------------------------------------------------------------------
128
129 public boolean equals(Object obj) {
130 return ((obj != null) && obj.getClass().equals(
131 EasySSLSocketFactory.class));
132 }
133
134 public int hashCode() {
135 return EasySSLSocketFactory.class.hashCode();
136 }
137
138 }