core_algorithm.c 6.1 KB

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