author | xuelei |
Wed, 09 Apr 2014 12:49:51 +0000 | |
changeset 23733 | b9b80421cfa7 |
parent 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
7043 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
14664
diff
changeset
|
2 |
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
7043 | 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 sun.security.ssl; |
|
27 |
||
28 |
import java.security.AlgorithmConstraints; |
|
29 |
import java.security.CryptoPrimitive; |
|
30 |
import java.security.AlgorithmParameters; |
|
31 |
||
32 |
import javax.net.ssl.*; |
|
33 |
||
34 |
import java.security.Key; |
|
35 |
||
36 |
import java.util.Set; |
|
37 |
import java.util.HashSet; |
|
38 |
||
39 |
import sun.security.util.DisabledAlgorithmConstraints; |
|
40 |
import sun.security.ssl.CipherSuite.*; |
|
41 |
||
42 |
/** |
|
43 |
* Algorithm constraints for disabled algorithms property |
|
44 |
* |
|
45 |
* See the "jdk.certpath.disabledAlgorithms" specification in java.security |
|
46 |
* for the syntax of the disabled algorithm string. |
|
47 |
*/ |
|
48 |
final class SSLAlgorithmConstraints implements AlgorithmConstraints { |
|
49 |
private final static AlgorithmConstraints tlsDisabledAlgConstraints = |
|
50 |
new TLSDisabledAlgConstraints(); |
|
51 |
private final static AlgorithmConstraints x509DisabledAlgConstraints = |
|
52 |
new X509DisabledAlgConstraints(); |
|
53 |
private AlgorithmConstraints userAlgConstraints = null; |
|
54 |
private AlgorithmConstraints peerAlgConstraints = null; |
|
55 |
||
56 |
private boolean enabledX509DisabledAlgConstraints = true; |
|
57 |
||
58 |
SSLAlgorithmConstraints(AlgorithmConstraints algorithmConstraints) { |
|
59 |
userAlgConstraints = algorithmConstraints; |
|
60 |
} |
|
61 |
||
62 |
SSLAlgorithmConstraints(SSLSocket socket, |
|
63 |
boolean withDefaultCertPathConstraints) { |
|
64 |
if (socket != null) { |
|
65 |
userAlgConstraints = |
|
66 |
socket.getSSLParameters().getAlgorithmConstraints(); |
|
67 |
} |
|
68 |
||
69 |
if (!withDefaultCertPathConstraints) { |
|
70 |
enabledX509DisabledAlgConstraints = false; |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
SSLAlgorithmConstraints(SSLEngine engine, |
|
75 |
boolean withDefaultCertPathConstraints) { |
|
76 |
if (engine != null) { |
|
77 |
userAlgConstraints = |
|
78 |
engine.getSSLParameters().getAlgorithmConstraints(); |
|
79 |
} |
|
80 |
||
81 |
if (!withDefaultCertPathConstraints) { |
|
82 |
enabledX509DisabledAlgConstraints = false; |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
SSLAlgorithmConstraints(SSLSocket socket, String[] supportedAlgorithms, |
|
87 |
boolean withDefaultCertPathConstraints) { |
|
88 |
if (socket != null) { |
|
89 |
userAlgConstraints = |
|
90 |
socket.getSSLParameters().getAlgorithmConstraints(); |
|
91 |
peerAlgConstraints = |
|
92 |
new SupportedSignatureAlgorithmConstraints(supportedAlgorithms); |
|
93 |
} |
|
94 |
||
95 |
if (!withDefaultCertPathConstraints) { |
|
96 |
enabledX509DisabledAlgConstraints = false; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
SSLAlgorithmConstraints(SSLEngine engine, String[] supportedAlgorithms, |
|
101 |
boolean withDefaultCertPathConstraints) { |
|
102 |
if (engine != null) { |
|
103 |
userAlgConstraints = |
|
104 |
engine.getSSLParameters().getAlgorithmConstraints(); |
|
105 |
peerAlgConstraints = |
|
106 |
new SupportedSignatureAlgorithmConstraints(supportedAlgorithms); |
|
107 |
} |
|
108 |
||
109 |
if (!withDefaultCertPathConstraints) { |
|
110 |
enabledX509DisabledAlgConstraints = false; |
|
111 |
} |
|
112 |
} |
|
113 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9035
diff
changeset
|
114 |
@Override |
7043 | 115 |
public boolean permits(Set<CryptoPrimitive> primitives, |
116 |
String algorithm, AlgorithmParameters parameters) { |
|
117 |
||
118 |
boolean permitted = true; |
|
119 |
||
120 |
if (peerAlgConstraints != null) { |
|
121 |
permitted = peerAlgConstraints.permits( |
|
122 |
primitives, algorithm, parameters); |
|
123 |
} |
|
124 |
||
125 |
if (permitted && userAlgConstraints != null) { |
|
126 |
permitted = userAlgConstraints.permits( |
|
127 |
primitives, algorithm, parameters); |
|
128 |
} |
|
129 |
||
130 |
if (permitted) { |
|
131 |
permitted = tlsDisabledAlgConstraints.permits( |
|
132 |
primitives, algorithm, parameters); |
|
133 |
} |
|
134 |
||
135 |
if (permitted && enabledX509DisabledAlgConstraints) { |
|
136 |
permitted = x509DisabledAlgConstraints.permits( |
|
137 |
primitives, algorithm, parameters); |
|
138 |
} |
|
139 |
||
140 |
return permitted; |
|
141 |
} |
|
142 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9035
diff
changeset
|
143 |
@Override |
7043 | 144 |
public boolean permits(Set<CryptoPrimitive> primitives, Key key) { |
145 |
||
146 |
boolean permitted = true; |
|
147 |
||
148 |
if (peerAlgConstraints != null) { |
|
149 |
permitted = peerAlgConstraints.permits(primitives, key); |
|
150 |
} |
|
151 |
||
152 |
if (permitted && userAlgConstraints != null) { |
|
153 |
permitted = userAlgConstraints.permits(primitives, key); |
|
154 |
} |
|
155 |
||
156 |
if (permitted) { |
|
157 |
permitted = tlsDisabledAlgConstraints.permits(primitives, key); |
|
158 |
} |
|
159 |
||
160 |
if (permitted && enabledX509DisabledAlgConstraints) { |
|
161 |
permitted = x509DisabledAlgConstraints.permits(primitives, key); |
|
162 |
} |
|
163 |
||
164 |
return permitted; |
|
165 |
} |
|
166 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9035
diff
changeset
|
167 |
@Override |
7043 | 168 |
public boolean permits(Set<CryptoPrimitive> primitives, |
169 |
String algorithm, Key key, AlgorithmParameters parameters) { |
|
170 |
||
171 |
boolean permitted = true; |
|
172 |
||
173 |
if (peerAlgConstraints != null) { |
|
174 |
permitted = peerAlgConstraints.permits( |
|
175 |
primitives, algorithm, key, parameters); |
|
176 |
} |
|
177 |
||
178 |
if (permitted && userAlgConstraints != null) { |
|
179 |
permitted = userAlgConstraints.permits( |
|
180 |
primitives, algorithm, key, parameters); |
|
181 |
} |
|
182 |
||
183 |
if (permitted) { |
|
184 |
permitted = tlsDisabledAlgConstraints.permits( |
|
185 |
primitives, algorithm, key, parameters); |
|
186 |
} |
|
187 |
||
188 |
if (permitted && enabledX509DisabledAlgConstraints) { |
|
189 |
permitted = x509DisabledAlgConstraints.permits( |
|
190 |
primitives, algorithm, key, parameters); |
|
191 |
} |
|
192 |
||
193 |
return permitted; |
|
194 |
} |
|
195 |
||
196 |
||
197 |
static private class SupportedSignatureAlgorithmConstraints |
|
198 |
implements AlgorithmConstraints { |
|
199 |
// supported signature algorithms |
|
200 |
private String[] supportedAlgorithms; |
|
201 |
||
202 |
SupportedSignatureAlgorithmConstraints(String[] supportedAlgorithms) { |
|
203 |
if (supportedAlgorithms != null) { |
|
204 |
this.supportedAlgorithms = supportedAlgorithms.clone(); |
|
205 |
} else { |
|
206 |
this.supportedAlgorithms = null; |
|
207 |
} |
|
208 |
} |
|
209 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9035
diff
changeset
|
210 |
@Override |
7043 | 211 |
public boolean permits(Set<CryptoPrimitive> primitives, |
212 |
String algorithm, AlgorithmParameters parameters) { |
|
213 |
||
214 |
if (algorithm == null || algorithm.length() == 0) { |
|
215 |
throw new IllegalArgumentException( |
|
216 |
"No algorithm name specified"); |
|
217 |
} |
|
218 |
||
219 |
if (primitives == null || primitives.isEmpty()) { |
|
220 |
throw new IllegalArgumentException( |
|
221 |
"No cryptographic primitive specified"); |
|
222 |
} |
|
223 |
||
224 |
if (supportedAlgorithms == null || |
|
225 |
supportedAlgorithms.length == 0) { |
|
226 |
return false; |
|
227 |
} |
|
228 |
||
229 |
// trim the MGF part: <digest>with<encryption>and<mgf> |
|
230 |
int position = algorithm.indexOf("and"); |
|
231 |
if (position > 0) { |
|
232 |
algorithm = algorithm.substring(0, position); |
|
233 |
} |
|
234 |
||
235 |
for (String supportedAlgorithm : supportedAlgorithms) { |
|
236 |
if (algorithm.equalsIgnoreCase(supportedAlgorithm)) { |
|
237 |
return true; |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
return false; |
|
242 |
} |
|
243 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9035
diff
changeset
|
244 |
@Override |
7043 | 245 |
final public boolean permits(Set<CryptoPrimitive> primitives, Key key) { |
246 |
return true; |
|
247 |
} |
|
248 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
9035
diff
changeset
|
249 |
@Override |
7043 | 250 |
final public boolean permits(Set<CryptoPrimitive> primitives, |
251 |
String algorithm, Key key, AlgorithmParameters parameters) { |
|
252 |
||
253 |
if (algorithm == null || algorithm.length() == 0) { |
|
254 |
throw new IllegalArgumentException( |
|
255 |
"No algorithm name specified"); |
|
256 |
} |
|
257 |
||
258 |
return permits(primitives, algorithm, parameters); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
static private class BasicDisabledAlgConstraints |
|
263 |
extends DisabledAlgorithmConstraints { |
|
264 |
BasicDisabledAlgConstraints(String propertyName) { |
|
265 |
super(propertyName); |
|
266 |
} |
|
267 |
||
268 |
protected Set<String> decomposes(KeyExchange keyExchange, |
|
269 |
boolean forCertPathOnly) { |
|
7990 | 270 |
Set<String> components = new HashSet<>(); |
7043 | 271 |
switch (keyExchange) { |
272 |
case K_NULL: |
|
273 |
if (!forCertPathOnly) { |
|
274 |
components.add("NULL"); |
|
275 |
} |
|
276 |
break; |
|
277 |
case K_RSA: |
|
278 |
components.add("RSA"); |
|
279 |
break; |
|
280 |
case K_RSA_EXPORT: |
|
281 |
components.add("RSA"); |
|
282 |
components.add("RSA_EXPORT"); |
|
283 |
break; |
|
284 |
case K_DH_RSA: |
|
285 |
components.add("RSA"); |
|
286 |
components.add("DH"); |
|
287 |
components.add("DiffieHellman"); |
|
288 |
components.add("DH_RSA"); |
|
289 |
break; |
|
290 |
case K_DH_DSS: |
|
291 |
components.add("DSA"); |
|
292 |
components.add("DSS"); |
|
293 |
components.add("DH"); |
|
294 |
components.add("DiffieHellman"); |
|
295 |
components.add("DH_DSS"); |
|
296 |
break; |
|
297 |
case K_DHE_DSS: |
|
298 |
components.add("DSA"); |
|
299 |
components.add("DSS"); |
|
300 |
components.add("DH"); |
|
301 |
components.add("DHE"); |
|
302 |
components.add("DiffieHellman"); |
|
303 |
components.add("DHE_DSS"); |
|
304 |
break; |
|
305 |
case K_DHE_RSA: |
|
306 |
components.add("RSA"); |
|
307 |
components.add("DH"); |
|
308 |
components.add("DHE"); |
|
309 |
components.add("DiffieHellman"); |
|
310 |
components.add("DHE_RSA"); |
|
311 |
break; |
|
312 |
case K_DH_ANON: |
|
313 |
if (!forCertPathOnly) { |
|
314 |
components.add("ANON"); |
|
315 |
components.add("DH"); |
|
316 |
components.add("DiffieHellman"); |
|
317 |
components.add("DH_ANON"); |
|
318 |
} |
|
319 |
break; |
|
320 |
case K_ECDH_ECDSA: |
|
321 |
components.add("ECDH"); |
|
322 |
components.add("ECDSA"); |
|
323 |
components.add("ECDH_ECDSA"); |
|
324 |
break; |
|
325 |
case K_ECDH_RSA: |
|
326 |
components.add("ECDH"); |
|
327 |
components.add("RSA"); |
|
328 |
components.add("ECDH_RSA"); |
|
329 |
break; |
|
330 |
case K_ECDHE_ECDSA: |
|
331 |
components.add("ECDHE"); |
|
332 |
components.add("ECDSA"); |
|
333 |
components.add("ECDHE_ECDSA"); |
|
334 |
break; |
|
335 |
case K_ECDHE_RSA: |
|
336 |
components.add("ECDHE"); |
|
337 |
components.add("RSA"); |
|
338 |
components.add("ECDHE_RSA"); |
|
339 |
break; |
|
340 |
case K_ECDH_ANON: |
|
341 |
if (!forCertPathOnly) { |
|
342 |
components.add("ECDH"); |
|
343 |
components.add("ANON"); |
|
344 |
components.add("ECDH_ANON"); |
|
345 |
} |
|
346 |
break; |
|
347 |
case K_KRB5: |
|
348 |
if (!forCertPathOnly) { |
|
349 |
components.add("KRB5"); |
|
350 |
} |
|
351 |
break; |
|
352 |
case K_KRB5_EXPORT: |
|
353 |
if (!forCertPathOnly) { |
|
354 |
components.add("KRB5_EXPORT"); |
|
355 |
} |
|
356 |
break; |
|
357 |
default: |
|
358 |
// ignore |
|
359 |
} |
|
360 |
||
361 |
return components; |
|
362 |
} |
|
363 |
||
364 |
protected Set<String> decomposes(BulkCipher bulkCipher) { |
|
7990 | 365 |
Set<String> components = new HashSet<>(); |
7043 | 366 |
|
367 |
if (bulkCipher.transformation != null) { |
|
368 |
components.addAll(super.decomposes(bulkCipher.transformation)); |
|
369 |
} |
|
370 |
||
371 |
return components; |
|
372 |
} |
|
373 |
||
374 |
protected Set<String> decomposes(MacAlg macAlg) { |
|
7990 | 375 |
Set<String> components = new HashSet<>(); |
7043 | 376 |
|
377 |
if (macAlg == CipherSuite.M_MD5) { |
|
378 |
components.add("MD5"); |
|
379 |
components.add("HmacMD5"); |
|
380 |
} else if (macAlg == CipherSuite.M_SHA) { |
|
381 |
components.add("SHA1"); |
|
382 |
components.add("SHA-1"); |
|
383 |
components.add("HmacSHA1"); |
|
384 |
} else if (macAlg == CipherSuite.M_SHA256) { |
|
385 |
components.add("SHA256"); |
|
386 |
components.add("SHA-256"); |
|
387 |
components.add("HmacSHA256"); |
|
388 |
} else if (macAlg == CipherSuite.M_SHA384) { |
|
389 |
components.add("SHA384"); |
|
390 |
components.add("SHA-384"); |
|
391 |
components.add("HmacSHA384"); |
|
392 |
} |
|
393 |
||
394 |
return components; |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
static private class TLSDisabledAlgConstraints |
|
399 |
extends BasicDisabledAlgConstraints { |
|
400 |
||
401 |
TLSDisabledAlgConstraints() { |
|
402 |
super(DisabledAlgorithmConstraints.PROPERTY_TLS_DISABLED_ALGS); |
|
403 |
} |
|
404 |
||
405 |
@Override |
|
406 |
protected Set<String> decomposes(String algorithm) { |
|
407 |
if (algorithm.startsWith("SSL_") || algorithm.startsWith("TLS_")) { |
|
408 |
CipherSuite cipherSuite = null; |
|
409 |
try { |
|
410 |
cipherSuite = CipherSuite.valueOf(algorithm); |
|
411 |
} catch (IllegalArgumentException iae) { |
|
412 |
// ignore: unknown or unsupported ciphersuite |
|
413 |
} |
|
414 |
||
415 |
if (cipherSuite != null) { |
|
7990 | 416 |
Set<String> components = new HashSet<>(); |
7043 | 417 |
|
418 |
if(cipherSuite.keyExchange != null) { |
|
419 |
components.addAll( |
|
420 |
decomposes(cipherSuite.keyExchange, false)); |
|
421 |
} |
|
422 |
||
423 |
if (cipherSuite.cipher != null) { |
|
424 |
components.addAll(decomposes(cipherSuite.cipher)); |
|
425 |
} |
|
426 |
||
427 |
if (cipherSuite.macAlg != null) { |
|
428 |
components.addAll(decomposes(cipherSuite.macAlg)); |
|
429 |
} |
|
430 |
||
431 |
return components; |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
return super.decomposes(algorithm); |
|
436 |
} |
|
437 |
} |
|
438 |
||
439 |
static private class X509DisabledAlgConstraints |
|
440 |
extends BasicDisabledAlgConstraints { |
|
441 |
||
442 |
X509DisabledAlgConstraints() { |
|
443 |
super(DisabledAlgorithmConstraints.PROPERTY_CERTPATH_DISABLED_ALGS); |
|
444 |
} |
|
445 |
||
446 |
@Override |
|
447 |
protected Set<String> decomposes(String algorithm) { |
|
448 |
if (algorithm.startsWith("SSL_") || algorithm.startsWith("TLS_")) { |
|
449 |
CipherSuite cipherSuite = null; |
|
450 |
try { |
|
451 |
cipherSuite = CipherSuite.valueOf(algorithm); |
|
452 |
} catch (IllegalArgumentException iae) { |
|
453 |
// ignore: unknown or unsupported ciphersuite |
|
454 |
} |
|
455 |
||
456 |
if (cipherSuite != null) { |
|
7990 | 457 |
Set<String> components = new HashSet<>(); |
7043 | 458 |
|
459 |
if(cipherSuite.keyExchange != null) { |
|
460 |
components.addAll( |
|
461 |
decomposes(cipherSuite.keyExchange, true)); |
|
462 |
} |
|
463 |
||
464 |
// Certification path algorithm constraints do not apply |
|
465 |
// to cipherSuite.cipher and cipherSuite.macAlg. |
|
466 |
||
467 |
return components; |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
return super.decomposes(algorithm); |
|
472 |
} |
|
473 |
} |
|
474 |
} |
|
475 |