2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/provider.h>
14 extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
17 static OSSL_PARAM greeting_request[] = {
18 { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf) },
19 { NULL, 0, NULL, 0, 0 }
22 static unsigned int digestsuccess = 0;
23 static OSSL_PARAM digest_check[] = {
24 { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, &digestsuccess,
25 sizeof(digestsuccess) },
26 { NULL, 0, NULL, 0, 0 }
29 static int test_provider(OSSL_LIB_CTX **libctx, const char *name,
30 OSSL_PROVIDER *legacy)
32 OSSL_PROVIDER *prov = NULL;
33 const char *greeting = NULL;
34 char expected_greeting[256];
37 int dolegacycheck = (legacy != NULL);
38 OSSL_PROVIDER *deflt = NULL, *base = NULL;
40 BIO_snprintf(expected_greeting, sizeof(expected_greeting),
41 "Hello OpenSSL %.20s, greetings from %s!",
42 OPENSSL_VERSION_STR, name);
46 * We set properties that we know the providers we are using don't have.
47 * This should mean that the p_test provider will fail any fetches - which
48 * is something we test inside the provider.
50 EVP_set_default_properties(*libctx, "fips=yes");
52 * Check that it is possible to have a built-in provider mirrored in
55 if (!TEST_ptr(base = OSSL_PROVIDER_load(*libctx, "base")))
57 if (!TEST_ptr(prov = OSSL_PROVIDER_load(*libctx, name)))
61 * Once the provider is loaded we clear the default properties and fetches
62 * should start working again.
64 EVP_set_default_properties(*libctx, "");
66 if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
67 || !TEST_true(digestsuccess))
70 if (!TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request))
71 || !TEST_ptr(greeting = greeting_request[0].data)
72 || !TEST_size_t_gt(greeting_request[0].data_size, 0)
73 || !TEST_str_eq(greeting, expected_greeting))
76 /* Make sure we got the error we were expecting */
77 err = ERR_peek_last_error();
78 if (!TEST_int_gt(err, 0)
79 || !TEST_int_eq(ERR_GET_REASON(err), 1))
82 OSSL_PROVIDER_unload(legacy);
86 /* Legacy provider should also be unloaded from child libctx */
87 if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
88 || !TEST_false(digestsuccess))
91 * Loading the legacy provider again should make it available again in
92 * the child libctx. Loading and unloading the default provider should
93 * have no impact on the child because the child loads it explicitly
96 legacy = OSSL_PROVIDER_load(*libctx, "legacy");
97 deflt = OSSL_PROVIDER_load(*libctx, "default");
99 || !TEST_true(OSSL_PROVIDER_available(*libctx, "default")))
101 OSSL_PROVIDER_unload(deflt);
103 if (!TEST_ptr(legacy)
104 || !TEST_false(OSSL_PROVIDER_available(*libctx, "default"))
105 || !TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
106 || !TEST_true(digestsuccess))
108 OSSL_PROVIDER_unload(legacy);
112 if (!TEST_true(OSSL_PROVIDER_unload(base)))
115 if (!TEST_true(OSSL_PROVIDER_unload(prov)))
120 * We must free the libctx to force the provider to really be unloaded from
123 OSSL_LIB_CTX_free(*libctx);
126 /* We print out all the data to make sure it can still be accessed */
127 ERR_print_errors_fp(stderr);
130 OSSL_PROVIDER_unload(base);
131 OSSL_PROVIDER_unload(deflt);
132 OSSL_PROVIDER_unload(legacy);
134 OSSL_PROVIDER_unload(prov);
135 OSSL_LIB_CTX_free(*libctx);
140 static int test_builtin_provider(void)
142 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
143 const char *name = "p_test_builtin";
148 && TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
149 PROVIDER_INIT_FUNCTION_NAME))
150 && test_provider(&libctx, name, NULL);
152 OSSL_LIB_CTX_free(libctx);
157 /* Test relies on fetching the MD4 digest from the legacy provider */
158 #ifndef OPENSSL_NO_MD4
159 static int test_builtin_provider_with_child(void)
161 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
162 const char *name = "p_test";
163 OSSL_PROVIDER *legacy;
165 if (!TEST_ptr(libctx))
168 legacy = OSSL_PROVIDER_load(libctx, "legacy");
169 if (legacy == NULL) {
171 * In this case we assume we've been built with "no-legacy" and skip
172 * this test (there is no OPENSSL_NO_LEGACY)
177 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
178 PROVIDER_INIT_FUNCTION_NAME)))
181 /* test_provider will free libctx and unload legacy as part of the test */
182 return test_provider(&libctx, name, legacy);
186 #ifndef NO_PROVIDER_MODULE
187 static int test_loaded_provider(void)
189 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
190 const char *name = "p_test";
192 if (!TEST_ptr(libctx))
195 /* test_provider will free libctx as part of the test */
196 return test_provider(&libctx, name, NULL);
200 typedef enum OPTION_choice {
207 const OPTIONS *test_get_options(void)
209 static const OPTIONS test_options[] = {
210 OPT_TEST_OPTIONS_DEFAULT_USAGE,
211 { "loaded", OPT_LOADED, '-', "Run test with a loaded provider" },
217 int setup_tests(void)
222 while ((o = opt_next()) != OPT_EOF) {
235 ADD_TEST(test_builtin_provider);
236 #ifndef OPENSSL_NO_MD4
237 ADD_TEST(test_builtin_provider_with_child);
240 #ifndef NO_PROVIDER_MODULE
242 ADD_TEST(test_loaded_provider);