mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:46:53 +00:00
Use a programmatically-generated keystore in SniMapperTest
This commit is contained in:
1 parent
3447345d02
commit
ac8d5f81bf
4 files changed
+63
-48
No files matched your search
+57
-12
@@ -24,9 +24,15 @@ import io.netty.handler.ssl.SslContextBuilder;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
|
||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import io.netty.pkitesting.CertificateBuilder;
|
||||
import io.netty.pkitesting.X509Bundle;
|
||||
import io.netty.util.Mapping;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -35,7 +41,10 @@ import javax.net.ssl.SSLEngine;
|
||||
import javax.net.ssl.SSLParameters;
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -47,20 +56,54 @@ class SniMapperTest {
|
||||
// Configuration for precomputed keystore blob defined in sni-mapper-test-keystore.p12
|
||||
private static final String FOO_DOMAIN = "foo.example.com";
|
||||
private static final String BAR_DOMAIN = "bar.example.com";
|
||||
private static final String KEY_STORE_PASSWORD = "password";
|
||||
private static final String KEY_STORE_NAME = "sni-mapper-test-keystore.p12";
|
||||
|
||||
private DefaultEventLoopGroup eventLoopGroup;
|
||||
private static final CertificateBuilder.Algorithm[] ALGORITHMS = new CertificateBuilder.Algorithm[] {
|
||||
CertificateBuilder.Algorithm.ed25519,
|
||||
CertificateBuilder.Algorithm.rsa2048
|
||||
};
|
||||
|
||||
private static DefaultEventLoopGroup eventLoopGroup;
|
||||
private static Mapping<String, SslContext> sniMapping;
|
||||
|
||||
private Channel serverChannel;
|
||||
|
||||
@BeforeAll
|
||||
static void setUpBeforeAll() throws Exception {
|
||||
final Instant now = Instant.now();
|
||||
|
||||
final KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
||||
keyStore.load(null);
|
||||
|
||||
final char[] keyStorePassword = RandomStringUtils.insecure().nextAlphanumeric(16).toCharArray();
|
||||
|
||||
for (final String domain : new String[] { FOO_DOMAIN, BAR_DOMAIN }) {
|
||||
for (final CertificateBuilder.Algorithm algorithm : ALGORITHMS) {
|
||||
final X509Bundle x509Bundle = new CertificateBuilder()
|
||||
.notBefore(now)
|
||||
.notAfter(now.plus(Duration.ofDays(1)))
|
||||
.setIsCertificateAuthority(true)
|
||||
.algorithm(algorithm)
|
||||
.subject("CN=" + domain)
|
||||
.addSanDnsName(domain)
|
||||
.buildSelfSigned();
|
||||
|
||||
keyStore.setEntry(domain + "-" + algorithm,
|
||||
new KeyStore.PrivateKeyEntry(x509Bundle.getKeyPair().getPrivate(), x509Bundle.getCertificatePath()),
|
||||
new KeyStore.PasswordProtection(keyStorePassword));
|
||||
}
|
||||
}
|
||||
|
||||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
keyStore.store(byteArrayOutputStream, keyStorePassword);
|
||||
|
||||
sniMapping = SniMapper.buildSniMapping(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()),
|
||||
new String(keyStorePassword));
|
||||
|
||||
eventLoopGroup = new DefaultEventLoopGroup();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
final InputStream keyStore = SniMapper.class.getResourceAsStream(KEY_STORE_NAME);
|
||||
eventLoopGroup = new DefaultEventLoopGroup();
|
||||
|
||||
final Mapping<String, SslContext> sniMapping =
|
||||
SniMapper.buildSniMapping(keyStore, KEY_STORE_PASSWORD);
|
||||
|
||||
final LocalAddress localAddress = new LocalAddress(SniMapper.class.getSimpleName());
|
||||
serverChannel = new ServerBootstrap()
|
||||
.group(eventLoopGroup)
|
||||
@@ -81,13 +124,15 @@ class SniMapperTest {
|
||||
if (serverChannel != null) {
|
||||
serverChannel.close().sync();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void tearDownAfterAll() throws InterruptedException {
|
||||
eventLoopGroup.shutdownGracefully(1, 1000, TimeUnit.MILLISECONDS).sync();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownDomain() throws Exception {
|
||||
final InputStream keyStore = SniMapper.class.getResourceAsStream(KEY_STORE_NAME);
|
||||
final Mapping<String, SslContext> sniMapping = SniMapper.buildSniMapping(keyStore, KEY_STORE_PASSWORD);
|
||||
assertNotNull(sniMapping.map("unknown.example.com"));
|
||||
final X509Certificate defaultCertificate = connectAndGetServerCertificate("unknown.example.com", null);
|
||||
|
||||
|
||||
Reference in new issue
Block a user