From 30d6e8146bcac462e6b6601a370731eb1c0262c1 Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Tue, 22 May 2012 16:08:42 +0100 Subject: [PATCH] tidy up mess that is SSL support. --- .../ui/infrastructure/MySSLSocketFactory.java | 62 ------------------- .../infrastructure/RestTemplateFactory.java | 53 ++-------------- 2 files changed, 5 insertions(+), 110 deletions(-) delete mode 100644 mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/MySSLSocketFactory.java diff --git a/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/MySSLSocketFactory.java b/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/MySSLSocketFactory.java deleted file mode 100644 index 597c66427..000000000 --- a/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/MySSLSocketFactory.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.mifosng.ui.infrastructure; - -import java.io.IOException; -import java.net.Socket; -import java.net.UnknownHostException; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import org.apache.http.conn.ssl.SSLSocketFactory; - -public class MySSLSocketFactory extends SSLSocketFactory { - - private SSLContext sslContext; - - public MySSLSocketFactory(SSLContext sslContext, KeyStore truststore) - throws NoSuchAlgorithmException, KeyManagementException, - KeyStoreException, UnrecoverableKeyException { - super(truststore); - this.sslContext = sslContext; - - TrustManager tm = new X509TrustManager() { - - @Override - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return null; - } - - @Override - public void checkServerTrusted( - java.security.cert.X509Certificate[] chain, String authType) - throws java.security.cert.CertificateException { - } - - @Override - public void checkClientTrusted( - java.security.cert.X509Certificate[] chain, String authType) - throws java.security.cert.CertificateException { - } - }; - - sslContext.init(null, new TrustManager[] { tm }, null); - } - - @Override - public Socket createSocket(Socket socket, String host, int port, - boolean autoClose) throws IOException, UnknownHostException { - return sslContext.getSocketFactory().createSocket(socket, host, port, - autoClose); - } - - @Override - public Socket createSocket() throws IOException { - return sslContext.getSocketFactory().createSocket(); - } -} \ No newline at end of file diff --git a/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/RestTemplateFactory.java b/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/RestTemplateFactory.java index a1c15a6f8..bbbf93159 100644 --- a/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/RestTemplateFactory.java +++ b/mifosng-individual-lending-app/src/main/java/org/mifosng/ui/infrastructure/RestTemplateFactory.java @@ -1,25 +1,16 @@ package org.mifosng.ui.infrastructure; -import java.io.IOException; import java.security.KeyManagementException; -import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLException; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSocket; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.conn.ssl.X509HostnameVerifier; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; @@ -56,39 +47,9 @@ public class RestTemplateFactory implements FactoryBean, ClientConnectionManager clientConnectionManager = client.getConnectionManager(); SchemeRegistry registry = clientConnectionManager.getSchemeRegistry(); - Scheme oldScheme = registry.unregister("https"); - - KeyStore trustStore = KeyStore.getInstance(KeyStore - .getDefaultType()); - - trustStore.load(null, null); - - X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() { - @Override - public boolean verify(String arg0, SSLSession arg1) { - return true; - } - - @Override - public void verify(String host, String[] cns, String[] subjectAlts) - throws SSLException { - } - - @Override - public void verify(String host, X509Certificate cert) throws SSLException { - } - - @Override - public void verify(String host, SSLSocket ssl) throws IOException { - } - }; - - SSLContext context = SSLContext.getInstance("TLS"); - SSLSocketFactory factory = new MySSLSocketFactory(context, trustStore); - factory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - factory.setHostnameVerifier(hostnameVerifier); - - Scheme newHttpsScheme = new Scheme("https", 8443, factory); + SSLSocketFactory factory = new SSLSocketFactory(new TrustSelfSignedStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); +// SSLContext context = SSLContext.getInstance("TLS"); + Scheme newHttpsScheme = new Scheme("https", 443, factory); registry.register(newHttpsScheme); restTemplate = new RestTemplate(requestFactory); @@ -96,14 +57,10 @@ public class RestTemplateFactory implements FactoryBean, throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); - } catch (CertificateException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); } catch (KeyManagementException e) { throw new RuntimeException(e); } catch (UnrecoverableKeyException e) { throw new RuntimeException(e); } } -} +} \ No newline at end of file