1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/CopyOfEasySSLSocketFactory.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 +/* 1.28 + * Licensed to the Apache Software Foundation (ASF) under one 1.29 + * or more contributor license agreements. See the NOTICE file 1.30 + * distributed with this work for additional information 1.31 + * regarding copyright ownership. The ASF licenses this file 1.32 + * to you under the Apache License, Version 2.0 (the 1.33 + * "License"); you may not use this file except in compliance 1.34 + * with the License. You may obtain a copy of the License at 1.35 + * 1.36 + * http://www.apache.org/licenses/LICENSE-2.0 1.37 + * 1.38 + * Unless required by applicable law or agreed to in writing, 1.39 + * software distributed under the License is distributed on an 1.40 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.41 + * KIND, either express or implied. See the License for the 1.42 + * specific language governing permissions and limitations 1.43 + * under the License. 1.44 + */ 1.45 + 1.46 +import java.io.IOException; 1.47 +import java.net.InetAddress; 1.48 +import java.net.InetSocketAddress; 1.49 +import java.net.Socket; 1.50 +import java.net.UnknownHostException; 1.51 + 1.52 +import javax.net.ssl.SSLContext; 1.53 +import javax.net.ssl.SSLSocket; 1.54 +import javax.net.ssl.TrustManager; 1.55 + 1.56 +import org.apache.http.conn.ConnectTimeoutException; 1.57 +import org.apache.http.conn.scheme.LayeredSocketFactory; 1.58 +import org.apache.http.conn.scheme.SocketFactory; 1.59 +import org.apache.http.params.HttpConnectionParams; 1.60 +import org.apache.http.params.HttpParams; 1.61 + 1.62 +/** 1.63 + * This socket factory will create ssl socket that accepts self signed 1.64 + * certificate 1.65 + * 1.66 + * @author olamy 1.67 + * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse 1.68 + * $ 1.69 + * @since 1.2.3 1.70 + */ 1.71 +public class CopyOfEasySSLSocketFactory implements SocketFactory, 1.72 + LayeredSocketFactory { 1.73 + 1.74 + private SSLContext sslcontext = null; 1.75 + 1.76 + private static SSLContext createEasySSLContext() throws IOException { 1.77 + try { 1.78 + SSLContext context = SSLContext.getInstance("TLS"); 1.79 + context.init(null, new TrustManager[] { new EasyX509TrustManager( 1.80 + null) }, null); 1.81 + return context; 1.82 + } catch (Exception e) { 1.83 + throw new IOException(e.getMessage()); 1.84 + } 1.85 + } 1.86 + 1.87 + private SSLContext getSSLContext() throws IOException { 1.88 + if (this.sslcontext == null) { 1.89 + this.sslcontext = createEasySSLContext(); 1.90 + } 1.91 + return this.sslcontext; 1.92 + } 1.93 + 1.94 + /** 1.95 + * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, 1.96 + * java.lang.String, int, java.net.InetAddress, int, 1.97 + * org.apache.http.params.HttpParams) 1.98 + */ 1.99 + public Socket connectSocket(Socket sock, String host, int port, 1.100 + InetAddress localAddress, int localPort, HttpParams params) 1.101 + throws IOException, UnknownHostException, ConnectTimeoutException { 1.102 + int connTimeout = HttpConnectionParams.getConnectionTimeout(params); 1.103 + int soTimeout = HttpConnectionParams.getSoTimeout(params); 1.104 + 1.105 + InetSocketAddress remoteAddress = new InetSocketAddress(host, port); 1.106 + SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket()); 1.107 + 1.108 + if ((localAddress != null) || (localPort > 0)) { 1.109 + // we need to bind explicitly 1.110 + if (localPort < 0) { 1.111 + localPort = 0; // indicates "any" 1.112 + } 1.113 + InetSocketAddress isa = new InetSocketAddress(localAddress, 1.114 + localPort); 1.115 + sslsock.bind(isa); 1.116 + } 1.117 + 1.118 + sslsock.connect(remoteAddress, connTimeout); 1.119 + sslsock.setSoTimeout(soTimeout); 1.120 + return sslsock; 1.121 + 1.122 + } 1.123 + 1.124 + /** 1.125 + * @see org.apache.http.conn.scheme.SocketFactory#createSocket() 1.126 + */ 1.127 + public Socket createSocket() throws IOException { 1.128 + return getSSLContext().getSocketFactory().createSocket(); 1.129 + } 1.130 + 1.131 + /** 1.132 + * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket) 1.133 + */ 1.134 + public boolean isSecure(Socket socket) throws IllegalArgumentException { 1.135 + return true; 1.136 + } 1.137 + 1.138 + /** 1.139 + * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, 1.140 + * java.lang.String, int, boolean) 1.141 + */ 1.142 + public Socket createSocket(Socket socket, String host, int port, 1.143 + boolean autoClose) throws IOException, UnknownHostException { 1.144 + return getSSLContext().getSocketFactory().createSocket(); 1.145 + } 1.146 + 1.147 + // ------------------------------------------------------------------- 1.148 + // javadoc in org.apache.http.conn.scheme.SocketFactory says : 1.149 + // Both Object.equals() and Object.hashCode() must be overridden 1.150 + // for the correct operation of some connection managers 1.151 + // ------------------------------------------------------------------- 1.152 + 1.153 + public boolean equals(Object obj) { 1.154 + return ((obj != null) && obj.getClass().equals( 1.155 + CopyOfEasySSLSocketFactory.class)); 1.156 + } 1.157 + 1.158 + public int hashCode() { 1.159 + return CopyOfEasySSLSocketFactory.class.hashCode(); 1.160 + } 1.161 + 1.162 +} 1.163 \ No newline at end of file