author | xuelei |
Mon, 11 Jun 2018 17:19:28 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56738 | 0811eaea3cd4 |
parent 56710 | f4c7a97a1275 |
child 56750 | 2b4ae319412b |
permissions | -rw-r--r-- |
56542 | 1 |
/* |
2 |
* Copyright (c) 2015, 2018, 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 sun.security.ssl; |
|
27 |
||
28 |
import java.security.*; |
|
56706 | 29 |
import java.security.interfaces.ECPrivateKey; |
56542 | 30 |
import java.security.spec.AlgorithmParameterSpec; |
56706 | 31 |
import java.security.spec.ECParameterSpec; |
56710 | 32 |
import java.security.spec.MGF1ParameterSpec; |
56542 | 33 |
import java.security.spec.PSSParameterSpec; |
34 |
import java.util.ArrayList; |
|
35 |
import java.util.Arrays; |
|
36 |
import java.util.Collection; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.EnumSet; |
|
39 |
import java.util.LinkedList; |
|
40 |
import java.util.List; |
|
41 |
import java.util.Set; |
|
56706 | 42 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroup; |
43 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroupType; |
|
56542 | 44 |
import sun.security.util.KeyUtil; |
45 |
||
46 |
enum SignatureScheme { |
|
47 |
// EdDSA algorithms |
|
48 |
ED25519 (0x0807, "ed25519", "ed25519", |
|
49 |
"ed25519", |
|
50 |
ProtocolVersion.PROTOCOLS_OF_13), |
|
51 |
ED448 (0x0808, "ed448", "ed448", |
|
52 |
"ed448", |
|
53 |
ProtocolVersion.PROTOCOLS_OF_13), |
|
54 |
||
55 |
// ECDSA algorithms |
|
56 |
ECDSA_SECP256R1_SHA256 (0x0403, "ecdsa_secp256r1_sha256", |
|
57 |
"SHA256withECDSA", |
|
58 |
"EC", |
|
56706 | 59 |
NamedGroup.SECP256_R1, |
56542 | 60 |
ProtocolVersion.PROTOCOLS_TO_13), |
61 |
ECDSA_SECP384R1_SHA384 (0x0503, "ecdsa_secp384r1_sha384", |
|
62 |
"SHA384withECDSA", |
|
63 |
"EC", |
|
56706 | 64 |
NamedGroup.SECP384_R1, |
56542 | 65 |
ProtocolVersion.PROTOCOLS_TO_13), |
66 |
ECDSA_SECP512R1_SHA512 (0x0603, "ecdsa_secp512r1_sha512", |
|
67 |
"SHA512withECDSA", |
|
68 |
"EC", |
|
56706 | 69 |
NamedGroup.SECP521_R1, |
56542 | 70 |
ProtocolVersion.PROTOCOLS_TO_13), |
71 |
||
72 |
// RSASSA-PSS algorithms with public key OID rsaEncryption |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
73 |
// |
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
74 |
// The minimalKeySize is calculated as (See RFC 8017 for details): |
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
75 |
// hash length + salt length + 16 |
56542 | 76 |
RSA_PSS_RSAE_SHA256 (0x0804, "rsa_pss_rsae_sha256", |
77 |
"RSASSA-PSS", "RSA", |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
78 |
SigAlgParamSpec.RSA_PSS_SHA256, 528, |
56542 | 79 |
ProtocolVersion.PROTOCOLS_OF_13), |
80 |
RSA_PSS_RSAE_SHA384 (0x0805, "rsa_pss_rsae_sha384", |
|
81 |
"RSASSA-PSS", "RSA", |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
82 |
SigAlgParamSpec.RSA_PSS_SHA384, 784, |
56542 | 83 |
ProtocolVersion.PROTOCOLS_OF_13), |
84 |
RSA_PSS_RSAE_SHA512 (0x0806, "rsa_pss_rsae_sha512", |
|
85 |
"RSASSA-PSS", "RSA", |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
86 |
SigAlgParamSpec.RSA_PSS_SHA512, 1040, |
56542 | 87 |
ProtocolVersion.PROTOCOLS_OF_13), |
88 |
||
89 |
// RSASSA-PSS algorithms with public key OID RSASSA-PSS |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
90 |
// |
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
91 |
// The minimalKeySize is calculated as (See RFC 8017 for details): |
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
92 |
// hash length + salt length + 16 |
56542 | 93 |
RSA_PSS_PSS_SHA256 (0x0809, "rsa_pss_pss_sha256", |
94 |
"RSASSA-PSS", "RSASSA-PSS", |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
95 |
SigAlgParamSpec.RSA_PSS_SHA256, 528, |
56542 | 96 |
ProtocolVersion.PROTOCOLS_OF_13), |
97 |
RSA_PSS_PSS_SHA384 (0x080A, "rsa_pss_pss_sha384", |
|
98 |
"RSASSA-PSS", "RSASSA-PSS", |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
99 |
SigAlgParamSpec.RSA_PSS_SHA384, 784, |
56542 | 100 |
ProtocolVersion.PROTOCOLS_OF_13), |
101 |
RSA_PSS_PSS_SHA512 (0x080B, "rsa_pss_pss_sha512", |
|
102 |
"RSASSA-PSS", "RSASSA-PSS", |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
103 |
SigAlgParamSpec.RSA_PSS_SHA512, 1040, |
56542 | 104 |
ProtocolVersion.PROTOCOLS_OF_13), |
105 |
||
106 |
// RSASSA-PKCS1-v1_5 algorithms |
|
107 |
RSA_PKCS1_SHA256 (0x0401, "rsa_pkcs1_sha256", "SHA256withRSA", |
|
56706 | 108 |
"RSA", null, null, 512, |
56542 | 109 |
ProtocolVersion.PROTOCOLS_TO_13, |
110 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
111 |
RSA_PKCS1_SHA384 (0x0501, "rsa_pkcs1_sha384", "SHA384withRSA", |
|
56706 | 112 |
"RSA", null, null, 768, |
56542 | 113 |
ProtocolVersion.PROTOCOLS_TO_13, |
114 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
115 |
RSA_PKCS1_SHA512 (0x0601, "rsa_pkcs1_sha512", "SHA512withRSA", |
|
56706 | 116 |
"RSA", null, null, 768, |
56542 | 117 |
ProtocolVersion.PROTOCOLS_TO_13, |
118 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
119 |
||
120 |
// Legacy algorithms |
|
121 |
DSA_SHA256 (0x0402, "dsa_sha256", "SHA256withDSA", |
|
122 |
"dsa", |
|
123 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
124 |
ECDSA_SHA224 (0x0303, "ecdsa_sha224", "SHA224withECDSA", |
|
125 |
"EC", |
|
126 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
127 |
RSA_SHA224 (0x0301, "rsa_sha224", "SHA224withRSA", |
|
128 |
"rsa", 768, |
|
129 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
130 |
DSA_SHA224 (0x0302, "dsa_sha224", "SHA224withDSA", |
|
131 |
"dsa", |
|
132 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
133 |
ECDSA_SHA1 (0x0203, "ecdsa_sha1", "SHA1withECDSA", |
|
134 |
"EC", |
|
135 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
136 |
RSA_PKCS1_SHA1 (0x0201, "rsa_pkcs1_sha1", "SHA1withRSA", |
|
56706 | 137 |
"rsa", null, null, 512, |
56542 | 138 |
ProtocolVersion.PROTOCOLS_TO_13, |
139 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
140 |
DSA_SHA1 (0x0202, "dsa_sha1", "SHA1withDSA", |
|
141 |
"dsa", |
|
142 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
143 |
RSA_MD5 (0x0101, "rsa_md5", "MD5withRSA", |
|
144 |
"rsa", 512, |
|
145 |
ProtocolVersion.PROTOCOLS_TO_12); |
|
146 |
||
147 |
final int id; // hash + signature |
|
148 |
final String name; // literal name |
|
149 |
private final String algorithm; // signature algorithm |
|
150 |
final String keyAlgorithm; // signature key algorithm |
|
151 |
private final AlgorithmParameterSpec signAlgParameter; |
|
56706 | 152 |
private final NamedGroup namedGroup; // associated named group |
56542 | 153 |
|
56710 | 154 |
// The minimal required key size in bits. |
56542 | 155 |
// |
156 |
// Only need to check RSA algorithm at present. RSA keys of 512 bits |
|
157 |
// have been shown to be practically breakable, it does not make much |
|
158 |
// sense to use the strong hash algorithm for keys whose key size less |
|
56710 | 159 |
// than 512 bits. So it is not necessary to calculate the minimal |
56542 | 160 |
// required key size exactly for a hash algorithm. |
161 |
final int minimalKeySize; |
|
162 |
final List<ProtocolVersion> supportedProtocols; |
|
56706 | 163 |
|
56542 | 164 |
// Some signature schemes are supported in different versions for handshake |
165 |
// messages and certificates. This field holds the supported protocols |
|
166 |
// for handshake messages. |
|
167 |
final List<ProtocolVersion> handshakeSupportedProtocols; |
|
168 |
final boolean isAvailable; |
|
169 |
||
170 |
private static final String[] hashAlgorithms = new String[] { |
|
171 |
"none", "md5", "sha1", "sha224", |
|
172 |
"sha256", "sha384", "sha512" |
|
173 |
}; |
|
174 |
||
175 |
private static final String[] signatureAlgorithms = new String[] { |
|
176 |
"anonymous", "rsa", "dsa", "ecdsa", |
|
177 |
}; |
|
178 |
||
179 |
static enum SigAlgParamSpec { // support RSASSA-PSS only now |
|
180 |
RSA_PSS_SHA256 ("SHA-256", 32), |
|
181 |
RSA_PSS_SHA384 ("SHA-384", 48), |
|
182 |
RSA_PSS_SHA512 ("SHA-512", 64); |
|
183 |
||
184 |
final private AlgorithmParameterSpec parameterSpec; |
|
185 |
final boolean isAvailable; |
|
186 |
||
187 |
SigAlgParamSpec(String hash, int saltLength) { |
|
188 |
// See RFC 8017 |
|
189 |
PSSParameterSpec pssParamSpec = |
|
56710 | 190 |
new PSSParameterSpec(hash, "MGF1", |
191 |
new MGF1ParameterSpec(hash), saltLength, 1); |
|
56542 | 192 |
|
193 |
boolean mediator = true; |
|
194 |
try { |
|
195 |
Signature signer = JsseJce.getSignature("RSASSA-PSS"); |
|
196 |
signer.setParameter(pssParamSpec); |
|
197 |
} catch (InvalidAlgorithmParameterException | |
|
198 |
NoSuchAlgorithmException exp) { |
|
199 |
mediator = false; |
|
200 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
201 |
SSLLogger.warning( |
|
202 |
"RSASSA-PSS signature with " + hash + |
|
203 |
" is not supported by the underlying providers", exp); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
this.isAvailable = mediator; |
|
208 |
this.parameterSpec = mediator ? pssParamSpec : null; |
|
209 |
} |
|
210 |
||
211 |
AlgorithmParameterSpec getParameterSpec() { |
|
212 |
return parameterSpec; |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
// performance optimization |
|
217 |
private static final Set<CryptoPrimitive> SIGNATURE_PRIMITIVE_SET = |
|
218 |
Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE)); |
|
219 |
||
220 |
||
221 |
private SignatureScheme(int id, String name, |
|
222 |
String algorithm, String keyAlgorithm, |
|
223 |
ProtocolVersion[] supportedProtocols) { |
|
224 |
this(id, name, algorithm, keyAlgorithm, -1, supportedProtocols); |
|
225 |
} |
|
226 |
||
227 |
private SignatureScheme(int id, String name, |
|
228 |
String algorithm, String keyAlgorithm, |
|
229 |
int minimalKeySize, |
|
230 |
ProtocolVersion[] supportedProtocols) { |
|
56706 | 231 |
this(id, name, algorithm, keyAlgorithm, |
232 |
null, minimalKeySize, supportedProtocols); |
|
56542 | 233 |
} |
234 |
||
235 |
private SignatureScheme(int id, String name, |
|
236 |
String algorithm, String keyAlgorithm, |
|
237 |
SigAlgParamSpec signAlgParamSpec, int minimalKeySize, |
|
56706 | 238 |
ProtocolVersion[] supportedProtocols) { |
239 |
this(id, name, algorithm, keyAlgorithm, |
|
240 |
signAlgParamSpec, null, minimalKeySize, |
|
241 |
supportedProtocols, supportedProtocols); |
|
242 |
} |
|
243 |
||
244 |
private SignatureScheme(int id, String name, |
|
245 |
String algorithm, String keyAlgorithm, |
|
246 |
NamedGroup namedGroup, |
|
247 |
ProtocolVersion[] supportedProtocols) { |
|
248 |
this(id, name, algorithm, keyAlgorithm, |
|
249 |
null, namedGroup, -1, |
|
250 |
supportedProtocols, supportedProtocols); |
|
251 |
} |
|
252 |
||
253 |
private SignatureScheme(int id, String name, |
|
254 |
String algorithm, String keyAlgorithm, |
|
255 |
SigAlgParamSpec signAlgParamSpec, |
|
256 |
NamedGroup namedGroup, int minimalKeySize, |
|
56542 | 257 |
ProtocolVersion[] supportedProtocols, |
258 |
ProtocolVersion[] handshakeSupportedProtocols) { |
|
259 |
this.id = id; |
|
260 |
this.name = name; |
|
261 |
this.algorithm = algorithm; |
|
262 |
this.keyAlgorithm = keyAlgorithm; |
|
263 |
this.signAlgParameter = |
|
264 |
signAlgParamSpec != null ? signAlgParamSpec.parameterSpec : null; |
|
56706 | 265 |
this.namedGroup = namedGroup; |
56542 | 266 |
this.minimalKeySize = minimalKeySize; |
267 |
this.supportedProtocols = Arrays.asList(supportedProtocols); |
|
56706 | 268 |
this.handshakeSupportedProtocols = |
269 |
Arrays.asList(handshakeSupportedProtocols); |
|
56542 | 270 |
|
271 |
boolean mediator = true; |
|
272 |
if (signAlgParamSpec != null) { |
|
273 |
mediator = signAlgParamSpec.isAvailable; |
|
274 |
} else { |
|
275 |
try { |
|
276 |
JsseJce.getSignature(algorithm); |
|
277 |
} catch (Exception e) { |
|
278 |
mediator = false; |
|
279 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
280 |
SSLLogger.warning( |
|
281 |
"Signature algorithm, " + algorithm + |
|
282 |
", is not supported by the underlying providers"); |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
if (mediator && ((id >> 8) & 0xFF) == 0x03) { // SHA224 |
|
288 |
// There are some problems to use SHA224 on Windows. |
|
289 |
if (Security.getProvider("SunMSCAPI") != null) { |
|
290 |
mediator = false; |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
this.isAvailable = mediator; |
|
295 |
} |
|
296 |
||
297 |
static SignatureScheme valueOf(int id) { |
|
298 |
for (SignatureScheme ss: SignatureScheme.values()) { |
|
299 |
if (ss.id == id) { |
|
300 |
return ss; |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
return null; |
|
305 |
} |
|
306 |
||
307 |
static String nameOf(int id) { |
|
308 |
for (SignatureScheme ss: SignatureScheme.values()) { |
|
309 |
if (ss.id == id) { |
|
310 |
return ss.name; |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
// Use TLS 1.2 style name for unknown signature scheme. |
|
315 |
int hashId = ((id >> 8) & 0xFF); |
|
316 |
int signId = (id & 0xFF); |
|
317 |
String hashName = (hashId >= hashAlgorithms.length) ? |
|
318 |
"UNDEFINED-HASH(" + hashId + ")" : hashAlgorithms[hashId]; |
|
319 |
String signName = (signId >= signatureAlgorithms.length) ? |
|
320 |
"UNDEFINED-SIGNATURE(" + signId + ")" : |
|
321 |
signatureAlgorithms[signId]; |
|
322 |
||
323 |
return signName + "_" + hashName; |
|
324 |
} |
|
325 |
||
326 |
// Return the size of a SignatureScheme structure in TLS record |
|
327 |
static int sizeInRecord() { |
|
328 |
return 2; |
|
329 |
} |
|
330 |
||
331 |
// Get local supported algorithm collection complying to algorithm |
|
332 |
// constraints. |
|
333 |
static List<SignatureScheme> getSupportedAlgorithms( |
|
334 |
AlgorithmConstraints constraints, |
|
335 |
List<ProtocolVersion> activeProtocols) { |
|
336 |
List<SignatureScheme> supported = new LinkedList<>(); |
|
337 |
for (SignatureScheme ss: SignatureScheme.values()) { |
|
338 |
if (!ss.isAvailable) { |
|
339 |
continue; |
|
340 |
} |
|
341 |
||
342 |
boolean isMatch = false; |
|
343 |
for (ProtocolVersion pv : activeProtocols) { |
|
344 |
if (ss.supportedProtocols.contains(pv)) { |
|
345 |
isMatch = true; |
|
346 |
break; |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
if (isMatch) { |
|
351 |
if (constraints.permits( |
|
352 |
SIGNATURE_PRIMITIVE_SET, ss.algorithm, null)) { |
|
353 |
supported.add(ss); |
|
354 |
} else if (SSLLogger.isOn && |
|
355 |
SSLLogger.isOn("ssl,handshake,verbose")) { |
|
356 |
SSLLogger.finest( |
|
357 |
"Ignore disabled signature sheme: " + ss.name); |
|
358 |
} |
|
359 |
} else if (SSLLogger.isOn && |
|
360 |
SSLLogger.isOn("ssl,handshake,verbose")) { |
|
361 |
SSLLogger.finest( |
|
362 |
"Ignore inactive signature sheme: " + ss.name); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
return supported; |
|
367 |
} |
|
368 |
||
369 |
static List<SignatureScheme> getSupportedAlgorithms( |
|
370 |
AlgorithmConstraints constraints, |
|
371 |
ProtocolVersion protocolVersion, int[] algorithmIds) { |
|
372 |
List<SignatureScheme> supported = new LinkedList<>(); |
|
373 |
for (int ssid : algorithmIds) { |
|
374 |
SignatureScheme ss = SignatureScheme.valueOf(ssid); |
|
375 |
if (ss == null) { |
|
376 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
377 |
SSLLogger.warning( |
|
378 |
"Unsupported signature scheme: " + |
|
379 |
SignatureScheme.nameOf(ssid)); |
|
380 |
} |
|
381 |
} else if (ss.isAvailable && |
|
382 |
ss.supportedProtocols.contains(protocolVersion) && |
|
383 |
constraints.permits(SIGNATURE_PRIMITIVE_SET, |
|
384 |
ss.algorithm, null)) { |
|
385 |
supported.add(ss); |
|
386 |
} else { |
|
387 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
388 |
SSLLogger.warning( |
|
389 |
"Unsupported signature scheme: " + ss.name); |
|
390 |
} |
|
391 |
} |
|
392 |
} |
|
393 |
||
394 |
return supported; |
|
395 |
} |
|
396 |
||
397 |
static SignatureScheme getPreferableAlgorithm( |
|
398 |
List<SignatureScheme> schemes, |
|
399 |
SignatureScheme certScheme, |
|
400 |
ProtocolVersion version) { |
|
401 |
||
402 |
for (SignatureScheme ss : schemes) { |
|
403 |
if (ss.isAvailable && |
|
404 |
ss.handshakeSupportedProtocols.contains(version) && |
|
405 |
certScheme.keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) { |
|
406 |
||
407 |
return ss; |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
return null; |
|
412 |
} |
|
413 |
||
414 |
static SignatureScheme getPreferableAlgorithm( |
|
415 |
List<SignatureScheme> schemes, |
|
416 |
PrivateKey signingKey, |
|
417 |
ProtocolVersion version) { |
|
418 |
||
419 |
String keyAlgorithm = signingKey.getAlgorithm(); |
|
420 |
int keySize; |
|
421 |
// Only need to check RSA algorithm at present. |
|
56738
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
422 |
if (keyAlgorithm.equalsIgnoreCase("RSA") || |
0811eaea3cd4
Clean up of SSLConfiguration, correct the key size of RSASSA-PSS signature
xuelei
parents:
56710
diff
changeset
|
423 |
keyAlgorithm.equalsIgnoreCase("RSASSA-PSS")) { |
56542 | 424 |
keySize = KeyUtil.getKeySize(signingKey); |
425 |
} else { |
|
426 |
keySize = Integer.MAX_VALUE; |
|
427 |
} |
|
428 |
for (SignatureScheme ss : schemes) { |
|
429 |
if (ss.isAvailable && (keySize >= ss.minimalKeySize) && |
|
430 |
ss.handshakeSupportedProtocols.contains(version) && |
|
431 |
keyAlgorithm.equalsIgnoreCase(ss.keyAlgorithm)) { |
|
56706 | 432 |
if (ss.namedGroup != null && |
433 |
ss.namedGroup.type == NamedGroupType.NAMED_GROUP_ECDHE) { |
|
434 |
ECParameterSpec params = |
|
435 |
((ECPrivateKey)signingKey).getParams(); |
|
436 |
if (ss.namedGroup == NamedGroup.valueOf(params)) { |
|
437 |
return ss; |
|
438 |
} |
|
439 |
} else { |
|
440 |
return ss; |
|
441 |
} |
|
56542 | 442 |
} |
443 |
} |
|
444 |
||
445 |
return null; |
|
446 |
} |
|
447 |
||
448 |
static String[] getAlgorithmNames(Collection<SignatureScheme> schemes) { |
|
449 |
if (schemes != null) { |
|
450 |
ArrayList<String> names = new ArrayList<>(schemes.size()); |
|
451 |
for (SignatureScheme scheme : schemes) { |
|
452 |
names.add(scheme.algorithm); |
|
453 |
} |
|
454 |
||
455 |
return names.toArray(new String[0]); |
|
456 |
} |
|
457 |
||
458 |
return new String[0]; |
|
459 |
} |
|
460 |
||
461 |
Signature getSignature() throws NoSuchAlgorithmException, |
|
462 |
InvalidAlgorithmParameterException { |
|
463 |
if (!isAvailable) { |
|
464 |
return null; |
|
465 |
} |
|
466 |
||
467 |
Signature signer = JsseJce.getSignature(algorithm); |
|
468 |
if (signAlgParameter != null) { |
|
469 |
signer.setParameter(signAlgParameter); |
|
470 |
} |
|
471 |
||
472 |
return signer; |
|
473 |
} |
|
474 |
} |