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