2019-02-21 05:55:43 +08:00
|
|
|
/*
|
2023-09-07 16:59:15 +08:00
|
|
|
* Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2019-02-21 05:55:43 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2019-03-31 05:10:39 +08:00
|
|
|
#include <openssl/crypto.h>
|
2019-02-21 05:55:43 +08:00
|
|
|
#include "internal/provider.h"
|
|
|
|
#include "testutil.h"
|
|
|
|
|
|
|
|
extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
|
|
|
|
|
|
|
|
static char buf[256];
|
|
|
|
static OSSL_PARAM greeting_request[] = {
|
2019-06-24 12:43:55 +08:00
|
|
|
{ "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), 0 },
|
|
|
|
{ NULL, 0, NULL, 0, 0 }
|
2019-02-21 05:55:43 +08:00
|
|
|
};
|
|
|
|
|
2019-03-31 05:10:39 +08:00
|
|
|
static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting)
|
2019-02-21 05:55:43 +08:00
|
|
|
{
|
|
|
|
const char *greeting = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret =
|
2021-06-21 19:08:39 +08:00
|
|
|
TEST_true(ossl_provider_activate(prov, 1, 0))
|
2019-02-21 05:55:43 +08:00
|
|
|
&& TEST_true(ossl_provider_get_params(prov, greeting_request))
|
2019-03-12 04:51:01 +08:00
|
|
|
&& TEST_ptr(greeting = greeting_request[0].data)
|
|
|
|
&& TEST_size_t_gt(greeting_request[0].data_size, 0)
|
2020-12-16 22:15:06 +08:00
|
|
|
&& TEST_str_eq(greeting, expected_greeting)
|
2021-11-05 21:42:40 +08:00
|
|
|
&& TEST_true(ossl_provider_deactivate(prov, 1));
|
2019-02-21 05:55:43 +08:00
|
|
|
|
2019-03-31 05:10:39 +08:00
|
|
|
TEST_info("Got this greeting: %s\n", greeting);
|
2019-02-21 05:55:43 +08:00
|
|
|
ossl_provider_free(prov);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-31 05:10:39 +08:00
|
|
|
static const char *expected_greeting1(const char *name)
|
|
|
|
{
|
|
|
|
static char expected_greeting[256] = "";
|
|
|
|
|
2019-04-03 21:44:17 +08:00
|
|
|
BIO_snprintf(expected_greeting, sizeof(expected_greeting),
|
|
|
|
"Hello OpenSSL %.20s, greetings from %s!",
|
|
|
|
OPENSSL_VERSION_STR, name);
|
2019-03-31 05:10:39 +08:00
|
|
|
|
|
|
|
return expected_greeting;
|
|
|
|
}
|
|
|
|
|
2019-02-21 05:55:43 +08:00
|
|
|
static int test_builtin_provider(void)
|
|
|
|
{
|
|
|
|
const char *name = "p_test_builtin";
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
2021-05-11 18:44:43 +08:00
|
|
|
int ret;
|
2019-02-21 05:55:43 +08:00
|
|
|
|
2021-05-11 18:44:43 +08:00
|
|
|
/*
|
|
|
|
* We set properties that we know the providers we are using don't have.
|
|
|
|
* This should mean that the p_test provider will fail any fetches - which
|
|
|
|
* is something we test inside the provider.
|
|
|
|
*/
|
|
|
|
EVP_set_default_properties(NULL, "fips=yes");
|
|
|
|
|
|
|
|
ret =
|
2019-02-21 05:55:43 +08:00
|
|
|
TEST_ptr(prov =
|
2023-08-03 19:20:33 +08:00
|
|
|
ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, NULL, 0))
|
2019-03-31 05:10:39 +08:00
|
|
|
&& test_provider(prov, expected_greeting1(name));
|
2021-05-11 18:44:43 +08:00
|
|
|
|
|
|
|
EVP_set_default_properties(NULL, "");
|
|
|
|
|
|
|
|
return ret;
|
2019-02-21 05:55:43 +08:00
|
|
|
}
|
|
|
|
|
2019-04-04 18:35:47 +08:00
|
|
|
#ifndef NO_PROVIDER_MODULE
|
2019-02-21 05:55:43 +08:00
|
|
|
static int test_loaded_provider(void)
|
|
|
|
{
|
|
|
|
const char *name = "p_test";
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
|
|
|
|
|
|
|
return
|
2023-08-03 19:20:33 +08:00
|
|
|
TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, NULL, 0))
|
2019-03-31 05:10:39 +08:00
|
|
|
&& test_provider(prov, expected_greeting1(name));
|
|
|
|
}
|
|
|
|
|
2023-08-02 08:44:37 +08:00
|
|
|
# ifndef OPENSSL_NO_AUTOLOAD_CONFIG
|
2019-03-31 05:10:39 +08:00
|
|
|
static int test_configured_provider(void)
|
|
|
|
{
|
|
|
|
const char *name = "p_test_configured";
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
|
|
|
/* This MUST match the config file */
|
|
|
|
const char *expected_greeting =
|
|
|
|
"Hello OpenSSL, greetings from Test Provider";
|
|
|
|
|
|
|
|
return
|
2019-07-30 23:42:53 +08:00
|
|
|
TEST_ptr(prov = ossl_provider_find(NULL, name, 0))
|
2019-03-31 05:10:39 +08:00
|
|
|
&& test_provider(prov, expected_greeting);
|
2019-02-21 05:55:43 +08:00
|
|
|
}
|
2023-08-02 08:44:37 +08:00
|
|
|
# endif
|
2019-02-21 05:55:43 +08:00
|
|
|
#endif
|
|
|
|
|
2021-04-27 13:29:16 +08:00
|
|
|
static int test_cache_flushes(void)
|
|
|
|
{
|
|
|
|
OSSL_LIB_CTX *ctx;
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
|
|
|
EVP_MD *md = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
|
|
|
|
|| !TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default"))
|
|
|
|
|| !TEST_true(OSSL_PROVIDER_available(ctx, "default"))
|
|
|
|
|| !TEST_ptr(md = EVP_MD_fetch(ctx, "SHA256", NULL)))
|
|
|
|
goto err;
|
|
|
|
EVP_MD_free(md);
|
|
|
|
md = NULL;
|
|
|
|
OSSL_PROVIDER_unload(prov);
|
|
|
|
prov = NULL;
|
|
|
|
|
|
|
|
if (!TEST_false(OSSL_PROVIDER_available(ctx, "default")))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!TEST_ptr_null(md = EVP_MD_fetch(ctx, "SHA256", NULL))) {
|
2021-05-28 22:57:22 +08:00
|
|
|
const char *provname = OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(md));
|
2021-04-27 13:29:16 +08:00
|
|
|
|
|
|
|
if (OSSL_PROVIDER_available(NULL, provname))
|
|
|
|
TEST_info("%s provider is available\n", provname);
|
|
|
|
else
|
|
|
|
TEST_info("%s provider is not available\n", provname);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
err:
|
|
|
|
OSSL_PROVIDER_unload(prov);
|
|
|
|
EVP_MD_free(md);
|
|
|
|
OSSL_LIB_CTX_free(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-21 05:55:43 +08:00
|
|
|
int setup_tests(void)
|
|
|
|
{
|
|
|
|
ADD_TEST(test_builtin_provider);
|
2019-04-04 18:35:47 +08:00
|
|
|
#ifndef NO_PROVIDER_MODULE
|
2019-02-21 05:55:43 +08:00
|
|
|
ADD_TEST(test_loaded_provider);
|
2023-08-02 08:44:37 +08:00
|
|
|
# ifndef OPENSSL_NO_AUTOLOAD_CONFIG
|
2019-03-31 05:10:39 +08:00
|
|
|
ADD_TEST(test_configured_provider);
|
2023-08-02 08:44:37 +08:00
|
|
|
# endif
|
2019-02-21 05:55:43 +08:00
|
|
|
#endif
|
2021-04-27 13:29:16 +08:00
|
|
|
ADD_TEST(test_cache_flushes);
|
2019-02-21 05:55:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|