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

Sat, 14 Feb 2015 21:29:32 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 14 Feb 2015 21:29:32 +0100
branch
ICAL4J_EMBED_1
changeset 33
a6e06f8d463c
parent 0
fb9019fb1bf7
permissions
-rw-r--r--

Merge manually from fa9cb0e70440, conclusively use Lolipop API level 21.

     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  */
    22 package org.gege.caldavsyncadapter.caldav;
    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;
    35 import javax.net.ssl.SSLContext;
    36 import javax.net.ssl.SSLSocket;
    37 import javax.net.ssl.TrustManager;
    38 import javax.net.ssl.X509TrustManager;
    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;
    46 import android.util.Log;
    50 public final class EasySSLSocketFactory implements
    51     LayeredSocketFactory {
    53 	private static final String TAG = "TrustAllSSLSocketFactory";
    55     private static final EasySSLSocketFactory DEFAULT_FACTORY = new EasySSLSocketFactory();
    57     public static EasySSLSocketFactory getSocketFactory
    58             () {
    59         return DEFAULT_FACTORY;
    60     }
    62     private SSLContext sslcontext;
    63     private javax.net.ssl.SSLSocketFactory socketfactory;
    65     private EasySSLSocketFactory() {
    66         super();
    67         TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
    69             @Override
    70             public void checkClientTrusted(X509Certificate[] chain,
    71                 String authType) throws CertificateException {
    72                 // do nothing
    73             }
    75             @Override
    76             public void checkServerTrusted(X509Certificate[] chain,
    77                 String authType) throws CertificateException {
    78                 // do nothing
    79             }
    81             @Override
    82             public X509Certificate[] getAcceptedIssuers() {
    83                 return new X509Certificate[0];
    84             }
    86         } };
    87         try {
    88             this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
    89             this.sslcontext.init(null, tm, new SecureRandom());
    90             this.socketfactory = this.sslcontext.getSocketFactory();
    91         } catch ( NoSuchAlgorithmException e ) {
    92             Log.e(TAG,
    93                 "Faild to instantiate TrustAllSSLSocketFactory!", e);
    94         } catch ( KeyManagementException e ) {
    95             Log.e(TAG,
    96                 "Failed to instantiate TrustAllSSLSocketFactory!", e);
    97         }
    98     }
   100     @Override
   101     public Socket createSocket(Socket socket, String host, int port,
   102         boolean autoClose) throws IOException, UnknownHostException {
   103         SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(
   104             socket, host, port, autoClose);
   105         return sslSocket;
   106     }
   108     @Override
   109     public Socket connectSocket(Socket sock, String host, int port,
   110         InetAddress localAddress, int localPort, HttpParams params)
   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         }
   121         SSLSocket sslsock = (SSLSocket) ( ( sock != null ) ? sock
   122             : createSocket() );
   124         if ( ( localAddress != null ) || ( localPort > 0 ) ) {
   126             // we need to bind explicitly
   127             if ( localPort < 0 ) {
   128                 localPort = 0; // indicates "any"
   129             }
   131             InetSocketAddress isa = new InetSocketAddress(localAddress,
   132                 localPort);
   133             sslsock.bind(isa);
   134         }
   136         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
   137         int soTimeout = HttpConnectionParams.getSoTimeout(params);
   139         InetSocketAddress remoteAddress;
   140         remoteAddress = new InetSocketAddress(host, port);
   142         sslsock.connect(remoteAddress, connTimeout);
   144         sslsock.setSoTimeout(soTimeout);
   146         return sslsock;
   147     }
   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     }
   155     @Override
   156     public boolean isSecure(Socket sock) throws IllegalArgumentException {
   157         return true;
   158     }
   160 }

mercurial