core_fetch.c 3.6 KB

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