Path: blob/master/src/java.naming/share/classes/javax/naming/ldap/StartTlsResponse.java
41159 views
/*1* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming.ldap;2627import java.io.IOException;28import javax.net.ssl.SSLSession;29import javax.net.ssl.SSLSocketFactory;30import javax.net.ssl.HostnameVerifier;3132/**33* This class implements the LDAPv3 Extended Response for StartTLS as34* defined in35* <a href="http://www.ietf.org/rfc/rfc2830.txt">Lightweight Directory36* Access Protocol (v3): Extension for Transport Layer Security</a>37*38* The object identifier for StartTLS is 1.3.6.1.4.1.1466.2003739* and no extended response value is defined.40*41*<p>42* The Start TLS extended request and response are used to establish43* a TLS connection over the existing LDAP connection associated with44* the JNDI context on which {@code extendedOperation()} is invoked.45* Typically, a JNDI program uses the StartTLS extended request and response46* classes as follows.47* <blockquote><pre>48* import javax.naming.ldap.*;49*50* // Open an LDAP association51* LdapContext ctx = new InitialLdapContext();52*53* // Perform a StartTLS extended operation54* StartTlsResponse tls =55* (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());56*57* // Open a TLS connection (over the existing LDAP association) and get details58* // of the negotiated TLS session: cipher suite, peer certificate, ...59* SSLSession session = tls.negotiate();60*61* // ... use ctx to perform protected LDAP operations62*63* // Close the TLS connection (revert back to the underlying LDAP association)64* tls.close();65*66* // ... use ctx to perform unprotected LDAP operations67*68* // Close the LDAP association69* ctx.close;70* </pre></blockquote>71*72* @since 1.473* @see StartTlsRequest74* @author Vincent Ryan75*/76public abstract class StartTlsResponse implements ExtendedResponse {7778// Constant7980/**81* The StartTLS extended response's assigned object identifier82* is 1.3.6.1.4.1.1466.20037.83*/84public static final String OID = "1.3.6.1.4.1.1466.20037";858687// Called by subclass8889/**90* Constructs a StartTLS extended response.91* A concrete subclass must have a public no-arg constructor.92*/93protected StartTlsResponse() {94}959697// ExtendedResponse methods9899/**100* Retrieves the StartTLS response's object identifier string.101*102* @return The object identifier string, "1.3.6.1.4.1.1466.20037".103*/104public String getID() {105return OID;106}107108/**109* Retrieves the StartTLS response's ASN.1 BER encoded value.110* Since the response has no defined value, null is always111* returned.112*113* @return The null value.114*/115public byte[] getEncodedValue() {116return null;117}118119// StartTls-specific methods120121/**122* Overrides the default list of cipher suites enabled for use on the123* TLS connection. The cipher suites must have already been listed by124* {@code SSLSocketFactory.getSupportedCipherSuites()} as being supported.125* Even if a suite has been enabled, it still might not be used because126* the peer does not support it, or because the requisite certificates127* (and private keys) are not available.128*129* @param suites The non-null list of names of all the cipher suites to130* enable.131* @see #negotiate132*/133public abstract void setEnabledCipherSuites(String[] suites);134135/**136* Sets the hostname verifier used by {@code negotiate()}137* after the TLS handshake has completed and the default hostname138* verification has failed.139* {@code setHostnameVerifier()} must be called before140* {@code negotiate()} is invoked for it to have effect.141* If called after142* {@code negotiate()}, this method does not do anything.143*144* @param verifier The non-null hostname verifier callback.145* @see #negotiate146*/147public abstract void setHostnameVerifier(HostnameVerifier verifier);148149/**150* Negotiates a TLS session using the default SSL socket factory.151* <p>152* This method is equivalent to {@code negotiate(null)}.153*154* @return The negotiated SSL session155* @throws IOException If an IO error was encountered while establishing156* the TLS session.157* @see #setEnabledCipherSuites158* @see #setHostnameVerifier159*/160public abstract SSLSession negotiate() throws IOException;161162/**163* Negotiates a TLS session using an SSL socket factory.164* <p>165* Creates an SSL socket using the supplied SSL socket factory and166* attaches it to the existing connection. Performs the TLS handshake167* and returns the negotiated session information.168* <p>169* If cipher suites have been set via {@code setEnabledCipherSuites}170* then they are enabled before the TLS handshake begins.171* <p>172* Hostname verification is performed after the TLS handshake completes.173* The default hostname verification performs a match of the server's174* hostname against the hostname information found in the server's certificate.175* If this verification fails and no callback has been set via176* {@code setHostnameVerifier} then the negotiation fails.177* If this verification fails and a callback has been set via178* {@code setHostnameVerifier}, then the callback is used to determine whether179* the negotiation succeeds.180* <p>181* If an error occurs then the SSL socket is closed and an IOException182* is thrown. The underlying connection remains intact.183*184* @param factory The possibly null SSL socket factory to use.185* If null, the default SSL socket factory is used.186* @return The negotiated SSL session187* @throws IOException If an IO error was encountered while establishing188* the TLS session.189* @see #setEnabledCipherSuites190* @see #setHostnameVerifier191*/192public abstract SSLSession negotiate(SSLSocketFactory factory)193throws IOException;194195/**196* Closes the TLS connection gracefully and reverts back to the underlying197* connection.198*199* @throws IOException If an IO error was encountered while closing the200* TLS connection201*/202public abstract void close() throws IOException;203204private static final long serialVersionUID = 8372842182579276418L;205}206207208