From: Joshua Rogers Date: Sat, 4 Apr 2026 10:28:46 +0000 (+0800) Subject: evp_skey_test.c: Add test for EVP_SKEY_to_provider cross-provider transfer X-Git-Tag: openssl-4.0.0~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3219b391cd03411d828a79d879e3304dda31b1bc;p=thirdparty%2Fopenssl.git evp_skey_test.c: Add test for EVP_SKEY_to_provider cross-provider transfer Reviewed-by: Dmitry Belyavskiy Reviewed-by: Frederik Wedel-Heinen MergeDate: Wed Apr 8 10:27:04 2026 (Merged from https://github.com/openssl/openssl/pull/30650) (cherry picked from commit e249566980232cfe7f2d9b9b48e5102b3005eddb) --- diff --git a/test/evp_skey_test.c b/test/evp_skey_test.c index f0e794ec7f6..453dc7733dd 100644 --- a/test/evp_skey_test.c +++ b/test/evp_skey_test.c @@ -391,6 +391,64 @@ end: return ret; } +static int test_skey_to_diff_provider(void) +{ + OSSL_PROVIDER *fake_prov = NULL; + EVP_SKEY *key = NULL, *key2 = NULL; + const unsigned char *export_key = NULL; + size_t export_len; + unsigned char import_key[KEY_SIZE] = { + 0x53, + 0x4B, + 0x45, + 0x59, + 0x53, + 0x4B, + 0x45, + 0x59, + 0x53, + 0x4B, + 0x45, + 0x59, + 0x53, + 0x4B, + 0x45, + 0x59, + }; + 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; + + if (!TEST_ptr(key = EVP_SKEY_import_raw_key(libctx, OSSL_SKEY_TYPE_GENERIC, + import_key, sizeof(import_key), NULL))) + goto end; + + if (!TEST_ptr(key2 = EVP_SKEY_to_provider(key, libctx, fake_prov, + FAKE_CIPHER_FETCH_PROPS))) + goto end; + + /* Different provider must return a different object */ + if (!TEST_ptr_ne(key2, key)) + goto end; + + if (!TEST_int_gt(EVP_SKEY_get0_raw_key(key2, &export_key, &export_len), 0) + || !TEST_mem_eq(import_key, sizeof(import_key), export_key, export_len)) + goto end; + + ret = 1; +end: + EVP_SKEY_free(key2); + EVP_SKEY_free(key); + fake_cipher_finish(fake_prov); + OSSL_PROVIDER_unload(deflprov); + return ret; +} + int setup_tests(void) { libctx = OSSL_LIB_CTX_new(); @@ -401,6 +459,7 @@ int setup_tests(void) ADD_TEST(test_skey_skeymgmt); ADD_TEST(test_skey_to_same_provider); + ADD_TEST(test_skey_to_diff_provider); ADD_TEST(test_aes_raw_skey); #ifndef OPENSSL_NO_DES ADD_TEST(test_des_raw_skey); diff --git a/test/fake_cipherprov.c b/test/fake_cipherprov.c index 2322a1a8122..a595c89799f 100644 --- a/test/fake_cipherprov.c +++ b/test/fake_cipherprov.c @@ -110,6 +110,7 @@ static const OSSL_DISPATCH fake_skeymgmt_funcs[] = { static const OSSL_ALGORITHM fake_skeymgmt_algs[] = { { "fake_cipher", FAKE_CIPHER_FETCH_PROPS, fake_skeymgmt_funcs, "Fake Cipher Key Management" }, + { OSSL_SKEY_TYPE_GENERIC, FAKE_CIPHER_FETCH_PROPS, fake_skeymgmt_funcs, "Fake Generic Key Management" }, { NULL, NULL, NULL, NULL } }; static OSSL_FUNC_cipher_newctx_fn fake_newctx;