blob: 58e35912626d5d521b17b9a9c130c392bfafffbc [file] [log] [blame]
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00001/*
Narayan Kamath2c87ad32015-12-21 13:53:32 +00002 * Copyright (C) 2014 The Android Open Source Project
Przemyslaw Szczepaniak89e6fad2017-03-29 17:17:34 +01003 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +00004 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation. Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26
27package javax.net.ssl;
28
29import java.net.URL;
30import java.net.HttpURLConnection;
31import java.security.Principal;
32import java.security.cert.X509Certificate;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +000033
34/**
35 * <code>HttpsURLConnection</code> extends <code>HttpURLConnection</code>
36 * with support for https-specific features.
37 * <P>
38 * See <A HREF="http://www.w3.org/pub/WWW/Protocols/">
39 * http://www.w3.org/pub/WWW/Protocols/</A> and
40 * <A HREF="http://www.ietf.org/"> RFC 2818 </A>
41 * for more details on the
42 * https specification.
43 * <P>
44 * This class uses <code>HostnameVerifier</code> and
45 * <code>SSLSocketFactory</code>.
46 * There are default implementations defined for both classes.
47 * However, the implementations can be replaced on a per-class (static) or
48 * per-instance basis. All new <code>HttpsURLConnection</code>s instances
49 * will be assigned
50 * the "default" static values at instance creation, but they can be overriden
51 * by calling the appropriate per-instance set method(s) before
52 * <code>connect</code>ing.
53 *
54 * @since 1.4
55 */
56abstract public
57class HttpsURLConnection extends HttpURLConnection
58{
59 /**
60 * Creates an <code>HttpsURLConnection</code> using the
61 * URL specified.
62 *
63 * @param url the URL
64 */
65 protected HttpsURLConnection(URL url) {
66 super(url);
67 }
68
69 /**
70 * Returns the cipher suite in use on this connection.
71 *
72 * @return the cipher suite
73 * @throws IllegalStateException if this method is called before
74 * the connection has been established.
75 */
76 public abstract String getCipherSuite();
77
78 /**
79 * Returns the certificate(s) that were sent to the server during
80 * handshaking.
81 * <P>
82 * Note: This method is useful only when using certificate-based
83 * cipher suites.
84 * <P>
85 * When multiple certificates are available for use in a
86 * handshake, the implementation chooses what it considers the
87 * "best" certificate chain available, and transmits that to
88 * the other side. This method allows the caller to know
89 * which certificate chain was actually sent.
90 *
91 * @return an ordered array of certificates,
92 * with the client's own certificate first followed by any
93 * certificate authorities. If no certificates were sent,
94 * then null is returned.
95 * @throws IllegalStateException if this method is called before
96 * the connection has been established.
97 * @see #getLocalPrincipal()
98 */
99 public abstract java.security.cert.Certificate [] getLocalCertificates();
100
101 /**
102 * Returns the server's certificate chain which was established
103 * as part of defining the session.
104 * <P>
105 * Note: This method can be used only when using certificate-based
106 * cipher suites; using it with non-certificate-based cipher suites,
107 * such as Kerberos, will throw an SSLPeerUnverifiedException.
108 *
109 * @return an ordered array of server certificates,
110 * with the peer's own certificate first followed by
111 * any certificate authorities.
112 * @throws SSLPeerUnverifiedException if the peer is not verified.
113 * @throws IllegalStateException if this method is called before
114 * the connection has been established.
115 * @see #getPeerPrincipal()
116 */
117 public abstract java.security.cert.Certificate [] getServerCertificates()
118 throws SSLPeerUnverifiedException;
119
120 /**
121 * Returns the server's principal which was established as part of
122 * defining the session.
123 * <P>
124 * Note: Subclasses should override this method. If not overridden, it
125 * will default to returning the X500Principal of the server's end-entity
126 * certificate for certificate-based ciphersuites, or throw an
127 * SSLPeerUnverifiedException for non-certificate based ciphersuites,
128 * such as Kerberos.
129 *
130 * @return the server's principal. Returns an X500Principal of the
131 * end-entity certiticate for X509-based cipher suites, and
132 * KerberosPrincipal for Kerberos cipher suites.
133 *
134 * @throws SSLPeerUnverifiedException if the peer was not verified
135 * @throws IllegalStateException if this method is called before
136 * the connection has been established.
137 *
138 * @see #getServerCertificates()
139 * @see #getLocalPrincipal()
140 *
141 * @since 1.5
142 */
143 public Principal getPeerPrincipal()
144 throws SSLPeerUnverifiedException {
145
146 java.security.cert.Certificate[] certs = getServerCertificates();
Przemyslaw Szczepaniak89e6fad2017-03-29 17:17:34 +0100147 return ((X509Certificate)certs[0]).getSubjectX500Principal();
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000148 }
149
150 /**
151 * Returns the principal that was sent to the server during handshaking.
152 * <P>
153 * Note: Subclasses should override this method. If not overridden, it
154 * will default to returning the X500Principal of the end-entity certificate
155 * that was sent to the server for certificate-based ciphersuites or,
156 * return null for non-certificate based ciphersuites, such as Kerberos.
157 *
158 * @return the principal sent to the server. Returns an X500Principal
159 * of the end-entity certificate for X509-based cipher suites, and
160 * KerberosPrincipal for Kerberos cipher suites. If no principal was
161 * sent, then null is returned.
162 *
163 * @throws IllegalStateException if this method is called before
164 * the connection has been established.
165 *
166 * @see #getLocalCertificates()
167 * @see #getPeerPrincipal()
168 *
169 * @since 1.5
170 */
171 public Principal getLocalPrincipal() {
172
173 java.security.cert.Certificate[] certs = getLocalCertificates();
174 if (certs != null) {
Przemyslaw Szczepaniak89e6fad2017-03-29 17:17:34 +0100175 return ((X509Certificate)certs[0]).getSubjectX500Principal();
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000176 } else {
177 return null;
178 }
179 }
180
Tobias Thiererae3e6a42018-05-01 14:10:47 +0100181 // BEGIN Android-changed: Use holder class idiom for a lazily-created OkHttp hostname verifier.
Adam Vartanian09828c02017-05-10 13:50:54 +0100182 // The RI default hostname verifier is a static member of the class, which means
183 // it's created when the class is initialized. As well, its default verifier
184 // just fails all verification attempts, whereas we use OkHttp's verifier.
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100185 /*
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000186 * Holds the default instance so class preloading doesn't create an instance of
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100187 * it.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000188 */
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100189 private static class NoPreloadHolder {
190 public static HostnameVerifier defaultHostnameVerifier;
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000191 public static final Class<? extends HostnameVerifier> originalDefaultHostnameVerifierClass;
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100192 static {
193 try {
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000194 /**
195 * <code>HostnameVerifier</code> provides a callback mechanism so that
196 * implementers of this interface can supply a policy for
197 * handling the case where the host to connect to and
198 * the server name from the certificate mismatch.
199 */
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100200 defaultHostnameVerifier = (HostnameVerifier)
201 Class.forName("com.android.okhttp.internal.tls.OkHostnameVerifier")
202 .getField("INSTANCE").get(null);
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000203 originalDefaultHostnameVerifierClass = defaultHostnameVerifier.getClass();
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100204 } catch (Exception e) {
205 throw new AssertionError("Failed to obtain okhttp HostnameVerifier", e);
206 }
207 }
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100208 }
209
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000210 /**
211 * The <code>hostnameVerifier</code> for this object.
212 */
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000213 protected HostnameVerifier hostnameVerifier;
Tobias Thiererae3e6a42018-05-01 14:10:47 +0100214 // END Android-changed: Use holder class idiom for a lazily-created OkHttp hostname verifier.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000215
216 /**
217 * Sets the default <code>HostnameVerifier</code> inherited by a
218 * new instance of this class.
219 * <P>
220 * If this method is not called, the default
221 * <code>HostnameVerifier</code> assumes the connection should not
222 * be permitted.
223 *
224 * @param v the default host name verifier
225 * @throws IllegalArgumentException if the <code>HostnameVerifier</code>
226 * parameter is null.
227 * @throws SecurityException if a security manager exists and its
228 * <code>checkPermission</code> method does not allow
229 * <code>SSLPermission("setHostnameVerifier")</code>
230 * @see #getDefaultHostnameVerifier()
231 */
232 public static void setDefaultHostnameVerifier(HostnameVerifier v) {
233 if (v == null) {
234 throw new IllegalArgumentException(
235 "no default HostnameVerifier specified");
236 }
237
238 SecurityManager sm = System.getSecurityManager();
239 if (sm != null) {
240 sm.checkPermission(new SSLPermission("setHostnameVerifier"));
241 }
Tobias Thiererae3e6a42018-05-01 14:10:47 +0100242 // Android-changed: Use holder class idiom for a lazily-created OkHttp hostname verifier.
243 // defaultHostnameVerifier = v;
Przemyslaw Szczepaniak419d15f2015-09-28 16:24:00 +0100244 NoPreloadHolder.defaultHostnameVerifier = v;
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000245 }
246
247 /**
248 * Gets the default <code>HostnameVerifier</code> that is inherited
249 * by new instances of this class.
250 *
251 * @return the default host name verifier
252 * @see #setDefaultHostnameVerifier(HostnameVerifier)
253 */
254 public static HostnameVerifier getDefaultHostnameVerifier() {
Tobias Thiererae3e6a42018-05-01 14:10:47 +0100255 // Android-changed: Use holder class idiom for a lazily-created OkHttp hostname verifier.
256 // return defaultHostnameVerifier;
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000257 return NoPreloadHolder.defaultHostnameVerifier;
Piotr Jastrzebskif7ab2bc2015-05-06 14:00:00 +0100258 }
259
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000260 /**
261 * Sets the <code>HostnameVerifier</code> for this instance.
262 * <P>
263 * New instances of this class inherit the default static hostname
264 * verifier set by {@link #setDefaultHostnameVerifier(HostnameVerifier)
265 * setDefaultHostnameVerifier}. Calls to this method replace
266 * this object's <code>HostnameVerifier</code>.
267 *
268 * @param v the host name verifier
269 * @throws IllegalArgumentException if the <code>HostnameVerifier</code>
270 * parameter is null.
271 * @see #getHostnameVerifier()
272 * @see #setDefaultHostnameVerifier(HostnameVerifier)
273 */
274 public void setHostnameVerifier(HostnameVerifier v) {
275 if (v == null) {
276 throw new IllegalArgumentException(
277 "no HostnameVerifier specified");
278 }
279
280 hostnameVerifier = v;
281 }
282
283 /**
284 * Gets the <code>HostnameVerifier</code> in place on this instance.
285 *
286 * @return the host name verifier
287 * @see #setHostnameVerifier(HostnameVerifier)
288 * @see #setDefaultHostnameVerifier(HostnameVerifier)
289 */
290 public HostnameVerifier getHostnameVerifier() {
Tobias Thiererae3e6a42018-05-01 14:10:47 +0100291 // Android-added: Use the default verifier if none is set.
292 // Note that this also has the side effect of *setting* (if unset)
293 // hostnameVerifier to be the default one. It's not clear why this
294 // was done (commit abd00f0eaa46f71f98e75a631c268c812d1ec7c1) but
295 // we're keeping this behavior for lack of a strong reason to do
296 // otherwise.
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000297 if (hostnameVerifier == null) {
Sergio Giroabd00f02015-11-27 14:47:50 +0000298 hostnameVerifier = NoPreloadHolder.defaultHostnameVerifier;
Sergio Giro2e1b9c62015-11-25 15:59:03 +0000299 }
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000300 return hostnameVerifier;
301 }
302
303 private static SSLSocketFactory defaultSSLSocketFactory = null;
304
305 /**
306 * The <code>SSLSocketFactory</code> inherited when an instance
307 * of this class is created.
308 */
309 private SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory();
310
311 /**
312 * Sets the default <code>SSLSocketFactory</code> inherited by new
313 * instances of this class.
314 * <P>
315 * The socket factories are used when creating sockets for secure
316 * https URL connections.
317 *
318 * @param sf the default SSL socket factory
319 * @throws IllegalArgumentException if the SSLSocketFactory
320 * parameter is null.
321 * @throws SecurityException if a security manager exists and its
322 * <code>checkSetFactory</code> method does not allow
323 * a socket factory to be specified.
324 * @see #getDefaultSSLSocketFactory()
325 */
326 public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) {
327 if (sf == null) {
328 throw new IllegalArgumentException(
329 "no default SSLSocketFactory specified");
330 }
331
332 SecurityManager sm = System.getSecurityManager();
333 if (sm != null) {
334 sm.checkSetFactory();
335 }
336 defaultSSLSocketFactory = sf;
337 }
338
339 /**
340 * Gets the default static <code>SSLSocketFactory</code> that is
341 * inherited by new instances of this class.
342 * <P>
343 * The socket factories are used when creating sockets for secure
344 * https URL connections.
345 *
346 * @return the default <code>SSLSocketFactory</code>
347 * @see #setDefaultSSLSocketFactory(SSLSocketFactory)
348 */
349 public static SSLSocketFactory getDefaultSSLSocketFactory() {
350 if (defaultSSLSocketFactory == null) {
351 defaultSSLSocketFactory =
352 (SSLSocketFactory)SSLSocketFactory.getDefault();
353 }
354 return defaultSSLSocketFactory;
355 }
356
357 /**
358 * Sets the <code>SSLSocketFactory</code> to be used when this instance
359 * creates sockets for secure https URL connections.
360 * <P>
361 * New instances of this class inherit the default static
362 * <code>SSLSocketFactory</code> set by
363 * {@link #setDefaultSSLSocketFactory(SSLSocketFactory)
364 * setDefaultSSLSocketFactory}. Calls to this method replace
365 * this object's <code>SSLSocketFactory</code>.
366 *
367 * @param sf the SSL socket factory
368 * @throws IllegalArgumentException if the <code>SSLSocketFactory</code>
369 * parameter is null.
Tobias Thiererae3e6a42018-05-01 14:10:47 +0100370 * @throws SecurityException if a security manager exists and its
371 * <code>checkSetFactory</code> method does not allow
372 * a socket factory to be specified.
Piotr Jastrzebski51b1b692015-02-16 15:01:09 +0000373 * @see #getSSLSocketFactory()
374 */
375 public void setSSLSocketFactory(SSLSocketFactory sf) {
376 if (sf == null) {
377 throw new IllegalArgumentException(
378 "no SSLSocketFactory specified");
379 }
380
381 SecurityManager sm = System.getSecurityManager();
382 if (sm != null) {
383 sm.checkSetFactory();
384 }
385 sslSocketFactory = sf;
386 }
387
388 /**
389 * Gets the SSL socket factory to be used when creating sockets
390 * for secure https URL connections.
391 *
392 * @return the <code>SSLSocketFactory</code>
393 * @see #setSSLSocketFactory(SSLSocketFactory)
394 */
395 public SSLSocketFactory getSSLSocketFactory() {
396 return sslSocketFactory;
397 }
398}