001 /*
002 * Copyright 1999-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.security.sasl;
027
028 /**
029 * Performs SASL authentication as a client.
030 *<p>
031 * A protocol library such as one for LDAP gets an instance of this
032 * class in order to perform authentication defined by a specific SASL
033 * mechanism. Invoking methods on the <tt>SaslClient</tt> instance
034 * process challenges and create responses according to the SASL
035 * mechanism implemented by the <tt>SaslClient</tt>.
036 * As the authentication proceeds, the instance
037 * encapsulates the state of a SASL client's authentication exchange.
038 *<p>
039 * Here's an example of how an LDAP library might use a <tt>SaslClient</tt>.
040 * It first gets an instance of a <tt>SaslClient</tt>:
041 *<blockquote><pre>
042 * SaslClient sc = Sasl.createSaslClient(mechanisms,
043 * authorizationId, protocol, serverName, props, callbackHandler);
044 *</pre></blockquote>
045 * It can then proceed to use the client for authentication.
046 * For example, an LDAP library might use the client as follows:
047 *<blockquote><pre>
048 * // Get initial response and send to server
049 * byte[] response = (sc.hasInitialResponse() ? sc.evaluateChallenge(new byte[0]) :
050 * null);
051 * LdapResult res = ldap.sendBindRequest(dn, sc.getName(), response);
052 * while (!sc.isComplete() &&
053 * (res.status == SASL_BIND_IN_PROGRESS || res.status == SUCCESS)) {
054 * response = sc.evaluateChallenge(res.getBytes());
055 * if (res.status == SUCCESS) {
056 * // we're done; don't expect to send another BIND
057 * if (response != null) {
058 * throw new SaslException(
059 * "Protocol error: attempting to send response after completion");
060 * }
061 * break;
062 * }
063 * res = ldap.sendBindRequest(dn, sc.getName(), response);
064 * }
065 * if (sc.isComplete() && res.status == SUCCESS) {
066 * String qop = (String) sc.getNegotiatedProperty(Sasl.QOP);
067 * if (qop != null
068 * && (qop.equalsIgnoreCase("auth-int")
069 * || qop.equalsIgnoreCase("auth-conf"))) {
070 *
071 * // Use SaslClient.wrap() and SaslClient.unwrap() for future
072 * // communication with server
073 * ldap.in = new SecureInputStream(sc, ldap.in);
074 * ldap.out = new SecureOutputStream(sc, ldap.out);
075 * }
076 * }
077 *</pre></blockquote>
078 *
079 * If the mechanism has an initial response, the library invokes
080 * <tt>evaluateChallenge()</tt> with an empty
081 * challenge and to get initial response.
082 * Protocols such as IMAP4, which do not include an initial response with
083 * their first authentication command to the server, initiates the
084 * authentication without first calling <tt>hasInitialResponse()</tt>
085 * or <tt>evaluateChallenge()</tt>.
086 * When the server responds to the command, it sends an initial challenge.
087 * For a SASL mechanism in which the client sends data first, the server should
088 * have issued a challenge with no data. This will then result in a call
089 * (on the client) to <tt>evaluateChallenge()</tt> with an empty challenge.
090 *
091 * @since 1.5
092 *
093 * @see Sasl
094 * @see SaslClientFactory
095 *
096 * @author Rosanna Lee
097 * @author Rob Weltman
098 */
099 public abstract interface SaslClient {
100
101 /**
102 * Returns the IANA-registered mechanism name of this SASL client.
103 * (e.g. "CRAM-MD5", "GSSAPI").
104 * @return A non-null string representing the IANA-registered mechanism name.
105 */
106 public abstract String getMechanismName();
107
108 /**
109 * Determines whether this mechanism has an optional initial response.
110 * If true, caller should call <tt>evaluateChallenge()</tt> with an
111 * empty array to get the initial response.
112 *
113 * @return true if this mechanism has an initial response.
114 */
115 public abstract boolean hasInitialResponse();
116
117 /**
118 * Evaluates the challenge data and generates a response.
119 * If a challenge is received from the server during the authentication
120 * process, this method is called to prepare an appropriate next
121 * response to submit to the server.
122 *
123 * @param challenge The non-null challenge sent from the server.
124 * The challenge array may have zero length.
125 *
126 * @return The possibly null reponse to send to the server.
127 * It is null if the challenge accompanied a "SUCCESS" status and the challenge
128 * only contains data for the client to update its state and no response
129 * needs to be sent to the server. The response is a zero-length byte
130 * array if the client is to send a response with no data.
131 * @exception SaslException If an error occurred while processing
132 * the challenge or generating a response.
133 */
134 public abstract byte[] evaluateChallenge(byte[] challenge)
135 throws SaslException;
136
137 /**
138 * Determines whether the authentication exchange has completed.
139 * This method may be called at any time, but typically, it
140 * will not be called until the caller has received indication
141 * from the server
142 * (in a protocol-specific manner) that the exchange has completed.
143 *
144 * @return true if the authentication exchange has completed; false otherwise.
145 */
146 public abstract boolean isComplete();
147
148 /**
149 * Unwraps a byte array received from the server.
150 * This method can be called only after the authentication exchange has
151 * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
152 * the authentication exchange has negotiated integrity and/or privacy
153 * as the quality of protection; otherwise, an
154 * <tt>IllegalStateException</tt> is thrown.
155 *<p>
156 * <tt>incoming</tt> is the contents of the SASL buffer as defined in RFC 2222
157 * without the leading four octet field that represents the length.
158 * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>incoming</tt>
159 * to use.
160 *
161 * @param incoming A non-null byte array containing the encoded bytes
162 * from the server.
163 * @param offset The starting position at <tt>incoming</tt> of the bytes to use.
164 * @param len The number of bytes from <tt>incoming</tt> to use.
165 * @return A non-null byte array containing the decoded bytes.
166 * @exception SaslException if <tt>incoming</tt> cannot be successfully
167 * unwrapped.
168 * @exception IllegalStateException if the authentication exchange has
169 * not completed, or if the negotiated quality of protection
170 * has neither integrity nor privacy.
171 */
172 public abstract byte[] unwrap(byte[] incoming, int offset, int len)
173 throws SaslException;
174
175 /**
176 * Wraps a byte array to be sent to the server.
177 * This method can be called only after the authentication exchange has
178 * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
179 * the authentication exchange has negotiated integrity and/or privacy
180 * as the quality of protection; otherwise, an
181 * <tt>IllegalStateException</tt> is thrown.
182 *<p>
183 * The result of this method will make up the contents of the SASL buffer
184 * as defined in RFC 2222 without the leading four octet field that
185 * represents the length.
186 * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>outgoing</tt>
187 * to use.
188 *
189 * @param outgoing A non-null byte array containing the bytes to encode.
190 * @param offset The starting position at <tt>outgoing</tt> of the bytes to use.
191 * @param len The number of bytes from <tt>outgoing</tt> to use.
192 * @return A non-null byte array containing the encoded bytes.
193 * @exception SaslException if <tt>outgoing</tt> cannot be successfully
194 * wrapped.
195 * @exception IllegalStateException if the authentication exchange has
196 * not completed, or if the negotiated quality of protection
197 * has neither integrity nor privacy.
198 */
199 public abstract byte[] wrap(byte[] outgoing, int offset, int len)
200 throws SaslException;
201
202 /**
203 * Retrieves the negotiated property.
204 * This method can be called only after the authentication exchange has
205 * completed (i.e., when <tt>isComplete()</tt> returns true); otherwise, an
206 * <tt>IllegalStateException</tt> is thrown.
207 *
208 * @param propName The non-null property name.
209 * @return The value of the negotiated property. If null, the property was
210 * not negotiated or is not applicable to this mechanism.
211 * @exception IllegalStateException if this authentication exchange
212 * has not completed
213 */
214
215 public abstract Object getNegotiatedProperty(String propName);
216
217 /**
218 * Disposes of any system resources or security-sensitive information
219 * the SaslClient might be using. Invoking this method invalidates
220 * the SaslClient instance. This method is idempotent.
221 * @throws SaslException If a problem was encountered while disposing
222 * the resources.
223 */
224 public abstract void dispose() throws SaslException;
225 }
|