/* * HttpsSocketFactory.java * * Copyright (c) 2003-2004 Digi International * This program and the information contained in it is confidential and * proprietary to Digi International and may not be used, copied, or re- * produced without the prior written permission of Digi International. * */ package com.digi.config.util; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import javax.net.ssl.*; import javax.net.SocketFactory; import javax.net.ssl.SSLSocketFactory; /** * Socket factory required to override trust manager when using HttpsURLConnection classes. * This class simply 'decorates' the default socket factory, initializing it with our custom * trust manager. The default trust manager throws an exception if a certificate is not in * the truststore. Our custom trust manager will prompt the user. * Also implements HostnameVerifier to resolve hostname issues. */ public class HttpsSocketFactory extends SSLSocketFactory implements HostnameVerifier { private SSLSocketFactory factory; public HttpsSocketFactory() { try { SSLContext sslcontext = SSLContext.getInstance( "TLS"); sslcontext.init( null, // No KeyManager required new TrustManager[] { new HttpsTrustManager()}, new java.security.SecureRandom()); factory = ( SSLSocketFactory) sslcontext.getSocketFactory(); } catch ( Exception ex) { System.out.println("Exception creating our custom socket factory for the application."); ex.printStackTrace(); } } public static SocketFactory getDefault() { return new HttpsSocketFactory(); } public Socket createSocket( Socket socket, String s, int i, boolean flag) throws IOException { return factory.createSocket( socket, s, i, flag); } public Socket createSocket( InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException { return factory.createSocket( inaddr, i, inaddr1, j); } public Socket createSocket( InetAddress inaddr, int i) throws IOException { return factory.createSocket( inaddr, i); } public Socket createSocket( String s, int i, InetAddress inaddr, int j) throws IOException { return factory.createSocket( s, i, inaddr, j); } public Socket createSocket( String s, int i) throws IOException { return factory.createSocket( s, i); } public String[] getDefaultCipherSuites() { return factory.getSupportedCipherSuites(); } public String[] getSupportedCipherSuites() { return factory.getSupportedCipherSuites(); } // HostnameVerifier interface public boolean verify(String hostname, SSLSession sess) { return true; } }