1. /*
  2. * @(#)Sasl.java 1.21 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.sasl;
  8. import javax.security.auth.callback.CallbackHandler;
  9. import java.util.Enumeration;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import java.util.HashSet;
  14. import java.util.Collections;
  15. import java.security.Provider;
  16. import java.security.Security;
  17. /**
  18. * A static class for creating SASL clients and servers.
  19. *<p>
  20. * This class defines the policy of how to locate, load, and instantiate
  21. * SASL clients and servers.
  22. *<p>
  23. * For example, an application or library gets a SASL client by doing
  24. * something like:
  25. *<blockquote><pre>
  26. * SaslClient sc = Sasl.createSaslClient(mechanisms,
  27. * authorizationId, protocol, serverName, props, callbackHandler);
  28. *</pre></blockquote>
  29. * It can then proceed to use the instance to create an authentication connection.
  30. *<p>
  31. * Similarly, a server gets a SASL server by using code that looks as follows:
  32. *<blockquote><pre>
  33. * SaslServer ss = Sasl.createSaslServer(mechanism,
  34. * protocol, serverName, props, callbackHandler);
  35. *</pre></blockquote>
  36. *
  37. * @since 1.5
  38. *
  39. * @author Rosanna Lee
  40. * @author Rob Weltman
  41. */
  42. public class Sasl {
  43. // Cannot create one of these
  44. private Sasl() {
  45. }
  46. /**
  47. * The name of a property that specifies the quality-of-protection to use.
  48. * The property contains a comma-separated, ordered list
  49. * of quality-of-protection values that the
  50. * client or server is willing to support. A qop value is one of
  51. * <ul>
  52. * <li><tt>"auth"</tt> - authentication only</li>
  53. * <li><tt>"auth-int"</tt> - authentication plus integrity protection</li>
  54. * <li><tt>"auth-conf"</tt> - authentication plus integrity and confidentiality
  55. * protection</li>
  56. * </ul>
  57. *
  58. * The order of the list specifies the preference order of the client or
  59. * server. If this property is absent, the default qop is <tt>"auth"</tt>.
  60. * The value of this constant is <tt>"javax.security.sasl.qop"</tt>.
  61. */
  62. public static final String QOP = "javax.security.sasl.qop";
  63. /**
  64. * The name of a property that specifies the cipher strength to use.
  65. * The property contains a comma-separated, ordered list
  66. * of cipher strength values that
  67. * the client or server is willing to support. A strength value is one of
  68. * <ul>
  69. * <li><tt>"low"</tt></li>
  70. * <li><tt>"medium"</tt></li>
  71. * <li><tt>"high"</tt></li>
  72. * </ul>
  73. * The order of the list specifies the preference order of the client or
  74. * server. An implementation should allow configuration of the meaning
  75. * of these values. An application may use the Java Cryptography
  76. * Extension (JCE) with JCE-aware mechanisms to control the selection of
  77. *cipher suites that match the strength values.
  78. * <BR>
  79. * If this property is absent, the default strength is
  80. * <tt>"high,medium,low"</tt>.
  81. * The value of this constant is <tt>"javax.security.sasl.strength"</tt>.
  82. */
  83. public static final String STRENGTH = "javax.security.sasl.strength";
  84. /**
  85. * The name of a property that specifies whether the
  86. * server must authenticate to the client. The property contains
  87. * <tt>"true"</tt> if the server must
  88. * authenticate the to client; <tt>"false"</tt> otherwise.
  89. * The default is <tt>"false"</tt>.
  90. * <br>The value of this constant is
  91. * <tt>"javax.security.sasl.server.authentication"</tt>.
  92. */
  93. public static final String SERVER_AUTH =
  94. "javax.security.sasl.server.authentication";
  95. /**
  96. * The name of a property that specifies the maximum size of the receive
  97. * buffer in bytes of <tt>SaslClient</tt>/<tt>SaslServer</tt>.
  98. * The property contains the string representation of an integer.
  99. * <br>If this property is absent, the default size
  100. * is defined by the mechanism.
  101. * <br>The value of this constant is <tt>"javax.security.sasl.maxbuffer"</tt>.
  102. */
  103. public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer";
  104. /**
  105. * The name of a property that specifies the maximum size of the raw send
  106. * buffer in bytes of <tt>SaslClient</tt>/<tt>SaslServer</tt>.
  107. * The property contains the string representation of an integer.
  108. * The value of this property is negotiated between the client and server
  109. * during the authentication exchange.
  110. * <br>The value of this constant is <tt>"javax.security.sasl.rawsendsize"</tt>.
  111. */
  112. public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize";
  113. /**
  114. * The name of a property that specifies whether to reuse previously
  115. * authenticated session information. The property contains "true" if the
  116. * mechanism implementation may attempt to reuse previously authenticated
  117. * session information; it contains "false" if the implementation must
  118. * not reuse previously authenticated session information. A setting of
  119. * "true" serves only as a hint: it does not necessarily entail actual
  120. * reuse because reuse might not be possible due to a number of reasons,
  121. * including, but not limited to, lack of mechanism support for reuse,
  122. * expiration of reusable information, and the peer's refusal to support
  123. * reuse.
  124. *
  125. * The property's default value is "false". The value of this constant
  126. * is "javax.security.sasl.reuse".
  127. *
  128. * Note that all other parameters and properties required to create a
  129. * SASL client/server instance must be provided regardless of whether
  130. * this property has been supplied. That is, you cannot supply any less
  131. * information in anticipation of reuse.
  132. *
  133. * Mechanism implementations that support reuse might allow customization
  134. * of its implementation, for factors such as cache size, timeouts, and
  135. * criteria for reuseability. Such customizations are
  136. * implementation-dependent.
  137. */
  138. public static final String REUSE = "javax.security.sasl.reuse";
  139. /**
  140. * The name of a property that specifies
  141. * whether mechanisms susceptible to simple plain passive attacks (e.g.,
  142. * "PLAIN") are not permitted. The property
  143. * contains <tt>"true"</tt> if such mechanisms are not permitted;
  144. * <tt>"false"</tt> if such mechanisms are permitted.
  145. * The default is <tt>"false"</tt>.
  146. * <br>The value of this constant is
  147. * <tt>"javax.security.sasl.policy.noplaintext"</tt>.
  148. */
  149. public static final String POLICY_NOPLAINTEXT =
  150. "javax.security.sasl.policy.noplaintext";
  151. /**
  152. * The name of a property that specifies whether
  153. * mechanisms susceptible to active (non-dictionary) attacks
  154. * are not permitted.
  155. * The property contains <tt>"true"</tt>
  156. * if mechanisms susceptible to active attacks
  157. * are not permitted; <tt>"false"</tt> if such mechanisms are permitted.
  158. * The default is <tt>"false"</tt>.
  159. * <br>The value of this constant is
  160. * <tt>"javax.security.sasl.policy.noactive"</tt>.
  161. */
  162. public static final String POLICY_NOACTIVE =
  163. "javax.security.sasl.policy.noactive";
  164. /**
  165. * The name of a property that specifies whether
  166. * mechanisms susceptible to passive dictionary attacks are not permitted.
  167. * The property contains <tt>"true"</tt>
  168. * if mechanisms susceptible to dictionary attacks are not permitted;
  169. * <tt>"false"</tt> if such mechanisms are permitted.
  170. * The default is <tt>"false"</tt>.
  171. *<br>
  172. * The value of this constant is
  173. * <tt>"javax.security.sasl.policy.nodictionary"</tt>.
  174. */
  175. public static final String POLICY_NODICTIONARY =
  176. "javax.security.sasl.policy.nodictionary";
  177. /**
  178. * The name of a property that specifies whether mechanisms that accept
  179. * anonymous login are not permitted. The property contains <tt>"true"</tt>
  180. * if mechanisms that accept anonymous login are not permitted;
  181. * <tt>"false"</tt>
  182. * if such mechanisms are permitted. The default is <tt>"false"</tt>.
  183. *<br>
  184. * The value of this constant is
  185. * <tt>"javax.security.sasl.policy.noanonymous"</tt>.
  186. */
  187. public static final String POLICY_NOANONYMOUS =
  188. "javax.security.sasl.policy.noanonymous";
  189. /**
  190. * The name of a property that specifies whether mechanisms that implement
  191. * forward secrecy between sessions are required. Forward secrecy
  192. * means that breaking into one session will not automatically
  193. * provide information for breaking into future sessions.
  194. * The property
  195. * contains <tt>"true"</tt> if mechanisms that implement forward secrecy
  196. * between sessions are required; <tt>"false"</tt> if such mechanisms
  197. * are not required. The default is <tt>"false"</tt>.
  198. *<br>
  199. * The value of this constant is
  200. * <tt>"javax.security.sasl.policy.forward"</tt>.
  201. */
  202. public static final String POLICY_FORWARD_SECRECY =
  203. "javax.security.sasl.policy.forward";
  204. /**
  205. * The name of a property that specifies whether
  206. * mechanisms that pass client credentials are required. The property
  207. * contains <tt>"true"</tt> if mechanisms that pass
  208. * client credentials are required; <tt>"false"</tt>
  209. * if such mechanisms are not required. The default is <tt>"false"</tt>.
  210. *<br>
  211. * The value of this constant is
  212. * <tt>"javax.security.sasl.policy.credentials"</tt>.
  213. */
  214. public static final String POLICY_PASS_CREDENTIALS =
  215. "javax.security.sasl.policy.credentials";
  216. /**
  217. * Creates a <tt>SaslClient</tt> using the parameters supplied.
  218. *
  219. * This method uses the
  220. <a href="http://java.sun.com/j2se/1.4/docs/guide/security/CryptoSpec.html#Provider">JCA Security Provider Framework</a>, described in the
  221. * "Java Cryptography Architecture API Specification & Reference", for
  222. * locating and selecting a <tt>SaslClient</tt> implementation.
  223. *
  224. * First, it
  225. * obtains an ordered list of <tt>SaslClientFactory</tt> instances from
  226. * the registered security providers for the "SaslClientFactory" service
  227. * and the specified SASL mechanism(s). It then invokes
  228. * <tt>createSaslClient()</tt> on each factory instance on the list
  229. * until one produces a non-null <tt>SaslClient</tt> instance. It returns
  230. * the non-null <tt>SaslClient</tt> instance, or null if the search fails
  231. * to produce a non-null <tt>SaslClient</tt> instance.
  232. *<p>
  233. * A security provider for SaslClientFactory registers with the
  234. * JCA Security Provider Framework keys of the form <br>
  235. * <tt>SaslClientFactory.<em>mechanism_name</em></tt>
  236. * <br>
  237. * and values that are class names of implementations of
  238. * <tt>javax.security.sasl.SaslClientFactory</tt>.
  239. *
  240. * For example, a provider that contains a factory class,
  241. * <tt>com.wiz.sasl.digest.ClientFactory</tt>, that supports the
  242. * "DIGEST-MD5" mechanism would register the following entry with the JCA:
  243. * <tt>SaslClientFactory.DIGEST-MD5 com.wiz.sasl.digest.ClientFactory</tt>
  244. *<p>
  245. * See the
  246. * "Java Cryptography Architecture API Specification & Reference"
  247. * for information about how to install and configure security service
  248. * providers.
  249. *
  250. * @param mechanisms The non-null list of mechanism names to try. Each is the
  251. * IANA-registered name of a SASL mechanism. (e.g. "GSSAPI", "CRAM-MD5").
  252. * @param authorizationId The possibly null protocol-dependent
  253. * identification to be used for authorization.
  254. * If null or empty, the server derives an authorization
  255. * ID from the client's authentication credentials.
  256. * When the SASL authentication completes successfully,
  257. * the specified entity is granted access.
  258. *
  259. * @param protocol The non-null string name of the protocol for which
  260. * the authentication is being performed (e.g., "ldap").
  261. *
  262. * @param serverName The non-null fully-qualified host name of the server
  263. * to authenticate to.
  264. *
  265. * @param props The possibly null set of properties used to
  266. * select the SASL mechanism and to configure the authentication
  267. * exchange of the selected mechanism.
  268. * For example, if <tt>props</tt> contains the
  269. * <code>Sasl.POLICY_NOPLAINTEXT</code> property with the value
  270. * <tt>"true"</tt>, then the selected
  271. * SASL mechanism must not be susceptible to simple plain passive attacks.
  272. * In addition to the standard properties declared in this class,
  273. * other, possibly mechanism-specific, properties can be included.
  274. * Properties not relevant to the selected mechanism are ignored.
  275. *
  276. * @param cbh The possibly null callback handler to used by the SASL
  277. * mechanisms to get further information from the application/library
  278. * to complete the authentication. For example, a SASL mechanism might
  279. * require the authentication ID, password and realm from the caller.
  280. * The authentication ID is requested by using a <tt>NameCallback</tt>.
  281. * The password is requested by using a <tt>PasswordCallback</tt>.
  282. * The realm is requested by using a <tt>RealmChoiceCallback</tt> if there is a list
  283. * of realms to choose from, and by using a <tt>RealmCallback</tt> if
  284. * the realm must be entered.
  285. *
  286. *@return A possibly null <tt>SaslClient</tt> created using the parameters
  287. * supplied. If null, cannot find a <tt>SaslClientFactory</tt>
  288. * that will produce one.
  289. *@exception SaslException If cannot create a <tt>SaslClient</tt> because
  290. * of an error.
  291. */
  292. public static SaslClient createSaslClient(
  293. String[] mechanisms,
  294. String authorizationId,
  295. String protocol,
  296. String serverName,
  297. Map<String,?> props,
  298. CallbackHandler cbh) throws SaslException {
  299. SaslClient mech = null;
  300. SaslClientFactory fac;
  301. String className;
  302. String mechName;
  303. for (int i = 0; i < mechanisms.length; i++) {
  304. if ((mechName=mechanisms[i]) == null) {
  305. throw new NullPointerException(
  306. "Mechanism name cannot be null");
  307. } else if (mechName.length() == 0) {
  308. continue;
  309. }
  310. String mechFilter = "SaslClientFactory." + mechName;
  311. Provider[] provs = Security.getProviders(mechFilter);
  312. for (int j = 0; provs != null && j < provs.length; j++) {
  313. className = provs[j].getProperty(mechFilter);
  314. if (className == null) {
  315. // Case is ignored
  316. continue;
  317. }
  318. fac = (SaslClientFactory) loadFactory(provs[j], className);
  319. if (fac != null) {
  320. mech = fac.createSaslClient(
  321. new String[]{mechanisms[i]}, authorizationId,
  322. protocol, serverName, props, cbh);
  323. if (mech != null) {
  324. return mech;
  325. }
  326. }
  327. }
  328. }
  329. return null;
  330. }
  331. private static Object loadFactory(Provider p, String className)
  332. throws SaslException {
  333. try {
  334. /*
  335. * Load the implementation class with the same class loader
  336. * that was used to load the provider.
  337. * In order to get the class loader of a class, the
  338. * caller's class loader must be the same as or an ancestor of
  339. * the class loader being returned. Otherwise, the caller must
  340. * have "getClassLoader" permission, or a SecurityException
  341. * will be thrown.
  342. */
  343. ClassLoader cl = p.getClass().getClassLoader();
  344. Class implClass;
  345. implClass = Class.forName(className, true, cl);
  346. return implClass.newInstance();
  347. } catch (ClassNotFoundException e) {
  348. throw new SaslException("Cannot load class " + className, e);
  349. } catch (InstantiationException e) {
  350. throw new SaslException("Cannot instantiate class " + className, e);
  351. } catch (IllegalAccessException e) {
  352. throw new SaslException("Cannot access class " + className, e);
  353. } catch (SecurityException e) {
  354. throw new SaslException("Cannot access class " + className, e);
  355. }
  356. }
  357. /**
  358. * Creates a <tt>SaslServer</tt> for the specified mechanism.
  359. *
  360. * This method uses the
  361. <a href="http://java.sun.com/j2se/1.4/docs/guide/security/CryptoSpec.html#Provider">JCA Security Provider Framework</a>,
  362. * described in the
  363. * "Java Cryptography Architecture API Specification & Reference", for
  364. * locating and selecting a <tt>SaslServer</tt> implementation.
  365. *
  366. * First, it
  367. * obtains an ordered list of <tt>SaslServerFactory</tt> instances from
  368. * the registered security providers for the "SaslServerFactory" service
  369. * and the specified mechanism. It then invokes
  370. * <tt>createSaslServer()</tt> on each factory instance on the list
  371. * until one produces a non-null <tt>SaslServer</tt> instance. It returns
  372. * the non-null <tt>SaslServer</tt> instance, or null if the search fails
  373. * to produce a non-null <tt>SaslServer</tt> instance.
  374. *<p>
  375. * A security provider for SaslServerFactory registers with the
  376. * JCA Security Provider Framework keys of the form <br>
  377. * <tt>SaslServerFactory.<em>mechanism_name</em></tt>
  378. * <br>
  379. * and values that are class names of implementations of
  380. * <tt>javax.security.sasl.SaslServerFactory</tt>.
  381. *
  382. * For example, a provider that contains a factory class,
  383. * <tt>com.wiz.sasl.digest.ServerFactory</tt>, that supports the
  384. * "DIGEST-MD5" mechanism would register the following entry with the JCA:
  385. * <tt>SaslServerFactory.DIGEST-MD5 com.wiz.sasl.digest.ServerFactory</tt>
  386. *<p>
  387. * See the
  388. * "Java Cryptography Architecture API Specification & Reference"
  389. * for information about how to install and configure security
  390. * service providers.
  391. *
  392. * @param mechanism The non-null mechanism name. It must be an
  393. * IANA-registered name of a SASL mechanism. (e.g. "GSSAPI", "CRAM-MD5").
  394. * @param protocol The non-null string name of the protocol for which
  395. * the authentication is being performed (e.g., "ldap").
  396. * @param serverName The non-null fully qualified host name of the server.
  397. * @param props The possibly null set of properties used to
  398. * select the SASL mechanism and to configure the authentication
  399. * exchange of the selected mechanism.
  400. * For example, if <tt>props</tt> contains the
  401. * <code>Sasl.POLICY_NOPLAINTEXT</code> property with the value
  402. * <tt>"true"</tt>, then the selected
  403. * SASL mechanism must not be susceptible to simple plain passive attacks.
  404. * In addition to the standard properties declared in this class,
  405. * other, possibly mechanism-specific, properties can be included.
  406. * Properties not relevant to the selected mechanism are ignored.
  407. *
  408. * @param cbh The possibly null callback handler to used by the SASL
  409. * mechanisms to get further information from the application/library
  410. * to complete the authentication. For example, a SASL mechanism might
  411. * require the authentication ID, password and realm from the caller.
  412. * The authentication ID is requested by using a <tt>NameCallback</tt>.
  413. * The password is requested by using a <tt>PasswordCallback</tt>.
  414. * The realm is requested by using a <tt>RealmChoiceCallback</tt> if there is a list
  415. * of realms to choose from, and by using a <tt>RealmCallback</tt> if
  416. * the realm must be entered.
  417. *
  418. *@return A possibly null <tt>SaslServer</tt> created using the parameters
  419. * supplied. If null, cannot find a <tt>SaslServerFactory</tt>
  420. * that will produce one.
  421. *@exception SaslException If cannot create a <tt>SaslServer</tt> because
  422. * of an error.
  423. **/
  424. public static SaslServer
  425. createSaslServer(String mechanism,
  426. String protocol,
  427. String serverName,
  428. Map<String,?> props,
  429. javax.security.auth.callback.CallbackHandler cbh)
  430. throws SaslException {
  431. SaslServer mech = null;
  432. SaslServerFactory fac;
  433. String className;
  434. if (mechanism == null) {
  435. throw new NullPointerException("Mechanism name cannot be null");
  436. } else if (mechanism.length() == 0) {
  437. return null;
  438. }
  439. String mechFilter = "SaslServerFactory." + mechanism;
  440. Provider[] provs = Security.getProviders(mechFilter);
  441. for (int j = 0; provs != null && j < provs.length; j++) {
  442. className = provs[j].getProperty(mechFilter);
  443. if (className == null) {
  444. throw new SaslException("Provider does not support " +
  445. mechFilter);
  446. }
  447. fac = (SaslServerFactory) loadFactory(provs[j], className);
  448. if (fac != null) {
  449. mech = fac.createSaslServer(
  450. mechanism, protocol, serverName, props, cbh);
  451. if (mech != null) {
  452. return mech;
  453. }
  454. }
  455. }
  456. return null;
  457. }
  458. /**
  459. * Gets an enumeration of known factories for producing <tt>SaslClient</tt>.
  460. * This method uses the same algorithm for locating factories as
  461. * <tt>createSaslClient()</tt>.
  462. * @return A non-null enumeration of known factories for producing
  463. * <tt>SaslClient</tt>.
  464. * @see #createSaslClient
  465. */
  466. public static Enumeration<SaslClientFactory> getSaslClientFactories() {
  467. Set facs = getFactories("SaslClientFactory");
  468. final Iterator iter = facs.iterator();
  469. return new Enumeration<SaslClientFactory>() {
  470. public boolean hasMoreElements() {
  471. return iter.hasNext();
  472. }
  473. public SaslClientFactory nextElement() {
  474. return (SaslClientFactory)iter.next();
  475. }
  476. };
  477. }
  478. /**
  479. * Gets an enumeration of known factories for producing <tt>SaslServer</tt>.
  480. * This method uses the same algorithm for locating factories as
  481. * <tt>createSaslServer()</tt>.
  482. * @return A non-null enumeration of known factories for producing
  483. * <tt>SaslServer</tt>.
  484. * @see #createSaslServer
  485. */
  486. public static Enumeration<SaslServerFactory> getSaslServerFactories() {
  487. Set facs = getFactories("SaslServerFactory");
  488. final Iterator iter = facs.iterator();
  489. return new Enumeration<SaslServerFactory>() {
  490. public boolean hasMoreElements() {
  491. return iter.hasNext();
  492. }
  493. public SaslServerFactory nextElement() {
  494. return (SaslServerFactory)iter.next();
  495. }
  496. };
  497. }
  498. private static Set getFactories(String serviceName) {
  499. HashSet result = new HashSet();
  500. if ((serviceName == null) || (serviceName.length() == 0) ||
  501. (serviceName.endsWith("."))) {
  502. return result;
  503. }
  504. Provider[] providers = Security.getProviders();
  505. HashSet classes = new HashSet();
  506. Object fac;
  507. for (int i = 0; i < providers.length; i++) {
  508. classes.clear();
  509. // Check the keys for each provider.
  510. for (Enumeration e = providers[i].keys(); e.hasMoreElements(); ) {
  511. String currentKey = (String)e.nextElement();
  512. if (currentKey.startsWith(serviceName)) {
  513. // We should skip the currentKey if it contains a
  514. // whitespace. The reason is: such an entry in the
  515. // provider property contains attributes for the
  516. // implementation of an algorithm. We are only interested
  517. // in entries which lead to the implementation
  518. // classes.
  519. if (currentKey.indexOf(" ") < 0) {
  520. String className = providers[i].getProperty(currentKey);
  521. if (!classes.contains(className)) {
  522. classes.add(className);
  523. try {
  524. fac = loadFactory(providers[i], className);
  525. if (fac != null) {
  526. result.add(fac);
  527. }
  528. }catch (Exception ignore) {
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. return Collections.unmodifiableSet(result);
  536. }
  537. }