1. /*
  2. * @(#)StartTlsResponse.java 1.20 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming.ldap;
  8. import java.io.IOException;
  9. import javax.net.ssl.SSLSession;
  10. import javax.net.ssl.SSLSocketFactory;
  11. import javax.net.ssl.HostnameVerifier;
  12. /**
  13. * This class implements the LDAPv3 Extended Response for StartTLS as
  14. * defined in
  15. * <a href="http://www.ietf.org/rfc/rfc2830.txt">Lightweight Directory
  16. * Access Protocol (v3): Extension for Transport Layer Security</a>
  17. *
  18. * The object identifier for StartTLS is 1.3.6.1.4.1.1466.20037
  19. * and no extended response value is defined.
  20. *
  21. *<p>
  22. * The Start TLS extended request and response are used to establish
  23. * a TLS connection over the existing LDAP connection associated with
  24. * the JNDI context on which <tt>extendedOperation()</tt> is invoked.
  25. * Typically, a JNDI program uses the StartTLS extended request and response
  26. * classes as follows.
  27. * <blockquote><pre>
  28. * import javax.naming.ldap.*;
  29. *
  30. * // Open an LDAP association
  31. * LdapContext ctx = new InitialLdapContext();
  32. *
  33. * // Perform a StartTLS extended operation
  34. * StartTlsResponse tls =
  35. * (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
  36. *
  37. * // Open a TLS connection (over the existing LDAP association) and get details
  38. * // of the negotiated TLS session: cipher suite, peer certificate, ...
  39. * SSLSession session = tls.negotiate();
  40. *
  41. * // ... use ctx to perform protected LDAP operations
  42. *
  43. * // Close the TLS connection (revert back to the underlying LDAP association)
  44. * tls.close();
  45. *
  46. * // ... use ctx to perform unprotected LDAP operations
  47. *
  48. * // Close the LDAP association
  49. * ctx.close;
  50. * </pre></blockquote>
  51. *
  52. * @since 1.4
  53. * @see StartTlsRequest
  54. * @author Vincent Ryan
  55. */
  56. public abstract class StartTlsResponse implements ExtendedResponse {
  57. // Constant
  58. /**
  59. * The StartTLS extended response's assigned object identifier
  60. * is 1.3.6.1.4.1.1466.20037.
  61. */
  62. public static final String OID = "1.3.6.1.4.1.1466.20037";
  63. // Called by subclass
  64. /**
  65. * Constructs a StartTLS extended response.
  66. * A concrete subclass must have a public no-arg constructor.
  67. */
  68. protected StartTlsResponse() {
  69. }
  70. // ExtendedResponse methods
  71. /**
  72. * Retrieves the StartTLS response's object identifier string.
  73. *
  74. * @return The object identifier string, "1.3.6.1.4.1.1466.20037".
  75. */
  76. public String getID() {
  77. return OID;
  78. }
  79. /**
  80. * Retrieves the StartTLS response's ASN.1 BER encoded value.
  81. * Since the response has no defined value, null is always
  82. * returned.
  83. *
  84. * @return The null value.
  85. */
  86. public byte[] getEncodedValue() {
  87. return null;
  88. }
  89. // StartTls-specific methods
  90. /**
  91. * Overrides the default list of cipher suites enabled for use on the
  92. * TLS connection. The cipher suites must have already been listed by
  93. * <tt>SSLSocketFactory.getSupportedCipherSuites()</tt> as being supported.
  94. * Even if a suite has been enabled, it still might not be used because
  95. * the peer does not support it, or because the requisite certificates
  96. * (and private keys) are not available.
  97. *
  98. * @param suites The non-null list of names of all the cipher suites to
  99. * enable.
  100. * @see #negotiate
  101. */
  102. public abstract void setEnabledCipherSuites(String[] suites);
  103. /**
  104. * Sets the hostname verifier used by <tt>negotiate()</tt>
  105. * after the TLS handshake has completed and the default hostname
  106. * verification has failed.
  107. * <tt>setHostnameVerifier()</tt> must be called before
  108. * <tt>negotiate()</tt> is invoked for it to have effect.
  109. * If called after
  110. * <tt>negotiate()</tt>, this method does not do anything.
  111. *
  112. * @param verifier The non-null hostname verifier callback.
  113. * @see #negotiate
  114. */
  115. public abstract void setHostnameVerifier(HostnameVerifier verifier);
  116. /**
  117. * Negotiates a TLS session using the default SSL socket factory.
  118. * <p>
  119. * This method is equivalent to <tt>negotiate(null)</tt>.
  120. *
  121. * @return The negotiated SSL session
  122. * @throws IOException If an IO error was encountered while establishing
  123. * the TLS session.
  124. * @see #setEnabledCipherSuites
  125. * @see #setHostnameVerifier
  126. */
  127. public abstract SSLSession negotiate() throws IOException;
  128. /**
  129. * Negotiates a TLS session using an SSL socket factory.
  130. * <p>
  131. * Creates an SSL socket using the supplied SSL socket factory and
  132. * attaches it to the existing connection. Performs the TLS handshake
  133. * and returns the negotiated session information.
  134. * <p>
  135. * If cipher suites have been set via <tt>setEnabledCipherSuites</tt>
  136. * then they are enabled before the TLS handshake begins.
  137. * <p>
  138. * Hostname verification is performed after the TLS handshake completes.
  139. * The default hostname verification performs a match of the server's
  140. * hostname against the hostname information found in the server's certificate.
  141. * If this verification fails and no callback has been set via
  142. * <tt>setHostnameVerifier</tt> then the negotiation fails.
  143. * If this verification fails and a callback has been set via
  144. * <tt>setHostnameVerifier</tt>, then the callback is used to determine whether
  145. * the negotiation succeeds.
  146. * <p>
  147. * If an error occurs then the SSL socket is closed and an IOException
  148. * is thrown. The underlying connection remains intact.
  149. *
  150. * @param factory The possibly null SSL socket factory to use.
  151. * If null, the default SSL socket factory is used.
  152. * @return The negotiated SSL session
  153. * @throws IOException If an IO error was encountered while establishing
  154. * the TLS session.
  155. * @see #setEnabledCipherSuites
  156. * @see #setHostnameVerifier
  157. */
  158. public abstract SSLSession negotiate(SSLSocketFactory factory)
  159. throws IOException;
  160. /**
  161. * Closes the TLS connection gracefully and reverts back to the underlying
  162. * connection.
  163. *
  164. * @throws IOException If an IO error was encountered while closing the
  165. * TLS connection
  166. */
  167. public abstract void close() throws IOException;
  168. private static final long serialVersionUID = 8372842182579276418L;
  169. }