Skip to content

Commit a7a8801

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents fdefdde + a464146 commit a7a8801

File tree

5 files changed

+123
-63
lines changed

5 files changed

+123
-63
lines changed

tls/docs/GnuTLSSetup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h3>Instructions for setting up a GnuTLS server for use with DTLSClientTest, Tls
88

99
<li> Unpack to folder and add ${GNUTLS_HOME}/bin to PATH
1010

11-
<li> Make a working folder somewhere and copy the x509-*.pem from this package (in src/test/resources) to there.
11+
<li> Make a working folder somewhere and copy the x509-*.pem from the bc-test-data repository (in tls/credentials) to there.
1212

1313
<li> Go to working folder and start GnuTLS server (defaults to port 5556):
1414
<ul>

tls/docs/OpenSSLSetup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h3>Instructions for setting up an OpenSSL server for use with DTLSClientTest, D
77
<ul>
88
<li> Download and Install OpenSSL (exercise for the reader)
99

10-
<li> Make a working folder somewhere and copy the x509-*.pem from this package (in src/test/resources) to there.
10+
<li> Make a working folder somewhere and copy the x509-*.pem from the bc-test-data repository (in tls/credentials) to there.
1111

1212
<li> Go to working folder and start OpenSSL client or server:
1313
<ul>

tls/src/main/java/org/bouncycastle/jsse/provider/AbstractAlgorithmConstraints.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,12 @@ abstract class AbstractAlgorithmConstraints implements BCAlgorithmConstraints
1414

1515
AbstractAlgorithmConstraints(AlgorithmDecomposer decomposer)
1616
{
17-
this.decomposer = decomposer;
18-
}
19-
20-
protected void checkAlgorithmName(String algorithm)
21-
{
22-
if (!JsseUtils.isNameSpecified(algorithm))
23-
{
24-
throw new IllegalArgumentException("No algorithm name specified");
25-
}
26-
}
27-
28-
protected void checkKey(Key key)
29-
{
30-
if (null == key)
17+
if (decomposer == null)
3118
{
32-
throw new NullPointerException("'key' cannot be null");
19+
throw new NullPointerException("'decomposer' cannot be null");
3320
}
34-
}
3521

36-
protected void checkPrimitives(Set<BCCryptoPrimitive> primitives)
37-
{
38-
if (!isPrimitivesSpecified(primitives))
39-
{
40-
throw new IllegalArgumentException("No cryptographic primitive specified");
41-
}
22+
this.decomposer = decomposer;
4223
}
4324

4425
protected boolean containsAnyPartIgnoreCase(Set<String> elements, String algorithm)
@@ -53,21 +34,42 @@ protected boolean containsAnyPartIgnoreCase(Set<String> elements, String algorit
5334
return true;
5435
}
5536

56-
if (null != decomposer)
37+
for (String part : decomposer.decompose(algorithm))
5738
{
58-
for (String part : decomposer.decompose(algorithm))
39+
if (containsIgnoreCase(elements, part))
5940
{
60-
if (containsIgnoreCase(elements, part))
61-
{
62-
return true;
63-
}
41+
return true;
6442
}
6543
}
6644

6745
return false;
6846
}
6947

70-
protected boolean containsIgnoreCase(Set<String> elements, String s)
48+
static void checkAlgorithmName(String algorithm)
49+
{
50+
if (!JsseUtils.isNameSpecified(algorithm))
51+
{
52+
throw new IllegalArgumentException("No algorithm name specified");
53+
}
54+
}
55+
56+
static void checkKey(Key key)
57+
{
58+
if (null == key)
59+
{
60+
throw new NullPointerException("'key' cannot be null");
61+
}
62+
}
63+
64+
static void checkPrimitives(Set<BCCryptoPrimitive> primitives)
65+
{
66+
if (!isPrimitivesSpecified(primitives))
67+
{
68+
throw new IllegalArgumentException("No cryptographic primitive specified");
69+
}
70+
}
71+
72+
static boolean containsIgnoreCase(Set<String> elements, String s)
7173
{
7274
for (String element : elements)
7375
{
@@ -79,12 +81,12 @@ protected boolean containsIgnoreCase(Set<String> elements, String s)
7981
return false;
8082
}
8183

82-
protected boolean isPrimitivesSpecified(Set<BCCryptoPrimitive> primitives)
84+
static boolean isPrimitivesSpecified(Set<BCCryptoPrimitive> primitives)
8385
{
8486
return null != primitives && !primitives.isEmpty();
8587
}
8688

87-
protected static Set<String> asUnmodifiableSet(String[] algorithms)
89+
static Set<String> asUnmodifiableSet(String[] algorithms)
8890
{
8991
if (null != algorithms && algorithms.length > 0)
9092
{
@@ -97,7 +99,7 @@ protected static Set<String> asUnmodifiableSet(String[] algorithms)
9799
return Collections.<String> emptySet();
98100
}
99101

100-
protected static Set<String> asSet(String[] algorithms)
102+
static Set<String> asSet(String[] algorithms)
101103
{
102104
Set<String> result = new HashSet<String>();
103105
if (null != algorithms)
Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,85 @@
11
package org.bouncycastle.jsse.provider;
22

3-
import java.util.Collections;
3+
import java.util.HashMap;
44
import java.util.HashSet;
5+
import java.util.Map;
56
import java.util.Set;
67
import java.util.regex.Pattern;
78

89
class JcaAlgorithmDecomposer
910
implements AlgorithmDecomposer
1011
{
12+
private static final Map<String, String> SHA_DIGEST_MAP = createSHADigestMap();
13+
1114
private static final Pattern PATTERN = Pattern.compile("with|and|(?<!padd)in", Pattern.CASE_INSENSITIVE);
1215

1316
static final JcaAlgorithmDecomposer INSTANCE_JCA = new JcaAlgorithmDecomposer();
1417

1518
public Set<String> decompose(String algorithm)
1619
{
17-
if (algorithm.indexOf('/') < 0)
20+
HashSet<String> result = new HashSet<String>();
21+
22+
if (JsseUtils.isNameSpecified(algorithm))
1823
{
19-
return Collections.emptySet();
24+
implDecompose(result, algorithm);
25+
26+
if (algorithm.contains("SHA"))
27+
{
28+
for (Map.Entry<String, String> entry : SHA_DIGEST_MAP.entrySet())
29+
{
30+
includeBothIfEither(result, entry.getKey(), entry.getValue());
31+
}
32+
}
2033
}
2134

22-
Set<String> result = new HashSet<String>();
35+
return result;
36+
}
2337

38+
static String decomposeDigestName(String algorithm)
39+
{
40+
String result = SHA_DIGEST_MAP.get(algorithm);
41+
if (result == null)
42+
{
43+
result = algorithm;
44+
}
45+
return result;
46+
}
47+
48+
static Set<String> decomposeName(String algorithm)
49+
{
50+
HashSet<String> result = new HashSet<String>();
51+
52+
if (JsseUtils.isNameSpecified(algorithm))
53+
{
54+
implDecompose(result, algorithm);
55+
56+
if (algorithm.contains("SHA"))
57+
{
58+
for (Map.Entry<String, String> entry : SHA_DIGEST_MAP.entrySet())
59+
{
60+
replaceFirstWithSecond(result, entry.getKey(), entry.getValue());
61+
}
62+
}
63+
}
64+
65+
return result;
66+
}
67+
68+
private static Map<String, String> createSHADigestMap()
69+
{
70+
HashMap<String, String> result = new HashMap<String, String>();
71+
result.put("SHA-1", "SHA1");
72+
result.put("SHA-224", "SHA224");
73+
result.put("SHA-256", "SHA256");
74+
result.put("SHA-384", "SHA384");
75+
result.put("SHA-512", "SHA512");
76+
result.put("SHA-512/224", "SHA512/224");
77+
result.put("SHA-512/256", "SHA512/256");
78+
return result;
79+
}
80+
81+
private static void implDecompose(HashSet<String> result, String algorithm)
82+
{
2483
for (String section : algorithm.split("/"))
2584
{
2685
if (section.length() > 0)
@@ -34,22 +93,25 @@ public Set<String> decompose(String algorithm)
3493
}
3594
}
3695
}
96+
}
3797

38-
ensureBothIfEither(result, "SHA1", "SHA-1");
39-
ensureBothIfEither(result, "SHA224", "SHA-224");
40-
ensureBothIfEither(result, "SHA256", "SHA-256");
41-
ensureBothIfEither(result, "SHA384", "SHA-384");
42-
ensureBothIfEither(result, "SHA512", "SHA-512");
43-
44-
return result;
98+
private static void includeBothIfEither(HashSet<String> elements, String a, String b)
99+
{
100+
if (elements.contains(a))
101+
{
102+
elements.add(b);
103+
}
104+
else if (elements.contains(b))
105+
{
106+
elements.add(a);
107+
}
45108
}
46109

47-
private static void ensureBothIfEither(Set<String> elements, String a, String b)
110+
private static void replaceFirstWithSecond(HashSet<String> elements, String a, String b)
48111
{
49-
boolean hasA = elements.contains(a), hasB = elements.contains(b);
50-
if (hasA ^ hasB)
112+
if (elements.remove(a))
51113
{
52-
elements.add(hasA ? b : a);
114+
elements.add(b);
53115
}
54116
}
55117
}

tls/src/main/java/org/bouncycastle/jsse/provider/ProvAlgorithmConstraints.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.bouncycastle.jsse.java.security.BCCryptoPrimitive;
1111

1212
class ProvAlgorithmConstraints
13-
extends AbstractAlgorithmConstraints
13+
implements BCAlgorithmConstraints
1414
{
1515
private static final Logger LOG = Logger.getLogger(ProvAlgorithmConstraints.class.getName());
1616

@@ -43,8 +43,6 @@ class ProvAlgorithmConstraints
4343

4444
ProvAlgorithmConstraints(BCAlgorithmConstraints configAlgorithmConstraints, boolean enableX509Constraints)
4545
{
46-
super(null);
47-
4846
this.configAlgorithmConstraints = configAlgorithmConstraints;
4947
this.supportedSignatureAlgorithms = null;
5048
this.enableX509Constraints = enableX509Constraints;
@@ -53,17 +51,15 @@ class ProvAlgorithmConstraints
5351
ProvAlgorithmConstraints(BCAlgorithmConstraints configAlgorithmConstraints,
5452
String[] supportedSignatureAlgorithms, boolean enableX509Constraints)
5553
{
56-
super(null);
57-
5854
this.configAlgorithmConstraints = configAlgorithmConstraints;
59-
this.supportedSignatureAlgorithms = asUnmodifiableSet(supportedSignatureAlgorithms);
55+
this.supportedSignatureAlgorithms = AbstractAlgorithmConstraints.asUnmodifiableSet(supportedSignatureAlgorithms);
6056
this.enableX509Constraints = enableX509Constraints;
6157
}
6258

6359
public boolean permits(Set<BCCryptoPrimitive> primitives, String algorithm, AlgorithmParameters parameters)
6460
{
65-
checkPrimitives(primitives);
66-
checkAlgorithmName(algorithm);
61+
AbstractAlgorithmConstraints.checkPrimitives(primitives);
62+
AbstractAlgorithmConstraints.checkAlgorithmName(algorithm);
6763

6864
if (null != supportedSignatureAlgorithms)
6965
{
@@ -105,8 +101,8 @@ public boolean permits(Set<BCCryptoPrimitive> primitives, String algorithm, Algo
105101

106102
public boolean permits(Set<BCCryptoPrimitive> primitives, Key key)
107103
{
108-
checkPrimitives(primitives);
109-
checkKey(key);
104+
AbstractAlgorithmConstraints.checkPrimitives(primitives);
105+
AbstractAlgorithmConstraints.checkKey(key);
110106

111107
if (null != configAlgorithmConstraints && !configAlgorithmConstraints.permits(primitives, key))
112108
{
@@ -129,9 +125,9 @@ public boolean permits(Set<BCCryptoPrimitive> primitives, Key key)
129125

130126
public boolean permits(Set<BCCryptoPrimitive> primitives, String algorithm, Key key, AlgorithmParameters parameters)
131127
{
132-
checkPrimitives(primitives);
133-
checkAlgorithmName(algorithm);
134-
checkKey(key);
128+
AbstractAlgorithmConstraints.checkPrimitives(primitives);
129+
AbstractAlgorithmConstraints.checkAlgorithmName(algorithm);
130+
AbstractAlgorithmConstraints.checkKey(key);
135131

136132
if (null != supportedSignatureAlgorithms)
137133
{
@@ -175,6 +171,6 @@ private String getAlgorithm(String algorithmBC)
175171

176172
private boolean isSupportedSignatureAlgorithm(String algorithmBC)
177173
{
178-
return containsIgnoreCase(supportedSignatureAlgorithms, algorithmBC);
174+
return AbstractAlgorithmConstraints.containsIgnoreCase(supportedSignatureAlgorithms, algorithmBC);
179175
}
180176
}

0 commit comments

Comments
 (0)