core_fetch.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2019-2020 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. OPENSSL_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 ossl_method_construct_precondition(OSSL_PROVIDER *provider,
  24. int operation_id, void *cbdata,
  25. int *result)
  26. {
  27. if (!ossl_assert(result != NULL)) {
  28. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  29. return 0;
  30. }
  31. if (!ossl_provider_test_operation_bit(provider, operation_id, result))
  32. return 0;
  33. /*
  34. * The result we get tells if methods have already been constructed.
  35. * However, we want to tell whether construction should happen (true)
  36. * or not (false), which is the opposite of what we got.
  37. */
  38. *result = !*result;
  39. return 1;
  40. }
  41. static int ossl_method_construct_postcondition(OSSL_PROVIDER *provider,
  42. int operation_id, int no_store,
  43. void *cbdata, int *result)
  44. {
  45. if (!ossl_assert(result != NULL)) {
  46. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  47. return 0;
  48. }
  49. *result = 1;
  50. return no_store != 0
  51. || ossl_provider_set_operation_bit(provider, operation_id);
  52. }
  53. static void ossl_method_construct_this(OSSL_PROVIDER *provider,
  54. const OSSL_ALGORITHM *algo,
  55. int no_store, void *cbdata)
  56. {
  57. struct construct_data_st *data = cbdata;
  58. void *method = NULL;
  59. if ((method = data->mcm->construct(algo, provider, data->mcm_data))
  60. == NULL)
  61. return;
  62. /*
  63. * Note regarding putting the method in stores:
  64. *
  65. * we don't need to care if it actually got in or not here.
  66. * If it didn't get in, it will simply not be available when
  67. * ossl_method_construct() tries to get it from the store.
  68. *
  69. * It is *expected* that the put function increments the refcnt
  70. * of the passed method.
  71. */
  72. if (data->force_store || !no_store) {
  73. /*
  74. * If we haven't been told not to store,
  75. * add to the global store
  76. */
  77. data->mcm->put(data->libctx, NULL, method, provider,
  78. data->operation_id, algo->algorithm_names,
  79. algo->property_definition, data->mcm_data);
  80. }
  81. data->mcm->put(data->libctx, data->store, method, provider,
  82. data->operation_id, algo->algorithm_names,
  83. algo->property_definition, data->mcm_data);
  84. /* refcnt-- because we're dropping the reference */
  85. data->mcm->destruct(method, data->mcm_data);
  86. }
  87. void *ossl_method_construct(OPENSSL_CTX *libctx, int operation_id,
  88. int force_store,
  89. OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
  90. {
  91. void *method = NULL;
  92. if ((method = mcm->get(libctx, NULL, mcm_data)) == NULL) {
  93. struct construct_data_st cbdata;
  94. /*
  95. * We have a temporary store to be able to easily search among new
  96. * items, or items that should find themselves in the global store.
  97. */
  98. if ((cbdata.store = mcm->alloc_tmp_store(libctx)) == NULL)
  99. goto fin;
  100. cbdata.libctx = libctx;
  101. cbdata.operation_id = operation_id;
  102. cbdata.force_store = force_store;
  103. cbdata.mcm = mcm;
  104. cbdata.mcm_data = mcm_data;
  105. ossl_algorithm_do_all(libctx, operation_id, NULL,
  106. ossl_method_construct_precondition,
  107. ossl_method_construct_this,
  108. ossl_method_construct_postcondition,
  109. &cbdata);
  110. method = mcm->get(libctx, cbdata.store, mcm_data);
  111. mcm->dealloc_tmp_store(cbdata.store);
  112. }
  113. fin:
  114. return method;
  115. }