Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/net/ssl/HttpsURLConnection.java
41159 views
1
/*
2
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.net.ssl;
27
28
import java.net.URL;
29
import java.net.HttpURLConnection;
30
import java.security.Principal;
31
import java.security.cert.X509Certificate;
32
import java.util.Optional;
33
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 overridden
51
* by calling the appropriate per-instance set method(s) before
52
* <code>connect</code>ing.
53
*
54
* @since 1.4
55
*/
56
public abstract class HttpsURLConnection extends HttpURLConnection {
57
/**
58
* Creates an <code>HttpsURLConnection</code> using the
59
* URL specified.
60
*
61
* @param url the URL
62
*/
63
protected HttpsURLConnection(URL url) {
64
super(url);
65
}
66
67
/**
68
* Returns the cipher suite in use on this connection.
69
*
70
* @return the cipher suite
71
* @throws IllegalStateException if this method is called before
72
* the connection has been established.
73
*/
74
public abstract String getCipherSuite();
75
76
/**
77
* Returns the certificate(s) that were sent to the server during
78
* handshaking.
79
* <P>
80
* Note: This method is useful only when using certificate-based
81
* cipher suites.
82
* <P>
83
* When multiple certificates are available for use in a
84
* handshake, the implementation chooses what it considers the
85
* "best" certificate chain available, and transmits that to
86
* the other side. This method allows the caller to know
87
* which certificate chain was actually sent.
88
*
89
* @return an ordered array of certificates,
90
* with the client's own certificate first followed by any
91
* certificate authorities. If no certificates were sent,
92
* then null is returned.
93
* @throws IllegalStateException if this method is called before
94
* the connection has been established.
95
* @see #getLocalPrincipal()
96
*/
97
public abstract java.security.cert.Certificate [] getLocalCertificates();
98
99
/**
100
* Returns the server's certificate chain which was established
101
* as part of defining the session.
102
* <P>
103
* Note: This method can be used only when using certificate-based
104
* cipher suites; using it with non-certificate-based cipher suites,
105
* such as Kerberos, will throw an SSLPeerUnverifiedException.
106
* <P>
107
* Note: The returned value may not be a valid certificate chain
108
* and should not be relied on for trust decisions.
109
*
110
* @return an ordered array of server certificates,
111
* with the peer's own certificate first followed by
112
* any certificate authorities.
113
* @throws SSLPeerUnverifiedException if the peer is not verified.
114
* @throws IllegalStateException if this method is called before
115
* the connection has been established.
116
* @see #getPeerPrincipal()
117
*/
118
public abstract java.security.cert.Certificate [] getServerCertificates()
119
throws SSLPeerUnverifiedException;
120
121
/**
122
* Returns the server's principal which was established as part of
123
* defining the session.
124
* <P>
125
* Note: Subclasses should override this method. If not overridden, it
126
* will default to returning the X500Principal of the server's end-entity
127
* certificate for certificate-based ciphersuites, or throw an
128
* SSLPeerUnverifiedException for non-certificate based ciphersuites,
129
* such as Kerberos.
130
*
131
* @return the server's principal. Returns an X500Principal of the
132
* end-entity certiticate for X509-based cipher suites, and
133
* KerberosPrincipal for Kerberos cipher suites.
134
*
135
* @throws SSLPeerUnverifiedException if the peer was not verified
136
* @throws IllegalStateException if this method is called before
137
* the connection has been established.
138
*
139
* @see #getServerCertificates()
140
* @see #getLocalPrincipal()
141
*
142
* @since 1.5
143
*/
144
public Principal getPeerPrincipal()
145
throws SSLPeerUnverifiedException {
146
147
java.security.cert.Certificate[] certs = getServerCertificates();
148
return ((X509Certificate)certs[0]).getSubjectX500Principal();
149
}
150
151
/**
152
* Returns the principal that was sent to the server during handshaking.
153
* <P>
154
* Note: Subclasses should override this method. If not overridden, it
155
* will default to returning the X500Principal of the end-entity certificate
156
* that was sent to the server for certificate-based ciphersuites or,
157
* return null for non-certificate based ciphersuites, such as Kerberos.
158
*
159
* @return the principal sent to the server. Returns an X500Principal
160
* of the end-entity certificate for X509-based cipher suites, and
161
* KerberosPrincipal for Kerberos cipher suites. If no principal was
162
* sent, then null is returned.
163
*
164
* @throws IllegalStateException if this method is called before
165
* the connection has been established.
166
*
167
* @see #getLocalCertificates()
168
* @see #getPeerPrincipal()
169
*
170
* @since 1.5
171
*/
172
public Principal getLocalPrincipal() {
173
174
java.security.cert.Certificate[] certs = getLocalCertificates();
175
if (certs != null) {
176
return ((X509Certificate)certs[0]).getSubjectX500Principal();
177
} else {
178
return null;
179
}
180
}
181
182
/**
183
* <code>HostnameVerifier</code> provides a callback mechanism so that
184
* implementers of this interface can supply a policy for
185
* handling the case where the host to connect to and
186
* the server name from the certificate mismatch.
187
* <p>
188
* The default implementation will deny such connections.
189
*/
190
private static HostnameVerifier defaultHostnameVerifier =
191
new DefaultHostnameVerifier();
192
193
/*
194
* The initial default <code>HostnameVerifier</code>. Should be
195
* updated for another other type of <code>HostnameVerifier</code>
196
* that are created.
197
*/
198
private static class DefaultHostnameVerifier
199
implements HostnameVerifier {
200
@Override
201
public boolean verify(String hostname, SSLSession session) {
202
return false;
203
}
204
}
205
206
/**
207
* The <code>hostnameVerifier</code> for this object.
208
*/
209
protected HostnameVerifier hostnameVerifier = defaultHostnameVerifier;
210
211
/**
212
* Sets the default <code>HostnameVerifier</code> inherited by a
213
* new instance of this class.
214
* <P>
215
* If this method is not called, the default
216
* <code>HostnameVerifier</code> assumes the connection should not
217
* be permitted.
218
*
219
* @param v the default host name verifier
220
* @throws IllegalArgumentException if the <code>HostnameVerifier</code>
221
* parameter is null.
222
* @throws SecurityException if a security manager exists and its
223
* <code>checkPermission</code> method does not allow
224
* <code>SSLPermission("setHostnameVerifier")</code>
225
* @see #getDefaultHostnameVerifier()
226
*/
227
public static void setDefaultHostnameVerifier(HostnameVerifier v) {
228
if (v == null) {
229
throw new IllegalArgumentException(
230
"no default HostnameVerifier specified");
231
}
232
233
@SuppressWarnings("removal")
234
SecurityManager sm = System.getSecurityManager();
235
if (sm != null) {
236
sm.checkPermission(new SSLPermission("setHostnameVerifier"));
237
}
238
defaultHostnameVerifier = v;
239
}
240
241
/**
242
* Gets the default <code>HostnameVerifier</code> that is inherited
243
* by new instances of this class.
244
*
245
* @return the default host name verifier
246
* @see #setDefaultHostnameVerifier(HostnameVerifier)
247
*/
248
public static HostnameVerifier getDefaultHostnameVerifier() {
249
return defaultHostnameVerifier;
250
}
251
252
/**
253
* Sets the <code>HostnameVerifier</code> for this instance.
254
* <P>
255
* New instances of this class inherit the default static hostname
256
* verifier set by {@link #setDefaultHostnameVerifier(HostnameVerifier)
257
* setDefaultHostnameVerifier}. Calls to this method replace
258
* this object's <code>HostnameVerifier</code>.
259
*
260
* @param v the host name verifier
261
* @throws IllegalArgumentException if the <code>HostnameVerifier</code>
262
* parameter is null.
263
* @see #getHostnameVerifier()
264
* @see #setDefaultHostnameVerifier(HostnameVerifier)
265
*/
266
public void setHostnameVerifier(HostnameVerifier v) {
267
if (v == null) {
268
throw new IllegalArgumentException(
269
"no HostnameVerifier specified");
270
}
271
272
hostnameVerifier = v;
273
}
274
275
/**
276
* Gets the <code>HostnameVerifier</code> in place on this instance.
277
*
278
* @return the host name verifier
279
* @see #setHostnameVerifier(HostnameVerifier)
280
* @see #setDefaultHostnameVerifier(HostnameVerifier)
281
*/
282
public HostnameVerifier getHostnameVerifier() {
283
return hostnameVerifier;
284
}
285
286
private static SSLSocketFactory defaultSSLSocketFactory = null;
287
288
/**
289
* The <code>SSLSocketFactory</code> inherited when an instance
290
* of this class is created.
291
*/
292
private SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory();
293
294
/**
295
* Sets the default <code>SSLSocketFactory</code> inherited by new
296
* instances of this class.
297
* <P>
298
* The socket factories are used when creating sockets for secure
299
* https URL connections.
300
*
301
* @param sf the default SSL socket factory
302
* @throws IllegalArgumentException if the SSLSocketFactory
303
* parameter is null.
304
* @throws SecurityException if a security manager exists and its
305
* <code>checkSetFactory</code> method does not allow
306
* a socket factory to be specified.
307
* @see #getDefaultSSLSocketFactory()
308
*/
309
public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) {
310
if (sf == null) {
311
throw new IllegalArgumentException(
312
"no default SSLSocketFactory specified");
313
}
314
315
@SuppressWarnings("removal")
316
SecurityManager sm = System.getSecurityManager();
317
if (sm != null) {
318
sm.checkSetFactory();
319
}
320
defaultSSLSocketFactory = sf;
321
}
322
323
/**
324
* Gets the default static <code>SSLSocketFactory</code> that is
325
* inherited by new instances of this class.
326
* <P>
327
* The socket factories are used when creating sockets for secure
328
* https URL connections.
329
*
330
* @return the default <code>SSLSocketFactory</code>
331
* @see #setDefaultSSLSocketFactory(SSLSocketFactory)
332
*/
333
public static SSLSocketFactory getDefaultSSLSocketFactory() {
334
if (defaultSSLSocketFactory == null) {
335
defaultSSLSocketFactory =
336
(SSLSocketFactory)SSLSocketFactory.getDefault();
337
}
338
return defaultSSLSocketFactory;
339
}
340
341
/**
342
* Sets the <code>SSLSocketFactory</code> to be used when this instance
343
* creates sockets for secure https URL connections.
344
* <P>
345
* New instances of this class inherit the default static
346
* <code>SSLSocketFactory</code> set by
347
* {@link #setDefaultSSLSocketFactory(SSLSocketFactory)
348
* setDefaultSSLSocketFactory}. Calls to this method replace
349
* this object's <code>SSLSocketFactory</code>.
350
*
351
* @param sf the SSL socket factory
352
* @throws IllegalArgumentException if the <code>SSLSocketFactory</code>
353
* parameter is null.
354
* @throws SecurityException if a security manager exists and its
355
* <code>checkSetFactory</code> method does not allow
356
* a socket factory to be specified.
357
* @see #getSSLSocketFactory()
358
*/
359
public void setSSLSocketFactory(SSLSocketFactory sf) {
360
if (sf == null) {
361
throw new IllegalArgumentException(
362
"no SSLSocketFactory specified");
363
}
364
365
@SuppressWarnings("removal")
366
SecurityManager sm = System.getSecurityManager();
367
if (sm != null) {
368
sm.checkSetFactory();
369
}
370
sslSocketFactory = sf;
371
}
372
373
/**
374
* Gets the SSL socket factory to be used when creating sockets
375
* for secure https URL connections.
376
*
377
* @return the <code>SSLSocketFactory</code>
378
* @see #setSSLSocketFactory(SSLSocketFactory)
379
*/
380
public SSLSocketFactory getSSLSocketFactory() {
381
return sslSocketFactory;
382
}
383
384
/**
385
* Returns an {@link Optional} containing the {@code SSLSession} in
386
* use on this connection. Returns an empty {@code Optional} if the
387
* underlying implementation does not support this method.
388
*
389
* @implSpec For compatibility, the default implementation of this
390
* method returns an empty {@code Optional}. Subclasses
391
* should override this method with an appropriate
392
* implementation since an application may need to access
393
* additional parameters associated with the SSL session.
394
*
395
* @return an {@link Optional} containing the {@code SSLSession} in
396
* use on this connection.
397
*
398
* @throws IllegalStateException if this method is called before
399
* the connection has been established
400
*
401
* @see SSLSession
402
*
403
* @since 12
404
*/
405
public Optional<SSLSession> getSSLSession() {
406
return Optional.empty();
407
}
408
}
409
410