1 7 8 package org.jboss.security.ssl; 9 10 import java.io.IOException ; 11 import java.net.InetAddress ; 12 import java.net.ServerSocket ; 13 import java.net.UnknownHostException ; 14 import java.util.Arrays ; 15 import javax.naming.InitialContext ; 16 import javax.net.ServerSocketFactory; 17 import javax.net.ssl.SSLContext; 18 import javax.net.ssl.SSLServerSocketFactory; 19 import javax.net.ssl.SSLServerSocket; 20 21 import org.jboss.logging.Logger; 22 import org.jboss.security.SecurityDomain; 23 24 34 public class DomainServerSocketFactory extends SSLServerSocketFactory 35 { 36 private static Logger log = Logger.getLogger(DomainServerSocketFactory.class); 37 private transient SecurityDomain securityDomain; 38 private transient InetAddress bindAddress; 39 private transient SSLContext sslCtx = null; 40 private boolean wantsClientAuth = true; 41 private boolean needsClientAuth = false; 42 43 46 public DomainServerSocketFactory() 47 { 48 } 49 52 public DomainServerSocketFactory(SecurityDomain securityDomain) throws IOException 53 { 54 if( securityDomain == null ) 55 throw new IOException ("The securityDomain may not be null"); 56 this.securityDomain = securityDomain; 57 } 58 59 public String getBindAddress() 60 { 61 String address = null; 62 if( bindAddress != null ) 63 address = bindAddress.getHostAddress(); 64 return address; 65 } 66 public void setBindAddress(String host) throws UnknownHostException 67 { 68 bindAddress = InetAddress.getByName(host); 69 } 70 71 public SecurityDomain getSecurityDomain() 72 { 73 return securityDomain; 74 } 75 public void setSecurityDomain(SecurityDomain securityDomain) 76 { 77 this.securityDomain = securityDomain; 78 } 79 80 public boolean isWantsClientAuth() 81 { 82 return wantsClientAuth; 83 } 84 public void setWantsClientAuth(boolean wantsClientAuth) 85 { 86 this.wantsClientAuth = wantsClientAuth; 87 } 88 89 public boolean isNeedsClientAuth() 90 { 91 return needsClientAuth; 92 } 93 public void setNeedsClientAuth(boolean needsClientAuth) 94 { 95 this.needsClientAuth = needsClientAuth; 96 } 97 98 public ServerSocket createServerSocket(int port) throws IOException 100 { 101 return createServerSocket(port, 50, bindAddress); 102 } 103 public ServerSocket createServerSocket(int port, int backlog) 104 throws IOException 105 { 106 return createServerSocket(port, backlog, bindAddress); 107 } 108 120 public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) 121 throws IOException 122 { 123 initSSLContext(); 124 SSLServerSocketFactory factory = sslCtx.getServerSocketFactory(); 125 SSLServerSocket socket = (SSLServerSocket) factory.createServerSocket(port, backlog, ifAddress); 126 String [] supportedProtocols = socket.getSupportedProtocols(); 127 log.debug("Supported protocols: " + Arrays.asList(supportedProtocols)); 128 String [] protocols = supportedProtocols; socket.setEnabledProtocols(protocols); 130 socket.setNeedClientAuth(needsClientAuth); 131 socket.setWantClientAuth(wantsClientAuth); 132 return socket; 133 } 134 135 138 public static ServerSocketFactory getDefault() 139 { 140 DomainServerSocketFactory ssf = null; 141 try 142 { 143 InitialContext iniCtx = new InitialContext (); 144 SecurityDomain sd = (SecurityDomain) iniCtx.lookup("java:/jaas/other"); 145 ssf = new DomainServerSocketFactory(sd); 146 } 147 catch(Exception e) 148 { 149 log.error("Failed to create default ServerSocketFactory", e); 150 } 151 return ssf; 152 } 153 154 public String [] getDefaultCipherSuites() 155 { 156 String [] cipherSuites = {}; 157 try 158 { 159 initSSLContext(); 160 SSLServerSocketFactory factory = sslCtx.getServerSocketFactory(); 161 cipherSuites = factory.getDefaultCipherSuites(); 162 } 163 catch(IOException e) 164 { 165 log.error("Failed to get default SSLServerSocketFactory", e); 166 } 167 return cipherSuites; 168 } 169 170 public String [] getSupportedCipherSuites() 171 { 172 String [] cipherSuites = {}; 173 try 174 { 175 initSSLContext(); 176 SSLServerSocketFactory factory = sslCtx.getServerSocketFactory(); 177 cipherSuites = factory.getSupportedCipherSuites(); 178 } 179 catch(IOException e) 180 { 181 log.error("Failed to get default SSLServerSocketFactory", e); 182 } 183 return cipherSuites; 184 } 185 186 188 private void initSSLContext() 189 throws IOException 190 { 191 if( sslCtx != null ) 192 return; 193 sslCtx = Context.forDomain(securityDomain); 194 } 195 } 196 | Popular Tags |