src/java.base/share/classes/sun/security/ssl/SSLSessionContextImpl.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 51398 3c389a284345
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2018, 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: 2060
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: 2060
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: 2060
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2060
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2060
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 sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    28
import java.util.ArrayList;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    29
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
    31
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.SSLSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ssl.SSLSessionContext;
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    34
import sun.security.util.Cache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    37
final class SSLSessionContextImpl implements SSLSessionContext {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    38
    private final Cache<SessionId, SSLSessionImpl> sessionCache;
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    39
                                        // session cache, session id as key
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    40
    private final Cache<String, SSLSessionImpl> sessionHostPortCache;
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    41
                                        // session cache, "host:port" as key
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    42
    private int cacheLimit;             // the max cache size
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    43
    private int timeout;                // timeout in seconds
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    44
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    45
    // package private
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    46
    SSLSessionContextImpl() {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    47
        cacheLimit = getDefaultCacheLimit();    // default cache size
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    48
        timeout = 86400;                        // default, 24 hours
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    49
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    50
        // use soft reference
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    51
        sessionCache = Cache.newSoftMemoryCache(cacheLimit, timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    52
        sessionHostPortCache = Cache.newSoftMemoryCache(cacheLimit, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    56
     * Returns the <code>SSLSession</code> bound to the specified session id.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
    58
    @Override
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    59
    public SSLSession getSession(byte[] sessionId) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    60
        if (sessionId == null) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    61
            throw new NullPointerException("session id cannot be null");
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    62
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    63
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    64
        SSLSessionImpl sess = sessionCache.get(new SessionId(sessionId));
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    65
        if (!isTimedout(sess)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    66
            return sess;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    67
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    68
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    69
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns an enumeration of the active SSL sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
    75
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public Enumeration<byte[]> getIds() {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    77
        SessionCacheVisitor scVisitor = new SessionCacheVisitor();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    78
        sessionCache.accept(scVisitor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    80
        return scVisitor.getSessionIds();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    83
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    84
     * Sets the timeout limit for cached <code>SSLSession</code> objects
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    85
     *
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    86
     * Note that after reset the timeout, the cached session before
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    87
     * should be timed within the shorter one of the old timeout and the
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    88
     * new timeout.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    89
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
    90
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void setSessionTimeout(int seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                 throws IllegalArgumentException {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    93
        if (seconds < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new IllegalArgumentException();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    95
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    96
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    97
        if (timeout != seconds) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    98
            sessionCache.setTimeout(seconds);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    99
            sessionHostPortCache.setTimeout(seconds);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   100
            timeout = seconds;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   101
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   104
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   105
     * Gets the timeout limit for cached <code>SSLSession</code> objects
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   106
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   107
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int getSessionTimeout() {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   109
        return timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   112
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   113
     * Sets the size of the cache used for storing
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   114
     * <code>SSLSession</code> objects.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   115
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   116
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void setSessionCacheSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                 throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   122
        if (cacheLimit != size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   123
            sessionCache.setCapacity(size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   124
            sessionHostPortCache.setCapacity(size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   125
            cacheLimit = size;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   126
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   129
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   130
     * Gets the size of the cache used for storing
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   131
     * <code>SSLSession</code> objects.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   132
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   133
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public int getSessionCacheSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return cacheLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   138
    // package-private method, used ONLY by ServerHandshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    SSLSessionImpl get(byte[] id) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   140
        return (SSLSessionImpl)getSession(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   143
    // package-private method, used ONLY by ClientHandshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    SSLSessionImpl get(String hostname, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * If no session caching info is available, we won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * get one, so exit before doing a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (hostname == null && port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   152
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   153
        SSLSessionImpl sess = sessionHostPortCache.get(getKey(hostname, port));
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   154
        if (!isTimedout(sess)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   155
            return sess;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   156
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   157
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   158
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   161
    private static String getKey(String hostname, int port) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   162
        return (hostname + ":" +
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   163
            String.valueOf(port)).toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   166
    // cache a SSLSession
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   167
    //
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   168
    // In SunJSSE implementation, a session is created while getting a
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   169
    // client hello or a server hello message, and cached while the
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   170
    // handshaking finished.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   171
    // Here we time the session from the time it cached instead of the
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   172
    // time it created, which is a little longer than the expected. So
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   173
    // please do check isTimedout() while getting entry from the cache.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    void put(SSLSessionImpl s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        sessionCache.put(s.getSessionId(), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   177
        // If no hostname/port info is available, don't add this one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if ((s.getPeerHost() != null) && (s.getPeerPort() != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            sessionHostPortCache.put(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                getKey(s.getPeerHost(), s.getPeerPort()), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   182
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        s.setContext(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   186
    // package-private method, remove a cached SSLSession
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   187
    void remove(SessionId key) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   188
        SSLSessionImpl s = sessionCache.get(key);
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   189
        if (s != null) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   190
            sessionCache.remove(key);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   191
            sessionHostPortCache.remove(
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   192
                    getKey(s.getPeerHost(), s.getPeerPort()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   196
    private static int getDefaultCacheLimit() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   197
        int defaultCacheLimit = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   199
            String s = java.security.AccessController.doPrivileged(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   200
                    new java.security.PrivilegedAction<String>() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   201
                    @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   202
                    public String run() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   203
                        return System.getProperty(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   204
                            "javax.net.ssl.sessionCacheSize");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   205
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   206
                });
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   207
                defaultCacheLimit = (s != null) ? Integer.parseInt(s) : 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (Exception e) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   209
            // swallow the exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   212
        return (defaultCacheLimit > 0) ? defaultCacheLimit : 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   215
    private boolean isTimedout(SSLSession sess) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   216
        if (timeout == 0) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   217
            return false;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   218
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   219
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   220
        if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   221
                                        <= (System.currentTimeMillis()))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            sess.invalidate();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   223
            return true;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   224
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   225
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   226
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   229
    private final class SessionCacheVisitor
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   230
            implements Cache.CacheVisitor<SessionId, SSLSessionImpl> {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   231
        ArrayList<byte[]> ids = null;
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   232
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   233
        // public void visit(java.util.Map<K,V> map) {}
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   234
        @Override
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   235
        public void visit(java.util.Map<SessionId, SSLSessionImpl> map) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   236
            ids = new ArrayList<>(map.size());
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   237
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   238
            for (SessionId key : map.keySet()) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   239
                SSLSessionImpl value = map.get(key);
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   240
                if (!isTimedout(value)) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   241
                    ids.add(key.getId());
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   242
                }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   243
            }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   244
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   245
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   246
        Enumeration<byte[]> getSessionIds() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   247
            return  ids != null ? Collections.enumeration(ids) :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   248
                                  Collections.emptyEnumeration();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   249
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}