ClientMechanism.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* ClientMechanism.java --
  2. Copyright (C) 2003, 2005, 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 java.util.HashMap;
  34. import java.util.Map;
  35. import javax.security.auth.callback.CallbackHandler;
  36. import javax.security.sasl.Sasl;
  37. import javax.security.sasl.SaslClient;
  38. import javax.security.sasl.SaslException;
  39. /**
  40. * A base class to facilitate implementing SASL client-side mechanisms.
  41. */
  42. public abstract class ClientMechanism
  43. implements SaslClient
  44. {
  45. /** Name of this mechanism. */
  46. protected String mechanism;
  47. /** The authorisation identity. */
  48. protected String authorizationID;
  49. /** Name of protocol using this mechanism. */
  50. protected String protocol;
  51. /** Name of server to authenticate to. */
  52. protected String serverName;
  53. /** Properties of qualities desired for this mechanism. */
  54. protected Map properties;
  55. /** Callback handler to use with this mechanism instance. */
  56. protected CallbackHandler handler;
  57. /** Channel binding data to use with this mechanism instance. */
  58. protected byte[] channelBinding;
  59. /** Whether authentication phase is completed (true) or not (false). */
  60. protected boolean complete = false;
  61. /** The state of the authentication automaton. */
  62. protected int state = -1;
  63. protected ClientMechanism(final String mechanism)
  64. {
  65. super();
  66. this.mechanism = mechanism;
  67. this.state = -1;
  68. }
  69. protected abstract void initMechanism() throws SaslException;
  70. protected abstract void resetMechanism() throws SaslException;
  71. public abstract byte[] evaluateChallenge(byte[] challenge)
  72. throws SaslException;
  73. public abstract boolean hasInitialResponse();
  74. public boolean isComplete()
  75. {
  76. return complete;
  77. }
  78. public byte[] unwrap(final byte[] incoming, final int offset, final int len)
  79. throws SaslException
  80. {
  81. if (! isComplete())
  82. throw new IllegalMechanismStateException();
  83. return this.engineUnwrap(incoming, offset, len);
  84. }
  85. public byte[] wrap(final byte[] outgoing, final int offset, final int len)
  86. throws SaslException
  87. {
  88. if (! isComplete())
  89. throw new IllegalMechanismStateException();
  90. return this.engineWrap(outgoing, offset, len);
  91. }
  92. public String getMechanismName()
  93. {
  94. return mechanism;
  95. }
  96. public Object getNegotiatedProperty(final String propName)
  97. {
  98. if (! isComplete())
  99. throw new IllegalStateException();
  100. if (Sasl.QOP.equals(propName))
  101. return getNegotiatedQOP();
  102. if (Sasl.STRENGTH.equals(propName))
  103. return getNegotiatedStrength();
  104. if (Sasl.SERVER_AUTH.equals(propName))
  105. return getNegotiatedServerAuth();
  106. if (Sasl.MAX_BUFFER.equals(propName))
  107. return getNegotiatedMaxBuffer();
  108. if (Sasl.RAW_SEND_SIZE.equals(propName))
  109. return getNegotiatedRawSendSize();
  110. if (Sasl.POLICY_NOPLAINTEXT.equals(propName))
  111. return getNegotiatedPolicyNoPlainText();
  112. if (Sasl.POLICY_NOACTIVE.equals(propName))
  113. return getNegotiatedPolicyNoActive();
  114. if (Sasl.POLICY_NODICTIONARY.equals(propName))
  115. return getNegotiatedPolicyNoDictionary();
  116. if (Sasl.POLICY_NOANONYMOUS.equals(propName))
  117. return getNegotiatedPolicyNoAnonymous();
  118. if (Sasl.POLICY_FORWARD_SECRECY.equals(propName))
  119. return getNegotiatedPolicyForwardSecrecy();
  120. if (Sasl.POLICY_PASS_CREDENTIALS.equals(propName))
  121. return getNegotiatedPolicyPassCredentials();
  122. if (Sasl.REUSE.equals(propName))
  123. return getReuse();
  124. return null;
  125. }
  126. public void dispose() throws SaslException
  127. {
  128. }
  129. public String getAuthorizationID()
  130. {
  131. return authorizationID;
  132. }
  133. protected String getNegotiatedQOP()
  134. {
  135. return Registry.QOP_AUTH;
  136. }
  137. protected String getNegotiatedStrength()
  138. {
  139. return Registry.STRENGTH_LOW;
  140. }
  141. protected String getNegotiatedServerAuth()
  142. {
  143. return Registry.SERVER_AUTH_FALSE;
  144. }
  145. protected String getNegotiatedMaxBuffer()
  146. {
  147. return null;
  148. }
  149. protected String getNegotiatedRawSendSize()
  150. {
  151. return String.valueOf(Registry.SASL_BUFFER_MAX_LIMIT);
  152. }
  153. protected String getNegotiatedPolicyNoPlainText()
  154. {
  155. return null;
  156. }
  157. protected String getNegotiatedPolicyNoActive()
  158. {
  159. return null;
  160. }
  161. protected String getNegotiatedPolicyNoDictionary()
  162. {
  163. return null;
  164. }
  165. protected String getNegotiatedPolicyNoAnonymous()
  166. {
  167. return null;
  168. }
  169. protected String getNegotiatedPolicyForwardSecrecy()
  170. {
  171. return null;
  172. }
  173. protected String getNegotiatedPolicyPassCredentials()
  174. {
  175. return null;
  176. }
  177. protected String getReuse()
  178. {
  179. return Registry.REUSE_FALSE;
  180. }
  181. protected byte[] engineUnwrap(final byte[] incoming, final int offset,
  182. final int len) throws SaslException
  183. {
  184. final byte[] result = new byte[len];
  185. System.arraycopy(incoming, offset, result, 0, len);
  186. return result;
  187. }
  188. protected byte[] engineWrap(final byte[] outgoing, final int offset,
  189. final int len) throws SaslException
  190. {
  191. final byte[] result = new byte[len];
  192. System.arraycopy(outgoing, offset, result, 0, len);
  193. return result;
  194. }
  195. /**
  196. * Initialises the mechanism with designated attributes. Permissible names and
  197. * values are mechanism specific.
  198. *
  199. * @param attributes a set of name-value pairs that describes the desired
  200. * future behaviour of this instance.
  201. * @throws IllegalMechanismStateException if the instance is already
  202. * initialised.
  203. * @throws SaslException if an exception occurs during the process.
  204. */
  205. public void init(final Map attributes) throws SaslException
  206. {
  207. if (state != -1)
  208. throw new IllegalMechanismStateException("init()");
  209. if (properties == null)
  210. properties = new HashMap();
  211. else
  212. properties.clear();
  213. if (attributes != null)
  214. {
  215. authorizationID = (String) attributes.get(Registry.SASL_AUTHORISATION_ID);
  216. protocol = (String) attributes.get(Registry.SASL_PROTOCOL);
  217. serverName = (String) attributes.get(Registry.SASL_SERVER_NAME);
  218. handler = (CallbackHandler) attributes.get(Registry.SASL_CALLBACK_HANDLER);
  219. channelBinding = (byte[]) attributes.get(Registry.SASL_CHANNEL_BINDING);
  220. properties.putAll(attributes);
  221. }
  222. else
  223. handler = null;
  224. if (authorizationID == null)
  225. authorizationID = "";
  226. if (protocol == null)
  227. protocol = "";
  228. if (serverName == null)
  229. serverName = "";
  230. if (channelBinding == null)
  231. channelBinding = new byte[0];
  232. initMechanism();
  233. complete = false;
  234. state = 0;
  235. }
  236. /**
  237. * Resets the mechanism instance for re-initialisation and use with other
  238. * characteristics.
  239. *
  240. * @throws SaslException if an exception occurs during the process.
  241. */
  242. public void reset() throws SaslException
  243. {
  244. resetMechanism();
  245. properties.clear();
  246. authorizationID = protocol = serverName = null;
  247. channelBinding = null;
  248. complete = false;
  249. state = -1;
  250. }
  251. }