/* ownCloud Android Library is available under MIT license * Copyright (C) 2014 ownCloud (http://www.owncloud.org/) * Copyright (C) 2012 Bartek Przybylski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package com.owncloud.android.lib.network; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; //import java.security.Provider; import java.security.cert.X509Certificate; //import java.util.Enumeration; import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLHandshakeException; //import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.http.conn.ssl.X509HostnameVerifier; //import android.os.Build; import android.util.Log; /** * AdvancedSSLProtocolSocketFactory allows to create SSL {@link Socket}s with * a custom SSLContext and an optional Hostname Verifier. * * @author David A. Velasco */ public class AdvancedSslSocketFactory implements ProtocolSocketFactory { private static final String TAG = AdvancedSslSocketFactory.class.getSimpleName(); private SSLContext mSslContext = null; private AdvancedX509TrustManager mTrustManager = null; private X509HostnameVerifier mHostnameVerifier = null; public SSLContext getSslContext() { return mSslContext; } /** * Constructor for AdvancedSSLProtocolSocketFactory. */ public AdvancedSslSocketFactory(SSLContext sslContext, AdvancedX509TrustManager trustManager, X509HostnameVerifier hostnameVerifier) { if (sslContext == null) throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null SSLContext"); if (trustManager == null) throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null Trust Manager"); mSslContext = sslContext; mTrustManager = trustManager; mHostnameVerifier = hostnameVerifier; } /** * @see ProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int) */ public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { Socket socket = mSslContext.getSocketFactory().createSocket(host, port, clientHost, clientPort); verifyPeerIdentity(host, port, socket); return socket; } /* private void logSslInfo() { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) { Log.v(TAG, "SUPPORTED SSL PARAMETERS"); logSslParameters(mSslContext.getSupportedSSLParameters()); Log.v(TAG, "DEFAULT SSL PARAMETERS"); logSslParameters(mSslContext.getDefaultSSLParameters()); Log.i(TAG, "CURRENT PARAMETERS"); Log.i(TAG, "Protocol: " + mSslContext.getProtocol()); } Log.i(TAG, "PROVIDER"); logSecurityProvider(mSslContext.getProvider()); } private void logSecurityProvider(Provider provider) { Log.i(TAG, "name: " + provider.getName()); Log.i(TAG, "version: " + provider.getVersion()); Log.i(TAG, "info: " + provider.getInfo()); Enumeration keys = provider.propertyNames(); String key; while (keys.hasMoreElements()) { key = (String) keys.nextElement(); Log.i(TAG, " property " + key + " : " + provider.getProperty(key)); } } private void logSslParameters(SSLParameters params) { Log.v(TAG, "Cipher suites: "); String [] elements = params.getCipherSuites(); for (int i=0; i