1 |
769 |
jeremybenn |
/* ClientFactory.java --
|
2 |
|
|
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or (at
|
9 |
|
|
your option) any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
19 |
|
|
USA
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.sasl;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Registry;
|
42 |
|
|
import gnu.javax.crypto.sasl.anonymous.AnonymousClient;
|
43 |
|
|
import gnu.javax.crypto.sasl.crammd5.CramMD5Client;
|
44 |
|
|
import gnu.javax.crypto.sasl.plain.PlainClient;
|
45 |
|
|
import gnu.javax.crypto.sasl.srp.SRPClient;
|
46 |
|
|
|
47 |
|
|
import java.util.ArrayList;
|
48 |
|
|
import java.util.Arrays;
|
49 |
|
|
import java.util.Collections;
|
50 |
|
|
import java.util.HashMap;
|
51 |
|
|
import java.util.HashSet;
|
52 |
|
|
import java.util.List;
|
53 |
|
|
import java.util.Map;
|
54 |
|
|
import java.util.Set;
|
55 |
|
|
|
56 |
|
|
import javax.security.auth.callback.CallbackHandler;
|
57 |
|
|
import javax.security.sasl.Sasl;
|
58 |
|
|
import javax.security.sasl.SaslClient;
|
59 |
|
|
import javax.security.sasl.SaslClientFactory;
|
60 |
|
|
import javax.security.sasl.SaslException;
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* The implementation of {@link SaslClientFactory}.
|
64 |
|
|
*/
|
65 |
|
|
public class ClientFactory
|
66 |
|
|
implements SaslClientFactory
|
67 |
|
|
{
|
68 |
|
|
// implicit 0-arguments constructor
|
69 |
|
|
|
70 |
|
|
public static final Set getNames()
|
71 |
|
|
{
|
72 |
|
|
return Collections.unmodifiableSet(new HashSet(Arrays.asList(getNamesInternal(null))));
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
private static final String[] getNamesInternal(Map props)
|
76 |
|
|
{
|
77 |
|
|
String[] all = new String[] {
|
78 |
|
|
Registry.SASL_SRP_MECHANISM,
|
79 |
|
|
Registry.SASL_CRAM_MD5_MECHANISM,
|
80 |
|
|
Registry.SASL_PLAIN_MECHANISM,
|
81 |
|
|
Registry.SASL_ANONYMOUS_MECHANISM };
|
82 |
|
|
if (props == null)
|
83 |
|
|
return all;
|
84 |
|
|
if (hasPolicy(Sasl.POLICY_PASS_CREDENTIALS, props))
|
85 |
|
|
return new String[0];
|
86 |
|
|
List result = new ArrayList(all.length);
|
87 |
|
|
for (int i = 0; i < all.length;)
|
88 |
|
|
result.add(all[i++]);
|
89 |
|
|
if (hasPolicy(Sasl.POLICY_NOPLAINTEXT, props))
|
90 |
|
|
result.remove(Registry.SASL_PLAIN_MECHANISM);
|
91 |
|
|
if (hasPolicy(Sasl.POLICY_NOACTIVE, props))
|
92 |
|
|
{
|
93 |
|
|
result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
|
94 |
|
|
result.remove(Registry.SASL_PLAIN_MECHANISM);
|
95 |
|
|
}
|
96 |
|
|
if (hasPolicy(Sasl.POLICY_NODICTIONARY, props))
|
97 |
|
|
{
|
98 |
|
|
result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
|
99 |
|
|
result.remove(Registry.SASL_PLAIN_MECHANISM);
|
100 |
|
|
}
|
101 |
|
|
if (hasPolicy(Sasl.POLICY_NOANONYMOUS, props))
|
102 |
|
|
{
|
103 |
|
|
result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
|
104 |
|
|
}
|
105 |
|
|
if (hasPolicy(Sasl.POLICY_FORWARD_SECRECY, props))
|
106 |
|
|
{
|
107 |
|
|
result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
|
108 |
|
|
result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
|
109 |
|
|
result.remove(Registry.SASL_PLAIN_MECHANISM);
|
110 |
|
|
}
|
111 |
|
|
return (String[]) result.toArray(new String[0]);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
public static final ClientMechanism getInstance(String mechanism)
|
115 |
|
|
{
|
116 |
|
|
if (mechanism == null)
|
117 |
|
|
return null;
|
118 |
|
|
mechanism = mechanism.trim().toUpperCase();
|
119 |
|
|
if (mechanism.equals(Registry.SASL_SRP_MECHANISM))
|
120 |
|
|
return new SRPClient();
|
121 |
|
|
if (mechanism.equals(Registry.SASL_CRAM_MD5_MECHANISM))
|
122 |
|
|
return new CramMD5Client();
|
123 |
|
|
if (mechanism.equals(Registry.SASL_PLAIN_MECHANISM))
|
124 |
|
|
return new PlainClient();
|
125 |
|
|
if (mechanism.equals(Registry.SASL_ANONYMOUS_MECHANISM))
|
126 |
|
|
return new AnonymousClient();
|
127 |
|
|
return null;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
public SaslClient createSaslClient(String[] mechanisms,
|
131 |
|
|
String authorisationID, String protocol,
|
132 |
|
|
String serverName, Map props,
|
133 |
|
|
CallbackHandler cbh) throws SaslException
|
134 |
|
|
{
|
135 |
|
|
ClientMechanism result = null;
|
136 |
|
|
String mechanism;
|
137 |
|
|
for (int i = 0; i < mechanisms.length; i++)
|
138 |
|
|
{
|
139 |
|
|
mechanism = mechanisms[i];
|
140 |
|
|
result = getInstance(mechanism);
|
141 |
|
|
if (result != null)
|
142 |
|
|
break;
|
143 |
|
|
}
|
144 |
|
|
if (result != null)
|
145 |
|
|
{
|
146 |
|
|
HashMap attributes = new HashMap();
|
147 |
|
|
if (props != null)
|
148 |
|
|
attributes.putAll(props);
|
149 |
|
|
attributes.put(Registry.SASL_AUTHORISATION_ID, authorisationID);
|
150 |
|
|
attributes.put(Registry.SASL_PROTOCOL, protocol);
|
151 |
|
|
attributes.put(Registry.SASL_SERVER_NAME, serverName);
|
152 |
|
|
attributes.put(Registry.SASL_CALLBACK_HANDLER, cbh);
|
153 |
|
|
result.init(attributes);
|
154 |
|
|
return result;
|
155 |
|
|
}
|
156 |
|
|
throw new SaslException("No supported mechanism found in given mechanism list");
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
public String[] getMechanismNames(Map props)
|
160 |
|
|
{
|
161 |
|
|
return getNamesInternal(props);
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
private static boolean hasPolicy(String propertyName, Map props)
|
165 |
|
|
{
|
166 |
|
|
return "true".equalsIgnoreCase(String.valueOf(props.get(propertyName)));
|
167 |
|
|
}
|
168 |
|
|
}
|