From a4c3012d5e8caf3b7e8953666ceaee6930a05492 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 09:51:43 -0700 Subject: [PATCH 01/15] Report each server certificate against the config that loaded it tlsRefreshServerCertInfo walked the SSL_CTX certificate cursor with SSL_CERT_SET_FIRST and SSL_CERT_SET_NEXT and assigned the results to tls_server_cert_serial and tls_server_alt_cert_serial in that order. OpenSSL keeps one certificate slot per key algorithm and orders the slots by algorithm, so ssl_cert_set_current(SSL_CERT_SET_FIRST) returns the lowest-numbered algorithm rather than the certificate configured first. SSL_PKEY_RSA is 0, so an RSA certificate always reported as the primary regardless of which config loaded it, and swapping tls-cert-file with tls-alt-cert-file produced identical INFO output. Select each certificate by the key algorithm of the file that configured it instead. The two are already required to use different key algorithms, so the algorithm identifies the slot. The single-certificate case keeps using the cursor, where there is nothing to disambiguate. Also drops the third SSL_CTX_set_current_cert block: ssl_cert_set_current rescans from slot 0 on SSL_CERT_SET_FIRST over an unchanged certificate set, so it could not fail once the first call had succeeded. Signed-off-by: Madelyn Olson --- src/tls.c | 70 +++++++++++++++++++++++++++++++++++++++------- tests/unit/tls.tcl | 43 ++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 10 deletions(-) diff --git a/src/tls.c b/src/tls.c index b4878af61..b7b241364 100644 --- a/src/tls.c +++ b/src/tls.c @@ -398,22 +398,72 @@ static int tlsUpdateCertInfoFromDir(const char *path, long long *expiry, sds *se return tlsStoreCertInfo(earliest_expiry, earliest_serial, cert_count, expiry, serial, count); } +/* Key algorithm of the leaf certificate in 'path', or NID_undef if it can't be read. */ +static int tlsCertFileKeyAlgorithm(const char *path) { + if (!path) return NID_undef; + FILE *fp = fopen(path, "r"); + if (!fp) return NID_undef; + X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL); + fclose(fp); + if (!cert) return NID_undef; + int alg = NID_undef; + EVP_PKEY *pkey = X509_get_pubkey(cert); + if (pkey) { + alg = EVP_PKEY_base_id(pkey); + EVP_PKEY_free(pkey); + } + X509_free(cert); + return alg; +} + +/* Point ctx's current certificate at the one whose key algorithm is 'alg'. + * + * OpenSSL keeps one certificate slot per key algorithm and orders the slots by + * algorithm, so SSL_CERT_SET_FIRST selects the lowest-numbered algorithm rather + * than the certificate configured first. That makes the cursor position useless + * for telling tls-cert-file from tls-alt-cert-file. The two are required to use + * different key algorithms, so the algorithm identifies the slot. */ +static int tlsSelectCertByKeyAlgorithm(SSL_CTX *ctx, int alg) { + if (!ctx || alg == NID_undef) return C_ERR; + for (int op = SSL_CERT_SET_FIRST;; op = SSL_CERT_SET_NEXT) { + if (SSL_CTX_set_current_cert(ctx, op) != 1) return C_ERR; + EVP_PKEY *pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); + if (!pkey) continue; + int slot_alg = EVP_PKEY_base_id(pkey); + EVP_PKEY_free(pkey); + if (slot_alg == alg) return C_OK; + } +} + +static void tlsRefreshCertInfoForFile(const char *cert_file, long long *expiry, sds *serial) { + if (tlsSelectCertByKeyAlgorithm(valkey_tls_ctx, tlsCertFileKeyAlgorithm(cert_file)) == C_ERR || + tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_ERR) { + tlsClearCertInfo(expiry, 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) { + 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); - } - 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); + return; } - 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"); - tlsClearCertInfo(&server.tls_server_cert_expire_time, &server.tls_server_cert_serial); + + if (!server.tls_ctx_config.alt_cert_file) { + /* Only one certificate is loaded, so the cursor is unambiguous. */ + if (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); + } tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial); + return; } + + tlsRefreshCertInfoForFile(server.tls_ctx_config.cert_file, &server.tls_server_cert_expire_time, + &server.tls_server_cert_serial); + tlsRefreshCertInfoForFile(server.tls_ctx_config.alt_cert_file, &server.tls_server_alt_cert_expire_time, + &server.tls_server_alt_cert_serial); + SSL_CTX_set_current_cert(valkey_tls_ctx, SSL_CERT_SET_FIRST); } static void tlsRefreshClientCertInfo(void) { diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 4619e6828..a18808617 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -154,6 +154,49 @@ start_server {tags {"tls"}} { } } + test {TLS: INFO reports each certificate against the config that loaded it} { + # backup current certificates + set orig_server_crt [lindex [r config get tls-cert-file] 1] + set orig_server_key [lindex [r config get tls-key-file] 1] + set orig_server_alt_crt [lindex [r config get tls-alt-cert-file] 1] + set orig_server_alt_key [lindex [r config get tls-alt-key-file] 1] + + set valkey_crt [format "%s/tests/tls/valkey.crt" [pwd]] + set valkey_key [format "%s/tests/tls/valkey.key" [pwd]] + set valkey_ec_crt [format "%s/tests/tls/valkey-ec.crt" [pwd]] + set valkey_ec_key [format "%s/tests/tls/valkey-ec.key" [pwd]] + + # OpenSSL orders its certificate slots by key algorithm, not by the + # order the certificates were configured, so swapping the two configs + # must swap the two reported serials. + proc server_cert_serials {} { + set info [r info tls] + if {![regexp {tls_server_cert_serial:([^\r\n]+)} $info -> primary]} { + fail "INFO tls missing tls_server_cert_serial" + } + if {![regexp {tls_server_alt_cert_serial:([^\r\n]+)} $info -> alt]} { + fail "INFO tls missing tls_server_alt_cert_serial" + } + return [list $primary $alt] + } + + try { + r CONFIG SET tls-cert-file $valkey_ec_crt tls-key-file $valkey_ec_key tls-alt-cert-file $valkey_crt tls-alt-key-file $valkey_key + lassign [server_cert_serials] ec_as_primary rsa_as_alt + assert {$ec_as_primary ne "none"} + assert {$rsa_as_alt ne "none"} + assert {$ec_as_primary ne $rsa_as_alt} + + r CONFIG SET tls-cert-file $valkey_crt tls-key-file $valkey_key tls-alt-cert-file $valkey_ec_crt tls-alt-key-file $valkey_ec_key + lassign [server_cert_serials] rsa_as_primary ec_as_alt + assert_equal $rsa_as_alt $rsa_as_primary + assert_equal $ec_as_primary $ec_as_alt + } finally { + #cleanup + r CONFIG SET tls-cert-file $orig_server_crt tls-key-file $orig_server_key tls-alt-cert-file $orig_server_alt_crt tls-alt-key-file $orig_server_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] From 35b2e8c0f8ced785a6ca1912e2aa45ebc304d5c3 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 10:10:19 -0700 Subject: [PATCH 02/15] Use two servers instead of CONFIG SET for the ordering test Starting each ordering in its own server drops the config backup and restore, and asserts the two orderings agree on which certificate belongs to which config. Signed-off-by: Madelyn Olson --- tests/unit/tls.tcl | 57 +++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index a18808617..daf2155e9 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -155,46 +155,31 @@ start_server {tags {"tls"}} { } test {TLS: INFO reports each certificate against the config that loaded it} { - # backup current certificates - set orig_server_crt [lindex [r config get tls-cert-file] 1] - set orig_server_key [lindex [r config get tls-key-file] 1] - set orig_server_alt_crt [lindex [r config get tls-alt-cert-file] 1] - set orig_server_alt_key [lindex [r config get tls-alt-key-file] 1] - - set valkey_crt [format "%s/tests/tls/valkey.crt" [pwd]] - set valkey_key [format "%s/tests/tls/valkey.key" [pwd]] - set valkey_ec_crt [format "%s/tests/tls/valkey-ec.crt" [pwd]] - set valkey_ec_key [format "%s/tests/tls/valkey-ec.key" [pwd]] - - # OpenSSL orders its certificate slots by key algorithm, not by the - # order the certificates were configured, so swapping the two configs - # must swap the two reported serials. - proc server_cert_serials {} { + 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]] + + # OpenSSL orders its certificate slots by key algorithm, not by the order + # the certificates were configured, so the two orderings must report the + # same certificate against the same config. + 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] - if {![regexp {tls_server_cert_serial:([^\r\n]+)} $info -> primary]} { - fail "INFO tls missing tls_server_cert_serial" - } - if {![regexp {tls_server_alt_cert_serial:([^\r\n]+)} $info -> alt]} { - fail "INFO tls missing tls_server_alt_cert_serial" - } - return [list $primary $alt] + assert {[regexp {tls_server_cert_serial:([^\r\n]+)} $info -> ec_first_primary]} + assert {[regexp {tls_server_alt_cert_serial:([^\r\n]+)} $info -> ec_first_alt]} } - try { - r CONFIG SET tls-cert-file $valkey_ec_crt tls-key-file $valkey_ec_key tls-alt-cert-file $valkey_crt tls-alt-key-file $valkey_key - lassign [server_cert_serials] ec_as_primary rsa_as_alt - assert {$ec_as_primary ne "none"} - assert {$rsa_as_alt ne "none"} - assert {$ec_as_primary ne $rsa_as_alt} - - r CONFIG SET tls-cert-file $valkey_crt tls-key-file $valkey_key tls-alt-cert-file $valkey_ec_crt tls-alt-key-file $valkey_ec_key - lassign [server_cert_serials] rsa_as_primary ec_as_alt - assert_equal $rsa_as_alt $rsa_as_primary - assert_equal $ec_as_primary $ec_as_alt - } finally { - #cleanup - r CONFIG SET tls-cert-file $orig_server_crt tls-key-file $orig_server_key tls-alt-cert-file $orig_server_alt_crt tls-alt-key-file $orig_server_alt_key + 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 {[regexp {tls_server_cert_serial:([^\r\n]+)} $info -> rsa_first_primary]} + assert {[regexp {tls_server_alt_cert_serial:([^\r\n]+)} $info -> rsa_first_alt]} } + + assert_equal $ec_first_primary $rsa_first_alt + assert_equal $ec_first_alt $rsa_first_primary + assert {$ec_first_primary ne $ec_first_alt} } test {TLS: alt cert and key files must be provided together} { From 566c3e622fc60bcf64c00ba7581a21b0b89d40d6 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 10:16:56 -0700 Subject: [PATCH 03/15] Simplify the certificate info refresh Drop the single-certificate special case: the general path already handles it, since a NULL alt_cert_file yields NID_undef and clears the alt fields, and the primary lookup finds its own slot either way. Pull the pubkey extraction into tlsCertKeyAlgorithm() so the slot scan and the file read share it, and use it for the primary and alternate algorithm comparison in createSSLContext too, which drops two EVP_PKEY locals and the four EVP_PKEY_free calls on the two exit paths. Signed-off-by: Madelyn Olson --- src/tls.c | 81 ++++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 49 deletions(-) diff --git a/src/tls.c b/src/tls.c index b7b241364..752b3b636 100644 --- a/src/tls.c +++ b/src/tls.c @@ -398,48 +398,45 @@ static int tlsUpdateCertInfoFromDir(const char *path, long long *expiry, sds *se return tlsStoreCertInfo(earliest_expiry, earliest_serial, cert_count, expiry, serial, count); } -/* Key algorithm of the leaf certificate in 'path', or NID_undef if it can't be read. */ +/* Key algorithm of a certificate as an EVP_PKEY base id, or NID_undef. */ +static int tlsCertKeyAlgorithm(X509 *cert) { + if (!cert) return NID_undef; + EVP_PKEY *pkey = X509_get_pubkey(cert); + if (!pkey) return NID_undef; + int alg = EVP_PKEY_base_id(pkey); + EVP_PKEY_free(pkey); + return alg; +} + +/* Same, for the leaf certificate in a PEM file. NID_undef if it can't be read. */ static int tlsCertFileKeyAlgorithm(const char *path) { if (!path) return NID_undef; FILE *fp = fopen(path, "r"); if (!fp) return NID_undef; X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL); fclose(fp); - if (!cert) return NID_undef; - int alg = NID_undef; - EVP_PKEY *pkey = X509_get_pubkey(cert); - if (pkey) { - alg = EVP_PKEY_base_id(pkey); - EVP_PKEY_free(pkey); - } + int alg = tlsCertKeyAlgorithm(cert); X509_free(cert); return alg; } -/* Point ctx's current certificate at the one whose key algorithm is 'alg'. +/* Report the certificate that 'cert_file' configured. * * OpenSSL keeps one certificate slot per key algorithm and orders the slots by * algorithm, so SSL_CERT_SET_FIRST selects the lowest-numbered algorithm rather - * than the certificate configured first. That makes the cursor position useless - * for telling tls-cert-file from tls-alt-cert-file. The two are required to use - * different key algorithms, so the algorithm identifies the slot. */ -static int tlsSelectCertByKeyAlgorithm(SSL_CTX *ctx, int alg) { - if (!ctx || alg == NID_undef) return C_ERR; - for (int op = SSL_CERT_SET_FIRST;; op = SSL_CERT_SET_NEXT) { - if (SSL_CTX_set_current_cert(ctx, op) != 1) return C_ERR; - EVP_PKEY *pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); - if (!pkey) continue; - int slot_alg = EVP_PKEY_base_id(pkey); - EVP_PKEY_free(pkey); - if (slot_alg == alg) return C_OK; - } -} - + * than the certificate configured first, and the cursor cannot tell tls-cert-file + * from tls-alt-cert-file. The two must use different key algorithms, so the + * algorithm identifies the slot. */ static void tlsRefreshCertInfoForFile(const char *cert_file, long long *expiry, sds *serial) { - if (tlsSelectCertByKeyAlgorithm(valkey_tls_ctx, tlsCertFileKeyAlgorithm(cert_file)) == C_ERR || - tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_ERR) { - tlsClearCertInfo(expiry, serial); + int alg = tlsCertFileKeyAlgorithm(cert_file); + if (alg != NID_undef) { + for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(valkey_tls_ctx, op) == 1; op = SSL_CERT_SET_NEXT) { + if (tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(valkey_tls_ctx)) != alg) continue; + if (tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_OK) return; + break; + } } + tlsClearCertInfo(expiry, serial); } static void tlsRefreshServerCertInfo(void) { @@ -448,21 +445,13 @@ static void tlsRefreshServerCertInfo(void) { tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial); return; } - - if (!server.tls_ctx_config.alt_cert_file) { - /* Only one certificate is loaded, so the cursor is unambiguous. */ - if (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); - } - tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial); - return; - } - tlsRefreshCertInfoForFile(server.tls_ctx_config.cert_file, &server.tls_server_cert_expire_time, &server.tls_server_cert_serial); tlsRefreshCertInfoForFile(server.tls_ctx_config.alt_cert_file, &server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial); + /* valkey_tls_ctx is also the client context when tls-client-cert-file is unset, + * and client side certificate selection does read the cursor, so don't leave it + * wherever the lookups landed. */ SSL_CTX_set_current_cert(valkey_tls_ctx, SSL_CERT_SET_FIRST); } @@ -631,8 +620,6 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco const char *alt_key_file_pass = client ? NULL : ctx_config->alt_key_file_pass; char errbuf[256]; SSL_CTX *ctx = NULL; - EVP_PKEY *primary_pkey = NULL; - EVP_PKEY *alt_pkey = NULL; ctx = SSL_CTX_new(SSLv23_method()); if (!ctx) goto error; @@ -673,8 +660,8 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco } if (alt_cert_file) { - primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); - if (!primary_pkey) { + int primary_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); + if (primary_alg == NID_undef) { serverLog(LL_WARNING, "Could not get public key from primary certificate"); goto error; } @@ -690,13 +677,13 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - alt_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); - if (!alt_pkey) { + int alt_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); + if (alt_alg == NID_undef) { serverLog(LL_WARNING, "Could not get public key from alternate certificate"); goto error; } - if (EVP_PKEY_base_id(primary_pkey) == EVP_PKEY_base_id(alt_pkey)) { + if (primary_alg == alt_alg) { serverLog(LL_WARNING, "Primary and alternate certificates must use different key algorithms"); goto error; } @@ -746,13 +733,9 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco } #endif - EVP_PKEY_free(primary_pkey); - EVP_PKEY_free(alt_pkey); return ctx; error: - EVP_PKEY_free(primary_pkey); - EVP_PKEY_free(alt_pkey); if (ctx) SSL_CTX_free(ctx); return NULL; } From cd6e611254f868549c42f2300517c2366f6b1174 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 10:43:35 -0700 Subject: [PATCH 04/15] Carry the certificate key algorithms with the context The refresh was reading the configured PEM file to learn its key algorithm, which made the INFO fields track the file on disk rather than the loaded certificate. tls-auto-reload-interval defaults to 0 and applyTLSPort calls tlsResetCertInfo() without rebuilding the context, so the two can diverge indefinitely: deleting tls-cert-file and setting tls-port reported tls_server_cert_serial:none for a certificate the server was still serving, and rotating tls-cert-file to the alternate's key algorithm pointed both fields at the alternate. Capture both algorithms in createSSLContext, where the certificate each file produced is unambiguous, and swap them in with the context. Also lands the cursor on tls-cert-file's slot rather than the algorithm-first slot. A TLS 1.2 peer picks its client certificate from the cursor, and valkey_tls_ctx doubles as the client context when tls-client-cert-file is unset, so a replica was presenting the alternate certificate. Signed-off-by: Madelyn Olson --- src/tls.c | 102 +++++++++++++++++++++++++-------------------- tests/unit/tls.tcl | 26 +++++++----- 2 files changed, 73 insertions(+), 55 deletions(-) diff --git a/src/tls.c b/src/tls.c index 752b3b636..6e4d3517c 100644 --- a/src/tls.c +++ b/src/tls.c @@ -186,6 +186,17 @@ static void tlsInit(void) { pending_list = listCreate(); } +/* Key algorithms of the certificates loaded into valkey_tls_ctx. OpenSSL orders + * its certificate slots by key algorithm and keeps no record of the order they + * were configured in, so this is what maps a slot back to the config that loaded + * it. Captured when the context is built and swapped in with it. */ +typedef struct { + int cert_alg; /* tls-cert-file */ + int alt_cert_alg; /* tls-alt-cert-file, NID_undef when unconfigured */ +} tlsCertAlgs; + +static tlsCertAlgs active_cert_algs = {NID_undef, NID_undef}; + static void tlsClearCertInfo(long long *expiry, sds *serial); static void tlsClearCACertInfo(void); static void tlsClearAllCertInfo(void); @@ -408,35 +419,25 @@ static int tlsCertKeyAlgorithm(X509 *cert) { return alg; } -/* Same, for the leaf certificate in a PEM file. NID_undef if it can't be read. */ -static int tlsCertFileKeyAlgorithm(const char *path) { - if (!path) return NID_undef; - FILE *fp = fopen(path, "r"); - if (!fp) return NID_undef; - X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL); - fclose(fp); - int alg = tlsCertKeyAlgorithm(cert); - X509_free(cert); - return alg; -} - -/* Report the certificate that 'cert_file' configured. +/* Point ctx's current certificate at the slot holding key algorithm 'alg'. * - * OpenSSL keeps one certificate slot per key algorithm and orders the slots by - * algorithm, so SSL_CERT_SET_FIRST selects the lowest-numbered algorithm rather - * than the certificate configured first, and the cursor cannot tell tls-cert-file + * SSL_CERT_SET_FIRST selects the lowest-numbered key algorithm rather than the + * certificate configured first, so the cursor on its own cannot tell tls-cert-file * from tls-alt-cert-file. The two must use different key algorithms, so the - * algorithm identifies the slot. */ -static void tlsRefreshCertInfoForFile(const char *cert_file, long long *expiry, sds *serial) { - int alg = tlsCertFileKeyAlgorithm(cert_file); - if (alg != NID_undef) { - for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(valkey_tls_ctx, op) == 1; op = SSL_CERT_SET_NEXT) { - if (tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(valkey_tls_ctx)) != alg) continue; - if (tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_OK) return; - break; - } + * algorithm recorded at load time identifies the slot. */ +static int tlsSelectCertByAlg(SSL_CTX *ctx, int alg) { + if (alg == NID_undef) return C_ERR; + for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(ctx, op) == 1; op = SSL_CERT_SET_NEXT) { + if (tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)) == alg) return C_OK; + } + return C_ERR; +} + +static void tlsRefreshCertInfoForAlg(int alg, long long *expiry, sds *serial) { + if (tlsSelectCertByAlg(valkey_tls_ctx, alg) == C_ERR || + tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_ERR) { + tlsClearCertInfo(expiry, serial); } - tlsClearCertInfo(expiry, serial); } static void tlsRefreshServerCertInfo(void) { @@ -445,14 +446,14 @@ static void tlsRefreshServerCertInfo(void) { tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial); return; } - tlsRefreshCertInfoForFile(server.tls_ctx_config.cert_file, &server.tls_server_cert_expire_time, - &server.tls_server_cert_serial); - tlsRefreshCertInfoForFile(server.tls_ctx_config.alt_cert_file, &server.tls_server_alt_cert_expire_time, - &server.tls_server_alt_cert_serial); + tlsRefreshCertInfoForAlg(active_cert_algs.cert_alg, &server.tls_server_cert_expire_time, + &server.tls_server_cert_serial); + tlsRefreshCertInfoForAlg(active_cert_algs.alt_cert_alg, &server.tls_server_alt_cert_expire_time, + &server.tls_server_alt_cert_serial); /* valkey_tls_ctx is also the client context when tls-client-cert-file is unset, - * and client side certificate selection does read the cursor, so don't leave it - * wherever the lookups landed. */ - SSL_CTX_set_current_cert(valkey_tls_ctx, SSL_CERT_SET_FIRST); + * and a TLS 1.2 client picks its certificate from the cursor, so leave it on the + * one tls-cert-file configured rather than wherever the lookups landed. */ + tlsSelectCertByAlg(valkey_tls_ctx, active_cert_algs.cert_alg); } static void tlsRefreshClientCertInfo(void) { @@ -610,7 +611,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, tlsCertAlgs *out_algs) { 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; @@ -659,13 +660,14 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - if (alt_cert_file) { - int primary_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); - if (primary_alg == NID_undef) { - serverLog(LL_WARNING, "Could not get public key from primary certificate"); - goto error; - } + int primary_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); + if (primary_alg == NID_undef) { + serverLog(LL_WARNING, "Could not get public key from %s certificate", client ? "client" : "primary"); + goto error; + } + if (out_algs) out_algs->cert_alg = primary_alg; + if (alt_cert_file) { if (SSL_CTX_use_certificate_chain_file(ctx, alt_cert_file) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); serverLog(LL_WARNING, "Failed to load certificate: %s: %s", alt_cert_file, errbuf); @@ -687,6 +689,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_algs) out_algs->alt_cert_alg = alt_alg; } if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) { @@ -745,7 +748,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, + tlsCertAlgs *out_algs) { char errbuf[256]; SSL_CTX *ctx = NULL; SSL_CTX *client_ctx = NULL; @@ -781,7 +787,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_algs); if (!ctx) goto error; if (ctx_config->session_caching) { @@ -862,7 +868,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; } @@ -902,6 +908,7 @@ typedef struct { SSL_CTX *ctx; SSL_CTX *client_ctx; tlsMaterialsMetadata metadata; + tlsCertAlgs cert_algs; } tlsPendingReload; /* Last known (active) TLS materials metadata */ @@ -1039,7 +1046,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) { + tlsCertAlgs cert_algs = {NID_undef, NID_undef}; + if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_algs) == C_ERR) { serverLog(LL_WARNING, "Background TLS reload failed"); return C_ERR; } @@ -1053,11 +1061,13 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { pending_reload.ctx = ctx; pending_reload.client_ctx = client_ctx; pending_reload.metadata = new_metadata; + pending_reload.cert_algs = cert_algs; 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) { + tlsCertAlgs cert_algs = {NID_undef, NID_undef}; + if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_algs) == C_ERR) { return C_ERR; } @@ -1065,6 +1075,7 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { SSL_CTX_free(valkey_tls_client_ctx); valkey_tls_ctx = ctx; valkey_tls_client_ctx = client_ctx; + active_cert_algs = cert_algs; captureMetadata(ctx_config, &active_metadata); tlsRefreshAllCertInfo(); } @@ -1117,6 +1128,7 @@ void tlsApplyPendingReload(void) { valkey_tls_client_ctx = local_pending.client_ctx; active_metadata = local_pending.metadata; + active_cert_algs = local_pending.cert_algs; SSL_CTX_free(old_ctx); SSL_CTX_free(old_client_ctx); diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index daf2155e9..947b18fd7 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -160,26 +160,32 @@ start_server {tags {"tls"}} { 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 two orderings must report the - # same certificate against the same config. + # 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 {[regexp {tls_server_cert_serial:([^\r\n]+)} $info -> ec_first_primary]} - assert {[regexp {tls_server_alt_cert_serial:([^\r\n]+)} $info -> ec_first_alt]} + 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 {[regexp {tls_server_cert_serial:([^\r\n]+)} $info -> rsa_first_primary]} - assert {[regexp {tls_server_alt_cert_serial:([^\r\n]+)} $info -> rsa_first_alt]} + assert_equal $rsa_serial [getInfoProperty $info tls_server_cert_serial] + assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial] } - - assert_equal $ec_first_primary $rsa_first_alt - assert_equal $ec_first_alt $rsa_first_primary - assert {$ec_first_primary ne $ec_first_alt} } test {TLS: alt cert and key files must be provided together} { From 954ae28e79a251f6cba6fc48c6b87b13fd37f2da Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 11:02:14 -0700 Subject: [PATCH 05/15] Cover the background reload path The background reload builds its context off the main thread and carries the key algorithms through pending_reload, but nothing exercised that. Deleting the swap at the apply site left the whole unit/tls suite green while a real reload reported the mapping inverted. Rewrite the two configured files so their key algorithms trade places and assert the mapping still follows the config after the reload. Signed-off-by: Madelyn Olson --- src/tls.c | 34 +++++++++++++++++----------------- tests/unit/tls.tcl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/tls.c b/src/tls.c index 6e4d3517c..c17b4235b 100644 --- a/src/tls.c +++ b/src/tls.c @@ -186,17 +186,6 @@ static void tlsInit(void) { pending_list = listCreate(); } -/* Key algorithms of the certificates loaded into valkey_tls_ctx. OpenSSL orders - * its certificate slots by key algorithm and keeps no record of the order they - * were configured in, so this is what maps a slot back to the config that loaded - * it. Captured when the context is built and swapped in with it. */ -typedef struct { - int cert_alg; /* tls-cert-file */ - int alt_cert_alg; /* tls-alt-cert-file, NID_undef when unconfigured */ -} tlsCertAlgs; - -static tlsCertAlgs active_cert_algs = {NID_undef, NID_undef}; - static void tlsClearCertInfo(long long *expiry, sds *serial); static void tlsClearCACertInfo(void); static void tlsClearAllCertInfo(void); @@ -409,6 +398,17 @@ static int tlsUpdateCertInfoFromDir(const char *path, long long *expiry, sds *se return tlsStoreCertInfo(earliest_expiry, earliest_serial, cert_count, expiry, serial, count); } +/* Key algorithms of the certificates loaded into valkey_tls_ctx. OpenSSL orders + * its certificate slots by key algorithm and keeps no record of the order they + * were configured in, so this is what maps a slot back to the config that loaded + * it. Captured when the context is built and swapped in with it. */ +typedef struct { + int cert_alg; /* tls-cert-file */ + int alt_cert_alg; /* tls-alt-cert-file, NID_undef when unconfigured */ +} tlsCertAlgs; + +static tlsCertAlgs active_cert_algs = {NID_undef, NID_undef}; + /* Key algorithm of a certificate as an EVP_PKEY base id, or NID_undef. */ static int tlsCertKeyAlgorithm(X509 *cert) { if (!cert) return NID_undef; @@ -425,16 +425,16 @@ static int tlsCertKeyAlgorithm(X509 *cert) { * certificate configured first, so the cursor on its own cannot tell tls-cert-file * from tls-alt-cert-file. The two must use different key algorithms, so the * algorithm recorded at load time identifies the slot. */ -static int tlsSelectCertByAlg(SSL_CTX *ctx, int alg) { +static int tlsSelectCertByAlg(int alg) { if (alg == NID_undef) return C_ERR; - for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(ctx, op) == 1; op = SSL_CERT_SET_NEXT) { - if (tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)) == alg) return C_OK; + for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(valkey_tls_ctx, op) == 1; op = SSL_CERT_SET_NEXT) { + if (tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(valkey_tls_ctx)) == alg) return C_OK; } return C_ERR; } static void tlsRefreshCertInfoForAlg(int alg, long long *expiry, sds *serial) { - if (tlsSelectCertByAlg(valkey_tls_ctx, alg) == C_ERR || + if (tlsSelectCertByAlg(alg) == C_ERR || tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_ERR) { tlsClearCertInfo(expiry, serial); } @@ -453,7 +453,7 @@ static void tlsRefreshServerCertInfo(void) { /* valkey_tls_ctx is also the client context when tls-client-cert-file is unset, * and a TLS 1.2 client picks its certificate from the cursor, so leave it on the * one tls-cert-file configured rather than wherever the lookups landed. */ - tlsSelectCertByAlg(valkey_tls_ctx, active_cert_algs.cert_alg); + tlsSelectCertByAlg(active_cert_algs.cert_alg); } static void tlsRefreshClientCertInfo(void) { @@ -662,7 +662,7 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco int primary_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); if (primary_alg == NID_undef) { - serverLog(LL_WARNING, "Could not get public key from %s certificate", client ? "client" : "primary"); + serverLog(LL_WARNING, "Could not get public key from %s certificate", client ? "client" : "server"); goto error; } if (out_algs) out_algs->cert_alg = primary_alg; diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 947b18fd7..ab2d02ee4 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -186,6 +186,40 @@ start_server {tags {"tls"}} { assert_equal $rsa_serial [getInfoProperty $info tls_server_cert_serial] assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial] } + + # The background reload swaps in a context built off the main thread, so it + # has to carry its own mapping. Rewrite the two files so the algorithms + # trade places, which inverts the mapping if it does not. + set tmp_crt "$ec_crt.reload.crt" + set tmp_key "$ec_crt.reload.key" + set tmp_alt_crt "$ec_crt.reload.alt.crt" + set tmp_alt_key "$ec_crt.reload.alt.key" + 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] + assert_equal $ec_serial [getInfoProperty $info tls_server_cert_serial] + assert_equal $rsa_serial [getInfoProperty $info tls_server_alt_cert_serial] + + 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 $rsa_serial [getInfoProperty $info tls_server_cert_serial] + assert_equal $ec_serial [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} { From dd6a3bbb3d9ecfa03cb06c708b38d4886be0ebfb Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 11:22:43 -0700 Subject: [PATCH 06/15] Skip the reload check in TLS module mode The auto-reload cron is only installed when TLS is built in (server.c:1805 is USE_OPENSSL == 1), so under --tls-module the wait for the reload log times out. The assertions before it hold either way, so guard only that block. Also drop the two NID_undef checks in createSSLContext. ssl_set_cert rejects a certificate whose public key does not resolve to an SSL_PKEY slot, so SSL_CTX_use_certificate_chain_file cannot return success and then leave EVP_PKEY_base_id undefined. Name the reload fixtures for their role rather than hanging all four off the EC certificate's path. Signed-off-by: Madelyn Olson --- src/tls.c | 14 ++++------- tests/unit/tls.tcl | 59 ++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/tls.c b/src/tls.c index c17b4235b..bcb4fc472 100644 --- a/src/tls.c +++ b/src/tls.c @@ -419,7 +419,7 @@ static int tlsCertKeyAlgorithm(X509 *cert) { return alg; } -/* Point ctx's current certificate at the slot holding key algorithm 'alg'. +/* Point valkey_tls_ctx's current certificate at the slot holding key algorithm 'alg'. * * SSL_CERT_SET_FIRST selects the lowest-numbered key algorithm rather than the * certificate configured first, so the cursor on its own cannot tell tls-cert-file @@ -660,11 +660,10 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } + /* SSL_CTX_use_certificate_chain_file only succeeds for a certificate whose public + * key resolves to one of OpenSSL's key algorithm slots, so this cannot be + * NID_undef here. */ int primary_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); - if (primary_alg == NID_undef) { - serverLog(LL_WARNING, "Could not get public key from %s certificate", client ? "client" : "server"); - goto error; - } if (out_algs) out_algs->cert_alg = primary_alg; if (alt_cert_file) { @@ -680,11 +679,6 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco } int alt_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); - if (alt_alg == NID_undef) { - serverLog(LL_WARNING, "Could not get public key from alternate certificate"); - goto error; - } - if (primary_alg == alt_alg) { serverLog(LL_WARNING, "Primary and alternate certificates must use different key algorithms"); goto error; diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index ab2d02ee4..530c3a6cd 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -189,36 +189,39 @@ start_server {tags {"tls"}} { # The background reload swaps in a context built off the main thread, so it # has to carry its own mapping. Rewrite the two files so the algorithms - # trade places, which inverts the mapping if it does not. - set tmp_crt "$ec_crt.reload.crt" - set tmp_key "$ec_crt.reload.key" - set tmp_alt_crt "$ec_crt.reload.alt.crt" - set tmp_alt_key "$ec_crt.reload.alt.key" - 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 + # trade places, which inverts the mapping if it does not. The reload cron + # only exists when TLS is built in, not as a module (server.c:1805). + if {!$::tls_module} { + 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] - assert_equal $ec_serial [getInfoProperty $info tls_server_cert_serial] - assert_equal $rsa_serial [getInfoProperty $info tls_server_alt_cert_serial] - - 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 $rsa_serial [getInfoProperty $info tls_server_cert_serial] - assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial] + 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] + assert_equal $ec_serial [getInfoProperty $info tls_server_cert_serial] + assert_equal $rsa_serial [getInfoProperty $info tls_server_alt_cert_serial] + + 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 $rsa_serial [getInfoProperty $info tls_server_cert_serial] + assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial] + } + } finally { + file delete -force $tmp_crt $tmp_key $tmp_alt_crt $tmp_alt_key } - } finally { - file delete -force $tmp_crt $tmp_key $tmp_alt_crt $tmp_alt_key } } From 945aefb33b8b8b85e9ad5ea667e70a9e62ee9045 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 11:48:18 -0700 Subject: [PATCH 07/15] Capture the certificate info at load time, not the key algorithm EVP_PKEY_base_id() returns NID_undef for a provider-only algorithm, so keying the slot lookup on it reported tls_server_cert_serial:none for an ML-DSA certificate that OpenSSL 3.5 loads and serves happily. ssl_cert_lookup_by_pkey matches by name via EVP_PKEY_is_a and has a second loop over provider registered sigalgs, so the certificate resolves to a slot fine while the legacy pkey->type behind EVP_PKEY_base_id does not. PQC is the case this feature exists for, so that is the wrong key. Capture each certificate's serial and expiry in createSSLContext, where the file it came from is still known, and carry those with the context. Nothing has to identify a slot afterwards, so the lookup, the algorithm helper and the createSSLContext algorithm changes all go away. This drops the cursor repositioning. Landing the cursor on tls-cert-file's slot needs the same slot identification, and the TLS 1.2 client certificate behaviour it addressed is pre-existing rather than something this change introduces. Also splits the reload coverage into its own test so it can use the file's existing module-mode skip instead of a silent guard. Signed-off-by: Madelyn Olson --- src/tls.c | 124 +++++++++++++++++++++++---------------------- tests/unit/tls.tcl | 76 +++++++++++++++------------ 2 files changed, 106 insertions(+), 94 deletions(-) diff --git a/src/tls.c b/src/tls.c index bcb4fc472..e4c38676d 100644 --- a/src/tls.c +++ b/src/tls.c @@ -398,46 +398,35 @@ static int tlsUpdateCertInfoFromDir(const char *path, long long *expiry, sds *se return tlsStoreCertInfo(earliest_expiry, earliest_serial, cert_count, expiry, serial, count); } -/* Key algorithms of the certificates loaded into valkey_tls_ctx. OpenSSL orders - * its certificate slots by key algorithm and keeps no record of the order they - * were configured in, so this is what maps a slot back to the config that loaded - * it. Captured when the context is built and swapped in with it. */ +/* Serial and expiry of the two server certificates, captured while the context is + * being built, where the file each one came from is still known. + * + * OpenSSL keeps one certificate slot per key algorithm, orders the slots by + * algorithm, and records nothing about the order they were configured in, so which + * slot came from tls-cert-file cannot be recovered from a finished context. Neither + * can the key algorithm stand in for the slot: EVP_PKEY_base_id() returns NID_undef + * for a provider-only algorithm such as ML-DSA, which OpenSSL 3.5 loads happily. */ typedef struct { - int cert_alg; /* tls-cert-file */ - int alt_cert_alg; /* tls-alt-cert-file, NID_undef when unconfigured */ -} tlsCertAlgs; + long long cert_expiry; + sds cert_serial; + long long alt_cert_expiry; + sds alt_cert_serial; +} tlsServerCertInfo; -static tlsCertAlgs active_cert_algs = {NID_undef, NID_undef}; +static tlsServerCertInfo active_cert_info = {0}; -/* Key algorithm of a certificate as an EVP_PKEY base id, or NID_undef. */ -static int tlsCertKeyAlgorithm(X509 *cert) { - if (!cert) return NID_undef; - EVP_PKEY *pkey = X509_get_pubkey(cert); - if (!pkey) return NID_undef; - int alg = EVP_PKEY_base_id(pkey); - EVP_PKEY_free(pkey); - return alg; +static void tlsFreeServerCertInfo(tlsServerCertInfo *info) { + tlsClearCertSerial(&info->cert_serial); + tlsClearCertSerial(&info->alt_cert_serial); + info->cert_expiry = 0; + info->alt_cert_expiry = 0; } -/* Point valkey_tls_ctx's current certificate at the slot holding key algorithm 'alg'. - * - * SSL_CERT_SET_FIRST selects the lowest-numbered key algorithm rather than the - * certificate configured first, so the cursor on its own cannot tell tls-cert-file - * from tls-alt-cert-file. The two must use different key algorithms, so the - * algorithm recorded at load time identifies the slot. */ -static int tlsSelectCertByAlg(int alg) { - if (alg == NID_undef) return C_ERR; - for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(valkey_tls_ctx, op) == 1; op = SSL_CERT_SET_NEXT) { - if (tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(valkey_tls_ctx)) == alg) return C_OK; - } - return C_ERR; -} - -static void tlsRefreshCertInfoForAlg(int alg, long long *expiry, sds *serial) { - if (tlsSelectCertByAlg(alg) == C_ERR || - tlsUpdateCertInfoFromCtx(valkey_tls_ctx, expiry, serial) == C_ERR) { - tlsClearCertInfo(expiry, serial); - } +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) { @@ -446,14 +435,10 @@ static void tlsRefreshServerCertInfo(void) { tlsClearCertInfo(&server.tls_server_alt_cert_expire_time, &server.tls_server_alt_cert_serial); return; } - tlsRefreshCertInfoForAlg(active_cert_algs.cert_alg, &server.tls_server_cert_expire_time, - &server.tls_server_cert_serial); - tlsRefreshCertInfoForAlg(active_cert_algs.alt_cert_alg, &server.tls_server_alt_cert_expire_time, - &server.tls_server_alt_cert_serial); - /* valkey_tls_ctx is also the client context when tls-client-cert-file is unset, - * and a TLS 1.2 client picks its certificate from the cursor, so leave it on the - * one tls-cert-file configured rather than wherever the lookups landed. */ - tlsSelectCertByAlg(active_cert_algs.cert_alg); + 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) { @@ -611,7 +596,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, tlsCertAlgs *out_algs) { +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; @@ -660,13 +645,17 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - /* SSL_CTX_use_certificate_chain_file only succeeds for a certificate whose public - * key resolves to one of OpenSSL's key algorithm slots, so this cannot be - * NID_undef here. */ - int primary_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); - if (out_algs) out_algs->cert_alg = primary_alg; + if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->cert_expiry, &out_info->cert_serial); if (alt_cert_file) { + EVP_PKEY *primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); + if (!primary_pkey) { + serverLog(LL_WARNING, "Could not get public key from primary certificate"); + goto error; + } + int primary_alg = EVP_PKEY_base_id(primary_pkey); + EVP_PKEY_free(primary_pkey); + if (SSL_CTX_use_certificate_chain_file(ctx, alt_cert_file) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); serverLog(LL_WARNING, "Failed to load certificate: %s: %s", alt_cert_file, errbuf); @@ -678,12 +667,19 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - int alt_alg = tlsCertKeyAlgorithm(SSL_CTX_get0_certificate(ctx)); + EVP_PKEY *alt_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); + if (!alt_pkey) { + serverLog(LL_WARNING, "Could not get public key from alternate certificate"); + goto error; + } + int alt_alg = EVP_PKEY_base_id(alt_pkey); + EVP_PKEY_free(alt_pkey); + if (primary_alg == alt_alg) { serverLog(LL_WARNING, "Primary and alternate certificates must use different key algorithms"); goto error; } - if (out_algs) out_algs->alt_cert_alg = alt_alg; + 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) { @@ -745,7 +741,7 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco static int tlsCreateContexts(serverTLSContextConfig *ctx_config, SSL_CTX **out_ctx, SSL_CTX **out_client_ctx, - tlsCertAlgs *out_algs) { + tlsServerCertInfo *out_info) { char errbuf[256]; SSL_CTX *ctx = NULL; SSL_CTX *client_ctx = NULL; @@ -781,7 +777,7 @@ static int tlsCreateContexts(serverTLSContextConfig *ctx_config, if (protocols == -1) goto error; /* Create server side/general context */ - ctx = createSSLContext(ctx_config, protocols, 0, out_algs); + ctx = createSSLContext(ctx_config, protocols, 0, out_info); if (!ctx) goto error; if (ctx_config->session_caching) { @@ -902,7 +898,7 @@ typedef struct { SSL_CTX *ctx; SSL_CTX *client_ctx; tlsMaterialsMetadata metadata; - tlsCertAlgs cert_algs; + tlsServerCertInfo cert_info; } tlsPendingReload; /* Last known (active) TLS materials metadata */ @@ -1040,9 +1036,10 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { } serverLog(LL_NOTICE, "TLS materials changed, reloading in background"); - tlsCertAlgs cert_algs = {NID_undef, NID_undef}; - if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_algs) == C_ERR) { + tlsServerCertInfo cert_info = {0}; + if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_info) == C_ERR) { serverLog(LL_WARNING, "Background TLS reload failed"); + tlsFreeServerCertInfo(&cert_info); return C_ERR; } @@ -1050,18 +1047,20 @@ 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); + tlsFreeServerCertInfo(&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_algs = cert_algs; + pending_reload.cert_info = cert_info; pthread_mutex_unlock(&pending_reload_mutex); serverLog(LL_DEBUG, "Background TLS reload parsed TLS materials successfully"); } else { - tlsCertAlgs cert_algs = {NID_undef, NID_undef}; - if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_algs) == C_ERR) { + tlsServerCertInfo cert_info = {0}; + if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_info) == C_ERR) { + tlsFreeServerCertInfo(&cert_info); return C_ERR; } @@ -1069,7 +1068,8 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { SSL_CTX_free(valkey_tls_client_ctx); valkey_tls_ctx = ctx; valkey_tls_client_ctx = client_ctx; - active_cert_algs = cert_algs; + tlsFreeServerCertInfo(&active_cert_info); + active_cert_info = cert_info; captureMetadata(ctx_config, &active_metadata); tlsRefreshAllCertInfo(); } @@ -1105,6 +1105,7 @@ void tlsApplyPendingReload(void) { if (!metadataChanged(&active_metadata, &pending_reload.metadata)) { SSL_CTX_free(pending_reload.ctx); SSL_CTX_free(pending_reload.client_ctx); + tlsFreeServerCertInfo(&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"); @@ -1122,7 +1123,8 @@ void tlsApplyPendingReload(void) { valkey_tls_client_ctx = local_pending.client_ctx; active_metadata = local_pending.metadata; - active_cert_algs = local_pending.cert_algs; + tlsFreeServerCertInfo(&active_cert_info); + active_cert_info = local_pending.cert_info; SSL_CTX_free(old_ctx); SSL_CTX_free(old_client_ctx); diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 530c3a6cd..dd2d66152 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -186,42 +186,52 @@ start_server {tags {"tls"}} { assert_equal $rsa_serial [getInfoProperty $info tls_server_cert_serial] assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial] } + } - # The background reload swaps in a context built off the main thread, so it - # has to carry its own mapping. Rewrite the two files so the algorithms - # trade places, which inverts the mapping if it does not. The reload cron - # only exists when TLS is built in, not as a module (server.c:1805). - if {!$::tls_module} { - 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 + 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" + } - 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] - assert_equal $ec_serial [getInfoProperty $info tls_server_cert_serial] - assert_equal $rsa_serial [getInfoProperty $info tls_server_alt_cert_serial] - - 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 $rsa_serial [getInfoProperty $info tls_server_cert_serial] - assert_equal $ec_serial [getInfoProperty $info tls_server_alt_cert_serial] - } - } finally { - file delete -force $tmp_crt $tmp_key $tmp_alt_crt $tmp_alt_key + 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 } } From f318a5ed0325bc2ea6d0d20d570aaa4778e4d046 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 12:09:39 -0700 Subject: [PATCH 08/15] Leave the certificate cursor where it was before Loading the alternate certificate moves the context's current certificate to its slot, and nothing moved it back once the refresh stopped walking it. An outgoing TLS 1.2 connection picks its client certificate from there, so a replica with no tls-client-cert-file started presenting the alternate: tls-cert-file / tls-alt-cert-file 0e36225 before this commit RSA / EC Generic-cert EC-cert EC / RSA Generic-cert Generic-cert Reset it to the lowest slot at the end of createSSLContext, which is where 0e36225 left it after every refresh. Signed-off-by: Madelyn Olson --- src/tls.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tls.c b/src/tls.c index e4c38676d..81e4e0f31 100644 --- a/src/tls.c +++ b/src/tls.c @@ -645,6 +645,9 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } + /* INFO reporting only. isCertValid() above already parsed this certificate, so a + * failure here is not reachable, and it would leave the field as none rather than + * refuse the configuration. */ if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->cert_expiry, &out_info->cert_serial); if (alt_cert_file) { @@ -726,6 +729,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); return ctx; error: From d6ca802b9a76c882d2463c846caba5f051bbb431 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 12:28:11 -0700 Subject: [PATCH 09/15] Cover the certificate cursor, and drop a wrong claim Deleting the cursor reset in createSSLContext left the whole suite green, which is how it regressed once already. Authenticate the replica by the CN of the certificate it presents and assert it is the one tls-cert-file configured. The comment on the captured certificate info claimed a failure was unreachable. isCertValid() accepts an unparseable notAfter on OpenSSL 3.x, because X509_cmp_time returns 0 on a format error and 0 is neither > 0 nor < 0, so tlsUpdateCertInfoFromCtx can fail. The stated outcome, a field left as none, is right. Signed-off-by: Madelyn Olson --- src/tls.c | 5 ++--- tests/unit/tls.tcl | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/tls.c b/src/tls.c index 81e4e0f31..1f68340c8 100644 --- a/src/tls.c +++ b/src/tls.c @@ -645,9 +645,8 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - /* INFO reporting only. isCertValid() above already parsed this certificate, so a - * failure here is not reachable, and it would leave the field as none rather than - * refuse the configuration. */ + /* 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) { diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index dd2d66152..716e7a445 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -235,6 +235,48 @@ start_server {tags {"tls"}} { } } + test {TLS: outgoing connections present the tls-cert-file certificate} { + set primary [srv 0 client] + set primary_host [srv 0 host] + set primary_port [srv 0 port] + + 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]] + + # Authenticate the replica by the CN of the certificate it presents, and give + # both candidate CNs an account so a wrong choice reports as itself rather + # than just failing to connect. valkey.crt is CN=Generic-cert and + # valkey-ec.crt is CN=EC-cert. + r ACL SETUSER {Generic-cert} on nopass ~* &* +@all + r ACL SETUSER {EC-cert} on nopass ~* &* +@all + r CONFIG SET tls-auth-clients-user CN + # The certificate cursor only reaches client side selection below TLS 1.3. + r CONFIG SET tls-protocols TLSv1.2 + + try { + 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 \ + tls-protocols TLSv1.2] \ + omit [list tls-client-cert-file tls-client-key-file]] { + r replicaof $primary_host $primary_port + wait_for_condition 50 100 { + [string match {*master_link_status:up*} [r info replication]] + } else { + fail "replica could not sync using tls-cert-file as its client cert" + } + + assert {[regexp {user=(\S+)} [$primary client list type replica] -> replica_user]} + assert_equal "Generic-cert" $replica_user + } + } finally { + r CONFIG SET tls-auth-clients-user off + r CONFIG SET tls-protocols "" + r ACL DELUSER {Generic-cert} {EC-cert} + } + } + 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] From 5fdedca5f0a73ccdcb5401d172f916816559e653 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 12:45:31 -0700 Subject: [PATCH 10/15] Name the cursor test for what it proves A TLS 1.3 client picks its certificate from the peer's signature algorithm preference and never reads the cursor, so tls-cert-file does not decide this at the default protocol version. The test pins TLS 1.2 and the name now says so. The primary side tls-protocols pin was redundant: the replica caps the handshake with its own. Signed-off-by: Madelyn Olson --- tests/unit/tls.tcl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 716e7a445..5913a195c 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -235,7 +235,7 @@ start_server {tags {"tls"}} { } } - test {TLS: outgoing connections present the tls-cert-file certificate} { + test {TLS: a TLS 1.2 outgoing connection presents the tls-cert-file certificate} { set primary [srv 0 client] set primary_host [srv 0 host] set primary_port [srv 0 port] @@ -252,10 +252,10 @@ start_server {tags {"tls"}} { r ACL SETUSER {Generic-cert} on nopass ~* &* +@all r ACL SETUSER {EC-cert} on nopass ~* &* +@all r CONFIG SET tls-auth-clients-user CN - # The certificate cursor only reaches client side selection below TLS 1.3. - r CONFIG SET tls-protocols TLSv1.2 try { + # A TLS 1.3 client picks by the peer's signature algorithm preference and + # ignores the certificate cursor, so pin the replica to 1.2. 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 \ tls-protocols TLSv1.2] \ @@ -272,7 +272,6 @@ start_server {tags {"tls"}} { } } finally { r CONFIG SET tls-auth-clients-user off - r CONFIG SET tls-protocols "" r ACL DELUSER {Generic-cert} {EC-cert} } } From 9ac0f81058d009a5eeaff03f6152c37900b84ff7 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 13:08:02 -0700 Subject: [PATCH 11/15] Capture the certificate its key activated The capture ran before any private key loaded, so a certificate whose OpenSSL slot never receives a key still got a serial published. With tls-cert-file an ECDSA certificate and tls-key-file the RSA key, both keys land in the RSA slot and only the RSA certificate is servable, yet INFO named the ECDSA one. Load each certificate with its key and capture after the key, so the recorded certificate is the one the server can present. That config now reports none for the primary, where 0e36225 reported the alternate's certificate. Also renames tlsFreeServerCertInfo to match the tlsClear* helpers around it, and frees the captured info at tlsCreateContexts' error label alongside the two contexts instead of at both call sites. Signed-off-by: Madelyn Olson --- src/tls.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/tls.c b/src/tls.c index 1f68340c8..bf66ccc86 100644 --- a/src/tls.c +++ b/src/tls.c @@ -415,7 +415,7 @@ typedef struct { static tlsServerCertInfo active_cert_info = {0}; -static void tlsFreeServerCertInfo(tlsServerCertInfo *info) { +static void tlsClearServerCertInfo(tlsServerCertInfo *info) { tlsClearCertSerial(&info->cert_serial); tlsClearCertSerial(&info->alt_cert_serial); info->cert_expiry = 0; @@ -645,19 +645,29 @@ 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); - + int primary_alg = NID_undef; if (alt_cert_file) { EVP_PKEY *primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); if (!primary_pkey) { serverLog(LL_WARNING, "Could not get public key from primary certificate"); goto error; } - int primary_alg = EVP_PKEY_base_id(primary_pkey); + primary_alg = EVP_PKEY_base_id(primary_pkey); EVP_PKEY_free(primary_pkey); + } + + if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) { + ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); + serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf); + goto error; + } + + /* Record the certificate the key just activated, which is the one the server can + * actually present. 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) { if (SSL_CTX_use_certificate_chain_file(ctx, alt_cert_file) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); serverLog(LL_WARNING, "Failed to load certificate: %s: %s", alt_cert_file, errbuf); @@ -681,21 +691,14 @@ 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) { - ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); - serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf); - goto error; - } - if (alt_key_file) { SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *)alt_key_file_pass); if (SSL_CTX_use_PrivateKey_file(ctx, alt_key_file, SSL_FILETYPE_PEM) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); serverLog(LL_WARNING, "Failed to load private key: %s: %s", alt_key_file, errbuf); goto error; } + if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->alt_cert_expiry, &out_info->alt_cert_serial); } if (ctx_config->ca_cert_file || ctx_config->ca_cert_dir) { @@ -873,6 +876,7 @@ static int tlsCreateContexts(serverTLSContextConfig *ctx_config, 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; @@ -1045,7 +1049,6 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { tlsServerCertInfo cert_info = {0}; if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_info) == C_ERR) { serverLog(LL_WARNING, "Background TLS reload failed"); - tlsFreeServerCertInfo(&cert_info); return C_ERR; } @@ -1053,7 +1056,7 @@ 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); - tlsFreeServerCertInfo(&pending_reload.cert_info); + tlsClearServerCertInfo(&pending_reload.cert_info); serverLog(LL_DEBUG, "Replacing previous pending TLS reload"); } pending_reload.ctx = ctx; @@ -1066,7 +1069,6 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { } else { tlsServerCertInfo cert_info = {0}; if (tlsCreateContexts(ctx_config, &ctx, &client_ctx, &cert_info) == C_ERR) { - tlsFreeServerCertInfo(&cert_info); return C_ERR; } @@ -1074,7 +1076,7 @@ static int tlsConfigure(void *priv, int reconfigure, bool background) { SSL_CTX_free(valkey_tls_client_ctx); valkey_tls_ctx = ctx; valkey_tls_client_ctx = client_ctx; - tlsFreeServerCertInfo(&active_cert_info); + tlsClearServerCertInfo(&active_cert_info); active_cert_info = cert_info; captureMetadata(ctx_config, &active_metadata); tlsRefreshAllCertInfo(); @@ -1111,7 +1113,7 @@ void tlsApplyPendingReload(void) { if (!metadataChanged(&active_metadata, &pending_reload.metadata)) { SSL_CTX_free(pending_reload.ctx); SSL_CTX_free(pending_reload.client_ctx); - tlsFreeServerCertInfo(&pending_reload.cert_info); + 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"); @@ -1129,7 +1131,7 @@ void tlsApplyPendingReload(void) { valkey_tls_client_ctx = local_pending.client_ctx; active_metadata = local_pending.metadata; - tlsFreeServerCertInfo(&active_cert_info); + tlsClearServerCertInfo(&active_cert_info); active_cert_info = local_pending.cert_info; SSL_CTX_free(old_ctx); From 6aaffdc8930de4fce9679e67f992e559ef2f297c Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 13:32:44 -0700 Subject: [PATCH 12/15] Reject a certificate that has no matching private key Moving the key load ahead of the alternate certificate load made a mismatched primary pair loadable that 0e36225 refused, because ssl_set_cert unloads the key already sitting in a slot when the certificate handed to it does not match. Put the load order back and check the outcome instead: count the slots holding both a certificate and a key, and require one per configured certificate. OpenSSL routes a key to the slot for its own algorithm and only checks it against whatever certificate is already there, so a mismatched pair can leave a certificate with no key rather than failing. That also closes two shapes 0e36225 accepted: an ECDSA tls-cert-file with the RSA key alongside an RSA tls-alt-cert-file, which it reported as the alternate's serial, and the same mismatch with no alternate at all. The capture goes back to being per certificate, which is unambiguous again now that every accepted certificate is guaranteed a key. Signed-off-by: Madelyn Olson --- src/tls.c | 43 +++++++++++++++++++++++++++---------------- tests/unit/tls.tcl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/tls.c b/src/tls.c index bf66ccc86..201139db7 100644 --- a/src/tls.c +++ b/src/tls.c @@ -645,29 +645,19 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - int primary_alg = NID_undef; + /* INFO reporting only. A failure leaves the field as none rather than refusing the + * configuration. The pairing check below guarantees this certificate has a key. */ + if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->cert_expiry, &out_info->cert_serial); + if (alt_cert_file) { EVP_PKEY *primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); if (!primary_pkey) { serverLog(LL_WARNING, "Could not get public key from primary certificate"); goto error; } - primary_alg = EVP_PKEY_base_id(primary_pkey); + int primary_alg = EVP_PKEY_base_id(primary_pkey); EVP_PKEY_free(primary_pkey); - } - - if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) { - ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); - serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf); - goto error; - } - /* Record the certificate the key just activated, which is the one the server can - * actually present. 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) { if (SSL_CTX_use_certificate_chain_file(ctx, alt_cert_file) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); serverLog(LL_WARNING, "Failed to load certificate: %s: %s", alt_cert_file, errbuf); @@ -691,14 +681,35 @@ 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) { + ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); + serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf); + goto error; + } + if (alt_key_file) { SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *)alt_key_file_pass); if (SSL_CTX_use_PrivateKey_file(ctx, alt_key_file, SSL_FILETYPE_PEM) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); serverLog(LL_WARNING, "Failed to load private key: %s: %s", alt_key_file, errbuf); goto error; } - if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->alt_cert_expiry, &out_info->alt_cert_serial); + } + + /* Every configured certificate must have ended up with a matching private key. + * OpenSSL routes a key to the slot for its own key algorithm and only checks it + * against whatever certificate is already in that slot, so a mismatched pair can + * leave a certificate loaded with no key rather than failing outright. Such a + * certificate cannot be presented, and only the slots holding both are usable. */ + int usable_certs = 0; + for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(ctx, op) == 1; op = SSL_CERT_SET_NEXT) { + usable_certs++; + } + if (usable_certs != (alt_cert_file ? 2 : 1)) { + serverLog(LL_WARNING, "%s TLS certificate does not match its private key.", client ? "Client" : "Server"); + goto error; } if (ctx_config->ca_cert_file || ctx_config->ca_cert_dir) { diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 5913a195c..83ce40453 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -276,6 +276,36 @@ start_server {tags {"tls"}} { } } + test {TLS: a certificate without a matching key is rejected} { + set orig_crt [lindex [r config get tls-cert-file] 1] + set orig_key [lindex [r config get tls-key-file] 1] + set orig_alt_crt [lindex [r config get tls-alt-cert-file] 1] + set orig_alt_key [lindex [r config get tls-alt-key-file] 1] + + 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]] + + try { + # OpenSSL puts a key in the slot for its own algorithm and only checks it + # against the certificate already there, so a mismatched pair can leave a + # certificate loaded with no key instead of failing. + catch {r CONFIG SET tls-cert-file $ec_crt tls-key-file $rsa_key \ + tls-alt-cert-file $rsa_crt tls-alt-key-file $rsa_key} e + assert_match {*Unable to update TLS configuration*} $e + verify_log_message 0 "*TLS certificate does not match its private key*" 0 + + # Same the other way round: the alternate certificate left without a key. + catch {r CONFIG SET tls-cert-file $ec_crt tls-key-file $ec_key \ + tls-alt-cert-file $rsa_crt tls-alt-key-file $ec_key} e + assert_match {*Unable to update TLS configuration*} $e + } finally { + r CONFIG SET tls-cert-file $orig_crt tls-key-file $orig_key \ + tls-alt-cert-file $orig_alt_crt tls-alt-key-file $orig_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] From cdb954d5a37f34f83fd2a58c0262fd66e2cf89c2 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 13:55:13 -0700 Subject: [PATCH 13/15] Stop naming a pair the pairing check cannot identify Counting usable slots says a certificate is unpaired, not which one, so with a good tls-cert-file and tls-key-file and a mismatched alternate the old message sent the operator to the two files that were fine. Also pins each case in the test to its own log line rather than letting the pattern match an earlier one, and covers the client context, whose branch of the message had no test. Signed-off-by: Madelyn Olson --- src/tls.c | 5 +++-- tests/unit/tls.tcl | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/tls.c b/src/tls.c index 201139db7..1d6d8a49e 100644 --- a/src/tls.c +++ b/src/tls.c @@ -689,7 +689,7 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf); goto error; } - if (alt_key_file) { + if (alt_cert_file) { SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *)alt_key_file_pass); if (SSL_CTX_use_PrivateKey_file(ctx, alt_key_file, SSL_FILETYPE_PEM) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); @@ -708,7 +708,8 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco usable_certs++; } if (usable_certs != (alt_cert_file ? 2 : 1)) { - serverLog(LL_WARNING, "%s TLS certificate does not match its private key.", client ? "Client" : "Server"); + serverLog(LL_WARNING, "A configured %s TLS certificate has no matching private key.", + client ? "client" : "server"); goto error; } diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 83ce40453..1b0af4f6a 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -281,6 +281,8 @@ start_server {tags {"tls"}} { set orig_key [lindex [r config get tls-key-file] 1] set orig_alt_crt [lindex [r config get tls-alt-cert-file] 1] set orig_alt_key [lindex [r config get tls-alt-key-file] 1] + set orig_client_crt [lindex [r config get tls-client-cert-file] 1] + set orig_client_key [lindex [r config get tls-client-key-file] 1] set rsa_crt [format "%s/tests/tls/valkey.crt" [pwd]] set rsa_key [format "%s/tests/tls/valkey.key" [pwd]] @@ -291,18 +293,29 @@ start_server {tags {"tls"}} { # OpenSSL puts a key in the slot for its own algorithm and only checks it # against the certificate already there, so a mismatched pair can leave a # certificate loaded with no key instead of failing. + set from [count_log_lines 0] catch {r CONFIG SET tls-cert-file $ec_crt tls-key-file $rsa_key \ tls-alt-cert-file $rsa_crt tls-alt-key-file $rsa_key} e assert_match {*Unable to update TLS configuration*} $e - verify_log_message 0 "*TLS certificate does not match its private key*" 0 + verify_log_message 0 "*server TLS certificate has no matching private key*" $from - # Same the other way round: the alternate certificate left without a key. + # Same the other way round: the alternate certificate left without a key, + # while tls-cert-file and tls-key-file are a good pair. + set from [count_log_lines 0] catch {r CONFIG SET tls-cert-file $ec_crt tls-key-file $ec_key \ tls-alt-cert-file $rsa_crt tls-alt-key-file $ec_key} e assert_match {*Unable to update TLS configuration*} $e + verify_log_message 0 "*server TLS certificate has no matching private key*" $from + + # The client context goes through the same check. + set from [count_log_lines 0] + catch {r CONFIG SET tls-client-cert-file $ec_crt tls-client-key-file $rsa_key} e + assert_match {*Unable to update TLS configuration*} $e + verify_log_message 0 "*client TLS certificate has no matching private key*" $from } finally { r CONFIG SET tls-cert-file $orig_crt tls-key-file $orig_key \ - tls-alt-cert-file $orig_alt_crt tls-alt-key-file $orig_alt_key + tls-alt-cert-file $orig_alt_crt tls-alt-key-file $orig_alt_key \ + tls-client-cert-file $orig_client_crt tls-client-key-file $orig_client_key } } From a8e19d6b06e44439526cb389deb10e170307013a Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 14:31:53 -0700 Subject: [PATCH 14/15] Drop refactor churn from the diff The EVP_PKEY hoist was left over from a shared helper that no longer exists, and the alt_key_file to alt_cert_file swap was a style change to base's code with no behavior difference. Both are hunks a backport has to carry for nothing. Signed-off-by: Madelyn Olson --- src/tls.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/tls.c b/src/tls.c index 1d6d8a49e..0d6ef8c68 100644 --- a/src/tls.c +++ b/src/tls.c @@ -398,14 +398,11 @@ 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 - * being built, where the file each one came from is still known. - * - * OpenSSL keeps one certificate slot per key algorithm, orders the slots by - * algorithm, and records nothing about the order they were configured in, so which - * slot came from tls-cert-file cannot be recovered from a finished context. Neither - * can the key algorithm stand in for the slot: EVP_PKEY_base_id() returns NID_undef - * for a provider-only algorithm such as ML-DSA, which OpenSSL 3.5 loads happily. */ +/* 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; @@ -606,6 +603,8 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco const char *alt_key_file_pass = client ? NULL : ctx_config->alt_key_file_pass; char errbuf[256]; SSL_CTX *ctx = NULL; + EVP_PKEY *primary_pkey = NULL; + EVP_PKEY *alt_pkey = NULL; ctx = SSL_CTX_new(SSLv23_method()); if (!ctx) goto error; @@ -650,13 +649,11 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->cert_expiry, &out_info->cert_serial); if (alt_cert_file) { - EVP_PKEY *primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); + primary_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); if (!primary_pkey) { serverLog(LL_WARNING, "Could not get public key from primary certificate"); goto error; } - int primary_alg = EVP_PKEY_base_id(primary_pkey); - EVP_PKEY_free(primary_pkey); if (SSL_CTX_use_certificate_chain_file(ctx, alt_cert_file) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); @@ -669,15 +666,13 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - EVP_PKEY *alt_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); + alt_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); if (!alt_pkey) { serverLog(LL_WARNING, "Could not get public key from alternate certificate"); goto error; } - int alt_alg = EVP_PKEY_base_id(alt_pkey); - EVP_PKEY_free(alt_pkey); - if (primary_alg == alt_alg) { + if (EVP_PKEY_base_id(primary_pkey) == EVP_PKEY_base_id(alt_pkey)) { serverLog(LL_WARNING, "Primary and alternate certificates must use different key algorithms"); goto error; } @@ -689,7 +684,7 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf); goto error; } - if (alt_cert_file) { + if (alt_key_file) { SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *)alt_key_file_pass); if (SSL_CTX_use_PrivateKey_file(ctx, alt_key_file, SSL_FILETYPE_PEM) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); @@ -747,9 +742,13 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco * 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; error: + EVP_PKEY_free(primary_pkey); + EVP_PKEY_free(alt_pkey); if (ctx) SSL_CTX_free(ctx); return NULL; } From 84134b42cdf20682ab7e9d9786499d837e32f177 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Fri, 11 Sep 2026 14:48:19 -0700 Subject: [PATCH 15/15] Drop the pairing check and the adjacent tests Rejecting a certificate with no matching private key is a new startup refusal, and it is not needed to make the reporting correct: in that configuration 0e36225 also reported the wrong certificate, and unstable already starts with a mismatched pair. It is a second bug, so it goes in its own change. The TLS 1.2 client certificate test goes with it. It covers pre-existing cursor behaviour rather than anything this change introduces. Signed-off-by: Madelyn Olson --- src/tls.c | 17 +--------- tests/unit/tls.tcl | 84 ---------------------------------------------- 2 files changed, 1 insertion(+), 100 deletions(-) diff --git a/src/tls.c b/src/tls.c index 0d6ef8c68..ebc74ea06 100644 --- a/src/tls.c +++ b/src/tls.c @@ -645,7 +645,7 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco } /* INFO reporting only. A failure leaves the field as none rather than refusing the - * configuration. The pairing check below guarantees this certificate has a key. */ + * configuration. */ if (out_info) tlsUpdateCertInfoFromCtx(ctx, &out_info->cert_expiry, &out_info->cert_serial); if (alt_cert_file) { @@ -693,21 +693,6 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco } } - /* Every configured certificate must have ended up with a matching private key. - * OpenSSL routes a key to the slot for its own key algorithm and only checks it - * against whatever certificate is already in that slot, so a mismatched pair can - * leave a certificate loaded with no key rather than failing outright. Such a - * certificate cannot be presented, and only the slots holding both are usable. */ - int usable_certs = 0; - for (int op = SSL_CERT_SET_FIRST; SSL_CTX_set_current_cert(ctx, op) == 1; op = SSL_CERT_SET_NEXT) { - usable_certs++; - } - if (usable_certs != (alt_cert_file ? 2 : 1)) { - serverLog(LL_WARNING, "A configured %s TLS certificate has no matching private key.", - client ? "client" : "server"); - goto error; - } - if (ctx_config->ca_cert_file || ctx_config->ca_cert_dir) { if (SSL_CTX_load_verify_locations(ctx, ctx_config->ca_cert_file, ctx_config->ca_cert_dir) <= 0) { ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf)); diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 1b0af4f6a..dd2d66152 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -235,90 +235,6 @@ start_server {tags {"tls"}} { } } - test {TLS: a TLS 1.2 outgoing connection presents the tls-cert-file certificate} { - set primary [srv 0 client] - set primary_host [srv 0 host] - set primary_port [srv 0 port] - - 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]] - - # Authenticate the replica by the CN of the certificate it presents, and give - # both candidate CNs an account so a wrong choice reports as itself rather - # than just failing to connect. valkey.crt is CN=Generic-cert and - # valkey-ec.crt is CN=EC-cert. - r ACL SETUSER {Generic-cert} on nopass ~* &* +@all - r ACL SETUSER {EC-cert} on nopass ~* &* +@all - r CONFIG SET tls-auth-clients-user CN - - try { - # A TLS 1.3 client picks by the peer's signature algorithm preference and - # ignores the certificate cursor, so pin the replica to 1.2. - 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 \ - tls-protocols TLSv1.2] \ - omit [list tls-client-cert-file tls-client-key-file]] { - r replicaof $primary_host $primary_port - wait_for_condition 50 100 { - [string match {*master_link_status:up*} [r info replication]] - } else { - fail "replica could not sync using tls-cert-file as its client cert" - } - - assert {[regexp {user=(\S+)} [$primary client list type replica] -> replica_user]} - assert_equal "Generic-cert" $replica_user - } - } finally { - r CONFIG SET tls-auth-clients-user off - r ACL DELUSER {Generic-cert} {EC-cert} - } - } - - test {TLS: a certificate without a matching key is rejected} { - set orig_crt [lindex [r config get tls-cert-file] 1] - set orig_key [lindex [r config get tls-key-file] 1] - set orig_alt_crt [lindex [r config get tls-alt-cert-file] 1] - set orig_alt_key [lindex [r config get tls-alt-key-file] 1] - set orig_client_crt [lindex [r config get tls-client-cert-file] 1] - set orig_client_key [lindex [r config get tls-client-key-file] 1] - - 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]] - - try { - # OpenSSL puts a key in the slot for its own algorithm and only checks it - # against the certificate already there, so a mismatched pair can leave a - # certificate loaded with no key instead of failing. - set from [count_log_lines 0] - catch {r CONFIG SET tls-cert-file $ec_crt tls-key-file $rsa_key \ - tls-alt-cert-file $rsa_crt tls-alt-key-file $rsa_key} e - assert_match {*Unable to update TLS configuration*} $e - verify_log_message 0 "*server TLS certificate has no matching private key*" $from - - # Same the other way round: the alternate certificate left without a key, - # while tls-cert-file and tls-key-file are a good pair. - set from [count_log_lines 0] - catch {r CONFIG SET tls-cert-file $ec_crt tls-key-file $ec_key \ - tls-alt-cert-file $rsa_crt tls-alt-key-file $ec_key} e - assert_match {*Unable to update TLS configuration*} $e - verify_log_message 0 "*server TLS certificate has no matching private key*" $from - - # The client context goes through the same check. - set from [count_log_lines 0] - catch {r CONFIG SET tls-client-cert-file $ec_crt tls-client-key-file $rsa_key} e - assert_match {*Unable to update TLS configuration*} $e - verify_log_message 0 "*client TLS certificate has no matching private key*" $from - } finally { - r CONFIG SET tls-cert-file $orig_crt tls-key-file $orig_key \ - tls-alt-cert-file $orig_alt_crt tls-alt-key-file $orig_alt_key \ - tls-client-cert-file $orig_client_crt tls-client-key-file $orig_client_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]