app_provider.c 4.4 KB

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