1 16 package org.mortbay.http; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.security.KeyStore ; 21 import java.security.SecureRandom ; 22 import java.security.Security ; 23 24 import javax.net.ssl.SSLServerSocketFactory; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 import org.mortbay.util.InetAddrPort; 29 import org.mortbay.util.Password; 30 31 import com.sun.net.ssl.KeyManager; 32 import com.sun.net.ssl.KeyManagerFactory; 33 import com.sun.net.ssl.SSLContext; 34 import com.sun.net.ssl.TrustManager; 35 import com.sun.net.ssl.TrustManagerFactory; 36 37 38 39 53 public class SunJsseListener extends JsseListener 54 { 55 private static Log log = LogFactory.getLog(SunJsseListener.class); 56 57 private String _keystore=DEFAULT_KEYSTORE ; 58 private transient Password _password; 59 private transient Password _keypassword; 60 private String _keystore_type = DEFAULT_KEYSTORE_TYPE; 61 private String _keystore_provider_name = DEFAULT_KEYSTORE_PROVIDER_NAME; 62 private String _keystore_provider_class = DEFAULT_KEYSTORE_PROVIDER_CLASS; 63 private boolean _useDefaultTrustStore = false; 64 65 66 static 67 { 68 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 69 } 70 71 72 public void setKeystore(String keystore) 73 { 74 _keystore = keystore; 75 } 76 77 78 public String getKeystore() 79 { 80 return _keystore; 81 } 82 83 84 public void setPassword(String password) 85 { 86 _password = Password.getPassword(PASSWORD_PROPERTY,password,null); 87 } 88 89 90 public void setKeyPassword(String password) 91 { 92 _keypassword = Password.getPassword(KEYPASSWORD_PROPERTY,password,null); 93 } 94 95 96 97 public void setKeystoreType(String keystore_type) 98 { 99 _keystore_type = keystore_type; 100 } 101 102 103 public String getKeystoreType() 104 { 105 return _keystore_type; 106 } 107 108 109 public void setKeystoreProviderName(String name) 110 { 111 _keystore_provider_name = name; 112 } 113 114 115 public String getKeystoreProviderName() 116 { 117 return _keystore_provider_name; 118 } 119 120 121 public String getKeystoreProviderClass() 122 { 123 return _keystore_provider_class; 124 } 125 126 127 public void setKeystoreProviderClass(String classname) 128 { 129 _keystore_provider_class = classname; 130 } 131 132 133 139 public boolean getUseDefaultTrustStore() 140 { 141 return _useDefaultTrustStore; 142 } 143 144 145 153 public void setUseDefaultTrustStore(boolean flag) 154 { 155 _useDefaultTrustStore = flag; 156 } 157 158 159 161 public SunJsseListener() 162 { 163 super(); 164 } 165 166 167 170 public SunJsseListener(InetAddrPort p_address) 171 { 172 super( p_address); 173 } 174 175 176 180 protected SSLServerSocketFactory createFactory() 181 throws Exception 182 { 183 _keystore = System.getProperty( KEYSTORE_PROPERTY,_keystore); 184 185 log.info(KEYSTORE_PROPERTY+"="+_keystore); 186 187 if (_password==null) 188 _password = Password.getPassword(PASSWORD_PROPERTY,null,null); 189 log.info(PASSWORD_PROPERTY+"="+_password.toStarString()); 190 191 if (_keypassword==null) 192 _keypassword = Password.getPassword(KEYPASSWORD_PROPERTY, 193 null, 194 _password.toString()); 195 log.info(KEYPASSWORD_PROPERTY+"="+_keypassword.toStarString()); 196 197 198 KeyStore ks = null; 199 200 log.info(KEYSTORE_TYPE_PROPERTY+"="+_keystore_type); 201 202 if (_keystore_provider_class != null) { 203 java.security.Provider [] installed_providers = Security.getProviders(); 206 java.security.Provider myprovider = null; 207 for (int i=0; i < installed_providers.length; i++) { 208 if (installed_providers[i].getClass().getName().equals(_keystore_provider_class)) { 209 myprovider = installed_providers[i]; 210 break; 211 } 212 } 213 if (myprovider == null) { 214 myprovider = (java.security.Provider ) Class.forName(_keystore_provider_class).newInstance(); 216 Security.addProvider(myprovider); 217 } 218 log.info(KEYSTORE_PROVIDER_CLASS_PROPERTY+"="+_keystore_provider_class); 219 ks = KeyStore.getInstance(_keystore_type,myprovider.getName()); 220 } else if (_keystore_provider_name != null) { 221 log.info(KEYSTORE_PROVIDER_NAME_PROPERTY+"="+_keystore_provider_name); 222 ks = KeyStore.getInstance(_keystore_type,_keystore_provider_name); 223 } else { 224 ks = KeyStore.getInstance(_keystore_type); 225 log.info(KEYSTORE_PROVIDER_NAME_PROPERTY+"=[DEFAULT]"); 226 } 227 228 ks.load( new FileInputStream ( new File ( _keystore ) ), 229 _password.toString().toCharArray()); 230 231 KeyManagerFactory km = KeyManagerFactory.getInstance( "SunX509","SunJSSE"); 232 km.init( ks, _keypassword.toString().toCharArray() ); 233 KeyManager[] kma = km.getKeyManagers(); 234 235 TrustManagerFactory tm = TrustManagerFactory.getInstance("SunX509","SunJSSE"); 236 if (_useDefaultTrustStore) { 237 tm.init( (KeyStore )null ); 238 } else { 239 tm.init( ks ); 240 } 241 242 TrustManager[] tma = tm.getTrustManagers(); 243 244 SSLContext sslc = SSLContext.getInstance( "SSL" ); 245 sslc.init( kma, tma, SecureRandom.getInstance("SHA1PRNG")); 246 247 SSLServerSocketFactory ssfc = sslc.getServerSocketFactory(); 248 log.info("SSLServerSocketFactory="+ssfc); 249 return ssfc; 250 } 251 } 252 253 254 255 | Popular Tags |