1 7 package org.jboss.remoting.security; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 import java.security.AccessController ; 15 import java.security.InvalidParameterException ; 16 import java.security.KeyManagementException ; 17 import java.security.KeyStore ; 18 import java.security.KeyStoreException ; 19 import java.security.NoSuchAlgorithmException ; 20 import java.security.PrivilegedAction ; 21 import java.security.UnrecoverableKeyException ; 22 import java.security.cert.CertificateException ; 23 import javax.net.ServerSocketFactory; 24 import javax.net.SocketFactory; 25 import javax.net.ssl.KeyManager; 26 import javax.net.ssl.KeyManagerFactory; 27 import javax.net.ssl.SSLContext; 28 import javax.net.ssl.SSLServerSocketFactory; 29 import javax.net.ssl.SSLSocketFactory; 30 import javax.net.ssl.TrustManager; 31 import javax.net.ssl.TrustManagerFactory; 32 33 60 public class SSLSocketBuilder implements SSLSocketBuilderMBean 61 { 62 65 public static final String DEFAULT_SECURE_SOCKET_PROTOCOL = "TLS"; 66 69 public static final String DEFAULT_KEY_MANAGEMENT_ALGORITHM = "SunX509"; 70 73 public static final String DEFAULT_KEY_STORE_TYPE = "JKS"; 74 75 78 private String secureSocketProtocol = DEFAULT_SECURE_SOCKET_PROTOCOL; 79 80 83 private String keyManagementAlgorithm = DEFAULT_KEY_MANAGEMENT_ALGORITHM; 84 85 91 private String keyStoreType = DEFAULT_KEY_STORE_TYPE; 92 93 private boolean useSSLServerSocketFactory = true; 94 private boolean useSSLSocketFactory = true; 95 private char[] keyStorePassword = null; 96 private char[] keyPassword = null; 97 private URL keyStoreURL = null; 98 private URL trustStoreURL = null; 99 100 public SSLSocketBuilder() 101 { 102 } 103 104 118 public void setUseSSLServerSocketFactory(boolean shouldUse) 119 { 120 this.useSSLServerSocketFactory = shouldUse; 121 } 122 123 129 public boolean getUseSSLServerSocketFactory() 130 { 131 return useSSLServerSocketFactory; 132 } 133 134 147 public void setUseSSLSocketFactory(boolean shouldUse) 148 { 149 this.useSSLSocketFactory = shouldUse; 150 } 151 152 158 public boolean getUseSSLSocketFactory() 159 { 160 return useSSLSocketFactory; 161 } 162 163 167 public String getSecureSocketProtocol() 168 { 169 return secureSocketProtocol; 170 } 171 172 176 public void setSecureSocketProtocol(String secureSocketProtocol) 177 { 178 this.secureSocketProtocol = secureSocketProtocol; 179 } 180 181 185 public String getKeyManagementAlgorithm() 186 { 187 return keyManagementAlgorithm; 188 } 189 190 194 public void setKeyManagementAlgorithm(String keyManagementAlgorithm) 195 { 196 this.keyManagementAlgorithm = keyManagementAlgorithm; 197 } 198 199 206 public String getKeyStoreType() 207 { 208 return keyStoreType; 209 } 210 211 218 public void setKeyStoreType(String keyStoreType) 219 { 220 this.keyStoreType = keyStoreType; 221 } 222 223 230 public void setKeyStorePassword(String passphrase) 231 { 232 if(passphrase != null && passphrase.length() > 0) 233 { 234 keyStorePassword = passphrase.toCharArray(); 235 } 236 else 237 { 238 throw new InvalidParameterException ("Must enter a non null key store passphrase with at least one character."); 239 } 240 } 241 242 249 public void setKeyPassword(String passphrase) 250 { 251 if(passphrase != null && passphrase.length() > 0) 252 { 253 keyPassword = passphrase.toCharArray(); 254 } 255 else 256 { 257 throw new InvalidParameterException ("Must enter a non null key passphrase with at least one character."); 258 } 259 } 260 261 274 public ServerSocketFactory createSSLServerSocketFactory() 275 throws IOException , NoSuchAlgorithmException , KeyStoreException , 276 CertificateException , UnrecoverableKeyException , KeyManagementException 277 { 278 ServerSocketFactory ssf = null; 279 280 if(useSSLServerSocketFactory) 281 { 282 ssf = SSLServerSocketFactory.getDefault(); 283 } 284 else 285 { 286 ssf = createCustomServerSocketFactory(); 287 } 288 289 return ssf; 290 } 291 292 305 private ServerSocketFactory createCustomServerSocketFactory() 306 throws NoSuchAlgorithmException , KeyStoreException , IOException , 307 CertificateException , UnrecoverableKeyException , KeyManagementException 308 { 309 ServerSocketFactory ssf = null; 310 311 SSLContext sslContext = SSLContext.getInstance(secureSocketProtocol); 312 KeyManagerFactory keyMgrFactory = getKeyManagerFactory(); 313 314 KeyManager[] keyManagers = keyMgrFactory.getKeyManagers(); 315 sslContext.init(keyManagers, null, null); 316 ssf = sslContext.getServerSocketFactory(); 317 318 return ssf; 319 } 320 321 333 public SocketFactory createSSLSocketFactory() 334 throws IOException , NoSuchAlgorithmException , KeyStoreException , 335 CertificateException , KeyManagementException 336 { 337 SocketFactory sf = null; 338 339 if(useSSLSocketFactory) 340 { 341 sf = SSLSocketFactory.getDefault(); 342 } 343 else 344 { 345 sf = createCustomSocketFactory(); 346 } 347 348 return sf; 349 } 350 351 352 private SocketFactory createCustomSocketFactory() 353 throws NoSuchAlgorithmException , IOException , CertificateException , KeyStoreException , KeyManagementException 354 { 355 SocketFactory sf = null; 356 357 SSLContext sslContext = SSLContext.getInstance(secureSocketProtocol); 358 TrustManagerFactory trustMgrFactory = getTrustManagerFactory(); 359 360 TrustManager[] trustManagers = trustMgrFactory.getTrustManagers(); 361 sslContext.init(null, trustManagers, null); 362 sf = sslContext.getSocketFactory(); 363 364 return sf; 365 } 366 367 private TrustManagerFactory getTrustManagerFactory() 368 throws NoSuchAlgorithmException , IOException , CertificateException , KeyStoreException 369 { 370 TrustManagerFactory truestMgrFactory = null; 371 372 truestMgrFactory = TrustManagerFactory.getInstance(keyManagementAlgorithm); 373 KeyStore keyStore = getKeyStore(trustStoreURL); 374 truestMgrFactory.init(keyStore); 375 376 return truestMgrFactory; 377 378 } 379 380 private KeyManagerFactory getKeyManagerFactory() 381 throws NoSuchAlgorithmException , KeyStoreException , IOException , CertificateException , UnrecoverableKeyException 382 { 383 KeyManagerFactory keyMgrFactory = null; 384 385 keyMgrFactory = KeyManagerFactory.getInstance(keyManagementAlgorithm); 386 KeyStore keyStore = getKeyStore(keyStoreURL); 387 keyMgrFactory.init(keyStore, keyPassword); 388 389 return keyMgrFactory; 390 391 } 392 393 private KeyStore getKeyStore(URL storeURL) throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException 394 { 395 KeyStore keyStore = KeyStore.getInstance(keyStoreType); 396 if(storeURL == null) 397 { 398 throw new IOException ("Can not create SSL Server Socket Factory due to the url to the key store not being set."); 399 } 400 InputStream is = storeURL.openStream(); 401 keyStore.load(is, keyStorePassword); 402 403 if(keyPassword == null || keyPassword.length > 0) 405 { 406 keyPassword = keyStorePassword; 407 } 408 409 return keyStore; 410 411 } 412 413 421 public void setKeyStoreURL(String storeURL) throws IOException 422 { 423 this.keyStoreURL = this.validateStoreURL(storeURL); 424 } 425 426 434 public void setTrustStoreURL(String storeURL) throws IOException 435 { 436 this.trustStoreURL = this.validateStoreURL(storeURL); 437 } 438 439 private URL validateStoreURL(String storeURL) throws IOException 440 { 441 URL url = null; 442 try 444 { 445 url = new URL (storeURL); 446 } 447 catch(MalformedURLException e) 448 { 449 } 451 452 if(url == null) 454 { 455 File tst = new File (storeURL); 456 if(tst.exists() == true) 457 { 458 url = tst.toURL(); 459 } 460 } 461 462 if(url == null) 464 { 465 ClassLoader loader = getContextClassLoader(); 466 url = loader.getResource(storeURL); 467 } 468 469 if(url == null) 471 { 472 String msg = "Failed to find url=" + storeURL + " as a URL, file or resource"; 473 throw new MalformedURLException (msg); 474 } 475 return url; 476 } 477 478 481 482 485 public void create() throws Exception 486 { 487 } 489 490 493 public void start() throws Exception 494 { 495 } 497 498 501 public void stop() 502 { 503 } 505 506 509 public void destroy() 510 { 511 } 513 514 515 static ClassLoader getContextClassLoader() 517 { 518 ClassLoader loader = (ClassLoader ) AccessController.doPrivileged(GetTCLAction.ACTION); 519 return loader; 520 } 521 522 private static class GetTCLAction implements PrivilegedAction 523 { 524 static PrivilegedAction ACTION = new GetTCLAction(); 525 526 public Object run() 527 { 528 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 529 return loader; 530 } 531 } 532 533 } | Popular Tags |