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