core_fetch.c 5.5 KB

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