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