src/java.naming/share/classes/javax/naming/ldap/StartTlsResponse.java
author stefank
Mon, 25 Nov 2019 15:00:32 +0100
changeset 59255 a74627659f96
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234602: ZGC: Windows compile error in ZHeuristic Reviewed-by: pliden, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.naming.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.net.ssl.SSLSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.net.ssl.SSLSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.net.ssl.HostnameVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements the LDAPv3 Extended Response for StartTLS as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <a href="http://www.ietf.org/rfc/rfc2830.txt">Lightweight Directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Access Protocol (v3): Extension for Transport Layer Security</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The object identifier for StartTLS is 1.3.6.1.4.1.1466.20037
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and no extended response value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The Start TLS extended request and response are used to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * a TLS connection over the existing LDAP connection associated with
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
    45
 * the JNDI context on which {@code extendedOperation()} is invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Typically, a JNDI program uses the StartTLS extended request and response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * classes as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * // Open an LDAP association
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * LdapContext ctx = new InitialLdapContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * // Perform a StartTLS extended operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * StartTlsResponse tls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * // Open a TLS connection (over the existing LDAP association) and get details
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * // of the negotiated TLS session: cipher suite, peer certificate, ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * SSLSession session = tls.negotiate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * // ... use ctx to perform protected LDAP operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * // Close the TLS connection (revert back to the underlying LDAP association)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * tls.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * // ... use ctx to perform unprotected LDAP operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * // Close the LDAP association
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * ctx.close;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see StartTlsRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public abstract class StartTlsResponse implements ExtendedResponse {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The StartTLS extended response's assigned object identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * is 1.3.6.1.4.1.1466.20037.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final String OID = "1.3.6.1.4.1.1466.20037";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Called by subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Constructs a StartTLS extended response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * A concrete subclass must have a public no-arg constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected StartTlsResponse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // ExtendedResponse methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Retrieves the StartTLS response's object identifier string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return The object identifier string, "1.3.6.1.4.1.1466.20037".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public String getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Retrieves the StartTLS response's ASN.1 BER encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Since the response has no defined value, null is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return The null value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public byte[] getEncodedValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // StartTls-specific methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Overrides the default list of cipher suites enabled for use on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * TLS connection. The cipher suites must have already been listed by
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   125
     * {@code SSLSocketFactory.getSupportedCipherSuites()} as being supported.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Even if a suite has been enabled, it still might not be used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the peer does not support it, or because the requisite certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * (and private keys) are not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param suites The non-null list of names of all the cipher suites to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see #negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public abstract void setEnabledCipherSuites(String[] suites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   137
     * Sets the hostname verifier used by {@code negotiate()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * after the TLS handshake has completed and the default hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * verification has failed.
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   140
     * {@code setHostnameVerifier()} must be called before
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   141
     * {@code negotiate()} is invoked for it to have effect.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * If called after
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   143
     * {@code negotiate()}, this method does not do anything.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param verifier The non-null hostname verifier callback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see #negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public abstract void setHostnameVerifier(HostnameVerifier verifier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Negotiates a TLS session using the default SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   153
     * This method is equivalent to {@code negotiate(null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return The negotiated SSL session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws IOException If an IO error was encountered while establishing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * the TLS session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see #setEnabledCipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @see #setHostnameVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public abstract SSLSession negotiate() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Negotiates a TLS session using an SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Creates an SSL socket using the supplied SSL socket factory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * attaches it to the existing connection. Performs the TLS handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * and returns the negotiated session information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   170
     * If cipher suites have been set via {@code setEnabledCipherSuites}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * then they are enabled before the TLS handshake begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Hostname verification is performed after the TLS handshake completes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The default hostname verification performs a match of the server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * hostname against the hostname information found in the server's certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * If this verification fails and no callback has been set via
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   177
     * {@code setHostnameVerifier} then the negotiation fails.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * If this verification fails and a callback has been set via
32029
a5538163e144 8132877: docs: replace <tt> tags (obsolete in html5) for javax.naming
avstepan
parents: 25859
diff changeset
   179
     * {@code setHostnameVerifier}, then the callback is used to determine whether
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * the negotiation succeeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * If an error occurs then the SSL socket is closed and an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * is thrown. The underlying connection remains intact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param factory The possibly null SSL socket factory to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * If null, the default SSL socket factory is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return The negotiated SSL session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @throws IOException If an IO error was encountered while establishing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * the TLS session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see #setEnabledCipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see #setHostnameVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public abstract SSLSession negotiate(SSLSocketFactory factory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Closes the TLS connection gracefully and reverts back to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws IOException If an IO error was encountered while closing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * TLS connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public abstract void close() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private static final long serialVersionUID = 8372842182579276418L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}