author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57718 | a93b7b28f644 |
permissions | -rw-r--r-- |
50768 | 1 |
/* |
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
52337
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
50768 | 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.*; |
|
29 |
import java.security.spec.AlgorithmParameterSpec; |
|
30 |
import java.security.spec.ECParameterSpec; |
|
31 |
import java.security.spec.MGF1ParameterSpec; |
|
32 |
import java.security.spec.PSSParameterSpec; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Arrays; |
|
35 |
import java.util.Collection; |
|
36 |
import java.util.Collections; |
|
37 |
import java.util.EnumSet; |
|
38 |
import java.util.LinkedList; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Set; |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
41 |
import sun.security.ssl.NamedGroup.NamedGroupSpec; |
55452
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
42 |
import sun.security.ssl.SupportedGroupsExtension.SupportedGroups; |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
43 |
import sun.security.ssl.X509Authentication.X509Possession; |
50768 | 44 |
import sun.security.util.KeyUtil; |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
54417
diff
changeset
|
45 |
import sun.security.util.SignatureUtil; |
50768 | 46 |
|
47 |
enum SignatureScheme { |
|
48 |
// EdDSA algorithms |
|
49 |
ED25519 (0x0807, "ed25519", "ed25519", |
|
50 |
"ed25519", |
|
51 |
ProtocolVersion.PROTOCOLS_OF_13), |
|
52 |
ED448 (0x0808, "ed448", "ed448", |
|
53 |
"ed448", |
|
54 |
ProtocolVersion.PROTOCOLS_OF_13), |
|
55 |
||
56 |
// ECDSA algorithms |
|
57 |
ECDSA_SECP256R1_SHA256 (0x0403, "ecdsa_secp256r1_sha256", |
|
58 |
"SHA256withECDSA", |
|
59 |
"EC", |
|
60 |
NamedGroup.SECP256_R1, |
|
61 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
62 |
ECDSA_SECP384R1_SHA384 (0x0503, "ecdsa_secp384r1_sha384", |
|
63 |
"SHA384withECDSA", |
|
64 |
"EC", |
|
65 |
NamedGroup.SECP384_R1, |
|
66 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
52337
346ad00d6154
8212738: Incorrectly named signature scheme ecdsa_secp512r1_sha512
xuelei
parents:
52208
diff
changeset
|
67 |
ECDSA_SECP521R1_SHA512 (0x0603, "ecdsa_secp521r1_sha512", |
50768 | 68 |
"SHA512withECDSA", |
69 |
"EC", |
|
70 |
NamedGroup.SECP521_R1, |
|
71 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
72 |
||
73 |
// RSASSA-PSS algorithms with public key OID rsaEncryption |
|
74 |
// |
|
75 |
// The minimalKeySize is calculated as (See RFC 8017 for details): |
|
76 |
// hash length + salt length + 16 |
|
77 |
RSA_PSS_RSAE_SHA256 (0x0804, "rsa_pss_rsae_sha256", |
|
78 |
"RSASSA-PSS", "RSA", |
|
79 |
SigAlgParamSpec.RSA_PSS_SHA256, 528, |
|
80 |
ProtocolVersion.PROTOCOLS_12_13), |
|
81 |
RSA_PSS_RSAE_SHA384 (0x0805, "rsa_pss_rsae_sha384", |
|
82 |
"RSASSA-PSS", "RSA", |
|
83 |
SigAlgParamSpec.RSA_PSS_SHA384, 784, |
|
84 |
ProtocolVersion.PROTOCOLS_12_13), |
|
85 |
RSA_PSS_RSAE_SHA512 (0x0806, "rsa_pss_rsae_sha512", |
|
86 |
"RSASSA-PSS", "RSA", |
|
87 |
SigAlgParamSpec.RSA_PSS_SHA512, 1040, |
|
88 |
ProtocolVersion.PROTOCOLS_12_13), |
|
89 |
||
90 |
// RSASSA-PSS algorithms with public key OID RSASSA-PSS |
|
91 |
// |
|
92 |
// The minimalKeySize is calculated as (See RFC 8017 for details): |
|
93 |
// hash length + salt length + 16 |
|
94 |
RSA_PSS_PSS_SHA256 (0x0809, "rsa_pss_pss_sha256", |
|
95 |
"RSASSA-PSS", "RSASSA-PSS", |
|
96 |
SigAlgParamSpec.RSA_PSS_SHA256, 528, |
|
97 |
ProtocolVersion.PROTOCOLS_12_13), |
|
98 |
RSA_PSS_PSS_SHA384 (0x080A, "rsa_pss_pss_sha384", |
|
99 |
"RSASSA-PSS", "RSASSA-PSS", |
|
100 |
SigAlgParamSpec.RSA_PSS_SHA384, 784, |
|
101 |
ProtocolVersion.PROTOCOLS_12_13), |
|
102 |
RSA_PSS_PSS_SHA512 (0x080B, "rsa_pss_pss_sha512", |
|
103 |
"RSASSA-PSS", "RSASSA-PSS", |
|
104 |
SigAlgParamSpec.RSA_PSS_SHA512, 1040, |
|
105 |
ProtocolVersion.PROTOCOLS_12_13), |
|
106 |
||
107 |
// RSASSA-PKCS1-v1_5 algorithms |
|
108 |
RSA_PKCS1_SHA256 (0x0401, "rsa_pkcs1_sha256", "SHA256withRSA", |
|
109 |
"RSA", null, null, 511, |
|
110 |
ProtocolVersion.PROTOCOLS_TO_13, |
|
111 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
112 |
RSA_PKCS1_SHA384 (0x0501, "rsa_pkcs1_sha384", "SHA384withRSA", |
|
113 |
"RSA", null, null, 768, |
|
114 |
ProtocolVersion.PROTOCOLS_TO_13, |
|
115 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
116 |
RSA_PKCS1_SHA512 (0x0601, "rsa_pkcs1_sha512", "SHA512withRSA", |
|
117 |
"RSA", null, null, 768, |
|
118 |
ProtocolVersion.PROTOCOLS_TO_13, |
|
119 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
120 |
||
121 |
// Legacy algorithms |
|
122 |
DSA_SHA256 (0x0402, "dsa_sha256", "SHA256withDSA", |
|
123 |
"DSA", |
|
124 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
125 |
ECDSA_SHA224 (0x0303, "ecdsa_sha224", "SHA224withECDSA", |
|
126 |
"EC", |
|
127 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
128 |
RSA_SHA224 (0x0301, "rsa_sha224", "SHA224withRSA", |
|
129 |
"RSA", 511, |
|
130 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
131 |
DSA_SHA224 (0x0302, "dsa_sha224", "SHA224withDSA", |
|
132 |
"DSA", |
|
133 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
134 |
ECDSA_SHA1 (0x0203, "ecdsa_sha1", "SHA1withECDSA", |
|
135 |
"EC", |
|
136 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
137 |
RSA_PKCS1_SHA1 (0x0201, "rsa_pkcs1_sha1", "SHA1withRSA", |
|
138 |
"RSA", null, null, 511, |
|
139 |
ProtocolVersion.PROTOCOLS_TO_13, |
|
140 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
141 |
DSA_SHA1 (0x0202, "dsa_sha1", "SHA1withDSA", |
|
142 |
"DSA", |
|
143 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
144 |
RSA_MD5 (0x0101, "rsa_md5", "MD5withRSA", |
|
145 |
"RSA", 511, |
|
146 |
ProtocolVersion.PROTOCOLS_TO_12); |
|
147 |
||
148 |
final int id; // hash + signature |
|
149 |
final String name; // literal name |
|
150 |
private final String algorithm; // signature algorithm |
|
151 |
final String keyAlgorithm; // signature key algorithm |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
152 |
private final SigAlgParamSpec signAlgParams; // signature parameters |
50768 | 153 |
private final NamedGroup namedGroup; // associated named group |
154 |
||
155 |
// The minimal required key size in bits. |
|
156 |
// |
|
157 |
// Only need to check RSA algorithm at present. RSA keys of 512 bits |
|
158 |
// have been shown to be practically breakable, it does not make much |
|
159 |
// sense to use the strong hash algorithm for keys whose key size less |
|
160 |
// than 512 bits. So it is not necessary to calculate the minimal |
|
161 |
// required key size exactly for a hash algorithm. |
|
162 |
// |
|
163 |
// Note that some provider may use 511 bits for 512-bit strength RSA keys. |
|
164 |
final int minimalKeySize; |
|
165 |
final List<ProtocolVersion> supportedProtocols; |
|
166 |
||
167 |
// Some signature schemes are supported in different versions for handshake |
|
168 |
// messages and certificates. This field holds the supported protocols |
|
169 |
// for handshake messages. |
|
170 |
final List<ProtocolVersion> handshakeSupportedProtocols; |
|
171 |
final boolean isAvailable; |
|
172 |
||
173 |
private static final String[] hashAlgorithms = new String[] { |
|
174 |
"none", "md5", "sha1", "sha224", |
|
175 |
"sha256", "sha384", "sha512" |
|
176 |
}; |
|
177 |
||
178 |
private static final String[] signatureAlgorithms = new String[] { |
|
179 |
"anonymous", "rsa", "dsa", "ecdsa", |
|
180 |
}; |
|
181 |
||
182 |
static enum SigAlgParamSpec { // support RSASSA-PSS only now |
|
183 |
RSA_PSS_SHA256 ("SHA-256", 32), |
|
184 |
RSA_PSS_SHA384 ("SHA-384", 48), |
|
185 |
RSA_PSS_SHA512 ("SHA-512", 64); |
|
186 |
||
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
187 |
private final AlgorithmParameterSpec parameterSpec; |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
188 |
private final AlgorithmParameters parameters; |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
189 |
private final boolean isAvailable; |
50768 | 190 |
|
191 |
SigAlgParamSpec(String hash, int saltLength) { |
|
192 |
// See RFC 8017 |
|
193 |
PSSParameterSpec pssParamSpec = |
|
194 |
new PSSParameterSpec(hash, "MGF1", |
|
195 |
new MGF1ParameterSpec(hash), saltLength, 1); |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
196 |
AlgorithmParameters pssParams = null; |
50768 | 197 |
|
198 |
boolean mediator = true; |
|
199 |
try { |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
52337
diff
changeset
|
200 |
Signature signer = Signature.getInstance("RSASSA-PSS"); |
50768 | 201 |
signer.setParameter(pssParamSpec); |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
202 |
pssParams = signer.getParameters(); |
50768 | 203 |
} catch (InvalidAlgorithmParameterException | |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
204 |
NoSuchAlgorithmException | RuntimeException exp) { |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
205 |
// Signature.getParameters() may throw RuntimeException. |
50768 | 206 |
mediator = false; |
207 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
208 |
SSLLogger.warning( |
|
209 |
"RSASSA-PSS signature with " + hash + |
|
210 |
" is not supported by the underlying providers", exp); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
this.isAvailable = mediator; |
|
215 |
this.parameterSpec = mediator ? pssParamSpec : null; |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
216 |
this.parameters = mediator ? pssParams : null; |
50768 | 217 |
} |
218 |
} |
|
219 |
||
220 |
// performance optimization |
|
221 |
private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET = |
|
222 |
Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE)); |
|
223 |
||
224 |
||
225 |
private SignatureScheme(int id, String name, |
|
226 |
String algorithm, String keyAlgorithm, |
|
227 |
ProtocolVersion[] supportedProtocols) { |
|
228 |
this(id, name, algorithm, keyAlgorithm, -1, supportedProtocols); |
|
229 |
} |
|
230 |
||
231 |
private SignatureScheme(int id, String name, |
|
232 |
String algorithm, String keyAlgorithm, |
|
233 |
int minimalKeySize, |
|
234 |
ProtocolVersion[] supportedProtocols) { |
|
235 |
this(id, name, algorithm, keyAlgorithm, |
|
236 |
null, minimalKeySize, supportedProtocols); |
|
237 |
} |
|
238 |
||
239 |
private SignatureScheme(int id, String name, |
|
240 |
String algorithm, String keyAlgorithm, |
|
241 |
SigAlgParamSpec signAlgParamSpec, int minimalKeySize, |
|
242 |
ProtocolVersion[] supportedProtocols) { |
|
243 |
this(id, name, algorithm, keyAlgorithm, |
|
244 |
signAlgParamSpec, null, minimalKeySize, |
|
245 |
supportedProtocols, supportedProtocols); |
|
246 |
} |
|
247 |
||
248 |
private SignatureScheme(int id, String name, |
|
249 |
String algorithm, String keyAlgorithm, |
|
250 |
NamedGroup namedGroup, |
|
251 |
ProtocolVersion[] supportedProtocols) { |
|
252 |
this(id, name, algorithm, keyAlgorithm, |
|
253 |
null, namedGroup, -1, |
|
254 |
supportedProtocols, supportedProtocols); |
|
255 |
} |
|
256 |
||
257 |
private SignatureScheme(int id, String name, |
|
258 |
String algorithm, String keyAlgorithm, |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
259 |
SigAlgParamSpec signAlgParams, |
50768 | 260 |
NamedGroup namedGroup, int minimalKeySize, |
261 |
ProtocolVersion[] supportedProtocols, |
|
262 |
ProtocolVersion[] handshakeSupportedProtocols) { |
|
263 |
this.id = id; |
|
264 |
this.name = name; |
|
265 |
this.algorithm = algorithm; |
|
266 |
this.keyAlgorithm = keyAlgorithm; |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
267 |
this.signAlgParams = signAlgParams; |
50768 | 268 |
this.namedGroup = namedGroup; |
269 |
this.minimalKeySize = minimalKeySize; |
|
270 |
this.supportedProtocols = Arrays.asList(supportedProtocols); |
|
271 |
this.handshakeSupportedProtocols = |
|
272 |
Arrays.asList(handshakeSupportedProtocols); |
|
273 |
||
274 |
boolean mediator = true; |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
275 |
if (signAlgParams != null) { |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
276 |
mediator = signAlgParams.isAvailable; |
50768 | 277 |
} else { |
278 |
try { |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
52337
diff
changeset
|
279 |
Signature.getInstance(algorithm); |
50768 | 280 |
} catch (Exception e) { |
281 |
mediator = false; |
|
282 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
283 |
SSLLogger.warning( |
|
284 |
"Signature algorithm, " + algorithm + |
|
285 |
", is not supported by the underlying providers"); |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
if (mediator && ((id >> 8) & 0xFF) == 0x03) { // SHA224 |
|
291 |
// There are some problems to use SHA224 on Windows. |
|
292 |
if (Security.getProvider("SunMSCAPI") != null) { |
|
293 |
mediator = false; |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
this.isAvailable = mediator; |
|
298 |
} |
|
299 |
||
300 |
static SignatureScheme valueOf(int id) { |
|
301 |
for (SignatureScheme ss: SignatureScheme.values()) { |
|
302 |
if (ss.id == id) { |
|
303 |
return ss; |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
return null; |
|
308 |
} |
|
309 |
||
310 |
static String nameOf(int id) { |
|
311 |
for (SignatureScheme ss: SignatureScheme.values()) { |
|
312 |
if (ss.id == id) { |
|
313 |
return ss.name; |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
// Use TLS 1.2 style name for unknown signature scheme. |
|
318 |
int hashId = ((id >> 8) & 0xFF); |
|
319 |
int signId = (id & 0xFF); |
|
320 |
String hashName = (hashId >= hashAlgorithms.length) ? |
|
321 |
"UNDEFINED-HASH(" + hashId + ")" : hashAlgorithms[hashId]; |
|
322 |
String signName = (signId >= signatureAlgorithms.length) ? |
|
323 |
"UNDEFINED-SIGNATURE(" + signId + ")" : |
|
324 |
signatureAlgorithms[signId]; |
|
325 |
||
326 |
return signName + "_" + hashName; |
|
327 |
} |
|
328 |
||
329 |
// Return the size of a SignatureScheme structure in TLS record |
|
330 |
static int sizeInRecord() { |
|
331 |
return 2; |
|
332 |
} |
|
333 |
||
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
334 |
private boolean isPermitted(AlgorithmConstraints constraints) { |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
335 |
return constraints.permits(SIGNATURE_PRIMITIVE_SET, |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
336 |
this.name, null) && |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
337 |
constraints.permits(SIGNATURE_PRIMITIVE_SET, |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
338 |
this.keyAlgorithm, null) && |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
339 |
constraints.permits(SIGNATURE_PRIMITIVE_SET, |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
340 |
this.algorithm, (signAlgParams != null ? |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
341 |
signAlgParams.parameters : null)) && |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
342 |
(namedGroup != null ? |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
343 |
namedGroup.isPermitted(constraints) : true); |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
344 |
} |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
345 |
|
50768 | 346 |
// Get local supported algorithm collection complying to algorithm |
347 |
// constraints. |
|
348 |
static List<SignatureScheme> getSupportedAlgorithms( |
|
349 |
AlgorithmConstraints constraints, |
|
350 |
List<ProtocolVersion> activeProtocols) { |
|
351 |
List<SignatureScheme> supported = new LinkedList<>(); |
|
352 |
for (SignatureScheme ss: SignatureScheme.values()) { |
|
353 |
if (!ss.isAvailable) { |
|
354 |
continue; |
|
355 |
} |
|
356 |
||
357 |
boolean isMatch = false; |
|
358 |
for (ProtocolVersion pv : activeProtocols) { |
|
359 |
if (ss.supportedProtocols.contains(pv)) { |
|
360 |
isMatch = true; |
|
361 |
break; |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
if (isMatch) { |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
366 |
if (ss.isPermitted(constraints)) { |
50768 | 367 |
supported.add(ss); |
368 |
} else if (SSLLogger.isOn && |
|
369 |
SSLLogger.isOn("ssl,handshake,verbose")) { |
|
370 |
SSLLogger.finest( |
|
52208
5bf98ad48412
8212752: Typo in SSL log message related to inactive/disabled signature scheme
coffeys
parents:
51574
diff
changeset
|
371 |
"Ignore disabled signature scheme: " + ss.name); |
50768 | 372 |
} |
373 |
} else if (SSLLogger.isOn && |
|
374 |
SSLLogger.isOn("ssl,handshake,verbose")) { |
|
375 |
SSLLogger.finest( |
|
52208
5bf98ad48412
8212752: Typo in SSL log message related to inactive/disabled signature scheme
coffeys
parents:
51574
diff
changeset
|
376 |
"Ignore inactive signature scheme: " + ss.name); |
50768 | 377 |
} |
378 |
} |
|
379 |
||
380 |
return supported; |
|
381 |
} |
|
382 |
||
383 |
static List<SignatureScheme> getSupportedAlgorithms( |
|
384 |
AlgorithmConstraints constraints, |
|
385 |
ProtocolVersion protocolVersion, int[] algorithmIds) { |
|
386 |
List<SignatureScheme> supported = new LinkedList<>(); |
|
387 |
for (int ssid : algorithmIds) { |
|
388 |
SignatureScheme ss = SignatureScheme.valueOf(ssid); |
|
389 |
if (ss == null) { |
|
390 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
391 |
SSLLogger.warning( |
|
392 |
"Unsupported signature scheme: " + |
|
393 |
SignatureScheme.nameOf(ssid)); |
|
394 |
} |
|
395 |
} else if (ss.isAvailable && |
|
396 |
ss.supportedProtocols.contains(protocolVersion) && |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
397 |
ss.isPermitted(constraints)) { |
50768 | 398 |
supported.add(ss); |
399 |
} else { |
|
400 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
401 |
SSLLogger.warning( |
|
402 |
"Unsupported signature scheme: " + ss.name); |
|
403 |
} |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
return supported; |
|
408 |
} |
|
409 |
||
410 |
static SignatureScheme getPreferableAlgorithm( |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
411 |
AlgorithmConstraints constraints, |
50768 | 412 |
List<SignatureScheme> schemes, |
413 |
SignatureScheme certScheme, |
|
414 |
ProtocolVersion version) { |
|
415 |
||
416 |
for (SignatureScheme ss : schemes) { |
|
417 |
if (ss.isAvailable && |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
418 |
ss.handshakeSupportedProtocols.contains(version) && |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
419 |
certScheme.keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm) && |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
420 |
ss.isPermitted(constraints)) { |
50768 | 421 |
return ss; |
422 |
} |
|
423 |
} |
|
424 |
||
425 |
return null; |
|
426 |
} |
|
427 |
||
428 |
static SignatureScheme getPreferableAlgorithm( |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
429 |
AlgorithmConstraints constraints, |
50768 | 430 |
List<SignatureScheme> schemes, |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
431 |
X509Possession x509Possession, |
50768 | 432 |
ProtocolVersion version) { |
433 |
||
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
434 |
PrivateKey signingKey = x509Possession.popPrivateKey; |
50768 | 435 |
String keyAlgorithm = signingKey.getAlgorithm(); |
436 |
int keySize; |
|
437 |
// Only need to check RSA algorithm at present. |
|
438 |
if (keyAlgorithm.equalsIgnoreCase("RSA") || |
|
439 |
keyAlgorithm.equalsIgnoreCase("RSASSA-PSS")) { |
|
440 |
keySize = KeyUtil.getKeySize(signingKey); |
|
441 |
} else { |
|
442 |
keySize = Integer.MAX_VALUE; |
|
443 |
} |
|
444 |
for (SignatureScheme ss : schemes) { |
|
445 |
if (ss.isAvailable && (keySize >= ss.minimalKeySize) && |
|
55353 | 446 |
ss.handshakeSupportedProtocols.contains(version) && |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
447 |
keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm) && |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
448 |
ss.isPermitted(constraints)) { |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
449 |
if ((ss.namedGroup != null) && (ss.namedGroup.spec == |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
450 |
NamedGroupSpec.NAMED_GROUP_ECDHE)) { |
50768 | 451 |
ECParameterSpec params = |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
452 |
x509Possession.getECParameterSpec(); |
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
453 |
if (params != null && |
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
454 |
ss.namedGroup == NamedGroup.valueOf(params)) { |
50768 | 455 |
return ss; |
456 |
} |
|
55452
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
457 |
|
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
458 |
if (SSLLogger.isOn && |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
459 |
SSLLogger.isOn("ssl,handshake,verbose")) { |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
460 |
SSLLogger.finest( |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
461 |
"Ignore the signature algorithm (" + ss + |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
462 |
"), unsupported EC parameter spec: " + params); |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
463 |
} |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
464 |
} else if ("EC".equals(ss.keyAlgorithm)) { |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
465 |
// Must be a legacy signature algorithm, which does not |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
466 |
// specify the associated named groups. The connection |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
467 |
// cannot be established if the peer cannot recognize |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
468 |
// the named group used for the signature. RFC 8446 |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
469 |
// does not define countermeasures for the corner cases. |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
470 |
// In order to mitigate the impact, we choose to check |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
471 |
// against the local supported named groups. The risk |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
472 |
// should be minimal as applications should not use |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
473 |
// unsupported named groups for its certificates. |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
474 |
ECParameterSpec params = |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
475 |
x509Possession.getECParameterSpec(); |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
476 |
if (params != null) { |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
477 |
NamedGroup keyGroup = NamedGroup.valueOf(params); |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
478 |
if (keyGroup != null && |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
479 |
SupportedGroups.isSupported(keyGroup)) { |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
480 |
return ss; |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
481 |
} |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
482 |
} |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
483 |
|
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
484 |
if (SSLLogger.isOn && |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
485 |
SSLLogger.isOn("ssl,handshake,verbose")) { |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
486 |
SSLLogger.finest( |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
487 |
"Ignore the legacy signature algorithm (" + ss + |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
488 |
"), unsupported EC parameter spec: " + params); |
1170b6d92d1c
8225766: Curve in certificate should not affect signature scheme when using TLSv1.3
xuelei
parents:
55353
diff
changeset
|
489 |
} |
50768 | 490 |
} else { |
491 |
return ss; |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
return null; |
|
497 |
} |
|
498 |
||
499 |
static String[] getAlgorithmNames(Collection<SignatureScheme> schemes) { |
|
500 |
if (schemes != null) { |
|
501 |
ArrayList<String> names = new ArrayList<>(schemes.size()); |
|
502 |
for (SignatureScheme scheme : schemes) { |
|
503 |
names.add(scheme.algorithm); |
|
504 |
} |
|
505 |
||
506 |
return names.toArray(new String[0]); |
|
507 |
} |
|
508 |
||
509 |
return new String[0]; |
|
510 |
} |
|
511 |
||
512 |
Signature getSignature(Key key) throws NoSuchAlgorithmException, |
|
513 |
InvalidAlgorithmParameterException, InvalidKeyException { |
|
514 |
if (!isAvailable) { |
|
515 |
return null; |
|
516 |
} |
|
517 |
||
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
52337
diff
changeset
|
518 |
Signature signer = Signature.getInstance(algorithm); |
50768 | 519 |
if (key instanceof PublicKey) { |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
54417
diff
changeset
|
520 |
SignatureUtil.initVerifyWithParam(signer, (PublicKey)key, |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
521 |
(signAlgParams != null ? |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
522 |
signAlgParams.parameterSpec : null)); |
50768 | 523 |
} else { |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
54417
diff
changeset
|
524 |
SignatureUtil.initSignWithParam(signer, (PrivateKey)key, |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
525 |
(signAlgParams != null ? |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
526 |
signAlgParams.parameterSpec : null), |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55452
diff
changeset
|
527 |
null); |
50768 | 528 |
} |
529 |
||
530 |
return signer; |
|
531 |
} |
|
532 |
} |