1 28 29 package com.caucho.vfs; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.util.L10N; 33 34 import javax.annotation.PostConstruct; 35 import javax.net.ssl.KeyManagerFactory; 36 import javax.net.ssl.SSLContext; 37 import javax.net.ssl.SSLServerSocket; 38 import javax.net.ssl.SSLServerSocketFactory; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.net.InetAddress ; 42 import java.net.ServerSocket ; 43 import java.security.GeneralSecurityException ; 44 import java.security.Key ; 45 import java.security.KeyStore ; 46 import java.security.cert.Certificate ; 47 48 51 public class JsseSSLFactory implements SSLFactory { 52 private static final L10N L = new L10N(JsseSSLFactory.class); 53 54 private Path _keyStoreFile; 55 private String _alias; 56 private String _password; 57 private String _verifyClient; 58 private String _keyStoreType = "jks"; 59 private String _keyManagerFactory = "SunX509"; 60 private String _sslContext = "TLS"; 61 62 private KeyStore _keyStore; 63 64 67 public JsseSSLFactory() 68 { 69 } 70 71 74 public void setKeyStoreFile(Path keyStoreFile) 75 { 76 _keyStoreFile = keyStoreFile; 77 } 78 79 82 public Path getKeyStoreFile() 83 { 84 return _keyStoreFile; 85 } 86 87 90 public void setPassword(String password) 91 { 92 _password = password; 93 } 94 95 98 public String getPassword() 99 { 100 return _password; 101 } 102 103 106 public void setAlias(String alias) 107 { 108 _alias = alias; 109 } 110 111 114 public String getAlias() 115 { 116 return _alias; 117 } 118 119 122 public void setVerifyClient(String verifyClient) 123 { 124 _verifyClient = verifyClient; 125 } 126 127 130 public String getVerifyClient() 131 { 132 return _verifyClient; 133 } 134 135 138 public void setKeyManagerFactory(String keyManagerFactory) 139 { 140 _keyManagerFactory = keyManagerFactory; 141 } 142 143 146 public void setSSLContext(String sslContext) 147 { 148 _sslContext = sslContext; 149 } 150 151 154 public void setKeyStoreType(String keyStore) 155 { 156 _keyStoreType = keyStore; 157 } 158 159 162 @PostConstruct 163 public void init() 164 throws ConfigException, IOException , GeneralSecurityException 165 { 166 if (_keyStoreFile == null) 167 throw new ConfigException(L.l("`key-store-file' is required for JSSE.")); 168 if (_password == null) 169 throw new ConfigException(L.l("`password' is required for JSSE.")); 170 171 _keyStore = KeyStore.getInstance(_keyStoreType); 172 173 InputStream is = _keyStoreFile.openRead(); 174 try { 175 _keyStore.load(is, _password.toCharArray()); 176 } finally { 177 is.close(); 178 } 179 180 if (_alias != null) { 181 Key key = _keyStore.getKey(_alias, _password.toCharArray()); 182 183 if (key == null) 184 throw new ConfigException(L.l("JSSE alias '{0}' does not have a corresponding key.", 185 _alias)); 186 187 Certificate []certChain = _keyStore.getCertificateChain(_alias); 188 189 if (certChain == null) 190 throw new ConfigException(L.l("JSSE alias '{0}' does not have a corresponding certificate chain.", 191 _alias)); 192 193 _keyStore = KeyStore.getInstance(_keyStoreType); 194 _keyStore.load(null, _password.toCharArray()); 195 196 _keyStore.setKeyEntry(_alias, key, _password.toCharArray(), certChain); 197 } 198 } 199 200 203 public QServerSocket create(InetAddress host, int port) 204 throws IOException , GeneralSecurityException 205 { 206 if (_keyStore == null) 207 throw new IOException (L.l("key store is missing")); 208 209 KeyManagerFactory kmf = KeyManagerFactory.getInstance(_keyManagerFactory); 210 211 kmf.init(_keyStore, _password.toCharArray()); 212 213 SSLContext sslContext = SSLContext.getInstance(_sslContext); 214 215 sslContext.init(kmf.getKeyManagers(), null, null); 216 217 SSLServerSocketFactory factory; 218 factory = sslContext.getServerSocketFactory(); 219 220 ServerSocket serverSocket; 221 222 int listen = 100; 223 224 if (host == null) 225 serverSocket = factory.createServerSocket(port, listen); 226 else 227 serverSocket = factory.createServerSocket(port, listen, host); 228 229 SSLServerSocket sslServerSocket = (SSLServerSocket) serverSocket; 230 231 if ("required".equals(_verifyClient)) 232 sslServerSocket.setNeedClientAuth(true); 233 234 259 260 return new QServerSocketWrapper(serverSocket); 261 } 262 } 263 264 | Popular Tags |