2019-02-21 05:55:43 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2019-2021 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>
|
|
|
|
#include <openssl/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) },
|
|
|
|
{ NULL, 0, NULL, 0, 0 }
|
2019-02-21 05:55:43 +08:00
|
|
|
};
|
|
|
|
|
2021-02-16 18:10:26 +08:00
|
|
|
static int test_provider(OSSL_LIB_CTX **libctx, const char *name)
|
2019-02-21 05:55:43 +08:00
|
|
|
{
|
|
|
|
OSSL_PROVIDER *prov = NULL;
|
|
|
|
const char *greeting = NULL;
|
|
|
|
char expected_greeting[256];
|
2021-02-16 18:10:26 +08:00
|
|
|
int ok = 0;
|
|
|
|
long err;
|
2019-02-21 05:55:43 +08:00
|
|
|
|
2019-03-25 09:37:24 +08:00
|
|
|
BIO_snprintf(expected_greeting, sizeof(expected_greeting),
|
2019-03-26 13:20:22 +08:00
|
|
|
"Hello OpenSSL %.20s, greetings from %s!",
|
|
|
|
OPENSSL_VERSION_STR, name);
|
2019-02-21 05:55:43 +08:00
|
|
|
|
2021-02-16 18:10:26 +08:00
|
|
|
if (!TEST_ptr(prov = OSSL_PROVIDER_load(*libctx, name))
|
|
|
|
|| !TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request))
|
|
|
|
|| !TEST_ptr(greeting = greeting_request[0].data)
|
|
|
|
|| !TEST_size_t_gt(greeting_request[0].data_size, 0)
|
|
|
|
|| !TEST_str_eq(greeting, expected_greeting)
|
|
|
|
|| !TEST_true(OSSL_PROVIDER_unload(prov)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
prov = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We must free the libctx to force the provider to really be unloaded from
|
|
|
|
* memory
|
|
|
|
*/
|
|
|
|
OSSL_LIB_CTX_free(*libctx);
|
|
|
|
*libctx = NULL;
|
|
|
|
|
|
|
|
/* Make sure we got the error we were expecting */
|
|
|
|
err = ERR_peek_last_error();
|
|
|
|
if (!TEST_int_gt(err, 0)
|
|
|
|
|| !TEST_int_eq(ERR_GET_REASON(err), 1))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* We print out all the data to make sure it can still be accessed */
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
ok = 1;
|
|
|
|
err:
|
|
|
|
OSSL_PROVIDER_unload(prov);
|
|
|
|
OSSL_LIB_CTX_free(*libctx);
|
|
|
|
*libctx = NULL;
|
|
|
|
return ok;
|
2019-02-21 05:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_builtin_provider(void)
|
|
|
|
{
|
2021-02-16 18:10:26 +08:00
|
|
|
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
|
2019-02-21 05:55:43 +08:00
|
|
|
const char *name = "p_test_builtin";
|
2021-02-16 18:10:26 +08:00
|
|
|
int ok;
|
2019-02-21 05:55:43 +08:00
|
|
|
|
2021-02-16 18:10:26 +08:00
|
|
|
ok =
|
|
|
|
TEST_ptr(libctx)
|
|
|
|
&& TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
|
|
|
|
PROVIDER_INIT_FUNCTION_NAME))
|
|
|
|
&& test_provider(&libctx, name);
|
|
|
|
|
|
|
|
OSSL_LIB_CTX_free(libctx);
|
|
|
|
|
|
|
|
return ok;
|
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)
|
|
|
|
{
|
2021-02-16 18:10:26 +08:00
|
|
|
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
|
2019-02-21 05:55:43 +08:00
|
|
|
const char *name = "p_test";
|
|
|
|
|
2021-02-16 18:10:26 +08:00
|
|
|
if (!TEST_ptr(libctx))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* test_provider will free libctx as part of the test */
|
|
|
|
return test_provider(&libctx, name);
|
2019-02-21 05:55:43 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|