001    /* SaslServer.java
002       Copyright (C) 2003, 2005, Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.security.sasl;
040    
041    /**
042     * <p>Performs SASL authentication as a server.</p>
043     *
044     * <p>A server such as an LDAP server gets an instance of this class in order to
045     * perform authentication defined by a specific SASL mechanism. Invoking methods
046     * on the <code>SaslServer</code> instance generates challenges corresponding to
047     * the SASL mechanism implemented by the <code>SaslServer</code> instance. As
048     * the authentication proceeds, the instance encapsulates the state of a SASL
049     * server's authentication exchange.</p>
050     *
051     * <p>Here's an example of how an LDAP server might use a <code>SaslServer</code>
052     * instance. It first gets an instance of a <code>SaslServer</code> for the SASL
053     * mechanism requested by the client:</p>
054     *
055     * <pre>
056     *SaslServer ss =
057     *      Sasl.createSaslServer(mechanism, "ldap", myFQDN, props, callbackHandler);
058     * </pre>
059     *
060     * <p>It can then proceed to use the server for authentication. For example,
061     * suppose the LDAP server received an LDAP BIND request containing the name of
062     * the SASL mechanism and an (optional) initial response. It then might use the
063     * server as follows:</p>
064     *
065     * <pre>
066     *while (!ss.isComplete()) {
067     *   try {
068     *      byte[] challenge = ss.evaluateResponse(response);
069     *      if (ss.isComplete()) {
070     *         status = ldap.sendBindResponse(mechanism, challenge, SUCCESS);
071     *      } else {
072     *         status = ldap.sendBindResponse(mechanism, challenge, SASL_BIND_IN_PROGRESS);
073     *         response = ldap.readBindRequest();
074     *      }
075     *   } catch (SaslException x) {
076     *      status = ldap.sendErrorResponse(x);
077     *      break;
078     *   }
079     *}
080     *if (ss.isComplete() && (status == SUCCESS)) {
081     *   String qop = (String) sc.getNegotiatedProperty(Sasl.QOP);
082     *   if (qop != null
083     *         && (qop.equalsIgnoreCase("auth-int")
084     *            || qop.equalsIgnoreCase("auth-conf"))) {
085     *      // Use SaslServer.wrap() and SaslServer.unwrap() for future
086     *      // communication with client
087     *      ldap.in = new SecureInputStream(ss, ldap.in);
088     *      ldap.out = new SecureOutputStream(ss, ldap.out);
089     *   }
090     *}
091     * </pre>
092     *
093     * @see Sasl
094     * @see SaslServerFactory
095     *
096     * @since 1.5
097     */
098    public interface SaslServer
099    {
100    
101      /**
102       * Returns the IANA-registered mechanism name of this SASL server (e.g.
103       * "CRAM-MD5", "GSSAPI").
104       *
105       * @return a non-null string representing the IANA-registered mechanism name.
106       */
107      String getMechanismName();
108    
109      /**
110       * Evaluates the response data and generates a challenge. If a response is
111       * received from the client during the authentication process, this method is
112       * called to prepare an appropriate next challenge to submit to the client.
113       * The challenge is <code>null</code> if the authentication has succeeded and
114       * no more challenge data is to be sent to the client. It is non-null if the
115       * authentication must be continued by sending a challenge to the client, or
116       * if the authentication has succeeded but challenge data needs to be
117       * processed by the client. {@link #isComplete()} should be called after each
118       * call to <code>evaluateResponse()</code>,to determine if any further
119       * response is needed from the client.
120       *
121       * @param response the non-null (but possibly empty) response sent by the
122       * client.
123       * @return the possibly <code>null</code> challenge to send to the client.
124       * It is <code>null</code> if the authentication has succeeded and there is
125       * no more challenge data to be sent to the client.
126       * @throws SaslException if an error occurred while processing the response
127       * or generating a challenge.
128       */
129      byte[] evaluateResponse(byte[] response) throws SaslException;
130    
131      /**
132       * Determines if the authentication exchange has completed. This method is
133       * typically called after each invocation of {@link #evaluateResponse(byte[])}
134       * to determine whether the authentication has completed successfully or
135       * should be continued.
136       *
137       * @return <code>true</code> if the authentication exchange has completed;
138       * <code>false</code> otherwise.
139       */
140      boolean isComplete();
141    
142      /**
143       * Reports the authorization ID in effect for the client of this session This
144       * method can only be called if {@link #isComplete()} returns <code>true</code>.
145       *
146       * @return the authorization ID of the client.
147       * @throws IllegalStateException if this authentication session has not
148       * completed.
149       */
150      String getAuthorizationID();
151    
152      /**
153       * <p>Unwraps a byte array received from the client. This method can be called
154       * only after the authentication exchange has completed (i.e., when
155       * {@link #isComplete()} returns <code>true</code>) and only if the
156       * authentication exchange has negotiated integrity and/or privacy as the
157       * quality of protection; otherwise, an {@link IllegalStateException} is
158       * thrown.</p>
159       *
160       * <p><code>incoming</code> is the contents of the SASL buffer as defined in
161       * RFC 2222 without the leading four octet field that represents the length.
162       * <code>offset</code> and <code>len</code> specify the portion of incoming
163       * to use.</p>
164       *
165       * @param incoming a non-null byte array containing the encoded bytes from
166       * the client.
167       * @param offset the starting position at <code>incoming</code> of the bytes
168       * to use.
169       * @param len the number of bytes from <code>incoming</code> to use.
170       * @return a non-null byte array containing the decoded bytes.
171       * @throws SaslException if <code>incoming</code> cannot be successfully
172       * unwrapped.
173       * @throws IllegalStateException if the authentication exchange has not
174       * completed, or if the negotiated quality of protection has neither
175       * integrity nor privacy.
176       */
177      byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException;
178    
179      /**
180       * <p>Wraps a byte array to be sent to the client. This method can be called
181       * only after the authentication exchange has completed (i.e., when
182       * {@link #isComplete()} returns <code>true</code>) and only if the
183       * authentication exchange has negotiated integrity and/or privacy as the
184       * quality of protection; otherwise, an {@link IllegalStateException} is
185       * thrown.</p>
186       *
187       * <p>The result of this method will make up the contents of the SASL buffer
188       * as defined in RFC 2222 without the leading four octet field that
189       * represents the length. <code>offset</code> and <code>len</code> specify
190       * the portion of <code>outgoing</code> to use.
191       *
192       * @param outgoing a non-null byte array containing the bytes to encode.
193       * @param offset the starting position at <code>outgoing</code> of the bytes
194       * to use.
195       * @param len the number of bytes from <code>outgoing</code> to use.
196       * @return a non-null byte array containing the encoded bytes.
197       * @throws SaslException if <code>outgoing</code> cannot be successfully
198       * wrapped.
199       * @throws IllegalStateException if the authentication exchange has not
200       * completed, or if the negotiated quality of protection has neither
201       * integrity nor privacy.
202       */
203      byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException;
204    
205      /**
206       * Retrieves the negotiated property. This method can be called only after
207       * the authentication exchange has completed (i.e., when
208       * {@link #isComplete()} returns <code>true</code>); otherwise, an
209       * {@link IllegalStateException} is thrown.
210       *
211       * @return the value of the negotiated property. If <code>null</code>, the
212       * property was not negotiated or is not applicable to this mechanism.
213       * @throws IllegalStateException if this authentication exchange has not
214       * completed.
215       */
216      Object getNegotiatedProperty(String propName);
217    
218      /**
219       * Disposes of any system resources or security-sensitive information the
220       * <code>SaslServer</code> might be using. Invoking this method invalidates
221       * the <code>SaslServer</code> instance. This method is idempotent.
222       *
223       * @throws SaslException if a problem was encountered while disposing of the
224       * resources.
225       */
226      void dispose() throws SaslException;
227    }