1 23 24 29 package com.sun.appserv.management.client; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.IOException ; 34 import java.io.Serializable ; 35 import java.net.Socket ; 36 import java.util.logging.Logger ; 37 38 import javax.net.ssl.SSLContext; 39 import javax.net.ssl.TrustManager; 40 import javax.net.ssl.X509TrustManager; 41 import javax.net.ssl.KeyManager; 42 import javax.net.ssl.KeyManagerFactory; 43 import javax.net.ssl.SSLSocket; 44 import javax.net.ssl.SSLSocketFactory; 45 import javax.net.ssl.TrustManagerFactory; 46 import javax.net.ssl.HandshakeCompletedListener; 47 48 import java.security.KeyStore ; 49 import java.security.KeyStoreException ; 50 import java.security.cert.CertificateException ; 51 import java.security.NoSuchAlgorithmException ; 52 53 import java.rmi.server.RMIClientSocketFactory ; 54 55 import com.sun.appserv.management.client.TrustAnyTrustManager; 56 57 58 68 public class AdminRMISSLClientSocketFactory 69 implements RMIClientSocketFactory , Serializable 70 { 71 static final long serialVersionUID = 5096547838871926785L; 72 73 private transient SSLSocketFactory mFactory = null; 74 75 private transient MyEnvImpl mEnvImpl = null; 76 77 public 78 AdminRMISSLClientSocketFactory() 79 { 80 mEnvImpl = new MyEnvImpl(); 81 } 82 83 private static TrustManager[] 84 getTrustAny() 85 { 86 final TrustManager[] trustManagers = new TrustManager[ 1 ]; 87 trustManagers[ 0 ] = TrustAnyTrustManager.getInstance(); 88 return( trustManagers ); 89 } 90 91 97 private final class MyEnvImpl 98 implements AdminRMISSLClientSocketFactoryEnv 99 { 100 private transient boolean mTrace = false; 101 private MyEnvImpl() {} 102 103 public TrustManager[] 104 getTrustManagers( ) 105 { 106 final TrustStoreTrustManager mgr = TrustStoreTrustManager.getSystemInstance(); 107 108 return new TrustManager[] { mgr }; 109 } 110 111 public HandshakeCompletedListener getHandshakeCompletedListener( ) { return null; } 112 113 public void setTrace( final boolean trace ) { mTrace = trace; } 114 public boolean getTrace() { return mTrace; } 115 116 public Object getValue( final String key ) { return null; } 117 } 118 119 120 125 private synchronized AdminRMISSLClientSocketFactoryEnv 126 getEnv() 127 { 128 return( AdminRMISSLClientSocketFactoryEnvImpl.getInstance() ); 129 } 130 131 private final void 132 trace( Object o ) 133 { 134 if ( getEnv().getTrace() ) 135 { 136 final String name = this.getClass().getName(); 137 Logger.getLogger( name ).info( toString() + ": " + o.toString() ); 138 } 139 } 140 141 private static char[] 142 toCharArray( final String s ) 143 { 144 return( s == null ? null : s.toCharArray() ); 145 } 146 147 148 private final SSLSocketFactory 149 createSocketFactory( final AdminRMISSLClientSocketFactoryEnv env ) 150 throws IOException 151 { 152 SSLSocketFactory factory = null; 153 154 try 155 { 156 final TrustManager[] trustManagers = env.getTrustManagers( ); 157 158 final SSLContext sslContext = SSLContext.getInstance( "TLSv1" ); 159 sslContext.init( null, trustManagers, null ); 160 factory = sslContext.getSocketFactory(); 161 } 162 catch (Exception e) 163 { 164 e.printStackTrace(); 165 throw (IOException ) new IOException ().initCause(e); 166 } 167 168 return( factory ); 169 } 170 171 172 173 public synchronized Socket 174 createSocket( 175 final String host, 176 final int port) 177 throws IOException 178 { 179 final String target = host + ":" + port; 180 181 trace( "createSocket: " + target ); 182 183 final AdminRMISSLClientSocketFactoryEnv env = getEnv(); 184 185 if ( mFactory == null) 186 { 187 mFactory = createSocketFactory( env ); 188 trace( "createSocket: created socket factory" ); 189 } 190 191 final SSLSocket sslSocket = (SSLSocket)mFactory.createSocket( host, port ); 193 194 final HandshakeCompletedListener listener = env.getHandshakeCompletedListener( ); 195 if ( listener != null ) 196 { 197 trace( "createSocket: added HandshakeCompletedListener: " + listener ); 198 sslSocket.addHandshakeCompletedListener( listener ); 199 } 200 201 202 trace( "created socket: " + target ); 203 return( sslSocket ); 204 } 205 } 206 207 208 209 210 211 212 213 214 215 216 217 | Popular Tags |