Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 63 additions & 18 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,22 +398,44 @@ static int tlsUpdateCertInfoFromDir(const char *path, long long *expiry, sds *se
return tlsStoreCertInfo(earliest_expiry, earliest_serial, cert_count, expiry, serial, count);
}

/* Serial and expiry of the two server certificates, captured while the context is built
* because a finished context cannot tell you which slot came from tls-cert-file: OpenSSL
* orders slots by key algorithm and records nothing about configuration order. The key
* algorithm cannot stand in for the slot either, since EVP_PKEY_base_id() is NID_undef
* for a provider-only algorithm such as ML-DSA. */
typedef struct {
long long cert_expiry;
sds cert_serial;
long long alt_cert_expiry;
sds alt_cert_serial;
} tlsServerCertInfo;

static tlsServerCertInfo active_cert_info = {0};

static void tlsClearServerCertInfo(tlsServerCertInfo *info) {
tlsClearCertSerial(&info->cert_serial);
tlsClearCertSerial(&info->alt_cert_serial);
info->cert_expiry = 0;
info->alt_cert_expiry = 0;
}

static void tlsPublishCertInfo(long long src_expiry, sds src_serial, long long *expiry, sds *serial) {
tlsClearCertInfo(expiry, serial);
if (!src_serial) return;
*expiry = src_expiry;
*serial = sdsdup(src_serial);
}

static void tlsRefreshServerCertInfo(void) {
/* Cycle through both certificates to get the correct info for each */
if (!(server.tls_port || server.tls_replication || server.tls_cluster) || !valkey_tls_ctx ||
SSL_CTX_set_current_cert(valkey_tls_ctx, SSL_CERT_SET_FIRST) != 1 ||
tlsUpdateCertInfoFromCtx(valkey_tls_ctx, &server.tls_server_cert_expire_time, &server.tls_server_cert_serial) == C_ERR) {
tlsClearCertInfo(&server.tls_server_cert_expire_time, &server.tls_server_cert_serial);
}
if (SSL_CTX_set_current_cert(valkey_tls_ctx, SSL_CERT_SET_NEXT) != 1 ||
tlsUpdateCertInfoFromCtx(valkey_tls_ctx, &server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial) == C_ERR) {
tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial);
}
if (SSL_CTX_set_current_cert(valkey_tls_ctx, SSL_CERT_SET_FIRST) != 1) {
serverLog(LL_WARNING, "Certificate unset during refresh, clearing all server certificate info");
if (!(server.tls_port || server.tls_replication || server.tls_cluster) || !valkey_tls_ctx) {
tlsClearCertInfo(&server.tls_server_cert_expire_time, &server.tls_server_cert_serial);
tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial);
return;
}
tlsPublishCertInfo(active_cert_info.cert_expiry, active_cert_info.cert_serial,
&server.tls_server_cert_expire_time, &server.tls_server_cert_serial);
tlsPublishCertInfo(active_cert_info.alt_cert_expiry, active_cert_info.alt_cert_serial,
&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial);
}

static void tlsRefreshClientCertInfo(void) {
Expand Down Expand Up @@ -571,7 +593,7 @@ static bool areAllCaCertsValid(SSL_CTX *ctx) {
/* Create a *base* SSL_CTX using the SSL configuration provided. The base context
* includes everything that's common for both client-side and server-side connections.
*/
static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protocols, int client) {
static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protocols, int client, tlsServerCertInfo *out_info) {
const char *cert_file = client ? ctx_config->client_cert_file : ctx_config->cert_file;
const char *key_file = client ? ctx_config->client_key_file : ctx_config->key_file;
const char *key_file_pass = client ? ctx_config->client_key_file_pass : ctx_config->key_file_pass;
Expand Down Expand Up @@ -622,6 +644,10 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco
goto error;
}

/* INFO reporting only. A failure leaves the field as none rather than refusing the
* configuration. */
if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->cert_expiry, &out_info->cert_serial);

if (alt_cert_file) {
primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx));
if (!primary_pkey) {
Expand Down Expand Up @@ -650,6 +676,7 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco
serverLog(LL_WARNING, "Primary and alternate certificates must use different key algorithms");
goto error;
}
if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->alt_cert_expiry, &out_info->alt_cert_serial);
}

if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
Expand Down Expand Up @@ -696,6 +723,10 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco
}
#endif

/* Loading the alternate certificate moved the current certificate to its slot,
* and outgoing TLS 1.2 connections pick their client certificate from there.
* Leave it on the lowest slot, which is where it sat before dual certificates. */
SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
EVP_PKEY_free(primary_pkey);
EVP_PKEY_free(alt_pkey);
return ctx;
Expand All @@ -712,7 +743,10 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco
* Returns C_OK on success, C_ERR on failure.
* On success, *ctx and *client_ctx are set (client_ctx may be NULL).
* On failure, both are set to NULL. */
static int tlsCreateContexts(serverTLSContextConfig *ctx_config, SSL_CTX **out_ctx, SSL_CTX **out_client_ctx) {
static int tlsCreateContexts(serverTLSContextConfig *ctx_config,
SSL_CTX **out_ctx,
SSL_CTX **out_client_ctx,
tlsServerCertInfo *out_info) {
char errbuf[256];
SSL_CTX *ctx = NULL;
SSL_CTX *client_ctx = NULL;
Expand Down Expand Up @@ -748,7 +782,7 @@ static int tlsCreateContexts(serverTLSContextConfig *ctx_config, SSL_CTX **out_c
if (protocols == -1) goto error;

/* Create server side/general context */
ctx = createSSLContext(ctx_config, protocols, 0);
ctx = createSSLContext(ctx_config, protocols, 0, out_info);
if (!ctx) goto error;

if (ctx_config->session_caching) {
Expand Down Expand Up @@ -829,7 +863,7 @@ static int tlsCreateContexts(serverTLSContextConfig *ctx_config, SSL_CTX **out_c

/* If a client-side certificate is configured, create an explicit client context */
if (ctx_config->client_cert_file && ctx_config->client_key_file) {
client_ctx = createSSLContext(ctx_config, protocols, 1);
client_ctx = createSSLContext(ctx_config, protocols, 1, NULL);
if (!client_ctx) goto error;
}

Expand All @@ -838,6 +872,7 @@ static int tlsCreateContexts(serverTLSContextConfig *ctx_config, SSL_CTX **out_c
return C_OK;

error:
if (out_info) tlsClearServerCertInfo(out_info);
if (ctx) SSL_CTX_free(ctx);
if (client_ctx) SSL_CTX_free(client_ctx);
*out_ctx = NULL;
Expand Down Expand Up @@ -869,6 +904,7 @@ typedef struct {
SSL_CTX *ctx;
SSL_CTX *client_ctx;
tlsMaterialsMetadata metadata;
tlsServerCertInfo cert_info;
} tlsPendingReload;

/* Last known (active) TLS materials metadata */
Expand Down Expand Up @@ -1006,7 +1042,8 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) {
}
serverLog(LL_NOTICE, "TLS materials changed, reloading in background");

if (tlsCreateContexts(ctx_config, &ctx, &client_ctx) == C_ERR) {
tlsServerCertInfo cert_info = {0};
if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_info) == C_ERR) {
serverLog(LL_WARNING, "Background TLS reload failed");
return C_ERR;
}
Expand All @@ -1015,23 +1052,28 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) {
if (pending_reload.ctx) {
SSL_CTX_free(pending_reload.ctx);
SSL_CTX_free(pending_reload.client_ctx);
tlsClearServerCertInfo(&pending_reload.cert_info);
serverLog(LL_DEBUG, "Replacing previous pending TLS reload");
}
pending_reload.ctx = ctx;
pending_reload.client_ctx = client_ctx;
pending_reload.metadata = new_metadata;
pending_reload.cert_info = cert_info;
pthread_mutex_unlock(&pending_reload_mutex);

serverLog(LL_DEBUG, "Background TLS reload parsed TLS materials successfully");
} else {
if (tlsCreateContexts(ctx_config, &ctx, &client_ctx) == C_ERR) {
tlsServerCertInfo cert_info = {0};
if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_info) == C_ERR) {
return C_ERR;
}

SSL_CTX_free(valkey_tls_ctx);
SSL_CTX_free(valkey_tls_client_ctx);
valkey_tls_ctx = ctx;
valkey_tls_client_ctx = client_ctx;
tlsClearServerCertInfo(&active_cert_info);
active_cert_info = cert_info;
captureMetadata(ctx_config, &active_metadata);
tlsRefreshAllCertInfo();
}
Expand Down Expand Up @@ -1067,6 +1109,7 @@ void tlsApplyPendingReload(void) {
if (!metadataChanged(&active_metadata, &pending_reload.metadata)) {
SSL_CTX_free(pending_reload.ctx);
SSL_CTX_free(pending_reload.client_ctx);
tlsClearServerCertInfo(&pending_reload.cert_info);
memset(&pending_reload, 0, sizeof(pending_reload));
pthread_mutex_unlock(&pending_reload_mutex);
serverLog(LL_DEBUG, "Discarding pending TLS reload with unchanged materials");
Expand All @@ -1084,6 +1127,8 @@ void tlsApplyPendingReload(void) {
valkey_tls_client_ctx = local_pending.client_ctx;

active_metadata = local_pending.metadata;
tlsClearServerCertInfo(&active_cert_info);
active_cert_info = local_pending.cert_info;

SSL_CTX_free(old_ctx);
SSL_CTX_free(old_client_ctx);
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/tls.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,87 @@ start_server {tags {"tls"}} {
}
}

test {TLS: INFO reports each certificate against the config that loaded it} {
set rsa_crt [format "%s/tests/tls/valkey.crt" [pwd]]
set rsa_key [format "%s/tests/tls/valkey.key" [pwd]]
set ec_crt [format "%s/tests/tls/valkey-ec.crt" [pwd]]
set ec_key [format "%s/tests/tls/valkey-ec.key" [pwd]]

# With one certificate there is nothing to attribute, so this pins which
# serial belongs to which file.
start_server [list overrides [list tls-cert-file $ec_crt tls-key-file $ec_key]] {
set info [r info tls]
set ec_serial [getInfoProperty $info tls_server_cert_serial]
assert {$ec_serial ni {"" "none"}}
assert_equal "none" [getInfoProperty $info tls_server_alt_cert_serial]
}

# OpenSSL orders its certificate slots by key algorithm, not by the order
# the certificates were configured, so the EC certificate has to follow the
# config it was given to rather than staying on one side.
start_server [list overrides [list tls-cert-file $ec_crt tls-key-file $ec_key \
tls-alt-cert-file $rsa_crt tls-alt-key-file $rsa_key]] {
set info [r info tls]
assert_equal $ec_serial [getInfoProperty $info tls_server_cert_serial]
set rsa_serial [getInfoProperty $info tls_server_alt_cert_serial]
assert {$rsa_serial ne $ec_serial}
}

start_server [list overrides [list tls-cert-file $rsa_crt tls-key-file $rsa_key \
tls-alt-cert-file $ec_crt tls-alt-key-file $ec_key]] {
set info [r info tls]
assert_equal $rsa_serial [getInfoProperty $info tls_server_cert_serial]
assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial]
}
}

test {TLS: INFO follows the config across a background reload} {
if {$::tls_module} {
# The auto-reload cron is only installed when TLS is built in
skip "Not supported with TLS built as a module"
}

set rsa_crt [format "%s/tests/tls/valkey.crt" [pwd]]
set rsa_key [format "%s/tests/tls/valkey.key" [pwd]]
set ec_crt [format "%s/tests/tls/valkey-ec.crt" [pwd]]
set ec_key [format "%s/tests/tls/valkey-ec.key" [pwd]]
set tmp_crt [format "%s/tests/tls/reload-primary.crt" [pwd]]
set tmp_key [format "%s/tests/tls/reload-primary.key" [pwd]]
set tmp_alt_crt [format "%s/tests/tls/reload-alt.crt" [pwd]]
set tmp_alt_key [format "%s/tests/tls/reload-alt.key" [pwd]]
file copy -force $ec_crt $tmp_crt
file copy -force $ec_key $tmp_key
file copy -force $rsa_crt $tmp_alt_crt
file copy -force $rsa_key $tmp_alt_key

try {
start_server [list overrides [list tls-cert-file $tmp_crt tls-key-file $tmp_key \
tls-alt-cert-file $tmp_alt_crt tls-alt-key-file $tmp_alt_key \
tls-auto-reload-interval 1]] {
set info [r info tls]
set before_primary [getInfoProperty $info tls_server_cert_serial]
set before_alt [getInfoProperty $info tls_server_alt_cert_serial]
assert {$before_primary ni {"" "none"}}
assert {$before_primary ne $before_alt}

# The reload builds its context off the main thread, so it has to
# carry its own mapping. Trade the two files, and the two reported
# serials have to trade with them.
file copy -force $rsa_crt $tmp_crt
file copy -force $rsa_key $tmp_key
file copy -force $ec_crt $tmp_alt_crt
file copy -force $ec_key $tmp_alt_key
wait_for_log_messages 0 {"*TLS materials reloaded successfully*"} 0 150 100

set info [r info tls]
assert_equal $before_alt [getInfoProperty $info tls_server_cert_serial]
assert_equal $before_primary [getInfoProperty $info tls_server_alt_cert_serial]
}
} finally {
file delete -force $tmp_crt $tmp_key $tmp_alt_crt $tmp_alt_key
}
}

test {TLS: alt cert and key files must be provided together} {
# backup current certificates
set orig_server_crt [lindex [r config get tls-cert-file] 1]
Expand Down
Loading