core_fetch.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2019-2025 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 <stddef.h>
  10. #include <openssl/core.h>
  11. #include <openssl/trace.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/core.h"
  14. #include "internal/property.h"
  15. #include "internal/provider.h"
  16. struct construct_data_st {
  17. OSSL_LIB_CTX *libctx;
  18. OSSL_METHOD_STORE *store;
  19. int operation_id;
  20. int force_store;
  21. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  22. void *mcm_data;
  23. };
  24. static int is_temporary_method_store(int no_store, void *cbdata)
  25. {
  26. struct construct_data_st *data = cbdata;
  27. return no_store && !data->force_store;
  28. }
  29. static int ossl_method_construct_reserve_store(int no_store, void *cbdata)
  30. {
  31. struct construct_data_st *data = cbdata;
  32. if (is_temporary_method_store(no_store, data) && data->store == NULL) {
  33. /*
  34. * If we have been told not to store the method "permanently", we
  35. * ask for a temporary store, and store the method there.
  36. * The owner of |data->mcm| is completely responsible for managing
  37. * that temporary store.
  38. */
  39. if ((data->store = data->mcm->get_tmp_store(data->mcm_data)) == NULL)
  40. return 0;
  41. }
  42. return data->mcm->lock_store(data->store, data->mcm_data);
  43. }
  44. static int ossl_method_construct_unreserve_store(void *cbdata)
  45. {
  46. struct construct_data_st *data = cbdata;
  47. return data->mcm->unlock_store(data->store, data->mcm_data);
  48. }
  49. static int ossl_method_construct_precondition(OSSL_PROVIDER *provider,
  50. int operation_id, int no_store,
  51. void *cbdata, int *result)
  52. {
  53. if (!ossl_assert(result != NULL)) {
  54. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  55. return 0;
  56. }
  57. /* Assume that no bits are set */
  58. *result = 0;
  59. /* No flag bits for temporary stores */
  60. if (!is_temporary_method_store(no_store, cbdata)
  61. && !ossl_provider_test_operation_bit(provider, operation_id, result))
  62. return 0;
  63. /*
  64. * The result we get tells if methods have already been constructed.
  65. * However, we want to tell whether construction should happen (true)
  66. * or not (false), which is the opposite of what we got.
  67. */
  68. *result = !*result;
  69. return 1;
  70. }
  71. static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider,
  72. int operation_id, int no_store,
  73. void *cbdata, int *result)
  74. {
  75. if (!ossl_assert(result != NULL)) {
  76. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  77. return 0;
  78. }
  79. *result = 1;
  80. /* No flag bits for temporary stores */
  81. return is_temporary_method_store(no_store, cbdata)
  82. || ossl_provider_set_operation_bit(provider, operation_id);
  83. }
  84. static void ossl_method_construct_this(OSSL_PROVIDER *provider,
  85. const OSSL_ALGORITHM *algo,
  86. int no_store, void *cbdata)
  87. {
  88. struct construct_data_st *data = cbdata;
  89. void *method = NULL;
  90. if ((method = data->mcm->construct(algo, provider, data->mcm_data))
  91. == NULL)
  92. return;
  93. OSSL_TRACE2(QUERY,
  94. "ossl_method_construct_this: putting an algo to the store %p with no_store %d\n",
  95. (void *)data->store, no_store);
  96. /*
  97. * Note regarding putting the method in stores:
  98. *
  99. * we don't need to care if it actually got in or not here.
  100. * If it didn't get in, it will simply not be available when
  101. * ossl_method_construct() tries to get it from the store.
  102. *
  103. * It is *expected* that the put function increments the refcnt
  104. * of the passed method.
  105. */
  106. data->mcm->put(no_store ? data->store : NULL, method, provider, algo->algorithm_names,
  107. algo->property_definition, data->mcm_data);
  108. /* refcnt-- because we're dropping the reference */
  109. data->mcm->destruct(method, data->mcm_data);
  110. }
  111. void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
  112. OSSL_PROVIDER **provider_rw, int force_store,
  113. OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
  114. {
  115. void *method = NULL;
  116. OSSL_PROVIDER *provider = provider_rw != NULL ? *provider_rw : NULL;
  117. struct construct_data_st cbdata;
  118. /*
  119. * We might be tempted to try to look into the method store without
  120. * constructing to see if we can find our method there already.
  121. * Unfortunately that does not work well if the query contains
  122. * optional properties as newly loaded providers can match them better.
  123. * We trust that ossl_method_construct_precondition() and
  124. * ossl_method_construct_postcondition() make sure that the
  125. * ossl_algorithm_do_all() does very little when methods from
  126. * a provider have already been constructed.
  127. */
  128. cbdata.store = NULL;
  129. cbdata.force_store = force_store;
  130. cbdata.mcm = mcm;
  131. cbdata.mcm_data = mcm_data;
  132. ossl_algorithm_do_all(libctx, operation_id, provider,
  133. ossl_method_construct_precondition,
  134. ossl_method_construct_reserve_store,
  135. ossl_method_construct_this,
  136. ossl_method_construct_unreserve_store,
  137. ossl_method_construct_postcondition,
  138. &cbdata);
  139. /* If there is a temporary store, try there first */
  140. if (cbdata.store != NULL)
  141. method = mcm->get(cbdata.store, (const OSSL_PROVIDER **)provider_rw,
  142. mcm_data);
  143. /* If no method was found yet, try the global store */
  144. if (method == NULL)
  145. method = mcm->get(NULL, (const OSSL_PROVIDER **)provider_rw, mcm_data);
  146. return method;
  147. }