1 10 11 package org.mule.providers.http; 12 13 import java.io.FileNotFoundException ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.security.KeyStore ; 17 import java.security.Provider ; 18 import java.security.Security ; 19 20 import javax.net.ssl.KeyManagerFactory; 21 import javax.net.ssl.TrustManagerFactory; 22 23 import org.mule.config.i18n.Message; 24 import org.mule.config.i18n.Messages; 25 import org.mule.umo.lifecycle.InitialisationException; 26 import org.mule.umo.security.provider.AutoDiscoverySecurityProviderFactory; 27 import org.mule.umo.security.provider.SecurityProviderFactory; 28 import org.mule.umo.security.provider.SecurityProviderInfo; 29 import org.mule.util.FileUtils; 30 import org.mule.util.IOUtils; 31 32 37 public class HttpsConnector extends HttpConnector 38 { 39 public static final String DEFAULT_KEYSTORE = ".keystore"; 40 public static final String DEFAULT_KEYSTORE_TYPE = KeyStore.getDefaultType(); 41 public static final String DEFAULT_SSL_TYPE = "SSLv3"; 42 43 private SecurityProviderFactory spFactory = new AutoDiscoverySecurityProviderFactory(); 44 private SecurityProviderInfo spInfo = spFactory.getSecurityProviderInfo(); 45 46 private String keyStore = DEFAULT_KEYSTORE; 47 private String keyPassword = null; 48 private String storePassword = null; 49 private String keystoreType = DEFAULT_KEYSTORE_TYPE; 50 private String keyManagerAlgorithm = spInfo.getKeyManagerAlgorithm(); 51 private String sslType = DEFAULT_SSL_TYPE; 52 private Provider provider = spFactory.getProvider(); 53 private String protocolHandler = spInfo.getProtocolHandler(); 54 private String clientKeyStore = null; 55 private String clientKeyStorePassword = null; 56 private String trustStore = null; 57 private String trustStorePassword = null; 58 private String trustStoreType = DEFAULT_KEYSTORE_TYPE; 59 private String trustManagerAlgorithm = spInfo.getKeyManagerAlgorithm(); 61 private TrustManagerFactory trustManagerFactory; 62 private boolean explicitTrustStoreOnly = false; 63 64 private KeyManagerFactory keyManagerFactory = null; 65 private boolean requireClientAuthentication = false; 66 67 public void doInitialise() throws InitialisationException 68 { 69 if (getProvider() == null) 70 { 71 throw new NullPointerException ("The security provider cannot be null"); 72 } 73 if (getKeyStore() == null) 74 { 75 throw new NullPointerException ("The KeyStore location cannot be null"); 76 } 77 if (getKeyPassword() == null) 78 { 79 throw new NullPointerException ("The Key password cannot be null"); 80 } 81 if (getStorePassword() == null) 82 { 83 throw new NullPointerException ("The KeyStore password cannot be null"); 84 } 85 if (getKeyManagerAlgorithm() == null) 86 { 87 throw new NullPointerException ("The Key Manager Algorithm cannot be null"); 88 } 89 90 KeyStore keystore; 91 try 92 { 93 Security.addProvider(getProvider()); 94 keystore = KeyStore.getInstance(keystoreType); 96 InputStream is = IOUtils.getResourceAsStream(getKeyStore(), getClass()); 97 if (is == null) 98 { 99 throw new FileNotFoundException (new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, 100 "Keystore: " + getKeyStore()).getMessage()); 101 } 102 keystore.load(is, getStorePassword().toCharArray()); 103 } 104 catch (Exception e) 105 { 106 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "KeyStore: " 107 + getKeyStore()), e, this); 108 } 109 try 110 { 111 keyManagerFactory = KeyManagerFactory.getInstance(getKeyManagerAlgorithm()); 113 keyManagerFactory.init(keystore, getKeyPassword().toCharArray()); 115 } 116 catch (Exception e) 117 { 118 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "Key Manager"), e, this); 119 } 120 121 if (getTrustStore() != null) 122 { 123 KeyStore truststore; 124 try 125 { 126 truststore = KeyStore.getInstance(trustStoreType); 127 InputStream is = IOUtils.getResourceAsStream(getTrustStore(), getClass()); 128 if (is == null) 129 { 130 throw new FileNotFoundException ( 131 "Failed to load truststore from classpath or local file: " + getTrustStore()); 132 } 133 truststore.load(is, getTrustStorePassword().toCharArray()); 134 } 135 catch (Exception e) 136 { 137 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "TrustStore: " 138 + getTrustStore()), e, 139 this); 140 } 141 142 try 143 { 144 trustManagerFactory = TrustManagerFactory.getInstance(getTrustManagerAlgorithm()); 145 trustManagerFactory.init(truststore); 146 } 147 catch (Exception e) 148 { 149 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, 150 "Trust Manager (" + getTrustManagerAlgorithm() + ")"), e, this); 151 } 152 } 153 154 super.doInitialise(); 155 156 if (protocolHandler != null) 157 { 158 System.setProperty("java.protocol.handler.pkgs", protocolHandler); 159 } 160 if (clientKeyStore != null) 161 { 162 try 163 { 164 String clientPath = FileUtils.getResourcePath(clientKeyStore, getClass()); 165 if (clientPath == null) 166 { 167 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "ClientKeyStore: " 168 + clientKeyStore), 169 this); 170 } 171 System.setProperty("javax.net.ssl.keyStore", clientPath); 172 System.setProperty("javax.net.ssl.keyStorePassword", clientKeyStorePassword); 173 174 logger.info("Set Client Key store: javax.net.ssl.keyStore=" + clientPath); 175 } 176 catch (IOException e) 177 { 178 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "ClientKeyStore: " 179 + clientKeyStore), this); 180 } 181 } 182 183 if (trustStore != null) 184 { 185 System.setProperty("javax.net.ssl.trustStore", getTrustStore()); 186 if (getTrustStorePassword() != null) 187 { 188 System.setProperty("javax.net.ssl.trustStorePassword", getTrustStorePassword()); 189 } 190 logger.debug("Set Trust store: javax.net.ssl.trustStore=" + getTrustStore()); 191 } 192 else if (!isExplicitTrustStoreOnly()) 193 { 194 logger.info("Defaulting trust store to client Key Store"); 195 trustStore = getClientKeyStore(); 196 trustStorePassword = getClientKeyStorePassword(); 197 if (trustStore != null) 198 { 199 System.setProperty("javax.net.ssl.trustStore", getTrustStore()); 200 System.setProperty("javax.net.ssl.trustStorePassword", getTrustStorePassword()); 201 logger.debug("Set Trust store: javax.net.ssl.trustStore=" + getTrustStore()); 202 } 203 } 204 205 } 206 207 public String getProtocol() 208 { 209 return "https"; 210 } 211 212 public String getKeyStore() 213 { 214 return keyStore; 215 } 216 217 public void setKeyStore(String keyStore) 218 { 219 this.keyStore = keyStore; 220 } 221 222 public String getKeyPassword() 223 { 224 return keyPassword; 225 } 226 227 public void setKeyPassword(String keyPassword) 228 { 229 this.keyPassword = keyPassword; 230 } 231 232 public String getStorePassword() 233 { 234 return storePassword; 235 } 236 237 public void setStorePassword(String storePassword) 238 { 239 this.storePassword = storePassword; 240 } 241 242 public String getTrustStoreType() 243 { 244 return trustStoreType; 245 } 246 247 public void setTrustStoreType(String trustStoreType) 248 { 249 this.trustStoreType = trustStoreType; 250 } 251 252 public TrustManagerFactory getTrustManagerFactory() 253 { 254 return trustManagerFactory; 255 } 256 257 public void setTrustManagerFactory(TrustManagerFactory trustManagerFactory) 258 { 259 this.trustManagerFactory = trustManagerFactory; 260 } 261 262 public String getTrustManagerAlgorithm() 263 { 264 return trustManagerAlgorithm; 265 } 266 267 public void setTrustManagerAlgorithm(String trustManagerAlgorithm) 268 { 269 this.trustManagerAlgorithm = trustManagerAlgorithm; 270 } 271 272 public String getKeystoreType() 273 { 274 return keystoreType; 275 } 276 277 public void setKeystoreType(String keystoreType) 278 { 279 this.keystoreType = keystoreType; 280 } 281 282 public String getKeyManagerAlgorithm() 283 { 284 return keyManagerAlgorithm; 285 } 286 287 public void setKeyManagerAlgorithm(String keyManagerAlgorithm) 288 { 289 this.keyManagerAlgorithm = keyManagerAlgorithm; 290 } 291 292 public String getSslType() 293 { 294 return sslType; 295 } 296 297 public void setSslType(String sslType) 298 { 299 this.sslType = sslType; 300 } 301 302 public boolean isRequireClientAuthentication() 303 { 304 return requireClientAuthentication; 305 } 306 307 public void setRequireClientAuthentication(boolean requireClientAuthentication) 308 { 309 this.requireClientAuthentication = requireClientAuthentication; 310 } 311 312 public KeyManagerFactory getKeyManagerFactory() 313 { 314 return keyManagerFactory; 315 } 316 317 public Provider getProvider() 318 { 319 return provider; 320 } 321 322 public void setProvider(Provider provider) 323 { 324 this.provider = provider; 325 } 326 327 public String getProtocolHandler() 328 { 329 return protocolHandler; 330 } 331 332 public void setProtocolHandler(String protocolHandler) 333 { 334 this.protocolHandler = protocolHandler; 335 } 336 337 public String getClientKeyStore() 338 { 339 return clientKeyStore; 340 } 341 342 public void setClientKeyStore(String clientKeyStore) throws IOException 343 { 344 this.clientKeyStore = clientKeyStore; 345 if (this.clientKeyStore != null) 346 { 347 this.clientKeyStore = FileUtils.getResourcePath(clientKeyStore, getClass()); 348 logger.debug("Normalised clientKeyStore path to: " + getClientKeyStore()); 349 } 350 } 351 352 public String getClientKeyStorePassword() 353 { 354 return clientKeyStorePassword; 355 } 356 357 public void setClientKeyStorePassword(String clientKeyStorePassword) 358 { 359 this.clientKeyStorePassword = clientKeyStorePassword; 360 } 361 362 public String getTrustStore() 363 { 364 return trustStore; 365 } 366 367 public void setTrustStore(String trustStore) throws IOException 368 { 369 this.trustStore = trustStore; 370 if (this.trustStore != null) 371 { 372 this.trustStore = FileUtils.getResourcePath(trustStore, getClass()); 373 logger.debug("Normalised trustStore path to: " + getTrustStore()); 374 } 375 } 376 377 public String getTrustStorePassword() 378 { 379 return trustStorePassword; 380 } 381 382 public void setTrustStorePassword(String trustStorePassword) 383 { 384 this.trustStorePassword = trustStorePassword; 385 } 386 387 public boolean isExplicitTrustStoreOnly() 388 { 389 return explicitTrustStoreOnly; 390 } 391 392 public void setExplicitTrustStoreOnly(boolean explicitTrustStoreOnly) 393 { 394 this.explicitTrustStoreOnly = explicitTrustStoreOnly; 395 } 396 397 public SecurityProviderFactory getSecurityProviderFactory() 398 { 399 return spFactory; 400 } 401 402 public void setSecurityProviderFactory(SecurityProviderFactory spFactory) 403 { 404 this.spFactory = spFactory; 405 } 406 } 407 | Popular Tags |