fix(elbv2): preserve TLS handshakes during certificate reloads (#6473)

* fix(elbv2): preserve TLS handshakes during certificate reloads

Reuse the generation-aware TLS key manager for ELBv2 and publish fully loaded SNI snapshots without replacing the active SSL context. Preserve the previous snapshot on load failure.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* fix(elbv2): cover TLS resumption and wildcard selection

Verify TLS 1.2 resumption with unchanged material and fresh authentication after certificate replacement or removal. Separate the wildcard certificate from the default and document the full-handshake boundary.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* fix(elbv2): document retained TLS sessions on reload

Exercise stateful and stateless TLS 1.2 resumption with shared client contexts. Document the retained-session certificate boundary while preserving full-handshake refresh behavior.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-24 22:51:13 -04:00
1 parent c517e4eb76
commit 2525e38179
11 files changed
+532 -102

No files matched your search

@@ -1,4 +1,4 @@
package io.github.hectorvent.floci.services.iot;
package io.github.hectorvent.floci.core.common.tls;
import io.vertx.core.Vertx;
import io.vertx.core.net.KeyCertOptions;
@@ -18,7 +18,7 @@ import java.util.List;
import java.util.function.LongSupplier;
/**
* Server key manager for the MQTT TLS listener that answers every handshake from the key material
* Server key manager for TLS listeners that answers every handshake from the key material
* it was last given, so a reissued server certificate is served without rebuilding the listener's
* SSL context: Vert.x drops the connections it accepts while a context is being rebuilt, whereas
* JSSE asks the key manager for an alias, then that alias's key and chain, on every handshake.
@@ -29,7 +29,7 @@ import java.util.function.LongSupplier;
* a handshake that began before any number of reloads still reads a matching key and chain.
* Retired generations are dropped on the next reload after that window.
*/
final class ReloadingKeyManager extends X509ExtendedKeyManager {
public final class ReloadingKeyManager extends X509ExtendedKeyManager {
static final Duration RETIRED_GENERATION_RETENTION = Duration.ofSeconds(60);
@@ -41,7 +41,7 @@ final class ReloadingKeyManager extends X509ExtendedKeyManager {
/** Superseded generations, newest first, each within the retention window at its last reload. */
private volatile List<Generation> retired = List.of();
ReloadingKeyManager(X509KeyManager delegate) {
public ReloadingKeyManager(X509KeyManager delegate) {
this(delegate, System::nanoTime);
}
@@ -51,7 +51,7 @@ final class ReloadingKeyManager extends X509ExtendedKeyManager {
}
/** The first X.509 key manager of {@code options}, the TLS registry's default key store. */
static X509KeyManager keyManagerOf(Vertx vertx, KeyCertOptions options) {
public static X509KeyManager keyManagerOf(Vertx vertx, KeyCertOptions options) {
if (options == null) {
throw new IllegalStateException("the default TLS configuration has no key store");
}
@@ -72,7 +72,7 @@ final class ReloadingKeyManager extends X509ExtendedKeyManager {
}
/** Serves {@code delegate} from the next handshake on; the generations before it stay readable for a while. */
synchronized void reload(X509KeyManager delegate) {
public synchronized void reload(X509KeyManager delegate) {
long now = nanoTime.getAsLong();
List<Generation> kept = new ArrayList<>();
kept.add(new Generation(current.number(), current.delegate(), now));
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.config.TlsProxyServer;
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver;
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver.CertificateMaterial;
import io.github.hectorvent.floci.core.common.Pem;
import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.elbv2.model.Action;
import io.github.hectorvent.floci.services.elbv2.model.Listener;
@@ -31,9 +32,10 @@ import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.net.JdkSSLEngineOptions;
import io.vertx.core.net.KeyCertOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.core.net.SSLOptions;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
@@ -123,6 +125,7 @@ public class ElbV2DataPlane {
private int depth;
private int closing;
private long generation;
private ReloadingKeyManager tlsKeyManager;
private volatile RoutingView routing = new RoutingView(Map.of(), Map.of());
}
@@ -476,9 +479,10 @@ public class ElbV2DataPlane {
.setHost("0.0.0.0")
.setPort(port);
if (secure) {
state.tlsKeyManager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, tlsKeyCertOptions(port)));
options.setSsl(true)
.setSni(true)
.setPemKeyCertOptions(tlsKeyCertOptions(port));
.setSslEngineOptions(new JdkSSLEngineOptions())
.setKeyCertOptions(KeyCertOptions.wrap(state.tlsKeyManager));
}
HttpServer server = vertx.createHttpServer(options);
long generation = ++state.generation;
@@ -655,9 +659,15 @@ public class ElbV2DataPlane {
}
private void refreshTlsOptions(int port, HttpServer server) {
server.updateSSLOptions(new SSLOptions().setKeyCertOptions(tlsKeyCertOptions(port)), true)
.onFailure(err -> LOG.warnv("ELBv2 listener TLS certificates failed to refresh on {0}: {1}",
String.valueOf(port), err.getMessage()));
PortState state = portStates.get(port);
if (state != null && servers.get(port) == server && state.tlsKeyManager != null) {
try {
state.tlsKeyManager.reload(new ElbV2TlsKeyManager(vertx, tlsKeyCertOptions(port)));
} catch (RuntimeException failure) {
LOG.warnv(failure, "ELBv2 listener TLS certificates failed to refresh on {0}: {1}",
String.valueOf(port), failure.getMessage());
}
}
}
private void restartPortServer(int port) {
@@ -0,0 +1,117 @@
package io.github.hectorvent.floci.services.elbv2;
import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;
import io.vertx.core.Vertx;
import io.vertx.core.net.KeyCertOptions;
import javax.net.ssl.ExtendedSSLSession;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SNIServerName;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import javax.net.ssl.X509KeyManager;
import java.net.Socket;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Locale;
import java.util.function.Function;
/**
* An immutable SNI selection snapshot. Host-qualified aliases keep key and chain lookups on the
* selected manager; the enclosing ReloadingKeyManager also qualifies them by certificate generation.
*/
final class ElbV2TlsKeyManager extends X509ExtendedKeyManager {
private final X509KeyManager defaultManager;
private final Function<String, KeyManagerFactory> managers;
ElbV2TlsKeyManager(Vertx vertx, KeyCertOptions options) {
defaultManager = ReloadingKeyManager.keyManagerOf(vertx, options);
try {
managers = options.keyManagerFactoryMapper(vertx);
} catch (Exception failure) {
throw new IllegalStateException("Cannot load ELBv2 SNI certificates", failure);
}
}
@Override
public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) {
String host = "";
if (engine.getHandshakeSession() instanceof ExtendedSSLSession session) {
for (SNIServerName name : session.getRequestedServerNames()) {
if (name instanceof SNIHostName hostname) {
host = hostname.getAsciiName().toLowerCase(Locale.ROOT);
break;
}
}
}
X509KeyManager manager = managerFor(host);
String alias = manager instanceof X509ExtendedKeyManager extended
? extended.chooseEngineServerAlias(keyType, issuers, engine)
: manager.chooseServerAlias(keyType, issuers, null);
return alias == null ? null : host + ":" + alias;
}
@Override
public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
String alias = defaultManager.chooseServerAlias(keyType, issuers, socket);
return alias == null ? null : ":" + alias;
}
@Override
public String[] getServerAliases(String keyType, Principal[] issuers) {
String[] aliases = defaultManager.getServerAliases(keyType, issuers);
if (aliases == null) {
return new String[0];
}
String[] qualified = new String[aliases.length];
for (int i = 0; i < aliases.length; i++) {
qualified[i] = ":" + aliases[i];
}
return qualified;
}
@Override
public X509Certificate[] getCertificateChain(String alias) {
int separator = alias == null ? -1 : alias.indexOf(':');
return separator < 0 ? null : managerFor(alias.substring(0, separator))
.getCertificateChain(alias.substring(separator + 1));
}
@Override
public PrivateKey getPrivateKey(String alias) {
int separator = alias == null ? -1 : alias.indexOf(':');
return separator < 0 ? null : managerFor(alias.substring(0, separator))
.getPrivateKey(alias.substring(separator + 1));
}
@Override
public String[] getClientAliases(String keyType, Principal[] issuers) {
return new String[0];
}
@Override
public String chooseClientAlias(String[] keyTypes, Principal[] issuers, Socket socket) {
return null;
}
@Override
public String chooseEngineClientAlias(String[] keyTypes, Principal[] issuers, SSLEngine engine) {
return null;
}
private X509KeyManager managerFor(String host) {
KeyManagerFactory factory = host.isEmpty() ? null : managers.apply(host);
if (factory != null) {
for (KeyManager manager : factory.getKeyManagers()) {
if (manager instanceof X509KeyManager x509) {
return x509;
}
}
}
return defaultManager;
}
}
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.common.tls.ReloadingKeyManager;
import io.github.hectorvent.floci.services.iot.model.IotRetainedMessage;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;