Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / security / sasl / Sasl.java
blobdbe4cc8c2ae4476e44a3d72a09790648752c30d4
1 /* Sasl.java --
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.security.sasl;
41 import java.security.Provider;
42 import java.security.Security;
43 import java.util.Enumeration;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Vector;
49 import javax.security.auth.callback.CallbackHandler;
51 /**
52 * <p>A static class for creating SASL clients and servers.</p>
54 * <p>This class defines the policy of how to locate, load, and instantiate SASL
55 * clients and servers.</p>
57 * <p>For example, an application or library gets a SASL client instance by
58 * doing something like:</p>
60 * <pre>
61 *SaslClient sc =
62 * Sasl.createSaslClient(mechanisms, authorizationID, protocol,
63 * serverName, props, callbackHandler);
64 * </pre>
66 * <p>It can then proceed to use the instance to create an authenticated
67 * connection.</p>
69 * <p>Similarly, a server gets a SASL server instance by using code that looks
70 * as follows:</p>
72 * <pre>
73 *SaslServer ss =
74 * Sasl.createSaslServer(mechanism, protocol, serverName, props,
75 * callbackHandler);
76 * </pre>
78 * @since 1.5
80 public class Sasl
83 // Constants and variables
84 // -------------------------------------------------------------------------
86 /**
87 * <p>The name of a property that specifies the quality-of-protection to use.
88 * The property contains a comma-separated, ordered list of quality-of-
89 * protection values that the client or server is willing to support. A qop
90 * value is one of:</p>
92 * <ul>
93 * <li><code>"auth"</code> - authentication only,</li>
94 * <li><code>"auth-int"</code> - authentication plus integrity
95 * protection,</li>
96 * <li><code>"auth-conf"</code> - authentication plus integrity and
97 * confidentiality protection.</li>
98 * </ul>
100 * <p>The order of the list specifies the preference order of the client or
101 * server.</p>
103 * <p>If this property is absent, the default qop is <code>"auth"</code>.</p>
105 * <p>The value of this constant is <code>"javax.security.sasl.qop"</code>.</p>
107 public static final String QOP = "javax.security.sasl.qop";
110 * <p>The name of a property that specifies the cipher strength to use. The
111 * property contains a comma-separated, ordered list of cipher strength
112 * values that the client or server is willing to support. A strength value
113 * is one of:</p>
115 * <ul>
116 * <li><code>"low"</code>,</li>
117 * <li><code>"medium"</code>,</li>
118 * <li><code>"high"</code>.</li>
119 * </ul>
121 * <p>The order of the list specifies the preference order of the client or
122 * server. An implementation should allow configuration of the meaning of
123 * these values. An application may use the Java Cryptography Extension (JCE)
124 * with JCE-aware mechanisms to control the selection of cipher suites that
125 * match the strength values.</p>
127 * <p>If this property is absent, the default strength is
128 * <code>"high,medium,low"</code>.</p>
130 * <p>The value of this constant is <code>"javax.security.sasl.strength"</code>.
131 * </p>
133 public static final String STRENGTH = "javax.security.sasl.strength";
136 * <p>The name of a property that specifies whether the server must authenticate
137 * to the client. The property contains <code>"true"</code> if the server
138 * must authenticate the to client; <code>"false"</code> otherwise. The
139 * default is <code>"false"</code>.</p>
141 * <p>The value of this constant is
142 * <code>"javax.security.sasl.server.authentication"</code>.</p>
144 public static final String SERVER_AUTH = "javax.security.sasl.server.authentication";
147 * <p>The name of a property that specifies the maximum size of the receive
148 * buffer in bytes of {@link SaslClient}/{@link SaslServer}. The property
149 * contains the string representation of an integer.</p>
151 * <p>If this property is absent, the default size is defined by the
152 * mechanism.</p>
154 * <p>The value of this constant is <code>"javax.security.sasl.maxbuffer"</code>.
155 * </p>
157 public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer";
160 * <p>The name of a property that specifies the maximum size of the raw send
161 * buffer in bytes of {@link SaslClient}/{@link SaslServer}. The property
162 * contains the string representation of an integer. The value of this
163 * property is negotiated between the client and server during the
164 * authentication exchange.</p>
166 * <p>The value of this constant is <code>"javax.security.sasl.rawsendsize"</code>.
167 * </p>
169 public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize";
172 * <p>The name of a property that specifies whether mechanisms susceptible
173 * to simple plain passive attacks (e.g., "PLAIN") are not permitted. The
174 * property contains <code>"true"</code> if such mechanisms are not
175 * permitted; <code>"false"</code> if such mechanisms are permitted. The
176 * default is <code>"false"</code>.</p>
178 * <p>The value of this constant is <code>"javax.security.sasl.policy.noplaintext"</code>.
179 * </p>
181 public static final String POLICY_NOPLAINTEXT = "javax.security.sasl.policy.noplaintext";
184 * <p>The name of a property that specifies whether mechanisms susceptible to
185 * active (non-dictionary) attacks are not permitted. The property contains
186 * <code>"true"</code> if mechanisms susceptible to active attacks are not
187 * permitted; <code>"false"</code> if such mechanisms are permitted. The
188 * default is <code>"false"</code>.</p>
190 * <p>The value of this constant is <code>"javax.security.sasl.policy.noactive"</code>.
191 * </p>
193 public static final String POLICY_NOACTIVE = "javax.security.sasl.policy.noactive";
196 * <p>The name of a property that specifies whether mechanisms susceptible to
197 * passive dictionary attacks are not permitted. The property contains
198 * <code>"true"</code> if mechanisms susceptible to dictionary attacks are
199 * not permitted; <code>"false"</code> if such mechanisms are permitted. The
200 * default is <code>"false"</code>.</p>
202 * <p>The value of this constant is <code>"javax.security.sasl.policy.nodictionary"</code>.
203 * </p>
205 public static final String POLICY_NODICTIONARY = "javax.security.sasl.policy.nodictionary";
208 * <p>The name of a property that specifies whether mechanisms that accept
209 * anonymous login are not permitted. The property contains <code>"true"</code>
210 * if mechanisms that accept anonymous login are not permitted; <code>"false"
211 * </code> if such mechanisms are permitted. The default is <code>"false"</code>.
212 * </p>
214 * <p>The value of this constant is <code>"javax.security.sasl.policy.noanonymous"</code>.
215 * </p>
217 public static final String POLICY_NOANONYMOUS = "javax.security.sasl.policy.noanonymous";
220 * The name of a property that specifies whether mechanisms that implement
221 * forward secrecy between sessions are required. Forward secrecy means that
222 * breaking into one session will not automatically provide information for
223 * breaking into future sessions. The property contains <code>"true"</code>
224 * if mechanisms that implement forward secrecy between sessions are
225 * required; <code>"false"</code> if such mechanisms are not required. The
226 * default is <code>"false"</code>.
228 * <p>The value of this constant is <code>"javax.security.sasl.policy.forward"</code>.
229 * </p>
231 public static final String POLICY_FORWARD_SECRECY = "javax.security.sasl.policy.forward";
234 * The name of a property that specifies whether mechanisms that pass client
235 * credentials are required. The property contains <code>"true"</code> if
236 * mechanisms that pass client credentials are required; <code>"false"</code>
237 * if such mechanisms are not required. The default is <code>"false"</code>.
239 * <p>The value of this constant is <code>"javax.security.sasl.policy.credentials"</code>.
240 * </p>
242 public static final String POLICY_PASS_CREDENTIALS = "javax.security.sasl.policy.credentials";
245 * <p>The name of a property that specifies whether to reuse previously
246 * authenticated session information. The property contains <code>"true"</code>
247 * if the mechanism implementation may attempt to reuse previously
248 * authenticated session information; it contains <code>"false"</code> if the
249 * implementation must not reuse previously authenticated session information.
250 * A setting of <code>"true"</code> serves only as a hint; it does not
251 * necessarily entail actual reuse because reuse might not be possible due to
252 * a number of reasons, including, but not limited to, lack of mechanism
253 * support for reuse, expiration of reusable information, and the peer's
254 * refusal to support reuse. The property's default value is <code>"false"</code>.
255 * </p>
257 * <p>The value of this constant is <code>"javax.security.sasl.reuse"</code>.
258 * Note that all other parameters and properties required to create a SASL
259 * client/server instance must be provided regardless of whether this
260 * property has been supplied. That is, you cannot supply any less
261 * information in anticipation of reuse. Mechanism implementations that
262 * support reuse might allow customization of its implementation for factors
263 * such as cache size, timeouts, and criteria for reuseability. Such
264 * customizations are implementation-dependent.</p>
266 public static final String REUSE = "javax.security.sasl.reuse";
268 private static final String CLIENT_FACTORY_SVC = "SaslClientFactory.";
269 private static final String SERVER_FACTORY_SVC = "SaslServerFactory.";
270 private static final String ALIAS = "Alg.Alias.";
272 // Constructor(s)
273 // -------------------------------------------------------------------------
275 private Sasl()
277 super();
280 // Class methods
281 // -------------------------------------------------------------------------
284 * Creates a {@link SaslClient} for the specified mechanism.
286 * <p>This method uses the JCA Security Provider Framework, described in the
287 * "Java Cryptography Architecture API Specification &amp; Reference", for
288 * locating and selecting a {@link SaslClient} implementation.</p>
290 * <p>First, it obtains an ordered list of {@link SaslClientFactory}
291 * instances from the registered security providers for the
292 * <code>"SaslClientFactory"</code> service and the specified mechanism. It
293 * then invokes <code>createSaslClient()</code> on each factory instance on
294 * the list until one produces a non-null {@link SaslClient} instance. It
295 * returns the non-null {@link SaslClient} instance, or <code>null</code> if
296 * the search fails to produce a non-null {@link SaslClient} instance.</p>
298 * <p>A security provider for <code>SaslClientFactory</code> registers with
299 * the JCA Security Provider Framework keys of the form:</p>
301 * <pre>
302 * SaslClientFactory.mechanism_name
303 * </pre>
305 * <p>and values that are class names of implementations of {@link
306 * SaslClientFactory}.</p>
308 * <p>For example, a provider that contains a factory class,
309 * <code>com.wiz.sasl.digest.ClientFactory</code>, that supports the
310 * <code>"DIGEST-MD5"</code> mechanism would register the following entry
311 * with the JCA:</p>
313 * <pre>
314 * SaslClientFactory.DIGEST-MD5 com.wiz.sasl.digest.ClientFactory
315 * </pre>
317 * <p>See the "Java Cryptography Architecture API Specification &amp;
318 * Reference" for information about how to install and configure security
319 * service providers.</p>
321 * @param mechanisms the non-null list of mechanism names to try. Each is the
322 * IANA-registered name of a SASL mechanism. (e.g. "GSSAPI", "CRAM-MD5").
323 * @param authorizationID the possibly <code>null</code> protocol-dependent
324 * identification to be used for authorization. If <code>null</code> or
325 * empty, the server derives an authorization ID from the client's
326 * authentication credentials. When the SASL authentication completes
327 * successfully, the specified entity is granted access.
328 * @param protocol the non-null string name of the protocol for which the
329 * authentication is being performed (e.g. "ldap").
330 * @param serverName the non-null fully-qualified host name of the server to
331 * authenticate to.
332 * @param props the possibly null set of properties used to select the SASL
333 * mechanism and to configure the authentication exchange of the selected
334 * mechanism. For example, if props contains the {@link Sasl#POLICY_NOPLAINTEXT}
335 * property with the value <code>"true"</code>, then the selected SASL
336 * mechanism must not be susceptible to simple plain passive attacks. In
337 * addition to the standard properties declared in this class, other,
338 * possibly mechanism-specific, properties can be included. Properties not
339 * relevant to the selected mechanism are ignored.
340 * @param cbh the possibly <code>null</code> callback handler to used by the
341 * SASL mechanisms to get further information from the application/library to
342 * complete the authentication. For example, a SASL mechanism might require
343 * the authentication ID, password and realm from the caller. The
344 * authentication ID is requested by using a
345 * {@link javax.security.auth.callback.NameCallback}. The password is
346 * requested by using a {@link javax.security.auth.callback.PasswordCallback}.
347 * The realm is requested by using a {@link RealmChoiceCallback} if there is
348 * a list of realms to choose from, and by using a {@link RealmCallback} if
349 * the realm must be entered.
350 * @return a possibly <code>null</code> {@link SaslClient} created using the
351 * parameters supplied. If <code>null</code>, the method could not find a
352 * {@link SaslClientFactory} that will produce one.
353 * @throws SaslException if a {@link SaslClient} cannot be created because
354 * of an error.
356 public static SaslClient createSaslClient(String[] mechanisms,
357 String authorizationID,
358 String protocol,
359 String serverName, Map props,
360 CallbackHandler cbh)
361 throws SaslException
363 if (mechanisms == null)
365 return null;
367 Provider[] providers = Security.getProviders();
368 if (providers == null || providers.length == 0)
370 return null;
373 SaslClient result = null;
374 SaslClientFactory factory = null;
375 String m, clazz = null, upper, alias;
376 int j;
377 Provider p;
378 for (int i = 0; i < mechanisms.length; i++)
380 m = mechanisms[i];
381 if (m == null)
382 continue;
383 for (j = 0; j < providers.length; j++)
385 p = providers[j];
386 if (p != null)
388 // try the name as is
389 clazz = p.getProperty(CLIENT_FACTORY_SVC + m);
390 if (clazz == null) // try all uppercase
392 upper = m.toUpperCase();
393 clazz = p.getProperty(CLIENT_FACTORY_SVC + upper);
394 if (clazz == null) // try if it's an alias
396 alias = p.getProperty(ALIAS + CLIENT_FACTORY_SVC + m);
397 if (alias == null) // try all-uppercase alias name
399 alias = p.getProperty(ALIAS + CLIENT_FACTORY_SVC + upper);
400 if (alias == null) // spit the dummy
401 continue;
403 clazz = p.getProperty(CLIENT_FACTORY_SVC + alias);
406 if (clazz == null)
407 continue;
408 else
409 clazz = clazz.trim();
414 result = null;
415 factory = (SaslClientFactory) Class.forName(clazz).newInstance();
416 result = factory.createSaslClient(mechanisms, authorizationID,
417 protocol, serverName, props, cbh);
419 catch (ClassCastException ignored) // ignore instantiation exceptions
422 catch (ClassNotFoundException ignored)
425 catch (InstantiationException ignored)
428 catch (IllegalAccessException ignored)
431 if (result != null)
432 return result;
435 return null;
439 * Gets an enumeration of known factories for producing a {@link SaslClient}
440 * instance. This method uses the same sources for locating factories as
441 * <code>createSaslClient()</code>.
443 * @return a non-null {@link Enumeration} of known factories for producing a
444 * {@link SaslClient} instance.
445 * @see #createSaslClient(String[],String,String,String,Map,CallbackHandler)
447 public static Enumeration getSaslClientFactories()
449 Vector result = new Vector();
450 HashSet names = new HashSet();
451 Provider[] providers = Security.getProviders();
452 Iterator it;
453 if (providers != null)
455 Provider p;
456 String key;
457 for (int i = 0; i < providers.length; i++)
459 p = providers[i];
460 for (it = p.keySet().iterator(); it.hasNext(); )
462 key = (String) it.next();
463 // add key's binding (a) it is a class of a client factory,
464 // and (b) the key does not include blanks
465 if (key.startsWith(CLIENT_FACTORY_SVC) && key.indexOf(" ") == -1)
467 names.add(p.getProperty(key));
468 break;
473 // we have the factory class names in names; instantiate and enumerate
474 String c;
475 for (it = names.iterator(); it.hasNext(); )
477 c = (String) it.next();
480 SaslClientFactory f = (SaslClientFactory) Class.forName(c).newInstance();
481 if (f != null)
482 result.add(f);
483 } catch (ClassCastException ignored) { // ignore instantiation exceptions
484 } catch (ClassNotFoundException ignored) {
485 } catch (InstantiationException ignored) {
486 } catch (IllegalAccessException ignored) {
490 return result.elements();
494 * Creates a {@link SaslServer} for the specified mechanism.
496 * <p>This method uses the JCA Security Provider Framework, described in the
497 * "Java Cryptography Architecture API Specification &amp; Reference", for
498 * locating and selecting a SaslServer implementation.</p>
500 * <p>First, it obtains an ordered list of {@link SaslServerFactory}
501 * instances from the registered security providers for the
502 * <code>"SaslServerFactory"</code> service and the specified mechanism. It
503 * then invokes <code>createSaslServer()</code> on each factory instance on
504 * the list until one produces a non-null {@link SaslServer} instance. It
505 * returns the non-null {@link SaslServer} instance, or <code>null</code> if
506 * the search fails to produce a non-null {@link SaslServer} instance.</p>
508 * <p>A security provider for {@link SaslServerFactory} registers with the
509 * JCA Security Provider Framework keys of the form:</p>
511 * <pre>
512 * SaslServerFactory.mechanism_name
513 * </pre>
515 * <p>and values that are class names of implementations of {@link
516 * SaslServerFactory}.</p>
518 * <p>For example, a provider that contains a factory class,
519 * <code>com.wiz.sasl.digest.ServerFactory</code>, that supports the
520 * <code>"DIGEST-MD5"</code> mechanism would register the following entry
521 * with the JCA:</p>
523 * <pre>
524 * SaslServerFactory.DIGEST-MD5 com.wiz.sasl.digest.ServerFactory
525 * </pre>
527 * <p>See the "Java Cryptography Architecture API Specification &amp;
528 * Reference" for information about how to install and configure security
529 * service providers.</p>
531 * @param mechanism the non-null mechanism name. It must be an
532 * IANA-registered name of a SASL mechanism. (e.g. "GSSAPI", "CRAM-MD5").
533 * @param protocol the non-null string name of the protocol for which the
534 * authentication is being performed (e.g. "ldap").
535 * @param serverName the non-null fully qualified host name of the server.
536 * @param props the possibly <code>null</code> set of properties used to
537 * select the SASL mechanism and to configure the authentication exchange of
538 * the selected mechanism. For example, if props contains the {@link
539 * Sasl#POLICY_NOPLAINTEXT} property with the value <code>"true"</code>, then
540 * the selected SASL mechanism must not be susceptible to simple plain
541 * passive attacks. In addition to the standard properties declared in this
542 * class, other, possibly mechanism-specific, properties can be included.
543 * Properties not relevant to the selected mechanism are ignored.
544 * @param cbh the possibly <code>null</code> callback handler to used by the
545 * SASL mechanisms to get further information from the application/library to
546 * complete the authentication. For example, a SASL mechanism might require
547 * the authentication ID, password and realm from the caller. The
548 * authentication ID is requested by using a
549 * {@link javax.security.auth.callback.NameCallback}. The password is
550 * requested by using a {@link javax.security.auth.callback.PasswordCallback}.
551 * The realm is requested by using a {@link RealmChoiceCallback} if there is
552 * a list of realms to choose from, and by using a {@link RealmCallback} if
553 * the realm must be entered.
554 * @return a possibly <code>null</code> {@link SaslServer} created using the
555 * parameters supplied. If <code>null</code>, the method cannot find a
556 * {@link SaslServerFactory} instance that will produce one.
557 * @throws SaslException if a {@link SaslServer} instance cannot be created
558 * because of an error.
560 public static SaslServer createSaslServer(String mechanism, String protocol,
561 String serverName,
562 Map props, CallbackHandler cbh)
563 throws SaslException
565 if (mechanism == null)
566 return null;
567 Provider[] providers = Security.getProviders();
568 if (providers == null || providers.length == 0)
569 return null;
571 SaslServer result = null;
572 SaslServerFactory factory = null;
573 String clazz = null, upper, alias = null;
574 int j;
575 Provider p;
576 for (j = 0; j < providers.length; j++)
578 p = providers[j];
579 if (p != null)
581 // try the name as is
582 clazz = p.getProperty(SERVER_FACTORY_SVC + mechanism);
583 if (clazz == null) // try all uppercase
585 upper = mechanism.toUpperCase();
586 clazz = p.getProperty(SERVER_FACTORY_SVC + upper);
587 if (clazz == null) // try if it's an alias
589 alias = p.getProperty(ALIAS + SERVER_FACTORY_SVC + mechanism);
590 if (alias == null) // try all-uppercase alias name
592 alias = p.getProperty(ALIAS + SERVER_FACTORY_SVC + upper);
593 if (alias == null) // spit the dummy
594 continue;
597 clazz = p.getProperty(SERVER_FACTORY_SVC + alias);
600 if (clazz == null)
601 continue;
602 else
603 clazz = clazz.trim();
607 result = null;
608 factory = (SaslServerFactory) Class.forName(clazz).newInstance();
609 result =
610 factory.createSaslServer(mechanism, protocol, serverName, props, cbh);
612 catch (ClassCastException ignored) // ignore instantiation exceptions
615 catch (ClassNotFoundException ignored)
618 catch (InstantiationException ignored)
621 catch (IllegalAccessException ignored)
624 if (result != null)
625 return result;
627 return null;
631 * Gets an enumeration of known factories for producing a {@link SaslServer}
632 * instance. This method uses the same sources for locating factories as
633 * <code>createSaslServer()</code>.
635 * @return a non-null {@link Enumeration} of known factories for producing a
636 * {@link SaslServer} instance.
637 * @see #createSaslServer(String,String,String,Map,CallbackHandler)
639 public static Enumeration getSaslServerFactories()
641 Vector result = new Vector();
642 HashSet names = new HashSet();
643 Provider[] providers = Security.getProviders();
644 Iterator it;
645 if (providers != null)
647 Provider p;
648 String key;
649 for (int i = 0; i < providers.length; i++)
651 p = providers[i];
652 for (it = p.keySet().iterator(); it.hasNext(); )
654 key = (String) it.next();
655 // add key's binding (a) it is a class of a server factory,
656 // and (b) the key does not include blanks
657 if (key.startsWith(SERVER_FACTORY_SVC) && key.indexOf(" ") == -1)
659 names.add(p.getProperty(key));
660 break;
665 // we have the factory class names in names; instantiate and enumerate
666 String c;
667 for (it = names.iterator(); it.hasNext(); )
669 c = (String) it.next();
672 SaslServerFactory f = (SaslServerFactory) Class.forName(c).newInstance();
673 if (f != null)
674 result.add(f);
676 catch (ClassCastException ignored) // ignore instantiation exceptions
679 catch (ClassNotFoundException ignored)
682 catch (InstantiationException ignored)
685 catch (IllegalAccessException ignored)
690 return result.elements();