1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* 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 <openssl/param_build.h>
#include "testutil.h"
extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
static char buf[256];
static OSSL_PARAM greeting_request[] = {
{ "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf) },
{ NULL, 0, NULL, 0, 0 }
};
static unsigned int digestsuccess = 0;
static OSSL_PARAM digest_check[] = {
{ "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, &digestsuccess,
sizeof(digestsuccess) },
{ NULL, 0, NULL, 0, 0 }
};
static unsigned int stopsuccess = 0;
static OSSL_PARAM stop_property_mirror[] = {
{ "stop-property-mirror", OSSL_PARAM_UNSIGNED_INTEGER, &stopsuccess,
sizeof(stopsuccess) },
{ NULL, 0, NULL, 0, 0 }
};
static int test_provider(OSSL_LIB_CTX **libctx, const char *name,
OSSL_PROVIDER *legacy)
{
OSSL_PROVIDER *prov = NULL;
const char *greeting = NULL;
char expected_greeting[256];
int ok = 0;
long err;
int dolegacycheck = (legacy != NULL);
OSSL_PROVIDER *deflt = NULL, *base = NULL;
BIO_snprintf(expected_greeting, sizeof(expected_greeting),
"Hello OpenSSL %.20s, greetings from %s!",
OPENSSL_VERSION_STR, name);
/*
* 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(*libctx, "fips=yes");
/*
* Check that it is possible to have a built-in provider mirrored in
* a child lib ctx.
*/
if (!TEST_ptr(base = OSSL_PROVIDER_load(*libctx, "base")))
goto err;
if (!TEST_ptr(prov = OSSL_PROVIDER_load(*libctx, name)))
goto err;
/*
* Once the provider is loaded we clear the default properties and fetches
* should start working again.
*/
EVP_set_default_properties(*libctx, "");
if (dolegacycheck) {
if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
|| !TEST_true(digestsuccess))
goto err;
/*
* Check that a provider can prevent property mirroring if it sets its
* own properties explicitly
*/
if (!TEST_true(OSSL_PROVIDER_get_params(prov, stop_property_mirror))
|| !TEST_true(stopsuccess))
goto err;
EVP_set_default_properties(*libctx, "fips=yes");
if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
|| !TEST_true(digestsuccess))
goto err;
EVP_set_default_properties(*libctx, "");
}
if (!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))
goto err;
/* 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;
OSSL_PROVIDER_unload(legacy);
legacy = NULL;
if (dolegacycheck) {
/* Legacy provider should also be unloaded from child libctx */
if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
|| !TEST_false(digestsuccess))
goto err;
/*
* Loading the legacy provider again should make it available again in
* the child libctx. Loading and unloading the default provider should
* have no impact on the child because the child loads it explicitly
* before this point.
*/
legacy = OSSL_PROVIDER_load(*libctx, "legacy");
deflt = OSSL_PROVIDER_load(*libctx, "default");
if (!TEST_ptr(deflt)
|| !TEST_true(OSSL_PROVIDER_available(*libctx, "default")))
goto err;
OSSL_PROVIDER_unload(deflt);
deflt = NULL;
if (!TEST_ptr(legacy)
|| !TEST_false(OSSL_PROVIDER_available(*libctx, "default"))
|| !TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
|| !TEST_true(digestsuccess))
goto err;
OSSL_PROVIDER_unload(legacy);
legacy = NULL;
}
if (!TEST_true(OSSL_PROVIDER_unload(base)))
goto err;
base = NULL;
if (!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;
/* 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(base);
OSSL_PROVIDER_unload(deflt);
OSSL_PROVIDER_unload(legacy);
legacy = NULL;
OSSL_PROVIDER_unload(prov);
OSSL_LIB_CTX_free(*libctx);
*libctx = NULL;
return ok;
}
#ifndef NO_PROVIDER_MODULE
static int test_provider_ex(OSSL_LIB_CTX **libctx, const char *name)
{
OSSL_PROVIDER *prov = NULL;
const char *greeting = NULL;
int ok = 0;
long err;
const char custom_buf[] = "Custom greeting";
OSSL_PARAM_BLD *bld = NULL;
OSSL_PARAM *params = NULL;
if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
|| !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "greeting", custom_buf,
strlen(custom_buf)))
|| !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) {
goto err;
}
if (!TEST_ptr(prov = OSSL_PROVIDER_load_ex(*libctx, name, params)))
goto err;
if (!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, custom_buf))
goto err;
/* 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;
if (!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;
/* We print out all the data to make sure it can still be accessed */
ERR_print_errors_fp(stderr);
ok = 1;
err:
OSSL_PARAM_BLD_free(bld);
OSSL_PARAM_free(params);
OSSL_PROVIDER_unload(prov);
OSSL_LIB_CTX_free(*libctx);
*libctx = NULL;
return ok;
}
#endif
static int test_builtin_provider(void)
{
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
const char *name = "p_test_builtin";
int ok;
ok =
TEST_ptr(libctx)
&& TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
PROVIDER_INIT_FUNCTION_NAME))
&& test_provider(&libctx, name, NULL);
OSSL_LIB_CTX_free(libctx);
return ok;
}
/* Test relies on fetching the MD4 digest from the legacy provider */
#ifndef OPENSSL_NO_MD4
static int test_builtin_provider_with_child(void)
{
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
const char *name = "p_test";
OSSL_PROVIDER *legacy;
if (!TEST_ptr(libctx))
return 0;
legacy = OSSL_PROVIDER_load(libctx, "legacy");
if (legacy == NULL) {
/*
* In this case we assume we've been built with "no-legacy" and skip
* this test (there is no OPENSSL_NO_LEGACY)
*/
OSSL_LIB_CTX_free(libctx);
return 1;
}
if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
PROVIDER_INIT_FUNCTION_NAME))) {
OSSL_PROVIDER_unload(legacy);
OSSL_LIB_CTX_free(libctx);
return 0;
}
/* test_provider will free libctx and unload legacy as part of the test */
return test_provider(&libctx, name, legacy);
}
#endif
#ifndef NO_PROVIDER_MODULE
static int test_loaded_provider(void)
{
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
const char *name = "p_test";
int res = 0;
if (!TEST_ptr(libctx))
return 0;
/* test_provider will free libctx as part of the test */
res = test_provider(&libctx, name, NULL);
libctx = OSSL_LIB_CTX_new();
if (!TEST_ptr(libctx))
return 0;
/* test_provider_ex will free libctx as part of the test */
res = res && test_provider_ex(&libctx, name);
return res;
}
#endif
typedef enum OPTION_choice {
OPT_ERR = -1,
OPT_EOF = 0,
OPT_LOADED,
OPT_TEST_ENUM
} OPTION_CHOICE;
const OPTIONS *test_get_options(void)
{
static const OPTIONS test_options[] = {
OPT_TEST_OPTIONS_DEFAULT_USAGE,
{ "loaded", OPT_LOADED, '-', "Run test with a loaded provider" },
{ NULL }
};
return test_options;
}
int setup_tests(void)
{
OPTION_CHOICE o;
int loaded = 0;
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_TEST_CASES:
break;
case OPT_LOADED:
loaded = 1;
break;
default:
return 0;
}
}
if (!loaded) {
ADD_TEST(test_builtin_provider);
#ifndef OPENSSL_NO_MD4
ADD_TEST(test_builtin_provider_with_child);
#endif
}
#ifndef NO_PROVIDER_MODULE
else {
ADD_TEST(test_loaded_provider);
}
#endif
return 1;
}
|