1 18 19 package org.apache.jmeter.util; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.lang.reflect.Constructor ; 24 import java.net.HttpURLConnection ; 25 import java.security.KeyStore ; 26 import java.security.Provider ; 27 import java.security.Security ; 28 29 import javax.swing.JOptionPane ; 30 31 import org.apache.jmeter.gui.GuiPackage; 32 import org.apache.jmeter.util.keystore.JmeterKeyStore; 33 import org.apache.jorphan.logging.LoggingManager; 34 import org.apache.log.Logger; 35 36 47 public abstract class SSLManager 48 { 49 transient private static Logger log = LoggingManager.getLoggerForClass(); 50 51 private static SSLManager manager; 52 private static boolean isIAIKProvider = false; 53 private static boolean isSSLSupported = false; 54 private static Provider sslProvider = null; 55 56 57 private JmeterKeyStore keyStore; 58 59 private KeyStore trustStore; 60 61 protected String defaultpw = 62 JMeterUtils.getJMeterProperties().getProperty( 63 "javax.net.ssl.keyStorePassword"); 64 65 68 static public void reset() 69 { 70 SSLManager.manager = null; 71 } 72 73 public abstract void setContext(HttpURLConnection conn); 74 75 78 protected void setProvider(Provider provider) 79 { 80 if ( null != provider ) 81 { 82 Security.addProvider( provider ); 83 } 84 } 85 86 87 92 protected JmeterKeyStore getKeyStore() 93 { 94 String password = this.defaultpw; 95 96 if (null == this.keyStore) 97 { 98 String defaultName = 99 JMeterUtils.getJMeterProperties().getProperty("user.home") 100 + File.separator 101 + ".keystore"; 102 String fileName = 103 JMeterUtils.getJMeterProperties().getProperty( 104 "javax.net.ssl.keyStore", 105 defaultName); 106 System.setProperty("javax.net.ssl.keyStore", fileName); 107 108 try 109 { 110 if (fileName.endsWith(".p12") || fileName.endsWith(".P12")) 111 { 112 this.keyStore = JmeterKeyStore.getInstance("pkcs12"); 113 log.info("KeyStore Type: PKCS 12"); 114 System.setProperty("javax.net.ssl.keyStoreType", "pkcs12"); 115 } 116 else 117 { 118 this.keyStore = JmeterKeyStore.getInstance("JKS"); 119 log.info("KeyStore Type: JKS"); 120 } 121 } 122 catch (Exception e) 123 { 124 JOptionPane.showMessageDialog( 125 GuiPackage.getInstance().getMainFrame(), 126 e, 127 JMeterUtils.getResString("ssl_error_title"), 128 JOptionPane.ERROR_MESSAGE); 129 this.keyStore = null; 130 throw new RuntimeException ("KeyStore Problem"); 131 } 132 133 if (null == password) 134 { 135 if (null == defaultpw) 136 { 137 this.defaultpw = 138 JMeterUtils.getJMeterProperties().getProperty( 139 "javax.net.ssl.keyStorePassword"); 140 141 if (null == defaultpw) 142 { 143 synchronized (this) 144 { 145 this.defaultpw = 146 JOptionPane.showInputDialog( 147 GuiPackage.getInstance().getMainFrame(), 148 JMeterUtils.getResString("ssl_pass_prompt"), 149 JMeterUtils.getResString("ssl_pass_title"), 150 JOptionPane.QUESTION_MESSAGE); 151 JMeterUtils.getJMeterProperties().setProperty( 152 "javax.net.ssl.keyStorePassword", 153 this.defaultpw); 154 } 155 } 156 } 157 158 password = this.defaultpw; 159 System.setProperty("javax.net.ssl.keyStorePassword", password); 160 } 161 162 try 163 { 164 File initStore = new File (fileName); 165 166 if (initStore.exists()) 167 { 168 this.keyStore.load(new FileInputStream (initStore),password); 169 } 170 else 171 { 172 this.keyStore.load(null, password); 173 } 174 } 175 catch (Exception e) 176 { 177 log.error("Couldn't load keystore" ,e); 178 } 179 180 log.info("JmeterKeyStore Location: " + fileName); 181 log.info("JmeterKeyStore type: " + this.keyStore.getClass().toString()); 182 } 183 184 return this.keyStore; 185 } 186 187 190 protected KeyStore getTrustStore() 191 { 192 if (null == this.trustStore) 193 { 194 String fileName = 195 JMeterUtils.getPropDefault("javax.net.ssl.trustStore", ""); 196 System.setProperty("javax.net.ssl.trustStore", fileName); 197 198 try 199 { 200 if (fileName.endsWith(".iaik")) 201 { 202 this.trustStore = 203 KeyStore.getInstance("IAIKKeyStore", "IAIK"); 204 } 205 else 206 { 207 this.trustStore = KeyStore.getInstance("JKS"); 208 log.info("KeyStore Type: JKS"); 209 } 210 } 211 catch (Exception e) 212 { 213 JOptionPane.showMessageDialog( 214 GuiPackage.getInstance().getMainFrame(), 215 e, 216 JMeterUtils.getResString("ssl_error_title"), 217 JOptionPane.ERROR_MESSAGE); 218 this.trustStore = null; 219 throw new RuntimeException ("TrustStore Problem"); 220 } 221 222 try 223 { 224 File initStore = new File (fileName); 225 226 if (initStore.exists()) 227 { 228 this.trustStore.load(new FileInputStream (initStore), null); 229 } 230 else 231 { 232 this.trustStore.load(null, null); 233 } 234 } 235 catch (Exception e) 236 { 237 throw new RuntimeException ( 238 "Can't load TrustStore: " + e.toString()); 239 } 240 241 log.info("TrustStore Location: " + fileName); 242 log.info("TrustStore type: " + this.keyStore.getClass().toString()); 243 } 244 245 return this.trustStore; 246 } 247 248 253 protected SSLManager() {} 254 255 259 public static final SSLManager getInstance() 260 { 261 if (null == SSLManager.manager) 262 { 263 if (SSLManager.isSSLSupported) 264 { 265 String classname = null; 266 if (SSLManager.isIAIKProvider) 267 { 268 classname = "org.apache.jmeter.util.IaikSSLManager"; 269 } 270 else 271 { 272 classname = "org.apache.jmeter.util.JsseSSLManager"; 273 } 274 275 try 276 { 277 Class clazz = Class.forName(classname); 278 Constructor con = 279 clazz.getConstructor(new Class [] { Provider .class }); 280 SSLManager.manager = 281 (SSLManager) con.newInstance( 282 new Object [] { SSLManager.sslProvider }); 283 } 284 catch (Exception e) 285 { 286 log.error("",e); 287 SSLManager.isSSLSupported = false; 288 return null; 289 } 290 } 291 } 292 293 return SSLManager.manager; 294 } 295 296 299 public static final boolean isSSLSupported() 300 { 301 return SSLManager.isSSLSupported; 302 } 303 304 static 307 { 308 SSLManager.isSSLSupported = true; 309 SSLManager.sslProvider = null; 310 356 } 357 } 358 | Popular Tags |