jdk/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 5506 202f599c92aa
child 10369 e9d2e59e53f0
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2060
diff changeset
     2
 * Copyright (c) 1999, 2009, 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
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.net.ssl.SSLSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.SSLSessionContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.SSLSessionBindingListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.SSLSessionBindingEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.net.ssl.SSLPeerUnverifiedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.net.ssl.SSLSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    44
import sun.security.util.Cache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    47
final class SSLSessionContextImpl implements SSLSessionContext {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    48
    private Cache sessionCache;         // session cache, session id as key
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    49
    private Cache sessionHostPortCache; // session cache, "host:port" as key
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    50
    private int cacheLimit;             // the max cache size
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    51
    private int timeout;                // timeout in seconds
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    55
    // package private
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    56
    SSLSessionContextImpl() {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    57
        cacheLimit = getDefaultCacheLimit();    // default cache size
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    58
        timeout = 86400;                        // default, 24 hours
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    59
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    60
        // use soft reference
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    61
        sessionCache = Cache.newSoftMemoryCache(cacheLimit, timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    62
        sessionHostPortCache = Cache.newSoftMemoryCache(cacheLimit, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    66
     * Returns the <code>SSLSession</code> bound to the specified session id.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    68
    public SSLSession getSession(byte[] sessionId) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    69
        if (sessionId == null) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    70
            throw new NullPointerException("session id cannot be null");
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    71
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    72
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    73
        SSLSessionImpl sess =
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    74
                (SSLSessionImpl)sessionCache.get(new SessionId(sessionId));
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    75
        if (!isTimedout(sess)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    76
            return sess;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    77
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    78
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    79
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns an enumeration of the active SSL sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public Enumeration<byte[]> getIds() {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    86
        SessionCacheVisitor scVisitor = new SessionCacheVisitor();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    87
        sessionCache.accept(scVisitor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    89
        return scVisitor.getSessionIds();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    92
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    93
     * Sets the timeout limit for cached <code>SSLSession</code> objects
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    94
     *
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    95
     * Note that after reset the timeout, the cached session before
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    96
     * 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
    97
     * new timeout.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    98
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void setSessionTimeout(int seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                 throws IllegalArgumentException {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   101
        if (seconds < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   103
        }
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
        if (timeout != seconds) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   106
            sessionCache.setTimeout(seconds);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   107
            sessionHostPortCache.setTimeout(seconds);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   108
            timeout = seconds;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   109
        }
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
     * Gets the timeout limit for cached <code>SSLSession</code> objects
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   114
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public int getSessionTimeout() {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   116
        return timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   119
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   120
     * Sets the size of the cache used for storing
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   121
     * <code>SSLSession</code> objects.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   122
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void setSessionCacheSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                 throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   128
        if (cacheLimit != size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   129
            sessionCache.setCapacity(size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   130
            sessionHostPortCache.setCapacity(size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   131
            cacheLimit = size;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   132
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   135
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   136
     * Gets the size of the cache used for storing
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   137
     * <code>SSLSession</code> objects.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   138
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public int getSessionCacheSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return cacheLimit;
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
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   144
    // package-private method, used ONLY by ServerHandshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    SSLSessionImpl get(byte[] id) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   146
        return (SSLSessionImpl)getSession(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   149
    // package-private method, used ONLY by ClientHandshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    SSLSessionImpl get(String hostname, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * If no session caching info is available, we won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * get one, so exit before doing a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (hostname == null && port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   158
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   159
        SSLSessionImpl sess =
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   160
            (SSLSessionImpl)sessionHostPortCache.get(getKey(hostname, port));
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   161
        if (!isTimedout(sess)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   162
            return sess;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   163
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   164
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   165
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private String getKey(String hostname, int port) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   169
        return (hostname + ":" + String.valueOf(port)).toLowerCase();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   172
    // cache a SSLSession
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   173
    //
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   174
    // In SunJSSE implementation, a session is created while getting a
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   175
    // 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
   176
    // handshaking finished.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   177
    // 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
   178
    // 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
   179
    // please do check isTimedout() while getting entry from the cache.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    void put(SSLSessionImpl s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        sessionCache.put(s.getSessionId(), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   183
        // If no hostname/port info is available, don't add this one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if ((s.getPeerHost() != null) && (s.getPeerPort() != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            sessionHostPortCache.put(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                getKey(s.getPeerHost(), s.getPeerPort()), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        s.setContext(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   192
    // package-private method, remove a cached SSLSession
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   193
    void remove(SessionId key) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   194
        SSLSessionImpl s = (SSLSessionImpl)sessionCache.get(key);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   195
        if (s != null) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   196
            sessionCache.remove(key);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   197
            sessionHostPortCache.remove(
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   198
                        getKey(s.getPeerHost(), s.getPeerPort()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   202
    private int getDefaultCacheLimit() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int cacheLimit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String s = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    return System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        "javax.net.ssl.sessionCacheSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            cacheLimit = (s != null) ? Integer.valueOf(s).intValue() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return (cacheLimit > 0) ? cacheLimit : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   219
    boolean isTimedout(SSLSession sess) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   220
        if (timeout == 0) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   221
            return false;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   222
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   223
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   224
        if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   225
                                        <= (System.currentTimeMillis()))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            sess.invalidate();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   227
            return true;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   228
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   229
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   230
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   233
    final class SessionCacheVisitor
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   234
            implements sun.security.util.Cache.CacheVisitor {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   235
        Vector<byte[]> ids = null;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   236
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   237
        // public void visit(java.util.Map<Object, Object> map) {}
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   238
        public void visit(java.util.Map<Object, Object> map) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   239
            ids = new Vector<byte[]>(map.size());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   240
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   241
            for (Object key : map.keySet()) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   242
                SSLSessionImpl value = (SSLSessionImpl)map.get(key);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   243
                if (!isTimedout(value)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   244
                    ids.addElement(((SessionId)key).getId());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   245
                }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   246
            }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   247
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   248
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   249
        public Enumeration<byte[]> getSessionIds() {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   250
            return  ids != null ? ids.elements() :
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   251
                                  new Vector<byte[]>().elements();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   252
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   254
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
}