Skip to content

Commit

Permalink
tests: use actual tls certificate instead of generating one at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mostroverkhov committed Jun 11, 2024
1 parent f7f19c3 commit b48f8dc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static void main(String[] args) throws Exception {
boolean isNativeTransport =
Boolean.parseBoolean(System.getProperty("NATIVE_TRANSPORT", "true"));
boolean isEncrypted = Boolean.parseBoolean(System.getProperty("ENCRYPT", "true"));
String keyStoreFile = System.getProperty("KEYSTORE", "localhost.p12");
String keyStorePassword = System.getProperty("KEYSTORE_PASS", "localhost");

boolean isOpensslAvailable = OpenSsl.isAvailable();
boolean isEpollAvailable = Transport.isEpollAvailable();
Expand All @@ -67,7 +69,8 @@ public static void main(String[] args) throws Exception {

Transport transport = Transport.get(isNativeTransport);
logger.info("\n==> io transport: {}", transport.type());
SslContext sslContext = isEncrypted ? Security.serverSslContext() : null;
SslContext sslContext =
isEncrypted ? Security.serverSslContext(keyStoreFile, keyStorePassword) : null;

ServerBootstrap bootstrap = new ServerBootstrap();
Channel server =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static void main(String[] args) throws Exception {
boolean isNativeTransport =
Boolean.parseBoolean(System.getProperty("NATIVE_TRANSPORT", "true"));
boolean isEncrypted = Boolean.parseBoolean(System.getProperty("ENCRYPT", "true"));
String keyStoreFile = System.getProperty("KEYSTORE", "localhost.p12");
String keyStorePassword = System.getProperty("KEYSTORE_PASS", "localhost");

boolean isOpensslAvailable = OpenSsl.isAvailable();
boolean isEpollAvailable = Transport.isEpollAvailable();
Expand All @@ -67,7 +69,8 @@ public static void main(String[] args) throws Exception {

Transport transport = Transport.get(isNativeTransport);
logger.info("\n==> io transport: {}", transport.type());
SslContext sslContext = isEncrypted ? Security.serverSslContext() : null;
SslContext sslContext =
isEncrypted ? Security.serverSslContext(keyStoreFile, keyStorePassword) : null;

ServerBootstrap bootstrap = new ServerBootstrap();
Channel server =
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static void main(String[] args) throws Exception {
int frameSizeLimit = Integer.parseInt(System.getProperty("SIZE", "65535"));
boolean expectMasked = Boolean.parseBoolean(System.getProperty("MASKED", "false"));
boolean maskMismatch = !Boolean.parseBoolean(System.getProperty("STRICT", "false"));
String keyStoreFile = System.getProperty("KEYSTORE", "localhost.p12");
String keyStorePassword = System.getProperty("KEYSTORE_PASS", "localhost");

boolean isOpensslAvailable = OpenSsl.isAvailable();
boolean isEpollAvailable = Transport.isEpollAvailable();
Expand All @@ -67,7 +69,7 @@ public static void main(String[] args) throws Exception {

Transport transport = Transport.get(/*native IO*/ true);
logger.info("\n==> io transport: {}", transport.type());
SslContext sslContext = Security.serverSslContext();
SslContext sslContext = Security.serverSslContext(keyStoreFile, keyStorePassword);

ServerBootstrap bootstrap = new ServerBootstrap();
Channel server =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import java.security.SecureRandom;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Security {
private static final Logger logger = LoggerFactory.getLogger(Security.class);

public static SslContext serverSslContext() throws Exception {
SecureRandom random = new SecureRandom();
SelfSignedCertificate ssc = new SelfSignedCertificate("com.jauntsdn", random, 1024);
public static SslContext serverSslContext(String keystoreFile, String keystorePassword)
throws Exception {
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
KeyStore keyStore = KeyStore.getInstance("PKCS12");
InputStream keystoreStream = Security.class.getClassLoader().getResourceAsStream(keystoreFile);
char[] keystorePasswordArray = keystorePassword.toCharArray();
keyStore.load(keystoreStream, keystorePasswordArray);
keyManagerFactory.init(keyStore, keystorePasswordArray);

return SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
return SslContextBuilder.forServer(keyManagerFactory)
.protocols("TLSv1.3")
.sslProvider(sslProvider())
.ciphers(supportedCypherSuites(), SupportedCipherSuiteFilter.INSTANCE)
Expand Down

0 comments on commit b48f8dc

Please sign in to comment.