1e0c4386eSCy Schubert /* 2*a7148ab3SEnji Cooper * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. 3e0c4386eSCy Schubert * 4e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use 5e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy 6e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at 7e0c4386eSCy Schubert * https://www.openssl.org/source/license.html 8e0c4386eSCy Schubert */ 9e0c4386eSCy Schubert 10e0c4386eSCy Schubert #include <stddef.h> 11e0c4386eSCy Schubert #include <openssl/crypto.h> 12e0c4386eSCy Schubert #include "internal/provider.h" 13e0c4386eSCy Schubert #include "testutil.h" 14e0c4386eSCy Schubert 15e0c4386eSCy Schubert extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME; 16e0c4386eSCy Schubert 17e0c4386eSCy Schubert static char buf[256]; 18e0c4386eSCy Schubert static OSSL_PARAM greeting_request[] = { 19e0c4386eSCy Schubert { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), 0 }, 20e0c4386eSCy Schubert { NULL, 0, NULL, 0, 0 } 21e0c4386eSCy Schubert }; 22e0c4386eSCy Schubert 23e0c4386eSCy Schubert static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting) 24e0c4386eSCy Schubert { 25*a7148ab3SEnji Cooper const char *greeting = "no greeting received"; 26e0c4386eSCy Schubert int ret = 0; 27e0c4386eSCy Schubert 28e0c4386eSCy Schubert ret = 29e0c4386eSCy Schubert TEST_true(ossl_provider_activate(prov, 1, 0)) 30e0c4386eSCy Schubert && TEST_true(ossl_provider_get_params(prov, greeting_request)) 31e0c4386eSCy Schubert && TEST_ptr(greeting = greeting_request[0].data) 32e0c4386eSCy Schubert && TEST_size_t_gt(greeting_request[0].data_size, 0) 33e0c4386eSCy Schubert && TEST_str_eq(greeting, expected_greeting) 34e0c4386eSCy Schubert && TEST_true(ossl_provider_deactivate(prov, 1)); 35e0c4386eSCy Schubert 36e0c4386eSCy Schubert TEST_info("Got this greeting: %s\n", greeting); 37e0c4386eSCy Schubert ossl_provider_free(prov); 38e0c4386eSCy Schubert return ret; 39e0c4386eSCy Schubert } 40e0c4386eSCy Schubert 41e0c4386eSCy Schubert static const char *expected_greeting1(const char *name) 42e0c4386eSCy Schubert { 43e0c4386eSCy Schubert static char expected_greeting[256] = ""; 44e0c4386eSCy Schubert 45e0c4386eSCy Schubert BIO_snprintf(expected_greeting, sizeof(expected_greeting), 46e0c4386eSCy Schubert "Hello OpenSSL %.20s, greetings from %s!", 47e0c4386eSCy Schubert OPENSSL_VERSION_STR, name); 48e0c4386eSCy Schubert 49e0c4386eSCy Schubert return expected_greeting; 50e0c4386eSCy Schubert } 51e0c4386eSCy Schubert 52e0c4386eSCy Schubert static int test_builtin_provider(void) 53e0c4386eSCy Schubert { 54e0c4386eSCy Schubert const char *name = "p_test_builtin"; 55e0c4386eSCy Schubert OSSL_PROVIDER *prov = NULL; 56e0c4386eSCy Schubert int ret; 57e0c4386eSCy Schubert 58e0c4386eSCy Schubert /* 59e0c4386eSCy Schubert * We set properties that we know the providers we are using don't have. 60e0c4386eSCy Schubert * This should mean that the p_test provider will fail any fetches - which 61e0c4386eSCy Schubert * is something we test inside the provider. 62e0c4386eSCy Schubert */ 63e0c4386eSCy Schubert EVP_set_default_properties(NULL, "fips=yes"); 64e0c4386eSCy Schubert 65e0c4386eSCy Schubert ret = 66e0c4386eSCy Schubert TEST_ptr(prov = 67e0c4386eSCy Schubert ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, 0)) 68e0c4386eSCy Schubert && test_provider(prov, expected_greeting1(name)); 69e0c4386eSCy Schubert 70e0c4386eSCy Schubert EVP_set_default_properties(NULL, ""); 71e0c4386eSCy Schubert 72e0c4386eSCy Schubert return ret; 73e0c4386eSCy Schubert } 74e0c4386eSCy Schubert 75e0c4386eSCy Schubert #ifndef NO_PROVIDER_MODULE 76e0c4386eSCy Schubert static int test_loaded_provider(void) 77e0c4386eSCy Schubert { 78e0c4386eSCy Schubert const char *name = "p_test"; 79e0c4386eSCy Schubert OSSL_PROVIDER *prov = NULL; 80e0c4386eSCy Schubert 81e0c4386eSCy Schubert return 82e0c4386eSCy Schubert TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, 0)) 83e0c4386eSCy Schubert && test_provider(prov, expected_greeting1(name)); 84e0c4386eSCy Schubert } 85e0c4386eSCy Schubert 86e0c4386eSCy Schubert # ifndef OPENSSL_NO_AUTOLOAD_CONFIG 87e0c4386eSCy Schubert static int test_configured_provider(void) 88e0c4386eSCy Schubert { 89e0c4386eSCy Schubert const char *name = "p_test_configured"; 90e0c4386eSCy Schubert OSSL_PROVIDER *prov = NULL; 91e0c4386eSCy Schubert /* This MUST match the config file */ 92e0c4386eSCy Schubert const char *expected_greeting = 93e0c4386eSCy Schubert "Hello OpenSSL, greetings from Test Provider"; 94e0c4386eSCy Schubert 95e0c4386eSCy Schubert return 96e0c4386eSCy Schubert TEST_ptr(prov = ossl_provider_find(NULL, name, 0)) 97e0c4386eSCy Schubert && test_provider(prov, expected_greeting); 98e0c4386eSCy Schubert } 99e0c4386eSCy Schubert # endif 100e0c4386eSCy Schubert #endif 101e0c4386eSCy Schubert 102e0c4386eSCy Schubert static int test_cache_flushes(void) 103e0c4386eSCy Schubert { 104e0c4386eSCy Schubert OSSL_LIB_CTX *ctx; 105e0c4386eSCy Schubert OSSL_PROVIDER *prov = NULL; 106e0c4386eSCy Schubert EVP_MD *md = NULL; 107e0c4386eSCy Schubert int ret = 0; 108e0c4386eSCy Schubert 109e0c4386eSCy Schubert if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()) 110e0c4386eSCy Schubert || !TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default")) 111e0c4386eSCy Schubert || !TEST_true(OSSL_PROVIDER_available(ctx, "default")) 112e0c4386eSCy Schubert || !TEST_ptr(md = EVP_MD_fetch(ctx, "SHA256", NULL))) 113e0c4386eSCy Schubert goto err; 114e0c4386eSCy Schubert EVP_MD_free(md); 115e0c4386eSCy Schubert md = NULL; 116e0c4386eSCy Schubert OSSL_PROVIDER_unload(prov); 117e0c4386eSCy Schubert prov = NULL; 118e0c4386eSCy Schubert 119e0c4386eSCy Schubert if (!TEST_false(OSSL_PROVIDER_available(ctx, "default"))) 120e0c4386eSCy Schubert goto err; 121e0c4386eSCy Schubert 122e0c4386eSCy Schubert if (!TEST_ptr_null(md = EVP_MD_fetch(ctx, "SHA256", NULL))) { 123e0c4386eSCy Schubert const char *provname = OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(md)); 124e0c4386eSCy Schubert 125e0c4386eSCy Schubert if (OSSL_PROVIDER_available(NULL, provname)) 126e0c4386eSCy Schubert TEST_info("%s provider is available\n", provname); 127e0c4386eSCy Schubert else 128e0c4386eSCy Schubert TEST_info("%s provider is not available\n", provname); 129e0c4386eSCy Schubert } 130e0c4386eSCy Schubert 131e0c4386eSCy Schubert ret = 1; 132e0c4386eSCy Schubert err: 133e0c4386eSCy Schubert OSSL_PROVIDER_unload(prov); 134e0c4386eSCy Schubert EVP_MD_free(md); 135e0c4386eSCy Schubert OSSL_LIB_CTX_free(ctx); 136e0c4386eSCy Schubert return ret; 137e0c4386eSCy Schubert } 138e0c4386eSCy Schubert 139e0c4386eSCy Schubert int setup_tests(void) 140e0c4386eSCy Schubert { 141e0c4386eSCy Schubert ADD_TEST(test_builtin_provider); 142e0c4386eSCy Schubert #ifndef NO_PROVIDER_MODULE 143e0c4386eSCy Schubert ADD_TEST(test_loaded_provider); 144e0c4386eSCy Schubert # ifndef OPENSSL_NO_AUTOLOAD_CONFIG 145e0c4386eSCy Schubert ADD_TEST(test_configured_provider); 146e0c4386eSCy Schubert # endif 147e0c4386eSCy Schubert #endif 148e0c4386eSCy Schubert ADD_TEST(test_cache_flushes); 149e0c4386eSCy Schubert return 1; 150e0c4386eSCy Schubert } 151e0c4386eSCy Schubert 152