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

changeset 0
fb9019fb1bf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/EasySSLSocketFactory.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012-2013, Gerald Garcia
     1.6 + * 
     1.7 + * This file is part of Andoid Caldav Sync Adapter Free.
     1.8 + *
     1.9 + * Andoid Caldav Sync Adapter Free is free software: you can redistribute 
    1.10 + * it and/or modify it under the terms of the GNU General Public License 
    1.11 + * as published by the Free Software Foundation, either version 3 of the 
    1.12 + * License, or at your option any later version.
    1.13 + *
    1.14 + * Andoid Caldav Sync Adapter Free is distributed in the hope that 
    1.15 + * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
    1.16 + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with Andoid Caldav Sync Adapter Free.  
    1.21 + * If not, see <http://www.gnu.org/licenses/>.
    1.22 + * 
    1.23 + */
    1.24 +
    1.25 +package org.gege.caldavsyncadapter.caldav;
    1.26 +
    1.27 +import java.io.IOException;
    1.28 +import java.net.InetAddress;
    1.29 +import java.net.InetSocketAddress;
    1.30 +import java.net.Socket;
    1.31 +import java.net.UnknownHostException;
    1.32 +import java.security.KeyManagementException;
    1.33 +import java.security.NoSuchAlgorithmException;
    1.34 +import java.security.SecureRandom;
    1.35 +import java.security.cert.CertificateException;
    1.36 +import java.security.cert.X509Certificate;
    1.37 +
    1.38 +import javax.net.ssl.SSLContext;
    1.39 +import javax.net.ssl.SSLSocket;
    1.40 +import javax.net.ssl.TrustManager;
    1.41 +import javax.net.ssl.X509TrustManager;
    1.42 +
    1.43 +import org.apache.http.conn.ConnectTimeoutException;
    1.44 +import org.apache.http.conn.scheme.LayeredSocketFactory;
    1.45 +import org.apache.http.conn.ssl.SSLSocketFactory;
    1.46 +import org.apache.http.params.HttpConnectionParams;
    1.47 +import org.apache.http.params.HttpParams;
    1.48 +
    1.49 +import android.util.Log;
    1.50 +
    1.51 +
    1.52 +
    1.53 +public final class EasySSLSocketFactory implements
    1.54 +    LayeredSocketFactory {
    1.55 +
    1.56 +	private static final String TAG = "TrustAllSSLSocketFactory";
    1.57 +	
    1.58 +    private static final EasySSLSocketFactory DEFAULT_FACTORY = new EasySSLSocketFactory();
    1.59 +
    1.60 +    public static EasySSLSocketFactory getSocketFactory() {
    1.61 +        return DEFAULT_FACTORY;
    1.62 +    }
    1.63 +
    1.64 +    private SSLContext sslcontext;
    1.65 +    private javax.net.ssl.SSLSocketFactory socketfactory;
    1.66 +
    1.67 +    private EasySSLSocketFactory() {
    1.68 +        super();
    1.69 +        TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
    1.70 +
    1.71 +            @Override
    1.72 +            public void checkClientTrusted(X509Certificate[] chain,
    1.73 +                String authType) throws CertificateException {
    1.74 +                // do nothing
    1.75 +            }
    1.76 +
    1.77 +            @Override
    1.78 +            public void checkServerTrusted(X509Certificate[] chain,
    1.79 +                String authType) throws CertificateException {
    1.80 +                // do nothing
    1.81 +            }
    1.82 +
    1.83 +            @Override
    1.84 +            public X509Certificate[] getAcceptedIssuers() {
    1.85 +                return new X509Certificate[0];
    1.86 +            }
    1.87 +
    1.88 +        } };
    1.89 +        try {
    1.90 +            this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
    1.91 +            this.sslcontext.init(null, tm, new SecureRandom());
    1.92 +            this.socketfactory = this.sslcontext.getSocketFactory();
    1.93 +        } catch ( NoSuchAlgorithmException e ) {
    1.94 +            Log.e(TAG,
    1.95 +                "Faild to instantiate TrustAllSSLSocketFactory!", e);
    1.96 +        } catch ( KeyManagementException e ) {
    1.97 +            Log.e(TAG,
    1.98 +                "Failed to instantiate TrustAllSSLSocketFactory!", e);
    1.99 +        }
   1.100 +    }
   1.101 +
   1.102 +    @Override
   1.103 +    public Socket createSocket(Socket socket, String host, int port,
   1.104 +        boolean autoClose) throws IOException, UnknownHostException {
   1.105 +        SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(
   1.106 +            socket, host, port, autoClose);
   1.107 +        return sslSocket;
   1.108 +    }
   1.109 +
   1.110 +    @Override
   1.111 +    public Socket connectSocket(Socket sock, String host, int port,
   1.112 +        InetAddress localAddress, int localPort, HttpParams params)
   1.113 +        throws IOException, UnknownHostException, ConnectTimeoutException {
   1.114 +        if ( host == null ) {
   1.115 +            throw new IllegalArgumentException(
   1.116 +                "Target host may not be null.");
   1.117 +        }
   1.118 +        if ( params == null ) {
   1.119 +            throw new IllegalArgumentException(
   1.120 +                "Parameters may not be null.");
   1.121 +        }
   1.122 +
   1.123 +        SSLSocket sslsock = (SSLSocket) ( ( sock != null ) ? sock
   1.124 +            : createSocket() );
   1.125 +
   1.126 +        if ( ( localAddress != null ) || ( localPort > 0 ) ) {
   1.127 +
   1.128 +            // we need to bind explicitly
   1.129 +            if ( localPort < 0 ) {
   1.130 +                localPort = 0; // indicates "any"
   1.131 +            }
   1.132 +
   1.133 +            InetSocketAddress isa = new InetSocketAddress(localAddress,
   1.134 +                localPort);
   1.135 +            sslsock.bind(isa);
   1.136 +        }
   1.137 +
   1.138 +        int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
   1.139 +        int soTimeout = HttpConnectionParams.getSoTimeout(params);
   1.140 +
   1.141 +        InetSocketAddress remoteAddress;
   1.142 +        remoteAddress = new InetSocketAddress(host, port);
   1.143 +
   1.144 +        sslsock.connect(remoteAddress, connTimeout);
   1.145 +
   1.146 +        sslsock.setSoTimeout(soTimeout);
   1.147 +
   1.148 +        return sslsock;
   1.149 +    }
   1.150 +
   1.151 +    @Override
   1.152 +    public Socket createSocket() throws IOException {
   1.153 +        // the cast makes sure that the factory is working as expected
   1.154 +        return (SSLSocket) this.socketfactory.createSocket();
   1.155 +    }
   1.156 +
   1.157 +    @Override
   1.158 +    public boolean isSecure(Socket sock) throws IllegalArgumentException {
   1.159 +        return true;
   1.160 +    }
   1.161 +
   1.162 +}
   1.163 \ No newline at end of file

mercurial