001 /*
002 * Copyright 2000-2004 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 server.
030 *<p>
031 * A server such an LDAP server gets an instance of this
032 * class in order to perform authentication defined by a specific SASL
033 * mechanism. Invoking methods on the <tt>SaslServer</tt> instance
034 * generates challenges according to the SASL
035 * mechanism implemented by the <tt>SaslServer</tt>.
036 * As the authentication proceeds, the instance
037 * encapsulates the state of a SASL server's authentication exchange.
038 *<p>
039 * Here's an example of how an LDAP server might use a <tt>SaslServer</tt>.
040 * It first gets an instance of a <tt>SaslServer</tt> for the SASL mechanism
041 * requested by the client:
042 *<blockquote><pre>
043 * SaslServer ss = Sasl.createSaslServer(mechanism,
044 * "ldap", myFQDN, props, callbackHandler);
045 *</pre></blockquote>
046 * It can then proceed to use the server for authentication.
047 * For example, suppose the LDAP server received an LDAP BIND request
048 * containing the name of the SASL mechanism and an (optional) initial
049 * response. It then might use the server as follows:
050 *<blockquote><pre>
051 * while (!ss.isComplete()) {
052 * try {
053 * byte[] challenge = ss.evaluateResponse(response);
054 * if (ss.isComplete()) {
055 * status = ldap.sendBindResponse(mechanism, challenge, SUCCESS);
056 * } else {
057 * status = ldap.sendBindResponse(mechanism, challenge,
058 SASL_BIND_IN_PROGRESS);
059 * response = ldap.readBindRequest();
060 * }
061 * } catch (SaslException e) {
062 * status = ldap.sendErrorResponse(e);
063 * break;
064 * }
065 * }
066 * if (ss.isComplete() && status == SUCCESS) {
067 * String qop = (String) sc.getNegotiatedProperty(Sasl.QOP);
068 * if (qop != null
069 * && (qop.equalsIgnoreCase("auth-int")
070 * || qop.equalsIgnoreCase("auth-conf"))) {
071 *
072 * // Use SaslServer.wrap() and SaslServer.unwrap() for future
073 * // communication with client
074 * ldap.in = new SecureInputStream(ss, ldap.in);
075 * ldap.out = new SecureOutputStream(ss, ldap.out);
076 * }
077 * }
078 *</pre></blockquote>
079 *
080 * @since 1.5
081 *
082 * @see Sasl
083 * @see SaslServerFactory
084 *
085 * @author Rosanna Lee
086 * @author Rob Weltman
087 */
088 public abstract interface SaslServer {
089
090 /**
091 * Returns the IANA-registered mechanism name of this SASL server.
092 * (e.g. "CRAM-MD5", "GSSAPI").
093 * @return A non-null string representing the IANA-registered mechanism name.
094 */
095 public abstract String getMechanismName();
096
097 /**
098 * Evaluates the response data and generates a challenge.
099 *
100 * If a response is received from the client during the authentication
101 * process, this method is called to prepare an appropriate next
102 * challenge to submit to the client. The challenge is null if the
103 * authentication has succeeded and no more challenge data is to be sent
104 * to the client. It is non-null if the authentication must be continued
105 * by sending a challenge to the client, or if the authentication has
106 * succeeded but challenge data needs to be processed by the client.
107 * <tt>isComplete()</tt> should be called
108 * after each call to <tt>evaluateResponse()</tt>,to determine if any further
109 * response is needed from the client.
110 *
111 * @param response The non-null (but possibly empty) response sent
112 * by the client.
113 *
114 * @return The possibly null challenge to send to the client.
115 * It is null if the authentication has succeeded and there is
116 * no more challenge data to be sent to the client.
117 * @exception SaslException If an error occurred while processing
118 * the response or generating a challenge.
119 */
120 public abstract byte[] evaluateResponse(byte[] response)
121 throws SaslException;
122
123 /**
124 * Determines whether the authentication exchange has completed.
125 * This method is typically called after each invocation of
126 * <tt>evaluateResponse()</tt> to determine whether the
127 * authentication has completed successfully or should be continued.
128 * @return true if the authentication exchange has completed; false otherwise.
129 */
130 public abstract boolean isComplete();
131
132 /**
133 * Reports the authorization ID in effect for the client of this
134 * session.
135 * This method can only be called if isComplete() returns true.
136 * @return The authorization ID of the client.
137 * @exception IllegalStateException if this authentication session has not completed
138 */
139 public String getAuthorizationID();
140
141 /**
142 * Unwraps a byte array received from the client.
143 * This method can be called only after the authentication exchange has
144 * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
145 * the authentication exchange has negotiated integrity and/or privacy
146 * as the quality of protection; otherwise,
147 * an <tt>IllegalStateException</tt> is thrown.
148 *<p>
149 * <tt>incoming</tt> is the contents of the SASL buffer as defined in RFC 2222
150 * without the leading four octet field that represents the length.
151 * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>incoming</tt>
152 * to use.
153 *
154 * @param incoming A non-null byte array containing the encoded bytes
155 * from the client.
156 * @param offset The starting position at <tt>incoming</tt> of the bytes to use.
157 * @param len The number of bytes from <tt>incoming</tt> to use.
158 * @return A non-null byte array containing the decoded bytes.
159 * @exception SaslException if <tt>incoming</tt> cannot be successfully
160 * unwrapped.
161 * @exception IllegalStateException if the authentication exchange has
162 * not completed, or if the negotiated quality of protection
163 * has neither integrity nor privacy
164 */
165 public abstract byte[] unwrap(byte[] incoming, int offset, int len)
166 throws SaslException;
167
168 /**
169 * Wraps a byte array to be sent to the client.
170 * This method can be called only after the authentication exchange has
171 * completed (i.e., when <tt>isComplete()</tt> returns true) and only if
172 * the authentication exchange has negotiated integrity and/or privacy
173 * as the quality of protection; otherwise, a <tt>SaslException</tt> is thrown.
174 *<p>
175 * The result of this method
176 * will make up the contents of the SASL buffer as defined in RFC 2222
177 * without the leading four octet field that represents the length.
178 * <tt>offset</tt> and <tt>len</tt> specify the portion of <tt>outgoing</tt>
179 * to use.
180 *
181 * @param outgoing A non-null byte array containing the bytes to encode.
182 * @param offset The starting position at <tt>outgoing</tt> of the bytes to use.
183 * @param len The number of bytes from <tt>outgoing</tt> to use.
184 * @return A non-null byte array containing the encoded bytes.
185 * @exception SaslException if <tt>outgoing</tt> cannot be successfully
186 * wrapped.
187 * @exception IllegalStateException if the authentication exchange has
188 * not completed, or if the negotiated quality of protection has
189 * neither integrity nor privacy.
190 */
191 public abstract byte[] wrap(byte[] outgoing, int offset, int len)
192 throws SaslException;
193
194 /**
195 * Retrieves the negotiated property.
196 * This method can be called only after the authentication exchange has
197 * completed (i.e., when <tt>isComplete()</tt> returns true); otherwise, an
198 * <tt>IllegalStateException</tt> is thrown.
199 *
200 * @param propName the property
201 * @return The value of the negotiated property. If null, the property was
202 * not negotiated or is not applicable to this mechanism.
203 * @exception IllegalStateException if this authentication exchange has not completed
204 */
205
206 public abstract Object getNegotiatedProperty(String propName);
207
208 /**
209 * Disposes of any system resources or security-sensitive information
210 * the SaslServer might be using. Invoking this method invalidates
211 * the SaslServer instance. This method is idempotent.
212 * @throws SaslException If a problem was encountered while disposing
213 * the resources.
214 */
215 public abstract void dispose() throws SaslException;
216 }
|