From 5f11b3d998ffdb55c7e8a51d3ae0b51f13f9afdb Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Tue, 11 Nov 2014 13:37:30 +0100 Subject: [PATCH] Refactored test project to work correctly with AdvancedSslSocketFactory as base to create sockets --- .../network/AdvancedSslSocketFactory.java | 7 ++- .../SelfSignedConfidentSslSocketFactory.java | 47 ++++++------------- .../lib/test_project/TestActivity.java | 2 +- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/com/owncloud/android/lib/common/network/AdvancedSslSocketFactory.java b/src/com/owncloud/android/lib/common/network/AdvancedSslSocketFactory.java index 0f9d11e4..de3d9cb5 100644 --- a/src/com/owncloud/android/lib/common/network/AdvancedSslSocketFactory.java +++ b/src/com/owncloud/android/lib/common/network/AdvancedSslSocketFactory.java @@ -78,8 +78,11 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory { 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"); + if (trustManager == null && mHostnameVerifier != null) + throw new IllegalArgumentException( + "AdvancedSslSocketFactory can not be created with a null Trust Manager and a " + + "not null Hostname Verifier" + ); mSslContext = sslContext; mTrustManager = trustManager; mHostnameVerifier = hostnameVerifier; diff --git a/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java b/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java index 3626e0b8..829cf39e 100644 --- a/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java +++ b/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java @@ -26,9 +26,7 @@ package com.owncloud.android.lib.test_project; 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.GeneralSecurityException; import java.security.KeyStore; @@ -38,9 +36,7 @@ import java.security.cert.CertStoreException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; -import javax.net.SocketFactory; import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; @@ -49,7 +45,7 @@ import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; -import com.owncloud.android.lib.common.network.ServerNameIndicator; +import com.owncloud.android.lib.common.network.AdvancedSslSocketFactory; /** @@ -64,7 +60,8 @@ import com.owncloud.android.lib.common.network.ServerNameIndicator; public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocketFactory { - private SSLContext mSslContext = null; + //private SSLContext mSslContext = null; + private AdvancedSslSocketFactory mWrappedSslSocketFactory = null; /** @@ -72,7 +69,13 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket * @throws GeneralSecurityException */ public SelfSignedConfidentSslSocketFactory() throws GeneralSecurityException { - mSslContext = createSslContext(); + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init( + null, + new TrustManager[] { new SelfSignedConfidentX509TrustManager() }, + null + ); + mWrappedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, null, null); } @@ -81,7 +84,7 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket */ @Override public Socket createSocket(String host, int port) throws IOException, UnknownHostException { - return mSslContext.getSocketFactory().createSocket(host, port); + return mWrappedSslSocketFactory.createSocket(host, port); } /** @@ -90,7 +93,7 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket @Override public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { - return mSslContext.getSocketFactory().createSocket(host, port, clientHost, clientPort); + return mWrappedSslSocketFactory.createSocket(host, port, clientHost, clientPort); } /** @@ -112,19 +115,7 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { - if (params == null) { - throw new IllegalArgumentException("Parameters may not be null"); - } - int timeout = params.getConnectionTimeout(); - SocketFactory socketfactory = mSslContext.getSocketFactory(); - Socket socket = socketfactory.createSocket(); - SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); - SocketAddress remoteaddr = new InetSocketAddress(host, port); - socket.setSoTimeout(params.getSoTimeout()); - socket.bind(localaddr); - ServerNameIndicator.setServerNameIndication(host, (SSLSocket)socket); - socket.connect(remoteaddr, timeout); - return socket; + return mWrappedSslSocketFactory.createSocket(host, port, localAddress, localPort, params); } /** @@ -133,20 +124,10 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket @Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { - return mSslContext.getSocketFactory().createSocket(socket, host, port, autoClose); + return mWrappedSslSocketFactory.createSocket(socket, host, port, autoClose); } - - private static SSLContext createSslContext() throws GeneralSecurityException { - SSLContext context = SSLContext.getInstance("TLS"); - context.init( - null, - new TrustManager[] {new SelfSignedConfidentX509TrustManager()}, - null); - return context; - } - public static class SelfSignedConfidentX509TrustManager implements X509TrustManager { private X509TrustManager mStandardTrustManager = null; diff --git a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java index 905fa511..b2a37ba1 100644 --- a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java +++ b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java @@ -188,7 +188,7 @@ public class TestActivity extends Activity { public RemoteOperationResult removeFile(String remotePath) { RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); RemoteOperationResult result = removeOperation.execute(mClient); - return TestActivity.removeFile(remotePath, mClient); + return result; } /**