diff --git a/src/tls.c b/src/tls.c index 304113725e3..66eef0dd698 100644 --- a/src/tls.c +++ b/src/tls.c @@ -499,108 +499,6 @@ static int tlsPasswordCallback(char *buf, int size, int rwflag, void *u) { return (int)pass_len; } -/* Check a single X509 certificate validity */ -static bool isCertValid(X509 *cert) { - if (!cert) return false; -#if OPENSSL_VERSION_NUMBER >= 0x40000000L - int error = 0; - if (X509_check_certificate_times(NULL, cert, &error) != 1) { - if (error == X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD) { - serverLog(LL_WARNING, "Certificate has an invalid notBefore field"); - } else if (error == X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD) { - serverLog(LL_WARNING, "Certificate has an invalid notAfter field"); - } - return false; - } -#else - const ASN1_TIME *not_before = X509_get0_notBefore(cert); - const ASN1_TIME *not_after = X509_get0_notAfter(cert); - if (!not_before || !not_after) return false; - if (X509_cmp_current_time(not_before) > 0 || - X509_cmp_current_time(not_after) < 0) { - return false; - } -#endif - return true; -} - -/* Load all certificates from a directory into the X509_STORE - * Returns true on success, false on failure */ -static bool loadCaCertDir(SSL_CTX *ctx, const char *ca_cert_dir) { - if (!ca_cert_dir) return true; - - DIR *dir; - struct dirent *entry; - char full_path[PATH_MAX]; - X509_STORE *store = SSL_CTX_get_cert_store(ctx); - - if (!store) { - serverLog(LL_WARNING, "Failed to get X509_STORE from SSL_CTX"); - return false; - } - - dir = opendir(ca_cert_dir); - if (!dir) { - serverLog(LL_WARNING, "Failed to open CA certificate directory: %s", ca_cert_dir); - return false; - } - - int loaded = 0; - while ((entry = readdir(dir)) != NULL) { - if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; - - snprintf(full_path, sizeof(full_path), "%s/%s", ca_cert_dir, entry->d_name); - FILE *fp = fopen(full_path, "r"); - if (!fp) continue; - - X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL); - fclose(fp); - - if (cert) { - if (X509_STORE_add_cert(store, cert) != 1) { - unsigned long err = ERR_peek_last_error(); - if (ERR_GET_REASON(err) != X509_R_CERT_ALREADY_IN_HASH_TABLE) { - serverLog(LL_WARNING, "Failed to add CA certificate from %s to store", full_path); - X509_free(cert); - closedir(dir); - return false; - } - ERR_clear_error(); - } - X509_free(cert); - loaded++; - } - } - - closedir(dir); - - if (loaded == 0) { - serverLog(LL_WARNING, "No CA certificates loaded from directory: %s", ca_cert_dir); - return false; - } - - return true; -} - -/* Iterate over all CA certs in the SSL_CTX and fail-fast if any are invalid */ -static bool areAllCaCertsValid(SSL_CTX *ctx) { - X509_STORE *store = SSL_CTX_get_cert_store(ctx); - if (!store) return false; - STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); - if (!objs) return false; - for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { - X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i); - int type = X509_OBJECT_get_type(obj); - if (type == X509_LU_X509) { - X509 *ca_cert = X509_OBJECT_get0_X509(obj); - if (ca_cert && !isCertValid(ca_cert)) { - return false; - } - } - } - return true; -} - /* 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. */ @@ -650,11 +548,6 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - if (!isCertValid(SSL_CTX_get0_certificate(ctx))) { - serverLog(LL_WARNING, "%s TLS certificate is invalid. Aborting TLS configuration.", client ? "Client" : "Server"); - 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); @@ -672,11 +565,6 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco goto error; } - if (!isCertValid(SSL_CTX_get0_certificate(ctx))) { - serverLog(LL_WARNING, "Alternate server TLS certificate is invalid. Aborting TLS configuration."); - goto error; - } - alt_pkey = X509_get_pubkey(SSL_CTX_get0_certificate(ctx)); if (!alt_pkey) { serverLog(LL_WARNING, "Could not get public key from alternate certificate"); @@ -704,22 +592,11 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco } } - 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)); - serverLog(LL_WARNING, "Failed to configure CA certificate(s) file/directory: %s", errbuf); - goto error; - } - - if (!loadCaCertDir(ctx, ctx_config->ca_cert_dir)) { - serverLog(LL_WARNING, "Failed to load CA certificates from directory: %s", ctx_config->ca_cert_dir); - goto error; - } - - if (!areAllCaCertsValid(ctx)) { - serverLog(LL_WARNING, "One or more loaded CA certificates are invalid. Aborting TLS configuration."); - goto error; - } + if ((ctx_config->ca_cert_file || ctx_config->ca_cert_dir) && + 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)); + serverLog(LL_WARNING, "Failed to configure CA certificate(s) file/directory: %s", errbuf); + goto error; } if (ctx_config->ciphers && !SSL_CTX_set_cipher_list(ctx, ctx_config->ciphers)) { diff --git a/tests/README.md b/tests/README.md index 7e7caf9c97e..a766431bd54 100644 --- a/tests/README.md +++ b/tests/README.md @@ -39,7 +39,7 @@ match different external server configurations. All options are listed by Running with TLS requires the following preparations: * Build Valkey is TLS support, e.g. using `make BUILD_TLS=yes`, or `make BUILD_TLS=module`. -* Run `./utils/gen-test-certs.sh` to generate a root CA, server certificates, and invalid certificates for testing. +* Run `./utils/gen-test-certs.sh` to generate a root CA and a server certificate. * Install TLS support for TCL, e.g. the `tcl-tls` package on Debian/Ubuntu. Additional tests diff --git a/tests/unit/tls.tcl b/tests/unit/tls.tcl index 50385cfdbc2..41139875a7c 100644 --- a/tests/unit/tls.tcl +++ b/tests/unit/tls.tcl @@ -679,118 +679,6 @@ start_server {tags {"tls"}} { } } } - - proc test_tls_cert_rejection {cert_type cert_path expected_error} { - set tlsdir [file normalize ./tests/tls] - set server_path $::VALKEY_SERVER_BIN - set server_cert $tlsdir/server.crt - set server_key $tlsdir/server.key - set client_cert $tlsdir/client.crt - set client_key $tlsdir/client.key - set ca_cert_file $tlsdir/ca.crt - set ca_cert_dir "" - - switch -- $cert_type { - server { set server_cert $cert_path } - client { set client_cert $cert_path } - "ca-file" { set ca_cert_file $cert_path } - "ca-dir" { set ca_cert_dir $cert_path; set ca_cert_file "" } - } - - set cmd [list $server_path --port 0 --tls-port 16379 \ - --tls-cert-file $server_cert --tls-key-file $server_key \ - --tls-client-cert-file $client_cert --tls-client-key-file $client_key] - - if {$ca_cert_file ne ""} { lappend cmd --tls-ca-cert-file $ca_cert_file } - if {$ca_cert_dir ne ""} { lappend cmd --tls-ca-cert-dir $ca_cert_dir } - - if {$::tls_module} { - lappend cmd --loadmodule $::VALKEY_TLS_MODULE - } - - catch {exec {*}$cmd 2>@1} err - assert_match $expected_error $err - } - - test {TLS: Fail-fast on invalid certificates at startup} { - set tlsdir [file normalize ./tests/tls] - - # Expired server certificate - test_tls_cert_rejection server $tlsdir/server-expired.crt {*Server TLS certificate is invalid*} - - # Not-yet-valid server certificate - test_tls_cert_rejection server $tlsdir/server-notyet.crt {*Server TLS certificate is invalid*} - - # Expired client certificate - test_tls_cert_rejection client $tlsdir/client-expired.crt {*Client TLS certificate is invalid*} - - # Not-yet-valid client certificate - test_tls_cert_rejection client $tlsdir/client-notyet.crt {*Client TLS certificate is invalid*} - - # Expired CA certificate file - test_tls_cert_rejection ca-file $tlsdir/ca-expired.crt {*One or more loaded CA certificates are invalid*} - - # Not-yet-valid CA certificate file - test_tls_cert_rejection ca-file $tlsdir/ca-notyet.crt {*One or more loaded CA certificates are invalid*} - - # Expired CA certificate directory - test_tls_cert_rejection ca-dir $tlsdir/ca-expired {*One or more loaded CA certificates are invalid*} - - # Not-yet-valid CA certificate directory - test_tls_cert_rejection ca-dir $tlsdir/ca-notyet {*One or more loaded CA certificates are invalid*} - - # Empty CA certificate directory - test_tls_cert_rejection ca-dir $tlsdir/ca-empty {*No CA certificates loaded from directory*} - } - - proc test_tls_cert_rejection_runtime {r cert_type cert_path} { - switch -- $cert_type { - server { - catch {$r CONFIG SET tls-cert-file $cert_path} err - } - client { - catch {$r CONFIG SET tls-client-cert-file $cert_path} err - } - "ca-file" { - catch {$r CONFIG SET tls-ca-cert-file $cert_path} err - } - "ca-dir" { - catch {$r CONFIG SET tls-ca-cert-dir $cert_path} err - } - } - assert_match {*Unable to update TLS*} $err - } - - test {TLS: Fail-fast on invalid certificates at runtime} { - set tlsdir [file normalize ./tests/tls] - - # Expired server certificate - test_tls_cert_rejection_runtime r server $tlsdir/server-expired.crt - - # Not-yet-valid server certificate - test_tls_cert_rejection_runtime r server $tlsdir/server-notyet.crt - - # Expired client certificate - test_tls_cert_rejection_runtime r client $tlsdir/client-expired.crt - - # Not-yet-valid client certificate - test_tls_cert_rejection_runtime r client $tlsdir/client-notyet.crt - - # Expired CA certificate file - test_tls_cert_rejection_runtime r ca-file $tlsdir/ca-expired.crt - - # Not-yet-valid CA certificate file - test_tls_cert_rejection_runtime r ca-file $tlsdir/ca-notyet.crt - - # Expired CA certificate directory - test_tls_cert_rejection_runtime r ca-dir $tlsdir/ca-expired - - # Not-yet-valid CA certificate directory - test_tls_cert_rejection_runtime r ca-dir $tlsdir/ca-notyet - - # Empty CA certificate directory - test_tls_cert_rejection_runtime r ca-dir $tlsdir/ca-empty - } } } diff --git a/utils/gen-test-certs.sh b/utils/gen-test-certs.sh index 56dd6f0f8ca..08f6d0da2dd 100755 --- a/utils/gen-test-certs.sh +++ b/utils/gen-test-certs.sh @@ -3,18 +3,12 @@ # Generate some test certificates which are used by the regression test suite: # # tests/tls/ca.{crt,key} Self signed CA certificate. -# tests/tls/ca-{expired,notyet}.crt Self signed invalid CA certificates. -# tests/tls/ca-expired/ Directory containing expired CA certificate. -# tests/tls/ca-notyet/ Directory containing not-yet-valid CA certificate. -# tests/tls/ca-empty/ Empty directory for testing empty dir rejection. # tests/tls/ca-multi.crt CA bundle with multiple certs. # tests/tls/ca-dir/ CA directory with hashed links. # tests/tls/valkey{,-pw}.{crt,key} A certificate with no key usage/policy restrictions. With and without a passphrase. # tests/tls/valkey-ec{,-pw}.{crt,key} An ECDSA certificate with no key usage/policy restrictions. With and without a passphrase. # tests/tls/client.{crt,key} A certificate restricted for SSL client usage. -# tests/tls/client-{expired,notyet}.crt Invalid certificates restricted for SSL client usage. # tests/tls/server.{crt,key} A certificate restricted for SSL server usage. -# tests/tls/server-{expired,notyet}.crt Invalid certificates restricted for SSL server usage. # tests/tls/client-nul-cn.{crt,key} Client certificate whose CN contains an embedded NUL. # tests/tls/valkey.dh DH Params file. @@ -145,156 +139,3 @@ if [ "$ca_hash_old" != "$ca_hash" ]; then fi [ -f tests/tls/valkey.dh ] || openssl dhparam -out tests/tls/valkey.dh 2048 - -echo "Generating invalid TLS test certificates for fail-fast testing..." - -CA_CONFIG="tests/tls/ca_temp.cnf" -cat > "$CA_CONFIG" < tests/tls/serial - -# Generate expired server cert (valid Jan 1-2, 2020) -openssl req -new -sha256 \ - -subj "/O=Valkey Test/CN=Server-expired" \ - -key tests/tls/server.key \ - -out tests/tls/server-expired.csr - -openssl ca -batch -config "$CA_CONFIG" \ - -in tests/tls/server-expired.csr \ - -cert tests/tls/ca.crt \ - -keyfile tests/tls/ca.key \ - -startdate 20200101000000Z \ - -enddate 20200102000000Z \ - -extensions server_cert \ - -out tests/tls/server-expired.crt - -# Generate expired client cert (valid Jan 1-2, 2020) -openssl req -new -sha256 \ - -subj "/O=Valkey Test/CN=Client-expired" \ - -key tests/tls/client.key \ - -out tests/tls/client-expired.csr - -openssl ca -batch -config "$CA_CONFIG" \ - -in tests/tls/client-expired.csr \ - -cert tests/tls/ca.crt \ - -keyfile tests/tls/ca.key \ - -startdate 20200101000000Z \ - -enddate 20200102000000Z \ - -extensions client_cert \ - -out tests/tls/client-expired.crt - -# Generate not-yet-valid server cert (valid Jan 1-31, 2099) -openssl req -new -sha256 \ - -subj "/O=Valkey Test/CN=Server-notyet" \ - -key tests/tls/server.key \ - -out tests/tls/server-notyet.csr - -openssl ca -batch -config "$CA_CONFIG" \ - -in tests/tls/server-notyet.csr \ - -cert tests/tls/ca.crt \ - -keyfile tests/tls/ca.key \ - -startdate 20990101000000Z \ - -enddate 20990201000000Z \ - -extensions server_cert \ - -out tests/tls/server-notyet.crt - -# Generate not-yet-valid client cert (valid Jan 1-31, 2099) -openssl req -new -sha256 \ - -subj "/O=Valkey Test/CN=Client-notyet" \ - -key tests/tls/client.key \ - -out tests/tls/client-notyet.csr - -openssl ca -batch -config "$CA_CONFIG" \ - -in tests/tls/client-notyet.csr \ - -cert tests/tls/ca.crt \ - -keyfile tests/tls/ca.key \ - -startdate 20990101000000Z \ - -enddate 20990201000000Z \ - -extensions client_cert \ - -out tests/tls/client-notyet.crt - -# Generate expired CA certificate (valid Jan 1-2, 2020) using the CA to sign itself -openssl req -new -sha256 \ - -subj "/O=Valkey Test/CN=Certificate Authority Expired" \ - -key tests/tls/ca.key \ - -out tests/tls/ca-expired.csr - -openssl ca -batch -config "$CA_CONFIG" \ - -selfsign \ - -in tests/tls/ca-expired.csr \ - -keyfile tests/tls/ca.key \ - -startdate 20200101000000Z \ - -enddate 20200102000000Z \ - -out tests/tls/ca-expired.crt - -# Generate not-yet-valid CA certificate (valid Jan 1-31, 2099) -openssl req -new -sha256 \ - -subj "/O=Valkey Test/CN=Certificate Authority Not Yet Valid" \ - -key tests/tls/ca.key \ - -out tests/tls/ca-notyet.csr - -openssl ca -batch -config "$CA_CONFIG" \ - -selfsign \ - -in tests/tls/ca-notyet.csr \ - -keyfile tests/tls/ca.key \ - -startdate 20990101000000Z \ - -enddate 20990201000000Z \ - -out tests/tls/ca-notyet.crt - -# Create CA certificate directories for testing tls-ca-cert-dir with invalid certs -mkdir -p tests/tls/ca-expired -mkdir -p tests/tls/ca-notyet -mkdir -p tests/tls/ca-empty - -cp tests/tls/ca-expired.crt tests/tls/ca-expired/ -cp tests/tls/ca-notyet.crt tests/tls/ca-notyet/ - -echo "Created CA certificate test directories:" -echo " tests/tls/ca-expired/ (contains expired CA cert)" -echo " tests/tls/ca-notyet/ (contains not-yet-valid CA cert)" -echo " tests/tls/ca-empty/ (empty, for testing empty dir rejection)" - -# Clean up temporary files -rm -f tests/tls/*-expired.csr tests/tls/*-notyet.csr tests/tls/ca-expired.csr tests/tls/ca-notyet.csr -rm -f "$CA_CONFIG" tests/tls/index.txt tests/tls/index.txt.attr tests/tls/index.txt.attr.old tests/tls/index.txt.old tests/tls/serial tests/tls/serial.old -rm -f tests/tls/0[1-9].pem tests/tls/[0-9][0-9].pem - -echo "" -echo "Verification of generated invalid certificates:" -for crt in tests/tls/ca-expired.crt tests/tls/ca-notyet.crt tests/tls/*-expired.crt tests/tls/*-notyet.crt; do - if [ -f "$crt" ]; then - echo "" - echo "Certificate: $crt" - openssl x509 -in "$crt" -noout -subject -dates - fi -done