Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/net/ssl/SSLParameters.java
38918 views
1
/*
2
* Copyright (c) 2005, 2020, 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.security.AlgorithmConstraints;
29
import java.util.Map;
30
import java.util.List;
31
import java.util.HashMap;
32
import java.util.ArrayList;
33
import java.util.Collection;
34
import java.util.Collections;
35
import java.util.LinkedHashMap;
36
37
/**
38
* Encapsulates parameters for an SSL/TLS connection. The parameters
39
* are the list of ciphersuites to be accepted in an SSL/TLS handshake,
40
* the list of protocols to be allowed, the endpoint identification
41
* algorithm during SSL/TLS handshaking, the Server Name Indication (SNI),
42
* the algorithm constraints and whether SSL/TLS servers should request
43
* or require client authentication, etc.
44
* <p>
45
* SSLParameters can be created via the constructors in this class.
46
* Objects can also be obtained using the <code>getSSLParameters()</code>
47
* methods in
48
* {@link SSLSocket#getSSLParameters SSLSocket} and
49
* {@link SSLServerSocket#getSSLParameters SSLServerSocket} and
50
* {@link SSLEngine#getSSLParameters SSLEngine} or the
51
* {@link SSLContext#getDefaultSSLParameters getDefaultSSLParameters()} and
52
* {@link SSLContext#getSupportedSSLParameters getSupportedSSLParameters()}
53
* methods in <code>SSLContext</code>.
54
* <p>
55
* SSLParameters can be applied to a connection via the methods
56
* {@link SSLSocket#setSSLParameters SSLSocket.setSSLParameters()} and
57
* {@link SSLServerSocket#setSSLParameters SSLServerSocket.setSSLParameters()}
58
* and {@link SSLEngine#setSSLParameters SSLEngine.setSSLParameters()}.
59
* <p>
60
* For example:
61
*
62
* <blockquote><pre>
63
* SSLParameters p = sslSocket.getSSLParameters();
64
* p.setProtocols(new String[] { "TLSv1.2" });
65
* p.setCipherSuites(
66
* new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", ... });
67
* p.setApplicationProtocols(new String[] {"h2", "http/1.1"});
68
* sslSocket.setSSLParameters(p);
69
* </pre></blockquote>*
70
*
71
* @see SSLSocket
72
* @see SSLEngine
73
* @see SSLContext
74
*
75
* @since 1.6
76
*/
77
public class SSLParameters {
78
79
private String[] cipherSuites;
80
private String[] protocols;
81
private boolean wantClientAuth;
82
private boolean needClientAuth;
83
private String identificationAlgorithm;
84
private AlgorithmConstraints algorithmConstraints;
85
private Map<Integer, SNIServerName> sniNames = null;
86
private Map<Integer, SNIMatcher> sniMatchers = null;
87
private boolean preferLocalCipherSuites;
88
private String[] applicationProtocols = new String[0];
89
90
/**
91
* Constructs SSLParameters.
92
* <p>
93
* The values of cipherSuites, protocols, cryptographic algorithm
94
* constraints, endpoint identification algorithm, server names and
95
* server name matchers are set to <code>null</code>, useCipherSuitesOrder,
96
* wantClientAuth and needClientAuth are set to <code>false</code>.
97
*/
98
public SSLParameters() {
99
// empty
100
}
101
102
/**
103
* Constructs SSLParameters from the specified array of ciphersuites.
104
* <p>
105
* Calling this constructor is equivalent to calling the no-args
106
* constructor followed by
107
* <code>setCipherSuites(cipherSuites);</code>.
108
*
109
* @param cipherSuites the array of ciphersuites (or null)
110
*/
111
public SSLParameters(String[] cipherSuites) {
112
setCipherSuites(cipherSuites);
113
}
114
115
/**
116
* Constructs SSLParameters from the specified array of ciphersuites
117
* and protocols.
118
* <p>
119
* Calling this constructor is equivalent to calling the no-args
120
* constructor followed by
121
* <code>setCipherSuites(cipherSuites); setProtocols(protocols);</code>.
122
*
123
* @param cipherSuites the array of ciphersuites (or null)
124
* @param protocols the array of protocols (or null)
125
*/
126
public SSLParameters(String[] cipherSuites, String[] protocols) {
127
setCipherSuites(cipherSuites);
128
setProtocols(protocols);
129
}
130
131
private static String[] clone(String[] s) {
132
return (s == null) ? null : s.clone();
133
}
134
135
/**
136
* Returns a copy of the array of ciphersuites or null if none
137
* have been set.
138
*
139
* @return a copy of the array of ciphersuites or null if none
140
* have been set.
141
*/
142
public String[] getCipherSuites() {
143
return clone(cipherSuites);
144
}
145
146
/**
147
* Sets the array of ciphersuites.
148
*
149
* @param cipherSuites the array of ciphersuites (or null)
150
*/
151
public void setCipherSuites(String[] cipherSuites) {
152
this.cipherSuites = clone(cipherSuites);
153
}
154
155
/**
156
* Returns a copy of the array of protocols or null if none
157
* have been set.
158
*
159
* @return a copy of the array of protocols or null if none
160
* have been set.
161
*/
162
public String[] getProtocols() {
163
return clone(protocols);
164
}
165
166
/**
167
* Sets the array of protocols.
168
*
169
* @param protocols the array of protocols (or null)
170
*/
171
public void setProtocols(String[] protocols) {
172
this.protocols = clone(protocols);
173
}
174
175
/**
176
* Returns whether client authentication should be requested.
177
*
178
* @return whether client authentication should be requested.
179
*/
180
public boolean getWantClientAuth() {
181
return wantClientAuth;
182
}
183
184
/**
185
* Sets whether client authentication should be requested. Calling
186
* this method clears the <code>needClientAuth</code> flag.
187
*
188
* @param wantClientAuth whether client authentication should be requested
189
*/
190
public void setWantClientAuth(boolean wantClientAuth) {
191
this.wantClientAuth = wantClientAuth;
192
this.needClientAuth = false;
193
}
194
195
/**
196
* Returns whether client authentication should be required.
197
*
198
* @return whether client authentication should be required.
199
*/
200
public boolean getNeedClientAuth() {
201
return needClientAuth;
202
}
203
204
/**
205
* Sets whether client authentication should be required. Calling
206
* this method clears the <code>wantClientAuth</code> flag.
207
*
208
* @param needClientAuth whether client authentication should be required
209
*/
210
public void setNeedClientAuth(boolean needClientAuth) {
211
this.wantClientAuth = false;
212
this.needClientAuth = needClientAuth;
213
}
214
215
/**
216
* Returns the cryptographic algorithm constraints.
217
*
218
* @return the cryptographic algorithm constraints, or null if the
219
* constraints have not been set
220
*
221
* @see #setAlgorithmConstraints(AlgorithmConstraints)
222
*
223
* @since 1.7
224
*/
225
public AlgorithmConstraints getAlgorithmConstraints() {
226
return algorithmConstraints;
227
}
228
229
/**
230
* Sets the cryptographic algorithm constraints, which will be used
231
* in addition to any configured by the runtime environment.
232
* <p>
233
* If the <code>constraints</code> parameter is non-null, every
234
* cryptographic algorithm, key and algorithm parameters used in the
235
* SSL/TLS handshake must be permitted by the constraints.
236
*
237
* @param constraints the algorithm constraints (or null)
238
*
239
* @since 1.7
240
*/
241
public void setAlgorithmConstraints(AlgorithmConstraints constraints) {
242
// the constraints object is immutable
243
this.algorithmConstraints = constraints;
244
}
245
246
/**
247
* Gets the endpoint identification algorithm.
248
*
249
* @return the endpoint identification algorithm, or null if none
250
* has been set.
251
*
252
* @see X509ExtendedTrustManager
253
* @see #setEndpointIdentificationAlgorithm(String)
254
*
255
* @since 1.7
256
*/
257
public String getEndpointIdentificationAlgorithm() {
258
return identificationAlgorithm;
259
}
260
261
/**
262
* Sets the endpoint identification algorithm.
263
* <p>
264
* If the <code>algorithm</code> parameter is non-null or non-empty, the
265
* endpoint identification/verification procedures must be handled during
266
* SSL/TLS handshaking. This is to prevent man-in-the-middle attacks.
267
*
268
* @param algorithm The standard string name of the endpoint
269
* identification algorithm (or null). See Appendix A in the <a href=
270
* "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
271
* Java Cryptography Architecture API Specification &amp; Reference </a>
272
* for information about standard algorithm names.
273
*
274
* @see X509ExtendedTrustManager
275
*
276
* @since 1.7
277
*/
278
public void setEndpointIdentificationAlgorithm(String algorithm) {
279
this.identificationAlgorithm = algorithm;
280
}
281
282
/**
283
* Sets the desired {@link SNIServerName}s of the Server Name
284
* Indication (SNI) parameter.
285
* <P>
286
* This method is only useful to {@link SSLSocket}s or {@link SSLEngine}s
287
* operating in client mode.
288
* <P>
289
* Note that the {@code serverNames} list is cloned
290
* to protect against subsequent modification.
291
*
292
* @param serverNames
293
* the list of desired {@link SNIServerName}s (or null)
294
*
295
* @throws NullPointerException if the {@code serverNames}
296
* contains {@code null} element
297
* @throws IllegalArgumentException if the {@code serverNames}
298
* contains more than one name of the same name type
299
*
300
* @see SNIServerName
301
* @see #getServerNames()
302
*
303
* @since 1.8
304
*/
305
public final void setServerNames(List<SNIServerName> serverNames) {
306
if (serverNames != null) {
307
if (!serverNames.isEmpty()) {
308
sniNames = new LinkedHashMap<>(serverNames.size());
309
for (SNIServerName serverName : serverNames) {
310
if (sniNames.put(serverName.getType(),
311
serverName) != null) {
312
throw new IllegalArgumentException(
313
"Duplicated server name of type " +
314
serverName.getType());
315
}
316
}
317
} else {
318
sniNames = Collections.<Integer, SNIServerName>emptyMap();
319
}
320
} else {
321
sniNames = null;
322
}
323
}
324
325
/**
326
* Returns a {@link List} containing all {@link SNIServerName}s of the
327
* Server Name Indication (SNI) parameter, or null if none has been set.
328
* <P>
329
* This method is only useful to {@link SSLSocket}s or {@link SSLEngine}s
330
* operating in client mode.
331
* <P>
332
* For SSL/TLS connections, the underlying SSL/TLS provider
333
* may specify a default value for a certain server name type. In
334
* client mode, it is recommended that, by default, providers should
335
* include the server name indication whenever the server can be located
336
* by a supported server name type.
337
* <P>
338
* It is recommended that providers initialize default Server Name
339
* Indications when creating {@code SSLSocket}/{@code SSLEngine}s.
340
* In the following examples, the server name could be represented by an
341
* instance of {@link SNIHostName} which has been initialized with the
342
* hostname "www.example.com" and type
343
* {@link StandardConstants#SNI_HOST_NAME}.
344
*
345
* <pre>
346
* Socket socket =
347
* sslSocketFactory.createSocket("www.example.com", 443);
348
* </pre>
349
* or
350
* <pre>
351
* SSLEngine engine =
352
* sslContext.createSSLEngine("www.example.com", 443);
353
* </pre>
354
* <P>
355
*
356
* @return null or an immutable list of non-null {@link SNIServerName}s
357
*
358
* @see List
359
* @see #setServerNames(List)
360
*
361
* @since 1.8
362
*/
363
public final List<SNIServerName> getServerNames() {
364
if (sniNames != null) {
365
if (!sniNames.isEmpty()) {
366
return Collections.<SNIServerName>unmodifiableList(
367
new ArrayList<>(sniNames.values()));
368
} else {
369
return Collections.<SNIServerName>emptyList();
370
}
371
}
372
373
return null;
374
}
375
376
/**
377
* Sets the {@link SNIMatcher}s of the Server Name Indication (SNI)
378
* parameter.
379
* <P>
380
* This method is only useful to {@link SSLSocket}s or {@link SSLEngine}s
381
* operating in server mode.
382
* <P>
383
* Note that the {@code matchers} collection is cloned to protect
384
* against subsequent modification.
385
*
386
* @param matchers
387
* the collection of {@link SNIMatcher}s (or null)
388
*
389
* @throws NullPointerException if the {@code matchers}
390
* contains {@code null} element
391
* @throws IllegalArgumentException if the {@code matchers}
392
* contains more than one name of the same name type
393
*
394
* @see Collection
395
* @see SNIMatcher
396
* @see #getSNIMatchers()
397
*
398
* @since 1.8
399
*/
400
public final void setSNIMatchers(Collection<SNIMatcher> matchers) {
401
if (matchers != null) {
402
if (!matchers.isEmpty()) {
403
sniMatchers = new HashMap<>(matchers.size());
404
for (SNIMatcher matcher : matchers) {
405
if (sniMatchers.put(matcher.getType(),
406
matcher) != null) {
407
throw new IllegalArgumentException(
408
"Duplicated server name of type " +
409
matcher.getType());
410
}
411
}
412
} else {
413
sniMatchers = Collections.<Integer, SNIMatcher>emptyMap();
414
}
415
} else {
416
sniMatchers = null;
417
}
418
}
419
420
/**
421
* Returns a {@link Collection} containing all {@link SNIMatcher}s of the
422
* Server Name Indication (SNI) parameter, or null if none has been set.
423
* <P>
424
* This method is only useful to {@link SSLSocket}s or {@link SSLEngine}s
425
* operating in server mode.
426
* <P>
427
* For better interoperability, providers generally will not define
428
* default matchers so that by default servers will ignore the SNI
429
* extension and continue the handshake.
430
*
431
* @return null or an immutable collection of non-null {@link SNIMatcher}s
432
*
433
* @see SNIMatcher
434
* @see #setSNIMatchers(Collection)
435
*
436
* @since 1.8
437
*/
438
public final Collection<SNIMatcher> getSNIMatchers() {
439
if (sniMatchers != null) {
440
if (!sniMatchers.isEmpty()) {
441
return Collections.<SNIMatcher>unmodifiableList(
442
new ArrayList<>(sniMatchers.values()));
443
} else {
444
return Collections.<SNIMatcher>emptyList();
445
}
446
}
447
448
return null;
449
}
450
451
/**
452
* Sets whether the local cipher suites preference should be honored.
453
*
454
* @param honorOrder whether local cipher suites order in
455
* {@code #getCipherSuites} should be honored during
456
* SSL/TLS handshaking.
457
*
458
* @see #getUseCipherSuitesOrder()
459
*
460
* @since 1.8
461
*/
462
public final void setUseCipherSuitesOrder(boolean honorOrder) {
463
this.preferLocalCipherSuites = honorOrder;
464
}
465
466
/**
467
* Returns whether the local cipher suites preference should be honored.
468
*
469
* @return whether local cipher suites order in {@code #getCipherSuites}
470
* should be honored during SSL/TLS handshaking.
471
*
472
* @see #setUseCipherSuitesOrder(boolean)
473
*
474
* @since 1.8
475
*/
476
public final boolean getUseCipherSuitesOrder() {
477
return preferLocalCipherSuites;
478
}
479
480
/**
481
* Returns a prioritized array of application-layer protocol names that
482
* can be negotiated over the SSL/TLS/DTLS protocols.
483
* <p>
484
* The array could be empty (zero-length), in which case protocol
485
* indications will not be used.
486
* <p>
487
* This method will return a new array each time it is invoked.
488
*
489
* @return a non-null, possibly zero-length array of application protocol
490
* {@code String}s. The array is ordered based on protocol
491
* preference, with {@code protocols[0]} being the most preferred.
492
* @see #setApplicationProtocols
493
* @since 8
494
*/
495
public String[] getApplicationProtocols() {
496
return applicationProtocols.clone();
497
}
498
499
/**
500
* Sets the prioritized array of application-layer protocol names that
501
* can be negotiated over the SSL/TLS/DTLS protocols.
502
* <p>
503
* If application-layer protocols are supported by the underlying
504
* SSL/TLS implementation, this method configures which values can
505
* be negotiated by protocols such as <a
506
* href="http://www.ietf.org/rfc/rfc7301.txt"> RFC 7301 </a>, the
507
* Application Layer Protocol Negotiation (ALPN).
508
* <p>
509
* If this end of the connection is expected to offer application protocol
510
* values, all protocols configured by this method will be sent to the
511
* peer.
512
* <p>
513
* If this end of the connection is expected to select the application
514
* protocol value, the {@code protocols} configured by this method are
515
* compared with those sent by the peer. The first matched value becomes
516
* the negotiated value. If none of the {@code protocols} were actually
517
* requested by the peer, the underlying protocol will determine what
518
* action to take. (For example, ALPN will send a
519
* {@code "no_application_protocol"} alert and terminate the connection.)
520
*
521
* @implSpec
522
* This method will make a copy of the {@code protocols} array.
523
*
524
* @param protocols an ordered array of application protocols,
525
* with {@code protocols[0]} being the most preferred.
526
* If the array is empty (zero-length), protocol
527
* indications will not be used.
528
* @throws IllegalArgumentException if protocols is null, or if
529
* any element in a non-empty array is null or an
530
* empty (zero-length) string
531
* @see #getApplicationProtocols
532
* @since 8
533
*/
534
public void setApplicationProtocols(String[] protocols) {
535
if (protocols == null) {
536
throw new IllegalArgumentException("protocols was null");
537
}
538
539
String[] tempProtocols = protocols.clone();
540
541
for (String p : tempProtocols) {
542
if (p == null || p.equals("")) {
543
throw new IllegalArgumentException(
544
"An element of protocols was null/empty");
545
}
546
}
547
applicationProtocols = tempProtocols;
548
}
549
}
550
551