1 /* 2 * Copyright 2020-2026 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 "apps.h" 11 #include <ctype.h> 12 #include <string.h> 13 #include <openssl/err.h> 14 #include <openssl/provider.h> 15 #include <openssl/safestack.h> 16 17 /* Non-zero if any of the provider options have been seen */ 18 static int provider_option_given = 0; 19 20 DEFINE_STACK_OF(OSSL_PROVIDER) 21 22 /* 23 * See comments in opt_verify for explanation of this. 24 */ 25 enum prov_range { OPT_PROV_ENUM }; 26 27 static STACK_OF(OSSL_PROVIDER) *app_providers = NULL; 28 29 static void provider_free(OSSL_PROVIDER *prov) 30 { 31 OSSL_PROVIDER_unload(prov); 32 } 33 34 int app_provider_load(OSSL_LIB_CTX *libctx, const char *provider_name) 35 { 36 OSSL_PROVIDER *prov; 37 38 prov = OSSL_PROVIDER_load(libctx, provider_name); 39 if (prov == NULL) { 40 opt_printf_stderr("%s: unable to load provider %s\n" 41 "Hint: use -provider-path option or OPENSSL_MODULES environment variable.\n", 42 opt_getprog(), provider_name); 43 ERR_print_errors(bio_err); 44 return 0; 45 } 46 if (app_providers == NULL) 47 app_providers = sk_OSSL_PROVIDER_new_null(); 48 if (app_providers == NULL 49 || !sk_OSSL_PROVIDER_push(app_providers, prov)) { 50 OSSL_PROVIDER_unload(prov); 51 app_providers_cleanup(); 52 return 0; 53 } 54 return 1; 55 } 56 57 void app_providers_cleanup(void) 58 { 59 sk_OSSL_PROVIDER_pop_free(app_providers, provider_free); 60 app_providers = NULL; 61 } 62 63 static int opt_provider_path(const char *path) 64 { 65 if (path != NULL && *path == '\0') 66 path = NULL; 67 return OSSL_PROVIDER_set_default_search_path(app_get0_libctx(), path); 68 } 69 70 struct prov_param_st { 71 char *name; 72 char *key; 73 char *val; 74 int found; 75 }; 76 77 static int set_prov_param(OSSL_PROVIDER *prov, void *vp) 78 { 79 struct prov_param_st *p = (struct prov_param_st *)vp; 80 81 if (p->name != NULL && strcmp(OSSL_PROVIDER_get0_name(prov), p->name) != 0) 82 return 1; 83 p->found = 1; 84 return OSSL_PROVIDER_add_conf_parameter(prov, p->key, p->val); 85 } 86 87 static int opt_provider_param(const char *arg) 88 { 89 struct prov_param_st p; 90 char *copy, *tmp; 91 int ret = 0; 92 93 if ((copy = OPENSSL_strdup(arg)) == NULL 94 || (p.val = strchr(copy, '=')) == NULL) { 95 opt_printf_stderr("%s: malformed '-provparam' option value: '%s'\n", 96 opt_getprog(), arg); 97 goto end; 98 } 99 100 /* Drop whitespace on both sides of the '=' sign */ 101 *(tmp = p.val++) = '\0'; 102 while (tmp > copy && isspace(_UC(*--tmp))) 103 *tmp = '\0'; 104 while (isspace(_UC(*p.val))) 105 ++p.val; 106 107 /* 108 * Split the key on ':', to get the optional provider, empty or missing 109 * means all. 110 */ 111 if ((p.key = strchr(copy, ':')) != NULL) { 112 *p.key++ = '\0'; 113 p.name = *copy != '\0' ? copy : NULL; 114 } else { 115 p.name = NULL; 116 p.key = copy; 117 } 118 119 /* The key must not be empty */ 120 if (*p.key == '\0') { 121 opt_printf_stderr("%s: malformed '-provparam' option value: '%s'\n", 122 opt_getprog(), arg); 123 goto end; 124 } 125 126 p.found = 0; 127 ret = OSSL_PROVIDER_do_all(app_get0_libctx(), set_prov_param, (void *)&p); 128 if (ret == 0) { 129 opt_printf_stderr("%s: Error setting provider '%s' parameter '%s'\n", 130 opt_getprog(), p.name, p.key); 131 } else if (p.found == 0) { 132 opt_printf_stderr("%s: No provider named '%s' is loaded\n", 133 opt_getprog(), p.name); 134 ret = 0; 135 } 136 137 end: 138 OPENSSL_free(copy); 139 return ret; 140 } 141 142 int opt_provider(int opt) 143 { 144 const int given = provider_option_given; 145 146 provider_option_given = 1; 147 switch ((enum prov_range)opt) { 148 case OPT_PROV__FIRST: 149 case OPT_PROV__LAST: 150 return 1; 151 case OPT_PROV_PROVIDER: 152 return app_provider_load(app_get0_libctx(), opt_arg()); 153 case OPT_PROV_PROVIDER_PATH: 154 return opt_provider_path(opt_arg()); 155 case OPT_PROV_PARAM: 156 return opt_provider_param(opt_arg()); 157 case OPT_PROV_PROPQUERY: 158 return app_set_propq(opt_arg()); 159 } 160 /* Should never get here but if we do, undo what we did earlier */ 161 provider_option_given = given; 162 return 0; 163 } 164 165 int opt_provider_option_given(void) 166 { 167 return provider_option_given; 168 } 169