Showing the changes to java/pkg/sun/security/ssl/SSLSessionContextImpl.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,255 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | + | |
| 27 | +package sun.security.ssl; | |
| 28 | + | |
| 29 | +import java.io.*; | |
| 30 | +import java.net.*; | |
| 31 | +import java.util.Date; | |
| 32 | +import java.util.Enumeration; | |
| 33 | +import java.util.Hashtable; | |
| 34 | +import java.util.NoSuchElementException; | |
| 35 | +import java.util.Vector; | |
| 36 | + | |
| 37 | +import javax.net.ssl.SSLSession; | |
| 38 | +import javax.net.ssl.SSLSessionContext; | |
| 39 | +import javax.net.ssl.SSLSessionBindingListener; | |
| 40 | +import javax.net.ssl.SSLSessionBindingEvent; | |
| 41 | +import javax.net.ssl.SSLPeerUnverifiedException; | |
| 42 | +import javax.net.ssl.SSLSession; | |
| 43 | + | |
| 44 | +import sun.security.util.Cache; | |
| 45 | + | |
| 46 | + | |
| 47 | +final class SSLSessionContextImpl implements SSLSessionContext { | |
| 48 | + private Cache sessionCache; // session cache, session id as key | |
| 49 | + private Cache sessionHostPortCache; // session cache, "host:port" as key | |
| 50 | + private int cacheLimit; // the max cache size | |
| 51 | + private int timeout; // timeout in seconds | |
| 52 | + | |
| 53 | + private static final Debug debug = Debug.getInstance("ssl"); | |
| 54 | + | |
| 55 | + // package private | |
| 56 | + SSLSessionContextImpl() { | |
| 57 | + cacheLimit = getDefaultCacheLimit(); // default cache size | |
| 58 | + timeout = 86400; // default, 24 hours | |
| 59 | + | |
| 60 | + // use soft reference | |
| 61 | + sessionCache = Cache.newSoftMemoryCache(cacheLimit, timeout); | |
| 62 | + sessionHostPortCache = Cache.newSoftMemoryCache(cacheLimit, timeout); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Returns the <code>SSLSession</code> bound to the specified session id. | |
| 67 | + */ | |
| 68 | + public SSLSession getSession(byte[] sessionId) { | |
| 69 | + if (sessionId == null) { | |
| 70 | + throw new NullPointerException("session id cannot be null"); | |
| 71 | + } | |
| 72 | + | |
| 73 | + SSLSessionImpl sess = | |
| 74 | + (SSLSessionImpl)sessionCache.get(new SessionId(sessionId)); | |
| 75 | + if (!isTimedout(sess)) { | |
| 76 | + return sess; | |
| 77 | + } | |
| 78 | + | |
| 79 | + return null; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Returns an enumeration of the active SSL sessions. | |
| 84 | + */ | |
| 85 | + public Enumeration<byte[]> getIds() { | |
| 86 | + SessionCacheVisitor scVisitor = new SessionCacheVisitor(); | |
| 87 | + sessionCache.accept(scVisitor); | |
| 88 | + | |
| 89 | + return scVisitor.getSessionIds(); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Sets the timeout limit for cached <code>SSLSession</code> objects | |
| 94 | + * | |
| 95 | + * Note that after reset the timeout, the cached session before | |
| 96 | + * should be timed within the shorter one of the old timeout and the | |
| 97 | + * new timeout. | |
| 98 | + */ | |
| 99 | + public void setSessionTimeout(int seconds) | |
| 100 | + throws IllegalArgumentException { | |
| 101 | + if (seconds < 0) { | |
| 102 | + throw new IllegalArgumentException(); | |
| 103 | + } | |
| 104 | + | |
| 105 | + if (timeout != seconds) { | |
| 106 | + sessionCache.setTimeout(seconds); | |
| 107 | + sessionHostPortCache.setTimeout(seconds); | |
| 108 | + timeout = seconds; | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * Gets the timeout limit for cached <code>SSLSession</code> objects | |
| 114 | + */ | |
| 115 | + public int getSessionTimeout() { | |
| 116 | + return timeout; | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Sets the size of the cache used for storing | |
| 121 | + * <code>SSLSession</code> objects. | |
| 122 | + */ | |
| 123 | + public void setSessionCacheSize(int size) | |
| 124 | + throws IllegalArgumentException { | |
| 125 | + if (size < 0) | |
| 126 | + throw new IllegalArgumentException(); | |
| 127 | + | |
| 128 | + if (cacheLimit != size) { | |
| 129 | + sessionCache.setCapacity(size); | |
| 130 | + sessionHostPortCache.setCapacity(size); | |
| 131 | + cacheLimit = size; | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * Gets the size of the cache used for storing | |
| 137 | + * <code>SSLSession</code> objects. | |
| 138 | + */ | |
| 139 | + public int getSessionCacheSize() { | |
| 140 | + return cacheLimit; | |
| 141 | + } | |
| 142 | + | |
| 143 | + | |
| 144 | + // package-private method, used ONLY by ServerHandshaker | |
| 145 | + SSLSessionImpl get(byte[] id) { | |
| 146 | + return (SSLSessionImpl)getSession(id); | |
| 147 | + } | |
| 148 | + | |
| 149 | + // package-private method, used ONLY by ClientHandshaker | |
| 150 | + SSLSessionImpl get(String hostname, int port) { | |
| 151 | + /* | |
| 152 | + * If no session caching info is available, we won't | |
| 153 | + * get one, so exit before doing a lookup. | |
| 154 | + */ | |
| 155 | + if (hostname == null && port == -1) { | |
| 156 | + return null; | |
| 157 | + } | |
| 158 | + | |
| 159 | + SSLSessionImpl sess = | |
| 160 | + (SSLSessionImpl)sessionHostPortCache.get(getKey(hostname, port)); | |
| 161 | + if (!isTimedout(sess)) { | |
| 162 | + return sess; | |
| 163 | + } | |
| 164 | + | |
| 165 | + return null; | |
| 166 | + } | |
| 167 | + | |
| 168 | + private String getKey(String hostname, int port) { | |
| 169 | + return (hostname + ":" + String.valueOf(port)).toLowerCase(); | |
| 170 | + } | |
| 171 | + | |
| 172 | + // cache a SSLSession | |
| 173 | + // | |
| 174 | + // In SunJSSE implementation, a session is created while getting a | |
| 175 | + // client hello or a server hello message, and cached while the | |
| 176 | + // handshaking finished. | |
| 177 | + // Here we time the session from the time it cached instead of the | |
| 178 | + // time it created, which is a little longer than the expected. So | |
| 179 | + // please do check isTimedout() while getting entry from the cache. | |
| 180 | + void put(SSLSessionImpl s) { | |
| 181 | + sessionCache.put(s.getSessionId(), s); | |
| 182 | + | |
| 183 | + // If no hostname/port info is available, don't add this one. | |
| 184 | + if ((s.getPeerHost() != null) && (s.getPeerPort() != -1)) { | |
| 185 | + sessionHostPortCache.put( | |
| 186 | + getKey(s.getPeerHost(), s.getPeerPort()), s); | |
| 187 | + } | |
| 188 | + | |
| 189 | + s.setContext(this); | |
| 190 | + } | |
| 191 | + | |
| 192 | + // package-private method, remove a cached SSLSession | |
| 193 | + void remove(SessionId key) { | |
| 194 | + SSLSessionImpl s = (SSLSessionImpl)sessionCache.get(key); | |
| 195 | + if (s != null) { | |
| 196 | + sessionCache.remove(key); | |
| 197 | + sessionHostPortCache.remove( | |
| 198 | + getKey(s.getPeerHost(), s.getPeerPort())); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + private int getDefaultCacheLimit() { | |
| 203 | + int cacheLimit = 0; | |
| 204 | + try { | |
| 205 | + String s = java.security.AccessController.doPrivileged( | |
| 206 | + new java.security.PrivilegedAction<String>() { | |
| 207 | + public String run() { | |
| 208 | + return System.getProperty( | |
| 209 | + "javax.net.ssl.sessionCacheSize"); | |
| 210 | + } | |
| 211 | + }); | |
| 212 | + cacheLimit = (s != null) ? Integer.valueOf(s).intValue() : 0; | |
| 213 | + } catch (Exception e) { | |
| 214 | + } | |
| 215 | + | |
| 216 | + return (cacheLimit > 0) ? cacheLimit : 0; | |
| 217 | + } | |
| 218 | + | |
| 219 | + boolean isTimedout(SSLSession sess) { | |
| 220 | + if (timeout == 0) { | |
| 221 | + return false; | |
| 222 | + } | |
| 223 | + | |
| 224 | + if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L) | |
| 225 | + <= (System.currentTimeMillis()))) { | |
| 226 | + sess.invalidate(); | |
| 227 | + return true; | |
| 228 | + } | |
| 229 | + | |
| 230 | + return false; | |
| 231 | + } | |
| 232 | + | |
| 233 | + final class SessionCacheVisitor | |
| 234 | + implements sun.security.util.Cache.CacheVisitor { | |
| 235 | + Vector<byte[]> ids = null; | |
| 236 | + | |
| 237 | + // public void visit(java.util.Map<Object, Object> map) {} | |
| 238 | + public void visit(java.util.Map<Object, Object> map) { | |
| 239 | + ids = new Vector<byte[]>(map.size()); | |
| 240 | + | |
| 241 | + for (Object key : map.keySet()) { | |
| 242 | + SSLSessionImpl value = (SSLSessionImpl)map.get(key); | |
| 243 | + if (!isTimedout(value)) { | |
| 244 | + ids.addElement(((SessionId)key).getId()); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + } | |
| 248 | + | |
| 249 | + public Enumeration<byte[]> getSessionIds() { | |
| 250 | + return ids != null ? ids.elements() : | |
| 251 | + new Vector<byte[]>().elements(); | |
| 252 | + } | |
| 253 | + } | |
| 254 | + | |
| 255 | +} | |