001 /* SaslClient.java --
002 Copyright (C) 2003, 2004, 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 client.</p>
043 *
044 * <p>A protocol library such as one for LDAP gets an instance of this class in
045 * order to perform authentication defined by a specific SASL mechanism.
046 * Invoking methods on the <code>SaslClient</code> instance process challenges
047 * and create responses according to the SASL mechanism implemented by the
048 * <code>SaslClient</code>. As the authentication proceeds, the instance
049 * encapsulates the state of a SASL client's authentication exchange.</p>
050 *
051 * <p>Here's an example of how an LDAP library might use a <code>SaslClient</code>.
052 * It first gets an instance of a SaslClient:</p>
053 * <pre>
054 *SaslClient sc =
055 * Sasl.createSaslClient(mechanisms, authorizationID, protocol,
056 * serverName, props, callbackHandler);
057 * </pre>
058 *
059 * <p>It can then proceed to use the client for authentication. For example, an
060 * LDAP library might use the client as follows:</p>
061 * <pre>
062 * // Get initial response and send to server
063 *byte[] response = sc.hasInitialResponse()
064 * ? sc.evaluateChallenge(new byte[0]) : null;
065 *LdapResult res = ldap.sendBindRequest(dn, sc.getName(), response);
066 *while (!sc.isComplete()
067 * && ((res.status == SASL_BIND_IN_PROGRESS) || (res.status == SUCCESS))) {
068 * response = sc.evaluateChallenge( res.getBytes() );
069 * if (res.status == SUCCESS) {
070 * // we're done; don't expect to send another BIND
071 * if ( response != null ) {
072 * throw new SaslException(
073 * "Protocol error: attempting to send response after completion");
074 * }
075 * break;
076 * }
077 * res = ldap.sendBindRequest(dn, sc.getName(), response);
078 *}
079 *if (sc.isComplete() && (res.status == SUCCESS) ) {
080 * String qop = (String)sc.getNegotiatedProperty(Sasl.QOP);
081 * if ((qop != null)
082 * && (qop.equalsIgnoreCase("auth-int")
083 * || qop.equalsIgnoreCase("auth-conf"))) {
084 * // Use SaslClient.wrap() and SaslClient.unwrap() for future
085 * // communication with server
086 * ldap.in = new SecureInputStream(sc, ldap.in);
087 * ldap.out = new SecureOutputStream(sc, ldap.out);
088 * }
089 *}
090 * </pre>
091 *
092 * <p>If the mechanism has an initial response, the library invokes
093 * {@link #evaluateChallenge(byte[])} with an empty challenge to get the initial
094 * response. Protocols such as IMAP4, which do not include an initial response
095 * with their first authentication command to the server, initiate the
096 * authentication without first calling {@link #hasInitialResponse()} or
097 * {@link #evaluateChallenge(byte[])}. When the server responds to the command,
098 * it sends an initial challenge. For a SASL mechanism in which the client sends
099 * data first, the server should have issued a challenge with no data. This will
100 * then result in a call (on the client) to {@link #evaluateChallenge(byte[])}
101 * with an empty challenge.</p>
102 *
103 * @see Sasl
104 * @see SaslClientFactory
105 *
106 * @since 1.5
107 */
108 public interface SaslClient
109 {
110
111 /**
112 * Returns the IANA-registered mechanism name of this SASL client. (e.g.
113 * "CRAM-MD5", "GSSAPI").
114 *
115 * @return a non-null string representing the IANA-registered mechanism name.
116 */
117 String getMechanismName();
118
119 /**
120 * Determines if this mechanism has an optional initial response. If
121 * <code>true</code>, caller should call {@link #evaluateChallenge(byte[])}
122 * with an empty array to get the initial response.
123 *
124 * @return <code>true</code> if this mechanism has an initial response.
125 */
126 boolean hasInitialResponse();
127
128 /**
129 * Evaluates the challenge data and generates a response. If a challenge is
130 * received from the server during the authentication process, this method is
131 * called to prepare an appropriate next response to submit to the server.
132 *
133 * @param challenge the non-null challenge sent from the server. The
134 * challenge array may have zero length.
135 * @return the possibly <code>null</code> reponse to send to the server. It
136 * is <code>null</code> if the challenge accompanied a "SUCCESS" status and
137 * the challenge only contains data for the client to update its state and no
138 * response needs to be sent to the server. The response is a zero-length
139 * byte array if the client is to send a response with no data.
140 * @throws SaslException if an error occurred while processing the challenge
141 * or generating a response.
142 */
143 byte[] evaluateChallenge(byte[] challenge) throws SaslException;
144
145 /**
146 * Determines if the authentication exchange has completed. This method may
147 * be called at any time, but typically, it will not be called until the
148 * caller has received indication from the server (in a protocol-specific
149 * manner) that the exchange has completed.
150 *
151 * @return <code>true</code> if the authentication exchange has completed;
152 * <code>false</code> otherwise.
153 */
154 boolean isComplete();
155
156 /**
157 * <p>Unwraps a byte array received from the server. This method can be
158 * called only after the authentication exchange has completed (i.e., when
159 * {@link #isComplete()} returns <code>true</code>) and only if the
160 * authentication exchange has negotiated integrity and/or privacy as the
161 * quality of protection; otherwise, an {@link IllegalStateException} is
162 * thrown.</p>
163 *
164 * <p><code>incoming</code> is the contents of the SASL buffer as defined in
165 * RFC 2222 without the leading four octet field that represents the length.
166 * <code>offset</code> and <code>len</code> specify the portion of incoming
167 * to use.</p>
168 *
169 * @param incoming a non-null byte array containing the encoded bytes from
170 * the server.
171 * @param offset the starting position at <code>incoming</code> of the bytes
172 * to use.
173 * @param len the number of bytes from <code>incoming</code> to use.
174 * @return a non-null byte array containing the decoded bytes.
175 * @throws SaslException if <code>incoming</code> cannot be successfully
176 * unwrapped.
177 * @throws IllegalStateException if the authentication exchange has not
178 * completed, or if the negotiated quality of protection has neither
179 * integrity nor privacy.
180 */
181 byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException;
182
183 /**
184 * <p>Wraps a byte array to be sent to the server. This method can be called
185 * only after the authentication exchange has completed (i.e., when
186 * {@link #isComplete()} returns <code>true</code>) and only if the
187 * authentication exchange has negotiated integrity and/or privacy as the
188 * quality of protection; otherwise, an {@link IllegalStateException} is
189 * thrown.</p>
190 *
191 * <p>The result of this method will make up the contents of the SASL buffer
192 * as defined in RFC 2222 without the leading four octet field that
193 * represents the length. <code>offset</code> and <code>len</code> specify
194 * the portion of <code>outgoing</code> to use.</p>
195 *
196 * @param outgoing a non-null byte array containing the bytes to encode.
197 * @param offset the starting position at <code>outgoing</code> of the bytes
198 * to use.
199 * @param len the number of bytes from <code>outgoing</code> to use.
200 * @return a non-null byte array containing the encoded bytes.
201 * @throws SaslException if <code>outgoing</code> cannot be successfully
202 * wrapped.
203 * @throws IllegalStateException if the authentication exchange has not
204 * completed, or if the negotiated quality of protection has neither
205 * integrity nor privacy.
206 */
207 byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException;
208
209 /**
210 * Retrieves the negotiated property. This method can be called only after
211 * the authentication exchange has completed (i.e., when {@link #isComplete()}
212 * returns <code>true</code>); otherwise, an {@link IllegalStateException} is
213 * thrown.
214 *
215 * @param propName the non-null property name.
216 * @return the value of the negotiated property. If <code>null</code>, the
217 * property was not negotiated or is not applicable to this mechanism.
218 * @throws IllegalStateException if this authentication exchange has not
219 * completed.
220 */
221 Object getNegotiatedProperty(String propName);
222
223 /**
224 * Disposes of any system resources or security-sensitive information the
225 * <code>SaslClient</code> might be using. Invoking this method invalidates
226 * the <code>SaslClient</code> instance. This method is idempotent.
227 *
228 * @throws SaslException if a problem was encountered while disposing of the
229 * resources.
230 */
231 void dispose() throws SaslException;
232 }