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/SSLSocketFactory.java
41159 views
1
/*
2
* Copyright (c) 1997, 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
27
package javax.net.ssl;
28
29
import java.net.*;
30
import javax.net.SocketFactory;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.security.*;
34
import java.util.Locale;
35
36
import sun.security.action.GetPropertyAction;
37
38
/**
39
* <code>SSLSocketFactory</code>s create <code>SSLSocket</code>s.
40
*
41
* @since 1.4
42
* @see SSLSocket
43
* @author David Brownell
44
*/
45
public abstract class SSLSocketFactory extends SocketFactory {
46
static final boolean DEBUG;
47
48
static {
49
String s = GetPropertyAction.privilegedGetProperty(
50
"javax.net.debug", "").toLowerCase(Locale.ENGLISH);
51
DEBUG = s.contains("all") || s.contains("ssl");
52
}
53
54
/**
55
* Constructor is used only by subclasses.
56
*/
57
public SSLSocketFactory() {
58
// blank
59
}
60
61
/**
62
* Returns the default SSL socket factory.
63
*
64
* <p>The first time this method is called, the security property
65
* "ssl.SocketFactory.provider" is examined. If it is non-null, a class by
66
* that name is loaded and instantiated. If that is successful and the
67
* object is an instance of SSLSocketFactory, it is made the default SSL
68
* socket factory.
69
*
70
* <p>Otherwise, this method returns
71
* <code>SSLContext.getDefault().getSocketFactory()</code>. If that
72
* call fails, an inoperative factory is returned.
73
*
74
* @return the default <code>SocketFactory</code>
75
* @see SSLContext#getDefault
76
*/
77
public static SocketFactory getDefault() {
78
if (DefaultFactoryHolder.defaultFactory != null) {
79
return DefaultFactoryHolder.defaultFactory;
80
}
81
82
try {
83
return SSLContext.getDefault().getSocketFactory();
84
} catch (NoSuchAlgorithmException | UnsupportedOperationException e) {
85
return new DefaultSSLSocketFactory(e);
86
}
87
}
88
89
@SuppressWarnings("removal")
90
static String getSecurityProperty(final String name) {
91
return AccessController.doPrivileged(new PrivilegedAction<>() {
92
@Override
93
public String run() {
94
String s = java.security.Security.getProperty(name);
95
if (s != null) {
96
s = s.trim();
97
if (s.isEmpty()) {
98
s = null;
99
}
100
}
101
return s;
102
}
103
});
104
}
105
106
/**
107
* Returns the list of cipher suites which are enabled by default.
108
* Unless a different list is enabled, handshaking on an SSL connection
109
* will use one of these cipher suites. The minimum quality of service
110
* for these defaults requires confidentiality protection and server
111
* authentication (that is, no anonymous cipher suites).
112
* <P>
113
* The returned array includes cipher suites from the list of standard
114
* cipher suite names in the <a href=
115
* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">
116
* JSSE Cipher Suite Names</a> section of the Java Cryptography
117
* Architecture Standard Algorithm Name Documentation, and may also
118
* include other cipher suites that the provider supports.
119
*
120
* @see #getSupportedCipherSuites()
121
* @return array of the cipher suites enabled by default
122
*/
123
public abstract String [] getDefaultCipherSuites();
124
125
/**
126
* Returns the names of the cipher suites which could be enabled for use
127
* on an SSL connection. Normally, only a subset of these will actually
128
* be enabled by default, since this list may include cipher suites which
129
* do not meet quality of service requirements for those defaults. Such
130
* cipher suites are useful in specialized applications.
131
* <P>
132
* The returned array includes cipher suites from the list of standard
133
* cipher suite names in the <a href=
134
* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">
135
* JSSE Cipher Suite Names</a> section of the Java Cryptography
136
* Architecture Standard Algorithm Name Documentation, and may also
137
* include other cipher suites that the provider supports.
138
*
139
* @see #getDefaultCipherSuites()
140
* @return an array of cipher suite names
141
*/
142
public abstract String [] getSupportedCipherSuites();
143
144
/**
145
* Returns a socket layered over an existing socket connected to the named
146
* host, at the given port. This constructor can be used when tunneling SSL
147
* through a proxy or when negotiating the use of SSL over an existing
148
* socket. The host and port refer to the logical peer destination.
149
* This socket is configured using the socket options established for
150
* this factory.
151
*
152
* @param s the existing socket
153
* @param host the server host
154
* @param port the server port
155
* @param autoClose close the underlying socket when this socket is closed
156
* @return a socket connected to the specified host and port
157
* @throws IOException if an I/O error occurs when creating the socket
158
* @throws NullPointerException if the parameter s is null
159
*/
160
public abstract Socket createSocket(Socket s, String host,
161
int port, boolean autoClose) throws IOException;
162
163
/**
164
* Creates a server mode {@link Socket} layered over an
165
* existing connected socket, and is able to read data which has
166
* already been consumed/removed from the {@link Socket}'s
167
* underlying {@link InputStream}.
168
* <p>
169
* This method can be used by a server application that needs to
170
* observe the inbound data but still create valid SSL/TLS
171
* connections: for example, inspection of Server Name Indication
172
* (SNI) extensions (See section 3 of <A
173
* HREF="http://www.ietf.org/rfc/rfc6066.txt">TLS Extensions
174
* (RFC6066)</A>). Data that has been already removed from the
175
* underlying {@link InputStream} should be loaded into the
176
* {@code consumed} stream before this method is called, perhaps
177
* using a {@link java.io.ByteArrayInputStream}. When this
178
* {@link Socket} begins handshaking, it will read all of the data in
179
* {@code consumed} until it reaches {@code EOF}, then all further
180
* data is read from the underlying {@link InputStream} as
181
* usual.
182
* <p>
183
* The returned socket is configured using the socket options
184
* established for this factory, and is set to use server mode when
185
* handshaking (see {@link SSLSocket#setUseClientMode(boolean)}).
186
*
187
* @param s
188
* the existing socket
189
* @param consumed
190
* the consumed inbound network data that has already been
191
* removed from the existing {@link Socket}
192
* {@link InputStream}. This parameter may be
193
* {@code null} if no data has been removed.
194
* @param autoClose close the underlying socket when this socket is closed.
195
*
196
* @return the {@link Socket} compliant with the socket options
197
* established for this factory
198
*
199
* @throws IOException if an I/O error occurs when creating the socket
200
* @throws UnsupportedOperationException if the underlying provider
201
* does not implement the operation
202
* @throws NullPointerException if {@code s} is {@code null}
203
*
204
* @since 1.8
205
*/
206
public Socket createSocket(Socket s, InputStream consumed,
207
boolean autoClose) throws IOException {
208
throw new UnsupportedOperationException();
209
}
210
211
// lazy initialization holder class idiom for static default factory
212
//
213
// See Effective Java Second Edition: Item 71.
214
private static final class DefaultFactoryHolder {
215
private static final SSLSocketFactory defaultFactory;
216
217
static {
218
SSLSocketFactory mediator = null;
219
String clsName = getSecurityProperty("ssl.SocketFactory.provider");
220
if (clsName != null) {
221
log("setting up default SSLSocketFactory");
222
try {
223
Class<?> cls = null;
224
try {
225
cls = Class.forName(clsName);
226
} catch (ClassNotFoundException e) {
227
ClassLoader cl = ClassLoader.getSystemClassLoader();
228
if (cl != null) {
229
cls = cl.loadClass(clsName);
230
}
231
}
232
log("class " + clsName + " is loaded");
233
234
mediator = (SSLSocketFactory)cls
235
.getDeclaredConstructor().newInstance();
236
237
log("instantiated an instance of class " + clsName);
238
} catch (Exception e) {
239
log("SSLSocketFactory instantiation failed: " + e);
240
mediator = new DefaultSSLSocketFactory(e);
241
}
242
}
243
244
defaultFactory = mediator;
245
}
246
247
private static void log(String msg) {
248
if (DEBUG) {
249
System.out.println(msg);
250
}
251
}
252
}
253
}
254
255
256
// file private
257
class DefaultSSLSocketFactory extends SSLSocketFactory
258
{
259
private Exception reason;
260
261
DefaultSSLSocketFactory(Exception reason) {
262
this.reason = reason;
263
}
264
265
private Socket throwException() throws SocketException {
266
throw (SocketException)
267
new SocketException(reason.toString()).initCause(reason);
268
}
269
270
@Override
271
public Socket createSocket()
272
throws IOException
273
{
274
return throwException();
275
}
276
277
@Override
278
public Socket createSocket(String host, int port)
279
throws IOException
280
{
281
return throwException();
282
}
283
284
@Override
285
public Socket createSocket(Socket s, String host,
286
int port, boolean autoClose)
287
throws IOException
288
{
289
return throwException();
290
}
291
292
@Override
293
public Socket createSocket(InetAddress address, int port)
294
throws IOException
295
{
296
return throwException();
297
}
298
299
@Override
300
public Socket createSocket(String host, int port,
301
InetAddress clientAddress, int clientPort)
302
throws IOException
303
{
304
return throwException();
305
}
306
307
@Override
308
public Socket createSocket(InetAddress address, int port,
309
InetAddress clientAddress, int clientPort)
310
throws IOException
311
{
312
return throwException();
313
}
314
315
@Override
316
public String [] getDefaultCipherSuites() {
317
return new String[0];
318
}
319
320
@Override
321
public String [] getSupportedCipherSuites() {
322
return new String[0];
323
}
324
}
325
326