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