Skip to content

Commit 47a2464

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents ee70b26 + 4da9293 commit 47a2464

File tree

7 files changed

+100
-108
lines changed

7 files changed

+100
-108
lines changed

tls/src/main/java/org/bouncycastle/tls/TlsUtils.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,7 +5316,7 @@ static Hashtable addKeyShareToClientHello(TlsClientContext clientContext, TlsCli
53165316
Hashtable clientAgreements = new Hashtable(3);
53175317
Vector clientShares = new Vector(2);
53185318

5319-
collectKeyShares(clientContext, supportedGroups, keyShareGroups, clientAgreements, clientShares);
5319+
collectKeyShares(clientContext.getCrypto(), supportedGroups, keyShareGroups, clientAgreements, clientShares);
53205320

53215321
// TODO[tls13-psk] When clientShares empty, consider not adding extension if pre_shared_key in use
53225322
TlsExtensionsUtils.addKeyShareClientHello(clientExtensions, clientShares);
@@ -5332,7 +5332,7 @@ static Hashtable addKeyShareToClientHelloRetry(TlsClientContext clientContext, H
53325332
Hashtable clientAgreements = new Hashtable(1, 1.0f);
53335333
Vector clientShares = new Vector(1);
53345334

5335-
collectKeyShares(clientContext, supportedGroups, keyShareGroups, clientAgreements, clientShares);
5335+
collectKeyShares(clientContext.getCrypto(), supportedGroups, keyShareGroups, clientAgreements, clientShares);
53365336

53375337
TlsExtensionsUtils.addKeyShareClientHello(clientExtensions, clientShares);
53385338

@@ -5345,10 +5345,9 @@ static Hashtable addKeyShareToClientHelloRetry(TlsClientContext clientContext, H
53455345
return clientAgreements;
53465346
}
53475347

5348-
private static void collectKeyShares(TlsClientContext clientContext, int[] supportedGroups, Vector keyShareGroups,
5348+
private static void collectKeyShares(TlsCrypto crypto, int[] supportedGroups, Vector keyShareGroups,
53495349
Hashtable clientAgreements, Vector clientShares) throws IOException
53505350
{
5351-
TlsCrypto crypto = clientContext.getCrypto();
53525351
if (isNullOrEmpty(supportedGroups))
53535352
{
53545353
return;
@@ -5444,14 +5443,12 @@ static KeyShareEntry selectKeyShare(TlsCrypto crypto, ProtocolVersion negotiated
54445443
continue;
54455444
}
54465445

5447-
if ((NamedGroup.refersToAnECDHCurve(group) && !crypto.hasECDHAgreement()) ||
5448-
(NamedGroup.refersToASpecificFiniteField(group) && !crypto.hasDHAgreement()) ||
5449-
(NamedGroup.refersToASpecificKem(group) && !crypto.hasKemAgreement()))
5446+
if ((NamedGroup.refersToAnECDHCurve(group) && crypto.hasECDHAgreement()) ||
5447+
(NamedGroup.refersToASpecificFiniteField(group) && crypto.hasDHAgreement()) ||
5448+
(NamedGroup.refersToASpecificKem(group) && crypto.hasKemAgreement()))
54505449
{
5451-
continue;
5450+
return clientShare;
54525451
}
5453-
5454-
return clientShare;
54555452
}
54565453
}
54575454
return null;
@@ -5481,14 +5478,12 @@ static int selectKeyShareGroup(TlsCrypto crypto, ProtocolVersion negotiatedVersi
54815478
continue;
54825479
}
54835480

5484-
if ((NamedGroup.refersToAnECDHCurve(group) && !crypto.hasECDHAgreement()) ||
5485-
(NamedGroup.refersToASpecificFiniteField(group) && !crypto.hasDHAgreement()) ||
5486-
(NamedGroup.refersToASpecificKem(group) && !crypto.hasKemAgreement()))
5481+
if ((NamedGroup.refersToAnECDHCurve(group) && crypto.hasECDHAgreement()) ||
5482+
(NamedGroup.refersToASpecificFiniteField(group) && crypto.hasDHAgreement()) ||
5483+
(NamedGroup.refersToASpecificKem(group) && crypto.hasKemAgreement()))
54875484
{
5488-
continue;
5485+
return group;
54895486
}
5490-
5491-
return group;
54925487
}
54935488
}
54945489
return -1;

tls/src/main/java/org/bouncycastle/tls/crypto/TlsCrypto.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ public interface TlsCrypto
6969
*/
7070
boolean hasECDHAgreement();
7171

72-
/**
73-
* Return true if this TlsCrypto can support KEM key agreement.
74-
*
75-
* @return true if this instance can support KEM key agreement, false otherwise.
76-
*/
77-
boolean hasKemAgreement();
78-
7972
/**
8073
* Return true if this TlsCrypto can support the passed in block/stream encryption algorithm.
8174
*
@@ -92,6 +85,13 @@ public interface TlsCrypto
9285
*/
9386
boolean hasHKDFAlgorithm(int cryptoHashAlgorithm);
9487

88+
/**
89+
* Return true if this TlsCrypto can support KEM key agreement.
90+
*
91+
* @return true if this instance can support KEM key agreement, false otherwise.
92+
*/
93+
boolean hasKemAgreement();
94+
9595
/**
9696
* Return true if this TlsCrypto can support the passed in MAC algorithm.
9797
*

tls/src/main/java/org/bouncycastle/tls/crypto/impl/bc/BcTlsCrypto.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,6 @@ public boolean hasECDHAgreement()
316316
return true;
317317
}
318318

319-
public boolean hasKemAgreement()
320-
{
321-
return true;
322-
}
323-
324319
public boolean hasEncryptionAlgorithm(int encryptionAlgorithm)
325320
{
326321
switch (encryptionAlgorithm)
@@ -379,6 +374,11 @@ public boolean hasHKDFAlgorithm(int cryptoHashAlgorithm)
379374
}
380375
}
381376

377+
public boolean hasKemAgreement()
378+
{
379+
return true;
380+
}
381+
382382
public boolean hasMacAlgorithm(int macAlgorithm)
383383
{
384384
switch (macAlgorithm)

tls/src/main/java/org/bouncycastle/tls/crypto/impl/jcajce/JcaTlsCrypto.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,6 @@ public boolean hasECDHAgreement()
582582
{
583583
return true;
584584
}
585-
586-
public boolean hasKemAgreement()
587-
{
588-
return true;
589-
}
590585

591586
public boolean hasEncryptionAlgorithm(int encryptionAlgorithm)
592587
{
@@ -636,6 +631,11 @@ public boolean hasHKDFAlgorithm(int cryptoHashAlgorithm)
636631
}
637632
}
638633

634+
public boolean hasKemAgreement()
635+
{
636+
return true;
637+
}
638+
639639
public boolean hasMacAlgorithm(int macAlgorithm)
640640
{
641641
switch (macAlgorithm)

tls/src/test/java/org/bouncycastle/tls/test/MockTlsKemClient.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@
3333
import org.bouncycastle.util.encoders.Hex;
3434

3535
class MockTlsKemClient
36-
extends DefaultTlsClient
36+
extends DefaultTlsClient
3737
{
3838
TlsSession session;
3939

40+
int[] namedGroups = new int[]
41+
{
42+
NamedGroup.MLKEM512,
43+
NamedGroup.MLKEM768,
44+
NamedGroup.MLKEM1024,
45+
};
46+
4047
MockTlsKemClient(TlsSession session)
4148
{
4249
super(new BcTlsCrypto());
@@ -52,25 +59,19 @@ protected Vector getProtocolNames()
5259
return protocolNames;
5360
}
5461

55-
public int[] supportedGroups = new int[] {
56-
NamedGroup.MLKEM512,
57-
NamedGroup.MLKEM768,
58-
NamedGroup.MLKEM1024,
59-
};
60-
61-
public void setSupportedGroups(int[] supportedGroups)
62+
void setNamedGroups(int[] namedGroups)
6263
{
63-
this.supportedGroups = supportedGroups;
64+
this.namedGroups = namedGroups;
6465
}
6566

6667
protected Vector getSupportedGroups(Vector namedGroupRoles) {
6768
TlsCrypto crypto = getCrypto();
6869
Vector supportedGroups = new Vector();
6970

70-
if (namedGroupRoles.contains(Integers.valueOf(NamedGroupRole.kem))) {
71-
TlsUtils.addIfSupported(supportedGroups, crypto,
72-
this.supportedGroups);
73-
};
71+
if (namedGroupRoles.contains(Integers.valueOf(NamedGroupRole.kem)))
72+
{
73+
TlsUtils.addIfSupported(supportedGroups, crypto, this.namedGroups);
74+
}
7475
return supportedGroups;
7576
}
7677

@@ -82,8 +83,8 @@ public TlsSession getSessionToResume()
8283
public void notifyAlertRaised(short alertLevel, short alertDescription, String message, Throwable cause)
8384
{
8485
PrintStream out = (alertLevel == AlertLevel.fatal) ? System.err : System.out;
85-
out.println("TLS client raised alert: " + AlertLevel.getText(alertLevel)
86-
+ ", " + AlertDescription.getText(alertDescription));
86+
out.println("TLS KEM client raised alert: " + AlertLevel.getText(alertLevel)
87+
+ ", " + AlertDescription.getText(alertDescription));
8788
if (message != null)
8889
{
8990
out.println("> " + message);
@@ -98,7 +99,7 @@ public void notifyAlertReceived(short alertLevel, short alertDescription)
9899
{
99100
PrintStream out = (alertLevel == AlertLevel.fatal) ? System.err : System.out;
100101
out.println("TLS KEM client received alert: " + AlertLevel.getText(alertLevel)
101-
+ ", " + AlertDescription.getText(alertDescription));
102+
+ ", " + AlertDescription.getText(alertDescription));
102103
}
103104

104105
public Hashtable getClientExtensions() throws IOException
@@ -141,24 +142,24 @@ public void notifyServerCertificate(TlsServerCertificate serverCertificate) thro
141142
Certificate entry = Certificate.getInstance(chain[i].getEncoded());
142143
// TODO Create fingerprint based on certificate signature algorithm digest
143144
System.out.println(" fingerprint:SHA-256 " + TlsTestUtils.fingerprint(entry) + " ("
144-
+ entry.getSubject() + ")");
145+
+ entry.getSubject() + ")");
145146
}
146147

147148
boolean isEmpty = serverCertificate == null || serverCertificate.getCertificate() == null
148-
|| serverCertificate.getCertificate().isEmpty();
149+
|| serverCertificate.getCertificate().isEmpty();
149150

150151
if (isEmpty)
151152
{
152153
throw new TlsFatalAlert(AlertDescription.bad_certificate);
153154
}
154155

155156
String[] trustedCertResources = new String[]{ "x509-server-dsa.pem", "x509-server-ecdh.pem",
156-
"x509-server-ecdsa.pem", "x509-server-ed25519.pem", "x509-server-ed448.pem",
157-
"x509-server-rsa_pss_256.pem", "x509-server-rsa_pss_384.pem", "x509-server-rsa_pss_512.pem",
158-
"x509-server-rsa-enc.pem", "x509-server-rsa-sign.pem" };
157+
"x509-server-ecdsa.pem", "x509-server-ed25519.pem", "x509-server-ed448.pem",
158+
"x509-server-rsa_pss_256.pem", "x509-server-rsa_pss_384.pem", "x509-server-rsa_pss_512.pem",
159+
"x509-server-rsa-enc.pem", "x509-server-rsa-sign.pem" };
159160

160161
TlsCertificate[] certPath = TlsTestUtils.getTrustedCertPath(context.getCrypto(), chain[0],
161-
trustedCertResources);
162+
trustedCertResources);
162163

163164
if (null == certPath)
164165
{
@@ -177,7 +178,7 @@ public TlsCredentials getClientCredentials(CertificateRequest certificateRequest
177178
}
178179

179180
return TlsTestUtils.loadSignerCredentials(context, certificateRequest.getSupportedSignatureAlgorithms(),
180-
SignatureAlgorithm.rsa, "x509-client-rsa.pem", "x509-client-key-rsa.pem");
181+
SignatureAlgorithm.rsa, "x509-client-rsa.pem", "x509-client-key-rsa.pem");
181182
}
182183
};
183184
}
@@ -189,7 +190,7 @@ public void notifyHandshakeComplete() throws IOException
189190
ProtocolName protocolName = context.getSecurityParametersConnection().getApplicationProtocol();
190191
if (protocolName != null)
191192
{
192-
System.out.println("KEM Client ALPN: " + protocolName.getUtf8Decoding());
193+
System.out.println("Client ALPN: " + protocolName.getUtf8Decoding());
193194
}
194195

195196
TlsSession newSession = context.getSession();
@@ -202,11 +203,11 @@ public void notifyHandshakeComplete() throws IOException
202203

203204
if (this.session != null && Arrays.areEqual(this.session.getSessionID(), newSessionID))
204205
{
205-
System.out.println("KEM Client resumed session: " + hex);
206+
System.out.println("Client resumed session: " + hex);
206207
}
207208
else
208209
{
209-
System.out.println("KEM Client established session: " + hex);
210+
System.out.println("Client established session: " + hex);
210211
}
211212

212213
this.session = newSession;
@@ -215,14 +216,14 @@ public void notifyHandshakeComplete() throws IOException
215216
byte[] tlsServerEndPoint = context.exportChannelBinding(ChannelBinding.tls_server_end_point);
216217
if (null != tlsServerEndPoint)
217218
{
218-
System.out.println("KEM Client 'tls-server-end-point': " + hex(tlsServerEndPoint));
219+
System.out.println("Client 'tls-server-end-point': " + hex(tlsServerEndPoint));
219220
}
220221

221222
byte[] tlsUnique = context.exportChannelBinding(ChannelBinding.tls_unique);
222-
System.out.println("KEM Client 'tls-unique': " + hex(tlsUnique));
223+
System.out.println("Client 'tls-unique': " + hex(tlsUnique));
223224

224225
byte[] tlsExporter = context.exportChannelBinding(ChannelBinding.tls_exporter);
225-
System.out.println("KEM Client 'tls-exporter': " + hex(tlsExporter));
226+
System.out.println("Client 'tls-exporter': " + hex(tlsExporter));
226227
}
227228
}
228229

@@ -240,4 +241,5 @@ protected String hex(byte[] data)
240241
{
241242
return data == null ? "(null)" : Hex.toHexString(data);
242243
}
243-
}
244+
}
245+

0 commit comments

Comments
 (0)