0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
2362N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage javax.naming.ldap;
0N/A
0N/Aimport java.util.Iterator;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport javax.naming.ConfigurationException;
4378N/Aimport javax.naming.NamingException;
4378N/Aimport com.sun.naming.internal.VersionHelper;
4378N/Aimport java.util.ServiceLoader;
4378N/Aimport java.util.ServiceConfigurationError;
4378N/A
4378N/A/**
4378N/A * This class implements the LDAPv3 Extended Request for StartTLS as
4378N/A * defined in
4378N/A * <a href="http://www.ietf.org/rfc/rfc2830.txt">Lightweight Directory
0N/A * Access Protocol (v3): Extension for Transport Layer Security</a>
0N/A *
0N/A * The object identifier for StartTLS is 1.3.6.1.4.1.1466.20037
0N/A * and no extended request value is defined.
0N/A *<p>
0N/A * <tt>StartTlsRequest</tt>/<tt>StartTlsResponse</tt> are used to establish
0N/A * a TLS connection over the existing LDAP connection associated with
0N/A * the JNDI context on which <tt>extendedOperation()</tt> is invoked.
0N/A * Typically, a JNDI program uses these classes as follows.
0N/A * <blockquote><pre>
0N/A * import javax.naming.ldap.*;
0N/A *
0N/A * // Open an LDAP association
0N/A * LdapContext ctx = new InitialLdapContext();
0N/A *
0N/A * // Perform a StartTLS extended operation
0N/A * StartTlsResponse tls =
0N/A * (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
0N/A *
0N/A * // Open a TLS connection (over the existing LDAP association) and get details
0N/A * // of the negotiated TLS session: cipher suite, peer certificate, etc.
0N/A * SSLSession session = tls.negotiate();
0N/A *
0N/A * // ... use ctx to perform protected LDAP operations
0N/A *
0N/A * // Close the TLS connection (revert back to the underlying LDAP association)
0N/A * tls.close();
0N/A *
0N/A * // ... use ctx to perform unprotected LDAP operations
0N/A *
0N/A * // Close the LDAP association
0N/A * ctx.close;
0N/A * </pre></blockquote>
0N/A *
0N/A * @since 1.4
0N/A * @see StartTlsResponse
0N/A * @author Vincent Ryan
0N/A */
0N/Apublic class StartTlsRequest implements ExtendedRequest {
0N/A
0N/A // Constant
0N/A
0N/A /**
0N/A * The StartTLS extended request's assigned object identifier
0N/A * is 1.3.6.1.4.1.1466.20037.
0N/A */
0N/A public static final String OID = "1.3.6.1.4.1.1466.20037";
0N/A
0N/A
0N/A // Constructors
0N/A
0N/A /**
0N/A * Constructs a StartTLS extended request.
0N/A */
0N/A public StartTlsRequest() {
0N/A }
0N/A
0N/A
0N/A // ExtendedRequest methods
0N/A
0N/A /**
0N/A * Retrieves the StartTLS request's object identifier string.
0N/A *
0N/A * @return The object identifier string, "1.3.6.1.4.1.1466.20037".
0N/A */
0N/A public String getID() {
0N/A return OID;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the StartTLS request's ASN.1 BER encoded value.
0N/A * Since the request has no defined value, null is always
0N/A * returned.
0N/A *
0N/A * @return The null value.
0N/A */
0N/A public byte[] getEncodedValue() {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Creates an extended response object that corresponds to the
0N/A * LDAP StartTLS extended request.
0N/A * <p>
0N/A * The result must be a concrete subclass of StartTlsResponse
0N/A * and must have a public zero-argument constructor.
0N/A * <p>
0N/A * This method locates the implementation class by locating
0N/A * configuration files that have the name:
0N/A * <blockquote><tt>
0N/A * META-INF/services/javax.naming.ldap.StartTlsResponse
0N/A * </tt></blockquote>
0N/A * The configuration files and their corresponding implementation classes must
0N/A * be accessible to the calling thread's context class loader.
0N/A * <p>
0N/A * Each configuration file should contain a list of fully-qualified class
0N/A * names, one per line. Space and tab characters surrounding each name, as
0N/A * well as blank lines, are ignored. The comment character is <tt>'#'</tt>
0N/A * (<tt>0x23</tt>); on each line all characters following the first comment
0N/A * character are ignored. The file must be encoded in UTF-8.
0N/A * <p>
0N/A * This method will return an instance of the first implementation
0N/A * class that it is able to load and instantiate successfully from
0N/A * the list of class names collected from the configuration files.
0N/A * This method uses the calling thread's context classloader to find the
0N/A * configuration files and to load the implementation class.
0N/A * <p>
0N/A * If no class can be found in this way, this method will use
0N/A * an implementation-specific way to locate an implementation.
0N/A * If none is found, a NamingException is thrown.
0N/A *
0N/A * @param id The object identifier of the extended response.
0N/A * Its value must be "1.3.6.1.4.1.1466.20037" or null.
0N/A * Both values are equivalent.
0N/A * @param berValue The possibly null ASN.1 BER encoded value of the
0N/A * extended response. This is the raw BER bytes
0N/A * including the tag and length of the response value.
0N/A * It does not include the response OID.
0N/A * Its value is ignored because a Start TLS response
0N/A * is not expected to contain any response value.
0N/A * @param offset The starting position in berValue of the bytes to use.
0N/A * Its value is ignored because a Start TLS response
0N/A * is not expected to contain any response value.
0N/A * @param length The number of bytes in berValue to use.
0N/A * Its value is ignored because a Start TLS response
0N/A * is not expected to contain any response value.
0N/A * @return The StartTLS extended response object.
0N/A * @exception NamingException If a naming exception was encountered
0N/A * while creating the StartTLS extended response object.
0N/A */
0N/A public ExtendedResponse createExtendedResponse(String id, byte[] berValue,
0N/A int offset, int length) throws NamingException {
0N/A
0N/A // Confirm that the object identifier is correct
0N/A if ((id != null) && (!id.equals(OID))) {
0N/A throw new ConfigurationException(
0N/A "Start TLS received the following response instead of " +
0N/A OID + ": " + id);
0N/A }
0N/A
0N/A StartTlsResponse resp = null;
0N/A
0N/A ServiceLoader<StartTlsResponse> sl = ServiceLoader.load(
0N/A StartTlsResponse.class, getContextClassLoader());
0N/A Iterator<StartTlsResponse> iter = sl.iterator();
0N/A
0N/A while (resp == null && privilegedHasNext(iter)) {
0N/A resp = iter.next();
0N/A }
0N/A if (resp != null) {
0N/A return resp;
0N/A }
0N/A try {
0N/A VersionHelper helper = VersionHelper.getVersionHelper();
0N/A Class clas = helper.loadClass(
0N/A "com.sun.jndi.ldap.ext.StartTlsResponseImpl");
0N/A
0N/A resp = (StartTlsResponse) clas.newInstance();
0N/A
0N/A } catch (IllegalAccessException e) {
0N/A throw wrapException(e);
0N/A
0N/A } catch (InstantiationException e) {
0N/A throw wrapException(e);
0N/A
0N/A } catch (ClassNotFoundException e) {
0N/A throw wrapException(e);
0N/A }
0N/A
0N/A return resp;
0N/A }
0N/A
0N/A /*
0N/A * Wrap an exception, thrown while attempting to load the StartTlsResponse
0N/A * class, in a configuration exception.
0N/A */
0N/A private ConfigurationException wrapException(Exception e) {
0N/A ConfigurationException ce = new ConfigurationException(
0N/A "Cannot load implementation of javax.naming.ldap.StartTlsResponse");
0N/A
0N/A ce.setRootCause(e);
0N/A return ce;
0N/A }
0N/A
0N/A /*
0N/A * Acquire the class loader associated with this thread.
0N/A */
0N/A private final ClassLoader getContextClassLoader() {
0N/A return (ClassLoader) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A return Thread.currentThread().getContextClassLoader();
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A private final static boolean privilegedHasNext(final Iterator iter) {
0N/A Boolean answer = (Boolean) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A return Boolean.valueOf(iter.hasNext());
0N/A }
0N/A });
0N/A return answer.booleanValue();
0N/A }
0N/A
0N/A private static final long serialVersionUID = 4441679576360753397L;
0N/A}
0N/A