core_fetch.c 5.7 KB

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