app_provider.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2020-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. #include "apps.h"
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/provider.h>
  13. #include <openssl/safestack.h>
  14. DEFINE_STACK_OF(OSSL_PROVIDER)
  15. /*
  16. * See comments in opt_verify for explanation of this.
  17. */
  18. enum prov_range { OPT_PROV_ENUM };
  19. static STACK_OF(OSSL_PROVIDER) *app_providers = NULL;
  20. static void provider_free(OSSL_PROVIDER *prov)
  21. {
  22. OSSL_PROVIDER_unload(prov);
  23. }
  24. int app_provider_load(OSSL_LIB_CTX *libctx, const char *provider_name)
  25. {
  26. OSSL_PROVIDER *prov;
  27. prov = OSSL_PROVIDER_load(libctx, provider_name);
  28. if (prov == NULL) {
  29. opt_printf_stderr("%s: unable to load provider %s\n"
  30. "Hint: use -provider-path option or OPENSSL_MODULES environment variable.\n",
  31. opt_getprog(), provider_name);
  32. ERR_print_errors(bio_err);
  33. return 0;
  34. }
  35. if (app_providers == NULL)
  36. app_providers = sk_OSSL_PROVIDER_new_null();
  37. if (app_providers == NULL
  38. || !sk_OSSL_PROVIDER_push(app_providers, prov)) {
  39. app_providers_cleanup();
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. void app_providers_cleanup(void)
  45. {
  46. sk_OSSL_PROVIDER_pop_free(app_providers, provider_free);
  47. app_providers = NULL;
  48. }
  49. static int opt_provider_path(const char *path)
  50. {
  51. if (path != NULL && *path == '\0')
  52. path = NULL;
  53. return OSSL_PROVIDER_set_default_search_path(app_get0_libctx(), path);
  54. }
  55. int opt_provider(int opt)
  56. {
  57. switch ((enum prov_range)opt) {
  58. case OPT_PROV__FIRST:
  59. case OPT_PROV__LAST:
  60. return 1;
  61. case OPT_PROV_PROVIDER:
  62. return app_provider_load(app_get0_libctx(), opt_arg());
  63. case OPT_PROV_PROVIDER_PATH:
  64. return opt_provider_path(opt_arg());
  65. case OPT_PROV_PROPQUERY:
  66. return app_set_propq(opt_arg());
  67. }
  68. return 0;
  69. }