Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/net/ssl/HttpsURLConnection.java
38918 views
/*1* Copyright (c) 1999, 2012, 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.net.URL;28import java.net.HttpURLConnection;29import java.security.Principal;30import java.security.cert.X509Certificate;3132/**33* <code>HttpsURLConnection</code> extends <code>HttpURLConnection</code>34* with support for https-specific features.35* <P>36* See <A HREF="http://www.w3.org/pub/WWW/Protocols/">37* http://www.w3.org/pub/WWW/Protocols/</A> and38* <A HREF="http://www.ietf.org/"> RFC 2818 </A>39* for more details on the40* https specification.41* <P>42* This class uses <code>HostnameVerifier</code> and43* <code>SSLSocketFactory</code>.44* There are default implementations defined for both classes.45* However, the implementations can be replaced on a per-class (static) or46* per-instance basis. All new <code>HttpsURLConnection</code>s instances47* will be assigned48* the "default" static values at instance creation, but they can be overriden49* by calling the appropriate per-instance set method(s) before50* <code>connect</code>ing.51*52* @since 1.453*/54abstract public55class HttpsURLConnection extends HttpURLConnection56{57/**58* Creates an <code>HttpsURLConnection</code> using the59* URL specified.60*61* @param url the URL62*/63protected HttpsURLConnection(URL url) {64super(url);65}6667/**68* Returns the cipher suite in use on this connection.69*70* @return the cipher suite71* @throws IllegalStateException if this method is called before72* the connection has been established.73*/74public abstract String getCipherSuite();7576/**77* Returns the certificate(s) that were sent to the server during78* handshaking.79* <P>80* Note: This method is useful only when using certificate-based81* cipher suites.82* <P>83* When multiple certificates are available for use in a84* handshake, the implementation chooses what it considers the85* "best" certificate chain available, and transmits that to86* the other side. This method allows the caller to know87* which certificate chain was actually sent.88*89* @return an ordered array of certificates,90* with the client's own certificate first followed by any91* certificate authorities. If no certificates were sent,92* then null is returned.93* @throws IllegalStateException if this method is called before94* the connection has been established.95* @see #getLocalPrincipal()96*/97public abstract java.security.cert.Certificate [] getLocalCertificates();9899/**100* Returns the server's certificate chain which was established101* as part of defining the session.102* <P>103* Note: This method can be used only when using certificate-based104* cipher suites; using it with non-certificate-based cipher suites,105* such as Kerberos, will throw an SSLPeerUnverifiedException.106*107* @return an ordered array of server certificates,108* with the peer's own certificate first followed by109* any certificate authorities.110* @throws SSLPeerUnverifiedException if the peer is not verified.111* @throws IllegalStateException if this method is called before112* the connection has been established.113* @see #getPeerPrincipal()114*/115public abstract java.security.cert.Certificate [] getServerCertificates()116throws SSLPeerUnverifiedException;117118/**119* Returns the server's principal which was established as part of120* defining the session.121* <P>122* Note: Subclasses should override this method. If not overridden, it123* will default to returning the X500Principal of the server's end-entity124* certificate for certificate-based ciphersuites, or throw an125* SSLPeerUnverifiedException for non-certificate based ciphersuites,126* such as Kerberos.127*128* @return the server's principal. Returns an X500Principal of the129* end-entity certiticate for X509-based cipher suites, and130* KerberosPrincipal for Kerberos cipher suites.131*132* @throws SSLPeerUnverifiedException if the peer was not verified133* @throws IllegalStateException if this method is called before134* the connection has been established.135*136* @see #getServerCertificates()137* @see #getLocalPrincipal()138*139* @since 1.5140*/141public Principal getPeerPrincipal()142throws SSLPeerUnverifiedException {143144java.security.cert.Certificate[] certs = getServerCertificates();145return ((X509Certificate)certs[0]).getSubjectX500Principal();146}147148/**149* Returns the principal that was sent to the server during handshaking.150* <P>151* Note: Subclasses should override this method. If not overridden, it152* will default to returning the X500Principal of the end-entity certificate153* that was sent to the server for certificate-based ciphersuites or,154* return null for non-certificate based ciphersuites, such as Kerberos.155*156* @return the principal sent to the server. Returns an X500Principal157* of the end-entity certificate for X509-based cipher suites, and158* KerberosPrincipal for Kerberos cipher suites. If no principal was159* sent, then null is returned.160*161* @throws IllegalStateException if this method is called before162* the connection has been established.163*164* @see #getLocalCertificates()165* @see #getPeerPrincipal()166*167* @since 1.5168*/169public Principal getLocalPrincipal() {170171java.security.cert.Certificate[] certs = getLocalCertificates();172if (certs != null) {173return ((X509Certificate)certs[0]).getSubjectX500Principal();174} else {175return null;176}177}178179/**180* <code>HostnameVerifier</code> provides a callback mechanism so that181* implementers of this interface can supply a policy for182* handling the case where the host to connect to and183* the server name from the certificate mismatch.184* <p>185* The default implementation will deny such connections.186*/187private static HostnameVerifier defaultHostnameVerifier =188new DefaultHostnameVerifier();189190/*191* The initial default <code>HostnameVerifier</code>. Should be192* updated for another other type of <code>HostnameVerifier</code>193* that are created.194*/195private static class DefaultHostnameVerifier196implements HostnameVerifier {197@Override198public boolean verify(String hostname, SSLSession session) {199return false;200}201}202203/**204* The <code>hostnameVerifier</code> for this object.205*/206protected HostnameVerifier hostnameVerifier = defaultHostnameVerifier;207208/**209* Sets the default <code>HostnameVerifier</code> inherited by a210* new instance of this class.211* <P>212* If this method is not called, the default213* <code>HostnameVerifier</code> assumes the connection should not214* be permitted.215*216* @param v the default host name verifier217* @throws IllegalArgumentException if the <code>HostnameVerifier</code>218* parameter is null.219* @throws SecurityException if a security manager exists and its220* <code>checkPermission</code> method does not allow221* <code>SSLPermission("setHostnameVerifier")</code>222* @see #getDefaultHostnameVerifier()223*/224public static void setDefaultHostnameVerifier(HostnameVerifier v) {225if (v == null) {226throw new IllegalArgumentException(227"no default HostnameVerifier specified");228}229230SecurityManager sm = System.getSecurityManager();231if (sm != null) {232sm.checkPermission(new SSLPermission("setHostnameVerifier"));233}234defaultHostnameVerifier = v;235}236237/**238* Gets the default <code>HostnameVerifier</code> that is inherited239* by new instances of this class.240*241* @return the default host name verifier242* @see #setDefaultHostnameVerifier(HostnameVerifier)243*/244public static HostnameVerifier getDefaultHostnameVerifier() {245return defaultHostnameVerifier;246}247248/**249* Sets the <code>HostnameVerifier</code> for this instance.250* <P>251* New instances of this class inherit the default static hostname252* verifier set by {@link #setDefaultHostnameVerifier(HostnameVerifier)253* setDefaultHostnameVerifier}. Calls to this method replace254* this object's <code>HostnameVerifier</code>.255*256* @param v the host name verifier257* @throws IllegalArgumentException if the <code>HostnameVerifier</code>258* parameter is null.259* @see #getHostnameVerifier()260* @see #setDefaultHostnameVerifier(HostnameVerifier)261*/262public void setHostnameVerifier(HostnameVerifier v) {263if (v == null) {264throw new IllegalArgumentException(265"no HostnameVerifier specified");266}267268hostnameVerifier = v;269}270271/**272* Gets the <code>HostnameVerifier</code> in place on this instance.273*274* @return the host name verifier275* @see #setHostnameVerifier(HostnameVerifier)276* @see #setDefaultHostnameVerifier(HostnameVerifier)277*/278public HostnameVerifier getHostnameVerifier() {279return hostnameVerifier;280}281282private static SSLSocketFactory defaultSSLSocketFactory = null;283284/**285* The <code>SSLSocketFactory</code> inherited when an instance286* of this class is created.287*/288private SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory();289290/**291* Sets the default <code>SSLSocketFactory</code> inherited by new292* instances of this class.293* <P>294* The socket factories are used when creating sockets for secure295* https URL connections.296*297* @param sf the default SSL socket factory298* @throws IllegalArgumentException if the SSLSocketFactory299* parameter is null.300* @throws SecurityException if a security manager exists and its301* <code>checkSetFactory</code> method does not allow302* a socket factory to be specified.303* @see #getDefaultSSLSocketFactory()304*/305public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) {306if (sf == null) {307throw new IllegalArgumentException(308"no default SSLSocketFactory specified");309}310311SecurityManager sm = System.getSecurityManager();312if (sm != null) {313sm.checkSetFactory();314}315defaultSSLSocketFactory = sf;316}317318/**319* Gets the default static <code>SSLSocketFactory</code> that is320* inherited by new instances of this class.321* <P>322* The socket factories are used when creating sockets for secure323* https URL connections.324*325* @return the default <code>SSLSocketFactory</code>326* @see #setDefaultSSLSocketFactory(SSLSocketFactory)327*/328public static SSLSocketFactory getDefaultSSLSocketFactory() {329if (defaultSSLSocketFactory == null) {330defaultSSLSocketFactory =331(SSLSocketFactory)SSLSocketFactory.getDefault();332}333return defaultSSLSocketFactory;334}335336/**337* Sets the <code>SSLSocketFactory</code> to be used when this instance338* creates sockets for secure https URL connections.339* <P>340* New instances of this class inherit the default static341* <code>SSLSocketFactory</code> set by342* {@link #setDefaultSSLSocketFactory(SSLSocketFactory)343* setDefaultSSLSocketFactory}. Calls to this method replace344* this object's <code>SSLSocketFactory</code>.345*346* @param sf the SSL socket factory347* @throws IllegalArgumentException if the <code>SSLSocketFactory</code>348* parameter is null.349* @throws SecurityException if a security manager exists and its350* <code>checkSetFactory</code> method does not allow351* a socket factory to be specified.352* @see #getSSLSocketFactory()353*/354public void setSSLSocketFactory(SSLSocketFactory sf) {355if (sf == null) {356throw new IllegalArgumentException(357"no SSLSocketFactory specified");358}359360SecurityManager sm = System.getSecurityManager();361if (sm != null) {362sm.checkSetFactory();363}364sslSocketFactory = sf;365}366367/**368* Gets the SSL socket factory to be used when creating sockets369* for secure https URL connections.370*371* @return the <code>SSLSocketFactory</code>372* @see #setSSLSocketFactory(SSLSocketFactory)373*/374public SSLSocketFactory getSSLSocketFactory() {375return sslSocketFactory;376}377}378379380