jdk/src/share/classes/sun/security/ssl/SSLSessionImpl.java
author xuelei
Mon, 13 May 2013 05:41:51 -0700
changeset 17458 4252f06a378f
parent 14664 e71aa0962e70
permissions -rw-r--r--
8005535: SSLSessionImpl should have protected finalize() Reviewed-by: weijun, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17458
4252f06a378f 8005535: SSLSessionImpl should have protected finalize()
xuelei
parents: 14664
diff changeset
     2
 * Copyright (c) 1996, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    33
import java.util.Collection;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
    34
import java.util.Collections;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
    35
import java.util.List;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
    36
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.net.ssl.SSLSessionContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.net.ssl.SSLSessionBindingListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.net.ssl.SSLSessionBindingEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.net.ssl.SSLPeerUnverifiedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.net.ssl.SSLPermission;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    51
import javax.net.ssl.ExtendedSSLSession;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
    52
import javax.net.ssl.SNIServerName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import static sun.security.ssl.CipherSuite.KeyExchange.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Implements the SSL session interface, and exposes the session context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * which is maintained by SSL servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <P> Servers have the ability to manage the sessions associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * their authentication context(s).  They can do this by enumerating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * IDs of the sessions which are cached, examining those sessions, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * perhaps invalidating a given session so that it can't be used again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * If servers do not explicitly manage the cache, sessions will linger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * until memory is low enough that the runtime environment purges cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * entries automatically to reclaim space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <P><em> The only reason this class is not package-private is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * there's no other public way to get at the server session context which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * is associated with any given authentication context. </em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    74
final class SSLSessionImpl extends ExtendedSSLSession {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * we only really need a single null session
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final SSLSessionImpl         nullSession = new SSLSessionImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // compression methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final byte           compression_null = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * The state of a single session, as described in section 7.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * of the SSLv3 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private final ProtocolVersion       protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private final SessionId             sessionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private X509Certificate[]   peerCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private byte                compressionMethod;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    92
    private CipherSuite         cipherSuite;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private SecretKey           masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Information not part of the SSLv3 protocol spec, but used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * to support session management policies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private final long          creationTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private long                lastUsedTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final String        host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final int           port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private SSLSessionContextImpl       context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int                 sessionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private boolean             invalidated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private X509Certificate[]   localCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private PrivateKey          localPrivateKey;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   108
    private String[]            localSupportedSignAlgs;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   109
    private String[]            peerSupportedSignAlgs;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   110
    private List<SNIServerName>    requestedServerNames;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Principals for non-certificate based cipher suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private Principal peerPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Principal localPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * We count session creations, eventually for statistical data but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * also since counters make shorter debugging IDs than the big ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * we use in the protocol for uniqueness-over-time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static volatile int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Use of session caches is globally enabled/disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static boolean      defaultRejoinable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Create a new non-rejoinable session, using the default (null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * cipher spec.  This constructor returns a session which could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * be used either by a client or by a server, as a connection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * first opened and before handshaking begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private SSLSessionImpl() {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   139
        this(ProtocolVersion.NONE, CipherSuite.C_NULL, null,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   140
            new SessionId(false, null), null, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Create a new session, using a given cipher spec.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * be rejoinable if session caching is enabled; the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * is intended mostly for use by serves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    SSLSessionImpl(ProtocolVersion protocolVersion, CipherSuite cipherSuite,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   149
            Collection<SignatureAndHashAlgorithm> algorithms,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            SecureRandom generator, String host, int port) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   151
        this(protocolVersion, cipherSuite, algorithms,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
             new SessionId(defaultRejoinable, generator), host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Record a new session, using a given cipher spec and session ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    SSLSessionImpl(ProtocolVersion protocolVersion, CipherSuite cipherSuite,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   159
            Collection<SignatureAndHashAlgorithm> algorithms,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            SessionId id, String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        sessionId = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        peerCerts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        compressionMethod = compression_null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.cipherSuite = cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        masterSecret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sessionCount = ++counter;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   170
        localSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   171
            SignatureAndHashAlgorithm.getAlgorithmNames(algorithms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (debug != null && Debug.isOn("session")) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   174
            System.out.println("%% Initialized:  " + this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    void setMasterSecret(SecretKey secret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (masterSecret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            masterSecret = secret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new RuntimeException("setMasterSecret() error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the master secret ... treat with extreme caution!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    SecretKey getMasterSecret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return masterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    void setPeerCertificates(X509Certificate[] peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (peerCerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            peerCerts = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    void setLocalCertificates(X509Certificate[] local) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        localCerts = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    void setLocalPrivateKey(PrivateKey privateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        localPrivateKey = privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   207
    void setPeerSupportedSignatureAlgorithms(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   208
            Collection<SignatureAndHashAlgorithm> algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   209
        peerSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   210
            SignatureAndHashAlgorithm.getAlgorithmNames(algorithms);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   211
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   212
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   213
    void setRequestedServerNames(List<SNIServerName> requestedServerNames) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   214
        this.requestedServerNames = new ArrayList<>(requestedServerNames);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   215
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Set the peer principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    void setPeerPrincipal(Principal principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (peerPrincipal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            peerPrincipal = principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Set the local principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    void setLocalPrincipal(Principal principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        localPrincipal = principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns true iff this session may be resumed ... sessions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * usually resumable.  Security policies may suggest otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * for example sessions that haven't been used for a while (say,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * a working day) won't be resumable, and sessions might have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * maximum lifetime in any case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    boolean isRejoinable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return sessionId != null && sessionId.length() != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            !invalidated && isLocalAuthenticationValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   245
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public synchronized boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return isRejoinable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Check if the authentication used when establishing this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * is still valid. Returns true if no authentication was used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    boolean isLocalAuthenticationValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (localPrivateKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                // if the private key is no longer valid, getAlgorithm()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                // should throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                // (e.g. Smartcard has been removed from the reader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                localPrivateKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns the ID for this session.  The ID is fixed for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * duration of the session; neither it, nor its value, changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   273
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public byte[] getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return sessionId.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * For server sessions, this returns the set of sessions which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * are currently valid in this process.  For client sessions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * this returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   283
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public SSLSessionContext getSessionContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * An interim security policy until we can do something
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * more specific in 1.2. Only allow trusted code (code which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * can set system properties) to get an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * SSLSessionContext. This is to limit the ability of code to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * look up specific sessions or enumerate over them. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * code can only get session objects from successful SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * connections which implies that they must have had permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * to make the network connection in the first place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        SecurityManager sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if ((sm = System.getSecurityManager()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            sm.checkPermission(new SSLPermission("getSSLSessionContext"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    SessionId getSessionId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return sessionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns the cipher spec in use on this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    CipherSuite getSuite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   317
     * Resets the cipher spec in use on this session
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   318
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   319
    void setSuite(CipherSuite suite) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   320
       cipherSuite = suite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   321
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   322
       if (debug != null && Debug.isOn("session")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   323
           System.out.println("%% Negotiating:  " + this);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   324
       }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   325
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   326
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   327
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns the name of the cipher suite in use on this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   330
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public String getCipherSuite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return getSuite().name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    ProtocolVersion getProtocolVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns the standard name of the protocol in use on this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   342
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return getProtocolVersion().name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the compression technique used in this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    byte getCompression() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return compressionMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Returns the hashcode for this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   357
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return sessionId.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Returns true if sessions have same ids, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   366
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (obj instanceof SSLSessionImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            SSLSessionImpl sess = (SSLSessionImpl) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return (sessionId != null) && (sessionId.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        sess.getSessionId()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Return the cert chain presented by the peer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * java.security.cert format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Note: This method can be used only when using certificate-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * cipher suites; using it with non-certificate-based cipher suites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * such as Kerberos, will throw an SSLPeerUnverifiedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return array of peer X.509 certs, with the peer's own cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *  first in the chain, and with the "root" CA last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   393
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public java.security.cert.Certificate[] getPeerCertificates()
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
        // clone to preserve integrity of session ... caller can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // change record of peer identity even by accident, much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // less do it intentionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if ((cipherSuite.keyExchange == K_KRB5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            (cipherSuite.keyExchange == K_KRB5_EXPORT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throw new SSLPeerUnverifiedException("no certificates expected"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        + " for Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (peerCerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throw new SSLPeerUnverifiedException("peer not authenticated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Certs are immutable objects, therefore we don't clone them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // But do need to clone the array, so that nothing is inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // into peerCerts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return (java.security.cert.Certificate[])peerCerts.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Return the cert chain presented to the peer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * java.security.cert format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Note: This method is useful only when using certificate-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @return array of peer X.509 certs, with the peer's own cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *  first in the chain, and with the "root" CA last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   424
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public java.security.cert.Certificate[] getLocalCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // clone to preserve integrity of session ... caller can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // change record of peer identity even by accident, much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // less do it intentionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return (localCerts == null ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            (java.security.cert.Certificate[])localCerts.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Return the cert chain presented by the peer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * javax.security.cert format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Note: This method can be used only when using certificate-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * cipher suites; using it with non-certificate-based cipher suites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * such as Kerberos, will throw an SSLPeerUnverifiedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return array of peer X.509 certs, with the peer's own cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *  first in the chain, and with the "root" CA last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   444
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public javax.security.cert.X509Certificate[] getPeerCertificateChain()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            throws SSLPeerUnverifiedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // clone to preserve integrity of session ... caller can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // change record of peer identity even by accident, much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // less do it intentionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if ((cipherSuite.keyExchange == K_KRB5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            (cipherSuite.keyExchange == K_KRB5_EXPORT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            throw new SSLPeerUnverifiedException("no certificates expected"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        + " for Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (peerCerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throw new SSLPeerUnverifiedException("peer not authenticated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        javax.security.cert.X509Certificate[] certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        certs = new javax.security.cert.X509Certificate[peerCerts.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        for (int i = 0; i < peerCerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            byte[] der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                der = peerCerts[i].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                certs[i] = javax.security.cert.X509Certificate.getInstance(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            } catch (CertificateEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                throw new SSLPeerUnverifiedException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            } catch (javax.security.cert.CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                throw new SSLPeerUnverifiedException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Return the cert chain presented by the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Note: This method can be used only when using certificate-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * cipher suites; using it with non-certificate-based cipher suites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * such as Kerberos, will throw an SSLPeerUnverifiedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return array of peer X.509 certs, with the peer's own cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *  first in the chain, and with the "root" CA last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public X509Certificate[] getCertificateChain()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            throws SSLPeerUnverifiedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * clone to preserve integrity of session ... caller can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * change record of peer identity even by accident, much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * less do it intentionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if ((cipherSuite.keyExchange == K_KRB5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            (cipherSuite.keyExchange == K_KRB5_EXPORT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new SSLPeerUnverifiedException("no certificates expected"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        + " for Kerberos cipher suites");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (peerCerts != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   499
            return peerCerts.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new SSLPeerUnverifiedException("peer not authenticated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns the identity of the peer which was established as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * defining the session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return the peer's principal. Returns an X500Principal of the
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   510
     * end-entity certificate for X509-based cipher suites, and
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   511
     * Principal for Kerberos cipher suites.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @throws SSLPeerUnverifiedException if the peer's identity has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *          been verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   516
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public Principal getPeerPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                throws SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if ((cipherSuite.keyExchange == K_KRB5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            (cipherSuite.keyExchange == K_KRB5_EXPORT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (peerPrincipal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                throw new SSLPeerUnverifiedException("peer not authenticated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            } else {
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   525
                // Eliminate dependency on KerberosPrincipal
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   526
                return peerPrincipal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (peerCerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            throw new SSLPeerUnverifiedException("peer not authenticated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   532
        return peerCerts[0].getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Returns the principal that was sent to the peer during handshaking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @return the principal sent to the peer. Returns an X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * of the end-entity certificate for X509-based cipher suites, and
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   540
     * Principal for Kerberos cipher suites. If no principal was
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * sent, then null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   543
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public Principal getLocalPrincipal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if ((cipherSuite.keyExchange == K_KRB5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            (cipherSuite.keyExchange == K_KRB5_EXPORT)) {
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   548
                // Eliminate dependency on KerberosPrincipal
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 715
diff changeset
   549
                return (localPrincipal == null ? null : localPrincipal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return (localCerts == null ? null :
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   552
                localCerts[0].getSubjectX500Principal());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Returns the time this session was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   558
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public long getCreationTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return creationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Returns the last time this session was used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   567
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public long getLastAccessedTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return (lastUsedTime != 0) ? lastUsedTime : creationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    void setLastAccessedTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        lastUsedTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns the network address of the session's peer.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * implementation does not insist that connections between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * different ports on the same host must necessarily belong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * to different sessions, though that is of course allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public InetAddress getPeerAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return InetAddress.getByName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   591
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public String getPeerHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Need to provide the port info for caching sessions based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * host and port. Accessed by SSLSessionContextImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   600
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public int getPeerPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    void setContext(SSLSessionContextImpl ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            context = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Invalidate a session.  Active connections may still exist, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * no connections will be able to rejoin this session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   615
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    synchronized public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // Can't invalidate the NULL session -- this would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // attempted when we get a handshaking error on a brand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // new connection, with no "real" session yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (this == nullSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        invalidated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (debug != null && Debug.isOn("session")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            System.out.println("%% Invalidated:  " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (context != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            context.remove(sessionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Table of application-specific session data indexed by an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * key and the calling security context. This is important since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * sessions can be shared across different protection domains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   640
    private Hashtable<SecureKey, Object> table = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Assigns a session value.  Session change events are given if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * appropriate, to any original value as well as the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   646
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public void putValue(String key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if ((key == null) || (value == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            throw new IllegalArgumentException("arguments can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        SecureKey secureKey = new SecureKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        Object oldValue = table.put(secureKey, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (oldValue instanceof SSLSessionBindingListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            SSLSessionBindingEvent e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            e = new SSLSessionBindingEvent(this, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            ((SSLSessionBindingListener)oldValue).valueUnbound(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (value instanceof SSLSessionBindingListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            SSLSessionBindingEvent e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            e = new SSLSessionBindingEvent(this, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            ((SSLSessionBindingListener)value).valueBound(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Returns the specified session value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   673
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public Object getValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new IllegalArgumentException("argument can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        SecureKey secureKey = new SecureKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return table.get(secureKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Removes the specified session value, delivering a session changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * event as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   688
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    public void removeValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            throw new IllegalArgumentException("argument can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        SecureKey secureKey = new SecureKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        Object value = table.remove(secureKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (value instanceof SSLSessionBindingListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            SSLSessionBindingEvent e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            e = new SSLSessionBindingEvent(this, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            ((SSLSessionBindingListener)value).valueUnbound(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Lists the names of the session values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   709
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    public String[] getValueNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        Enumeration<SecureKey> e;
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   712
        Vector<Object> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        SecureKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        Object securityCtx = SecureKey.getCurrentSecurityContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        for (e = table.keys(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            key = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if (securityCtx.equals(key.getSecurityContext())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                v.addElement(key.getAppKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        String[] names = new String[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        v.copyInto(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Use large packet sizes now or follow RFC 2246 packet sizes (2^14)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * until changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * In the TLS specification (section 6.2.1, RFC2246), it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * recommended that the plaintext has more than 2^14 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * However, some TLS implementations violate the specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * This is a workaround for interoperability with these stacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Application could accept large fragments up to 2^15 bytes by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * setting the system property jsse.SSLEngine.acceptLargeFragments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * to "true".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    private boolean acceptLargeFragments =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        Debug.getBooleanProperty("jsse.SSLEngine.acceptLargeFragments", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Expand the buffer size of both SSL/TLS network packet and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * application data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    protected synchronized void expandBufferSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        acceptLargeFragments = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * Gets the current size of the largest SSL/TLS packet that is expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * when using this session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   757
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public synchronized int getPacketBufferSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        return acceptLargeFragments ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                Record.maxLargeRecordSize : Record.maxRecordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Gets the current size of the largest application data that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * expected when using this session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   767
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public synchronized int getApplicationBufferSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        return getPacketBufferSize() - Record.headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   772
    /**
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   773
     * Gets an array of supported signature algorithms that the local side is
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   774
     * willing to verify.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   775
     */
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   776
    @Override
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   777
    public String[] getLocalSupportedSignatureAlgorithms() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   778
        if (localSupportedSignAlgs != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   779
            return localSupportedSignAlgs.clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   780
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   781
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   782
        return new String[0];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   783
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   784
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   785
    /**
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   786
     * Gets an array of supported signature algorithms that the peer is
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   787
     * able to verify.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   788
     */
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   789
    @Override
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   790
    public String[] getPeerSupportedSignatureAlgorithms() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   791
        if (peerSupportedSignAlgs != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   792
            return peerSupportedSignAlgs.clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   793
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   794
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   795
        return new String[0];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   796
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   797
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   798
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   799
     * Obtains a <code>List</code> containing all {@link SNIServerName}s
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   800
     * of the requested Server Name Indication (SNI) extension.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   801
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   802
    @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   803
    public List<SNIServerName> getRequestedServerNames() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   804
        if (requestedServerNames != null && !requestedServerNames.isEmpty()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   805
            return Collections.<SNIServerName>unmodifiableList(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   806
                                                requestedServerNames);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   807
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   808
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   809
        return Collections.<SNIServerName>emptyList();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   810
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
   811
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /** Returns a string representation of this SSL session */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   813
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return "[Session-" + sessionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            + ", " + getCipherSuite()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * When SSL sessions are finalized, all values bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * them are removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   824
    @Override
17458
4252f06a378f 8005535: SSLSessionImpl should have protected finalize()
xuelei
parents: 14664
diff changeset
   825
    protected void finalize() throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        String[] names = getValueNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            removeValue(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
 * This "struct" class serves as a Hash Key that combines an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
 * application-specific key and a security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
class SecureKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    private static Object       nullObject = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    private Object        appKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private Object      securityCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    static Object getCurrentSecurityContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        Object context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            context = sm.getSecurityContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            context = nullObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    SecureKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        this.appKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        this.securityCtx = getCurrentSecurityContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    Object getAppKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return appKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    Object getSecurityContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return securityCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   867
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return appKey.hashCode() ^ securityCtx.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   872
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        return o instanceof SecureKey && ((SecureKey)o).appKey.equals(appKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        && ((SecureKey)o).securityCtx.equals(securityCtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
}