1. /*
  2. * @(#)SaslServer.java 1.13 04/02/03
  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 server.
  10. *<p>
  11. * A server such an LDAP server gets an instance of this
  12. * class in order to perform authentication defined by a specific SASL
  13. * mechanism. Invoking methods on the <tt>SaslServer</tt> instance
  14. * generates challenges according to the SASL
  15. * mechanism implemented by the <tt>SaslServer</tt>.
  16. * As the authentication proceeds, the instance
  17. * encapsulates the state of a SASL server's authentication exchange.
  18. *<p>
  19. * Here's an example of how an LDAP server might use a <tt>SaslServer</tt>.
  20. * It first gets an instance of a <tt>SaslServer</tt> for the SASL mechanism
  21. * requested by the client:
  22. *<blockquote><pre>
  23. * SaslServer ss = Sasl.createSaslServer(mechanism,
  24. * "ldap", myFQDN, props, callbackHandler);
  25. *</pre></blockquote>
  26. * It can then proceed to use the server for authentication.
  27. * For example, suppose the LDAP server received an LDAP BIND request
  28. * containing the name of the SASL mechanism and an (optional) initial
  29. * response. It then might use the server as follows:
  30. *<blockquote><pre>
  31. * while (!ss.isComplete()) {
  32. * try {
  33. * byte[] challenge = ss.evaluateResponse(response);
  34. * if (ss.isComplete()) {
  35. * status = ldap.sendBindResponse(mechanism, challenge, SUCCESS);
  36. * } else {
  37. * status = ldap.sendBindResponse(mechanism, challenge,
  38. SASL_BIND_IN_PROGRESS);
  39. * response = ldap.readBindRequest();
  40. * }
  41. * } catch (SaslException e) {
  42. * status = ldap.sendErrorResponse(e);
  43. * break;
  44. * }
  45. * }
  46. * if (ss.isComplete() && status == SUCCESS) {
  47. * String qop = (String) sc.getNegotiatedProperty(Sasl.QOP);
  48. * if (qop != null
  49. * && (qop.equalsIgnoreCase("auth-int")
  50. * || qop.equalsIgnoreCase("auth-conf"))) {
  51. *
  52. * // Use SaslServer.wrap() and SaslServer.unwrap() for future
  53. * // communication with client
  54. * ldap.in = new SecureInputStream(ss, ldap.in);
  55. * ldap.out = new SecureOutputStream(ss, ldap.out);
  56. * }
  57. * }
  58. *</pre></blockquote>
  59. *
  60. * @since 1.5
  61. *
  62. * @see Sasl
  63. * @see SaslServerFactory
  64. *
  65. * @author Rosanna Lee
  66. * @author Rob Weltman
  67. */
  68. public abstract interface SaslServer {
  69. /**
  70. * Returns the IANA-registered mechanism name of this SASL server.
  71. * (e.g. "CRAM-MD5", "GSSAPI").
  72. * @return A non-null string representing the IANA-registered mechanism name.
  73. */
  74. public abstract String getMechanismName();
  75. /**
  76. * Evaluates the response data and generates a challenge.
  77. *
  78. * If a response is received from the client during the authentication
  79. * process, this method is called to prepare an appropriate next
  80. * challenge to submit to the client. The challenge is null if the
  81. * authentication has succeeded and no more challenge data is to be sent
  82. * to the client. It is non-null if the authentication must be continued
  83. * by sending a challenge to the client, or if the authentication has
  84. * succeeded but challenge data needs to be processed by the client.
  85. * <tt>isComplete()</tt> should be called
  86. * after each call to <tt>evaluateResponse()</tt>,to determine if any further
  87. * response is needed from the client.
  88. *
  89. * @param response The non-null (but possibly empty) response sent
  90. * by the client.
  91. *
  92. * @return The possibly null challenge to send to the client.
  93. * It is null if the authentication has succeeded and there is
  94. * no more challenge data to be sent to the client.
  95. * @exception SaslException If an error occurred while processing
  96. * the response or generating a challenge.
  97. */
  98. public abstract byte[] evaluateResponse(byte[] response)
  99. throws SaslException;
  100. /**
  101. * Determines whether the authentication exchange has completed.
  102. * This method is typically called after each invocation of
  103. * <tt>evaluateResponse()</tt> to determine whether the
  104. * authentication has completed successfully or should be continued.
  105. * @return true if the authentication exchange has completed; false otherwise.
  106. */
  107. public abstract boolean isComplete();
  108. /**
  109. * Reports the authorization ID in effect for the client of this
  110. * session.
  111. * This method can only be called if isComplete() returns true.
  112. * @return The authorization ID of the client.
  113. * @exception IllegalStateException if this authentication session has not completed
  114. */
  115. public String getAuthorizationID();
  116. /**
  117. * Unwraps a byte array received from the client.
  118. * This method can be called only after the authentication exchange has
  119. * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
  120. * the authentication exchange has negotiated integrity and/or privacy
  121. * as the quality of protection; otherwise,
  122. * an <tt>IllegalStateException</tt> is thrown.
  123. *<p>
  124. * <tt>incoming</tt> is the contents of the SASL buffer as defined in RFC 2222
  125. * without the leading four octet field that represents the length.
  126. * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>incoming</tt>
  127. * to use.
  128. *
  129. * @param incoming A non-null byte array containing the encoded bytes
  130. * from the client.
  131. * @param offset The starting position at <tt>incoming</tt> of the bytes to use.
  132. * @param len The number of bytes from <tt>incoming</tt> to use.
  133. * @return A non-null byte array containing the decoded bytes.
  134. * @exception SaslException if <tt>incoming</tt> cannot be successfully
  135. * unwrapped.
  136. * @exception IllegalStateException if the authentication exchange has
  137. * not completed, or if the negotiated quality of protection
  138. * has neither integrity nor privacy
  139. */
  140. public abstract byte[] unwrap(byte[] incoming, int offset, int len)
  141. throws SaslException;
  142. /**
  143. * Wraps a byte array to be sent to the client.
  144. * This method can be called only after the authentication exchange has
  145. * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
  146. * the authentication exchange has negotiated integrity and/or privacy
  147. * as the quality of protection; otherwise, a <tt>SaslException</tt> is thrown.
  148. *<p>
  149. * The result of this method
  150. * will make up the contents of the SASL buffer as defined in RFC 2222
  151. * without the leading four octet field that represents the length.
  152. * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>outgoing</tt>
  153. * to use.
  154. *
  155. * @param outgoing A non-null byte array containing the bytes to encode.
  156. * @param offset The starting position at <tt>outgoing</tt> of the bytes to use.
  157. * @param len The number of bytes from <tt>outgoing</tt> to use.
  158. * @return A non-null byte array containing the encoded bytes.
  159. * @exception SaslException if <tt>outgoing</tt> cannot be successfully
  160. * wrapped.
  161. * @exception IllegalStateException if the authentication exchange has
  162. * not completed, or if the negotiated quality of protection has
  163. * neither integrity nor privacy.
  164. */
  165. public abstract byte[] wrap(byte[] outgoing, int offset, int len)
  166. throws SaslException;
  167. /**
  168. * Retrieves the negotiated property.
  169. * This method can be called only after the authentication exchange has
  170. * completed (i.e., when <tt>isComplete()</tt> returns true); otherwise, an
  171. * <tt>IllegalStateException</tt> is thrown.
  172. *
  173. * @param propName the property
  174. * @return The value of the negotiated property. If null, the property was
  175. * not negotiated or is not applicable to this mechanism.
  176. * @exception IllegalStateException if this authentication exchange has not completed
  177. */
  178. public abstract Object getNegotiatedProperty(String propName);
  179. /**
  180. * Disposes of any system resources or security-sensitive information
  181. * the SaslServer might be using. Invoking this method invalidates
  182. * the SaslServer instance. This method is idempotent.
  183. * @throws SaslException If a problem was encountered while disposing
  184. * the resources.
  185. */
  186. public abstract void dispose() throws SaslException;
  187. }