provider.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2019-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 <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(OSSL_LIB_CTX *libctx, const char *name,
  17. 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, 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_load(OSSL_LIB_CTX *libctx, const char *name)
  46. {
  47. /* Any attempt to load a provider disables auto-loading of defaults */
  48. if (ossl_provider_disable_fallback_loading(libctx))
  49. return OSSL_PROVIDER_try_load(libctx, name, 0);
  50. return NULL;
  51. }
  52. int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
  53. {
  54. if (!ossl_provider_deactivate(prov, 1))
  55. return 0;
  56. ossl_provider_free(prov);
  57. return 1;
  58. }
  59. const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
  60. {
  61. return ossl_provider_gettable_params(prov);
  62. }
  63. int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  64. {
  65. return ossl_provider_get_params(prov, params);
  66. }
  67. const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov,
  68. int operation_id,
  69. int *no_cache)
  70. {
  71. return ossl_provider_query_operation(prov, operation_id, no_cache);
  72. }
  73. void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov,
  74. int operation_id,
  75. const OSSL_ALGORITHM *algs)
  76. {
  77. ossl_provider_unquery_operation(prov, operation_id, algs);
  78. }
  79. void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov)
  80. {
  81. return ossl_provider_prov_ctx(prov);
  82. }
  83. const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov)
  84. {
  85. return ossl_provider_get0_dispatch(prov);
  86. }
  87. int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
  88. {
  89. return ossl_provider_self_test(prov);
  90. }
  91. int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
  92. const char *capability,
  93. OSSL_CALLBACK *cb,
  94. void *arg)
  95. {
  96. return ossl_provider_get_capabilities(prov, capability, cb, arg);
  97. }
  98. int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
  99. OSSL_provider_init_fn *init_fn)
  100. {
  101. OSSL_PROVIDER_INFO entry;
  102. if (name == NULL || init_fn == NULL) {
  103. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  104. return 0;
  105. }
  106. memset(&entry, 0, sizeof(entry));
  107. entry.name = OPENSSL_strdup(name);
  108. if (entry.name == NULL)
  109. return 0;
  110. entry.init = init_fn;
  111. if (!ossl_provider_info_add_to_store(libctx, &entry)) {
  112. ossl_provider_info_clear(&entry);
  113. return 0;
  114. }
  115. return 1;
  116. }
  117. const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov)
  118. {
  119. return ossl_provider_name(prov);
  120. }
  121. int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
  122. int (*cb)(OSSL_PROVIDER *provider,
  123. void *cbdata),
  124. void *cbdata)
  125. {
  126. return ossl_provider_doall_activated(ctx, cb, cbdata);
  127. }