jdk/src/java.security.sasl/share/classes/javax/security/sasl/Sasl.java
author goetz
Tue, 09 Dec 2014 11:57:46 +0100
changeset 28187 fc19df82d6ee
parent 25859 3317bb8137f4
child 31270 e6470b24700d
permissions -rw-r--r--
8066964: ppc64: argument and return type profiling, fix problem with popframe Reviewed-by: roland, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
     2
 * Copyright (c) 1999, 2013, 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>
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    75
     * <li>{@code "auth"} - authentication only</li>
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    76
     * <li>{@code "auth-int"} - authentication plus integrity protection</li>
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    77
     * <li>{@code "auth-conf"} - authentication plus integrity and confidentiality
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    82
     * server. If this property is absent, the default qop is {@code "auth"}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    83
     * The value of this constant is {@code "javax.security.sasl.qop"}.
2
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>
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    93
     * <li>{@code "low"}</li>
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    94
     * <li>{@code "medium"}</li>
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
    95
     * <li>{@code "high"}</li>
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   104
     * {@code "high,medium,low"}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   105
     * The value of this constant is {@code "javax.security.sasl.strength"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   112
     * {@code "true"} if the server must
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   113
     * authenticate the to client; {@code "false"} otherwise.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   114
     * The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <br>The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   116
     * {@code "javax.security.sasl.server.authentication"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   128
     * {@code "javax.security.sasl.bound.server.name"}.
14340
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   135
     * buffer in bytes of {@code SaslClient}/{@code SaslServer}.
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   139
     * <br>The value of this constant is {@code "javax.security.sasl.maxbuffer"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   145
     * buffer in bytes of {@code SaslClient}/{@code SaslServer}.
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   149
     * <br>The value of this constant is {@code "javax.security.sasl.rawsendsize"}.
2
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
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18830
diff changeset
   175
     * criteria for reusability. Such customizations are
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   184
     * contains {@code "true"} if such mechanisms are not permitted;
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   185
     * {@code "false"} if such mechanisms are permitted.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   186
     * The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <br>The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   188
     * {@code "javax.security.sasl.policy.noplaintext"}.
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   197
     * The property contains {@code "true"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * if mechanisms susceptible to active attacks
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   199
     * are not permitted; {@code "false"} if such mechanisms are permitted.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   200
     * The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <br>The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   202
     * {@code "javax.security.sasl.policy.noactive"}.
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   210
     * The property contains {@code "true"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * if mechanisms susceptible to dictionary attacks are not permitted;
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   212
     * {@code "false"} if such mechanisms are permitted.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   213
     * The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   216
     * {@code "javax.security.sasl.policy.nodictionary"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   223
     * anonymous login are not permitted. The property contains {@code "true"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * if mechanisms that accept anonymous login are not permitted;
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   225
     * {@code "false"}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   226
     * if such mechanisms are permitted. The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   229
     * {@code "javax.security.sasl.policy.noanonymous"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   240
      * contains {@code "true"} if mechanisms that implement forward secrecy
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   241
      * between sessions are required; {@code "false"} if such mechanisms
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   242
      * are not required. The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      *<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      * The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   245
      * {@code "javax.security.sasl.policy.forward"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   253
     * contains {@code "true"} if mechanisms that pass
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   254
     * client credentials are required; {@code "false"}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   255
     * if such mechanisms are not required. The default is {@code "false"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * The value of this constant is
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   258
     * {@code "javax.security.sasl.policy.credentials"}.
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   272
     * {@code "javax.security.sasl.credentials"}.
2
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
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   277
     * Creates a {@code SaslClient} using the parameters supplied.
2
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
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   281
     * "Java Cryptography Architecture API Specification &amp; Reference", for
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   282
     * locating and selecting a {@code SaslClient} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * First, it
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   285
     * obtains an ordered list of {@code SaslClientFactory} instances from
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   288
     * {@code createSaslClient()} on each factory instance on the list
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   289
     * until one produces a non-null {@code SaslClient} instance. It returns
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   290
     * the non-null {@code SaslClient} instance, or null if the search fails
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   291
     * to produce a non-null {@code SaslClient} instance.
2
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>
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   295
     * {@code SaslClientFactory.}<em>{@code mechanism_name}</em>
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   298
     * {@code javax.security.sasl.SaslClientFactory}.
2
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,
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   301
     * {@code com.wiz.sasl.digest.ClientFactory}, that supports the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * "DIGEST-MD5" mechanism would register the following entry with the JCA:
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   303
     * {@code SaslClientFactory.DIGEST-MD5 com.wiz.sasl.digest.ClientFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * See the
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   306
     * "Java Cryptography Architecture API Specification &amp; Reference"
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   328
     * For example, if {@code props} contains the
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   329
     * {@code Sasl.POLICY_NOPLAINTEXT} property with the value
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   330
     * {@code "true"}, then the selected
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   341
     * The authentication ID is requested by using a {@code NameCallback}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   342
     * The password is requested by using a {@code PasswordCallback}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   343
     * The realm is requested by using a {@code RealmChoiceCallback} if there is a list
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   344
     * of realms to choose from, and by using a {@code RealmCallback} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * the realm must be entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   347
     *@return A possibly null {@code SaslClient} created using the parameters
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   348
     * supplied. If null, cannot find a {@code SaslClientFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * that will produce one.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   350
     *@exception SaslException If cannot create a {@code SaslClient} because
2
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
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   426
     * Creates a {@code SaslServer} for the specified mechanism.
2
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
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   431
     * "Java Cryptography Architecture API Specification &amp; Reference", for
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   432
     * locating and selecting a {@code SaslServer} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * First, it
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   435
     * obtains an ordered list of {@code SaslServerFactory} instances from
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   438
     * {@code createSaslServer()} on each factory instance on the list
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   439
     * until one produces a non-null {@code SaslServer} instance. It returns
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   440
     * the non-null {@code SaslServer} instance, or null if the search fails
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   441
     * to produce a non-null {@code SaslServer} instance.
2
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>
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   445
     * {@code SaslServerFactory.}<em>{@code mechanism_name}</em>
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   448
     * {@code javax.security.sasl.SaslServerFactory}.
2
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,
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   451
     * {@code com.wiz.sasl.digest.ServerFactory}, that supports the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * "DIGEST-MD5" mechanism would register the following entry with the JCA:
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   453
     * {@code SaslServerFactory.DIGEST-MD5  com.wiz.sasl.digest.ServerFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * See the
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   456
     * "Java Cryptography Architecture API Specification &amp; Reference"
2
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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   466
     * does not allow an unbound server, a {@code SaslException} will
14340
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   471
     * For example, if {@code props} contains the
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   472
     * {@code Sasl.POLICY_NOPLAINTEXT} property with the value
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   473
     * {@code "true"}, then the selected
2
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.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   484
     * The authentication ID is requested by using a {@code NameCallback}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   485
     * The password is requested by using a {@code PasswordCallback}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   486
     * The realm is requested by using a {@code RealmChoiceCallback} if there is a list
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   487
     * of realms to choose from, and by using a {@code RealmCallback} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * the realm must be entered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   490
     *@return A possibly null {@code SaslServer} created using the parameters
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   491
     * supplied. If null, cannot find a {@code SaslServerFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * that will produce one.
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   493
     *@exception SaslException If cannot create a {@code SaslServer} because
2
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
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   536
     * Gets an enumeration of known factories for producing {@code SaslClient}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * This method uses the same algorithm for locating factories as
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   538
     * {@code createSaslClient()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @return A non-null enumeration of known factories for producing
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   540
     * {@code SaslClient}.
2
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
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   557
     * Gets an enumeration of known factories for producing {@code SaslServer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * This method uses the same algorithm for locating factories as
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   559
     * {@code createSaslServer()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @return A non-null enumeration of known factories for producing
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 18156
diff changeset
   561
     * {@code SaslServer}.
2
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.
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 21278
diff changeset
   603
                    if (currentKey.indexOf(' ') < 0) {
2
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
}