Skip to content

Commit d51e11e

Browse files
committed
Cleanup
1 parent 0979953 commit d51e11e

File tree

3 files changed

+60
-82
lines changed

3 files changed

+60
-82
lines changed

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.security.SecureRandom;
55
import java.util.Vector;
66

7-
import junit.framework.TestCase;
87
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
98
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
109
import org.bouncycastle.tls.Certificate;
@@ -23,16 +22,16 @@
2322
import org.bouncycastle.tls.TlsServerCertificate;
2423
import org.bouncycastle.tls.TlsUtils;
2524
import org.bouncycastle.tls.crypto.TlsCertificate;
26-
import org.bouncycastle.tls.crypto.TlsCrypto;
2725
import org.bouncycastle.tls.crypto.TlsCryptoParameters;
2826
import org.bouncycastle.tls.crypto.impl.bc.BcDefaultTlsCredentialedSigner;
2927
import org.bouncycastle.tls.crypto.impl.bc.BcTlsCrypto;
3028
import org.bouncycastle.tls.crypto.impl.bc.BcTlsRawKeyCertificate;
3129

30+
import junit.framework.TestCase;
31+
3232
class MockRawKeysTlsClient
3333
extends DefaultTlsClient
3434
{
35-
3635
private short serverCertType;
3736
private short clientCertType;
3837
private short[] offerServerCertTypes;
@@ -46,6 +45,7 @@ class MockRawKeysTlsClient
4645
throws Exception
4746
{
4847
super(new BcTlsCrypto(new SecureRandom()));
48+
4949
this.serverCertType = serverCertType;
5050
this.clientCertType = clientCertType;
5151
this.offerServerCertTypes = offerServerCertTypes;
@@ -61,9 +61,9 @@ protected ProtocolVersion[] getSupportedVersions()
6161

6262
protected int[] getSupportedCipherSuites()
6363
{
64-
return ProtocolVersion.TLSv13.equals(tlsVersion) ?
65-
new int[] {CipherSuite.TLS_AES_128_GCM_SHA256} :
66-
new int[] {CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
64+
return TlsUtils.isTLSv13(tlsVersion)
65+
? new int[]{ CipherSuite.TLS_AES_128_GCM_SHA256 }
66+
: new int[]{ CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 };
6767
}
6868

6969
protected short[] getAllowedClientCertificateTypes()
@@ -113,19 +113,13 @@ public TlsCredentials getClientCredentials(CertificateRequest certificateRequest
113113
SignatureAlgorithm.ed25519, "x509-client-ed25519.pem", "x509-client-key-ed25519.pem");
114114
break;
115115
case CertificateType.RawPublicKey:
116-
TlsCertificate rawKeyCert = new BcTlsRawKeyCertificate(
117-
(BcTlsCrypto)getCrypto(),
118-
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(privateKey.generatePublicKey()));
119-
Certificate cert = new Certificate(
120-
CertificateType.RawPublicKey,
121-
TlsUtils.isTLSv13(context) ? TlsUtils.EMPTY_BYTES : null,
122-
new CertificateEntry[] {new CertificateEntry(rawKeyCert, null)});
123-
credentials = new BcDefaultTlsCredentialedSigner(
124-
new TlsCryptoParameters(context),
125-
(BcTlsCrypto)getCrypto(),
126-
privateKey,
127-
cert,
128-
SignatureAndHashAlgorithm.ed25519);
116+
TlsCertificate rawKeyCert = new BcTlsRawKeyCertificate((BcTlsCrypto)getCrypto(),
117+
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(privateKey.generatePublicKey()));
118+
Certificate cert = new Certificate(CertificateType.RawPublicKey,
119+
TlsUtils.isTLSv13(context) ? TlsUtils.EMPTY_BYTES : null,
120+
new CertificateEntry[]{ new CertificateEntry(rawKeyCert, null) });
121+
credentials = new BcDefaultTlsCredentialedSigner(new TlsCryptoParameters(context),
122+
(BcTlsCrypto)getCrypto(), privateKey, cert, SignatureAndHashAlgorithm.ed25519);
129123
break;
130124
default:
131125
throw new IllegalArgumentException("Only supports X509 and raw keys");
@@ -136,9 +130,4 @@ public TlsCredentials getClientCredentials(CertificateRequest certificateRequest
136130
}
137131
};
138132
}
139-
140-
public TlsCrypto getCrypto()
141-
{
142-
return super.getCrypto();
143-
}
144133
}

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

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Hashtable;
66
import java.util.Vector;
77

8-
import junit.framework.TestCase;
98
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
109
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
1110
import org.bouncycastle.tls.Certificate;
@@ -22,15 +21,15 @@
2221
import org.bouncycastle.tls.TlsCredentials;
2322
import org.bouncycastle.tls.TlsUtils;
2423
import org.bouncycastle.tls.crypto.TlsCertificate;
25-
import org.bouncycastle.tls.crypto.TlsCrypto;
2624
import org.bouncycastle.tls.crypto.TlsCryptoParameters;
2725
import org.bouncycastle.tls.crypto.impl.bc.BcDefaultTlsCredentialedSigner;
2826
import org.bouncycastle.tls.crypto.impl.bc.BcTlsCrypto;
2927
import org.bouncycastle.tls.crypto.impl.bc.BcTlsRawKeyCertificate;
3028

29+
import junit.framework.TestCase;
30+
3131
class MockRawKeysTlsServer extends DefaultTlsServer
3232
{
33-
3433
private short serverCertType;
3534
private short clientCertType;
3635
private short[] allowedClientCertTypes;
@@ -40,11 +39,11 @@ class MockRawKeysTlsServer extends DefaultTlsServer
4039

4140
Hashtable receivedClientExtensions;
4241

43-
MockRawKeysTlsServer(short serverCertType, short clientCertType,
44-
short[] allowedClientCertTypes, Ed25519PrivateKeyParameters privateKey,
45-
ProtocolVersion tlsVersion) throws Exception
42+
MockRawKeysTlsServer(short serverCertType, short clientCertType, short[] allowedClientCertTypes,
43+
Ed25519PrivateKeyParameters privateKey, ProtocolVersion tlsVersion) throws Exception
4644
{
4745
super(new BcTlsCrypto(new SecureRandom()));
46+
4847
this.serverCertType = serverCertType;
4948
this.clientCertType = clientCertType;
5049
this.allowedClientCertTypes = allowedClientCertTypes;
@@ -68,47 +67,44 @@ public TlsCredentials getCredentials() throws IOException
6867

6968
protected ProtocolVersion[] getSupportedVersions()
7069
{
71-
return new ProtocolVersion[] {tlsVersion};
70+
return new ProtocolVersion[]{ tlsVersion };
7271
}
7372

7473
protected int[] getSupportedCipherSuites()
7574
{
76-
return ProtocolVersion.TLSv13.equals(tlsVersion) ?
77-
new int[] {CipherSuite.TLS_AES_128_GCM_SHA256} :
78-
new int[] {CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256};
75+
return TlsUtils.isTLSv13(tlsVersion)
76+
? new int[]{ CipherSuite.TLS_AES_128_GCM_SHA256 }
77+
: new int[]{ CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 };
7978
}
8079

8180
public void processClientExtensions(Hashtable clientExtensions) throws IOException
8281
{
83-
receivedClientExtensions = clientExtensions;
82+
this.receivedClientExtensions = clientExtensions;
83+
8484
super.processClientExtensions(clientExtensions);
8585
}
8686

8787
protected TlsCredentialedSigner getECDSASignerCredentials() throws IOException
8888
{
8989
if (credentials == null)
9090
{
91+
BcTlsCrypto crypto = (BcTlsCrypto)getCrypto();
92+
9193
switch (serverCertType)
9294
{
9395
case CertificateType.X509:
94-
credentials = TlsTestUtils.loadSignerCredentials(
95-
context, context.getSecurityParametersHandshake().getClientSigAlgs(),
96-
SignatureAlgorithm.ed25519, "x509-client-ed25519.pem", "x509-client-key-ed25519.pem");
96+
credentials = TlsTestUtils.loadSignerCredentials(context,
97+
context.getSecurityParametersHandshake().getClientSigAlgs(), SignatureAlgorithm.ed25519,
98+
"x509-client-ed25519.pem", "x509-client-key-ed25519.pem");
9799
break;
98100
case CertificateType.RawPublicKey:
99-
TlsCertificate rawKeyCert = new BcTlsRawKeyCertificate(
100-
(BcTlsCrypto)getCrypto(),
101-
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(privateKey.generatePublicKey()));
102-
Certificate cert = new Certificate(
103-
CertificateType.RawPublicKey,
104-
TlsUtils.isTLSv13(context) ? TlsUtils.EMPTY_BYTES : null,
105-
new CertificateEntry[] {new CertificateEntry(rawKeyCert, null)});
106-
credentials = new BcDefaultTlsCredentialedSigner(
107-
new TlsCryptoParameters(context),
108-
(BcTlsCrypto)getCrypto(),
109-
privateKey,
110-
cert,
111-
SignatureAndHashAlgorithm.ed25519);
101+
TlsCertificate rawKeyCert = new BcTlsRawKeyCertificate(crypto,
102+
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(privateKey.generatePublicKey()));
103+
Certificate cert = new Certificate(CertificateType.RawPublicKey,
104+
TlsUtils.isTLSv13(context) ? TlsUtils.EMPTY_BYTES : null,
105+
new CertificateEntry[]{ new CertificateEntry(rawKeyCert, null) });
106+
credentials = new BcDefaultTlsCredentialedSigner(new TlsCryptoParameters(context),
107+
crypto, privateKey, cert, SignatureAndHashAlgorithm.ed25519);
112108
break;
113109
default:
114110
throw new IllegalArgumentException("Only supports X509 and raw keys");
@@ -140,26 +136,21 @@ public CertificateRequest getCertificateRequest() throws IOException
140136
return null;
141137
}
142138

143-
short[] certificateTypes = new short[] {ClientCertificateType.ecdsa_sign};
139+
short[] certificateTypes = new short[]{ ClientCertificateType.ecdsa_sign };
144140

145141
Vector serverSigAlgs = null;
146142
if (TlsUtils.isSignatureAlgorithmsExtensionAllowed(context.getServerVersion()))
147143
{
148144
serverSigAlgs = TlsUtils.getDefaultSupportedSignatureAlgorithms(context);
149145
}
150146

151-
return ProtocolVersion.TLSv13.equals(tlsVersion) ?
152-
new CertificateRequest(TlsUtils.EMPTY_BYTES, serverSigAlgs, null, null) :
153-
new CertificateRequest(certificateTypes, serverSigAlgs, null);
147+
return TlsUtils.isTLSv13(tlsVersion)
148+
? new CertificateRequest(TlsUtils.EMPTY_BYTES, serverSigAlgs, null, null)
149+
: new CertificateRequest(certificateTypes, serverSigAlgs, null);
154150
}
155151

156152
public void notifyClientCertificate(Certificate clientCertificate) throws IOException
157153
{
158154
TestCase.assertEquals("client certificate is the wrong type", clientCertType, clientCertificate.getCertificateType());
159155
}
160-
161-
public TlsCrypto getCrypto()
162-
{
163-
return super.getCrypto();
164-
}
165156
}

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class TlsRawKeysProtocolTest
2424
extends TestCase
2525
{
26-
private SecureRandom rng = new SecureRandom();
26+
private final SecureRandom RANDOM = new SecureRandom();
2727

2828
public void testClientSendsExtensionButServerDoesNotSupportIt() throws Exception
2929
{
@@ -42,13 +42,13 @@ private void testClientSendsExtensionButServerDoesNotSupportIt(ProtocolVersion t
4242
(short) -1,
4343
new short[] {CertificateType.RawPublicKey, CertificateType.X509},
4444
null,
45-
generateKeypair(),
45+
generateKeyPair(),
4646
tlsVersion);
4747
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
4848
CertificateType.X509,
4949
(short) -1,
5050
null,
51-
generateKeypair(),
51+
generateKeyPair(),
5252
tlsVersion);
5353
pumpData(client, server);
5454
}
@@ -70,13 +70,13 @@ private void testExtensionsAreOmittedIfSpecifiedButOnlyContainX509(ProtocolVersi
7070
CertificateType.X509,
7171
new short[] {CertificateType.X509},
7272
new short[] {CertificateType.X509},
73-
generateKeypair(),
73+
generateKeyPair(),
7474
tlsVersion);
7575
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
7676
CertificateType.X509,
7777
CertificateType.X509,
7878
new short[] {CertificateType.X509},
79-
generateKeypair(),
79+
generateKeyPair(),
8080
tlsVersion);
8181
pumpData(client, server);
8282

@@ -105,13 +105,13 @@ private void testBothSidesUseRawKey(ProtocolVersion tlsVersion) throws Exception
105105
CertificateType.RawPublicKey,
106106
new short[] {CertificateType.RawPublicKey},
107107
new short[] {CertificateType.RawPublicKey},
108-
generateKeypair(),
108+
generateKeyPair(),
109109
tlsVersion);
110110
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
111111
CertificateType.RawPublicKey,
112112
CertificateType.RawPublicKey,
113113
new short[] {CertificateType.RawPublicKey},
114-
generateKeypair(),
114+
generateKeyPair(),
115115
tlsVersion);
116116
pumpData(client, server);
117117
}
@@ -133,13 +133,13 @@ private void testServerUsesRawKeyAndClientIsAnonymous(ProtocolVersion tlsVersion
133133
(short) -1,
134134
new short[] {CertificateType.RawPublicKey},
135135
null,
136-
generateKeypair(),
136+
generateKeyPair(),
137137
tlsVersion);
138138
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
139139
CertificateType.RawPublicKey,
140140
(short) -1,
141141
null,
142-
generateKeypair(),
142+
generateKeyPair(),
143143
tlsVersion);
144144
pumpData(client, server);
145145
}
@@ -161,13 +161,13 @@ private void testServerUsesRawKeyAndClientUsesX509(ProtocolVersion tlsVersion) t
161161
CertificateType.X509,
162162
new short[] {CertificateType.RawPublicKey},
163163
null,
164-
generateKeypair(),
164+
generateKeyPair(),
165165
tlsVersion);
166166
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
167167
CertificateType.RawPublicKey,
168168
CertificateType.X509,
169169
null,
170-
generateKeypair(),
170+
generateKeyPair(),
171171
tlsVersion);
172172
pumpData(client, server);
173173
}
@@ -189,13 +189,13 @@ private void testServerUsesX509AndClientUsesRawKey(ProtocolVersion tlsVersion) t
189189
CertificateType.RawPublicKey,
190190
null,
191191
new short[] {CertificateType.RawPublicKey},
192-
generateKeypair(),
192+
generateKeyPair(),
193193
tlsVersion);
194194
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
195195
CertificateType.X509,
196196
CertificateType.RawPublicKey,
197197
new short[] {CertificateType.RawPublicKey},
198-
generateKeypair(),
198+
generateKeyPair(),
199199
tlsVersion);
200200
pumpData(client, server);
201201
}
@@ -219,13 +219,13 @@ private void testClientSendsClientCertExtensionButServerHasNoCommonTypes(Protoco
219219
CertificateType.RawPublicKey,
220220
null,
221221
new short[] {CertificateType.RawPublicKey},
222-
generateKeypair(),
222+
generateKeyPair(),
223223
tlsVersion);
224224
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
225225
CertificateType.X509,
226226
CertificateType.X509,
227227
new short[] {CertificateType.X509},
228-
generateKeypair(),
228+
generateKeyPair(),
229229
tlsVersion);
230230
pumpData(client, server);
231231
fail("Should have caused unsupported_certificate alert");
@@ -255,13 +255,13 @@ private void testClientSendsServerCertExtensionButServerHasNoCommonTypes(Protoco
255255
CertificateType.RawPublicKey,
256256
new short[] {CertificateType.RawPublicKey},
257257
null,
258-
generateKeypair(),
258+
generateKeyPair(),
259259
tlsVersion);
260260
MockRawKeysTlsServer server = new MockRawKeysTlsServer(
261261
CertificateType.X509,
262262
CertificateType.RawPublicKey,
263263
new short[] {CertificateType.RawPublicKey},
264-
generateKeypair(),
264+
generateKeyPair(),
265265
tlsVersion);
266266
pumpData(client, server);
267267
fail("Should have caused unsupported_certificate alert");
@@ -272,15 +272,13 @@ private void testClientSendsServerCertExtensionButServerHasNoCommonTypes(Protoco
272272
}
273273
}
274274

275-
private Ed25519PrivateKeyParameters generateKeypair()
275+
private Ed25519PrivateKeyParameters generateKeyPair()
276276
{
277-
return new Ed25519PrivateKeyParameters(rng);
277+
return new Ed25519PrivateKeyParameters(RANDOM);
278278
}
279279

280280
private void pumpData(TlsClient client, TlsServer server) throws Exception
281281
{
282-
SecureRandom secureRandom = new SecureRandom();
283-
284282
PipedInputStream clientRead = TlsTestUtils.createPipedInputStream();
285283
PipedInputStream serverRead = TlsTestUtils.createPipedInputStream();
286284
PipedOutputStream clientWrite = new PipedOutputStream(serverRead);
@@ -298,7 +296,7 @@ private void pumpData(TlsClient client, TlsServer server) throws Exception
298296
int length = 1000;
299297

300298
byte[] data = new byte[length];
301-
secureRandom.nextBytes(data);
299+
RANDOM.nextBytes(data);
302300

303301
OutputStream output = clientProtocol.getOutputStream();
304302
output.write(data);

0 commit comments

Comments
 (0)