forked from bcgit/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamedGroupInfo.java
636 lines (553 loc) · 20.7 KB
/
NamedGroupInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package org.bouncycastle.jsse.provider;
import java.security.AlgorithmParameters;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import org.bouncycastle.jsse.java.security.BCAlgorithmConstraints;
import org.bouncycastle.jsse.java.security.BCCryptoPrimitive;
import org.bouncycastle.tls.NamedGroup;
import org.bouncycastle.tls.ProtocolVersion;
import org.bouncycastle.tls.TlsUtils;
import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCrypto;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Integers;
class NamedGroupInfo
{
private static final Logger LOG = Logger.getLogger(NamedGroupInfo.class.getName());
private static final String PROPERTY_NAMED_GROUPS = "jdk.tls.namedGroups";
// NOTE: Not all of these are necessarily enabled/supported; it will be checked at runtime
private enum All
{
sect163k1(NamedGroup.sect163k1, "EC"),
sect163r1(NamedGroup.sect163r1, "EC"),
sect163r2(NamedGroup.sect163r2, "EC"),
sect193r1(NamedGroup.sect193r1, "EC"),
sect193r2(NamedGroup.sect193r2, "EC"),
sect233k1(NamedGroup.sect233k1, "EC"),
sect233r1(NamedGroup.sect233r1, "EC"),
sect239k1(NamedGroup.sect239k1, "EC"),
sect283k1(NamedGroup.sect283k1, "EC"),
sect283r1(NamedGroup.sect283r1, "EC"),
sect409k1(NamedGroup.sect409k1, "EC"),
sect409r1(NamedGroup.sect409r1, "EC"),
sect571k1(NamedGroup.sect571k1, "EC"),
sect571r1(NamedGroup.sect571r1, "EC"),
secp160k1(NamedGroup.secp160k1, "EC"),
secp160r1(NamedGroup.secp160r1, "EC"),
secp160r2(NamedGroup.secp160r2, "EC"),
secp192k1(NamedGroup.secp192k1, "EC"),
secp192r1(NamedGroup.secp192r1, "EC"),
secp224k1(NamedGroup.secp224k1, "EC"),
secp224r1(NamedGroup.secp224r1, "EC"),
secp256k1(NamedGroup.secp256k1, "EC"),
secp256r1(NamedGroup.secp256r1, "EC"),
secp384r1(NamedGroup.secp384r1, "EC"),
secp521r1(NamedGroup.secp521r1, "EC"),
brainpoolP256r1(NamedGroup.brainpoolP256r1, "EC"),
brainpoolP384r1(NamedGroup.brainpoolP384r1, "EC"),
brainpoolP512r1(NamedGroup.brainpoolP512r1, "EC"),
x25519(NamedGroup.x25519, "XDH"),
x448(NamedGroup.x448, "XDH"),
brainpoolP256r1tls13(NamedGroup.brainpoolP256r1tls13, "EC"),
brainpoolP384r1tls13(NamedGroup.brainpoolP384r1tls13, "EC"),
brainpoolP512r1tls13(NamedGroup.brainpoolP512r1tls13, "EC"),
curveSM2(NamedGroup.curveSM2, "EC"),
ffdhe2048(NamedGroup.ffdhe2048, "DiffieHellman"),
ffdhe3072(NamedGroup.ffdhe3072, "DiffieHellman"),
ffdhe4096(NamedGroup.ffdhe4096, "DiffieHellman"),
ffdhe6144(NamedGroup.ffdhe6144, "DiffieHellman"),
ffdhe8192(NamedGroup.ffdhe8192, "DiffieHellman"),
kyber512(NamedGroup.kyber512, "KEM"),
kyber768(NamedGroup.kyber768, "KEM"),
kyber1024(NamedGroup.kyber1024, "KEM");
private final int namedGroup;
private final String name;
private final String text;
private final String jcaAlgorithm;
private final String jcaGroup;
private final boolean char2;
private final boolean supportedPost13;
private final boolean supportedPre13;
private final int bitsECDH;
private final int bitsFFDHE;
private All(int namedGroup, String jcaAlgorithm)
{
this.namedGroup = namedGroup;
this.name = NamedGroup.getName(namedGroup);
this.text = NamedGroup.getText(namedGroup);
this.jcaAlgorithm = jcaAlgorithm;
this.jcaGroup = NamedGroup.getStandardName(namedGroup);
this.supportedPost13 = NamedGroup.canBeNegotiated(namedGroup, ProtocolVersion.TLSv13);
this.supportedPre13 = NamedGroup.canBeNegotiated(namedGroup, ProtocolVersion.TLSv12);
this.char2 = NamedGroup.isChar2Curve(namedGroup);
this.bitsECDH = NamedGroup.getCurveBits(namedGroup);
this.bitsFFDHE = NamedGroup.getFiniteFieldBits(namedGroup);
}
}
private static final int[] CANDIDATES_DEFAULT = {
NamedGroup.x25519,
NamedGroup.x448,
NamedGroup.secp256r1,
NamedGroup.secp384r1,
NamedGroup.secp521r1,
NamedGroup.brainpoolP256r1tls13,
NamedGroup.brainpoolP384r1tls13,
NamedGroup.brainpoolP512r1tls13,
NamedGroup.ffdhe2048,
NamedGroup.ffdhe3072,
NamedGroup.ffdhe4096,
};
static class PerConnection
{
private final LinkedHashMap<Integer, NamedGroupInfo> local;
private final boolean localECDSA;
private final AtomicReference<List<NamedGroupInfo>> peer;
PerConnection(LinkedHashMap<Integer, NamedGroupInfo> local, boolean localECDSA)
{
if (local == null)
{
throw new NullPointerException("local");
}
this.local = local;
this.localECDSA = localECDSA;
this.peer = new AtomicReference<List<NamedGroupInfo>>();
}
List<NamedGroupInfo> getPeer()
{
return peer.get();
}
void notifyPeerData(int[] namedGroups)
{
// TODO[jsse] Is there any reason to preserve the unrecognized/disabled groups?
List<NamedGroupInfo> namedGroupInfos = getNamedGroupInfos(local, namedGroups);
peer.set(namedGroupInfos);
}
}
static class PerContext
{
private final Map<Integer, NamedGroupInfo> index;
private final int[] candidates;
PerContext(Map<Integer, NamedGroupInfo> index, int[] candidates)
{
this.index = index;
this.candidates = candidates;
}
}
static PerConnection createPerConnectionClient(PerContext perContext, ProvSSLParameters sslParameters,
ProtocolVersion[] activeProtocolVersions)
{
ProtocolVersion latest = ProtocolVersion.getLatestTLS(activeProtocolVersions);
ProtocolVersion earliest = ProtocolVersion.getEarliestTLS(activeProtocolVersions);
return createPerConnection(perContext, sslParameters, earliest, latest);
}
static PerConnection createPerConnectionServer(PerContext perContext, ProvSSLParameters sslParameters,
ProtocolVersion negotiatedVersion)
{
return createPerConnection(perContext, sslParameters, negotiatedVersion, negotiatedVersion);
}
private static PerConnection createPerConnection(PerContext perContext, ProvSSLParameters sslParameters,
ProtocolVersion earliest, ProtocolVersion latest)
{
String[] namedGroups = sslParameters.getNamedGroups();
int[] candidates;
if (namedGroups == null)
{
candidates = perContext.candidates;
}
else
{
candidates = createCandidates(perContext.index, namedGroups, "SSLParameters.namedGroups");
}
BCAlgorithmConstraints algorithmConstraints = sslParameters.getAlgorithmConstraints();
boolean post13Active = TlsUtils.isTLSv13(latest);
boolean pre13Active = !TlsUtils.isTLSv13(earliest);
int count = candidates.length;
LinkedHashMap<Integer, NamedGroupInfo> local = new LinkedHashMap<Integer, NamedGroupInfo>(count);
for (int i = 0; i < count; ++i)
{
Integer candidate = Integers.valueOf(candidates[i]);
NamedGroupInfo namedGroupInfo = perContext.index.get(candidate);
if (null != namedGroupInfo
&& namedGroupInfo.isActive(algorithmConstraints, post13Active, pre13Active))
{
// NOTE: Re-insertion doesn't affect iteration order for insertion-order LinkedHashMap
local.put(candidate, namedGroupInfo);
}
}
boolean localECDSA = hasAnyECDSA(local);
return new PerConnection(local, localECDSA);
}
static PerContext createPerContext(boolean isFipsContext, JcaTlsCrypto crypto)
{
Map<Integer, NamedGroupInfo> index = createIndex(isFipsContext, crypto);
int[] candidates = createCandidatesFromProperty(index, PROPERTY_NAMED_GROUPS);
return new PerContext(index, candidates);
}
static int getMaximumBitsServerECDH(PerConnection perConnection)
{
int maxBits = 0;
List<NamedGroupInfo> peer = perConnection.getPeer();
if (peer != null)
{
for (NamedGroupInfo namedGroupInfo : peer)
{
int bits = namedGroupInfo.getBitsECDH();
if (bits > maxBits)
{
if (perConnection.local.containsKey(namedGroupInfo.getNamedGroup()))
{
maxBits = bits;
}
}
}
}
else
{
/*
* RFC 4492 4. A client that proposes ECC cipher suites may choose not to include these
* extensions. In this case, the server is free to choose any one of the elliptic curves or point
* formats [...].
*/
for (NamedGroupInfo namedGroupInfo : perConnection.local.values())
{
maxBits = Math.max(maxBits, namedGroupInfo.getBitsECDH());
}
}
return maxBits;
}
static int getMaximumBitsServerFFDHE(PerConnection perConnection)
{
int maxBits = 0;
boolean anyPeerFF = false;
List<NamedGroupInfo> peer = perConnection.getPeer();
if (peer != null)
{
for (NamedGroupInfo namedGroupInfo : peer)
{
int namedGroup = namedGroupInfo.getNamedGroup();
anyPeerFF |= NamedGroup.isFiniteField(namedGroup);
int bits = namedGroupInfo.getBitsFFDHE();
if (bits > maxBits)
{
if (perConnection.local.containsKey(namedGroup))
{
maxBits = bits;
}
}
}
}
if (!anyPeerFF)
{
/*
* RFC 7919 4. If [...] the Supported Groups extension is either absent from the ClientHello
* entirely or contains no FFDHE groups (i.e., no codepoints between 256 and 511, inclusive), then
* the server [...] MAY select an FFDHE cipher suite and offer an FFDHE group of its choice [...].
*/
for (NamedGroupInfo namedGroupInfo : perConnection.local.values())
{
maxBits = Math.max(maxBits, namedGroupInfo.getBitsFFDHE());
}
}
return maxBits;
}
static NamedGroupInfo getNamedGroup(PerContext perContext, int namedGroup)
{
return perContext.index.get(namedGroup);
}
static Vector<Integer> getSupportedGroupsLocalClient(PerConnection perConnection)
{
return new Vector<Integer>(perConnection.local.keySet());
}
static int[] getSupportedGroupsLocalServer(PerConnection perConnection)
{
Set<Integer> keys = perConnection.local.keySet();
int count = keys.size(), pos = 0;
int[] result = new int[count];
for (Integer key : keys)
{
result[pos++] = key.intValue();
}
return result;
}
static boolean hasAnyECDSALocal(PerConnection perConnection)
{
return perConnection.localECDSA;
}
static boolean hasLocal(PerConnection perConnection, int namedGroup)
{
return perConnection.local.containsKey(namedGroup);
}
static int selectServerECDH(PerConnection perConnection, int minimumBitsECDH)
{
List<NamedGroupInfo> peer = perConnection.getPeer();
if (peer != null)
{
for (NamedGroupInfo namedGroupInfo : peer)
{
if (namedGroupInfo.getBitsECDH() >= minimumBitsECDH)
{
int namedGroup = namedGroupInfo.getNamedGroup();
if (perConnection.local.containsKey(namedGroup))
{
return namedGroup;
}
}
}
}
else
{
/*
* RFC 4492 4. A client that proposes ECC cipher suites may choose not to include these
* extensions. In this case, the server is free to choose any one of the elliptic curves or point
* formats [...].
*/
for (NamedGroupInfo namedGroupInfo : perConnection.local.values())
{
if (namedGroupInfo.getBitsECDH() >= minimumBitsECDH)
{
return namedGroupInfo.getNamedGroup();
}
}
}
return -1;
}
static int selectServerFFDHE(PerConnection perConnection, int minimumBitsFFDHE)
{
boolean anyPeerFF = false;
List<NamedGroupInfo> peer = perConnection.getPeer();
if (peer != null)
{
for (NamedGroupInfo namedGroupInfo : peer)
{
int namedGroup = namedGroupInfo.getNamedGroup();
anyPeerFF |= NamedGroup.isFiniteField(namedGroup);
if (namedGroupInfo.getBitsFFDHE() >= minimumBitsFFDHE)
{
if (perConnection.local.containsKey(namedGroup))
{
return namedGroup;
}
}
}
}
if (!anyPeerFF)
{
/*
* RFC 7919 4. If [...] the Supported Groups extension is either absent from the ClientHello
* entirely or contains no FFDHE groups (i.e., no codepoints between 256 and 511, inclusive), then
* the server [...] MAY select an FFDHE cipher suite and offer an FFDHE group of its choice [...].
*/
for (NamedGroupInfo namedGroupInfo : perConnection.local.values())
{
if (namedGroupInfo.getBitsFFDHE() >= minimumBitsFFDHE)
{
return namedGroupInfo.getNamedGroup();
}
}
}
return -1;
}
private static void addNamedGroup(boolean isFipsContext, JcaTlsCrypto crypto, boolean disableChar2,
boolean disableFFDHE, Map<Integer, NamedGroupInfo> ng, All all)
{
final int namedGroup = all.namedGroup;
if (isFipsContext && !FipsUtils.isFipsNamedGroup(namedGroup))
{
// In FIPS mode, non-FIPS groups are currently not even entered into the map
return;
}
boolean disable = (disableChar2 && all.char2) || (disableFFDHE && all.bitsFFDHE > 0);
boolean enabled = !disable && (null != all.jcaGroup) && crypto.hasNamedGroup(namedGroup);
AlgorithmParameters algorithmParameters = null;
if (enabled)
{
// TODO[jsse] Consider also fetching 'jcaAlgorithm'
try
{
algorithmParameters = crypto.getNamedGroupAlgorithmParameters(namedGroup);
}
catch (Exception e)
{
enabled = false;
}
}
NamedGroupInfo namedGroupInfo = new NamedGroupInfo(all, algorithmParameters, enabled);
if (null != ng.put(namedGroup, namedGroupInfo))
{
throw new IllegalStateException("Duplicate entries for NamedGroupInfo");
}
}
private static int[] createCandidatesFromProperty(Map<Integer, NamedGroupInfo> index, String propertyName)
{
String[] names = PropertyUtils.getStringArraySystemProperty(propertyName);
if (null == names)
{
return CANDIDATES_DEFAULT;
}
return createCandidates(index, names, propertyName);
}
private static int[] createCandidates(Map<Integer, NamedGroupInfo> index, String[] names, String description)
{
int[] result = new int[names.length];
int count = 0;
for (String name : names)
{
int namedGroup = getNamedGroupByName(name);
if (namedGroup < 0)
{
LOG.warning("'" + description + "' contains unrecognised NamedGroup: " + name);
continue;
}
NamedGroupInfo namedGroupInfo = index.get(namedGroup);
if (null == namedGroupInfo)
{
LOG.warning("'" + description + "' contains unsupported NamedGroup: " + name);
continue;
}
if (!namedGroupInfo.isEnabled())
{
LOG.warning("'" + description + "' contains disabled NamedGroup: " + name);
continue;
}
result[count++] = namedGroup;
}
if (count < result.length)
{
result = Arrays.copyOf(result, count);
}
if (result.length < 1)
{
LOG.severe("'" + description + "' contained no usable NamedGroup values");
}
return result;
}
private static Map<Integer, NamedGroupInfo> createIndex(boolean isFipsContext, JcaTlsCrypto crypto)
{
Map<Integer, NamedGroupInfo> ng = new TreeMap<Integer, NamedGroupInfo>();
final boolean disableChar2 =
PropertyUtils.getBooleanSystemProperty("org.bouncycastle.jsse.ec.disableChar2", false) ||
PropertyUtils.getBooleanSystemProperty("org.bouncycastle.ec.disable_f2m", false);
final boolean disableFFDHE = !PropertyUtils.getBooleanSystemProperty("jsse.enableFFDHE", true);
for (All all : All.values())
{
addNamedGroup(isFipsContext, crypto, disableChar2, disableFFDHE, ng, all);
}
return ng;
}
private static int getNamedGroupByName(String name)
{
for (All all : All.values())
{
if (all.name.equalsIgnoreCase(name))
{
return all.namedGroup;
}
}
return -1;
}
private static List<NamedGroupInfo> getNamedGroupInfos(Map<Integer, NamedGroupInfo> namedGroupInfos,
int[] namedGroups)
{
if (namedGroups == null)
{
return null;
}
if (namedGroups.length < 1)
{
return Collections.emptyList();
}
int count = namedGroups.length;
ArrayList<NamedGroupInfo> result = new ArrayList<NamedGroupInfo>(count);
for (int i = 0; i < count; ++i)
{
int namedGroup = namedGroups[i];
NamedGroupInfo namedGroupInfo = namedGroupInfos.get(namedGroup);
if (null != namedGroupInfo)
{
result.add(namedGroupInfo);
}
}
if (result.isEmpty())
{
return Collections.emptyList();
}
result.trimToSize();
return result;
}
private static boolean hasAnyECDSA(Map<Integer, NamedGroupInfo> local)
{
for (NamedGroupInfo namedGroupInfo : local.values())
{
if (NamedGroup.refersToAnECDSACurve(namedGroupInfo.getNamedGroup()))
{
return true;
}
}
return false;
}
private final All all;
private final AlgorithmParameters algorithmParameters;
private final boolean enabled;
NamedGroupInfo(All all, AlgorithmParameters algorithmParameters, boolean enabled)
{
this.all = all;
this.algorithmParameters = algorithmParameters;
this.enabled = enabled;
}
int getBitsECDH()
{
return all.bitsECDH;
}
int getBitsFFDHE()
{
return all.bitsFFDHE;
}
String getJcaAlgorithm()
{
return all.jcaAlgorithm;
}
String getJcaGroup()
{
return all.jcaGroup;
}
int getNamedGroup()
{
return all.namedGroup;
}
boolean isActive(BCAlgorithmConstraints algorithmConstraints, boolean post13Active, boolean pre13Active)
{
return enabled
&& ((post13Active && isSupportedPost13()) || (pre13Active && isSupportedPre13()))
&& isPermittedBy(algorithmConstraints);
}
boolean isEnabled()
{
return enabled;
}
boolean isSupportedPost13()
{
return all.supportedPost13;
}
boolean isSupportedPre13()
{
return all.supportedPre13;
}
@Override
public String toString()
{
return all.text;
}
private boolean isPermittedBy(BCAlgorithmConstraints algorithmConstraints)
{
Set<BCCryptoPrimitive> primitives = JsseUtils.KEY_AGREEMENT_CRYPTO_PRIMITIVES_BC;
return algorithmConstraints.permits(primitives, getJcaGroup(), null)
&& algorithmConstraints.permits(primitives, getJcaAlgorithm(), algorithmParameters);
}
}