core_algorithm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, void *data, int *result);
  18. void (*fn)(OSSL_PROVIDER *, const OSSL_ALGORITHM *, int no_store,
  19. void *data);
  20. int (*post)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
  21. int *result);
  22. void *data;
  23. };
  24. static int algorithm_do_this(OSSL_PROVIDER *provider, void *cbdata)
  25. {
  26. struct algorithm_data_st *data = cbdata;
  27. int no_store = 0; /* Assume caching is ok */
  28. int first_operation = 1;
  29. int last_operation = OSSL_OP__HIGHEST;
  30. int cur_operation;
  31. int ok = 1;
  32. if (data->operation_id != 0)
  33. first_operation = last_operation = data->operation_id;
  34. for (cur_operation = first_operation;
  35. cur_operation <= last_operation;
  36. cur_operation++) {
  37. const OSSL_ALGORITHM *map = NULL;
  38. int ret;
  39. /* Do we fulfill pre-conditions? */
  40. if (data->pre == NULL) {
  41. /* If there is no pre-condition function, assume "yes" */
  42. ret = 1;
  43. } else {
  44. if (!data->pre(provider, cur_operation, data->data, &ret))
  45. /* Error, bail out! */
  46. return 0;
  47. }
  48. /* If pre-condition not fulfilled, go to the next operation */
  49. if (!ret)
  50. continue;
  51. map = ossl_provider_query_operation(provider, cur_operation,
  52. &no_store);
  53. if (map != NULL) {
  54. while (map->algorithm_names != NULL) {
  55. const OSSL_ALGORITHM *thismap = map++;
  56. data->fn(provider, thismap, no_store, data->data);
  57. }
  58. }
  59. ossl_provider_unquery_operation(provider, cur_operation, map);
  60. /* Do we fulfill post-conditions? */
  61. if (data->post == NULL) {
  62. /* If there is no post-condition function, assume "yes" */
  63. ret = 1;
  64. } else {
  65. if (!data->post(provider, cur_operation, no_store, data->data,
  66. &ret))
  67. /* Error, bail out! */
  68. return 0;
  69. }
  70. /* If post-condition not fulfilled, set general failure */
  71. if (!ret)
  72. ok = 0;
  73. }
  74. return ok;
  75. }
  76. void ossl_algorithm_do_all(OSSL_LIB_CTX *libctx, int operation_id,
  77. OSSL_PROVIDER *provider,
  78. int (*pre)(OSSL_PROVIDER *, int operation_id,
  79. void *data, int *result),
  80. void (*fn)(OSSL_PROVIDER *provider,
  81. const OSSL_ALGORITHM *algo,
  82. int no_store, void *data),
  83. int (*post)(OSSL_PROVIDER *, int operation_id,
  84. int no_store, void *data, int *result),
  85. void *data)
  86. {
  87. struct algorithm_data_st cbdata = { 0, };
  88. cbdata.libctx = libctx;
  89. cbdata.operation_id = operation_id;
  90. cbdata.pre = pre;
  91. cbdata.fn = fn;
  92. cbdata.post = post;
  93. cbdata.data = data;
  94. if (provider == NULL)
  95. ossl_provider_doall_activated(libctx, algorithm_do_this, &cbdata);
  96. else
  97. algorithm_do_this(provider, &cbdata);
  98. }