src/java.naming/share/classes/com/sun/jndi/ldap/ext/StartTlsResponseImpl.java
author weijun
Thu, 07 Jun 2018 23:53:56 +0800
branchJDK-8145252-TLS13-branch
changeset 56693 64aa781522be
parent 47216 71c04702a3d5
child 53237 443abf0dc2ed
permissions -rw-r--r--
another KRB5 cleanup in java.naming
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17483
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 4236
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: 4236
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: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
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 com.sun.jndi.ldap.ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.net.ssl.SSLSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.net.ssl.SSLSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.SSLSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.SSLPeerUnverifiedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.HostnameVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.util.HostnameChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.jndi.ldap.Connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class implements the LDAPv3 Extended Response for StartTLS as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <a href="http://www.ietf.org/rfc/rfc2830.txt">Lightweight Directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Access Protocol (v3): Extension for Transport Layer Security</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The object identifier for StartTLS is 1.3.6.1.4.1.1466.20037
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * and no extended response value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    55
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The Start TLS extended request and response are used to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * a TLS connection over the existing LDAP connection associated with
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    58
 * the JNDI context on which {@code extendedOperation()} is invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see StartTlsRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
final public class StartTlsResponseImpl extends StartTlsResponse {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The dNSName type in a subjectAltName extension of an X.509 certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int DNSNAME_TYPE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The server's hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private transient String hostname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The LDAP socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private transient Connection ldapConnection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The original input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private transient InputStream originalInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The original output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private transient OutputStream originalOutputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The SSL socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private transient SSLSocket sslSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The SSL socket factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private transient SSLSocketFactory defaultFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private transient SSLSocketFactory currentFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The list of cipher suites to be enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private transient String[] suites = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The hostname verifier callback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private transient HostnameVerifier verifier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The flag to indicate that the TLS connection is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private transient boolean isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final long serialVersionUID = -1126624615143411328L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // public no-arg constructor required by JDK's Service Provider API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public StartTlsResponseImpl() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Overrides the default list of cipher suites enabled for use on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * TLS connection. The cipher suites must have already been listed by
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   127
     * {@code SSLSocketFactory.getSupportedCipherSuites()} as being supported.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Even if a suite has been enabled, it still might not be used because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * the peer does not support it, or because the requisite certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * (and private keys) are not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param suites The non-null list of names of all the cipher suites to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see #negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void setEnabledCipherSuites(String[] suites) {
17483
ed57f2f26fb6 8010814: More buffers are stored or returned without cloning
xuelei
parents: 10324
diff changeset
   137
        // The impl does accept null suites, although the spec requires
ed57f2f26fb6 8010814: More buffers are stored or returned without cloning
xuelei
parents: 10324
diff changeset
   138
        // a non-null list.
ed57f2f26fb6 8010814: More buffers are stored or returned without cloning
xuelei
parents: 10324
diff changeset
   139
        this.suites = suites == null ? null : suites.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   143
     * Overrides the default hostname verifier used by {@code negotiate()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * after the TLS handshake has completed. If
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   145
     * {@code setHostnameVerifier()} has not been called before
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   146
     * {@code negotiate()} is invoked, {@code negotiate()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * will perform a simple case ignore match. If called after
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   148
     * {@code negotiate()}, this method does not do anything.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param verifier The non-null hostname verifier callback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void setHostnameVerifier(HostnameVerifier verifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.verifier = verifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Negotiates a TLS session using the default SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   160
     * This method is equivalent to {@code negotiate(null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return The negotiated SSL session
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   163
     * @throws IOException If an IO error was encountered while establishing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * the TLS session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see #setEnabledCipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see #setHostnameVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public SSLSession negotiate() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return negotiate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Negotiates a TLS session using an SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Creates an SSL socket using the supplied SSL socket factory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * attaches it to the existing connection. Performs the TLS handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * and returns the negotiated session information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   180
     * If cipher suites have been set via {@code setEnabledCipherSuites}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * then they are enabled before the TLS handshake begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Hostname verification is performed after the TLS handshake completes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * The default check performs a case insensitive match of the server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * hostname against that in the server's certificate. The server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * hostname is extracted from the subjectAltName in the server's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * certificate (if present). Otherwise the value of the common name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * attribute of the subject name is used. If a callback has
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   189
     * been set via {@code setHostnameVerifier} then that verifier is used if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * the default check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * If an error occurs then the SSL socket is closed and an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * is thrown. The underlying connection remains intact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param factory The possibly null SSL socket factory to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * If null, the default SSL socket factory is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return The negotiated SSL session
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   198
     * @throws IOException If an IO error was encountered while establishing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * the TLS session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see #setEnabledCipherSuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @see #setHostnameVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public SSLSession negotiate(SSLSocketFactory factory) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (isClosed && sslSocket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new IOException("TLS connection is closed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (factory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            factory = getDefaultFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            System.out.println("StartTLS: About to start handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        SSLSession sslSession = startHandshake(factory).getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            System.out.println("StartTLS: Completed handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        SSLPeerUnverifiedException verifExcep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (verify(hostname, sslSession)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                isClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                return sslSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // Save to return the cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            verifExcep = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if ((verifier != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                verifier.verify(hostname, sslSession)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            isClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return sslSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Verification failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        sslSession.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (verifExcep == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            verifExcep = new SSLPeerUnverifiedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        "hostname of the server '" + hostname +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        "' does not match the hostname in the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        "server's certificate.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        throw verifExcep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Closes the TLS connection gracefully and reverts back to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   255
     * @throws IOException If an IO error was encountered while closing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * TLS connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            System.out.println("StartTLS: replacing SSL " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                "streams with originals");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Replace SSL streams with the original streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        ldapConnection.replaceStreams(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        originalInputStream, originalOutputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            System.out.println("StartTLS: closing SSL Socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Sets the connection for TLS to use. The TLS connection will be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * to this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param ldapConnection The non-null connection to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param hostname The server's hostname. If null, the hostname used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * open the connection will be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public void setConnection(Connection ldapConnection, String hostname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        this.ldapConnection = ldapConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        this.hostname = (hostname != null) ? hostname : ldapConnection.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        originalInputStream = ldapConnection.inStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        originalOutputStream = ldapConnection.outStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns the default SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return The default SSL socket factory.
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 30655
diff changeset
   300
     * @throws IOException If TLS is not supported.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private SSLSocketFactory getDefaultFactory() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (defaultFactory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return defaultFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return (defaultFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            (SSLSocketFactory) SSLSocketFactory.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Start the TLS handshake and manipulate the input and output streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param factory The SSL socket factory to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return The SSL socket.
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 30655
diff changeset
   317
     * @throws IOException If an exception occurred while performing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * TLS handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private SSLSocket startHandshake(SSLSocketFactory factory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (ldapConnection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new IllegalStateException("LDAP connection has not been set."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                + " TLS requires an existing LDAP connection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (factory != currentFactory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // Create SSL socket layered over the existing connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            sslSocket = (SSLSocket) factory.createSocket(ldapConnection.sock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                ldapConnection.host, ldapConnection.port, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            currentFactory = factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                System.out.println("StartTLS: Created socket : " + sslSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (suites != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            sslSocket.setEnabledCipherSuites(suites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                System.out.println("StartTLS: Enabled cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // Connection must be quite for handshake to proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        "StartTLS: Calling sslSocket.startHandshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            sslSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        "StartTLS: + Finished sslSocket.startHandshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // Replace original streams with the new SSL streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            ldapConnection.replaceStreams(sslSocket.getInputStream(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                sslSocket.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                System.out.println("StartTLS: Replaced IO Streams");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                System.out.println("StartTLS: Got IO error during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw e;   // pass up exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return sslSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Verifies that the hostname in the server's certificate matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * hostname of the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * The server's first certificate is examined. If it has a subjectAltName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * that contains a dNSName then that is used as the server's hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * The server's hostname may contain a wildcard for its left-most name part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Otherwise, if the certificate has no subjectAltName then the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * the common name attribute of the subject name is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param hostname The hostname of the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param session the SSLSession used on the connection to host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return true if the hostname is verified, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private boolean verify(String hostname, SSLSession session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        throws SSLPeerUnverifiedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        java.security.cert.Certificate[] certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // if IPv6 strip off the "[]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (hostname != null && hostname.startsWith("[") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                hostname.endsWith("]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            hostname = hostname.substring(1, hostname.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            HostnameChecker checker = HostnameChecker.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                                HostnameChecker.TYPE_LDAP);
56693
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   407
            // get the subject's certificate
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   408
            certs = session.getPeerCertificates();
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   409
            X509Certificate peerCert;
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   410
            if (certs[0] instanceof java.security.cert.X509Certificate) {
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   411
                peerCert = (java.security.cert.X509Certificate) certs[0];
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   412
            } else {
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   413
                throw new SSLPeerUnverifiedException(
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   414
                        "Received a non X509Certificate from the server");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
56693
64aa781522be another KRB5 cleanup in java.naming
weijun
parents: 47216
diff changeset
   416
            checker.match(hostname, peerCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // no exception means verification passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } catch (SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
             * The application may enable an anonymous SSL cipher suite, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
             * hostname verification is not done for anonymous ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            String cipher = session.getCipherSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (cipher != null && (cipher.indexOf("_anon_") != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
             * Pass up the cause of the failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            throw(SSLPeerUnverifiedException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                new SSLPeerUnverifiedException("hostname of the server '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                hostname +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                "' does not match the hostname in the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                "server's certificate.").initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Get the peer principal from the session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private static Principal getPeerPrincipal(SSLSession session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throws SSLPeerUnverifiedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            principal = session.getPeerPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (AbstractMethodError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            // if the JSSE provider does not support it, return null, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // we need it only for Kerberos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}