Path: blob/master/src/java.base/share/classes/javax/net/ssl/SSLSession.java
41159 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.net.ssl;2627import java.security.Principal;2829/**30* In SSL, sessions are used to describe an ongoing relationship between31* two entities. Each SSL connection involves one session at a time, but32* that session may be used on many connections between those entities,33* simultaneously or sequentially. The session used on a connection may34* also be replaced by a different session. Sessions are created, or35* rejoined, as part of the SSL handshaking protocol. Sessions may be36* invalidated due to policies affecting security or resource usage,37* or by an application explicitly calling {@code invalidate}.38* Session management policies are typically used to tune performance.39*40* <P> In addition to the standard session attributes, SSL sessions expose41* these read-only attributes: <UL>42*43* <LI> <em>Peer Identity.</em> Sessions are between a particular44* client and a particular server. The identity of the peer may45* have been established as part of session setup. Peers are46* generally identified by X.509 certificate chains.47*48* <LI> <em>Cipher Suite Name.</em> Cipher suites describe the49* kind of cryptographic protection that's used by connections50* in a particular session.51*52* <LI> <em>Peer Host.</em> All connections in a session are53* between the same two hosts. The address of the host on the other54* side of the connection is available.55*56* </UL>57*58* <P> Sessions may be explicitly invalidated. Invalidation may also59* be done implicitly, when faced with certain kinds of errors.60*61* @since 1.462* @author David Brownell63*/64public interface SSLSession {6566/**67* Returns the identifier assigned to this Session.68*69* @return the Session identifier70*/71public byte[] getId();727374/**75* Returns the context in which this session is bound.76* <P>77* This context may be unavailable in some environments,78* in which case this method returns null.79* <P>80* If the context is available and there is a81* security manager installed, the caller may require82* permission to access it or a security exception may be thrown.83* In a Java environment, the security manager's84* {@code checkPermission} method is called with a85* {@code SSLPermission("getSSLSessionContext")} permission.86*87* @throws SecurityException if the calling thread does not have88* permission to get SSL session context.89* @return the session context used for this session, or null90* if the context is unavailable.91*/92public SSLSessionContext getSessionContext();939495/**96* Returns the time at which this Session representation was created,97* in milliseconds since midnight, January 1, 1970 UTC.98*99* @return the time this Session was created100*/101public long getCreationTime();102103104/**105* Returns the last time this Session representation was accessed by the106* session level infrastructure, in milliseconds since107* midnight, January 1, 1970 UTC.108* <P>109* Access indicates a new connection being established using session data.110* Application level operations, such as getting or setting a value111* associated with the session, are not reflected in this access time.112*113* <P> This information is particularly useful in session management114* policies. For example, a session manager thread could leave all115* sessions in a given context which haven't been used in a long time;116* or, the sessions might be sorted according to age to optimize some task.117*118* @return the last time this Session was accessed119*/120public long getLastAccessedTime();121122123/**124* Invalidates the session.125* <P>126* Future connections will not be able to127* resume or join this session. However, any existing connection128* using this session can continue to use the session until the129* connection is closed.130*131* @see #isValid()132*/133public void invalidate();134135136/**137* Returns whether this session is valid and available for resuming or138* joining.139*140* @return true if this session may be rejoined.141* @see #invalidate()142*143* @since 1.5144*/145public boolean isValid();146147148/**149*150* Binds the specified {@code value} object into the151* session's application layer data152* with the given {@code name}.153* <P>154* Any existing binding using the same {@code name} is155* replaced. If the new (or existing) {@code value} implements the156* {@code SSLSessionBindingListener} interface, the object157* represented by {@code value} is notified appropriately.158* <p>159* For security reasons, the same named values may not be160* visible across different access control contexts.161*162* @param name the name to which the data object will be bound.163* This may not be null.164* @param value the data object to be bound. This may not be null.165* @throws IllegalArgumentException if either argument is null.166*/167public void putValue(String name, Object value);168169170/**171* Returns the object bound to the given name in the session's172* application layer data. Returns null if there is no such binding.173* <p>174* For security reasons, the same named values may not be175* visible across different access control contexts.176*177* @param name the name of the binding to find.178* @return the value bound to that name, or null if the binding does179* not exist.180* @throws IllegalArgumentException if the argument is null.181*/182public Object getValue(String name);183184185/**186* Removes the object bound to the given name in the session's187* application layer data. Does nothing if there is no object188* bound to the given name. If the bound existing object189* implements the {@code SSLSessionBindingListener} interface,190* it is notified appropriately.191* <p>192* For security reasons, the same named values may not be193* visible across different access control contexts.194*195* @param name the name of the object to remove visible196* across different access control contexts197* @throws IllegalArgumentException if the argument is null.198*/199public void removeValue(String name);200201202/**203* Returns an array of the names of all the application layer204* data objects bound into the Session.205* <p>206* For security reasons, the same named values may not be207* visible across different access control contexts.208*209* @return a non-null (possibly empty) array of names of the objects210* bound to this Session.211*/212public String [] getValueNames();213214/**215* Returns the identity of the peer which was established as part216* of defining the session.217* <P>218* Note: This method can be used only when using certificate-based219* cipher suites; using it with non-certificate-based cipher suites,220* such as Kerberos, will throw an SSLPeerUnverifiedException.221* <P>222* Note: The returned value may not be a valid certificate chain223* and should not be relied on for trust decisions.224*225* @return an ordered array of peer certificates,226* with the peer's own certificate first followed by any227* certificate authorities.228* @exception SSLPeerUnverifiedException if the peer's identity has not229* been verified230* @see #getPeerPrincipal()231*/232public java.security.cert.Certificate [] getPeerCertificates()233throws SSLPeerUnverifiedException;234235/**236* Returns the certificate(s) that were sent to the peer during237* handshaking.238* <P>239* Note: This method is useful only when using certificate-based240* cipher suites.241* <P>242* When multiple certificates are available for use in a243* handshake, the implementation chooses what it considers the244* "best" certificate chain available, and transmits that to245* the other side. This method allows the caller to know246* which certificate chain was actually used.247*248* @return an ordered array of certificates,249* with the local certificate first followed by any250* certificate authorities. If no certificates were sent,251* then null is returned.252*253* @see #getLocalPrincipal()254*/255public java.security.cert.Certificate [] getLocalCertificates();256257/**258* Returns the identity of the peer which was identified as part259* of defining the session.260* <P>261* Note: This method can be used only when using certificate-based262* cipher suites; using it with non-certificate-based cipher suites,263* such as Kerberos, will throw an SSLPeerUnverifiedException.264* <P>265* Note: The returned value may not be a valid certificate chain266* and should not be relied on for trust decisions.267*268* <p><em>Note: this method exists for compatibility with previous269* releases. New applications should use270* {@link #getPeerCertificates} instead.</em></p>271*272* @implSpec273* This default implementation throws UnsupportedOperationException.274*275* @return an ordered array of peer X.509 certificates,276* with the peer's own certificate first followed by any277* certificate authorities. (The certificates are in278* the original JSSE certificate279* {@link javax.security.cert.X509Certificate} format.)280* @throws SSLPeerUnverifiedException if the peer's identity281* has not been verified.282* @throws UnsupportedOperationException if the underlying provider283* does not implement the operation.284*285* @see #getPeerPrincipal()286* @deprecated The {@link #getPeerCertificates()} method that returns an287* array of {@code java.security.cert.Certificate} should288* be used instead.289*/290@SuppressWarnings("removal")291@Deprecated(since="9", forRemoval=true)292public default javax.security.cert.X509Certificate[]293getPeerCertificateChain() throws SSLPeerUnverifiedException {294throw new UnsupportedOperationException(295"This method is deprecated and marked for removal. Use the " +296"getPeerCertificates() method instead.");297}298299/**300* Returns the identity of the peer which was established as part of301* defining the session.302*303* @return the peer's principal. Returns an X500Principal of the304* end-entity certificate for X509-based cipher suites, and305* KerberosPrincipal for Kerberos cipher suites.306*307* @throws SSLPeerUnverifiedException if the peer's identity has not308* been verified309*310* @see #getPeerCertificates()311* @see #getLocalPrincipal()312*313* @since 1.5314*/315public Principal getPeerPrincipal()316throws SSLPeerUnverifiedException;317318/**319* Returns the principal that was sent to the peer during handshaking.320*321* @return the principal sent to the peer. Returns an X500Principal322* of the end-entity certificate for X509-based cipher suites, and323* KerberosPrincipal for Kerberos cipher suites. If no principal was324* sent, then null is returned.325*326* @see #getLocalCertificates()327* @see #getPeerPrincipal()328*329* @since 1.5330*/331public Principal getLocalPrincipal();332333/**334* Returns the name of the SSL cipher suite which is used for all335* connections in the session.336*337* <P> This defines the level of protection338* provided to the data sent on the connection, including the kind339* of encryption used and most aspects of how authentication is done.340*341* @return the name of the session's cipher suite342*/343public String getCipherSuite();344345/**346* Returns the standard name of the protocol used for all347* connections in the session.348*349* <P> This defines the protocol used in the connection.350*351* @return the standard name of the protocol used for all352* connections in the session.353*/354public String getProtocol();355356/**357* Returns the host name of the peer in this session.358* <P>359* For the server, this is the client's host; and for360* the client, it is the server's host. The name may not be361* a fully qualified host name or even a host name at all as362* it may represent a string encoding of the peer's network address.363* If such a name is desired, it might364* be resolved through a name service based on the value returned365* by this method.366* <P>367* This value is not authenticated and should not be relied upon.368* It is mainly used as a hint for {@code SSLSession} caching369* strategies.370*371* @return the host name of the peer host, or null if no information372* is available.373*/374public String getPeerHost();375376/**377* Returns the port number of the peer in this session.378* <P>379* For the server, this is the client's port number; and for380* the client, it is the server's port number.381* <P>382* This value is not authenticated and should not be relied upon.383* It is mainly used as a hint for {@code SSLSession} caching384* strategies.385*386* @return the port number of the peer host, or -1 if no information387* is available.388*389* @since 1.5390*/391public int getPeerPort();392393/**394* Gets the current size of the largest SSL/TLS/DTLS packet that is395* expected when using this session.396* <P>397* An {@code SSLEngine} using this session may generate SSL/TLS/DTLS398* packets of any size up to and including the value returned by this399* method. All {@code SSLEngine} network buffers should be sized400* at least this large to avoid insufficient space problems when401* performing {@code wrap} and {@code unwrap} calls.402*403* @return the current maximum expected network packet size404*405* @see SSLEngine#wrap(ByteBuffer, ByteBuffer)406* @see SSLEngine#unwrap(ByteBuffer, ByteBuffer)407*408* @since 1.5409*/410public int getPacketBufferSize();411412413/**414* Gets the current size of the largest application data that is415* expected when using this session.416* <P>417* {@code SSLEngine} application data buffers must be large418* enough to hold the application data from any inbound network419* application data packet received. Typically, outbound420* application data buffers can be of any size.421*422* @return the current maximum expected application packet size423*424* @see SSLEngine#wrap(ByteBuffer, ByteBuffer)425* @see SSLEngine#unwrap(ByteBuffer, ByteBuffer)426*427* @since 1.5428*/429public int getApplicationBufferSize();430}431432433