Skip to content

Commit a8219bf

Browse files
committed
Replace runtime certificate generation with pre-generated fixtures
1 parent 769a433 commit a8219bf

16 files changed

Lines changed: 1401 additions & 228 deletions

‎test/org/apache/tomcat/security/TestSecurity2018WebSocket.java‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.tomcat.security;
1919

20-
import java.io.File;
2120
import java.net.URI;
2221

2322
import javax.net.ssl.SSLContext;
@@ -34,7 +33,6 @@
3433
import org.apache.catalina.Context;
3534
import org.apache.catalina.servlets.DefaultServlet;
3635
import org.apache.catalina.startup.Tomcat;
37-
import org.apache.tomcat.util.net.TesterKeystoreGenerator;
3836
import org.apache.tomcat.util.net.TesterSupport;
3937
import org.apache.tomcat.websocket.TesterEchoServer;
4038
import org.apache.tomcat.websocket.TesterMessageCountClient;
@@ -45,13 +43,9 @@ public class TestSecurity2018WebSocket extends WebSocketBaseTest {
4543
// https://www.cve.org/CVERecord?id=CVE-2018-8034
4644
@Test(expected = DeploymentException.class)
4745
public void testCVE_2018_8034() throws Exception {
48-
File keystoreFile = TesterKeystoreGenerator.generateKeystore(
49-
"localhost", "tomcat",
50-
new String[]{"localhost"}, null);
51-
5246
Tomcat tomcat = getTomcatInstance();
5347

54-
TesterSupport.initSsl(tomcat, keystoreFile.getAbsolutePath(), false);
48+
TesterSupport.initSsl(tomcat, TesterSupport.LOCALHOST_RSA_DNSONLY_JKS, false);
5549

5650
Context ctx = getProgrammaticRootContext();
5751
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());

‎test/org/apache/tomcat/util/net/TestLargeClientHello.java‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.tomcat.util.net;
1919

20-
import java.io.File;
2120
import java.io.OutputStream;
2221
import java.net.InetSocketAddress;
2322
import java.net.Socket;
@@ -32,41 +31,19 @@
3231
import org.apache.catalina.Context;
3332
import org.apache.catalina.startup.Tomcat;
3433
import org.apache.catalina.startup.TomcatBaseTest;
35-
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
36-
import org.bouncycastle.asn1.DERUTF8String;
37-
import org.bouncycastle.asn1.x509.BasicConstraints;
38-
import org.bouncycastle.asn1.x509.Extension;
39-
import org.bouncycastle.asn1.x509.KeyUsage;
40-
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
4134

4235
public class TestLargeClientHello extends TomcatBaseTest {
4336

4437
// https://bz.apache.org/bugzilla/show_bug.cgi?id=67938
4538
@Test
4639
public void testLargeClientHelloWithSessionResumption() throws Exception {
47-
File keystoreFile = TesterKeystoreGenerator.generateKeystore("localhost", "tomcat",
48-
new String[]{"localhost", "*.localhost"},
49-
(keyPair, certBuilder) -> {
50-
JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
51-
certBuilder.addExtension(Extension.subjectKeyIdentifier, false,
52-
extUtils.createSubjectKeyIdentifier(keyPair.getPublic()));
53-
certBuilder.addExtension(Extension.authorityKeyIdentifier, false,
54-
extUtils.createAuthorityKeyIdentifier(keyPair.getPublic()));
55-
certBuilder.addExtension(Extension.basicConstraints, true,
56-
new BasicConstraints(true));
57-
certBuilder.addExtension(Extension.keyUsage, false,
58-
new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
59-
certBuilder.addExtension(new ASN1ObjectIdentifier("2.999"), false,
60-
new DERUTF8String("x".repeat(16922)));
61-
});
62-
6340
Tomcat tomcat = getTomcatInstance();
6441

6542
Context ctx = getProgrammaticRootContext();
6643
Tomcat.addServlet(ctx, "hello", new HelloWorldServlet());
6744
ctx.addServletMapping("/", "hello");
6845

69-
TesterSupport.initSsl(tomcat, keystoreFile.getAbsolutePath(), false);
46+
TesterSupport.initSsl(tomcat, TesterSupport.LOCALHOST_RSA_LARGECERT_JKS, false);
7047

7148
try (LogCapture logCapture = attachLogCapture(Level.FINE, "org.apache.tomcat.util.net.SecureNioChannel")) {
7249

‎test/org/apache/tomcat/util/net/TestPQC.java‎

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,22 @@ public void setUp() throws Exception {
101101

102102
@Test
103103
public void testHostMLDSA44() throws Exception {
104-
File[] pqcFiles = configureHostMLDSA("ML-DSA-44");
105-
doTestWithOpenSSLClient(pqcFiles[0].getAbsolutePath(), null, null, null);
104+
configureHostMLDSA("ML-DSA-44");
105+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), null, null, null);
106106
}
107107

108108

109109
@Test
110110
public void testHostMLDSA65() throws Exception {
111-
File[] pqcFiles = configureHostMLDSA("ML-DSA-65");
112-
doTestWithOpenSSLClient(pqcFiles[0].getAbsolutePath(), null, null, null);
111+
configureHostMLDSA("ML-DSA-65");
112+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), null, null, null);
113113
}
114114

115115

116116
@Test
117117
public void testHostMLDSA87() throws Exception {
118-
File[] pqcFiles = configureHostMLDSA("ML-DSA-87");
119-
doTestWithOpenSSLClient(pqcFiles[0].getAbsolutePath(), null, null, null);
118+
configureHostMLDSA("ML-DSA-87");
119+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), null, null, null);
120120
}
121121

122122
@Test
@@ -137,7 +137,7 @@ public void testHostECandMLDSA() throws Exception {
137137
public void testHostRSAwithX25519MLKEM768() throws Exception {
138138
configureHostRSA();
139139
configureHostWithGroup("X25519MLKEM768");
140-
doTestWithOpenSSLClient(new File(TesterSupport.CA_CERT_PEM).getAbsolutePath(),
140+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(),
141141
"X25519MLKEM768", null, null);
142142
}
143143

@@ -146,53 +146,51 @@ public void testHostRSAwithX25519MLKEM768() throws Exception {
146146
public void testHostRSAwithSecP256r1MLKEM768() throws Exception {
147147
configureHostRSA();
148148
configureHostWithGroup("SecP256r1MLKEM768");
149-
doTestWithOpenSSLClient(new File(TesterSupport.CA_CERT_PEM).getAbsolutePath(),
149+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(),
150150
"SecP256r1MLKEM768", null, null);
151151
}
152152

153153
@Test
154154
public void testHostRSAwithSecP384r1MLKEM1024() throws Exception {
155155
configureHostRSA();
156156
configureHostWithGroup("SecP384r1MLKEM1024");
157-
doTestWithOpenSSLClient(new File(TesterSupport.CA_CERT_PEM).getAbsolutePath(),
157+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(),
158158
"SecP384r1MLKEM1024", null, null);
159159
}
160160

161161
@Test
162162
public void testHostMLDSAwithX25519MLKEM768() throws Exception {
163-
File[] pqcFiles = configureHostMLDSA("ML-DSA-65");
163+
configureHostMLDSA("ML-DSA-65");
164164
configureHostWithGroup("X25519MLKEM768");
165-
doTestWithOpenSSLClient(pqcFiles[0].getAbsolutePath(), "X25519MLKEM768", null, null);
165+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), "X25519MLKEM768", null, null);
166166
}
167167

168168
@Test
169169
public void testHostMLDSAwithSecP256r1MLKEM768() throws Exception {
170-
File[] pqcFiles = configureHostMLDSA("ML-DSA-65");
170+
configureHostMLDSA("ML-DSA-65");
171171
configureHostWithGroup("SecP256r1MLKEM768");
172-
doTestWithOpenSSLClient(pqcFiles[0].getAbsolutePath(), "SecP256r1MLKEM768", null, null);
172+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), "SecP256r1MLKEM768", null, null);
173173
}
174174

175175
@Test
176176
public void testClientMLDSA() throws Exception {
177177
configureHostRSA();
178-
File[] clientFiles = TesterKeystoreGenerator.generatePQCCertificate("testuser", "ML-DSA-65",
179-
null, null);
178+
File[] clientFiles = getTestUserMLDSA65Files();
180179
SSLHostConfig sslHostConfig = getTomcatInstance().getConnector().findSslHostConfigs()[0];
181180
sslHostConfig.setCertificateVerification("required");
182-
sslHostConfig.setCaCertificateFile(clientFiles[0].getAbsolutePath());
183-
doTestWithOpenSSLClient(new File(TesterSupport.CA_CERT_PEM).getAbsolutePath(), null,
181+
sslHostConfig.setCaCertificateFile(getCaCertificateFile().getAbsolutePath());
182+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), null,
184183
clientFiles[0].getAbsolutePath(), clientFiles[1].getAbsolutePath());
185184
}
186185

187186
@Test
188187
public void testClientMLDSAwithMLDSAServer() throws Exception {
189-
File[] serverFiles = configureHostMLDSA("ML-DSA-65");
190-
File[] clientFiles = TesterKeystoreGenerator.generatePQCCertificate("testuser", "ML-DSA-65",
191-
null, null);
188+
configureHostMLDSA("ML-DSA-65");
189+
File[] clientFiles = getTestUserMLDSA65Files();
192190
SSLHostConfig sslHostConfig = getTomcatInstance().getConnector().findSslHostConfigs()[0];
193191
sslHostConfig.setCertificateVerification("required");
194-
sslHostConfig.setCaCertificateFile(clientFiles[0].getAbsolutePath());
195-
doTestWithOpenSSLClient(serverFiles[0].getAbsolutePath(), null,
192+
sslHostConfig.setCaCertificateFile(getCaCertificateFile().getAbsolutePath());
193+
doTestWithOpenSSLClient(getCaCertificateFile().getAbsolutePath(), null,
196194
clientFiles[0].getAbsolutePath(), clientFiles[1].getAbsolutePath());
197195
}
198196

@@ -230,20 +228,43 @@ private void assumePQCSupported() {
230228
}
231229
}
232230

233-
private File[] configureHostMLDSA(String algorithm) throws Exception {
234-
File[] pqcFiles = TesterKeystoreGenerator.generatePQCCertificate("localhost", algorithm,
235-
new String[] { "localhost" }, null);
231+
private File getCaCertificateFile() {
232+
return new File(TesterSupport.CA_CERT_PEM);
233+
}
234+
235+
private File[] getTestUserMLDSA65Files() {
236+
return new File[] { new File(TesterSupport.TESTUSER_MLDSA_65_CERT_PEM),
237+
new File(TesterSupport.TESTUSER_MLDSA_65_KEY_PEM) };
238+
}
239+
240+
private void configureHostMLDSA(String algorithm) {
241+
String certFile;
242+
String keyFile;
243+
switch (algorithm) {
244+
case "ML-DSA-44":
245+
certFile = TesterSupport.LOCALHOST_MLDSA_44_CERT_PEM;
246+
keyFile = TesterSupport.LOCALHOST_MLDSA_44_KEY_PEM;
247+
break;
248+
case "ML-DSA-65":
249+
certFile = TesterSupport.LOCALHOST_MLDSA_65_CERT_PEM;
250+
keyFile = TesterSupport.LOCALHOST_MLDSA_65_KEY_PEM;
251+
break;
252+
case "ML-DSA-87":
253+
certFile = TesterSupport.LOCALHOST_MLDSA_87_CERT_PEM;
254+
keyFile = TesterSupport.LOCALHOST_MLDSA_87_KEY_PEM;
255+
break;
256+
default:
257+
throw new IllegalArgumentException("Unknown ML-DSA algorithm [" + algorithm + "]");
258+
}
236259

237260
Tomcat tomcat = getTomcatInstance();
238261
Connector connector = tomcat.getConnector();
239262
SSLHostConfig sslHostConfig = connector.findSslHostConfigs()[0];
240263

241264
SSLHostConfigCertificate cert = new SSLHostConfigCertificate(sslHostConfig, Type.MLDSA);
242-
cert.setCertificateFile(pqcFiles[0].getAbsolutePath());
243-
cert.setCertificateKeyFile(pqcFiles[1].getAbsolutePath());
265+
cert.setCertificateFile(new File(certFile).getAbsolutePath());
266+
cert.setCertificateKeyFile(new File(keyFile).getAbsolutePath());
244267
sslHostConfig.addCertificate(cert);
245-
246-
return pqcFiles;
247268
}
248269

249270
private void configureHostRSA() {

0 commit comments

Comments
 (0)