ClientFactory.java 5.8 KB

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