1 10 11 package javax.rmi.ssl; 12 13 import java.io.IOException ; 14 import java.net.ServerSocket ; 15 import java.net.Socket ; 16 import java.rmi.server.RMIServerSocketFactory ; 17 import java.util.Arrays ; 18 import java.util.List ; 19 import javax.net.ssl.SSLServerSocketFactory; 20 import javax.net.ssl.SSLSocket; 21 import javax.net.ssl.SSLSocketFactory; 22 23 48 public class SslRMIServerSocketFactory implements RMIServerSocketFactory { 49 50 58 public SslRMIServerSocketFactory() { 59 this(null, null, false); 60 } 61 62 92 public SslRMIServerSocketFactory(String [] enabledCipherSuites, 93 String [] enabledProtocols, 94 boolean needClientAuth) 95 throws IllegalArgumentException { 96 97 this.enabledCipherSuites = enabledCipherSuites == null ? 100 null : (String []) enabledCipherSuites.clone(); 101 this.enabledProtocols = enabledProtocols == null ? 102 null : (String []) enabledProtocols.clone(); 103 this.needClientAuth = needClientAuth; 104 105 final SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory(); 110 SSLSocket sslSocket = null; 111 if (this.enabledCipherSuites != null || this.enabledProtocols != null) { 112 try { 113 sslSocket = (SSLSocket) sslSocketFactory.createSocket(); 114 } catch (Exception e) { 115 final String msg = "Unable to check if the cipher suites " + 116 "and protocols to enable are supported"; 117 throw (IllegalArgumentException ) 118 new IllegalArgumentException (msg).initCause(e); 119 } 120 } 121 122 if (this.enabledCipherSuites != null) { 127 sslSocket.setEnabledCipherSuites(this.enabledCipherSuites); 128 enabledCipherSuitesList = 129 Arrays.asList((String []) this.enabledCipherSuites); 130 } 131 if (this.enabledProtocols != null) { 132 sslSocket.setEnabledProtocols(this.enabledProtocols); 133 enabledProtocolsList = 134 Arrays.asList((String []) this.enabledProtocols); 135 } 136 } 137 138 148 public final String [] getEnabledCipherSuites() { 149 return enabledCipherSuites == null ? 150 null : (String []) enabledCipherSuites.clone(); 151 } 152 153 164 public final String [] getEnabledProtocols() { 165 return enabledProtocols == null ? 166 null : (String []) enabledProtocols.clone(); 167 } 168 169 178 public final boolean getNeedClientAuth() { 179 return needClientAuth; 180 } 181 182 187 public ServerSocket createServerSocket(int port) throws IOException { 188 final SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory(); 189 return new ServerSocket (port) { 190 public Socket accept() throws IOException { 191 Socket socket = super.accept(); 192 SSLSocket sslSocket = (SSLSocket) 193 sslSocketFactory.createSocket( 194 socket, socket.getInetAddress().getHostName(), 195 socket.getPort(), true); 196 sslSocket.setUseClientMode(false); 197 if (enabledCipherSuites != null) { 198 sslSocket.setEnabledCipherSuites(enabledCipherSuites); 199 } 200 if (enabledProtocols != null) { 201 sslSocket.setEnabledProtocols(enabledProtocols); 202 } 203 sslSocket.setNeedClientAuth(needClientAuth); 204 return sslSocket; 205 } 206 }; 207 208 } 240 241 252 public boolean equals(Object obj) { 253 if (obj == null) return false; 254 if (obj == this) return true; 255 if (!(obj instanceof SslRMIServerSocketFactory )) 256 return false; 257 SslRMIServerSocketFactory that = (SslRMIServerSocketFactory ) obj; 258 return (getClass().equals(that.getClass()) && checkParameters(that)); 259 } 260 261 private boolean checkParameters(SslRMIServerSocketFactory that) { 262 if (needClientAuth != that.needClientAuth) 265 return false; 266 267 if ((enabledCipherSuites == null && that.enabledCipherSuites != null) || 270 (enabledCipherSuites != null && that.enabledCipherSuites == null)) 271 return false; 272 if (enabledCipherSuites != null && that.enabledCipherSuites != null) { 273 List thatEnabledCipherSuitesList = 274 Arrays.asList((String []) that.enabledCipherSuites); 275 if (!enabledCipherSuitesList.equals(thatEnabledCipherSuitesList)) 276 return false; 277 } 278 279 if ((enabledProtocols == null && that.enabledProtocols != null) || 282 (enabledProtocols != null && that.enabledProtocols == null)) 283 return false; 284 if (enabledProtocols != null && that.enabledProtocols != null) { 285 List thatEnabledProtocolsList = 286 Arrays.asList((String []) that.enabledProtocols); 287 if (!enabledProtocolsList.equals(thatEnabledProtocolsList)) 288 return false; 289 } 290 291 return true; 292 } 293 294 301 public int hashCode() { 302 return getClass().hashCode() + 303 (needClientAuth ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode()) + 304 (enabledCipherSuites == null ? 0 : enabledCipherSuitesList.hashCode()) + 305 (enabledProtocols == null ? 0 : enabledProtocolsList.hashCode()); 306 } 307 308 private static SSLSocketFactory defaultSSLSocketFactory = null; 320 321 private static synchronized SSLSocketFactory getDefaultSSLSocketFactory() { 322 if (defaultSSLSocketFactory == null) 323 defaultSSLSocketFactory = 324 (SSLSocketFactory) SSLSocketFactory.getDefault(); 325 return defaultSSLSocketFactory; 326 } 327 328 private final String [] enabledCipherSuites; 329 private final String [] enabledProtocols; 330 private final boolean needClientAuth; 331 private List enabledCipherSuitesList; 332 private List enabledProtocolsList; 333 334 } 395 | Popular Tags |