1. /*
  2. * @(#)SaslClient.java 1.14 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.sasl;
  8. /**
  9. * Performs SASL authentication as a client.
  10. *<p>
  11. * A protocol library such as one for LDAP gets an instance of this
  12. * class in order to perform authentication defined by a specific SASL
  13. * mechanism. Invoking methods on the <tt>SaslClient</tt> instance
  14. * process challenges and create responses according to the SASL
  15. * mechanism implemented by the <tt>SaslClient</tt>.
  16. * As the authentication proceeds, the instance
  17. * encapsulates the state of a SASL client's authentication exchange.
  18. *<p>
  19. * Here's an example of how an LDAP library might use a <tt>SaslClient</tt>.
  20. * It first gets an instance of a <tt>SaslClient</tt>:
  21. *<blockquote><pre>
  22. * SaslClient sc = Sasl.createSaslClient(mechanisms,
  23. * authorizationId, protocol, serverName, props, callbackHandler);
  24. *</pre></blockquote>
  25. * It can then proceed to use the client for authentication.
  26. * For example, an LDAP library might use the client as follows:
  27. *<blockquote><pre>
  28. * // Get initial response and send to server
  29. * byte[] response = (sc.hasInitialResponse() ? sc.evaluateChallenge(new byte[0]) :
  30. * null);
  31. * LdapResult res = ldap.sendBindRequest(dn, sc.getName(), response);
  32. * while (!sc.isComplete() &&
  33. * (res.status == SASL_BIND_IN_PROGRESS || res.status == SUCCESS)) {
  34. * response = sc.evaluateChallenge(res.getBytes());
  35. * if (res.status == SUCCESS) {
  36. * // we're done; don't expect to send another BIND
  37. * if (response != null) {
  38. * throw new SaslException(
  39. * "Protocol error: attempting to send response after completion");
  40. * }
  41. * break;
  42. * }
  43. * res = ldap.sendBindRequest(dn, sc.getName(), response);
  44. * }
  45. * if (sc.isComplete() && res.status == SUCCESS) {
  46. * String qop = (String) sc.getNegotiatedProperty(Sasl.QOP);
  47. * if (qop != null
  48. * && (qop.equalsIgnoreCase("auth-int")
  49. * || qop.equalsIgnoreCase("auth-conf"))) {
  50. *
  51. * // Use SaslClient.wrap() and SaslClient.unwrap() for future
  52. * // communication with server
  53. * ldap.in = new SecureInputStream(sc, ldap.in);
  54. * ldap.out = new SecureOutputStream(sc, ldap.out);
  55. * }
  56. * }
  57. *</pre></blockquote>
  58. *
  59. * If the mechanism has an initial response, the library invokes
  60. * <tt>evaluateChallenge()</tt> with an empty
  61. * challenge and to get initial response.
  62. * Protocols such as IMAP4, which do not include an initial response with
  63. * their first authentication command to the server, initiates the
  64. * authentication without first calling <tt>hasInitialResponse()</tt>
  65. * or <tt>evaluateChallenge()</tt>.
  66. * When the server responds to the command, it sends an initial challenge.
  67. * For a SASL mechanism in which the client sends data first, the server should
  68. * have issued a challenge with no data. This will then result in a call
  69. * (on the client) to <tt>evaluateChallenge()</tt> with an empty challenge.
  70. *
  71. * @since 1.5
  72. *
  73. * @see Sasl
  74. * @see SaslClientFactory
  75. *
  76. * @author Rosanna Lee
  77. * @author Rob Weltman
  78. */
  79. public abstract interface SaslClient {
  80. /**
  81. * Returns the IANA-registered mechanism name of this SASL client.
  82. * (e.g. "CRAM-MD5", "GSSAPI").
  83. * @return A non-null string representing the IANA-registered mechanism name.
  84. */
  85. public abstract String getMechanismName();
  86. /**
  87. * Determines whether this mechanism has an optional initial response.
  88. * If true, caller should call <tt>evaluateChallenge()</tt> with an
  89. * empty array to get the initial response.
  90. *
  91. * @return true if this mechanism has an initial response.
  92. */
  93. public abstract boolean hasInitialResponse();
  94. /**
  95. * Evaluates the challenge data and generates a response.
  96. * If a challenge is received from the server during the authentication
  97. * process, this method is called to prepare an appropriate next
  98. * response to submit to the server.
  99. *
  100. * @param challenge The non-null challenge sent from the server.
  101. * The challenge array may have zero length.
  102. *
  103. * @return The possibly null reponse to send to the server.
  104. * It is null if the challenge accompanied a "SUCCESS" status and the challenge
  105. * only contains data for the client to update its state and no response
  106. * needs to be sent to the server. The response is a zero-length byte
  107. * array if the client is to send a response with no data.
  108. * @exception SaslException If an error occurred while processing
  109. * the challenge or generating a response.
  110. */
  111. public abstract byte[] evaluateChallenge(byte[] challenge)
  112. throws SaslException;
  113. /**
  114. * Determines whether the authentication exchange has completed.
  115. * This method may be called at any time, but typically, it
  116. * will not be called until the caller has received indication
  117. * from the server
  118. * (in a protocol-specific manner) that the exchange has completed.
  119. *
  120. * @return true if the authentication exchange has completed; false otherwise.
  121. */
  122. public abstract boolean isComplete();
  123. /**
  124. * Unwraps a byte array received from the server.
  125. * This method can be called only after the authentication exchange has
  126. * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
  127. * the authentication exchange has negotiated integrity and/or privacy
  128. * as the quality of protection; otherwise, an
  129. * <tt>IllegalStateException</tt> is thrown.
  130. *<p>
  131. * <tt>incoming</tt> is the contents of the SASL buffer as defined in RFC 2222
  132. * without the leading four octet field that represents the length.
  133. * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>incoming</tt>
  134. * to use.
  135. *
  136. * @param incoming A non-null byte array containing the encoded bytes
  137. * from the server.
  138. * @param offset The starting position at <tt>incoming</tt> of the bytes to use.
  139. * @param len The number of bytes from <tt>incoming</tt> to use.
  140. * @return A non-null byte array containing the decoded bytes.
  141. * @exception SaslException if <tt>incoming</tt> cannot be successfully
  142. * unwrapped.
  143. * @exception IllegalStateException if the authentication exchange has
  144. * not completed, or if the negotiated quality of protection
  145. * has neither integrity nor privacy.
  146. */
  147. public abstract byte[] unwrap(byte[] incoming, int offset, int len)
  148. throws SaslException;
  149. /**
  150. * Wraps a byte array to be sent to the server.
  151. * This method can be called only after the authentication exchange has
  152. * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
  153. * the authentication exchange has negotiated integrity and/or privacy
  154. * as the quality of protection; otherwise, an
  155. * <tt>IllegalStateException</tt> is thrown.
  156. *<p>
  157. * The result of this method will make up the contents of the SASL buffer
  158. * as defined in RFC 2222 without the leading four octet field that
  159. * represents the length.
  160. * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>outgoing</tt>
  161. * to use.
  162. *
  163. * @param outgoing A non-null byte array containing the bytes to encode.
  164. * @param offset The starting position at <tt>outgoing</tt> of the bytes to use.
  165. * @param len The number of bytes from <tt>outgoing</tt> to use.
  166. * @return A non-null byte array containing the encoded bytes.
  167. * @exception SaslException if <tt>outgoing</tt> cannot be successfully
  168. * wrapped.
  169. * @exception IllegalStateException if the authentication exchange has
  170. * not completed, or if the negotiated quality of protection
  171. * has neither integrity nor privacy.
  172. */
  173. public abstract byte[] wrap(byte[] outgoing, int offset, int len)
  174. throws SaslException;
  175. /**
  176. * Retrieves the negotiated property.
  177. * This method can be called only after the authentication exchange has
  178. * completed (i.e., when <tt>isComplete()</tt> returns true); otherwise, an
  179. * <tt>IllegalStateException</tt> is thrown.
  180. *
  181. * @param propName The non-null property name.
  182. * @return The value of the negotiated property. If null, the property was
  183. * not negotiated or is not applicable to this mechanism.
  184. * @exception IllegalStateException if this authentication exchange
  185. * has not completed
  186. */
  187. public abstract Object getNegotiatedProperty(String propName);
  188. /**
  189. * Disposes of any system resources or security-sensitive information
  190. * the SaslClient might be using. Invoking this method invalidates
  191. * the SaslClient instance. This method is idempotent.
  192. * @throws SaslException If a problem was encountered while disposing
  193. * the resources.
  194. */
  195. public abstract void dispose() throws SaslException;
  196. }