provider.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)) {
  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. int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
  77. {
  78. return ossl_provider_self_test(prov);
  79. }
  80. int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
  81. const char *capability,
  82. OSSL_CALLBACK *cb,
  83. void *arg)
  84. {
  85. return ossl_provider_get_capabilities(prov, capability, cb, arg);
  86. }
  87. int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
  88. OSSL_provider_init_fn *init_fn)
  89. {
  90. OSSL_PROVIDER *prov = NULL;
  91. if (name == NULL || init_fn == NULL) {
  92. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  93. return 0;
  94. }
  95. /* Create it */
  96. if ((prov = ossl_provider_new(libctx, name, init_fn, 0)) == NULL)
  97. return 0;
  98. /*
  99. * It's safely stored in the internal store at this point,
  100. * free the returned extra reference
  101. */
  102. ossl_provider_free(prov);
  103. return 1;
  104. }
  105. const char *OSSL_PROVIDER_name(const OSSL_PROVIDER *prov)
  106. {
  107. return ossl_provider_name(prov);
  108. }
  109. int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
  110. int (*cb)(OSSL_PROVIDER *provider,
  111. void *cbdata),
  112. void *cbdata)
  113. {
  114. return ossl_provider_doall_activated(ctx, cb, cbdata);
  115. }