0N/A/*
4467N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.net.ssl;
0N/A
0N/Aimport java.net.URL;
0N/Aimport java.net.HttpURLConnection;
0N/Aimport java.security.Principal;
0N/Aimport java.security.cert.X509Certificate;
0N/Aimport javax.security.auth.x500.X500Principal;
0N/A
0N/A/**
0N/A * <code>HttpsURLConnection</code> extends <code>HttpURLConnection</code>
0N/A * with support for https-specific features.
0N/A * <P>
0N/A * See <A HREF="http://www.w3.org/pub/WWW/Protocols/">
0N/A * http://www.w3.org/pub/WWW/Protocols/</A> and
0N/A * <A HREF="http://www.ietf.org/"> RFC 2818 </A>
0N/A * for more details on the
0N/A * https specification.
0N/A * <P>
0N/A * This class uses <code>HostnameVerifier</code> and
0N/A * <code>SSLSocketFactory</code>.
0N/A * There are default implementations defined for both classes.
0N/A * However, the implementations can be replaced on a per-class (static) or
0N/A * per-instance basis. All new <code>HttpsURLConnection</code>s instances
0N/A * will be assigned
0N/A * the "default" static values at instance creation, but they can be overriden
0N/A * by calling the appropriate per-instance set method(s) before
0N/A * <code>connect</code>ing.
0N/A *
0N/A * @since 1.4
0N/A */
0N/Aabstract public
0N/Aclass HttpsURLConnection extends HttpURLConnection
0N/A{
0N/A /**
0N/A * Creates an <code>HttpsURLConnection</code> using the
0N/A * URL specified.
0N/A *
0N/A * @param url the URL
0N/A */
0N/A protected HttpsURLConnection(URL url) {
0N/A super(url);
0N/A }
0N/A
0N/A /**
0N/A * Returns the cipher suite in use on this connection.
0N/A *
0N/A * @return the cipher suite
0N/A * @throws IllegalStateException if this method is called before
0N/A * the connection has been established.
0N/A */
0N/A public abstract String getCipherSuite();
0N/A
0N/A /**
0N/A * Returns the certificate(s) that were sent to the server during
0N/A * handshaking.
0N/A * <P>
0N/A * Note: This method is useful only when using certificate-based
0N/A * cipher suites.
0N/A * <P>
0N/A * When multiple certificates are available for use in a
0N/A * handshake, the implementation chooses what it considers the
0N/A * "best" certificate chain available, and transmits that to
0N/A * the other side. This method allows the caller to know
0N/A * which certificate chain was actually sent.
0N/A *
0N/A * @return an ordered array of certificates,
0N/A * with the client's own certificate first followed by any
0N/A * certificate authorities. If no certificates were sent,
0N/A * then null is returned.
0N/A * @throws IllegalStateException if this method is called before
0N/A * the connection has been established.
0N/A * @see #getLocalPrincipal()
0N/A */
0N/A public abstract java.security.cert.Certificate [] getLocalCertificates();
0N/A
0N/A /**
0N/A * Returns the server's certificate chain which was established
0N/A * as part of defining the session.
0N/A * <P>
0N/A * Note: This method can be used only when using certificate-based
0N/A * cipher suites; using it with non-certificate-based cipher suites,
0N/A * such as Kerberos, will throw an SSLPeerUnverifiedException.
0N/A *
0N/A * @return an ordered array of server certificates,
0N/A * with the peer's own certificate first followed by
0N/A * any certificate authorities.
0N/A * @throws SSLPeerUnverifiedException if the peer is not verified.
0N/A * @throws IllegalStateException if this method is called before
0N/A * the connection has been established.
0N/A * @see #getPeerPrincipal()
0N/A */
0N/A public abstract java.security.cert.Certificate [] getServerCertificates()
0N/A throws SSLPeerUnverifiedException;
0N/A
0N/A /**
0N/A * Returns the server's principal which was established as part of
0N/A * defining the session.
0N/A * <P>
0N/A * Note: Subclasses should override this method. If not overridden, it
0N/A * will default to returning the X500Principal of the server's end-entity
0N/A * certificate for certificate-based ciphersuites, or throw an
0N/A * SSLPeerUnverifiedException for non-certificate based ciphersuites,
0N/A * such as Kerberos.
0N/A *
0N/A * @return the server's principal. Returns an X500Principal of the
0N/A * end-entity certiticate for X509-based cipher suites, and
0N/A * KerberosPrincipal for Kerberos cipher suites.
0N/A *
0N/A * @throws SSLPeerUnverifiedException if the peer was not verified
0N/A * @throws IllegalStateException if this method is called before
0N/A * the connection has been established.
0N/A *
0N/A * @see #getServerCertificates()
0N/A * @see #getLocalPrincipal()
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public Principal getPeerPrincipal()
0N/A throws SSLPeerUnverifiedException {
0N/A
0N/A java.security.cert.Certificate[] certs = getServerCertificates();
0N/A return ((X500Principal)
0N/A ((X509Certificate)certs[0]).getSubjectX500Principal());
0N/A }
0N/A
0N/A /**
0N/A * Returns the principal that was sent to the server during handshaking.
0N/A * <P>
0N/A * Note: Subclasses should override this method. If not overridden, it
0N/A * will default to returning the X500Principal of the end-entity certificate
0N/A * that was sent to the server for certificate-based ciphersuites or,
0N/A * return null for non-certificate based ciphersuites, such as Kerberos.
0N/A *
0N/A * @return the principal sent to the server. Returns an X500Principal
0N/A * of the end-entity certificate for X509-based cipher suites, and
0N/A * KerberosPrincipal for Kerberos cipher suites. If no principal was
0N/A * sent, then null is returned.
0N/A *
0N/A * @throws IllegalStateException if this method is called before
0N/A * the connection has been established.
0N/A *
0N/A * @see #getLocalCertificates()
0N/A * @see #getPeerPrincipal()
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public Principal getLocalPrincipal() {
0N/A
0N/A java.security.cert.Certificate[] certs = getLocalCertificates();
0N/A if (certs != null) {
0N/A return ((X500Principal)
0N/A ((X509Certificate)certs[0]).getSubjectX500Principal());
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * <code>HostnameVerifier</code> provides a callback mechanism so that
0N/A * implementers of this interface can supply a policy for
0N/A * handling the case where the host to connect to and
0N/A * the server name from the certificate mismatch.
0N/A * <p>
0N/A * The default implementation will deny such connections.
0N/A */
3002N/A private static HostnameVerifier defaultHostnameVerifier =
3002N/A new DefaultHostnameVerifier();
0N/A
0N/A /*
0N/A * The initial default <code>HostnameVerifier</code>. Should be
0N/A * updated for another other type of <code>HostnameVerifier</code>
0N/A * that are created.
0N/A */
0N/A private static class DefaultHostnameVerifier
0N/A implements HostnameVerifier {
0N/A public boolean verify(String hostname, SSLSession session) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The <code>hostnameVerifier</code> for this object.
0N/A */
0N/A protected HostnameVerifier hostnameVerifier = defaultHostnameVerifier;
0N/A
0N/A /**
0N/A * Sets the default <code>HostnameVerifier</code> inherited by a
0N/A * new instance of this class.
0N/A * <P>
0N/A * If this method is not called, the default
0N/A * <code>HostnameVerifier</code> assumes the connection should not
0N/A * be permitted.
0N/A *
0N/A * @param v the default host name verifier
0N/A * @throws IllegalArgumentException if the <code>HostnameVerifier</code>
0N/A * parameter is null.
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkPermission</code> method does not allow
0N/A * <code>SSLPermission("setHostnameVerifier")</code>
0N/A * @see #getDefaultHostnameVerifier()
0N/A */
0N/A public static void setDefaultHostnameVerifier(HostnameVerifier v) {
0N/A if (v == null) {
0N/A throw new IllegalArgumentException(
0N/A "no default HostnameVerifier specified");
0N/A }
0N/A
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPermission(new SSLPermission("setHostnameVerifier"));
0N/A }
0N/A defaultHostnameVerifier = v;
0N/A }
0N/A
0N/A /**
0N/A * Gets the default <code>HostnameVerifier</code> that is inherited
0N/A * by new instances of this class.
0N/A *
0N/A * @return the default host name verifier
0N/A * @see #setDefaultHostnameVerifier(HostnameVerifier)
0N/A */
0N/A public static HostnameVerifier getDefaultHostnameVerifier() {
0N/A return defaultHostnameVerifier;
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>HostnameVerifier</code> for this instance.
0N/A * <P>
0N/A * New instances of this class inherit the default static hostname
0N/A * verifier set by {@link #setDefaultHostnameVerifier(HostnameVerifier)
0N/A * setDefaultHostnameVerifier}. Calls to this method replace
0N/A * this object's <code>HostnameVerifier</code>.
0N/A *
0N/A * @param v the host name verifier
0N/A * @throws IllegalArgumentException if the <code>HostnameVerifier</code>
0N/A * parameter is null.
0N/A * @see #getHostnameVerifier()
0N/A * @see #setDefaultHostnameVerifier(HostnameVerifier)
0N/A */
0N/A public void setHostnameVerifier(HostnameVerifier v) {
0N/A if (v == null) {
0N/A throw new IllegalArgumentException(
0N/A "no HostnameVerifier specified");
0N/A }
0N/A
0N/A hostnameVerifier = v;
0N/A }
0N/A
0N/A /**
0N/A * Gets the <code>HostnameVerifier</code> in place on this instance.
0N/A *
0N/A * @return the host name verifier
0N/A * @see #setHostnameVerifier(HostnameVerifier)
0N/A * @see #setDefaultHostnameVerifier(HostnameVerifier)
0N/A */
0N/A public HostnameVerifier getHostnameVerifier() {
0N/A return hostnameVerifier;
0N/A }
0N/A
0N/A private static SSLSocketFactory defaultSSLSocketFactory = null;
0N/A
0N/A /**
0N/A * The <code>SSLSocketFactory</code> inherited when an instance
0N/A * of this class is created.
0N/A */
0N/A private SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory();
0N/A
0N/A /**
0N/A * Sets the default <code>SSLSocketFactory</code> inherited by new
0N/A * instances of this class.
0N/A * <P>
0N/A * The socket factories are used when creating sockets for secure
0N/A * https URL connections.
0N/A *
0N/A * @param sf the default SSL socket factory
0N/A * @throws IllegalArgumentException if the SSLSocketFactory
0N/A * parameter is null.
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkSetFactory</code> method does not allow
0N/A * a socket factory to be specified.
0N/A * @see #getDefaultSSLSocketFactory()
0N/A */
0N/A public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) {
0N/A if (sf == null) {
0N/A throw new IllegalArgumentException(
0N/A "no default SSLSocketFactory specified");
0N/A }
0N/A
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkSetFactory();
0N/A }
0N/A defaultSSLSocketFactory = sf;
0N/A }
0N/A
0N/A /**
0N/A * Gets the default static <code>SSLSocketFactory</code> that is
0N/A * inherited by new instances of this class.
0N/A * <P>
0N/A * The socket factories are used when creating sockets for secure
0N/A * https URL connections.
0N/A *
0N/A * @return the default <code>SSLSocketFactory</code>
0N/A * @see #setDefaultSSLSocketFactory(SSLSocketFactory)
0N/A */
0N/A public static SSLSocketFactory getDefaultSSLSocketFactory() {
0N/A if (defaultSSLSocketFactory == null) {
0N/A defaultSSLSocketFactory =
0N/A (SSLSocketFactory)SSLSocketFactory.getDefault();
0N/A }
0N/A return defaultSSLSocketFactory;
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>SSLSocketFactory</code> to be used when this instance
0N/A * creates sockets for secure https URL connections.
0N/A * <P>
0N/A * New instances of this class inherit the default static
0N/A * <code>SSLSocketFactory</code> set by
0N/A * {@link #setDefaultSSLSocketFactory(SSLSocketFactory)
0N/A * setDefaultSSLSocketFactory}. Calls to this method replace
0N/A * this object's <code>SSLSocketFactory</code>.
0N/A *
0N/A * @param sf the SSL socket factory
0N/A * @throws IllegalArgumentException if the <code>SSLSocketFactory</code>
0N/A * parameter is null.
0N/A * @see #getSSLSocketFactory()
0N/A */
0N/A public void setSSLSocketFactory(SSLSocketFactory sf) {
0N/A if (sf == null) {
0N/A throw new IllegalArgumentException(
0N/A "no SSLSocketFactory specified");
0N/A }
0N/A
4467N/A SecurityManager sm = System.getSecurityManager();
4467N/A if (sm != null) {
4467N/A sm.checkSetFactory();
4467N/A }
0N/A sslSocketFactory = sf;
0N/A }
0N/A
0N/A /**
0N/A * Gets the SSL socket factory to be used when creating sockets
0N/A * for secure https URL connections.
0N/A *
0N/A * @return the <code>SSLSocketFactory</code>
0N/A * @see #setSSLSocketFactory(SSLSocketFactory)
0N/A */
0N/A public SSLSocketFactory getSSLSocketFactory() {
0N/A return sslSocketFactory;
0N/A }
0N/A}