001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.security.sasl;
019:
020: import java.security.Provider;
021: import java.security.Security;
022: import javax.security.auth.callback.CallbackHandler;
023:
024: import org.apache.harmony.auth.internal.nls.Messages;
025:
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.Enumeration;
029: import java.util.Map;
030: import java.util.HashSet;
031: import java.util.Iterator;
032:
033: public class Sasl {
034: // SaslClientFactory service name
035: private static final String CLIENTFACTORYSRV = "SaslClientFactory"; //$NON-NLS-1$
036:
037: // SaslServerFactory service name
038: private static final String SERVERFACTORYSRV = "SaslServerFactory"; //$NON-NLS-1$
039:
040: public static final String POLICY_NOPLAINTEXT = "javax.security.sasl.policy.noplaintext"; //$NON-NLS-1$
041:
042: public static final String POLICY_NOACTIVE = "javax.security.sasl.policy.noactive"; //$NON-NLS-1$
043:
044: public static final String POLICY_NODICTIONARY = "javax.security.sasl.policy.nodictionary"; //$NON-NLS-1$
045:
046: public static final String POLICY_NOANONYMOUS = "javax.security.sasl.policy.noanonymous"; //$NON-NLS-1$
047:
048: public static final String POLICY_FORWARD_SECRECY = "javax.security.sasl.policy.forward"; //$NON-NLS-1$
049:
050: public static final String POLICY_PASS_CREDENTIALS = "javax.security.sasl.policy.credentials"; //$NON-NLS-1$
051:
052: public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer"; //$NON-NLS-1$
053:
054: public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize"; //$NON-NLS-1$
055:
056: public static final String REUSE = "javax.security.sasl.reuse"; //$NON-NLS-1$
057:
058: public static final String QOP = "javax.security.sasl.qop"; //$NON-NLS-1$
059:
060: public static final String STRENGTH = "javax.security.sasl.strength"; //$NON-NLS-1$
061:
062: public static final String SERVER_AUTH = "javax.security.sasl.server.authentication"; //$NON-NLS-1$
063:
064: // Default public constructor is overridden
065: private Sasl() {
066: super ();
067: }
068:
069: // Forms new instance of factory
070: private static Object newInstance(String factoryName, Provider prv)
071: throws SaslException {
072: String msg = Messages.getString("auth.31"); //$NON-NLS-1$
073: Object factory;
074: ClassLoader cl = prv.getClass().getClassLoader();
075: if (cl == null) {
076: cl = ClassLoader.getSystemClassLoader();
077: }
078: try {
079: factory = (Class.forName(factoryName, true, cl))
080: .newInstance();
081: return factory;
082: } catch (IllegalAccessException e) {
083: throw new SaslException(msg + factoryName, e);
084: } catch (ClassNotFoundException e) {
085: throw new SaslException(msg + factoryName, e);
086: } catch (InstantiationException e) {
087: throw new SaslException(msg + factoryName, e);
088: }
089: }
090:
091: /**
092: * This method forms the list of SaslClient/SaslServer factories which are
093: * implemented in used providers
094: */
095: private static Collection<?> findFactories(String service) {
096: HashSet<Object> fact = new HashSet<Object>();
097: Provider[] pp = Security.getProviders();
098: if ((pp == null) || (pp.length == 0)) {
099: return fact;
100: }
101: HashSet<String> props = new HashSet<String>();
102: for (int i = 0; i < pp.length; i++) {
103: String prName = pp[i].getName();
104: Enumeration<Object> keys = pp[i].keys();
105: while (keys.hasMoreElements()) {
106: String s = (String) keys.nextElement();
107: if (s.startsWith(service)) {
108: String prop = pp[i].getProperty(s);
109: try {
110: if (props.add(prName.concat(prop))) {
111: fact.add(newInstance(prop, pp[i]));
112: }
113: } catch (SaslException e) {
114: // ignore this factory
115: e.printStackTrace();
116: }
117: }
118: }
119: }
120: return fact;
121: }
122:
123: @SuppressWarnings("unchecked")
124: public static Enumeration<SaslClientFactory> getSaslClientFactories() {
125: Collection<SaslClientFactory> res = (Collection<SaslClientFactory>) findFactories(CLIENTFACTORYSRV);
126: return Collections.enumeration(res);
127:
128: }
129:
130: @SuppressWarnings("unchecked")
131: public static Enumeration<SaslServerFactory> getSaslServerFactories() {
132: Collection<SaslServerFactory> res = (Collection<SaslServerFactory>) findFactories(SERVERFACTORYSRV);
133: return Collections.enumeration(res);
134: }
135:
136: public static SaslServer createSaslServer(String mechanism,
137: String protocol, String serverName, Map<String, ?> prop,
138: CallbackHandler cbh) throws SaslException {
139: if (mechanism == null) {
140: throw new NullPointerException(Messages
141: .getString("auth.32")); //$NON-NLS-1$
142: }
143: Collection<?> res = findFactories(SERVERFACTORYSRV);
144: if (res.isEmpty()) {
145: return null;
146: }
147:
148: Iterator<?> iter = res.iterator();
149: while (iter.hasNext()) {
150: SaslServerFactory fact = (SaslServerFactory) iter.next();
151: String[] mech = fact.getMechanismNames(null);
152: boolean is = false;
153: if (mech != null) {
154: for (int j = 0; j < mech.length; j++) {
155: if (mech[j].equals(mechanism)) {
156: is = true;
157: break;
158: }
159: }
160: }
161: if (is) {
162: SaslServer saslS = fact.createSaslServer(mechanism,
163: protocol, serverName, prop, cbh);
164: if (saslS != null) {
165: return saslS;
166: }
167: }
168: }
169: return null;
170: }
171:
172: public static SaslClient createSaslClient(String[] mechanisms,
173: String authanticationID, String protocol,
174: String serverName, Map<String, ?> prop, CallbackHandler cbh)
175: throws SaslException {
176: if (mechanisms == null) {
177: throw new NullPointerException(Messages
178: .getString("auth.33")); //$NON-NLS-1$
179: }
180: Collection<?> res = findFactories(CLIENTFACTORYSRV);
181: if (res.isEmpty()) {
182: return null;
183: }
184:
185: Iterator<?> iter = res.iterator();
186: while (iter.hasNext()) {
187: SaslClientFactory fact = (SaslClientFactory) iter.next();
188: String[] mech = fact.getMechanismNames(null);
189: boolean is = false;
190: if (mech != null) {
191: for (int j = 0; j < mech.length; j++) {
192: for (int n = 0; n < mechanisms.length; n++) {
193: if (mech[j].equals(mechanisms[n])) {
194: is = true;
195: break;
196: }
197: }
198: }
199: }
200: if (is) {
201: SaslClient saslC = fact.createSaslClient(mechanisms,
202: authanticationID, protocol, serverName, prop,
203: cbh);
204: if (saslC != null) {
205: return saslC;
206: }
207: }
208: }
209: return null;
210: }
211: }
|