1 7 8 package javax.security.sasl; 9 10 import javax.security.auth.callback.CallbackHandler ; 11 12 import java.util.Enumeration ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 import java.util.HashSet ; 17 import java.util.Collections ; 18 import java.security.Provider ; 19 import java.security.Security ; 20 21 46 public class Sasl { 47 private Sasl() { 49 } 50 51 67 public static final String QOP = "javax.security.sasl.qop"; 68 69 89 public static final String STRENGTH = "javax.security.sasl.strength"; 90 91 100 public static final String SERVER_AUTH = 101 "javax.security.sasl.server.authentication"; 102 103 111 public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer"; 112 113 121 public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize"; 122 123 148 public static final String REUSE = "javax.security.sasl.reuse"; 149 150 160 public static final String POLICY_NOPLAINTEXT = 161 "javax.security.sasl.policy.noplaintext"; 162 163 174 public static final String POLICY_NOACTIVE = 175 "javax.security.sasl.policy.noactive"; 176 177 188 public static final String POLICY_NODICTIONARY = 189 "javax.security.sasl.policy.nodictionary"; 190 191 201 public static final String POLICY_NOANONYMOUS = 202 "javax.security.sasl.policy.noanonymous"; 203 204 217 public static final String POLICY_FORWARD_SECRECY = 218 "javax.security.sasl.policy.forward"; 219 220 230 public static final String POLICY_PASS_CREDENTIALS = 231 "javax.security.sasl.policy.credentials"; 232 233 234 310 public static SaslClient createSaslClient( 311 String [] mechanisms, 312 String authorizationId, 313 String protocol, 314 String serverName, 315 Map <String ,?> props, 316 CallbackHandler cbh) throws SaslException { 317 318 SaslClient mech = null; 319 SaslClientFactory fac; 320 String className; 321 String mechName; 322 323 for (int i = 0; i < mechanisms.length; i++) { 324 if ((mechName=mechanisms[i]) == null) { 325 throw new NullPointerException ( 326 "Mechanism name cannot be null"); 327 } else if (mechName.length() == 0) { 328 continue; 329 } 330 String mechFilter = "SaslClientFactory." + mechName; 331 Provider [] provs = Security.getProviders(mechFilter); 332 for (int j = 0; provs != null && j < provs.length; j++) { 333 className = provs[j].getProperty(mechFilter); 334 if (className == null) { 335 continue; 337 } 338 339 fac = (SaslClientFactory ) loadFactory(provs[j], className); 340 if (fac != null) { 341 mech = fac.createSaslClient( 342 new String []{mechanisms[i]}, authorizationId, 343 protocol, serverName, props, cbh); 344 if (mech != null) { 345 return mech; 346 } 347 } 348 } 349 } 350 351 return null; 352 } 353 354 private static Object loadFactory(Provider p, String className) 355 throws SaslException { 356 try { 357 366 ClassLoader cl = p.getClass().getClassLoader(); 367 Class implClass; 368 implClass = Class.forName(className, true, cl); 369 return implClass.newInstance(); 370 } catch (ClassNotFoundException e) { 371 throw new SaslException ("Cannot load class " + className, e); 372 } catch (InstantiationException e) { 373 throw new SaslException ("Cannot instantiate class " + className, e); 374 } catch (IllegalAccessException e) { 375 throw new SaslException ("Cannot access class " + className, e); 376 } catch (SecurityException e) { 377 throw new SaslException ("Cannot access class " + className, e); 378 } 379 } 380 381 382 449 public static SaslServer 450 createSaslServer(String mechanism, 451 String protocol, 452 String serverName, 453 Map <String ,?> props, 454 javax.security.auth.callback.CallbackHandler cbh) 455 throws SaslException { 456 457 SaslServer mech = null; 458 SaslServerFactory fac; 459 String className; 460 461 if (mechanism == null) { 462 throw new NullPointerException ("Mechanism name cannot be null"); 463 } else if (mechanism.length() == 0) { 464 return null; 465 } 466 467 String mechFilter = "SaslServerFactory." + mechanism; 468 Provider [] provs = Security.getProviders(mechFilter); 469 for (int j = 0; provs != null && j < provs.length; j++) { 470 className = provs[j].getProperty(mechFilter); 471 if (className == null) { 472 throw new SaslException ("Provider does not support " + 473 mechFilter); 474 } 475 fac = (SaslServerFactory ) loadFactory(provs[j], className); 476 if (fac != null) { 477 mech = fac.createSaslServer( 478 mechanism, protocol, serverName, props, cbh); 479 if (mech != null) { 480 return mech; 481 } 482 } 483 } 484 485 return null; 486 } 487 488 496 public static Enumeration <SaslClientFactory > getSaslClientFactories() { 497 Set facs = getFactories("SaslClientFactory"); 498 final Iterator iter = facs.iterator(); 499 return new Enumeration <SaslClientFactory >() { 500 public boolean hasMoreElements() { 501 return iter.hasNext(); 502 } 503 public SaslClientFactory nextElement() { 504 return (SaslClientFactory )iter.next(); 505 } 506 }; 507 } 508 509 517 public static Enumeration <SaslServerFactory > getSaslServerFactories() { 518 Set facs = getFactories("SaslServerFactory"); 519 final Iterator iter = facs.iterator(); 520 return new Enumeration <SaslServerFactory >() { 521 public boolean hasMoreElements() { 522 return iter.hasNext(); 523 } 524 public SaslServerFactory nextElement() { 525 return (SaslServerFactory )iter.next(); 526 } 527 }; 528 } 529 530 private static Set getFactories(String serviceName) { 531 HashSet result = new HashSet (); 532 533 if ((serviceName == null) || (serviceName.length() == 0) || 534 (serviceName.endsWith("."))) { 535 return result; 536 } 537 538 539 Provider [] providers = Security.getProviders(); 540 HashSet classes = new HashSet (); 541 Object fac; 542 543 for (int i = 0; i < providers.length; i++) { 544 classes.clear(); 545 546 for (Enumeration e = providers[i].keys(); e.hasMoreElements(); ) { 548 String currentKey = (String )e.nextElement(); 549 if (currentKey.startsWith(serviceName)) { 550 if (currentKey.indexOf(" ") < 0) { 557 String className = providers[i].getProperty(currentKey); 558 if (!classes.contains(className)) { 559 classes.add(className); 560 try { 561 fac = loadFactory(providers[i], className); 562 if (fac != null) { 563 result.add(fac); 564 } 565 }catch (Exception ignore) { 566 } 567 } 568 } 569 } 570 } 571 } 572 return Collections.unmodifiableSet(result); 573 } 574 } 575
| Popular Tags
|