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

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

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@0 29 import java.security.KeyManagementException;
michael@0 30 import java.security.NoSuchAlgorithmException;
michael@0 31 import java.security.SecureRandom;
michael@0 32 import java.security.cert.CertificateException;
michael@0 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@0 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@0 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@0 46 import android.util.Log;
michael@0 47
michael@0 48
michael@0 49
michael@0 50 public final class EasySSLSocketFactory implements
michael@0 51 LayeredSocketFactory {
michael@0 52
michael@0 53 private static final String TAG = "TrustAllSSLSocketFactory";
michael@0 54
michael@0 55 private static final EasySSLSocketFactory DEFAULT_FACTORY = new EasySSLSocketFactory();
michael@0 56
michael@0 57 public static EasySSLSocketFactory getSocketFactory() {
michael@0 58 return DEFAULT_FACTORY;
michael@0 59 }
michael@0 60
michael@0 61 private SSLContext sslcontext;
michael@0 62 private javax.net.ssl.SSLSocketFactory socketfactory;
michael@0 63
michael@0 64 private EasySSLSocketFactory() {
michael@0 65 super();
michael@0 66 TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
michael@0 67
michael@0 68 @Override
michael@0 69 public void checkClientTrusted(X509Certificate[] chain,
michael@0 70 String authType) throws CertificateException {
michael@0 71 // do nothing
michael@0 72 }
michael@0 73
michael@0 74 @Override
michael@0 75 public void checkServerTrusted(X509Certificate[] chain,
michael@0 76 String authType) throws CertificateException {
michael@0 77 // do nothing
michael@0 78 }
michael@0 79
michael@0 80 @Override
michael@0 81 public X509Certificate[] getAcceptedIssuers() {
michael@0 82 return new X509Certificate[0];
michael@0 83 }
michael@0 84
michael@0 85 } };
michael@0 86 try {
michael@0 87 this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
michael@0 88 this.sslcontext.init(null, tm, new SecureRandom());
michael@0 89 this.socketfactory = this.sslcontext.getSocketFactory();
michael@0 90 } catch ( NoSuchAlgorithmException e ) {
michael@0 91 Log.e(TAG,
michael@0 92 "Faild to instantiate TrustAllSSLSocketFactory!", e);
michael@0 93 } catch ( KeyManagementException e ) {
michael@0 94 Log.e(TAG,
michael@0 95 "Failed to instantiate TrustAllSSLSocketFactory!", e);
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 @Override
michael@0 100 public Socket createSocket(Socket socket, String host, int port,
michael@0 101 boolean autoClose) throws IOException, UnknownHostException {
michael@0 102 SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(
michael@0 103 socket, host, port, autoClose);
michael@0 104 return sslSocket;
michael@0 105 }
michael@0 106
michael@0 107 @Override
michael@0 108 public Socket connectSocket(Socket sock, String host, int port,
michael@0 109 InetAddress localAddress, int localPort, HttpParams params)
michael@0 110 throws IOException, UnknownHostException, ConnectTimeoutException {
michael@0 111 if ( host == null ) {
michael@0 112 throw new IllegalArgumentException(
michael@0 113 "Target host may not be null.");
michael@0 114 }
michael@0 115 if ( params == null ) {
michael@0 116 throw new IllegalArgumentException(
michael@0 117 "Parameters may not be null.");
michael@0 118 }
michael@0 119
michael@0 120 SSLSocket sslsock = (SSLSocket) ( ( sock != null ) ? sock
michael@0 121 : createSocket() );
michael@0 122
michael@0 123 if ( ( localAddress != null ) || ( localPort > 0 ) ) {
michael@0 124
michael@0 125 // we need to bind explicitly
michael@0 126 if ( localPort < 0 ) {
michael@0 127 localPort = 0; // indicates "any"
michael@0 128 }
michael@0 129
michael@0 130 InetSocketAddress isa = new InetSocketAddress(localAddress,
michael@0 131 localPort);
michael@0 132 sslsock.bind(isa);
michael@0 133 }
michael@0 134
michael@0 135 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
michael@0 136 int soTimeout = HttpConnectionParams.getSoTimeout(params);
michael@0 137
michael@0 138 InetSocketAddress remoteAddress;
michael@0 139 remoteAddress = new InetSocketAddress(host, port);
michael@0 140
michael@0 141 sslsock.connect(remoteAddress, connTimeout);
michael@0 142
michael@0 143 sslsock.setSoTimeout(soTimeout);
michael@0 144
michael@0 145 return sslsock;
michael@0 146 }
michael@0 147
michael@0 148 @Override
michael@0 149 public Socket createSocket() throws IOException {
michael@0 150 // the cast makes sure that the factory is working as expected
michael@0 151 return (SSLSocket) this.socketfactory.createSocket();
michael@0 152 }
michael@0 153
michael@0 154 @Override
michael@0 155 public boolean isSecure(Socket sock) throws IllegalArgumentException {
michael@0 156 return true;
michael@0 157 }
michael@0 158
michael@0 159 }

mercurial