Sasl.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.sasl;
  18. import java.security.Provider;
  19. import java.security.Security;
  20. import javax.security.auth.callback.CallbackHandler;
  21. import org.apache.harmony.auth.internal.nls.Messages;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Enumeration;
  25. import java.util.Map;
  26. import java.util.HashSet;
  27. import java.util.Iterator;
  28. public class Sasl {
  29. // SaslClientFactory service name
  30. private static final String CLIENTFACTORYSRV = "SaslClientFactory"; //$NON-NLS-1$
  31. // SaslServerFactory service name
  32. private static final String SERVERFACTORYSRV = "SaslServerFactory"; //$NON-NLS-1$
  33. public static final String POLICY_NOPLAINTEXT = "javax.security.sasl.policy.noplaintext"; //$NON-NLS-1$
  34. public static final String POLICY_NOACTIVE = "javax.security.sasl.policy.noactive"; //$NON-NLS-1$
  35. public static final String POLICY_NODICTIONARY = "javax.security.sasl.policy.nodictionary"; //$NON-NLS-1$
  36. public static final String POLICY_NOANONYMOUS = "javax.security.sasl.policy.noanonymous"; //$NON-NLS-1$
  37. public static final String POLICY_FORWARD_SECRECY = "javax.security.sasl.policy.forward"; //$NON-NLS-1$
  38. public static final String POLICY_PASS_CREDENTIALS = "javax.security.sasl.policy.credentials"; //$NON-NLS-1$
  39. public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer"; //$NON-NLS-1$
  40. public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize"; //$NON-NLS-1$
  41. public static final String REUSE = "javax.security.sasl.reuse"; //$NON-NLS-1$
  42. public static final String QOP = "javax.security.sasl.qop"; //$NON-NLS-1$
  43. public static final String STRENGTH = "javax.security.sasl.strength"; //$NON-NLS-1$
  44. public static final String SERVER_AUTH = "javax.security.sasl.server.authentication"; //$NON-NLS-1$
  45. // Default public constructor is overridden
  46. private Sasl() {
  47. super();
  48. }
  49. // Forms new instance of factory
  50. private static Object newInstance(String factoryName, Provider prv) throws SaslException {
  51. String msg = Messages.getString("auth.31"); //$NON-NLS-1$
  52. Object factory;
  53. ClassLoader cl = prv.getClass().getClassLoader();
  54. if (cl == null) {
  55. cl = ClassLoader.getSystemClassLoader();
  56. }
  57. try {
  58. factory = (Class.forName(factoryName, true, cl)).newInstance();
  59. return factory;
  60. } catch (IllegalAccessException e) {
  61. throw new SaslException(msg + factoryName, e);
  62. } catch (ClassNotFoundException e) {
  63. throw new SaslException(msg + factoryName, e);
  64. } catch (InstantiationException e) {
  65. throw new SaslException(msg + factoryName, e);
  66. }
  67. }
  68. /**
  69. * This method forms the list of SaslClient/SaslServer factories which are
  70. * implemented in used providers
  71. */
  72. private static Collection<?> findFactories(String service) {
  73. HashSet<Object> fact = new HashSet<Object>();
  74. Provider[] pp = Security.getProviders();
  75. if ((pp == null) || (pp.length == 0)) {
  76. return fact;
  77. }
  78. HashSet<String> props = new HashSet<String>();
  79. for (int i = 0; i < pp.length; i++) {
  80. String prName = pp[i].getName();
  81. Enumeration<Object> keys = pp[i].keys();
  82. while (keys.hasMoreElements()) {
  83. String s = (String) keys.nextElement();
  84. if (s.startsWith(service)) {
  85. String prop = pp[i].getProperty(s);
  86. try {
  87. if (props.add(prName.concat(prop))) {
  88. fact.add(newInstance(prop, pp[i]));
  89. }
  90. } catch (SaslException e) {
  91. // ignore this factory
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. }
  97. return fact;
  98. }
  99. @SuppressWarnings("unchecked")
  100. public static Enumeration<SaslClientFactory> getSaslClientFactories() {
  101. Collection<SaslClientFactory> res = (Collection<SaslClientFactory>) findFactories(CLIENTFACTORYSRV);
  102. return Collections.enumeration(res);
  103. }
  104. @SuppressWarnings("unchecked")
  105. public static Enumeration<SaslServerFactory> getSaslServerFactories() {
  106. Collection<SaslServerFactory> res = (Collection<SaslServerFactory>) findFactories(SERVERFACTORYSRV);
  107. return Collections.enumeration(res);
  108. }
  109. public static SaslServer createSaslServer(String mechanism, String protocol,
  110. String serverName, Map<String, ?> prop, CallbackHandler cbh) throws SaslException {
  111. if (mechanism == null) {
  112. throw new NullPointerException(Messages.getString("auth.32")); //$NON-NLS-1$
  113. }
  114. Collection<?> res = findFactories(SERVERFACTORYSRV);
  115. if (res.isEmpty()) {
  116. return null;
  117. }
  118. Iterator<?> iter = res.iterator();
  119. while (iter.hasNext()) {
  120. SaslServerFactory fact = (SaslServerFactory) iter.next();
  121. String[] mech = fact.getMechanismNames(null);
  122. boolean is = false;
  123. if (mech != null) {
  124. for (int j = 0; j < mech.length; j++) {
  125. if (mech[j].equals(mechanism)) {
  126. is = true;
  127. break;
  128. }
  129. }
  130. }
  131. if (is) {
  132. SaslServer saslS = fact.createSaslServer(mechanism, protocol, serverName, prop,
  133. cbh);
  134. if (saslS != null) {
  135. return saslS;
  136. }
  137. }
  138. }
  139. return null;
  140. }
  141. public static SaslClient createSaslClient(String[] mechanisms, String authanticationID,
  142. String protocol, String serverName, Map<String, ?> prop, CallbackHandler cbh)
  143. throws SaslException {
  144. if (mechanisms == null) {
  145. throw new NullPointerException(Messages.getString("auth.33")); //$NON-NLS-1$
  146. }
  147. Collection<?> res = findFactories(CLIENTFACTORYSRV);
  148. if (res.isEmpty()) {
  149. return null;
  150. }
  151. Iterator<?> iter = res.iterator();
  152. while (iter.hasNext()) {
  153. SaslClientFactory fact = (SaslClientFactory) iter.next();
  154. String[] mech = fact.getMechanismNames(null);
  155. boolean is = false;
  156. if (mech != null) {
  157. for (int j = 0; j < mech.length; j++) {
  158. for (int n = 0; n < mechanisms.length; n++) {
  159. if (mech[j].equals(mechanisms[n])) {
  160. is = true;
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. if (is) {
  167. SaslClient saslC = fact.createSaslClient(mechanisms, authanticationID,
  168. protocol, serverName, prop, cbh);
  169. if (saslC != null) {
  170. return saslC;
  171. }
  172. }
  173. }
  174. return null;
  175. }
  176. }