1 /*
2  * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 #include "../testutil.h"
11 #include <openssl/provider.h>
12 #include <string.h>
13 
test_get_libctx(OSSL_LIB_CTX ** libctx,OSSL_PROVIDER ** default_null_prov,const char * config_file,OSSL_PROVIDER ** provider,const char * module_name)14 int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
15                     const char *config_file,
16                     OSSL_PROVIDER **provider, const char *module_name)
17 {
18     OSSL_LIB_CTX *new_libctx = NULL;
19 
20     if (libctx != NULL) {
21         if ((new_libctx = *libctx = OSSL_LIB_CTX_new()) == NULL) {
22             opt_printf_stderr("Failed to create libctx\n");
23             goto err;
24         }
25     }
26 
27     if (default_null_prov != NULL
28         && (*default_null_prov = OSSL_PROVIDER_load(NULL, "null")) == NULL) {
29         opt_printf_stderr("Failed to load null provider into default libctx\n");
30         goto err;
31     }
32 
33     if (config_file != NULL
34             && !OSSL_LIB_CTX_load_config(new_libctx, config_file)) {
35         opt_printf_stderr("Error loading config from file %s\n", config_file);
36         goto err;
37     }
38 
39     if (module_name != NULL
40             && (*provider = OSSL_PROVIDER_load(new_libctx, module_name)) == NULL) {
41         opt_printf_stderr("Failed to load provider %s\n", module_name);
42         goto err;
43     }
44     return 1;
45 
46  err:
47     ERR_print_errors_fp(stderr);
48     return 0;
49 }
50 
test_arg_libctx(OSSL_LIB_CTX ** libctx,OSSL_PROVIDER ** default_null_prov,OSSL_PROVIDER ** provider,int argn,const char * usage)51 int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
52                     OSSL_PROVIDER **provider, int argn, const char *usage)
53 {
54     const char *module_name;
55 
56     if (!TEST_ptr(module_name = test_get_argument(argn))) {
57         TEST_error("usage: <prog> %s", usage);
58         return 0;
59     }
60     if (strcmp(module_name, "none") == 0)
61         return 1;
62     return test_get_libctx(libctx, default_null_prov,
63                            test_get_argument(argn + 1), provider, module_name);
64 }
65