1 18 19 package org.apache.jmeter.util; 20 import java.net.HttpURLConnection; 21 import java.net.Socket; 22 import java.security.KeyStore; 23 import java.security.Principal; 24 import java.security.PrivateKey; 25 import java.security.Provider; 26 import java.security.SecureRandom; 27 import java.security.cert.CertificateException; 28 import java.security.cert.X509Certificate; 29 30 import org.apache.jmeter.util.keystore.JmeterKeyStore; 31 import org.apache.jorphan.logging.LoggingManager; 32 import org.apache.log.Logger; 33 34 import com.sun.net.ssl.HostnameVerifier; 35 import com.sun.net.ssl.HttpsURLConnection; 36 import com.sun.net.ssl.KeyManager; 37 import com.sun.net.ssl.KeyManagerFactory; 38 import com.sun.net.ssl.SSLContext; 39 import com.sun.net.ssl.TrustManager; 40 import com.sun.net.ssl.X509KeyManager; 41 import com.sun.net.ssl.X509TrustManager; 42 43 55 public class JsseSSLManager extends SSLManager 56 { 57 transient private static Logger log = LoggingManager.getLoggerForClass(); 58 59 62 private SecureRandom rand; 63 66 private SSLContext context = null; 67 private Provider pro = null; 68 75 public JsseSSLManager(Provider provider) 76 { 77 log.debug("ssl Provider = " + provider); 78 setProvider(provider); 79 try 80 { 81 Class iaikProvider = 82 SSLManager.class.getClassLoader().loadClass( 83 "iaik.security.jsse.provider.IAIKJSSEProvider"); 84 setProvider((Provider) iaikProvider.newInstance()); 85 } 86 catch (Exception e) 87 {} 88 if (null == this.rand) 89 { 90 this.rand = new SecureRandom(); 91 } 92 93 if ("all" 94 .equalsIgnoreCase( 95 JMeterUtils.getPropDefault("javax.net.debug", "none"))) 96 { 97 System.setProperty("javax.net.debug", "all"); 98 } 99 this.getContext(); 100 log.info("JsseSSLManager installed"); 101 } 102 103 108 public void setContext(HttpURLConnection conn) 109 { 110 if(conn instanceof com.sun.net.ssl.HttpsURLConnection) 111 { 112 com.sun.net.ssl.HttpsURLConnection secureConn = 113 (com.sun.net.ssl.HttpsURLConnection) conn; 114 secureConn.setSSLSocketFactory( 115 this.getContext().getSocketFactory()); 116 } 117 else if ( 118 conn instanceof sun.net.www.protocol.https.HttpsURLConnectionImpl) 119 { 120 sun.net.www.protocol.https.HttpsURLConnectionImpl secureConn = 121 (sun.net.www.protocol.https.HttpsURLConnectionImpl) conn; 122 secureConn.setSSLSocketFactory( 123 this.getContext().getSocketFactory()); 124 } 125 } 126 127 132 protected final void setProvider(Provider p) 133 { 134 super.setProvider(p); 135 if (null == this.pro) 136 { 137 this.pro = p; 138 } 139 } 140 141 147 private SSLContext getContext() 148 { 149 if (null == this.context) 150 { 151 try 152 { 153 if (pro != null) 154 { 155 this.context = SSLContext.getInstance("TLS",pro); 156 } 157 else 158 { 159 this.context = SSLContext.getInstance("TLS"); 160 } 161 log.debug("SSL context = " + context); 162 } 163 catch (Exception ee) 164 { 165 log.error("Exception occurred",ee); 166 } 167 try 168 { 169 KeyManagerFactory managerFactory = 170 KeyManagerFactory.getInstance("SunX509"); 171 JmeterKeyStore keys = this.getKeyStore(); 172 managerFactory.init(null, this.defaultpw.toCharArray()); 173 KeyManager[] managers = managerFactory.getKeyManagers(); 174 log.info(keys.getClass().toString()); 175 for (int i = 0; i < managers.length; i++) 176 { 177 if (managers[i] instanceof X509KeyManager) 178 { 179 X509KeyManager manager = (X509KeyManager) managers[i]; 180 managers[i] = new WrappedX509KeyManager(manager, keys); 181 } 182 } 183 TrustManager[] trusts = 184 new TrustManager[] { 185 new AlwaysTrustManager(this.getTrustStore())}; 186 context.init(managers, trusts, this.rand); 187 HttpsURLConnection.setDefaultSSLSocketFactory( 188 context.getSocketFactory()); 189 HttpsURLConnection 190 .setDefaultHostnameVerifier(new HostnameVerifier() 191 { 192 public boolean verify( 193 String urlHostname, 194 String certHostname) 195 { 196 return true; 197 } 198 }); 199 log.debug("SSL stuff all set"); 200 } 201 catch (Exception e) 202 { 203 log.error("Exception occurred",e); 204 } 205 206 String[] dCiphers = 207 this.context.getSocketFactory().getDefaultCipherSuites(); 208 String[] sCiphers = 209 this.context.getSocketFactory().getSupportedCipherSuites(); 210 int len = 211 (dCiphers.length > sCiphers.length) 212 ? dCiphers.length 213 : sCiphers.length; 214 for (int i = 0; i < len; i++) 215 { 216 if (i < dCiphers.length) 217 { 218 log.info("Default Cipher: " + dCiphers[i]); 219 } 220 if (i < sCiphers.length) 221 { 222 log.info("Supported Cipher: " + sCiphers[i]); 223 } 224 } 225 } 226 return this.context; 227 } 228 229 233 protected static class AlwaysTrustManager implements X509TrustManager 234 { 235 238 protected X509Certificate[] certs; 239 244 public AlwaysTrustManager(KeyStore store) 245 { 246 try 247 { 248 java.util.Enumeration enum = store.aliases(); 249 java.util.ArrayList list = 250 new java.util.ArrayList(store.size()); 251 while (enum.hasMoreElements()) 252 { 253 String alias = (String) enum.nextElement(); 254 log.info("AlwaysTrustManager alias: " + alias); 255 if (store.isCertificateEntry(alias)) 256 { 257 list.add(store.getCertificate(alias)); 258 log.info(" INSTALLED"); 259 } 260 else 261 { 262 log.info(" SKIPPED"); 263 } 264 } 265 this.certs = 266 (X509Certificate[]) list.toArray(new X509Certificate[] { 267 }); 268 } 269 catch (Exception e) 270 { 271 this.certs = null; 272 } 273 } 274 275 280 public X509Certificate[] getAcceptedIssuers() 281 { 282 log.info("Get accepted Issuers"); 283 return certs; 284 } 285 288 public void checkClientTrusted(X509Certificate[] arg0, String arg1) 289 throws CertificateException 290 {} 291 292 295 public void checkServerTrusted(X509Certificate[] arg0, String arg1) 296 throws CertificateException 297 {} 298 299 public boolean isClientTrusted(X509Certificate[] arg0) 300 { 301 return true; 303 } 304 305 public boolean isServerTrusted(X509Certificate[] arg0) 306 { 307 return true; 309 } 310 311 } 312 319 private static class WrappedX509KeyManager implements X509KeyManager 320 { 321 324 private final X509KeyManager manager; 325 328 private final JmeterKeyStore store; 329 335 public WrappedX509KeyManager(X509KeyManager parent, JmeterKeyStore ks) 336 { 337 this.manager = parent; 338 this.store = ks; 339 } 340 350 public String[] getClientAliases(String keyType, Principal[] issuers) 351 { 352 log.info("WrappedX509Manager: getClientAliases: "); 353 log.info(this.store.getAlias()); 354 return new String[] { this.store.getAlias()}; 355 } 356 366 public String[] getServerAliases(String keyType, Principal[] issuers) 367 { 368 log.info("WrappedX509Manager: getServerAliases: "); 369 log.info( 370 this.manager.getServerAliases(keyType, issuers).toString()); 371 return this.manager.getServerAliases(keyType, issuers); 372 } 373 374 380 public X509Certificate[] getCertificateChain(String alias) 381 { 382 log.info("WrappedX509Manager: getCertificateChain(" + alias + ")"); 383 log.info(this.store.getCertificateChain().toString()); 384 return this.store.getCertificateChain(); 385 } 386 387 393 public PrivateKey getPrivateKey(String alias) 394 { 395 log.info( 396 "WrappedX509Manager: getPrivateKey: " 397 + this.store.getPrivateKey()); 398 return this.store.getPrivateKey(); 399 } 400 401 411 public String chooseClientAlias( 412 String[] arg0, 413 Principal[] arg1, 414 Socket arg2) 415 { 416 log.info("Alias: " + this.store.getAlias()); 417 return this.store.getAlias(); 418 } 419 420 425 public String chooseServerAlias( 426 String arg0, 427 Principal[] arg1, 428 Socket arg2) 429 { 430 return this.manager.chooseServerAlias(arg0, arg1); 431 } 432 433 public String chooseClientAlias(String arg0, Principal[] arg1) 434 { 435 return store.getAlias(); 436 } 437 438 public String chooseServerAlias(String arg0, Principal[] arg1) 439 { 440 return manager.chooseServerAlias(arg0,arg1); 441 } 442 } 443 } 444 | Popular Tags |