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

changeset 0
fb9019fb1bf7
equal deleted inserted replaced
-1:000000000000 0:a2df9e2ced1a
1 /**
2 * Copyright (c) 2012-2013, Gerald Garcia
3 *
4 * This file is part of Andoid Caldav Sync Adapter Free.
5 *
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute
7 * it and/or modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, either version 3 of the
9 * License, or at your option any later version.
10 *
11 * Andoid Caldav Sync Adapter Free is distributed in the hope that
12 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Andoid Caldav Sync Adapter Free.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package org.gege.caldavsyncadapter.caldav;
23
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27 import java.net.Socket;
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;
34
35 import javax.net.ssl.SSLContext;
36 import javax.net.ssl.SSLSocket;
37 import javax.net.ssl.TrustManager;
38 import javax.net.ssl.X509TrustManager;
39
40 import org.apache.http.conn.ConnectTimeoutException;
41 import org.apache.http.conn.scheme.LayeredSocketFactory;
42 import org.apache.http.conn.ssl.SSLSocketFactory;
43 import org.apache.http.params.HttpConnectionParams;
44 import org.apache.http.params.HttpParams;
45
46 import android.util.Log;
47
48
49
50 public final class EasySSLSocketFactory implements
51 LayeredSocketFactory {
52
53 private static final String TAG = "TrustAllSSLSocketFactory";
54
55 private static final EasySSLSocketFactory DEFAULT_FACTORY = new EasySSLSocketFactory();
56
57 public static EasySSLSocketFactory getSocketFactory() {
58 return DEFAULT_FACTORY;
59 }
60
61 private SSLContext sslcontext;
62 private javax.net.ssl.SSLSocketFactory socketfactory;
63
64 private EasySSLSocketFactory() {
65 super();
66 TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
67
68 @Override
69 public void checkClientTrusted(X509Certificate[] chain,
70 String authType) throws CertificateException {
71 // do nothing
72 }
73
74 @Override
75 public void checkServerTrusted(X509Certificate[] chain,
76 String authType) throws CertificateException {
77 // do nothing
78 }
79
80 @Override
81 public X509Certificate[] getAcceptedIssuers() {
82 return new X509Certificate[0];
83 }
84
85 } };
86 try {
87 this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
88 this.sslcontext.init(null, tm, new SecureRandom());
89 this.socketfactory = this.sslcontext.getSocketFactory();
90 } catch ( NoSuchAlgorithmException e ) {
91 Log.e(TAG,
92 "Faild to instantiate TrustAllSSLSocketFactory!", e);
93 } catch ( KeyManagementException e ) {
94 Log.e(TAG,
95 "Failed to instantiate TrustAllSSLSocketFactory!", e);
96 }
97 }
98
99 @Override
100 public Socket createSocket(Socket socket, String host, int port,
101 boolean autoClose) throws IOException, UnknownHostException {
102 SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(
103 socket, host, port, autoClose);
104 return sslSocket;
105 }
106
107 @Override
108 public Socket connectSocket(Socket sock, String host, int port,
109 InetAddress localAddress, int localPort, HttpParams params)
110 throws IOException, UnknownHostException, ConnectTimeoutException {
111 if ( host == null ) {
112 throw new IllegalArgumentException(
113 "Target host may not be null.");
114 }
115 if ( params == null ) {
116 throw new IllegalArgumentException(
117 "Parameters may not be null.");
118 }
119
120 SSLSocket sslsock = (SSLSocket) ( ( sock != null ) ? sock
121 : createSocket() );
122
123 if ( ( localAddress != null ) || ( localPort > 0 ) ) {
124
125 // we need to bind explicitly
126 if ( localPort < 0 ) {
127 localPort = 0; // indicates "any"
128 }
129
130 InetSocketAddress isa = new InetSocketAddress(localAddress,
131 localPort);
132 sslsock.bind(isa);
133 }
134
135 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
136 int soTimeout = HttpConnectionParams.getSoTimeout(params);
137
138 InetSocketAddress remoteAddress;
139 remoteAddress = new InetSocketAddress(host, port);
140
141 sslsock.connect(remoteAddress, connTimeout);
142
143 sslsock.setSoTimeout(soTimeout);
144
145 return sslsock;
146 }
147
148 @Override
149 public Socket createSocket() throws IOException {
150 // the cast makes sure that the factory is working as expected
151 return (SSLSocket) this.socketfactory.createSocket();
152 }
153
154 @Override
155 public boolean isSecure(Socket sock) throws IllegalArgumentException {
156 return true;
157 }
158
159 }

mercurial