Path: blob/master/src/java.base/share/classes/javax/net/ssl/HandshakeCompletedEvent.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.util.EventObject;28import java.security.cert.Certificate;29import java.security.Principal;30import java.security.cert.X509Certificate;3132/**33* This event indicates that an SSL handshake completed on a given34* SSL connection. All of the core information about that handshake's35* result is captured through an "SSLSession" object. As a convenience,36* this event class provides direct access to some important session37* attributes.38*39* <P> The source of this event is the SSLSocket on which handshaking40* just completed.41*42* @see SSLSocket43* @see HandshakeCompletedListener44* @see SSLSession45*46* @since 1.447* @author David Brownell48*/49public class HandshakeCompletedEvent extends EventObject50{51@java.io.Serial52private static final long serialVersionUID = 7914963744257769778L;5354private transient SSLSession session;5556/**57* Constructs a new HandshakeCompletedEvent.58*59* @param sock the SSLSocket acting as the source of the event60* @param s the SSLSession this event is associated with61*/62public HandshakeCompletedEvent(SSLSocket sock, SSLSession s)63{64super(sock);65session = s;66}676869/**70* Returns the session that triggered this event.71*72* @return the <code>SSLSession</code> for this handshake73*/74public SSLSession getSession()75{76return session;77}787980/**81* Returns the cipher suite in use by the session which was produced82* by the handshake. (This is a convenience method for83* getting the ciphersuite from the SSLsession.)84*85* @return the name of the cipher suite negotiated during this session.86*/87public String getCipherSuite()88{89return session.getCipherSuite();90}919293/**94* Returns the certificate(s) that were sent to the peer during95* handshaking.96* Note: This method is useful only when using certificate-based97* cipher suites.98*99* When multiple certificates are available for use in a100* handshake, the implementation chooses what it considers the101* "best" certificate chain available, and transmits that to102* the other side. This method allows the caller to know103* which certificate chain was actually used.104*105* @return an ordered array of certificates, with the local106* certificate first followed by any107* certificate authorities. If no certificates were sent,108* then null is returned.109* @see #getLocalPrincipal()110*/111public java.security.cert.Certificate [] getLocalCertificates()112{113return session.getLocalCertificates();114}115116117/**118* Returns the identity of the peer which was established as part119* of defining the session.120* Note: This method can be used only when using certificate-based121* cipher suites; using it with non-certificate-based cipher suites,122* such as Kerberos, will throw an SSLPeerUnverifiedException.123* <P>124* Note: The returned value may not be a valid certificate chain125* and should not be relied on for trust decisions.126*127* @return an ordered array of the peer certificates,128* with the peer's own certificate first followed by129* any certificate authorities.130* @exception SSLPeerUnverifiedException if the peer is not verified.131* @see #getPeerPrincipal()132*/133public java.security.cert.Certificate [] getPeerCertificates()134throws SSLPeerUnverifiedException135{136return session.getPeerCertificates();137}138139140/**141* Returns the identity of the peer which was identified as part142* of defining the session.143* Note: This method can be used only when using certificate-based144* cipher suites; using it with non-certificate-based cipher suites,145* such as Kerberos, will throw an SSLPeerUnverifiedException.146* <P>147* Note: The returned value may not be a valid certificate chain148* and should not be relied on for trust decisions.149*150* <p><em>Note: this method exists for compatibility with previous151* releases. New applications should use152* {@link #getPeerCertificates} instead.</em></p>153*154* @return an ordered array of peer X.509 certificates,155* with the peer's own certificate first followed by any156* certificate authorities. (The certificates are in157* the original JSSE158* {@link javax.security.cert.X509Certificate} format).159* @throws SSLPeerUnverifiedException if the peer is not verified.160* @throws UnsupportedOperationException if the underlying provider161* does not implement the162* {@link SSLSession#getPeerCertificateChain} operation.163* @see #getPeerPrincipal()164* @deprecated The {@link #getPeerCertificates()} method that returns an165* array of {@code java.security.cert.Certificate} should166* be used instead.167*/168@SuppressWarnings("removal")169@Deprecated(since="9", forRemoval=true)170public javax.security.cert.X509Certificate [] getPeerCertificateChain()171throws SSLPeerUnverifiedException {172return session.getPeerCertificateChain();173}174175/**176* Returns the identity of the peer which was established as part of177* defining the session.178*179* @return the peer's principal. Returns an X500Principal of the180* end-entity certificate for X509-based cipher suites, and181* KerberosPrincipal for Kerberos cipher suites.182*183* @throws SSLPeerUnverifiedException if the peer's identity has not184* been verified185*186* @see #getPeerCertificates()187* @see #getLocalPrincipal()188*189* @since 1.5190*/191public Principal getPeerPrincipal()192throws SSLPeerUnverifiedException193{194Principal principal;195try {196principal = session.getPeerPrincipal();197} catch (AbstractMethodError e) {198// if the provider does not support it, fallback to peer certs.199// return the X500Principal of the end-entity cert.200Certificate[] certs = getPeerCertificates();201principal = ((X509Certificate)certs[0]).getSubjectX500Principal();202}203return principal;204}205206/**207* Returns the principal that was sent to the peer during handshaking.208*209* @return the principal sent to the peer. Returns an X500Principal210* of the end-entity certificate for X509-based cipher suites, and211* KerberosPrincipal for Kerberos cipher suites. If no principal was212* sent, then null is returned.213*214* @see #getLocalCertificates()215* @see #getPeerPrincipal()216*217* @since 1.5218*/219public Principal getLocalPrincipal()220{221Principal principal;222try {223principal = session.getLocalPrincipal();224} catch (AbstractMethodError e) {225principal = null;226// if the provider does not support it, fallback to local certs.227// return the X500Principal of the end-entity cert.228Certificate[] certs = getLocalCertificates();229if (certs != null) {230principal =231((X509Certificate)certs[0]).getSubjectX500Principal();232}233}234return principal;235}236237/**238* Returns the socket which is the source of this event.239* (This is a convenience function, to let applications240* write code without type casts.)241*242* @return the socket on which the connection was made.243*/244public SSLSocket getSocket()245{246return (SSLSocket) getSource();247}248}249250251