J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
Sean Mullan | 779401f | 2017-02-06 08:59:00 -0500 | [diff] [blame] | 2 | * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. Oracle designates this |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 8 | * particular file as subject to the "Classpath" exception as provided |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 9 | * by Oracle in the LICENSE file that accompanied this code. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | package javax.net.ssl; |
| 27 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 28 | import java.security.Principal; |
| 29 | |
| 30 | /** |
| 31 | * In SSL, sessions are used to describe an ongoing relationship between |
| 32 | * two entities. Each SSL connection involves one session at a time, but |
| 33 | * that session may be used on many connections between those entities, |
| 34 | * simultaneously or sequentially. The session used on a connection may |
| 35 | * also be replaced by a different session. Sessions are created, or |
| 36 | * rejoined, as part of the SSL handshaking protocol. Sessions may be |
| 37 | * invalidated due to policies affecting security or resource usage, |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 38 | * or by an application explicitly calling {@code invalidate}. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 39 | * Session management policies are typically used to tune performance. |
| 40 | * |
| 41 | * <P> In addition to the standard session attributes, SSL sessions expose |
| 42 | * these read-only attributes: <UL> |
| 43 | * |
| 44 | * <LI> <em>Peer Identity.</em> Sessions are between a particular |
| 45 | * client and a particular server. The identity of the peer may |
| 46 | * have been established as part of session setup. Peers are |
| 47 | * generally identified by X.509 certificate chains. |
| 48 | * |
| 49 | * <LI> <em>Cipher Suite Name.</em> Cipher suites describe the |
| 50 | * kind of cryptographic protection that's used by connections |
| 51 | * in a particular session. |
| 52 | * |
| 53 | * <LI> <em>Peer Host.</em> All connections in a session are |
| 54 | * between the same two hosts. The address of the host on the other |
| 55 | * side of the connection is available. |
| 56 | * |
| 57 | * </UL> |
| 58 | * |
| 59 | * <P> Sessions may be explicitly invalidated. Invalidation may also |
| 60 | * be done implicitly, when faced with certain kinds of errors. |
| 61 | * |
| 62 | * @since 1.4 |
| 63 | * @author David Brownell |
| 64 | */ |
| 65 | public interface SSLSession { |
| 66 | |
| 67 | /** |
| 68 | * Returns the identifier assigned to this Session. |
| 69 | * |
| 70 | * @return the Session identifier |
| 71 | */ |
| 72 | public byte[] getId(); |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Returns the context in which this session is bound. |
| 77 | * <P> |
| 78 | * This context may be unavailable in some environments, |
| 79 | * in which case this method returns null. |
| 80 | * <P> |
| 81 | * If the context is available and there is a |
| 82 | * security manager installed, the caller may require |
| 83 | * permission to access it or a security exception may be thrown. |
| 84 | * In a Java environment, the security manager's |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 85 | * {@code checkPermission} method is called with a |
| 86 | * {@code SSLPermission("getSSLSessionContext")} permission. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 87 | * |
| 88 | * @throws SecurityException if the calling thread does not have |
| 89 | * permission to get SSL session context. |
| 90 | * @return the session context used for this session, or null |
| 91 | * if the context is unavailable. |
| 92 | */ |
| 93 | public SSLSessionContext getSessionContext(); |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Returns the time at which this Session representation was created, |
| 98 | * in milliseconds since midnight, January 1, 1970 UTC. |
| 99 | * |
| 100 | * @return the time this Session was created |
| 101 | */ |
| 102 | public long getCreationTime(); |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Returns the last time this Session representation was accessed by the |
| 107 | * session level infrastructure, in milliseconds since |
| 108 | * midnight, January 1, 1970 UTC. |
| 109 | * <P> |
| 110 | * Access indicates a new connection being established using session data. |
| 111 | * Application level operations, such as getting or setting a value |
| 112 | * associated with the session, are not reflected in this access time. |
| 113 | * |
| 114 | * <P> This information is particularly useful in session management |
| 115 | * policies. For example, a session manager thread could leave all |
| 116 | * sessions in a given context which haven't been used in a long time; |
| 117 | * or, the sessions might be sorted according to age to optimize some task. |
| 118 | * |
| 119 | * @return the last time this Session was accessed |
| 120 | */ |
| 121 | public long getLastAccessedTime(); |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Invalidates the session. |
| 126 | * <P> |
| 127 | * Future connections will not be able to |
| 128 | * resume or join this session. However, any existing connection |
| 129 | * using this session can continue to use the session until the |
| 130 | * connection is closed. |
| 131 | * |
| 132 | * @see #isValid() |
| 133 | */ |
| 134 | public void invalidate(); |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Returns whether this session is valid and available for resuming or |
| 139 | * joining. |
| 140 | * |
| 141 | * @return true if this session may be rejoined. |
| 142 | * @see #invalidate() |
| 143 | * |
| 144 | * @since 1.5 |
| 145 | */ |
| 146 | public boolean isValid(); |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 151 | * Binds the specified {@code value} object into the |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 152 | * session's application layer data |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 153 | * with the given {@code name}. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 154 | * <P> |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 155 | * Any existing binding using the same {@code name} is |
| 156 | * replaced. If the new (or existing) {@code value} implements the |
| 157 | * {@code SSLSessionBindingListener} interface, the object |
| 158 | * represented by {@code value} is notified appropriately. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 159 | * <p> |
| 160 | * For security reasons, the same named values may not be |
| 161 | * visible across different access control contexts. |
| 162 | * |
| 163 | * @param name the name to which the data object will be bound. |
| 164 | * This may not be null. |
| 165 | * @param value the data object to be bound. This may not be null. |
| 166 | * @throws IllegalArgumentException if either argument is null. |
| 167 | */ |
| 168 | public void putValue(String name, Object value); |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Returns the object bound to the given name in the session's |
| 173 | * application layer data. Returns null if there is no such binding. |
| 174 | * <p> |
| 175 | * For security reasons, the same named values may not be |
| 176 | * visible across different access control contexts. |
| 177 | * |
| 178 | * @param name the name of the binding to find. |
| 179 | * @return the value bound to that name, or null if the binding does |
| 180 | * not exist. |
| 181 | * @throws IllegalArgumentException if the argument is null. |
| 182 | */ |
| 183 | public Object getValue(String name); |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Removes the object bound to the given name in the session's |
| 188 | * application layer data. Does nothing if there is no object |
| 189 | * bound to the given name. If the bound existing object |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 190 | * implements the {@code SessionBindingListener} interface, |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 191 | * it is notified appropriately. |
| 192 | * <p> |
| 193 | * For security reasons, the same named values may not be |
| 194 | * visible across different access control contexts. |
| 195 | * |
| 196 | * @param name the name of the object to remove visible |
| 197 | * across different access control contexts |
| 198 | * @throws IllegalArgumentException if the argument is null. |
| 199 | */ |
| 200 | public void removeValue(String name); |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Returns an array of the names of all the application layer |
| 205 | * data objects bound into the Session. |
| 206 | * <p> |
| 207 | * For security reasons, the same named values may not be |
| 208 | * visible across different access control contexts. |
| 209 | * |
| 210 | * @return a non-null (possibly empty) array of names of the objects |
| 211 | * bound to this Session. |
| 212 | */ |
| 213 | public String [] getValueNames(); |
| 214 | |
| 215 | /** |
| 216 | * Returns the identity of the peer which was established as part |
| 217 | * of defining the session. |
| 218 | * <P> |
| 219 | * Note: This method can be used only when using certificate-based |
| 220 | * cipher suites; using it with non-certificate-based cipher suites, |
| 221 | * such as Kerberos, will throw an SSLPeerUnverifiedException. |
Xue-Lei Andrew Fan | d109f68 | 2016-07-27 02:23:16 +0000 | [diff] [blame] | 222 | * <P> |
| 223 | * Note: The returned value may not be a valid certificate chain |
| 224 | * and should not be relied on for trust decisions. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 225 | * |
| 226 | * @return an ordered array of peer certificates, |
| 227 | * with the peer's own certificate first followed by any |
| 228 | * certificate authorities. |
| 229 | * @exception SSLPeerUnverifiedException if the peer's identity has not |
| 230 | * been verified |
| 231 | * @see #getPeerPrincipal() |
| 232 | */ |
| 233 | public java.security.cert.Certificate [] getPeerCertificates() |
| 234 | throws SSLPeerUnverifiedException; |
| 235 | |
| 236 | /** |
| 237 | * Returns the certificate(s) that were sent to the peer during |
| 238 | * handshaking. |
| 239 | * <P> |
| 240 | * Note: This method is useful only when using certificate-based |
| 241 | * cipher suites. |
| 242 | * <P> |
| 243 | * When multiple certificates are available for use in a |
| 244 | * handshake, the implementation chooses what it considers the |
| 245 | * "best" certificate chain available, and transmits that to |
| 246 | * the other side. This method allows the caller to know |
| 247 | * which certificate chain was actually used. |
| 248 | * |
| 249 | * @return an ordered array of certificates, |
| 250 | * with the local certificate first followed by any |
| 251 | * certificate authorities. If no certificates were sent, |
| 252 | * then null is returned. |
| 253 | * |
| 254 | * @see #getLocalPrincipal() |
| 255 | */ |
| 256 | public java.security.cert.Certificate [] getLocalCertificates(); |
| 257 | |
| 258 | /** |
| 259 | * Returns the identity of the peer which was identified as part |
| 260 | * of defining the session. |
| 261 | * <P> |
| 262 | * Note: This method can be used only when using certificate-based |
| 263 | * cipher suites; using it with non-certificate-based cipher suites, |
| 264 | * such as Kerberos, will throw an SSLPeerUnverifiedException. |
Xue-Lei Andrew Fan | d109f68 | 2016-07-27 02:23:16 +0000 | [diff] [blame] | 265 | * <P> |
| 266 | * Note: The returned value may not be a valid certificate chain |
| 267 | * and should not be relied on for trust decisions. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 268 | * |
| 269 | * <p><em>Note: this method exists for compatibility with previous |
| 270 | * releases. New applications should use |
| 271 | * {@link #getPeerCertificates} instead.</em></p> |
| 272 | * |
| 273 | * @return an ordered array of peer X.509 certificates, |
| 274 | * with the peer's own certificate first followed by any |
| 275 | * certificate authorities. (The certificates are in |
| 276 | * the original JSSE certificate |
| 277 | * {@link javax.security.cert.X509Certificate} format.) |
| 278 | * @exception SSLPeerUnverifiedException if the peer's identity |
| 279 | * has not been verified |
| 280 | * @see #getPeerPrincipal() |
Jason Uh | 47e85e2 | 2015-03-09 17:21:07 -0700 | [diff] [blame] | 281 | * @deprecated The {@link #getPeerCertificates()} method that returns an |
Sean Mullan | 779401f | 2017-02-06 08:59:00 -0500 | [diff] [blame] | 282 | * array of {@code java.security.cert.Certificate} should |
| 283 | * be used instead. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 284 | */ |
Sean Mullan | 779401f | 2017-02-06 08:59:00 -0500 | [diff] [blame] | 285 | @Deprecated(since="9") |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 286 | public javax.security.cert.X509Certificate [] getPeerCertificateChain() |
| 287 | throws SSLPeerUnverifiedException; |
| 288 | |
| 289 | /** |
| 290 | * Returns the identity of the peer which was established as part of |
| 291 | * defining the session. |
| 292 | * |
| 293 | * @return the peer's principal. Returns an X500Principal of the |
| 294 | * end-entity certiticate for X509-based cipher suites, and |
| 295 | * KerberosPrincipal for Kerberos cipher suites. |
| 296 | * |
| 297 | * @throws SSLPeerUnverifiedException if the peer's identity has not |
| 298 | * been verified |
| 299 | * |
| 300 | * @see #getPeerCertificates() |
| 301 | * @see #getLocalPrincipal() |
| 302 | * |
| 303 | * @since 1.5 |
| 304 | */ |
| 305 | public Principal getPeerPrincipal() |
| 306 | throws SSLPeerUnverifiedException; |
| 307 | |
| 308 | /** |
| 309 | * Returns the principal that was sent to the peer during handshaking. |
| 310 | * |
| 311 | * @return the principal sent to the peer. Returns an X500Principal |
| 312 | * of the end-entity certificate for X509-based cipher suites, and |
| 313 | * KerberosPrincipal for Kerberos cipher suites. If no principal was |
| 314 | * sent, then null is returned. |
| 315 | * |
| 316 | * @see #getLocalCertificates() |
| 317 | * @see #getPeerPrincipal() |
| 318 | * |
| 319 | * @since 1.5 |
| 320 | */ |
| 321 | public Principal getLocalPrincipal(); |
| 322 | |
| 323 | /** |
| 324 | * Returns the name of the SSL cipher suite which is used for all |
| 325 | * connections in the session. |
| 326 | * |
| 327 | * <P> This defines the level of protection |
| 328 | * provided to the data sent on the connection, including the kind |
| 329 | * of encryption used and most aspects of how authentication is done. |
| 330 | * |
| 331 | * @return the name of the session's cipher suite |
| 332 | */ |
| 333 | public String getCipherSuite(); |
| 334 | |
| 335 | /** |
| 336 | * Returns the standard name of the protocol used for all |
| 337 | * connections in the session. |
| 338 | * |
| 339 | * <P> This defines the protocol used in the connection. |
| 340 | * |
| 341 | * @return the standard name of the protocol used for all |
| 342 | * connections in the session. |
| 343 | */ |
| 344 | public String getProtocol(); |
| 345 | |
| 346 | /** |
| 347 | * Returns the host name of the peer in this session. |
| 348 | * <P> |
| 349 | * For the server, this is the client's host; and for |
| 350 | * the client, it is the server's host. The name may not be |
| 351 | * a fully qualified host name or even a host name at all as |
| 352 | * it may represent a string encoding of the peer's network address. |
| 353 | * If such a name is desired, it might |
| 354 | * be resolved through a name service based on the value returned |
| 355 | * by this method. |
| 356 | * <P> |
| 357 | * This value is not authenticated and should not be relied upon. |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 358 | * It is mainly used as a hint for {@code SSLSession} caching |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 359 | * strategies. |
| 360 | * |
| 361 | * @return the host name of the peer host, or null if no information |
| 362 | * is available. |
| 363 | */ |
| 364 | public String getPeerHost(); |
| 365 | |
| 366 | /** |
| 367 | * Returns the port number of the peer in this session. |
| 368 | * <P> |
| 369 | * For the server, this is the client's port number; and for |
| 370 | * the client, it is the server's port number. |
| 371 | * <P> |
| 372 | * This value is not authenticated and should not be relied upon. |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 373 | * It is mainly used as a hint for {@code SSLSession} caching |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 374 | * strategies. |
| 375 | * |
| 376 | * @return the port number of the peer host, or -1 if no information |
| 377 | * is available. |
| 378 | * |
| 379 | * @since 1.5 |
| 380 | */ |
| 381 | public int getPeerPort(); |
| 382 | |
| 383 | /** |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 384 | * Gets the current size of the largest SSL/TLS/DTLS packet that is |
| 385 | * expected when using this session. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 386 | * <P> |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 387 | * An {@code SSLEngine} using this session may generate SSL/TLS/DTLS |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 388 | * packets of any size up to and including the value returned by this |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 389 | * method. All {@code SSLEngine} network buffers should be sized |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 390 | * at least this large to avoid insufficient space problems when |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 391 | * performing {@code wrap} and {@code unwrap} calls. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 392 | * |
| 393 | * @return the current maximum expected network packet size |
| 394 | * |
| 395 | * @see SSLEngine#wrap(ByteBuffer, ByteBuffer) |
| 396 | * @see SSLEngine#unwrap(ByteBuffer, ByteBuffer) |
| 397 | * |
| 398 | * @since 1.5 |
| 399 | */ |
| 400 | public int getPacketBufferSize(); |
| 401 | |
| 402 | |
| 403 | /** |
| 404 | * Gets the current size of the largest application data that is |
| 405 | * expected when using this session. |
| 406 | * <P> |
Xue-Lei Andrew Fan | 0f1698f | 2015-06-02 04:01:04 +0000 | [diff] [blame] | 407 | * {@code SSLEngine} application data buffers must be large |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 408 | * enough to hold the application data from any inbound network |
| 409 | * application data packet received. Typically, outbound |
| 410 | * application data buffers can be of any size. |
| 411 | * |
| 412 | * @return the current maximum expected application packet size |
| 413 | * |
| 414 | * @see SSLEngine#wrap(ByteBuffer, ByteBuffer) |
| 415 | * @see SSLEngine#unwrap(ByteBuffer, ByteBuffer) |
| 416 | * |
| 417 | * @since 1.5 |
| 418 | */ |
| 419 | public int getApplicationBufferSize(); |
| 420 | } |