Commit 7272e2ebb0 for openssl.org

commit 7272e2ebb09bf6c354a9dd353c3430f374d5c70d
Author: Dmitry Belyavskiy <beldmit@gmail.com>
Date:   Fri Nov 20 15:24:29 2026 +0100

    Run EVP_SKEY tests in FIPS mode

    Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com>

    Reviewed-by: Igor Ustinov <igus@openssl.foundation>
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
    Reviewed-by: Simo Sorce <simo@redhat.com>
    MergeDate: Tue Nov 24 14:38:04 2026
    (Merged from https://github.com/openssl/openssl/pull/33852)

diff --git a/test/evp_skey_test.c b/test/evp_skey_test.c
index 29548510a4..3cd7233e13 100644
--- a/test/evp_skey_test.c
+++ b/test/evp_skey_test.c
@@ -16,7 +16,7 @@
 #include "fake_cipherprov.h"

 static OSSL_LIB_CTX *libctx = NULL;
-static OSSL_PROVIDER *deflprov = NULL;
+static OSSL_PROVIDER *nullprov = NULL;

 #define KEY_SIZE 16

@@ -109,10 +109,6 @@ static int test_skey_skeymgmt(void)
     const void *export_key = NULL;
     size_t export_len;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     /* Fetch our SKEYMGMT for Generic Secrets */
     if (!TEST_ptr(skeymgmt = EVP_SKEYMGMT_fetch(libctx, OSSL_SKEY_TYPE_GENERIC,
                       NULL)))
@@ -178,17 +174,13 @@ static int test_aes_raw_skey(void)
     OSSL_PARAM *params = NULL;
     int ret = 0;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     memset(encrypted_skey, 0, sizeof(encrypted_skey));
     memset(encrypted_raw, 0, sizeof(encrypted_raw));
     memset(aes_key, 1, KEY_SIZE);
     memset(aes_iv, 2, IV_SIZE);

-    /* Do a direct fetch to see it works */
-    aes_cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", "provider=default");
+    /* Fetch AES-128-CBC from whatever provider is configured */
+    aes_cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
     if (!TEST_ptr(aes_cbc))
         goto end;

@@ -227,16 +219,18 @@ end:
     EVP_SKEY_free(skey);
     EVP_CIPHER_free(aes_cbc);
     EVP_CIPHER_CTX_free(ctx);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }

 #ifndef OPENSSL_NO_DES
-/* DES is used to test a "skey-unware" cipher provider */
+/* DES is used to test a "skey-unaware" cipher provider */
 #define DES_KEY_SIZE 24
 #define DES_IV_SIZE 8
 static int test_des_raw_skey(void)
 {
+    /* DES is not available in FIPS mode */
+    if (OSSL_PROVIDER_available(libctx, "fips"))
+        return TEST_skip("DES not available in FIPS mode");
     const unsigned char data[DATA_SIZE] = {
         0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,
         0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,
@@ -254,17 +248,13 @@ static int test_des_raw_skey(void)
     EVP_SKEY *skey = NULL;
     int ret = 0;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     memset(encrypted_skey, 0, sizeof(encrypted_skey));
     memset(encrypted_raw, 0, sizeof(encrypted_raw));
     memset(des_key, 1, DES_KEY_SIZE);
     memset(des_iv, 2, DES_IV_SIZE);

-    /* Do a direct fetch to see it works */
-    des_cbc = EVP_CIPHER_fetch(libctx, "DES-EDE3-CBC", "provider=default");
+    /* Fetch DES-EDE3-CBC from whatever provider is configured */
+    des_cbc = EVP_CIPHER_fetch(libctx, "DES-EDE3-CBC", NULL);
     if (!TEST_ptr(des_cbc))
         goto end;

@@ -303,7 +293,6 @@ end:
     EVP_SKEY_free(skey);
     EVP_CIPHER_free(des_cbc);
     EVP_CIPHER_CTX_free(ctx);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }
 #endif
@@ -330,10 +319,6 @@ static int test_skey_metadata_import(int tst)
     const unsigned char *out_oid = NULL, *out_params = NULL;
     size_t out_oid_len = 0, out_params_len = 0;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
         goto end;

@@ -433,7 +418,6 @@ end:
     OSSL_PARAM_free(exp_params);
     OSSL_PARAM_BLD_free(bld);
     EVP_SKEY_free(key);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }

@@ -453,10 +437,6 @@ static int test_skey_metadata_export(int tst)
     const unsigned char local_keyid[] = { 0xAA, 0xBB, 0xCC };
     int export_ret;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
         goto end;

@@ -533,7 +513,6 @@ end:
     OSSL_PARAM_free(exp_params);
     OSSL_PARAM_BLD_free(bld);
     EVP_SKEY_free(key);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }

@@ -590,10 +569,6 @@ static int test_skey_to_diff_provider(void)
     };
     int ret = 0;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     if (!TEST_ptr(fake_prov = fake_cipher_start(libctx)))
         goto end;

@@ -618,7 +593,6 @@ end:
     EVP_SKEY_free(key2);
     EVP_SKEY_free(key);
     fake_cipher_finish(fake_prov);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }

@@ -637,10 +611,6 @@ static int test_skey_get0_raw_key_len(int tst)
     size_t len = 0;
     int ret = 0;

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     switch (tst) {
     case 0:
         /* Generic key, only the length is requested */
@@ -689,7 +659,6 @@ static int test_skey_get0_raw_key_len(int tst)
 end:
     EVP_SKEY_free(key);
     fake_cipher_finish(fake_prov);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }

@@ -709,10 +678,6 @@ static int test_hmac_skey(void)
     size_t mac_raw_len = sizeof(mac_raw);
     OSSL_PARAM params[2];

-    deflprov = OSSL_PROVIDER_load(libctx, "default");
-    if (!TEST_ptr(deflprov))
-        return 0;
-
     /* Create EVP_SKEY from raw key */
     if (!TEST_ptr(skey = EVP_SKEY_import_raw_key(libctx, OSSL_SKEY_TYPE_GENERIC,
                       key, sizeof(key), NULL)))
@@ -753,14 +718,56 @@ end:
     EVP_MAC_CTX_free(ctx_raw);
     EVP_MAC_free(hmac);
     EVP_SKEY_free(skey);
-    OSSL_PROVIDER_unload(deflprov);
     return ret;
 }

+typedef enum OPTION_choice {
+    OPT_ERR = -1,
+    OPT_EOF = 0,
+    OPT_CONFIG_FILE,
+    OPT_TEST_ENUM
+} OPTION_CHOICE;
+
+const OPTIONS *test_get_options(void)
+{
+    static const OPTIONS options[] = {
+        OPT_TEST_OPTIONS_DEFAULT_USAGE,
+        { "config", OPT_CONFIG_FILE, '<',
+            "The configuration file to use for the libctx" },
+        { NULL }
+    };
+    return options;
+}
+
 int setup_tests(void)
 {
-    libctx = OSSL_LIB_CTX_new();
-    if (libctx == NULL)
+    char *config_file = NULL;
+    OPTION_CHOICE o;
+
+    while ((o = opt_next()) != OPT_EOF) {
+        switch (o) {
+        case OPT_CONFIG_FILE:
+            config_file = opt_arg();
+            break;
+        case OPT_TEST_CASES:
+        default:
+        case OPT_ERR:
+            return 0;
+        }
+    }
+
+    /*
+     * This test must be run with a config file to ensure providers are
+     * properly configured. This allows the test to work in both default
+     * and FIPS modes without hardcoding provider loading.
+     */
+    if (config_file == NULL) {
+        TEST_error("No config file specified. Use -config <file>");
+        return 0;
+    }
+
+    /* Load the configuration - this sets up the appropriate providers */
+    if (!test_get_libctx(&libctx, &nullprov, config_file, NULL, NULL))
         return 0;

     ADD_TEST(test_skey_cipher);
@@ -782,5 +789,6 @@ int setup_tests(void)

 void cleanup_tests(void)
 {
+    OSSL_PROVIDER_unload(nullprov);
     OSSL_LIB_CTX_free(libctx);
 }
diff --git a/test/recipes/30-test_evp_skey.t b/test/recipes/30-test_evp_skey.t
index b535b28849..f3660487d0 100644
--- a/test/recipes/30-test_evp_skey.t
+++ b/test/recipes/30-test_evp_skey.t
@@ -7,7 +7,32 @@
 # in the file LICENSE in the source distribution or at
 # https://www.openssl.org/source/license.html

+use strict;
+use warnings;

-use OpenSSL::Test::Simple;
+use OpenSSL::Test qw/:DEFAULT srctop_file/;
+use OpenSSL::Test::Utils;

-simple_test("test_evp_skey", "evp_skey_test");
+setup("test_evp_skey");
+
+my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
+
+plan tests => $no_fips ? 1 : 2;
+
+ok(run(test(["evp_skey_test", "-config", srctop_file("test", "default.cnf")])),
+   "running evp_skey_test with default provider");
+
+unless ($no_fips) {
+    my $fips_conf = srctop_file("test", "fips-and-base.cnf");
+
+    SKIP: {
+        # SKEYMGMT support in FIPS provider requires >= 3.6.0
+        run(test(["fips_version_test", "-config", $fips_conf, ">=4.2.0"]),
+                 capture => 1, statusvar => \my $fips_ge_4_2);
+        skip "FIPS provider version < 4.2.0 (SKEYMGMT not supported)", 1
+            if !$fips_ge_4_2;
+
+        ok(run(test(["evp_skey_test", "-config", $fips_conf])),
+           "running evp_skey_test with FIPS provider");
+    }
+}