core_algorithm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <openssl/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include "internal/core.h"
  12. #include "internal/property.h"
  13. #include "internal/provider.h"
  14. struct algorithm_data_st {
  15. OSSL_LIB_CTX *libctx;
  16. int operation_id; /* May be zero for finding them all */
  17. int (*pre)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
  18. int *result);
  19. int (*reserve_store)(int no_store, void *data);
  20. void (*fn)(OSSL_PROVIDER *, const OSSL_ALGORITHM *, int no_store,
  21. void *data);
  22. int (*unreserve_store)(void *data);
  23. int (*post)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
  24. int *result);
  25. void *data;
  26. };
  27. /*
  28. * Process one OSSL_ALGORITHM array, for the operation |cur_operation|,
  29. * by constructing methods for all its implementations and adding those
  30. * to the appropriate method store.
  31. * Which method store is appropriate is given by |no_store| ("permanent"
  32. * if 0, temporary if 1) and other data in |data->data|.
  33. *
  34. * Returns:
  35. * -1 to quit adding algorithm implementations immediately
  36. * 0 if not successful, but adding should continue
  37. * 1 if successful so far, and adding should continue
  38. */
  39. static int algorithm_do_map(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *map,
  40. int cur_operation, int no_store, void *cbdata)
  41. {
  42. struct algorithm_data_st *data = cbdata;
  43. int ret = 0;
  44. if (!data->reserve_store(no_store, data->data))
  45. /* Error, bail out! */
  46. return -1;
  47. /* Do we fulfill pre-conditions? */
  48. if (data->pre == NULL) {
  49. /* If there is no pre-condition function, assume "yes" */
  50. ret = 1;
  51. } else if (!data->pre(provider, cur_operation, no_store, data->data,
  52. &ret)) {
  53. /* Error, bail out! */
  54. ret = -1;
  55. goto end;
  56. }
  57. /*
  58. * If pre-condition not fulfilled don't add this set of implementations,
  59. * but do continue with the next. This simply means that another thread
  60. * got to it first.
  61. */
  62. if (ret == 0) {
  63. ret = 1;
  64. goto end;
  65. }
  66. if (map != NULL) {
  67. const OSSL_ALGORITHM *thismap;
  68. for (thismap = map; thismap->algorithm_names != NULL; thismap++)
  69. data->fn(provider, thismap, no_store, data->data);
  70. }
  71. /* Do we fulfill post-conditions? */
  72. if (data->post == NULL) {
  73. /* If there is no post-condition function, assume "yes" */
  74. ret = 1;
  75. } else if (!data->post(provider, cur_operation, no_store, data->data,
  76. &ret)) {
  77. /* Error, bail out! */
  78. ret = -1;
  79. }
  80. end:
  81. data->unreserve_store(data->data);
  82. return ret;
  83. }
  84. /*
  85. * Given a provider, process one operation given by |data->operation_id|, or
  86. * if that's zero, process all known operations.
  87. * For each such operation, query the associated OSSL_ALGORITHM array from
  88. * the provider, then process that array with |algorithm_do_map()|.
  89. */
  90. static int algorithm_do_this(OSSL_PROVIDER *provider, void *cbdata)
  91. {
  92. struct algorithm_data_st *data = cbdata;
  93. int first_operation = 1;
  94. int last_operation = OSSL_OP__HIGHEST;
  95. int cur_operation;
  96. int ok = 1;
  97. if (data->operation_id != 0)
  98. first_operation = last_operation = data->operation_id;
  99. for (cur_operation = first_operation;
  100. cur_operation <= last_operation;
  101. cur_operation++) {
  102. int no_store = 0; /* Assume caching is ok */
  103. const OSSL_ALGORITHM *map = NULL;
  104. int ret = 0;
  105. map = ossl_provider_query_operation(provider, cur_operation,
  106. &no_store);
  107. ret = algorithm_do_map(provider, map, cur_operation, no_store, data);
  108. ossl_provider_unquery_operation(provider, cur_operation, map);
  109. if (ret < 0)
  110. /* Hard error, bail out immediately! */
  111. return 0;
  112. /* If post-condition not fulfilled, set general failure */
  113. if (!ret)
  114. ok = 0;
  115. }
  116. return ok;
  117. }
  118. void ossl_algorithm_do_all(OSSL_LIB_CTX *libctx, int operation_id,
  119. OSSL_PROVIDER *provider,
  120. int (*pre)(OSSL_PROVIDER *, int operation_id,
  121. int no_store, void *data, int *result),
  122. int (*reserve_store)(int no_store, void *data),
  123. void (*fn)(OSSL_PROVIDER *provider,
  124. const OSSL_ALGORITHM *algo,
  125. int no_store, void *data),
  126. int (*unreserve_store)(void *data),
  127. int (*post)(OSSL_PROVIDER *, int operation_id,
  128. int no_store, void *data, int *result),
  129. void *data)
  130. {
  131. struct algorithm_data_st cbdata = { 0, };
  132. cbdata.libctx = libctx;
  133. cbdata.operation_id = operation_id;
  134. cbdata.pre = pre;
  135. cbdata.reserve_store = reserve_store;
  136. cbdata.fn = fn;
  137. cbdata.unreserve_store = unreserve_store;
  138. cbdata.post = post;
  139. cbdata.data = data;
  140. if (provider == NULL) {
  141. ossl_provider_doall_activated(libctx, algorithm_do_this, &cbdata);
  142. } else {
  143. OSSL_LIB_CTX *libctx2 = ossl_provider_libctx(provider);
  144. /*
  145. * If a provider is given, its library context MUST match the library
  146. * context we're passed. If this turns out not to be true, there is
  147. * a programming error in the functions up the call stack.
  148. */
  149. if (!ossl_assert(ossl_lib_ctx_get_concrete(libctx)
  150. == ossl_lib_ctx_get_concrete(libctx2)))
  151. return;
  152. cbdata.libctx = libctx2;
  153. algorithm_do_this(provider, &cbdata);
  154. }
  155. }
  156. char *ossl_algorithm_get1_first_name(const OSSL_ALGORITHM *algo)
  157. {
  158. const char *first_name_end = NULL;
  159. size_t first_name_len = 0;
  160. char *ret;
  161. if (algo->algorithm_names == NULL)
  162. return NULL;
  163. first_name_end = strchr(algo->algorithm_names, ':');
  164. if (first_name_end == NULL)
  165. first_name_len = strlen(algo->algorithm_names);
  166. else
  167. first_name_len = first_name_end - algo->algorithm_names;
  168. ret = OPENSSL_strndup(algo->algorithm_names, first_name_len);
  169. return ret;
  170. }