provider.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <openssl/err.h>
  10. #include <openssl/cryptoerr.h>
  11. #include <openssl/provider.h>
  12. #include <openssl/core_names.h>
  13. #include "internal/provider.h"
  14. OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name,
  15. int retain_fallbacks)
  16. {
  17. OSSL_PROVIDER *prov = NULL;
  18. /* Find it or create it */
  19. if ((prov = ossl_provider_find(libctx, name, 0)) == NULL
  20. && (prov = ossl_provider_new(libctx, name, NULL, 0)) == NULL)
  21. return NULL;
  22. if (!ossl_provider_activate(prov, retain_fallbacks, 1)) {
  23. ossl_provider_free(prov);
  24. return NULL;
  25. }
  26. return prov;
  27. }
  28. OSSL_PROVIDER *OSSL_PROVIDER_load(OSSL_LIB_CTX *libctx, const char *name)
  29. {
  30. /* Any attempt to load a provider disables auto-loading of defaults */
  31. if (ossl_provider_disable_fallback_loading(libctx))
  32. return OSSL_PROVIDER_try_load(libctx, name, 0);
  33. return NULL;
  34. }
  35. int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
  36. {
  37. if (!ossl_provider_deactivate(prov))
  38. return 0;
  39. ossl_provider_free(prov);
  40. return 1;
  41. }
  42. int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
  43. {
  44. OSSL_PROVIDER *prov = NULL;
  45. int available = 0;
  46. /* Find it or create it */
  47. prov = ossl_provider_find(libctx, name, 0);
  48. available = ossl_provider_available(prov);
  49. ossl_provider_free(prov);
  50. return available;
  51. }
  52. const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
  53. {
  54. return ossl_provider_gettable_params(prov);
  55. }
  56. int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  57. {
  58. return ossl_provider_get_params(prov, params);
  59. }
  60. const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov,
  61. int operation_id,
  62. int *no_cache)
  63. {
  64. return ossl_provider_query_operation(prov, operation_id, no_cache);
  65. }
  66. void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov,
  67. int operation_id,
  68. const OSSL_ALGORITHM *algs)
  69. {
  70. ossl_provider_unquery_operation(prov, operation_id, algs);
  71. }
  72. void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov)
  73. {
  74. return ossl_provider_prov_ctx(prov);
  75. }
  76. const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov)
  77. {
  78. return ossl_provider_get0_dispatch(prov);
  79. }
  80. int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
  81. {
  82. return ossl_provider_self_test(prov);
  83. }
  84. int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
  85. const char *capability,
  86. OSSL_CALLBACK *cb,
  87. void *arg)
  88. {
  89. return ossl_provider_get_capabilities(prov, capability, cb, arg);
  90. }
  91. int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
  92. OSSL_provider_init_fn *init_fn)
  93. {
  94. OSSL_PROVIDER *prov = NULL;
  95. if (name == NULL || init_fn == NULL) {
  96. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  97. return 0;
  98. }
  99. /* Create it */
  100. if ((prov = ossl_provider_new(libctx, name, init_fn, 0)) == NULL)
  101. return 0;
  102. /*
  103. * It's safely stored in the internal store at this point,
  104. * free the returned extra reference
  105. */
  106. ossl_provider_free(prov);
  107. return 1;
  108. }
  109. const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov)
  110. {
  111. return ossl_provider_name(prov);
  112. }
  113. int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
  114. int (*cb)(OSSL_PROVIDER *provider,
  115. void *cbdata),
  116. void *cbdata)
  117. {
  118. return ossl_provider_doall_activated(ctx, cb, cbdata);
  119. }