provider.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright 2019-2023 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 <string.h>
  10. #include <openssl/err.h>
  11. #include <openssl/cryptoerr.h>
  12. #include <openssl/provider.h>
  13. #include <openssl/core_names.h>
  14. #include "internal/provider.h"
  15. #include "provider_local.h"
  16. OSSL_PROVIDER *OSSL_PROVIDER_try_load_ex(OSSL_LIB_CTX *libctx, const char *name,
  17. OSSL_PARAM *params, int retain_fallbacks)
  18. {
  19. OSSL_PROVIDER *prov = NULL, *actual;
  20. int isnew = 0;
  21. /* Find it or create it */
  22. if ((prov = ossl_provider_find(libctx, name, 0)) == NULL) {
  23. if ((prov = ossl_provider_new(libctx, name, NULL, params, 0)) == NULL)
  24. return NULL;
  25. isnew = 1;
  26. }
  27. if (!ossl_provider_activate(prov, 1, 0)) {
  28. ossl_provider_free(prov);
  29. return NULL;
  30. }
  31. actual = prov;
  32. if (isnew && !ossl_provider_add_to_store(prov, &actual, retain_fallbacks)) {
  33. ossl_provider_deactivate(prov, 1);
  34. ossl_provider_free(prov);
  35. return NULL;
  36. }
  37. if (actual != prov) {
  38. if (!ossl_provider_activate(actual, 1, 0)) {
  39. ossl_provider_free(actual);
  40. return NULL;
  41. }
  42. }
  43. return actual;
  44. }
  45. OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name,
  46. int retain_fallbacks)
  47. {
  48. return OSSL_PROVIDER_try_load_ex(libctx, name, NULL, retain_fallbacks);
  49. }
  50. OSSL_PROVIDER *OSSL_PROVIDER_load_ex(OSSL_LIB_CTX *libctx, const char *name, OSSL_PARAM *params)
  51. {
  52. /* Any attempt to load a provider disables auto-loading of defaults */
  53. if (ossl_provider_disable_fallback_loading(libctx))
  54. return OSSL_PROVIDER_try_load_ex(libctx, name, params, 0);
  55. return NULL;
  56. }
  57. OSSL_PROVIDER *OSSL_PROVIDER_load(OSSL_LIB_CTX *libctx, const char *name)
  58. {
  59. return OSSL_PROVIDER_load_ex(libctx, name, NULL);
  60. }
  61. int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
  62. {
  63. if (!ossl_provider_deactivate(prov, 1))
  64. return 0;
  65. ossl_provider_free(prov);
  66. return 1;
  67. }
  68. const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
  69. {
  70. return ossl_provider_gettable_params(prov);
  71. }
  72. int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  73. {
  74. return ossl_provider_get_params(prov, params);
  75. }
  76. const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov,
  77. int operation_id,
  78. int *no_cache)
  79. {
  80. return ossl_provider_query_operation(prov, operation_id, no_cache);
  81. }
  82. void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov,
  83. int operation_id,
  84. const OSSL_ALGORITHM *algs)
  85. {
  86. ossl_provider_unquery_operation(prov, operation_id, algs);
  87. }
  88. void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov)
  89. {
  90. return ossl_provider_prov_ctx(prov);
  91. }
  92. const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov)
  93. {
  94. return ossl_provider_get0_dispatch(prov);
  95. }
  96. int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
  97. {
  98. return ossl_provider_self_test(prov);
  99. }
  100. int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
  101. const char *capability,
  102. OSSL_CALLBACK *cb,
  103. void *arg)
  104. {
  105. return ossl_provider_get_capabilities(prov, capability, cb, arg);
  106. }
  107. int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
  108. OSSL_provider_init_fn *init_fn)
  109. {
  110. OSSL_PROVIDER_INFO entry;
  111. if (name == NULL || init_fn == NULL) {
  112. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  113. return 0;
  114. }
  115. memset(&entry, 0, sizeof(entry));
  116. entry.name = OPENSSL_strdup(name);
  117. if (entry.name == NULL)
  118. return 0;
  119. entry.init = init_fn;
  120. if (!ossl_provider_info_add_to_store(libctx, &entry)) {
  121. ossl_provider_info_clear(&entry);
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov)
  127. {
  128. return ossl_provider_name(prov);
  129. }
  130. int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
  131. int (*cb)(OSSL_PROVIDER *provider,
  132. void *cbdata),
  133. void *cbdata)
  134. {
  135. return ossl_provider_doall_activated(ctx, cb, cbdata);
  136. }