author | apetcher |
Thu, 16 Aug 2018 14:01:03 -0400 | |
branch | JDK-8171279-XDH-TLS-branch-2 |
changeset 56858 | 829e9b5ace08 |
parent 51398 | 3c389a284345 |
child 56859 | 6f9f7f4100c7 |
permissions | -rw-r--r-- |
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1 |
/* |
50768 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
4 |
* |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
10 |
* |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
15 |
* accompanied this code). |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
16 |
* |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
20 |
* |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
23 |
* questions. |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
24 |
*/ |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
25 |
|
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
26 |
package sun.security.ssl; |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
27 |
|
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
28 |
import java.io.IOException; |
50768 | 29 |
import java.nio.ByteBuffer; |
30 |
import java.security.AccessController; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
31 |
import java.security.AlgorithmConstraints; |
50768 | 32 |
import java.security.AlgorithmParameters; |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
33 |
import java.security.CryptoPrimitive; |
56858 | 34 |
import java.security.GeneralSecurityException; |
50768 | 35 |
import java.security.NoSuchAlgorithmException; |
56858 | 36 |
import java.security.SecureRandom; |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
37 |
import java.security.spec.AlgorithmParameterSpec; |
50768 | 38 |
import java.security.spec.ECGenParameterSpec; |
39 |
import java.security.spec.ECParameterSpec; |
|
40 |
import java.security.spec.InvalidParameterSpecException; |
|
56858 | 41 |
import java.security.spec.NamedParameterSpec; |
50768 | 42 |
import java.text.MessageFormat; |
43 |
import java.util.ArrayList; |
|
44 |
import java.util.Collections; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
45 |
import java.util.EnumSet; |
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
46 |
import java.util.HashMap; |
50768 | 47 |
import java.util.LinkedList; |
48 |
import java.util.List; |
|
49 |
import java.util.Locale; |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
50 |
import java.util.Map; |
56858 | 51 |
import java.util.Optional; |
50768 | 52 |
import javax.crypto.spec.DHParameterSpec; |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
53 |
import javax.net.ssl.SSLProtocolException; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
54 |
import sun.security.action.GetPropertyAction; |
50768 | 55 |
import static sun.security.ssl.SSLExtension.CH_SUPPORTED_GROUPS; |
56 |
import static sun.security.ssl.SSLExtension.EE_SUPPORTED_GROUPS; |
|
57 |
import sun.security.ssl.SSLExtension.ExtensionConsumer; |
|
58 |
import sun.security.ssl.SSLExtension.SSLExtensionSpec; |
|
59 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
60 |
|
50768 | 61 |
/** |
62 |
* Pack of the "supported_groups" extensions [RFC 4492/7919]. |
|
63 |
*/ |
|
64 |
final class SupportedGroupsExtension { |
|
65 |
static final HandshakeProducer chNetworkProducer = |
|
66 |
new CHSupportedGroupsProducer(); |
|
67 |
static final ExtensionConsumer chOnLoadConsumer = |
|
68 |
new CHSupportedGroupsConsumer(); |
|
69 |
static final SSLStringizer sgsStringizer = |
|
70 |
new SupportedGroupsStringizer(); |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
71 |
|
50768 | 72 |
static final HandshakeProducer eeNetworkProducer = |
73 |
new EESupportedGroupsProducer(); |
|
74 |
static final ExtensionConsumer eeOnLoadConsumer = |
|
75 |
new EESupportedGroupsConsumer(); |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
76 |
|
50768 | 77 |
/** |
78 |
* The "supported_groups" extension. |
|
79 |
*/ |
|
80 |
static final class SupportedGroupsSpec implements SSLExtensionSpec { |
|
81 |
final int[] namedGroupsIds; |
|
82 |
||
83 |
private SupportedGroupsSpec(int[] namedGroupsIds) { |
|
84 |
this.namedGroupsIds = namedGroupsIds; |
|
85 |
} |
|
86 |
||
87 |
private SupportedGroupsSpec(List<NamedGroup> namedGroups) { |
|
88 |
this.namedGroupsIds = new int[namedGroups.size()]; |
|
89 |
int i = 0; |
|
90 |
for (NamedGroup ng : namedGroups) { |
|
91 |
namedGroupsIds[i++] = ng.id; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
92 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
93 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
94 |
|
50768 | 95 |
private SupportedGroupsSpec(ByteBuffer m) throws IOException { |
96 |
if (m.remaining() < 2) { // 2: the length of the list |
|
97 |
throw new SSLProtocolException( |
|
98 |
"Invalid supported_groups extension: insufficient data"); |
|
99 |
} |
|
100 |
||
101 |
byte[] ngs = Record.getBytes16(m); |
|
102 |
if (m.hasRemaining()) { |
|
103 |
throw new SSLProtocolException( |
|
104 |
"Invalid supported_groups extension: unknown extra data"); |
|
105 |
} |
|
106 |
||
107 |
if ((ngs == null) || (ngs.length == 0) || (ngs.length % 2 != 0)) { |
|
108 |
throw new SSLProtocolException( |
|
109 |
"Invalid supported_groups extension: incomplete data"); |
|
110 |
} |
|
111 |
||
112 |
int[] ids = new int[ngs.length / 2]; |
|
113 |
for (int i = 0, j = 0; i < ngs.length;) { |
|
114 |
ids[j++] = ((ngs[i++] & 0xFF) << 8) | (ngs[i++] & 0xFF); |
|
115 |
} |
|
116 |
||
117 |
this.namedGroupsIds = ids; |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public String toString() { |
|
122 |
MessageFormat messageFormat = new MessageFormat( |
|
123 |
"\"versions\": '['{0}']'", Locale.ENGLISH); |
|
124 |
||
125 |
if (namedGroupsIds == null || namedGroupsIds.length == 0) { |
|
126 |
Object[] messageFields = { |
|
127 |
"<no supported named group specified>" |
|
128 |
}; |
|
129 |
return messageFormat.format(messageFields); |
|
130 |
} else { |
|
131 |
StringBuilder builder = new StringBuilder(512); |
|
132 |
boolean isFirst = true; |
|
133 |
for (int ngid : namedGroupsIds) { |
|
134 |
if (isFirst) { |
|
135 |
isFirst = false; |
|
136 |
} else { |
|
137 |
builder.append(", "); |
|
138 |
} |
|
139 |
||
140 |
builder.append(NamedGroup.nameOf(ngid)); |
|
141 |
} |
|
142 |
||
143 |
Object[] messageFields = { |
|
144 |
builder.toString() |
|
145 |
}; |
|
146 |
||
147 |
return messageFormat.format(messageFields); |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
private static final |
|
153 |
class SupportedGroupsStringizer implements SSLStringizer { |
|
154 |
@Override |
|
155 |
public String toString(ByteBuffer buffer) { |
|
156 |
try { |
|
157 |
return (new SupportedGroupsSpec(buffer)).toString(); |
|
158 |
} catch (IOException ioe) { |
|
159 |
// For debug logging only, so please swallow exceptions. |
|
160 |
return ioe.getMessage(); |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
static enum NamedGroupType { |
|
166 |
NAMED_GROUP_ECDHE, // Elliptic Curve Groups (ECDHE) |
|
167 |
NAMED_GROUP_FFDHE, // Finite Field Groups (DHE) |
|
168 |
NAMED_GROUP_XDH, // Finite Field Groups (XDH) |
|
169 |
NAMED_GROUP_ARBITRARY, // arbitrary prime and curves (ECDHE) |
|
170 |
NAMED_GROUP_NONE; // Not predefined named group |
|
171 |
||
172 |
boolean isSupported(List<CipherSuite> cipherSuites) { |
|
173 |
for (CipherSuite cs : cipherSuites) { |
|
174 |
if (cs.keyExchange == null || cs.keyExchange.groupType == this) { |
|
175 |
return true; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
return false; |
|
180 |
} |
|
181 |
} |
|
182 |
||
56858 | 183 |
interface NamedGroupFunctions { |
184 |
||
185 |
SSLKeyAgreementCredentials decodeCredentials(byte[] encoded) |
|
186 |
throws IOException, GeneralSecurityException; |
|
187 |
||
188 |
SSLPossession createPossession(SecureRandom random); |
|
189 |
||
190 |
SSLKeyDerivation createKeyDerivation(HandshakeContext hc) |
|
191 |
throws IOException; |
|
192 |
||
193 |
AlgorithmParameterSpec getParameterSpec(); |
|
194 |
||
195 |
boolean isAvailable(); |
|
196 |
} |
|
197 |
||
198 |
private static class FFDHFunctions implements NamedGroupFunctions { |
|
199 |
||
200 |
private final NamedGroup ng; |
|
201 |
||
202 |
FFDHFunctions(NamedGroup ng) { |
|
203 |
this.ng = ng; |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public SSLKeyAgreementCredentials decodeCredentials(byte[] encoded) |
|
208 |
throws IOException, GeneralSecurityException { |
|
209 |
return DHKeyExchange.DHECredentials.valueOf(ng, encoded); |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
213 |
public SSLPossession createPossession(SecureRandom random) { |
|
214 |
return new DHKeyExchange.DHEPossession(ng, random); |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public SSLKeyDerivation createKeyDerivation(HandshakeContext hc) |
|
219 |
throws IOException { |
|
220 |
return DHKeyExchange.kaGenerator.createKeyDerivation(hc); |
|
221 |
} |
|
222 |
||
223 |
@Override |
|
224 |
public AlgorithmParameterSpec getParameterSpec() { |
|
225 |
return getDHParameterSpec(ng); |
|
226 |
} |
|
227 |
||
228 |
static DHParameterSpec getDHParameterSpec(NamedGroup namedGroup) { |
|
229 |
if (namedGroup.type != NamedGroupType.NAMED_GROUP_FFDHE) { |
|
230 |
throw new RuntimeException( |
|
231 |
"Not a named DH group: " + namedGroup); |
|
232 |
} |
|
233 |
||
234 |
AlgorithmParameters params = SupportedGroups.namedGroupParams.get(namedGroup); |
|
235 |
try { |
|
236 |
return params.getParameterSpec(DHParameterSpec.class); |
|
237 |
} catch (InvalidParameterSpecException ipse) { |
|
238 |
// should be unlikely |
|
239 |
return getPredefinedDHParameterSpec(namedGroup); |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
private static DHParameterSpec getFFDHEDHParameterSpec( |
|
244 |
NamedGroup namedGroup) { |
|
245 |
DHParameterSpec spec = null; |
|
246 |
switch (namedGroup) { |
|
247 |
case FFDHE_2048: |
|
248 |
spec = PredefinedDHParameterSpecs.ffdheParams.get(2048); |
|
249 |
break; |
|
250 |
case FFDHE_3072: |
|
251 |
spec = PredefinedDHParameterSpecs.ffdheParams.get(3072); |
|
252 |
break; |
|
253 |
case FFDHE_4096: |
|
254 |
spec = PredefinedDHParameterSpecs.ffdheParams.get(4096); |
|
255 |
break; |
|
256 |
case FFDHE_6144: |
|
257 |
spec = PredefinedDHParameterSpecs.ffdheParams.get(6144); |
|
258 |
break; |
|
259 |
case FFDHE_8192: |
|
260 |
spec = PredefinedDHParameterSpecs.ffdheParams.get(8192); |
|
261 |
} |
|
262 |
||
263 |
return spec; |
|
264 |
} |
|
265 |
||
266 |
private static DHParameterSpec getPredefinedDHParameterSpec( |
|
267 |
NamedGroup namedGroup) { |
|
268 |
DHParameterSpec spec = null; |
|
269 |
switch (namedGroup) { |
|
270 |
case FFDHE_2048: |
|
271 |
spec = PredefinedDHParameterSpecs.definedParams.get(2048); |
|
272 |
break; |
|
273 |
case FFDHE_3072: |
|
274 |
spec = PredefinedDHParameterSpecs.definedParams.get(3072); |
|
275 |
break; |
|
276 |
case FFDHE_4096: |
|
277 |
spec = PredefinedDHParameterSpecs.definedParams.get(4096); |
|
278 |
break; |
|
279 |
case FFDHE_6144: |
|
280 |
spec = PredefinedDHParameterSpecs.definedParams.get(6144); |
|
281 |
break; |
|
282 |
case FFDHE_8192: |
|
283 |
spec = PredefinedDHParameterSpecs.definedParams.get(8192); |
|
284 |
} |
|
285 |
||
286 |
return spec; |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
290 |
public boolean isAvailable() { |
|
291 |
||
292 |
try { |
|
293 |
AlgorithmParameters params = JsseJce.getAlgorithmParameters("DiffieHellman"); |
|
294 |
AlgorithmParameterSpec spec = getFFDHEDHParameterSpec(ng); |
|
295 |
params.init(spec); |
|
296 |
SupportedGroups.putNamedGroupParams(ng, params); |
|
297 |
return true; |
|
298 |
} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) { |
|
299 |
return false; |
|
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
private static class ECDHFunctions implements NamedGroupFunctions { |
|
305 |
||
306 |
private final NamedGroup ng; |
|
307 |
||
308 |
ECDHFunctions(NamedGroup ng) { |
|
309 |
this.ng = ng; |
|
310 |
} |
|
311 |
||
312 |
@Override |
|
313 |
public SSLKeyAgreementCredentials decodeCredentials(byte[] encoded) |
|
314 |
throws IOException, GeneralSecurityException { |
|
315 |
return ECDHKeyExchange.ECDHECredentials.valueOf(ng, encoded); |
|
316 |
} |
|
317 |
||
318 |
@Override |
|
319 |
public SSLPossession createPossession(SecureRandom random) { |
|
320 |
return new ECDHKeyExchange.ECDHEPossession(ng, random); |
|
321 |
} |
|
322 |
||
323 |
@Override |
|
324 |
public SSLKeyDerivation createKeyDerivation(HandshakeContext hc) |
|
325 |
throws IOException { |
|
326 |
return ECDHKeyExchange.ecdheKAGenerator.createKeyDerivation(hc); |
|
327 |
} |
|
328 |
||
329 |
@Override |
|
330 |
public AlgorithmParameterSpec getParameterSpec() { |
|
331 |
return SupportedGroups.getECGenParamSpec(ng); |
|
332 |
} |
|
333 |
||
334 |
@Override |
|
335 |
public boolean isAvailable() { |
|
336 |
||
337 |
try { |
|
338 |
AlgorithmParameters params = JsseJce.getAlgorithmParameters("EC"); |
|
339 |
AlgorithmParameterSpec spec = new ECGenParameterSpec(ng.oid); |
|
340 |
params.init(spec); |
|
341 |
SupportedGroups.putNamedGroupParams(ng, params); |
|
342 |
return true; |
|
343 |
} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) { |
|
344 |
return false; |
|
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
||
349 |
private static class XDHFunctions implements NamedGroupFunctions { |
|
350 |
||
351 |
private final NamedGroup ng; |
|
352 |
||
353 |
XDHFunctions(NamedGroup ng) { |
|
354 |
this.ng = ng; |
|
355 |
} |
|
356 |
||
357 |
@Override |
|
358 |
public SSLKeyAgreementCredentials decodeCredentials(byte[] encoded) |
|
359 |
throws IOException, GeneralSecurityException { |
|
360 |
return XDHKeyExchange.XDHECredentials.valueOf(ng, encoded); |
|
361 |
} |
|
362 |
||
363 |
@Override |
|
364 |
public SSLPossession createPossession(SecureRandom random) { |
|
365 |
return new XDHKeyExchange.XDHEPossession(ng, random); |
|
366 |
} |
|
367 |
||
368 |
@Override |
|
369 |
public SSLKeyDerivation createKeyDerivation(HandshakeContext hc) |
|
370 |
throws IOException { |
|
371 |
return XDHKeyExchange.xdheKAGenerator.createKeyDerivation(hc); |
|
372 |
} |
|
373 |
||
374 |
@Override |
|
375 |
public AlgorithmParameterSpec getParameterSpec() { |
|
376 |
return new NamedParameterSpec(ng.algorithm); |
|
377 |
} |
|
378 |
||
379 |
@Override |
|
380 |
public boolean isAvailable() { |
|
381 |
||
382 |
try { |
|
383 |
JsseJce.getKeyAgreement(ng.algorithm); |
|
384 |
return true; |
|
385 |
} catch (NoSuchAlgorithmException ex) { |
|
386 |
return false; |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
||
50768 | 391 |
static enum NamedGroup { |
392 |
// Elliptic Curves (RFC 4492) |
|
393 |
// |
|
394 |
// See sun.security.util.CurveDB for the OIDs |
|
395 |
// NIST K-163 |
|
396 |
SECT163_K1 (0x0001, "sect163k1", "1.3.132.0.1", true, |
|
397 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
398 |
SECT163_R1 (0x0002, "sect163r1", "1.3.132.0.2", false, |
|
399 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
400 |
||
401 |
// NIST B-163 |
|
402 |
SECT163_R2 (0x0003, "sect163r2", "1.3.132.0.15", true, |
|
403 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
404 |
SECT193_R1 (0x0004, "sect193r1", "1.3.132.0.24", false, |
|
405 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
406 |
SECT193_R2 (0x0005, "sect193r2", "1.3.132.0.25", false, |
|
407 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
408 |
||
409 |
// NIST K-233 |
|
410 |
SECT233_K1 (0x0006, "sect233k1", "1.3.132.0.26", true, |
|
411 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
412 |
||
413 |
// NIST B-233 |
|
414 |
SECT233_R1 (0x0007, "sect233r1", "1.3.132.0.27", true, |
|
415 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
416 |
SECT239_K1 (0x0008, "sect239k1", "1.3.132.0.3", false, |
|
417 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
418 |
||
419 |
// NIST K-283 |
|
420 |
SECT283_K1 (0x0009, "sect283k1", "1.3.132.0.16", true, |
|
421 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
422 |
||
423 |
// NIST B-283 |
|
424 |
SECT283_R1 (0x000A, "sect283r1", "1.3.132.0.17", true, |
|
425 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
426 |
||
427 |
// NIST K-409 |
|
428 |
SECT409_K1 (0x000B, "sect409k1", "1.3.132.0.36", true, |
|
429 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
430 |
||
431 |
// NIST B-409 |
|
432 |
SECT409_R1 (0x000C, "sect409r1", "1.3.132.0.37", true, |
|
433 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
434 |
||
435 |
// NIST K-571 |
|
436 |
SECT571_K1 (0x000D, "sect571k1", "1.3.132.0.38", true, |
|
437 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
438 |
||
439 |
// NIST B-571 |
|
440 |
SECT571_R1 (0x000E, "sect571r1", "1.3.132.0.39", true, |
|
441 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
442 |
SECP160_K1 (0x000F, "secp160k1", "1.3.132.0.9", false, |
|
443 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
444 |
SECP160_R1 (0x0010, "secp160r1", "1.3.132.0.8", false, |
|
445 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
446 |
SECP160_R2 (0x0011, "secp160r2", "1.3.132.0.30", false, |
|
447 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
448 |
SECP192_K1 (0x0012, "secp192k1", "1.3.132.0.31", false, |
|
449 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
450 |
||
451 |
// NIST P-192 |
|
452 |
SECP192_R1 (0x0013, "secp192r1", "1.2.840.10045.3.1.1", true, |
|
453 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
454 |
SECP224_K1 (0x0014, "secp224k1", "1.3.132.0.32", false, |
|
455 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
456 |
// NIST P-224 |
|
457 |
SECP224_R1 (0x0015, "secp224r1", "1.3.132.0.33", true, |
|
458 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
459 |
SECP256_K1 (0x0016, "secp256k1", "1.3.132.0.10", false, |
|
460 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
461 |
||
462 |
// NIST P-256 |
|
463 |
SECP256_R1 (0x0017, "secp256r1", "1.2.840.10045.3.1.7", true, |
|
464 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
465 |
||
466 |
// NIST P-384 |
|
467 |
SECP384_R1 (0x0018, "secp384r1", "1.3.132.0.34", true, |
|
468 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
469 |
||
470 |
// NIST P-521 |
|
471 |
SECP521_R1 (0x0019, "secp521r1", "1.3.132.0.35", true, |
|
472 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
473 |
||
474 |
// x25519 and x448 |
|
475 |
X25519 (0x001D, "x25519", true, "x25519", |
|
476 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
477 |
X448 (0x001E, "x448", true, "x448", |
|
478 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
479 |
||
480 |
// Finite Field Diffie-Hellman Ephemeral Parameters (RFC 7919) |
|
481 |
FFDHE_2048 (0x0100, "ffdhe2048", true, |
|
482 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
483 |
FFDHE_3072 (0x0101, "ffdhe3072", true, |
|
484 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
485 |
FFDHE_4096 (0x0102, "ffdhe4096", true, |
|
486 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
487 |
FFDHE_6144 (0x0103, "ffdhe6144", true, |
|
488 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
489 |
FFDHE_8192 (0x0104, "ffdhe8192", true, |
|
490 |
ProtocolVersion.PROTOCOLS_TO_13), |
|
491 |
||
492 |
// Elliptic Curves (RFC 4492) |
|
493 |
// |
|
494 |
// arbitrary prime and characteristic-2 curves |
|
495 |
ARBITRARY_PRIME (0xFF01, "arbitrary_explicit_prime_curves", |
|
496 |
ProtocolVersion.PROTOCOLS_TO_12), |
|
497 |
ARBITRARY_CHAR2 (0xFF02, "arbitrary_explicit_char2_curves", |
|
498 |
ProtocolVersion.PROTOCOLS_TO_12); |
|
499 |
||
500 |
final int id; // hash + signature |
|
501 |
final NamedGroupType type; // group type |
|
502 |
final String name; // literal name |
|
503 |
final String oid; // object identifier of the named group |
|
504 |
final String algorithm; // signature algorithm |
|
505 |
final boolean isFips; // can be used in FIPS mode? |
|
506 |
final ProtocolVersion[] supportedProtocols; |
|
56858 | 507 |
private final NamedGroupFunctions functions; // may be null |
50768 | 508 |
|
509 |
// Constructor used for Elliptic Curve Groups (ECDHE) |
|
510 |
private NamedGroup(int id, String name, String oid, boolean isFips, |
|
511 |
ProtocolVersion[] supportedProtocols) { |
|
512 |
this.id = id; |
|
513 |
this.type = NamedGroupType.NAMED_GROUP_ECDHE; |
|
56858 | 514 |
this.functions = new ECDHFunctions(this); |
50768 | 515 |
this.name = name; |
516 |
this.oid = oid; |
|
517 |
this.algorithm = "EC"; |
|
518 |
this.isFips = isFips; |
|
519 |
this.supportedProtocols = supportedProtocols; |
|
520 |
} |
|
521 |
||
522 |
// Constructor used for Elliptic Curve Groups (XDH) |
|
523 |
private NamedGroup(int id, String name, |
|
524 |
boolean isFips, String algorithm, |
|
525 |
ProtocolVersion[] supportedProtocols) { |
|
526 |
this.id = id; |
|
527 |
this.type = NamedGroupType.NAMED_GROUP_XDH; |
|
56858 | 528 |
this.functions = new XDHFunctions(this); |
50768 | 529 |
this.name = name; |
530 |
this.oid = null; |
|
531 |
this.algorithm = algorithm; |
|
532 |
this.isFips = isFips; |
|
533 |
this.supportedProtocols = supportedProtocols; |
|
534 |
} |
|
535 |
||
536 |
// Constructor used for Finite Field Diffie-Hellman Groups (FFDHE) |
|
537 |
private NamedGroup(int id, String name, boolean isFips, |
|
538 |
ProtocolVersion[] supportedProtocols) { |
|
539 |
this.id = id; |
|
540 |
this.type = NamedGroupType.NAMED_GROUP_FFDHE; |
|
56858 | 541 |
this.functions = new FFDHFunctions(this); |
50768 | 542 |
this.name = name; |
543 |
this.oid = null; |
|
544 |
this.algorithm = "DiffieHellman"; |
|
545 |
this.isFips = isFips; |
|
546 |
this.supportedProtocols = supportedProtocols; |
|
547 |
} |
|
548 |
||
549 |
// Constructor used for arbitrary prime and curves (ECDHE) |
|
550 |
private NamedGroup(int id, String name, |
|
551 |
ProtocolVersion[] supportedProtocols) { |
|
552 |
this.id = id; |
|
553 |
this.type = NamedGroupType.NAMED_GROUP_ARBITRARY; |
|
56858 | 554 |
this.functions = null; |
50768 | 555 |
this.name = name; |
556 |
this.oid = null; |
|
557 |
this.algorithm = "EC"; |
|
558 |
this.isFips = false; |
|
559 |
this.supportedProtocols = supportedProtocols; |
|
560 |
} |
|
561 |
||
56858 | 562 |
Optional<NamedGroupFunctions> getFunctions() { |
563 |
return Optional.ofNullable(functions); |
|
564 |
} |
|
565 |
||
50768 | 566 |
static NamedGroup valueOf(int id) { |
567 |
for (NamedGroup group : NamedGroup.values()) { |
|
568 |
if (group.id == id) { |
|
569 |
return group; |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
return null; |
|
574 |
} |
|
575 |
||
576 |
static NamedGroup valueOf(ECParameterSpec params) { |
|
577 |
String oid = JsseJce.getNamedCurveOid(params); |
|
578 |
if ((oid != null) && (!oid.isEmpty())) { |
|
579 |
for (NamedGroup group : NamedGroup.values()) { |
|
580 |
if ((group.type == NamedGroupType.NAMED_GROUP_ECDHE) && |
|
581 |
oid.equals(group.oid)) { |
|
582 |
return group; |
|
583 |
} |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
584 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
585 |
} |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
586 |
|
50768 | 587 |
return null; |
588 |
} |
|
589 |
||
590 |
static NamedGroup valueOf(DHParameterSpec params) { |
|
591 |
for (Map.Entry<NamedGroup, AlgorithmParameters> me : |
|
592 |
SupportedGroups.namedGroupParams.entrySet()) { |
|
593 |
NamedGroup ng = me.getKey(); |
|
594 |
if (ng.type != NamedGroupType.NAMED_GROUP_FFDHE) { |
|
595 |
continue; |
|
596 |
} |
|
597 |
||
598 |
DHParameterSpec ngParams = null; |
|
599 |
AlgorithmParameters aps = me.getValue(); |
|
600 |
try { |
|
601 |
ngParams = aps.getParameterSpec(DHParameterSpec.class); |
|
602 |
} catch (InvalidParameterSpecException ipse) { |
|
603 |
// should be unlikely |
|
604 |
} |
|
605 |
||
606 |
if (ngParams == null) { |
|
607 |
continue; |
|
608 |
} |
|
609 |
||
610 |
if (ngParams.getP().equals(params.getP()) && |
|
611 |
ngParams.getG().equals(params.getG())) { |
|
612 |
return ng; |
|
613 |
} |
|
43693
e340c8d10cbb
8173783: IllegalArgumentException: jdk.tls.namedGroups
coffeys
parents:
41473
diff
changeset
|
614 |
} |
50768 | 615 |
|
616 |
return null; |
|
617 |
} |
|
618 |
||
619 |
static NamedGroup nameOf(String name) { |
|
620 |
for (NamedGroup group : NamedGroup.values()) { |
|
621 |
if (group.name.equals(name)) { |
|
622 |
return group; |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
return null; |
|
627 |
} |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
628 |
|
50768 | 629 |
static String nameOf(int id) { |
630 |
for (NamedGroup group : NamedGroup.values()) { |
|
631 |
if (group.id == id) { |
|
632 |
return group.name; |
|
633 |
} |
|
634 |
} |
|
635 |
||
636 |
return "UNDEFINED-NAMED-GROUP(" + id + ")"; |
|
637 |
} |
|
638 |
||
639 |
boolean isAvailable(List<ProtocolVersion> protocolVersions) { |
|
640 |
for (ProtocolVersion pv : supportedProtocols) { |
|
641 |
if (protocolVersions.contains(pv)) { |
|
642 |
return true; |
|
643 |
} |
|
644 |
} |
|
645 |
return false; |
|
646 |
} |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
647 |
|
50768 | 648 |
boolean isAvailable(ProtocolVersion protocolVersion) { |
649 |
for (ProtocolVersion pv : supportedProtocols) { |
|
650 |
if (protocolVersion == pv) { |
|
651 |
return true; |
|
652 |
} |
|
653 |
} |
|
654 |
return false; |
|
655 |
} |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
656 |
|
50768 | 657 |
boolean isSupported(List<CipherSuite> cipherSuites) { |
658 |
for (CipherSuite cs : cipherSuites) { |
|
659 |
boolean isMatch = isAvailable(cs.supportedProtocols); |
|
660 |
if (isMatch && (cs.keyExchange == null || |
|
661 |
cs.keyExchange.groupType == type)) { |
|
662 |
return true; |
|
663 |
} |
|
664 |
} |
|
665 |
return false; |
|
666 |
} |
|
667 |
||
668 |
// lazy loading of parameters |
|
669 |
AlgorithmParameters getParameters() { |
|
670 |
return SupportedGroups.namedGroupParams.get(this); |
|
671 |
} |
|
672 |
||
673 |
AlgorithmParameterSpec getParameterSpec() { |
|
56858 | 674 |
Optional<NamedGroupFunctions> ngf = getFunctions(); |
675 |
if (ngf.isEmpty()) { |
|
676 |
return null; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
677 |
} |
56858 | 678 |
return ngf.get().getParameterSpec(); |
50768 | 679 |
} |
680 |
} |
|
681 |
||
682 |
static class SupportedGroups { |
|
683 |
// To switch off the supported_groups extension for DHE cipher suite. |
|
684 |
static final boolean enableFFDHE = |
|
685 |
Utilities.getBooleanProperty("jsse.enableFFDHE", true); |
|
686 |
||
687 |
// cache to speed up the parameters construction |
|
688 |
static final Map<NamedGroup, |
|
689 |
AlgorithmParameters> namedGroupParams = new HashMap<>(); |
|
690 |
||
691 |
// the supported named groups |
|
692 |
static final NamedGroup[] supportedNamedGroups; |
|
693 |
||
694 |
static { |
|
695 |
boolean requireFips = SunJSSE.isFIPS(); |
|
696 |
||
697 |
// The value of the System Property defines a list of enabled named |
|
698 |
// groups in preference order, separated with comma. For example: |
|
699 |
// |
|
700 |
// jdk.tls.namedGroups="secp521r1, secp256r1, ffdhe2048" |
|
701 |
// |
|
702 |
// If the System Property is not defined or the value is empty, the |
|
703 |
// default groups and preferences will be used. |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
50768
diff
changeset
|
704 |
String property = GetPropertyAction |
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
50768
diff
changeset
|
705 |
.privilegedGetProperty("jdk.tls.namedGroups"); |
50768 | 706 |
if (property != null && property.length() != 0) { |
707 |
// remove double quote marks from beginning/end of the property |
|
708 |
if (property.length() > 1 && property.charAt(0) == '"' && |
|
709 |
property.charAt(property.length() - 1) == '"') { |
|
710 |
property = property.substring(1, property.length() - 1); |
|
711 |
} |
|
712 |
} |
|
713 |
||
714 |
ArrayList<NamedGroup> groupList; |
|
715 |
if (property != null && property.length() != 0) { |
|
716 |
String[] groups = property.split(","); |
|
717 |
groupList = new ArrayList<>(groups.length); |
|
718 |
for (String group : groups) { |
|
719 |
group = group.trim(); |
|
720 |
if (!group.isEmpty()) { |
|
721 |
NamedGroup namedGroup = NamedGroup.nameOf(group); |
|
722 |
if (namedGroup != null && |
|
723 |
(!requireFips || namedGroup.isFips)) { |
|
724 |
if (isAvailableGroup(namedGroup)) { |
|
725 |
groupList.add(namedGroup); |
|
726 |
} |
|
727 |
} // ignore unknown groups |
|
728 |
} |
|
729 |
} |
|
730 |
||
731 |
if (groupList.isEmpty()) { |
|
732 |
throw new IllegalArgumentException( |
|
733 |
"System property jdk.tls.namedGroups(" + |
|
734 |
property + ") contains no supported named groups"); |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
735 |
} |
50768 | 736 |
} else { // default groups |
737 |
NamedGroup[] groups; |
|
738 |
if (requireFips) { |
|
739 |
groups = new NamedGroup[] { |
|
740 |
// only NIST curves in FIPS mode |
|
741 |
NamedGroup.SECP256_R1, |
|
742 |
NamedGroup.SECP384_R1, |
|
743 |
NamedGroup.SECP521_R1, |
|
744 |
NamedGroup.SECT283_K1, |
|
745 |
NamedGroup.SECT283_R1, |
|
746 |
NamedGroup.SECT409_K1, |
|
747 |
NamedGroup.SECT409_R1, |
|
748 |
NamedGroup.SECT571_K1, |
|
749 |
NamedGroup.SECT571_R1, |
|
750 |
||
751 |
// FFDHE 2048 |
|
752 |
NamedGroup.FFDHE_2048, |
|
753 |
NamedGroup.FFDHE_3072, |
|
754 |
NamedGroup.FFDHE_4096, |
|
755 |
NamedGroup.FFDHE_6144, |
|
756 |
NamedGroup.FFDHE_8192, |
|
757 |
}; |
|
758 |
} else { |
|
759 |
groups = new NamedGroup[] { |
|
760 |
// NIST curves first |
|
761 |
NamedGroup.SECP256_R1, |
|
762 |
NamedGroup.SECP384_R1, |
|
763 |
NamedGroup.SECP521_R1, |
|
764 |
NamedGroup.SECT283_K1, |
|
765 |
NamedGroup.SECT283_R1, |
|
766 |
NamedGroup.SECT409_K1, |
|
767 |
NamedGroup.SECT409_R1, |
|
768 |
NamedGroup.SECT571_K1, |
|
769 |
NamedGroup.SECT571_R1, |
|
770 |
||
771 |
// non-NIST curves |
|
772 |
NamedGroup.SECP256_K1, |
|
773 |
||
56858 | 774 |
// XDH |
775 |
NamedGroup.X25519, |
|
776 |
NamedGroup.X448, |
|
777 |
||
50768 | 778 |
// FFDHE 2048 |
779 |
NamedGroup.FFDHE_2048, |
|
780 |
NamedGroup.FFDHE_3072, |
|
781 |
NamedGroup.FFDHE_4096, |
|
782 |
NamedGroup.FFDHE_6144, |
|
783 |
NamedGroup.FFDHE_8192, |
|
784 |
}; |
|
785 |
} |
|
786 |
||
787 |
groupList = new ArrayList<>(groups.length); |
|
788 |
for (NamedGroup group : groups) { |
|
789 |
if (isAvailableGroup(group)) { |
|
790 |
groupList.add(group); |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
if (groupList.isEmpty() && |
|
795 |
SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
796 |
SSLLogger.warning("No default named groups"); |
|
797 |
} |
|
798 |
} |
|
799 |
||
800 |
supportedNamedGroups = new NamedGroup[groupList.size()]; |
|
801 |
int i = 0; |
|
802 |
for (NamedGroup namedGroup : groupList) { |
|
803 |
supportedNamedGroups[i++] = namedGroup; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
804 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
805 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
806 |
|
50768 | 807 |
// check whether the group is supported by the underlying providers |
808 |
private static boolean isAvailableGroup(NamedGroup namedGroup) { |
|
809 |
||
56858 | 810 |
Optional<NamedGroupFunctions> ngf = namedGroup.getFunctions(); |
811 |
if (ngf.isEmpty()) { |
|
812 |
return false; |
|
813 |
} |
|
814 |
return ngf.get().isAvailable(); |
|
50768 | 815 |
|
43693
e340c8d10cbb
8173783: IllegalArgumentException: jdk.tls.namedGroups
coffeys
parents:
41473
diff
changeset
|
816 |
} |
e340c8d10cbb
8173783: IllegalArgumentException: jdk.tls.namedGroups
coffeys
parents:
41473
diff
changeset
|
817 |
|
56858 | 818 |
static void putNamedGroupParams(NamedGroup ng, |
819 |
AlgorithmParameters params) { |
|
820 |
namedGroupParams.put(ng, params); |
|
50768 | 821 |
} |
822 |
||
823 |
static ECGenParameterSpec getECGenParamSpec(NamedGroup namedGroup) { |
|
824 |
if (namedGroup.type != NamedGroupType.NAMED_GROUP_ECDHE) { |
|
825 |
throw new RuntimeException( |
|
826 |
"Not a named EC group: " + namedGroup); |
|
827 |
} |
|
828 |
||
829 |
AlgorithmParameters params = namedGroupParams.get(namedGroup); |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
830 |
try { |
50768 | 831 |
return params.getParameterSpec(ECGenParameterSpec.class); |
832 |
} catch (InvalidParameterSpecException ipse) { |
|
833 |
// should be unlikely |
|
834 |
return new ECGenParameterSpec(namedGroup.oid); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
835 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
836 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
837 |
|
50768 | 838 |
static DHParameterSpec getDHParameterSpec(NamedGroup namedGroup) { |
839 |
if (namedGroup.type != NamedGroupType.NAMED_GROUP_FFDHE) { |
|
840 |
throw new RuntimeException( |
|
841 |
"Not a named DH group: " + namedGroup); |
|
842 |
} |
|
843 |
||
844 |
AlgorithmParameters params = namedGroupParams.get(namedGroup); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
845 |
try { |
50768 | 846 |
return params.getParameterSpec(DHParameterSpec.class); |
847 |
} catch (InvalidParameterSpecException ipse) { |
|
848 |
// should be unlikely |
|
849 |
return getPredefinedDHParameterSpec(namedGroup); |
|
850 |
} |
|
851 |
} |
|
852 |
||
853 |
// Is there any supported group permitted by the constraints? |
|
854 |
static boolean isActivatable( |
|
855 |
AlgorithmConstraints constraints, NamedGroupType type) { |
|
856 |
||
857 |
boolean hasFFDHEGroups = false; |
|
858 |
for (NamedGroup namedGroup : supportedNamedGroups) { |
|
859 |
if (namedGroup.type == type) { |
|
860 |
if (constraints.permits( |
|
861 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
862 |
namedGroup.algorithm, |
|
863 |
namedGroupParams.get(namedGroup))) { |
|
864 |
||
865 |
return true; |
|
866 |
} |
|
867 |
||
868 |
if (!hasFFDHEGroups && |
|
869 |
(type == NamedGroupType.NAMED_GROUP_FFDHE)) { |
|
870 |
hasFFDHEGroups = true; |
|
871 |
} |
|
872 |
} |
|
873 |
} |
|
874 |
||
875 |
// For compatibility, if no FFDHE groups are defined, the non-FFDHE |
|
876 |
// compatible mode (using DHE cipher suite without FFDHE extension) |
|
877 |
// is allowed. |
|
878 |
// |
|
879 |
// Note that the constraints checking on DHE parameters will be |
|
880 |
// performed during key exchanging in a handshake. |
|
881 |
return !hasFFDHEGroups && type == NamedGroupType.NAMED_GROUP_FFDHE; |
|
882 |
} |
|
883 |
||
884 |
// Is the named group permitted by the constraints? |
|
885 |
static boolean isActivatable( |
|
886 |
AlgorithmConstraints constraints, NamedGroup namedGroup) { |
|
887 |
if (!isSupported(namedGroup)) { |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
888 |
return false; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
889 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
890 |
|
50768 | 891 |
return constraints.permits( |
892 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
893 |
namedGroup.algorithm, |
|
894 |
namedGroupParams.get(namedGroup)); |
|
895 |
} |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
896 |
|
50768 | 897 |
// Is there any supported group permitted by the constraints? |
898 |
static boolean isSupported(NamedGroup namedGroup) { |
|
899 |
for (NamedGroup group : supportedNamedGroups) { |
|
900 |
if (namedGroup.id == group.id) { |
|
901 |
return true; |
|
902 |
} |
|
903 |
} |
|
904 |
||
905 |
return false; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
906 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
907 |
|
50768 | 908 |
static NamedGroup getPreferredGroup( |
909 |
ProtocolVersion negotiatedProtocol, |
|
910 |
AlgorithmConstraints constraints, NamedGroupType type, |
|
911 |
List<NamedGroup> requestedNamedGroups) { |
|
912 |
for (NamedGroup namedGroup : requestedNamedGroups) { |
|
913 |
if ((namedGroup.type == type) && |
|
914 |
namedGroup.isAvailable(negotiatedProtocol) && |
|
915 |
constraints.permits( |
|
916 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
917 |
namedGroup.algorithm, |
|
918 |
namedGroupParams.get(namedGroup))) { |
|
919 |
return namedGroup; |
|
920 |
} |
|
921 |
} |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
922 |
|
50768 | 923 |
return null; |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
924 |
} |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
925 |
|
50768 | 926 |
static NamedGroup getPreferredGroup( |
927 |
ProtocolVersion negotiatedProtocol, |
|
928 |
AlgorithmConstraints constraints, NamedGroupType type) { |
|
929 |
for (NamedGroup namedGroup : supportedNamedGroups) { |
|
930 |
if ((namedGroup.type == type) && |
|
931 |
namedGroup.isAvailable(negotiatedProtocol) && |
|
932 |
constraints.permits( |
|
933 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
934 |
namedGroup.algorithm, |
|
935 |
namedGroupParams.get(namedGroup))) { |
|
936 |
return namedGroup; |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
return null; |
|
941 |
} |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
942 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
943 |
|
50768 | 944 |
/** |
945 |
* Network data producer of a "supported_groups" extension in |
|
946 |
* the ClientHello handshake message. |
|
947 |
*/ |
|
948 |
private static final class CHSupportedGroupsProducer |
|
949 |
extends SupportedGroups implements HandshakeProducer { |
|
950 |
// Prevent instantiation of this class. |
|
951 |
private CHSupportedGroupsProducer() { |
|
952 |
// blank |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
953 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
954 |
|
50768 | 955 |
@Override |
956 |
public byte[] produce(ConnectionContext context, |
|
957 |
HandshakeMessage message) throws IOException { |
|
958 |
// The producing happens in client side only. |
|
959 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
960 |
|
50768 | 961 |
// Is it a supported and enabled extension? |
962 |
if (!chc.sslConfig.isAvailable(CH_SUPPORTED_GROUPS)) { |
|
963 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
964 |
SSLLogger.fine( |
|
965 |
"Ignore unavailable supported_groups extension"); |
|
966 |
} |
|
967 |
return null; |
|
968 |
} |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
969 |
|
50768 | 970 |
// Produce the extension. |
971 |
ArrayList<NamedGroup> namedGroups = |
|
972 |
new ArrayList<>(SupportedGroups.supportedNamedGroups.length); |
|
973 |
for (NamedGroup ng : SupportedGroups.supportedNamedGroups) { |
|
974 |
if ((!SupportedGroups.enableFFDHE) && |
|
975 |
(ng.type == NamedGroupType.NAMED_GROUP_FFDHE)) { |
|
976 |
continue; |
|
977 |
} |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
978 |
|
50768 | 979 |
if (ng.isAvailable(chc.activeProtocols) && |
980 |
ng.isSupported(chc.activeCipherSuites) && |
|
981 |
chc.algorithmConstraints.permits( |
|
982 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
983 |
ng.algorithm, namedGroupParams.get(ng))) { |
|
984 |
namedGroups.add(ng); |
|
985 |
} else if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
986 |
SSLLogger.fine( |
|
987 |
"Ignore inactive or disabled named group: " + ng.name); |
|
988 |
} |
|
989 |
} |
|
990 |
||
991 |
if (namedGroups.isEmpty()) { |
|
992 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
993 |
SSLLogger.warning("no available named group"); |
|
994 |
} |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
995 |
|
50768 | 996 |
return null; |
997 |
} |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
998 |
|
50768 | 999 |
int vectorLen = namedGroups.size() << 1; |
1000 |
byte[] extData = new byte[vectorLen + 2]; |
|
1001 |
ByteBuffer m = ByteBuffer.wrap(extData); |
|
1002 |
Record.putInt16(m, vectorLen); |
|
1003 |
for (NamedGroup namedGroup : namedGroups) { |
|
1004 |
Record.putInt16(m, namedGroup.id); |
|
1005 |
} |
|
1006 |
||
1007 |
// Update the context. |
|
1008 |
chc.clientRequestedNamedGroups = |
|
1009 |
Collections.<NamedGroup>unmodifiableList(namedGroups); |
|
1010 |
chc.handshakeExtensions.put(CH_SUPPORTED_GROUPS, |
|
1011 |
new SupportedGroupsSpec(namedGroups)); |
|
1012 |
||
1013 |
return extData; |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1014 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1015 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1016 |
|
50768 | 1017 |
/** |
1018 |
* Network data producer of a "supported_groups" extension in |
|
1019 |
* the ClientHello handshake message. |
|
1020 |
*/ |
|
1021 |
private static final |
|
1022 |
class CHSupportedGroupsConsumer implements ExtensionConsumer { |
|
1023 |
// Prevent instantiation of this class. |
|
1024 |
private CHSupportedGroupsConsumer() { |
|
1025 |
// blank |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1026 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1027 |
|
50768 | 1028 |
@Override |
1029 |
public void consume(ConnectionContext context, |
|
1030 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
1031 |
// The consuming happens in server side only. |
|
1032 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1033 |
|
50768 | 1034 |
// Is it a supported and enabled extension? |
1035 |
if (!shc.sslConfig.isAvailable(CH_SUPPORTED_GROUPS)) { |
|
1036 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1037 |
SSLLogger.fine( |
|
1038 |
"Ignore unavailable supported_groups extension"); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1039 |
} |
50768 | 1040 |
return; // ignore the extension |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1041 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1042 |
|
50768 | 1043 |
// Parse the extension. |
1044 |
SupportedGroupsSpec spec; |
|
1045 |
try { |
|
1046 |
spec = new SupportedGroupsSpec(buffer); |
|
1047 |
} catch (IOException ioe) { |
|
1048 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
1049 |
return; // fatal() always throws, make the compiler happy. |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1050 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1051 |
|
50768 | 1052 |
// Update the context. |
1053 |
List<NamedGroup> knownNamedGroups = new LinkedList<>(); |
|
1054 |
for (int id : spec.namedGroupsIds) { |
|
1055 |
NamedGroup ng = NamedGroup.valueOf(id); |
|
1056 |
if (ng != null) { |
|
1057 |
knownNamedGroups.add(ng); |
|
1058 |
} |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1059 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1060 |
|
50768 | 1061 |
shc.clientRequestedNamedGroups = knownNamedGroups; |
1062 |
shc.handshakeExtensions.put(CH_SUPPORTED_GROUPS, spec); |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1063 |
|
50768 | 1064 |
// No impact on session resumption. |
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1065 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1066 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1067 |
|
50768 | 1068 |
/** |
1069 |
* Network data producer of a "supported_groups" extension in |
|
1070 |
* the EncryptedExtensions handshake message. |
|
1071 |
*/ |
|
1072 |
private static final class EESupportedGroupsProducer |
|
1073 |
extends SupportedGroups implements HandshakeProducer { |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1074 |
|
50768 | 1075 |
// Prevent instantiation of this class. |
1076 |
private EESupportedGroupsProducer() { |
|
1077 |
// blank |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1078 |
} |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1079 |
|
50768 | 1080 |
@Override |
1081 |
public byte[] produce(ConnectionContext context, |
|
1082 |
HandshakeMessage message) throws IOException { |
|
1083 |
// The producing happens in server side only. |
|
1084 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
1085 |
||
1086 |
// Is it a supported and enabled extension? |
|
1087 |
if (!shc.sslConfig.isAvailable(EE_SUPPORTED_GROUPS)) { |
|
1088 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1089 |
SSLLogger.fine( |
|
1090 |
"Ignore unavailable supported_groups extension"); |
|
1091 |
} |
|
1092 |
return null; |
|
1093 |
} |
|
1094 |
||
1095 |
// Produce the extension. |
|
1096 |
// |
|
1097 |
// Contains all groups the server supports, regardless of whether |
|
1098 |
// they are currently supported by the client. |
|
1099 |
ArrayList<NamedGroup> namedGroups = new ArrayList<>( |
|
1100 |
SupportedGroups.supportedNamedGroups.length); |
|
1101 |
for (NamedGroup ng : SupportedGroups.supportedNamedGroups) { |
|
1102 |
if ((!SupportedGroups.enableFFDHE) && |
|
1103 |
(ng.type == NamedGroupType.NAMED_GROUP_FFDHE)) { |
|
1104 |
continue; |
|
1105 |
} |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1106 |
|
50768 | 1107 |
if (ng.isAvailable(shc.activeProtocols) && |
1108 |
ng.isSupported(shc.activeCipherSuites) && |
|
1109 |
shc.algorithmConstraints.permits( |
|
1110 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
1111 |
ng.algorithm, namedGroupParams.get(ng))) { |
|
1112 |
namedGroups.add(ng); |
|
1113 |
} else if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1114 |
SSLLogger.fine( |
|
1115 |
"Ignore inactive or disabled named group: " + ng.name); |
|
1116 |
} |
|
1117 |
} |
|
1118 |
||
1119 |
if (namedGroups.isEmpty()) { |
|
1120 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1121 |
SSLLogger.warning("no available named group"); |
|
1122 |
} |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1123 |
|
50768 | 1124 |
return null; |
1125 |
} |
|
1126 |
||
1127 |
int vectorLen = namedGroups.size() << 1; |
|
1128 |
byte[] extData = new byte[vectorLen + 2]; |
|
1129 |
ByteBuffer m = ByteBuffer.wrap(extData); |
|
1130 |
Record.putInt16(m, vectorLen); |
|
1131 |
for (NamedGroup namedGroup : namedGroups) { |
|
1132 |
Record.putInt16(m, namedGroup.id); |
|
1133 |
} |
|
1134 |
||
1135 |
// Update the context. |
|
1136 |
shc.conContext.serverRequestedNamedGroups = |
|
1137 |
Collections.<NamedGroup>unmodifiableList(namedGroups); |
|
1138 |
SupportedGroupsSpec spec = new SupportedGroupsSpec(namedGroups); |
|
1139 |
shc.handshakeExtensions.put(EE_SUPPORTED_GROUPS, spec); |
|
1140 |
||
1141 |
return extData; |
|
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1142 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1143 |
} |
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1144 |
|
50768 | 1145 |
private static final |
1146 |
class EESupportedGroupsConsumer implements ExtensionConsumer { |
|
1147 |
// Prevent instantiation of this class. |
|
1148 |
private EESupportedGroupsConsumer() { |
|
1149 |
// blank |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1150 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1151 |
|
50768 | 1152 |
@Override |
1153 |
public void consume(ConnectionContext context, |
|
1154 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
1155 |
// The consuming happens in client side only. |
|
1156 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
1157 |
||
1158 |
// Is it a supported and enabled extension? |
|
1159 |
if (!chc.sslConfig.isAvailable(EE_SUPPORTED_GROUPS)) { |
|
1160 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1161 |
SSLLogger.fine( |
|
1162 |
"Ignore unavailable supported_groups extension"); |
|
1163 |
} |
|
1164 |
return; // ignore the extension |
|
1165 |
} |
|
1166 |
||
1167 |
// Parse the extension. |
|
1168 |
SupportedGroupsSpec spec; |
|
1169 |
try { |
|
1170 |
spec = new SupportedGroupsSpec(buffer); |
|
1171 |
} catch (IOException ioe) { |
|
1172 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
1173 |
return; // fatal() always throws, make the compiler happy. |
|
1174 |
} |
|
1175 |
||
1176 |
// Update the context. |
|
1177 |
List<NamedGroup> knownNamedGroups = |
|
1178 |
new ArrayList<>(spec.namedGroupsIds.length); |
|
1179 |
for (int id : spec.namedGroupsIds) { |
|
1180 |
NamedGroup ng = NamedGroup.valueOf(id); |
|
1181 |
if (ng != null) { |
|
1182 |
knownNamedGroups.add(ng); |
|
1183 |
} |
|
1184 |
} |
|
1185 |
||
1186 |
chc.conContext.serverRequestedNamedGroups = knownNamedGroups; |
|
1187 |
chc.handshakeExtensions.put(EE_SUPPORTED_GROUPS, spec); |
|
1188 |
||
1189 |
// No impact on session resumption. |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
43693
diff
changeset
|
1190 |
} |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
37814
diff
changeset
|
1191 |
} |
14330
e4cb78065603
8000970: break out auxiliary classes that will prevent multi-core compilation of the JDK
ohrstrom
parents:
diff
changeset
|
1192 |
} |