src/org/gege/caldavsyncadapter/caldav/EasySSLSocketFactory.java

branch
ICAL4J_EMBED_1
changeset 33
a6e06f8d463c
parent 0
fb9019fb1bf7
equal deleted inserted replaced
0:828b81e76142 1:5e582d3598a7
19 * 19 *
20 */ 20 */
21 21
22 package org.gege.caldavsyncadapter.caldav; 22 package org.gege.caldavsyncadapter.caldav;
23 23
24 /*
25 * Licensed to the Apache Software Foundation (ASF) under one
26 * or more contributor license agreements. See the NOTICE file
27 * distributed with this work for additional information
28 * regarding copyright ownership. The ASF licenses this file
29 * to you under the Apache License, Version 2.0 (the
30 * "License"); you may not use this file except in compliance
31 * with the License. You may obtain a copy of the License at
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing,
36 * software distributed under the License is distributed on an
37 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
38 * KIND, either express or implied. See the License for the
39 * specific language governing permissions and limitations
40 * under the License.
41 */
42
43 import java.io.IOException; 24 import java.io.IOException;
44 import java.net.InetAddress; 25 import java.net.InetAddress;
45 import java.net.InetSocketAddress; 26 import java.net.InetSocketAddress;
46 import java.net.Socket; 27 import java.net.Socket;
47 import java.net.UnknownHostException; 28 import java.net.UnknownHostException;
29 import java.security.KeyManagementException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.SecureRandom;
32 import java.security.cert.CertificateException;
33 import java.security.cert.X509Certificate;
48 34
49 import javax.net.ssl.SSLContext; 35 import javax.net.ssl.SSLContext;
50 import javax.net.ssl.SSLSocket; 36 import javax.net.ssl.SSLSocket;
51 import javax.net.ssl.TrustManager; 37 import javax.net.ssl.TrustManager;
38 import javax.net.ssl.X509TrustManager;
52 39
53 import org.apache.http.conn.ConnectTimeoutException; 40 import org.apache.http.conn.ConnectTimeoutException;
54 import org.apache.http.conn.scheme.LayeredSocketFactory; 41 import org.apache.http.conn.scheme.LayeredSocketFactory;
55 import org.apache.http.conn.scheme.SocketFactory; 42 import org.apache.http.conn.ssl.SSLSocketFactory;
56 import org.apache.http.params.HttpConnectionParams; 43 import org.apache.http.params.HttpConnectionParams;
57 import org.apache.http.params.HttpParams; 44 import org.apache.http.params.HttpParams;
58 45
59 /** 46 import android.util.Log;
60 * This socket factory will create ssl socket that accepts self signed
61 * certificate
62 *
63 * @author olamy
64 * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse
65 * $
66 * @since 1.2.3
67 */
68 public class CopyOfEasySSLSocketFactory implements SocketFactory,
69 LayeredSocketFactory {
70 47
71 private SSLContext sslcontext = null;
72 48
73 private static SSLContext createEasySSLContext() throws IOException {
74 try {
75 SSLContext context = SSLContext.getInstance("TLS");
76 context.init(null, new TrustManager[] { new EasyX509TrustManager(
77 null) }, null);
78 return context;
79 } catch (Exception e) {
80 throw new IOException(e.getMessage());
81 }
82 }
83 49
84 private SSLContext getSSLContext() throws IOException { 50 public final class EasySSLSocketFactory implements
85 if (this.sslcontext == null) { 51 LayeredSocketFactory {
86 this.sslcontext = createEasySSLContext();
87 }
88 return this.sslcontext;
89 }
90 52
91 /** 53 private static final String TAG = "TrustAllSSLSocketFactory";
92 * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, 54
93 * java.lang.String, int, java.net.InetAddress, int, 55 private static final EasySSLSocketFactory DEFAULT_FACTORY = new EasySSLSocketFactory();
94 * org.apache.http.params.HttpParams)
95 */
96 public Socket connectSocket(Socket sock, String host, int port,
97 InetAddress localAddress, int localPort, HttpParams params)
98 throws IOException, UnknownHostException, ConnectTimeoutException {
99 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
100 int soTimeout = HttpConnectionParams.getSoTimeout(params);
101 56
102 InetSocketAddress remoteAddress = new InetSocketAddress(host, port); 57 public static EasySSLSocketFactory getSocketFactory
103 SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket()); 58 () {
59 return DEFAULT_FACTORY;
60 }
104 61
105 if ((localAddress != null) || (localPort > 0)) { 62 private SSLContext sslcontext;
106 // we need to bind explicitly 63 private javax.net.ssl.SSLSocketFactory socketfactory;
107 if (localPort < 0) {
108 localPort = 0; // indicates "any"
109 }
110 InetSocketAddress isa = new InetSocketAddress(localAddress,
111 localPort);
112 sslsock.bind(isa);
113 }
114 64
115 sslsock.connect(remoteAddress, connTimeout); 65 private EasySSLSocketFactory() {
116 sslsock.setSoTimeout(soTimeout); 66 super();
117 return sslsock; 67 TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
118 68
119 } 69 @Override
70 public void checkClientTrusted(X509Certificate[] chain,
71 String authType) throws CertificateException {
72 // do nothing
73 }
120 74
121 /** 75 @Override
122 * @see org.apache.http.conn.scheme.SocketFactory#createSocket() 76 public void checkServerTrusted(X509Certificate[] chain,
123 */ 77 String authType) throws CertificateException {
124 public Socket createSocket() throws IOException { 78 // do nothing
125 return getSSLContext().getSocketFactory().createSocket(); 79 }
126 }
127 80
128 /** 81 @Override
129 * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket) 82 public X509Certificate[] getAcceptedIssuers() {
130 */ 83 return new X509Certificate[0];
131 public boolean isSecure(Socket socket) throws IllegalArgumentException { 84 }
132 return true;
133 }
134 85
135 /** 86 } };
136 * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, 87 try {
137 * java.lang.String, int, boolean) 88 this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
138 */ 89 this.sslcontext.init(null, tm, new SecureRandom());
139 public Socket createSocket(Socket socket, String host, int port, 90 this.socketfactory = this.sslcontext.getSocketFactory();
140 boolean autoClose) throws IOException, UnknownHostException { 91 } catch ( NoSuchAlgorithmException e ) {
141 return getSSLContext().getSocketFactory().createSocket(); 92 Log.e(TAG,
142 } 93 "Faild to instantiate TrustAllSSLSocketFactory!", e);
94 } catch ( KeyManagementException e ) {
95 Log.e(TAG,
96 "Failed to instantiate TrustAllSSLSocketFactory!", e);
97 }
98 }
143 99
144 // ------------------------------------------------------------------- 100 @Override
145 // javadoc in org.apache.http.conn.scheme.SocketFactory says : 101 public Socket createSocket(Socket socket, String host, int port,
146 // Both Object.equals() and Object.hashCode() must be overridden 102 boolean autoClose) throws IOException, UnknownHostException {
147 // for the correct operation of some connection managers 103 SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(
148 // ------------------------------------------------------------------- 104 socket, host, port, autoClose);
105 return sslSocket;
106 }
149 107
150 public boolean equals(Object obj) { 108 @Override
151 return ((obj != null) && obj.getClass().equals( 109 public Socket connectSocket(Socket sock, String host, int port,
152 CopyOfEasySSLSocketFactory.class)); 110 InetAddress localAddress, int localPort, HttpParams params)
153 } 111 throws IOException, UnknownHostException, ConnectTimeoutException {
112 if ( host == null ) {
113 throw new IllegalArgumentException(
114 "Target host may not be null.");
115 }
116 if ( params == null ) {
117 throw new IllegalArgumentException(
118 "Parameters may not be null.");
119 }
154 120
155 public int hashCode() { 121 SSLSocket sslsock = (SSLSocket) ( ( sock != null ) ? sock
156 return CopyOfEasySSLSocketFactory.class.hashCode(); 122 : createSocket() );
157 } 123
124 if ( ( localAddress != null ) || ( localPort > 0 ) ) {
125
126 // we need to bind explicitly
127 if ( localPort < 0 ) {
128 localPort = 0; // indicates "any"
129 }
130
131 InetSocketAddress isa = new InetSocketAddress(localAddress,
132 localPort);
133 sslsock.bind(isa);
134 }
135
136 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
137 int soTimeout = HttpConnectionParams.getSoTimeout(params);
138
139 InetSocketAddress remoteAddress;
140 remoteAddress = new InetSocketAddress(host, port);
141
142 sslsock.connect(remoteAddress, connTimeout);
143
144 sslsock.setSoTimeout(soTimeout);
145
146 return sslsock;
147 }
148
149 @Override
150 public Socket createSocket() throws IOException {
151 // the cast makes sure that the factory is working as expected
152 return (SSLSocket) this.socketfactory.createSocket();
153 }
154
155 @Override
156 public boolean isSecure(Socket sock) throws IllegalArgumentException {
157 return true;
158 }
158 159
159 } 160 }

mercurial