core_fetch.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2019 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 void ossl_method_construct_this(OSSL_PROVIDER *provider,
  24. const OSSL_ALGORITHM *algo,
  25. int no_store, void *cbdata)
  26. {
  27. struct construct_data_st *data = cbdata;
  28. void *method = NULL;
  29. if ((method = data->mcm->construct(algo, provider, data->mcm_data))
  30. == NULL)
  31. return;
  32. /*
  33. * Note regarding putting the method in stores:
  34. *
  35. * we don't need to care if it actually got in or not here.
  36. * If it didn't get in, it will simply not be available when
  37. * ossl_method_construct() tries to get it from the store.
  38. *
  39. * It is *expected* that the put function increments the refcnt
  40. * of the passed method.
  41. */
  42. if (data->force_store || !no_store) {
  43. /*
  44. * If we haven't been told not to store,
  45. * add to the global store
  46. */
  47. data->mcm->put(data->libctx, NULL, method, provider,
  48. data->operation_id, algo->algorithm_names,
  49. algo->property_definition, data->mcm_data);
  50. }
  51. data->mcm->put(data->libctx, data->store, method, provider,
  52. data->operation_id, algo->algorithm_names,
  53. algo->property_definition, data->mcm_data);
  54. /* refcnt-- because we're dropping the reference */
  55. data->mcm->destruct(method, data->mcm_data);
  56. }
  57. void *ossl_method_construct(OPENSSL_CTX *libctx, int operation_id,
  58. int force_store,
  59. OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
  60. {
  61. void *method = NULL;
  62. if ((method = mcm->get(libctx, NULL, mcm_data)) == NULL) {
  63. struct construct_data_st cbdata;
  64. /*
  65. * We have a temporary store to be able to easily search among new
  66. * items, or items that should find themselves in the global store.
  67. */
  68. if ((cbdata.store = mcm->alloc_tmp_store(libctx)) == NULL)
  69. goto fin;
  70. cbdata.libctx = libctx;
  71. cbdata.operation_id = operation_id;
  72. cbdata.force_store = force_store;
  73. cbdata.mcm = mcm;
  74. cbdata.mcm_data = mcm_data;
  75. ossl_algorithm_do_all(libctx, operation_id, NULL,
  76. ossl_method_construct_this, &cbdata);
  77. method = mcm->get(libctx, cbdata.store, mcm_data);
  78. mcm->dealloc_tmp_store(cbdata.store);
  79. }
  80. fin:
  81. return method;
  82. }